Wednesday, April 6, 2011

Is there a way to query the current brightness level of a the MacBook LCD?

The idea being that, once the brightness passes a certain level, one could switch to a different visual scheme to give greater visibility. Also, if it could be some sort of listener type thing, that would be even better, but I'll take what I can get.

From stackoverflow
  • Uhm.. I am not a mac guy .. but does /proc exist in the filesystem? You might want to look in that virtual file directory if it exists.

    mouviciel : /proc doesn't exist on Max OS X. I don't know if some other directory plays the same role.
    strager : /proc does exist, but isn't mounted on the filesystem. You can still access it programatically. [citation-needed]
  • I believe one could look it up with IOKit. Running the ioreg command in the terminal as below gives two lines where a brightness value is visible.

    % ioreg -c AppleGraphicsControlBacklight | grep brightness
    
    | | |     "IODisplayParameters" = {"brightness"={"min"=0,"value"=408,"max"=1024},"commit"={"reg"=0}}
    | |   |     "IODisplayParameters" = {"brightness"={"min"=0,"value"=408,"max"=1024},"commit"={"reg"=0}}
    

    Maybe someone with enough IOKit knowledge could put together a sample...

  • epatel was pretty close, I just had to change the AppleGraphicsControlBacklight keyword to something else to get it to work on my macbook, so I'd guess that this is something that might change between OSX versions and/or macbook versions.

    I threw together a short ruby script to print out a little visual indicator on the command line.

    # grab the string containing the values
    brite_string = `ioreg -c AppleBacklightDisplay | grep brightness`
    
    # build a regex to match those vals
    brite_regex  = /"brightness"=\{"min"=([0-9]{1,3}),"value"=([0-9]{1,3}),"max"=([0-9]{1,3})/
    
    # match them
    match_data = brite_regex.match(brite_string)
    
    # extract the values from the match
    min = match_data[1].to_i
    val = match_data[2].to_i
    max = match_data[3].to_i
    
    # print them out nice
    puts "Current Brightness"
    print "["
    
    max.times do  |i|
      print i > val ? " " : "*"
    end
    
    puts "]"
    
    epatel : Great scripting...I thought a more C/C++/ObjC solution was actually wanted. It should be possible accessing it through IOKit then...
    Paul Wicks : Eventually, yeah, I might want something a little more connected to IOKit. This is basically just a proof of concept.

0 comments:

Post a Comment