Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.07k stars 583 forks source link

Document shortcomings of hs.brightness #1641

Open hnry opened 6 years ago

hnry commented 6 years ago

Not working as _I_ expect, not sure if it's working as intended. But should have a note in API docs to clear up confusion if it's suppose to be this way.

I expected it to be exactly the same as if I was interacting with the brightness keys or manually adjusting the brightness slider in OS X.

What it actually does for me is just set the brightness correctly via hardware it looks like but ends up being 'temporarily'. But OS X isn't informed, and it's own brightness controls kick in and 'fixes' the value I set.

For example, I via OS X key or slider set the brightness very low. Run hs.brightness.set(70), I see it light up, then immediately the display dims back to very low on it's own. When set to 70 for that brief moment the OS X brightness slider never moves.

If that's not the intention for this, then maybe a note in the docs to explain what the purpose of it is? Because right now I can't think of a usefulness for it's current functionality as setting a new brightness value is only temporary.

MacBook Pro 13" retina 2015 OSX 10.13.2 (high sierra) With / without external monitor tested; same result.

jvzr commented 6 years ago

@hnry I had the same problem but found a fix. In System Preferences > Energy Saver > Battery, uncheck “Slightly dim the display while on battery power”. It worked instantly as soon as I clicked the check box 😄

cmsj commented 6 years ago

Unfortunately I'm not aware of a way for us to set the brightness in a way that macOS will recognise. Disabling things like the battery dim and also the automatic brightness, may well help, in that macOS will be less likely to set the brightness itself.

We could certainly document that more clearly though!

latenitefilms commented 6 years ago

@cmsj - Is this of any relevance? https://github.com/Bensge/NativeDisplayBrightness

oschrenk commented 5 years ago

Can confirm that unticking “Slightly dim the display while on battery power” works. The thing that was so confusing to me is that hs.brightness.set(70) works as expected from the Hammerspoon console but not in the script (even with a checkmark in mentioned checkbox)

GregHilston commented 4 years ago

As of writing this, I'm unable to reproduce the success experienced by unchecking

Slightly dim the display while on battery power

Relevant code:

function alternateLaptopScreenBrightness()
  if hs.brightness.get() == 0 then
    hs.brightness.set(100)
  else
    hs.brightness.set(0)
  end
end

Any update on this?

apeschar commented 2 years ago

While hs.brightness.set(100) doesn't appear to work, this does:

function setBrightness(v)
    for i, screen in ipairs(hs.screen.allScreens()) do
        screen:setBrightness(v)
    end
end

This is on MacOS Big Sur 11.6.2 with Hammerspoon 0.9.93.

GregHilston commented 2 years ago

I don't have a Mac anymore, so I cannot verify this on my system.

Can you document which version of osx and hamerspoon you're running?

GregHilston commented 2 years ago

I have access to an Intel OSX machine again and was setting up Hammerspoon when I noticed the brightness function above doesn't work as expected. When I hit google, I stumbled upon this very thread that I was apart of not to long ago... haha

I'm on OS X 12.3.1 (Monterey) using Hammerspoon 0.9.97 and have been unable to successfully modify the brightness using either of these two functions on my active Hammerspoon init.lua

I am able to modify the Laptop's monitor's brightness using a function like this:

function alternateLaptopScreenBrightness()
  if hs.brightness.get() == 0 then
    hs.brightness.set(100)
  else
    hs.brightness.set(0)
  end
end

I have been unable to loop over my three externally plugged in monitors and successfully modify their brightness

function testBrightnessChange()
    currentScreenWithFocus = hs.screen.mainScreen()

    for i, screen in ipairs(hs.screen.allScreens()) do
        -- We only want to modify the brightness of the non-focused monitors, so we can focus.
        if (currentScreenWithFocus ~= screen) then
            hs.alert.show(string.format("attempting to modify brightness of screen %s", screen))
            screen:setBrightness(20)
        end
    end
end

I know its been quite some time, but does anyone have any ideas why this functionality might not be working? I'm pretty sure that this is related to this closed issue, where they discuss external monitors' brightness not being able to be controlled.

I've learned that hs.brightness.ambient() returns -1 which the documents state, that that is an error...

drn commented 2 years ago

I've never had success modifying my external monitor's brightness, though I dug into it a while ago. IIRC, I gave up with the reason that my external monitor just didn't have the interface for OSX to control its brightness. The way I confirmed that was by going to System Preferences and seeing if there was a Brightness slider for that monitor. In my case, there was not.

Probably not the answer you were looking for, but hope this helps save you some time!

Relevant note from https://github.com/nriley/brightness

If you cannot control your display’s brightness from Displays System Preferences, you will not be able to do it with brightness. See https://github.com/nriley/brightness/issues/11 for more information and some potential other options.

Does it work through the brightness CLI?

GregHilston commented 2 years ago

Very good to know, thanks for sharing this. Such an unfortunate situation but seemingly nothing I can do, or nothing I'm willing to explore further ha

On Mon, Apr 18, 2022, 1:16 PM Darren Cheng @.***> wrote:

I've never had success modifying my external monitor's brightness, though I dug into it a while ago. IIRC, I gave up with the reason that my external monitor just didn't have the interface for OSX to control its brightness. The way I confirmed that was by going to System Preferences and seeing if there was a Brightness slider for that monitor. In my case, there was not.

Probably not the answer you were looking for, but hope this helps save you some time!

— Reply to this email directly, view it on GitHub https://github.com/Hammerspoon/hammerspoon/issues/1641#issuecomment-1101578738, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAIFQH4XYX3YIWHATTXRRLVFWKEZANCNFSM4EKISQ3Q . You are receiving this because you commented.Message ID: @.***>

drn commented 2 years ago

I had the same thought process. Too bad!

GregHilston commented 2 years ago

:/

If I come up with a solution or perhaps an alternative solution, I'll post here you update you and future readers ha

On Mon, Apr 18, 2022, 1:38 PM Darren Cheng @.***> wrote:

I had the same thought process. Too bad!

— Reply to this email directly, view it on GitHub https://github.com/Hammerspoon/hammerspoon/issues/1641#issuecomment-1101595766, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAIFQDSHVFJMIUR7MNPDGLVFWMXXANCNFSM4EKISQ3Q . You are receiving this because you commented.Message ID: @.***>

orval commented 2 years ago

Just in case this is useful, here is an application that can control brightness on an external display: https://github.com/MonitorControl/MonitorControlLite

Digging about in the code, I found this function being used: https://developer.apple.com/documentation/coregraphics/1456604-cgsetdisplaytransferbytable