Hi,
On Fri, Jul 09, 2021 at 05:14:54PM +0300, Eyal Erez wrote:
I often use multiple displays. Here are some configurations that I've used
{.quotelead}>with my laptop:
[…]
I was wondering how others deal with this challenge? Are there other, more
well-built tools, than the script I have to do this more robustly and
automatically?
There are many ways to do this. I'll describe my setup and then point to some alternatives.
I have a script that extends xrandr command-line usage so that I can just write a generic vertical layout like so:
xrandr-smart --output 'eDP-*' --auto \
--output '!(eDP-*)' --auto --above 'eDP-*'
Then I have another script that lets me save layouts for specific configurations of connected monitors. This one is invoked automatically whenever an output is connected/disconnected, using https://xmonad.github.io/xmonad-docs/xmonad-contrib-0.16.999/XMonad-Hooks-Rescreen.html#v:addRandrChangeHook.
I can also invoke it manually via a keybinding to force a laptop-only layout, or to force an automatic multi-head layout in an unknown setup. If I need something extra (like external monitor only for games), I just invoke a "layout-extonly" script via rofi (I only need to type "ex" for it to know what I mean).
(Re)starting xmobars and trayers is handled by https://xmonad.github.io/xmonad-docs/xmonad-contrib-0.16.999/XMonad-Hooks-StatusBar.html#v:dynamicSBs, as mentioned by Yecine earlier.
In my setup, the automatic switching doesn't happen when the screen is locked, and neither do the keybindings work, but that is just my paranoia (and yeah, I should just use https://packages.debian.org/experimental/cryptsetup-suspend instead :-)). It's totally possible to let X.H.Rescreen invoke the script even if locked, and xsecurelock allows configuration of keybindings when the screen is locked.
Now for the alternatives:
Others have mentioned autorandr. That is probably what most minimalist window manager users use. Autorandr can be invoked on output (dis)connect, lid open/close and suspend/resume in various ways:
Also see https://wiki.archlinux.org/title/xrandr for other tools and tips.