Created
November 6, 2013 08:22
-
-
Save benvd/7332668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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