Programmatically change Mac display brightness
Asked Answered
F

4

17

How to change Mac display brightness from cocoa application?

Fitted answered 13/7, 2010 at 17:30 Comment(3)
Would this be on a Macintosh itself, or an iP*?Unicef
Mac itself (more specific — Mac Book Pro), I already found thread here telling that it is impossible to do this on iP*.Fitted
I released a shell utility to do it here: github.com/pirate/mac-keyboard-brightnessLymphangial
D
15

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)

Debark answered 9/2, 2014 at 3:53 Comment(1)
compiler throws error: "Value of type 'CFMutableDictionary' has no member 'takeUnretainedValue'" in second line of 'let result...'Kolkhoz
E
6

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)
}
Elastomer answered 29/7, 2016 at 22:33 Comment(0)
D
3

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;
    }
}       

}
Dermatosis answered 5/1, 2011 at 10:21 Comment(1)
There is already a link to page where I can find all source code.Fitted
H
2

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)
Hebel answered 16/8, 2016 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.