mintmenu/usr/lib/linuxmint/mintMenu/pointerMonitor.py
gm10 3c6e384a1d Assorted cleanup, fixes and performance improvements (#206)
* initial cleanup with some debug messaging

* further cleanup, mostly coding style

* - more cleanup, and speed-up
- more fixes, including many icon and encoding related issues
- replace some icons and remove compile.py
- prepare for python3 port as much as possible

* remove some more unneeded files, few left-over cleanups

* move some external scripts to python3 already

* Fix and clean up add_search_suggestions and add_apt_filter_results logic

* more cleanup, thx Codacity

* fix issue with a path

* add a killall to the test script, fix a method declaration

* fix custom colour setting

* keybinding: re-add GdkX11

* re-add mint-common dep

* Use os.path.expanduser("~") instead of os.environ

* revert re-add GdkX11, but import Gtk first
2019-02-11 13:54:20 +00:00

75 lines
2.4 KiB
Python

#!/usr/bin/python2
import threading
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GObject, GLib
from Xlib import X
from Xlib.display import Display
class PointerMonitor(GObject.GObject, threading.Thread):
__gsignals__ = {
'activate': (GObject.SignalFlags.RUN_LAST, None, ()),
}
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 = []
# Receives GDK windows
def addWindowToMonitor(self, window):
self.windows.append(window)
def grabPointer(self):
self.root.grab_button(X.AnyButton, X.AnyModifier, True, X.ButtonPressMask, X.GrabModeSync, X.GrabModeAsync, 0, 0)
self.display.flush()
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)
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
for w in self.windows:
if (Gtk.MAJOR_VERSION, Gtk.MINOR_VERSION) >= (3, 20):
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:
break
else:
# Is outside, so activate
GLib.idle_add(self.idle)
self.display.allow_events(X.ReplayPointer, event.time)
else:
self.display.allow_events(X.ReplayPointer, X.CurrentTime)
except Exception as e:
print("Unexpected error:", e)
def stop(self):
self.running = False
self.root.ungrab_button(X.AnyButton, X.AnyModifier)
self.display.close()