Hammerspoon / hammerspoon

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

screen count when mirroring displays #168

Open marksantcroos opened 9 years ago

marksantcroos commented 9 years ago

Hi,

Im using hs.screens to detect the connection of an external screen and act upon that. However, in mirrored mode (which I use) the number of screens is 1.

At least the documentation should be changed that screens != monitors ;-)

It seems that I get two notifications though through the watcher, and after the 2nd one, the name of "the" screen becomes the name of my external screen.

I can probably work with the latter, but not sure how reliable/universal this is.

Anybody familiar with the underlying library and knows how this could be extended?

Thanks!

asmagill commented 9 years ago

Good to know about the mirrored mode -- I don't use it much myself as my external monitor differs greatly in size and looks horrible at the built-in's resolution...

The docs should also probably reflect that a screen change can simply be moving a monitor in the Arrangement section of the Display's preferences panel (in fact, that's how I tested initially when porting the watcher over).

I do recall in my config (https://github.com/asmagill/hammerspoon-config under utils/_actions) I added code to capture the number of displays on load and then check to see if it changed, rather than just assume a watcher invocation meant specifically what I wanted to capture.

I've found some code for command line tools to rearrange monitors and toggle mirrored mode that I plan to delve into and see if the same code can easily be added into either hs.screens or a new module -- perhaps that will provide a more accurate way to distinguish between multiple monitors and multiple displays.

marksantcroos commented 9 years ago

For me it happens to work because of the "raw" res of the retina screen is similar to my external res.

Note that I managed to work around the "double notification" with a small wrapper in Lua and now have a switch detector that works reliably for me and I can use to trigger other actions.

cmsj commented 9 years ago

FWIW, I tend to take the view that the C portions of our extensions should always just be emitting whatever is coming from the OS, even if that means double notifications. Where we can really add value is with the Lua portions of our extensions. Over time as we write wrappers ourselves that track state and react only when something has actually changed, we should try to fold those back into the extensions so everyone else can stand on our shoulders and not have to rewrite the same wrappers for themselves. I always encourage everyone to share their configs (there's a wiki page on our github project for that) and propose their wrappers for inclusion :)

marksantcroos commented 9 years ago

@cmsj Agreed. I saw the ticket on publishing configs and I'm working on cleaning and pushing my configs to github as we speak :-)

cmsj commented 9 years ago

We should be able to add some API to make this easier to figure out - the display server knows whether a monitor is mirrored or not, and differentiates between displays that are Online and ones that are Active.

adidoes commented 1 month ago

lol, i just ran into this issue almost 10 years later. i mirror my screens and i want to turn the macbook display brightness to 0 when i connect to an external display.

von commented 3 weeks ago

An implication of this is that as far as I can tell there is no way to tell if one has a mirrored display attached. I.e. I can't find a way to tell if I have no external display attached to my laptop or have a single external display attached that is mirroring my laptop screen.

What I'd like to be able to do is to use the mirrorOf() and mirrorStop() methods to implement mirror toggling (similar to the mirror-displays -t option), for which it would be nice to be able to tell if I'm mirroring or not. I don't see a robust way of doing that right now (I see I can imperfectly infer it form the number of screens or perhaps doing a "pythonic" try mirroring/un-mirroring and see if it fails.)

von commented 1 day ago

Both mirrorOf() and mirrorStop() always seem to return true meaning you can't use the functions to determine mirror state since they don't give you any programmatic clue as to state or changes to it.