Tuesday, 20 June 2006
Setting display brightness: updated tool
Here is my cleaned-up brightness tool, which now uses the IOKit APIs instead of the weird O3Engine SPIs that the old version did.
Quick example:
% ./brightness usage: ./brightness [-m|-d display] [-v] <brightness> or: ./brightness -l [-v] % ./brightness -l display 0: main display, ID 0x4270a80 display 0: brightness 0.734375 % ./brightness -v 0.3 display 0: brightness 0.296875
-m changes the brightness of the main display; -d changes brightness of whatever display number/ID you provide. -l -v dumps display attributes—sorry for the ugly XML output, but CoreFoundation claims to be unable to output an OpenStep-format property list even though Cocoa has no problem doing the same.
(February 2014: Please see an updated version of this code on GitHub; it makes the -v output more readable among other changes.)
hi,
i had to move the “const CFString…” (line 9) to inside of the main function to get this to compile on os x 10.4.7, xcode 2.3 (using your 1st line comment in the terminal to compile).
thanks,
-matt
When you say “Cocoa has no problem”, are you referring to -description or NSPropertyListSerialization? AFAIK, the latter simply maps to the CF functions, and will fail in the same way.
I’m just referring to -description. I don’t care about serialization, just making something human-readable.
[…] http://njr.sabi.net/2006/06/20/setting-display-brightness-updated-tool/ […]
I run this code on an intel imac with external 23 inch display. It doesn’t work for the external display :
[Session started at 2007-01-05 11:26:18 +0100.]
display 0: main display, ID 0x424a6d5
/Users/schelfd/dev/testB/bright/build/Debug/bright: failed to get brightness of display 0x424a6d5 (error -536870201)display 1: ID 0x4271600
display 1: brightness 0.000000
bright has exited with status 0.
I believe the error is : // device read locked
I tried running as root as well and changing the main display
I run 40.4.8
Yeah, I’ve had this report from someone else too. I don’t have a dual-display machine to test with unfortunately – when I first wrote it, I had access to a Power Mac G5 with two displays and it did work fine on both.
You are using an external Apple Cinema display (aluminum bezel, DVI)?
Confirmed that the IOKit-based methods do not work with Intel-based desktop systems with externally-attached Apple Cinema displays.
In my specific case, IODisplayGetFloatParameter(…, CFSTR(kIODisplayBrightnessKey), …) returns ‘kIOReturnUnsupported’ on a Mac Pro with twin 20″ Apple Cinema displays whereas it works just fine on my MacBook Pro C2D.
I’m running into this problem too; IODisplaySetFloatParameter() is returning ‘kIOReturnUnsupported’ when I have multiple displays attached.
Cheers,
Drew
With dual monitors and Intel vs. PowerPC environments, the only thing I have found to work reliably across all system configurations is the O3EngineWireProtocol using the private DisplayServices framework. More at:
http://dev.sabi.net/trac/dev/browser/trunk/LocationDo/brightness.m?rev=235
That is the case, unfortunately; that reverse-engineering time was worth it after all.
On my MacBookPro with “automatically adjust brightness as ambient light changes” checked, the brightness does change to what I set it to but then goes back to the original setting. Instead of trying to change the code of brightness, I was trying to find out how to uncheck the above checkbox, run brightness and then recheck the above checkbox all via the command line. I figure it’s defaults write com.apple.something something -bool YES/NO
Any help would be great.
Thanks.
Sorry, no idea… you’d have to twiddle some private stuff in IOKit I think; wouldn’t be as simple as setting a user default.
Well, that’s no fun. Probably why nothing useful has been showing up in any Google searches though.
Hi there!
What’s the license on this code? Could I change it and use for a little script to remember the brightness value on hackintoshes? ;)
Thanks.
BSD-ish – basically just give me credit in whatever you write based on it and I’ll be happy.
Great, thanks man!
Solved my problem, thanks. Looks like some output strings are missing newlines though; check lines 59, 79, 96, 98, 117, 133, 143. Cheers.
errexit prints a newline (see line 17). Glad it helped!
Does this work in Mountain Lion?
Is it capable of adjusting the brightness below GUI settings (1 bar with F1/F2, prefpane slider)?
Yes, brightness.c works in Mountain Lion. If you’re seeing discrete “steps” with the prefpane slider, that’s a hardware limitation; using another method to set the brightness won’t help. It really depends on the display. On some external Apple Cinema Displays I’ve tried with, you get relatively continuous brightness; on most if not all laptop displays I’ve tried, you get the same ~15 steps you get elsewhere.
Can it efectively adjust brightness between 0 and 1/16? The quarter steps with ⇧⌥F1/F2 don’t work actually, and besides I’m searching for a command line method to dim the display when working late at night.
Yes, sure. It’ll just round to the closest brightness percentage. On my MBP, a quarter step from 0 is 0.015625; a full step is 0.0625, but just as you describe, they both map to the same brightness level.
Incidentally, you could have answered these questions a lot faster by just trying it :-)
Unfortunately, i cannot get it to adjust my external display on mbp 13″ early 2011 via HDMI (on thunderbolt).
./brightness\ 2 -l
display 0: main display, ID 0x335841c1
./brightness 2: failed to get brightness of display 0x335841c1 (error -536870201)display 1: ID 0x4273140
display 1: brightness 0.752930
any ideas?
Are you able to adjust the brightness through Displays System Preferences? I’ve not had the opportunity to test with HDMI or Thunderbolt displays.
Well as you can see in my previous post, i can adjust monitor 1 (mbp display) but monitor0 (hdmi, full hd external) refuses.
So yes, i can change my internal screen’s brightness through keyboard or display prefpane but there are no such settings for an external monitor.
The brightness command line tool uses the same mechanism as the Display System Preferences pane, so it’s not going to help you in this case. I’m not sure if HDMI supports remote brightness control. Sorry!
Since part of this code is deprecated by Apple as of OSX 10.9, you may want to check out the implementation here. (https://github.com/jontaylor/DDC-CI-Tools-for-OS-X) It has no deprecated component. Compiles fine once the SDK is changed.
PPS. Hats off to any MD/PhD. As my son is part of the same program you graduated from.
[…] http://njr.sabi.net/2006/06/20/setting-display-brightness-updated-tool/ […]