Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import subprocess
  3. import time
  4.  
  5. #--- set both commands (connect / disconnect) below
  6. connect_command = "xrandr --output DP-2 --pos 0x0 --mode 1920x1200 "\
  7. "&& xrandr --output HDMI-0 --pos 1920x0 --mode 1920x1200 "\
  8. "&& xrandr --output DP-3 --pos 3840x0 --mode 1920x1200 "\
  9. "&& xrandr --output eDP-1-1 --off"
  10.  
  11. disconnect_command = ""
  12. #---
  13.  
  14. while True:
  15. time.sleep(5)
  16. try:
  17. subprocess.Popen(["xrandr"])
  18. except:
  19. pass
  20. else:
  21. break
  22.  
  23.  
  24. # function to get the output of xrandr
  25. def get(cmd): return subprocess.check_output(cmd).decode("utf-8")
  26. # - to count the occurrenc of " connected "
  27. def count_screens(xr): return xr.count(" connected ")
  28. # - to run the connect / disconnect command(s)
  29. def run_command(cmd): subprocess.Popen(["/bin/bash", "-c", cmd])
  30.  
  31. # first count
  32. xr1 = None
  33.  
  34. while True:
  35. time.sleep(5)
  36. # second count
  37. xr2 = count_screens(get(["xrandr"]))
  38. # check if there is a change in the screen state
  39. if xr2 != xr1:
  40. if xr2 == 4:
  41. # command to run if connected (two screens)
  42. time.sleep(2)
  43. run_command(connect_command)
  44. elif xr2 == 1:
  45. # command to run if disconnected (one screen)
  46. # uncomment run_command(disconnect_command) to enable, then also comment out pass
  47. pass
  48. # run_command(disconnect_command)
  49. # set the second count as initial state for the next loop
  50. xr1 = xr2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement