Skip to content

Instantly share code, notes, and snippets.

@krzys-h
Last active October 14, 2022 10:47
Show Gist options
  • Save krzys-h/c2c83e784cc61e848e861a9e556739fe to your computer and use it in GitHub Desktop.
Save krzys-h/c2c83e784cc61e848e861a9e556739fe to your computer and use it in GitHub Desktop.
[polybar] Start polybar on all monitors and restart on monitor configuration change
[barsettings]
monitor = ${env:MONITOR:}
; ... any other global settings for all bars ...
[bar/top]
inherit=barsettings
bottom=false
modules-left = ...
modules-center = ...
modules-right = ...
[bar/top-primary]
inherit=bar/top
; you can only have the tray on one of the monitors
tray-position = right
; feel free to also override the modules on non-primary displays if you want
[bar/bottom]
inherit=barsettings
bottom=true
modules-left = ...
modules-center = ...
modules-right = ...
[bar/bottom-primary]
inherit=bar/bottom
[settings]
; disable the builtin XCB_RANDR_SCREEN_CHANGE_NOTIFY handling
screenchange-reload = false
exec --no-startup-id $HOME/.config/polybar/launch_on_monitor_change.py
#!/bin/bash
# Terminate already running bar instances
killall -q polybar
# Wait until the processes have been shut down
while pgrep -u $UID -x polybar >/dev/null; do sleep 1; done
# Launch Polybar, using default config location ~/.config/polybar/config
polybar --list-monitors | while read -r line; do
m=$(echo $line | cut -d":" -f1)
if [[ "$line" == *"(primary)"* ]]; then
MONITOR=$m polybar --reload bottom-primary &
MONITOR=$m polybar --reload top-primary &
else
MONITOR=$m polybar --reload bottom &
MONITOR=$m polybar --reload top &
fi
done
echo "Polybar launched..."
#!/usr/bin/python3
import xcffib
from xcffib.xproto import *
import xcffib.randr
import os
conn = xcffib.connect()
setup = conn.get_setup()
root = setup.roots[0].root
randr = conn(xcffib.randr.key)
randr.SelectInput(root, xcffib.randr.NotifyMask.ScreenChange)
conn.flush()
os.system("/home/krzys_h/.config/polybar/launch.sh")
while True:
event = conn.wait_for_event()
if isinstance(event, xcffib.randr.ScreenChangeNotifyEvent):
os.system("/home/krzys_h/.config/polybar/launch.sh")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment