2015-06-26 01:36:36 +01:00
|
|
|
#!/usr/bin/python2
|
2014-03-28 15:44:13 +00:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
import gi
|
2016-09-06 15:58:34 +01:00
|
|
|
gi.require_version("Gtk", "3.0")
|
2014-01-22 20:16:27 +00:00
|
|
|
|
|
|
|
from Xlib.display import Display
|
|
|
|
from Xlib import X, error
|
|
|
|
from gi.repository import Gtk, Gdk, GObject, GLib
|
|
|
|
import threading
|
|
|
|
|
|
|
|
class PointerMonitor(GObject.GObject, threading.Thread):
|
|
|
|
__gsignals__ = {
|
|
|
|
'activate': (GObject.SignalFlags.RUN_LAST, None, ()),
|
|
|
|
}
|
2016-05-21 17:11:36 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
def __init__(self):
|
|
|
|
GObject.GObject.__init__ (self)
|
|
|
|
threading.Thread.__init__ (self)
|
|
|
|
self.setDaemon (True)
|
|
|
|
self.display = Display()
|
|
|
|
self.root = self.display.screen().root
|
|
|
|
self.windows = []
|
2016-05-21 17:11:36 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
# Receives GDK windows
|
|
|
|
def addWindowToMonitor(self, window):
|
2017-04-28 12:45:07 +01:00
|
|
|
self.windows.append(window)
|
2016-05-21 17:11:36 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
def grabPointer(self):
|
|
|
|
self.root.grab_button(X.AnyButton, X.AnyModifier, True, X.ButtonPressMask, X.GrabModeSync, X.GrabModeAsync, 0, 0)
|
|
|
|
self.display.flush()
|
2016-05-21 17:11:36 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
def ungrabPointer(self):
|
|
|
|
self.root.ungrab_button(X.AnyButton, X.AnyModifier)
|
|
|
|
self.display.flush()
|
|
|
|
|
|
|
|
def idle(self):
|
|
|
|
self.emit("activate")
|
|
|
|
return False
|
|
|
|
|
|
|
|
def activate(self):
|
|
|
|
GLib.idle_add(self.run)
|
2016-05-21 17:11:36 +01:00
|
|
|
|
2014-01-22 20:16:27 +00:00
|
|
|
def run(self):
|
|
|
|
self.running = True
|
|
|
|
while self.running:
|
|
|
|
event = self.display.next_event()
|
|
|
|
try:
|
|
|
|
if event.type == X.ButtonPress:
|
|
|
|
# Check if pointer is inside monitored windows
|
2014-05-01 18:13:17 +01:00
|
|
|
for w in self.windows:
|
2017-04-28 12:45:07 +01:00
|
|
|
if Gtk.check_version (3, 20, 0) is None:
|
|
|
|
pdevice = Gdk.Display.get_default().get_default_seat().get_pointer()
|
|
|
|
else:
|
|
|
|
pdevice = Gdk.Display.get_default().get_device_manager().get_client_pointer()
|
|
|
|
p = self.get_window().get_device_position(pdevice)
|
|
|
|
g = self.get_size()
|
|
|
|
|
|
|
|
if p.x >= 0 and p.y >= 0 and p.x <= g.width and p.y <= g.height:
|
2014-05-01 18:13:17 +01:00
|
|
|
break
|
2014-01-22 20:16:27 +00:00
|
|
|
else:
|
|
|
|
# Is outside, so activate
|
|
|
|
GLib.idle_add(self.idle)
|
2014-05-01 20:54:43 +01:00
|
|
|
self.display.allow_events(X.ReplayPointer, event.time)
|
2016-05-21 17:11:36 +01:00
|
|
|
else:
|
2014-05-01 20:54:43 +01:00
|
|
|
self.display.allow_events(X.ReplayPointer, X.CurrentTime)
|
2014-01-22 20:16:27 +00:00
|
|
|
except Exception as e:
|
|
|
|
print "Unexpected error: " + str(e)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.running = False
|
|
|
|
self.root.ungrab_button(X.AnyButton, X.AnyModifier)
|
|
|
|
self.display.close()
|
2016-05-21 17:11:36 +01:00
|
|
|
|