Skip to content

Instantly share code, notes, and snippets.

@sahib
Created April 10, 2012 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahib/2353174 to your computer and use it in GitHub Desktop.
Save sahib/2353174 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("Usage: {0} [-t <sec>] <appname> or when using wine: {0} wine <appname>".format(sys.argv[0]))
if __name__ == "__main__":
if len(sys.argv) < 2 :
usage()
else:
timeout = 0
opt_start = 1
if sys.argv[1] == "-t":
if len(sys.argv) < 3:
print("-t needs an argument, Sir.")
sys.exit(-1)
if len(sys.argv) < 4:
print("You should also add a command behind -t, Sir.")
sys.exit(-2)
try:
timeout = int(sys.argv[2])
except:
print("Cannot convert '" + sys.argv[2] + "' to an integer, Sir.")
sys.exit(-3)
opt_start = 3
out = ""
for x in sys.argv[(1 + opt_start):]:
out += (" " + x)
display = find_next_free_x_display()
if display != -1:
cmd = "xinit $(which " + sys.argv[opt_start] + ")" + out + " -- :" + str(display)
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()
os.system(cmd)
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