Xmonad and Xmobar for laptop
Here I publish my Xmonad and Xmobar setup for the laptop. Key features of this setup is ability of the proper dynamic monitor setup: laptop screen + second HDMI connected monitor.
- Monitor can be connected and disconnected any time.
- Each monitor has its instance of Xmobar.
- Instances of the Xmobar are added/removed dynamically.
I use systemd
service for the detection of the external monitor/screen attachment/detachment.
Screenshot
Note: To obtain all necessary files, please go to my Github repo: https://github.com/alexeygumirov/xmonad-xmobar-laptop/.
Sequence (logic)
- I start all applets, notify daemon and system tray application (
trayer
) in the.xprofile
.- It also launches
displaymode
script, which detects if the laptop lid is open or closed and whether HDMI monitor is attached or not. Depending on the condition it sets up appropriate displays configuration withxrandr
.
- It also launches
- In order to detect attachment or detachment of the screen I made my own
systemd
service for this event. (See chapter below).
- Both
systemd
service anddisplaymode
script restartxmonad
process when display setup is changed becausexmonad
dynamically spawns necessary number ofxmobar
instances on the screen.
Systemd service setup
Without desktop environment automatic detection of the attachment and detachment of the external monitor does not work. I took initial idea from here: Arch Linux forum. In order to implement this, three things are needed:
- Script to detect monitor state and execute action
- Systemd service using this script
- UDEV rule which is triggered by the device event and launches this systemd service
1. The script
I created the script /usr/local/bin/hdmi-unplug
with the following content:
This script updates Xmonad or Qtile depending on the Xsession configuration.
|
|
2. The Systemd service
Then I created /etc/systemd/system/hdmi-unplug.service
file with the following content:
|
|
3. UDEV rule
And, finally, UDEV rule /etc/udev/rules.d/95-hdmi-unplug.rules
:
|
|
Xmonad.hs critical section
Full xmonad.hs
configuration you can find here. Here is just show essential part of the code.
I use XMonad.Layout.IndependentScreens
to detect number of screens.
Then I put myLogHook in a separate definition (you can check it in the configuration file). It allows to make main
part short can clean and I can easier redirect log to both xmobar
pipes when they are open.
And then in the main
function:
|
|
Each time when xmobar
is restarted, it re-detects number of displays and spawns necessary number of bars.
The only exception is mirror mode (xrandr --output eDP --primary --auto --output HDMI-A-0 --auto --same-as eDP
), then no xmobar
restart is needed because I want to keep main screen xmobar
being projected.