Skip to content

Instantly share code, notes, and snippets.

@vedantroy
Created April 11, 2021 06:03
Show Gist options
  • Save vedantroy/f64405c7e78eb710f711ae3ff31435be to your computer and use it in GitHub Desktop.
Save vedantroy/f64405c7e78eb710f711ae3ff31435be to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import os
from subprocess import Popen, PIPE, STDOUT
def log(s):
home_dir = os.path.expanduser("~")
#with open (f"{home_dir}/monitor_script.log", "a+") as f:
with open (f"/home/vedantroy/monitor_script.log", "a+") as f:
f.write(s)
xrandr = "/usr/bin/xrandr"
xrandr_cmd = Popen(xrandr, shell=True, stdout=PIPE, stderr=STDOUT)
retval = xrandr_cmd.wait()
lines = map(lambda l: l.decode('ascii'), xrandr_cmd.stdout.readlines())
if retval != 0:
nl = "\n"
log(f"xrandr returned {retval} with output:\n{nl.join(lines)}")
else:
layout_cmds = [
# No monitors plugged in
f"{xrandr} --auto",
# Thinkpad T580
# Monitor plugged into HDMI port
# Monitor to right of laptop
f"{xrandr} --output HDMI2 --primary --auto --right-of eDP1"
]
layout = 0
for line in lines:
if "HDMI2 connected" in line:
layout = 1
break
layout_cmd_str = layout_cmds[layout]
layout_cmd = Popen(layout_cmd_str, shell=True)
retval = layout_cmd.wait()
if retval != 0:
log(f"{layout_cmd_str} returnd {retval}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment