Monday, 19 June 2006
Setting display brightness
Amit Singh has posted some source code for reading the ambient light sensor on appropriately equipped Apple laptops, and changing the keyboard and display backlight brightness. He sets the display brightness with only documented APIs, which is quite a bit cleaner than the code to do so I wrote about 7 months ago. Needless to say, at the time I couldn't find the appropriate IOKit methods to do so, so I ended up reverse engineering what the display preference pane did. It's rather strange that the convoluted Objective-C interface exists, since the IOKit code actually seems shorter!
One thing my code does that Amit's doesn't is change the brightness on all connected LCDs, not just the main display (which may not be the internal LCD if you've got a laptop connected to an external monitor). This should be easy enough to merge.
(February 2014: Please use the updated version of this code on GitHub.)
I think i’m going to play with this stuff reading the ambient light sensor, but changing the brightness on my external LCD is one thing I need to *stop* doing, as any brightness except 100% creates a terrible noise on the line-out of my PowerBook (see My Blog ). It is quite annoying though to have a well dimmed laptop display and an incredibly bright external display…
Yes but it doesn’t compile on my iMac G5 20″ OS 10.4.6…
gcc -o display-brightness display-brightness.c -framework IOKit -framework ApplicationServices
ld: /usr/lib/crt1.o illegal reference to symbol: __objcInit defined in indirectly referenced dynamic library /usr/lib/libobjc.A.dylib
Anyone know how to compile it?
Dave
Try my newer code instead. It should compile fine; if not, let me know.
Hey, thanks for reply, tried that, but it errors:
$ gcc -std=c99 -o brightness brightness.c -framework IOKit -framework ApplicationServices
brightness.c:9: error: initializer element is not constant
Hope you can help again…
Dave
Found the fix:
gcc -lobjc -o display-brightness display-brightness.c -framework IOKit -framework ApplicationServices
However, it doesn’t help me as I want to increase the range of adjustment. The scale 0…1 that it uses is exactly the same scale the Mac GUI uses. Hence I can’t turn the brightness down any further than I could before! [Isn’t it ironic, don’t you think; I’m got an extra line to an Alanis Morissette song]
Guess the fix would be at a lower level below the API Amit Singh found…
Thanks Nicholas
Hrm, not sure why you got the “initializer element is not constant”. CFSTR should create a constant CFString. Maybe you’re using a different compiler version – mine is gcc version 4.0.1 (Apple Computer, Inc. build 5341). You could try adding -fconstant-cfstrings which turns on this option (though it should be on by default in all recent Xcode versions).
Anyway, if the lowest brightness is still too high, try using the display calbirator (System Preferences > Displays > Color > Calibrate) to adjust the gamma; it’ll make the display darker even if the backlight is at the same level. I had to do this with my Dell LCD.