new tool: args2env
[hband-tools.git] / user-tools / set-xcursor-lock-and-run
blob00e7a19f88a9f1d0c312a66fb4819dbd576d9e8b
1 #!/usr/bin/env python
3 """
4 =pod
6 =head1 NAME
8 set-xcursor-lock-and-run - Set X11 cursor to a padlock and run a command
10 =cut
12 EOF
14 """
16 import sys
17 import os
18 import subprocess
19 import gtk
20 import glib
21 import threading
22 import time
23 import signal
24 gtk.gdk.threads_init()
26 xpm_cursor = [
27 "20 20 4 1 ",
28 " c None",
29 ". c white",
30 "X c black",
31 ", c yellow",
32 " ...... ",
33 " .XXXXXX. ",
34 " .XXXXXXXX. ",
35 " .XX......XX. ",
36 " .XX. .XX. ",
37 " .XX. .XX. ",
38 " .XX. .XX. ",
39 " .XX. .XX. ",
40 " ...XX. .XX... ",
41 " .XXXXXXXXXXXXXX. ",
42 " .X,,,,,,,,,,,,X. ",
43 " .X,,,,,,,,,,,,X. ",
44 " .X,,,,,,,,,,,,X. ",
45 " .X,,,,,,,,,,,,X. ",
46 " .X,,,,,XX,,,,,X. ",
47 " .X,,,,,XX,,,,,X. ",
48 " .X,,,,,XX,,,,,X. ",
49 " .X,,,,,,,,,,X. ",
50 " .XXXXXXXXXX. ",
51 " .......... ",
56 def on_start(*X):
57 #drawable = window.window
58 #drawable = None
59 #white = gtk.gdk.Color('white')
60 #black = gtk.gdk.Color('black')
61 #green = gtk.gdk.Color('green')
63 #pixmap, X = gtk.gdk.pixmap_create_from_xpm_d(drawable, green, xpm_cursor, 1)
64 #mask, X = gtk.gdk.pixmap_create_from_xpm_d(drawable, green, xpm_mask, 1)
65 #pixmap = gtk.gdk.pixmap_create_from_data(drawable, xbm_cursor, 20, 20, 1, black, white)
66 #mask = gtk.gdk.pixmap_create_from_data(drawable, xbm_mask, 20, 20, 1, black, white)
67 pixbuf = gtk.gdk.pixbuf_new_from_xpm_data(xpm_cursor)
69 #cursor = gtk.gdk.Cursor(gtk.gdk.X_CURSOR)
70 #cursor = gtk.gdk.Cursor(pixmap, mask, black, white, 0, 0)
71 cursor = gtk.gdk.Cursor(display, pixbuf, 0, 0)
72 window.window.set_cursor(cursor)
74 Xscreen, pointer_x, pointer_y, Xmods = display.get_pointer()
75 print pointer_x, pointer_y
76 window.move(pointer_x - int(ww/2), pointer_y - int(wh/2))
78 glib.idle_add(run_external_prog, priority=glib.PRIORITY_DEFAULT_IDLE)
80 def run_external_prog(*X):
81 subprocess.call(sys.argv[1:])
82 go_idle()
84 def go_idle():
85 window.connect('leave-notify-event', on_blur)
86 window.connect('key-press-event', on_blur)
87 try:
88 os.kill(parent, signal.SIGUSR1)
89 except:
90 pass
92 def on_blur(*X):
93 gtk.main_quit()
95 def set_mouse_pointer(x, y):
96 from ctypes import cdll, c_int, c_voidp, byref
97 xlib = cdll.LoadLibrary('libX11.so')
98 display = xlib.XOpenDisplay(None)
99 root = xlib.XDefaultRootWindow(display)
100 xlib.XWarpPointer(display, None, root, 0, 0, 0, 0, c_int(x), c_int(y))
101 xlib.XCloseDisplay(display)
104 parent = os.getpid()
105 pid = os.fork()
106 if pid > 0:
107 os.closerange(3, 65535)
108 wait = True
109 def leave(*X):
110 wait = False
111 sys.exit(0)
112 signal.signal(signal.SIGUSR1, leave)
113 while wait: time.sleep(86400)
114 sys.exit(0)
117 set_mouse_pointer(0, 0)
120 display = gtk.gdk.Display(None)
121 window = gtk.Window()
122 ww, wh = 2, 2
123 window.set_default_size(ww, wh)
124 window.set_geometry_hints(min_width=ww, min_height=wh)
125 window.set_decorated(False)
126 window.set_skip_taskbar_hint(True)
127 window.set_keep_above(True)
128 window.connect('map-event', on_start)
129 window.connect('delete-event', lambda *X: gtk.main_quit())
130 window.show_all()
131 gtk.main()