Skip to content

Instantly share code, notes, and snippets.

@sahib
Created April 10, 2012 18:11
Show Gist options
  • Save sahib/2353351 to your computer and use it in GitHub Desktop.
Save sahib/2353351 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os, sys, time
def find_next_free_x_display(max_tries = 10):
"""
Find the next free X Display by
investigating the Xlocks in /tmp
"""
for i in range(max_tries):
try_path = "/tmp/.X" + str(i) + "-lock"
try:
with open(try_path,"r"):
pass
except:
return i
else:
return -1
def usage():
"""
Print silly usage
"""
print("Usage: {0} [-t <sec>] <appname> or when using wine: {0} wine <appname>".format(sys.argv[0]))
def check(cond,msg,exit_rc):
"""
Abort if condition is false
"""
if cond:
print(msg)
usage()
sys.exit(exit_rc)
if __name__ == "__main__":
if len(sys.argv) < 2 :
usage()
else:
timeout = 0
command = sys.argv[1:]
# Parse -t option
if sys.argv[1] == "-t":
check(len(sys.argv) < 3,"-t needs an argument, Sir.",-1)
check(len(sys.argv) < 4,"You should also add a command behind -t, Sir.",-2)
try: timeout = int(sys.argv[2])
except: check(True,"Cannot convert '" + sys.argv[2] + "' to an integer, Sir.",-3)
command = sys.argv[3:]
# Count down if desired
if timeout > 0:
print("Will start app in ",end='')
for second in range(timeout):
print(str(timeout - second) + "..", end='')
sys.stdout.flush()
time.sleep(1)
print()
display = find_next_free_x_display()
if display != -1:
os.system("xinit $(which " + command[0] + ")" + ' '.join(command[1:]) + " -- :" + str(display))
else:
print("Sorry, couldn't find a free X display")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment