
Show HN: Change screen layout when connecting/disconnecting monitors - jlpc
https://github.com/jlpc/xoutputd
======
raphman_
For reference, there's also autorandr [1] and x-on-resize [2].

[1]
[https://github.com/wertarbyte/autorandr](https://github.com/wertarbyte/autorandr)

[2] [http://keithp.com/blogs/x-on-resize/](http://keithp.com/blogs/x-on-
resize/)

~~~
wlk
This is a good documentation for autorandr:
[https://help.ubuntu.com/community/DynamicMultiMonitor](https://help.ubuntu.com/community/DynamicMultiMonitor)

------
larkery
I have a little python script to do a similar thing:

[https://github.com/larkery/nix-
session/blob/master/scripts/d...](https://github.com/larkery/nix-
session/blob/master/scripts/displays)

This remembers the randr state that's associated with plugged in displays by
their EDID and the ACPI lid switch. When you change the randr state, it
remembers it, and when you change what displays are connected or open the
laptop lid, it loads a previously remembered state if there is one. The state
saving and loading is done by arandr, which handles the resolution, position,
and choice of primary output.

~~~
betimsl
I used a bash script to do this, until I got a laptop that has only a mini-
display port. Now I don't/can't connect to anything :(

~~~
qwertymaster
Why? you would've needed a cable anyway, so not owning the right one is no
excuse, (m)DP can connect to pretty much anything using cheap _passive_
adapter cables.

------
qwertymaster
This is exactly what I needed, gonna put it in my window manager startup
script. I already wrote a dock and undock script for my laptop a while ago,
which I still had to run manually, so this is the perfect addition to that
script. Though I had some problems compiling it. For others who want to try
it: On some systems (gentoo here) flex replaces lex and you need to use -lfl
to link it, even though lex seems to be ususally a symlink to flex, there is
no symlink for the libraries. Also on some distributions yacc doesn't seem to
come with liby anymore, just remove the -ly from cflags.

TL;DR: Replace "LDADD+= -ll -ly -lX11 -lXrandr" with "LDADD+= -lfl -lX11
-lXrandr" to make it compile on Gentoo. Make sure yacc and flex are emerged.

~~~
jlpc
Thank you for your comment, I tested on Debian jessie, and OpenBSD only. I
will try to make the build more compatible to other distro/OS.

------
paol
May I ask what desktop environment you use, and what you usually use this for?

I ask because a good DE should remember the settings pertaining to each
monitor configuration (display positions of course, but also things like
panels placed in secondary monitors). I've been using Plasma (KDE) for about a
year and it handles this reasonably well.

~~~
mbrock
Lots of people don't use any "desktop environment." I just use ratpoison, for
example.

~~~
paol
Well, call it a "window manager" then, it still should handle this. But that's
just my opinion of the responsibilities of WMs/DEs/whatever you call them, and
I understand that some don't.

~~~
creshal
No, handling screen layout isn't the _window_ manager's responsibility. That's
what external tools like xrandr and its various GUIs are for.

------
shenal
As an i3wm user I couldn't be happier

~~~
jlpc
I'm glad it is useful for someone else.

------
rndstr
Here is my script that supports scaling up if your main monitor is hidpi while
the external one is not:

[https://github.com/rndstr/dotfiles/blob/master/bin/monitor-h...](https://github.com/rndstr/dotfiles/blob/master/bin/monitor-
hotplug)

------
mikeokner
For those on Mac, Slate will do the same thing:
[https://github.com/jigish/slate](https://github.com/jigish/slate)

~~~
mwfunk
Actually for those on the Mac, OS X will do the same thing. It tries to at
least (not 100% reliable).

~~~
mikeokner
"xoutputd is a daemon that monitors the connection state of outputs and then
executes commands based on defined rules."

I'm not aware of anything built in to OS X that allows you to execute scripts
to perform custom actions when displays are connected/disconnected. OS X just
tries to replicate the last layout you had.

* Plugged into your desk monitor? You can have Slate hide Steam and put MacVim & iTerm side by side. * Plugged into your TV? You can have Slate run a script to make sure your Plex server is up and then full-screen your browser. * Etc.

------
daveloyall
[https://en.wikipedia.org/wiki/Udev](https://en.wikipedia.org/wiki/Udev)

~~~
jlpc
I wonder why I didn't think about it when looking for a solution at my
problem.

------
andmarios
KDE does this automatically. I'd guess there is similar functionality for
other DEs too.

~~~
gtirloni
The MATE DE seems to handle this pretty well too.

