Hammerspoon / hammerspoon

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

Changing laptop screen resolution via HS locks up computer (when I have 5k UltraFine screen attached) #1192

Open BrianGilbert opened 7 years ago

BrianGilbert commented 7 years ago

Delivered today and just setup the 5k UltraFine screen looks pretty sweet, however when I resize my laptop screen I get a horrible lockup that can only be cleared by an NVRAM reset.

Here is the relevant part of my HS setup for changing resolution:

--
-- Screen resolution management
--
local function setres(direction)
    local screen = hs.screen.mainScreen()
    local modes = screen:availableModes()
    local currentMode = screen:currentMode()
    local currentKey = tonumber(currentMode.w .. '.' .. currentMode.h)

    alert.closeAll()

    local modes_rekeyed = {}
    for key, values in pairs(modes) do
        new_key = tonumber(values.w .. '.' .. values.h)
        modes_rekeyed[new_key] = values
    end

    local ordered_keys = {}
    for k in pairs(modes_rekeyed) do
        table.insert(ordered_keys, k)
    end
    table.sort(ordered_keys);
    local last = nil
    for i = 1, #ordered_keys do
        local k, v = ordered_keys[i], modes_rekeyed[ordered_keys[i]]
        if direction == 'up' and currentKey == last then
            screen:setMode(v.w, v.h, v.scale)
            alert.show(v.w .. ' x ' .. v.h)
            break
        elseif direction == 'down' and currentKey == k then
            v = modes_rekeyed[last]
            screen:setMode(v.w, v.h, v.scale)
            alert.show(v.w .. ' x ' .. v.h)
            break
        end
        last = k
    end
end

hotkey.bind(mash, "=", function() setres('up') end)
hotkey.bind(mash, "-", function() setres('down') end)
--
-- /Screen resolution management
--

Here is an image of what happens when the lockup ocurrs, the graphic glitching can be on either screen, but all input is locked and only way to get control again is a hard reset, the only way to be able to plug the screen in again without the lockup ocurring is an NVRAM reset. concerning

cmsj commented 7 years ago

Would you be able to test some other third-party screen resolution changing tools, such as SetResX?

BrianGilbert commented 7 years ago

@cmsj I've just tested with Retina Display Menu (updated version of SetResX) which I grabbed from the link at https://www.reddit.com/r/apple/comments/vi9yf/set_your_retina_macbook_pros_resolution_to/

And the graphics glitching and crash that I'm getting with HS do not ocurr with it.

cmsj commented 7 years ago

Ok great, that gives us a useful baseline. I'll try and dig into it soon, but the next week or two are pretty crazy for me.

cmsj commented 7 years ago

Well this is rather odd. The app includes source and as far as I can see, they are setting the display mode with exactly the same API calls as us, but this is all private API - the public API for doing this in CoreGraphics still doesn't support retina modes :(

BrianGilbert commented 7 years ago

This has happened to me again, but this time an NVRAM reset doesn't resolve the issue.

I've tested doing a clean inastll of Mac OS on an external drive and the screen works again after that, but I really don't want to have to reinstall my onboard drive if an alternative fix can be found.

Is there any way to find out what files get changed when I change screen resolutions?