Programmatically change Mac display brightness

眉间皱痕 提交于 2019-11-28 19:19:01

问题


How to change Mac display brightness from cocoa application?


回答1:


CGDisplayIOServicePort is deprecated in OS 10.9 – so you have to use IOServiceGetMatchingServices to get the service parameter for IODisplaySetFloatParameter. Here's a basic function that looks for services named "display" and changes their brightness.

- (void) setBrightnessTo: (float) level
{

    io_iterator_t iterator;
    kern_return_t result = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                                        IOServiceMatching("IODisplayConnect"),
                                                        &iterator);

    // If we were successful
    if (result == kIOReturnSuccess)
    {
        io_object_t service;
        while ((service = IOIteratorNext(iterator))) {
            IODisplaySetFloatParameter(service, kNilOptions, CFSTR(kIODisplayBrightnessKey), level);

            // Let the object go
            IOObjectRelease(service);

            return;
        }
    }
}

And in Swift (via @Dov):

private func setBrightnessLevel(level: Float) {
    var iterator: io_iterator_t = 0

    let result = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                              IOServiceMatching("IODisplayConnect").takeUnretainedValue(),
                                              &iterator)

    if result == kIOReturnSuccess {
        var service: io_object_t = 1

        for ;; {
            service = IOIteratorNext(iterator)

            if service == 0 {
                break
            }

            IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level)
            IOObjectRelease(service)
        }
    }
}

(code is open source of course)




回答2:


From Alec Jacobson's Brightness Menu source code:

- (void) set_brightness:(float) new_brightness {
CGDirectDisplayID display[kMaxDisplays];
CGDisplayCount numDisplays;
CGDisplayErr err;
err = CGGetActiveDisplayList(kMaxDisplays, display, &numDisplays);

if (err != CGDisplayNoErr)
    printf("cannot get list of displays (error %d)\n",err);
for (CGDisplayCount i = 0; i < numDisplays; ++i) {
    CGDirectDisplayID dspy = display[i];
    CFDictionaryRef originalMode = CGDisplayCurrentMode(dspy);
    if (originalMode == NULL)
        continue;
            io_service_t service = CGDisplayIOServicePort(dspy);

    float brightness;
    err= IODisplayGetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                    &brightness);
    if (err != kIOReturnSuccess) {
        fprintf(stderr,
                "failed to get brightness of display 0x%x (error %d)",
                (unsigned int)dspy, err);
        continue;
    }

    err = IODisplaySetFloatParameter(service, kNilOptions, kDisplayBrightness,
                                     new_brightness);
    if (err != kIOReturnSuccess) {
        fprintf(stderr,
                "Failed to set brightness of display 0x%x (error %d)",
                 (unsigned int)dspy, err);
        continue;
    }
}       

}



回答3:


Expanding on Alex's answer:

In Xcode8 beta3 with Swift3, the code is a lot more streamlined.

 private func setBrightness(level: Float) {
    let service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IODisplayConnect"))

    IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level)
    IOObjectRelease(service)
}



回答4:


in Xcode 8 beta 6 does not compile:

IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level)

Cannot convert value of type 'String' to expected argument type 'CFString!'

so let' cast it:

IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey as CFString!, level)


来源:https://stackoverflow.com/questions/3239749/programmatically-change-mac-display-brightness

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!