mintmenu/usr/lib/linuxmint/mintMenu/pointerMonitor.py
hordepfo b14e2e0450 Changed all showing/hiding logic
It seems most GTK apps like mintMenu and the default Gnome2/Mate menu
use the GDK grab focus functions to grab input (pointer/keyboard).
These functions appear to suck all input to the app like a blackhole.
This patch gives more interactivity to the mintMenu window, making it
a first class object in the desktop.

Things that work now:
* When the window is open, hovering on other desktop icons highlights them

* Clicking outside the window propagates the event (e.g. select another
window)

* Alt-tab and other events unfocus and hide the window

* Scrollbars in the mintMenu finally work right

* Probably several other small things
2014-02-03 20:14:52 +00:00

85 lines
2.8 KiB
Python

import gi
gi.require_version("Gtk", "2.0")
from Xlib.display import Display
from Xlib import X, error
from gi.repository import Gtk, Gdk, GObject, GLib
import threading
import ctypes
from ctypes import *
gdk = CDLL("libgdk-x11-2.0.so.0")
gtk = CDLL("libgtk-x11-2.0.so.0")
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 = []
self.topWindows = []
# Receives GDK windows
def addWindowToMonitor(self, window):
xWindow = self.display.create_resource_object("window", gdk.gdk_x11_drawable_get_xid(hash(window)))
self.windows.append(xWindow)
self.topWindows.append(self.getToplevelParent(xWindow))
def getToplevelParent(self, window):
parent = window.query_tree().parent
if parent == self.root:
return window
else:
return self.getToplevelParent(parent)
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, topW in zip(self.windows, self.topWindows):
if event.child == topW:
if topW == w:
break
else:
p = w.query_pointer()
g = w.get_geometry()
if p.win_x >= 0 and p.win_y >= 0 and p.win_x <= g.width and p.win_y <= g.height:
break
else:
# Is outside, so activate
GLib.idle_add(self.idle)
self.display.allow_events(X.ReplayPointer, event.time)
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()