Skip to content

Instantly share code, notes, and snippets.

@benvd
Created November 6, 2013 08:22
Show Gist options
  • Save benvd/7332668 to your computer and use it in GitHub Desktop.
Save benvd/7332668 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
from pyudev import Context, Monitor, MonitorObserver
import subprocess
import re
import shlex
OUTPUTS = {
'LVDS1': '--mode 1600x900 --pos 0x0 --rotate normal',
'VGA1': '--mode 1920x1080 --pos 1600x0 --rotate normal'
}
def start_observer():
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='drm')
observer = MonitorObserver(monitor, callback=on_event, name='monitor-observer')
observer.daemon = False
observer.start()
def on_event(device):
if device.action == 'change':
run_xrandr()
def run_xrandr():
xrandr = subprocess.check_output('xrandr').decode('UTF-8')
command = 'xrandr'
for output in OUTPUTS:
connected = re.search(output + ' connected', xrandr) is not None
if connected:
command += ' --output %s %s' % (output, OUTPUTS[output])
else:
command += ' --output %s --off' % output
subprocess.call(shlex.split(command))
if __name__ == '__main__':
# Run xrandr based on currently connected monitors
run_xrandr()
# Then start listening for udev updates
start_observer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment