bugfix
[hband-tools.git] / xwin-tools / set-xcursor-lock-and-run
blob3ee9244be4e817ae0736e1aa77e9a7f6f6ec3f36
1 #!/usr/bin/env python
3 """
5 =pod
7 =head1 NAME
9 set-xcursor-lock-and-run - Set X11 mouse pointer to a lock icon and run the given command
11 =head1 SYNOPSIS
13 set-xcursor-lock-and-run <COMMAND> [<ARGS>]
15 =cut
17 """
19 import sys
20 import os
21 import subprocess
22 import gtk
23 import glib
24 import threading
25 import time
26 import signal
27 gtk.gdk.threads_init()
29 xpm_cursor = [
30 "20 20 4 1 ",
31 " c None",
32 ". c white",
33 "X c black",
34 ", c yellow",
35 " ...... ",
36 " .XXXXXX. ",
37 " .XXXXXXXX. ",
38 " .XX......XX. ",
39 " .XX. .XX. ",
40 " .XX. .XX. ",
41 " .XX. .XX. ",
42 " .XX. .XX. ",
43 " ...XX. .XX... ",
44 " .XXXXXXXXXXXXXX. ",
45 " .X,,,,,,,,,,,,X. ",
46 " .X,,,,,,,,,,,,X. ",
47 " .X,,,,,,,,,,,,X. ",
48 " .X,,,,,,,,,,,,X. ",
49 " .X,,,,,XX,,,,,X. ",
50 " .X,,,,,XX,,,,,X. ",
51 " .X,,,,,XX,,,,,X. ",
52 " .X,,,,,,,,,,X. ",
53 " .XXXXXXXXXX. ",
54 " .......... ",
59 def on_start(*X):
60 #drawable = window.window
61 #drawable = None
62 #white = gtk.gdk.Color('white')
63 #black = gtk.gdk.Color('black')
64 #green = gtk.gdk.Color('green')
66 #pixmap, X = gtk.gdk.pixmap_create_from_xpm_d(drawable, green, xpm_cursor, 1)
67 #mask, X = gtk.gdk.pixmap_create_from_xpm_d(drawable, green, xpm_mask, 1)
68 #pixmap = gtk.gdk.pixmap_create_from_data(drawable, xbm_cursor, 20, 20, 1, black, white)
69 #mask = gtk.gdk.pixmap_create_from_data(drawable, xbm_mask, 20, 20, 1, black, white)
70 pixbuf = gtk.gdk.pixbuf_new_from_xpm_data(xpm_cursor)
72 #cursor = gtk.gdk.Cursor(gtk.gdk.X_CURSOR)
73 #cursor = gtk.gdk.Cursor(pixmap, mask, black, white, 0, 0)
74 cursor = gtk.gdk.Cursor(display, pixbuf, 0, 0)
75 window.window.set_cursor(cursor)
77 Xscreen, pointer_x, pointer_y, Xmods = display.get_pointer()
78 print pointer_x, pointer_y
79 window.move(pointer_x - int(ww/2), pointer_y - int(wh/2))
81 glib.idle_add(run_external_prog, priority=glib.PRIORITY_DEFAULT_IDLE)
83 def run_external_prog(*X):
84 subprocess.call(sys.argv[1:])
85 go_idle()
87 def go_idle():
88 window.connect('leave-notify-event', on_blur)
89 window.connect('key-press-event', on_blur)
90 try:
91 os.kill(parent, signal.SIGUSR1)
92 except:
93 pass
95 def on_blur(*X):
96 gtk.main_quit()
98 def set_mouse_pointer(x, y):
99 from ctypes import cdll, c_int, c_voidp, byref
100 xlib = cdll.LoadLibrary('libX11.so')
101 display = xlib.XOpenDisplay(None)
102 root = xlib.XDefaultRootWindow(display)
103 xlib.XWarpPointer(display, None, root, 0, 0, 0, 0, c_int(x), c_int(y))
104 xlib.XCloseDisplay(display)
107 parent = os.getpid()
108 pid = os.fork()
109 if pid > 0:
110 os.closerange(3, 65535)
111 wait = True
112 def leave(*X):
113 wait = False
114 sys.exit(0)
115 signal.signal(signal.SIGUSR1, leave)
116 while wait: time.sleep(86400)
117 sys.exit(0)
120 set_mouse_pointer(0, 0)
123 display = gtk.gdk.Display(None)
124 window = gtk.Window()
125 ww, wh = 2, 2
126 window.set_default_size(ww, wh)
127 window.set_geometry_hints(min_width=ww, min_height=wh)
128 window.set_decorated(False)
129 window.set_skip_taskbar_hint(True)
130 window.connect('map-event', on_start)
131 window.connect('delete-event', lambda *X: gtk.main_quit())
132 window.show_all()
133 gtk.main()