Guest User

Untitled

a guest
Sep 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os, sys, time
  4.  
  5. def find_next_free_x_display(max_tries = 10):
  6. """
  7. Find the next free X Display by
  8. investigating the Xlocks in /tmp
  9. """
  10. for i in range(max_tries):
  11. try_path = "/tmp/.X" + str(i) + "-lock"
  12. try:
  13. with open(try_path,"r"):
  14. pass
  15. except:
  16. return i
  17. else:
  18. return -1
  19.  
  20. def usage():
  21. """
  22. Print silly usage
  23. """
  24. print("Usage: {0} [-t <sec>] <appname> or when using wine: {0} wine <appname>".format(sys.argv[0]))
  25.  
  26. def check(cond,msg,exit_rc):
  27. """
  28. Abort if condition is false
  29. """
  30. if cond:
  31. print(msg)
  32. usage()
  33. sys.exit(exit_rc)
  34.  
  35. if __name__ == "__main__":
  36. if len(sys.argv) < 2 :
  37. usage()
  38. else:
  39. timeout = 0
  40. command = sys.argv[1:]
  41.  
  42. # Parse -t option
  43. if sys.argv[1] == "-t":
  44. check(len(sys.argv) < 3,"-t needs an argument, Sir.",-1)
  45. check(len(sys.argv) < 4,"You should also add a command behind -t, Sir.",-2)
  46.  
  47. try: timeout = int(sys.argv[2])
  48. except: check(True,"Cannot convert '" + sys.argv[2] + "' to an integer, Sir.",-3)
  49.  
  50. command = sys.argv[3:]
  51.  
  52. # Count down if desired
  53. if timeout > 0:
  54. print("Will start app in ",end='')
  55. for second in range(timeout):
  56. print(str(timeout - second) + "..", end='')
  57. sys.stdout.flush()
  58. time.sleep(1)
  59. print()
  60.  
  61. display = find_next_free_x_display()
  62. if display != -1:
  63. os.system("xinit $(which " + command[0] + ")" + ' '.join(command[1:]) + " -- :" + str(display))
  64. else:
  65. print("Sorry, couldn't find a free X display")
Add Comment
Please, Sign In to add comment