Add AT-SPI pointer monitor wrappers

This commit is contained in:
Storm Dragon
2026-04-07 17:11:13 -04:00
parent 8e792dd4e2
commit d7d26c57f4
2 changed files with 213 additions and 5 deletions
+78 -5
View File
@@ -44,6 +44,7 @@ from typing import TYPE_CHECKING, Optional, Union, Tuple, List, Dict
import gi
gi.require_version("Atspi", "2.0")
from gi.repository import Atspi
from gi.repository import GLib
from . import debug
from . import focus_manager
@@ -64,21 +65,36 @@ class InputEventManager:
self._last_input_event: Optional[input_event.InputEvent] = None
self._last_non_modifier_key_event: Optional[input_event.KeyboardEvent] = None
self._device: Optional[Atspi.Device] = None
self._pointer_moved_id: int = 0
self._mapped_keycodes: List[int] = []
self._mapped_keysyms: List[int] = []
self._grabbed_bindings: Dict[int, keybindings.KeyBinding] = {}
self._paused: bool = False
def activate_device(self) -> Atspi.Device:
"""Creates and returns the AT-SPI device used by this manager."""
if self._device is not None:
return self._device
if Atspi.get_version() >= (2, 55, 90):
self._device = Atspi.Device.new_full("org.stormux.Cthulhu")
else:
self._device = Atspi.Device.new()
return self._device
def get_device(self) -> Optional[Atspi.Device]:
"""Returns the active AT-SPI device, if any."""
return self._device
def start_key_watcher(self) -> None:
"""Starts the watcher for keyboard input events."""
msg = "INPUT EVENT MANAGER: Starting key watcher."
debug.print_message(debug.LEVEL_INFO, msg, True)
if Atspi.get_version() >= (2, 55, 90):
self._device = Atspi.Device.new_full("org.stormux.Cthulhu")
else:
self._device = Atspi.Device.new()
self._device.add_key_watcher(self.process_keyboard_event)
self.activate_device().add_key_watcher(self.process_keyboard_event)
def stop_key_watcher(self) -> None:
"""Starts the watcher for keyboard input events."""
@@ -87,6 +103,63 @@ class InputEventManager:
debug.print_message(debug.LEVEL_INFO, msg, True)
self._device = None
def enable_pointer_monitoring(self) -> bool:
"""Enables pointer monitoring on the current device, if possible."""
device = self.get_device()
if device is None:
return False
deviceCapability = getattr(Atspi, "DeviceCapability", None)
if deviceCapability is None:
return False
pointerMonitor = getattr(deviceCapability, "POINTER_MONITOR", None)
if pointerMonitor is None:
return False
setCapabilities = getattr(device, "set_capabilities", None)
if not callable(setCapabilities):
return False
currentCapabilities = 0
getCapabilities = getattr(device, "get_capabilities", None)
if callable(getCapabilities):
currentCapabilities = getCapabilities()
try:
grantedCapabilities = setCapabilities(currentCapabilities | pointerMonitor)
except GLib.GError:
return False
if isinstance(grantedCapabilities, bool):
return grantedCapabilities
try:
return bool(int(grantedCapabilities) & int(pointerMonitor))
except (TypeError, ValueError):
return False
def start_pointer_watcher(self, callback) -> None:
"""Starts the watcher for pointer movement events."""
device = self.get_device()
if device is None:
return
self._pointer_moved_id = device.connect("pointer-moved", callback)
def stop_pointer_watcher(self) -> None:
"""Stops the watcher for pointer movement events."""
device = self.get_device()
if device is None or not self._pointer_moved_id:
self._pointer_moved_id = 0
return
device.disconnect(self._pointer_moved_id)
self._pointer_moved_id = 0
def pause_key_watcher(self, pause: bool = True, reason: str = "") -> None:
"""Pauses processing of keyboard input events."""