macoscocoaosx-mavericks

Get Display Brightness


Is there a way to get the display brightness in OS X 10.9+ now that CGDisplayIOServicePort has been deprecated?


Solution

  • After some searching and fiddling around, here is a "future proof" way to get the brightness of the display that doesn't use the CGDisplayIOServicePort deprecated in OS X 10.9.

    - (float)getDisplayBrightness
    {
        float brightness = 1.0f;
        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)))
            {
                IODisplayGetFloatParameter(service, 
                    kNilOptions, 
                    CFSTR(kIODisplayBrightnessKey), 
                    &brightness);
    
                // Let the object go
                IOObjectRelease(service);
            }
        }
    
        return brightness;
    }