maxmouchet / LightKit

Ambient Light Sensor, Display, and Keyboard brightness control in Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Controlling Display and Keyboard Brightness Does not function on 2016 Macbook Pro, TouchBar

jeffjet24 opened this issue · comments

Hello,
I am using your LightKit code in a project of mine, and I recently purchased a new 13" 2016 MacBook Pro, and I found that controlling the display and keyboard brightness does not work. I am receiving nil when I am initializing LightKit. This code works great on a 2015 MacBook Pro.

Here are some snippets showing how I am using your library:

Bringing Keyboard Brightness all the way down and back up:

import LightKit
func lowerAndRaiseKeyboardBrightness(){
    if let lk = LightKit() {
      // turning keyboard brightness all the way down
      for curLevel in [Int](0...10).reversed().map({Float($0) * Float(0.1)}) {
        _ = lk.setKeyboardBrightness(curLevel) // A value between 0 and 1.
        usleep(100000)
      }
      sleep(2) // waiting for the tester to evaluate
      
      // turning keyboard brightness all the way back up
      for curLevel in [Int](0...10).map({Float($0) * Float(0.1)}) {
        _ = lk.setKeyboardBrightness(curLevel) // A value between 0 and 1.
        usleep(100000)
      }
    } else {
      print("Light Sensors and Hooks not present")
    }
  }

Bringing Display Brightness all the way down and back up:

func lowerAndRaiseDisplayBrightness(){
    if let lk = LightKit() {
      // turning Display brightness all the way down
      for curLevel in [Int](0...10).reversed().map({Float($0) * Float(0.1)}) {
        _ = lk.setDisplayBrightness(curLevel) // A value between 0 and 1.
        usleep(100000)
      }
      sleep(2) // waiting for the tester to evaluate
      
      // turning Display brightness all the way back up
      for curLevel in [Int](0...10).map({Float($0) * Float(0.1)}) {
        _ = lk.setDisplayBrightness(curLevel) // A value between 0 and 1.
        usleep(100000)
      }
    } else {
      print("Light Sensors and Hooks not Present")
    }
  }

Both of my functions being are printing "Light Sensors and Hooks not Present" when they are run on my 2016 MacBook Pro with the TouchBar. When they run on my 2015 MacBook Pro, the keyboard and display brightness go down and up as expected. Both systems are running macOS 10.12.4 Beta 4.

I understand that you may not have a 2016 MacBook Pro with Touch Bar, so I would be glad to provide any information that you may need. I can get access to the 2016 MBP and take screenshots in IORegistryExplorer if that will help.

Thanks,
Mack

I would certainly be open to forking and making a PR to add support. However, I do not entirely understand how to interface with IOKit to interact with the brightness levels, or where to look to find those IO Registry locations on the 2016 MBP.

Thanks!

I have attached a screenshot from my 2016 MBP with Touch Bar in the IORegistry application looking at the keyboard. Hopefully this is helpful for you. Let me know if there is more information that I can provide. I will keep digging to provide more information to you.
screen shot 2017-03-02 at 6 10 36 pm

Thanks,
Mack

Hi,

Sorry I've been busy for the past few days. I originally found the information on this website: http://osxbook.com/book/bonus/chapter10/light/.

Do you still have access to a pre-TouchBar MB ?
If so it would be interesting to find the AppleLMUController in the IORegistry application and see if similar key/values can be found on the TouchBar MBP.

Otherwise I'll take a look on my MB but I don't have much time until next week.

Ok I see you also got the KeyboardBacklightBrightness key in your screenshot (below is on my MB). Can you check the value of the IOClass key ? Is it AppleHIDKeyboardEventDriverV2 ?

screen shot 2017-03-06 at 18 48 36

Thanks for taking the time to look at this! I understand that you may be busy.

Yeah, it appears to be AppleHIDKeyboardEventDriverV2. I tried searching for AppleLMUController on the TouchBar MBP and nothing came up in the search results.
I will take a look into that website that you linked and see if I can help figure out how to control it as well.
Thanks for your help!

Can you try by replacing AppleLMUController by AppleHIDKeyboardEventDriverV2 in the initLMUService() method ?

    /**
     Open a connection to the LMU controller.
     */
    fileprivate func initLMUService() -> Bool {
        // let serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleLMUController"))
        let serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleHIDKeyboardEventDriverV2"))
        ...

I made that change, and it still failed. However, I got a different error from that function initLMUService().
It previously had printed out: Failed to find ambient light sensor
It is now printing out: Failed to open IOService object
Because in that function, the condition kr != KERN_SUCCESS is true.

I will be able to try out doing some more troubleshooting when I get home from work today.

With the new suggested changes, the value of the serviceObject variable turns out to be 71451, and the value of the kr variable is -536870206. Quite different values than I was expecting from those two. :/

Unfortunately I cannot test on a TouchBar MBP but if it works on Linux (see "Keyboard Backlight" section of https://wiki.archlinux.org/index.php/Mac) I can have a look at how it's done in the kernel :)

According to https://github.com/Dunedan/mbp-2016-linux keyboard backlight control is not yet available on Linux.

Maybe by doing diff of all SMC keys using https://github.com/theopolis/smc-fuzzer when changing keyboard backlight we could identify the relevant key.