Skip to content

Instantly share code, notes, and snippets.

@eaceaser
Last active January 12, 2021 08:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eaceaser/f76911eee67a3282ff55813a267a10c7 to your computer and use it in GitHub Desktop.
Save eaceaser/f76911eee67a3282ff55813a267a10c7 to your computer and use it in GitHub Desktop.
Simple script to detect monitor changes
ACTION=="change", SUBSYSTEM=="drm", RUN+="/usr/bin/machinectl --uid=<YOUR-USER-NAME> shell .host /bin/bash -c '/bin/systemctl start --no-block --user sway-monitor'"
#!/usr/bin/env python
import subprocess
import json
import time
EXTERNAL_MONITOR_NAMES = set(['DELL U2718Q', 'DELL U3011'])
outputs_str = subprocess.check_output('swaymsg -s $SWAYSOCK -r -t get_outputs', shell=True)
outputs = json.loads(outputs_str)
output_names = set(x["model"] for x in outputs)
if len(EXTERNAL_MONITOR_NAMES.intersection(output_names)) > 0:
print("Dock detected")
subprocess.check_output('swaymsg -s $SWAYSOCK output eDP-1 disable', shell=True)
else :
print("Roaming detected")
subprocess.check_output('swaymsg -s $SWAYSOCK output eDP-1 enable', shell=True)
[Unit]
Description=sway-monitor execution hook
After=sleep.target
StartLimitInterval=5
StartLimitBurst=1
[Service]
ExecStart=/usr/local/bin/sway-monitor-update.py
Type=oneshot
RemainAfterExit=false
KillMode=process
[Install]
WantedBy=sleep.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment