def stop(self, *args): """ Quits the main loop. """ if not self.has_implementations: self._set_default_registry () Atspi.event_quit()
def deregisterKeystrokeListener(self, client, key_set=[], mask=0, kind=(_KEY_PRESSED_EVENT, _KEY_RELEASED_EVENT)): """ Deregisters a listener for key stroke events. @@param client: Callable to be invoked when the event occurs @@type client: callable @@param key_set: Set of hardware key codes to stop monitoring. Leave empty to indicate all keys. @@type key_set: list of integer @@param mask: When the mask is None, the codes in the key_set will be monitored only when no modifier is held. When the mask is an integer, keys in the key_set will be monitored only when the modifiers in the mask are held. When the mask is an iterable over more than one integer, keys in the key_set will be monitored when any of the modifier combinations in the set are held. @@type mask: integer, iterable, None @@param kind: Kind of events to stop watching, KEY_PRESSED_EVENT or KEY_RELEASED_EVENT. @@type kind: list @@raise KeyError: When the client isn't already registered for events """ if not self.has_implementations: self._set_default_registry () try: listener = self.event_listeners[client] except: return Atspi.deregister_keystroke_listener (listener, key_set, mask, kind)
def stop(self, *args): """ Quits the main loop. """ if not self.has_implementations: self._set_default_registry() Atspi.event_quit()
def _register_atspi_keystroke_listeners(self, register): if self._keystroke_listeners_registered != register: modifier_masks = range(16) if register: if not self._keystroke_listener: self._keystroke_listener = \ Atspi.DeviceListener.new(self._on_atspi_keystroke, None) for modifier_mask in modifier_masks: Atspi.register_keystroke_listener( \ self._keystroke_listener, None, # key set, None=all modifier_mask, Atspi.KeyEventType.PRESSED, Atspi.KeyListenerSyncType.SYNCHRONOUS) else: # Apparently any single deregister call will turn off # all the other registered modifier_masks too. Since # deregistering takes extremely long (~2.5s for 16 calls) # seize the opportunity and just pick a single arbitrary # mask (Quantal). modifier_masks = [2] for modifier_mask in modifier_masks: Atspi.deregister_keystroke_listener( self._keystroke_listener, None, # key set, None=all modifier_mask, Atspi.KeyEventType.PRESSED) self._keystroke_listeners_registered = register
def _register_atspi_keystroke_listeners(self, register): if not "Atspi" in globals(): return if self._keystroke_listeners_registered != register: modifier_masks = range(16) if register: if not self._keystroke_listener: self._keystroke_listener = \ Atspi.DeviceListener.new(self._on_atspi_keystroke, None) for modifier_mask in modifier_masks: Atspi.register_keystroke_listener( \ self._keystroke_listener, None, # key set, None=all modifier_mask, Atspi.KeyEventType.PRESSED, Atspi.KeyListenerSyncType.SYNCHRONOUS) else: # Apparently any single deregister call will turn off # all the other registered modifier_masks too. Since # deregistering takes extremely long (~2.5s for 16 calls) # seize the opportunity and just pick a single arbitrary # mask (Quantal). modifier_masks = [2] for modifier_mask in modifier_masks: Atspi.deregister_keystroke_listener( self._keystroke_listener, None, # key set, None=all modifier_mask, Atspi.KeyEventType.PRESSED) self._keystroke_listeners_registered = register
def registerKeystrokeListener(self, client, key_set=[], mask=0, kind=(_KEY_PRESSED_EVENT, _KEY_RELEASED_EVENT), synchronous=True, preemptive=True, global_=False): """ Registers a listener for key stroke events. @@param client: Callable to be invoked when the event occurs @@type client: callable @@param key_set: Set of hardware key codes to stop monitoring. Leave empty to indicate all keys. @@type key_set: list of integer @@param mask: When the mask is None, the codes in the key_set will be monitored only when no modifier is held. When the mask is an integer, keys in the key_set will be monitored only when the modifiers in the mask are held. When the mask is an iterable over more than one integer, keys in the key_set will be monitored when any of the modifier combinations in the set are held. @@type mask: integer, iterable, None @@param kind: Kind of events to watch, KEY_PRESSED_EVENT or KEY_RELEASED_EVENT. @@type kind: list @@param synchronous: Should the callback notification be synchronous, giving the client the chance to consume the event? @@type synchronous: boolean @@param preemptive: Should the callback be allowed to preempt / consume the event? @@type preemptive: boolean @@param global_: Should callback occur even if an application not supporting AT-SPI is in the foreground? (requires xevie) @@type global_: boolean """ if not self.has_implementations: self._set_default_registry () try: listener = self.event_listeners[client] except: listener = self.event_listeners[client] = Atspi.DeviceListener.new(self.eventWrapper, client) syncFlag = self.makeSyncType(synchronous, preemptive, global_) try: iter(mask) masks = mask except: masks = [mask] for m in masks: Atspi.register_keystroke_listener(listener, key_set, m, self.makeKind (kind), syncFlag)
def registerKeystrokeListener(self, client, key_set=[], mask=0, kind=(_KEY_PRESSED_EVENT, _KEY_RELEASED_EVENT), synchronous=True, preemptive=True, global_=False): """ Registers a listener for key stroke events. @@param client: Callable to be invoked when the event occurs @@type client: callable @@param key_set: Set of hardware key codes to stop monitoring. Leave empty to indicate all keys. @@type key_set: list of integer @@param mask: When the mask is None, the codes in the key_set will be monitored only when no modifier is held. When the mask is an integer, keys in the key_set will be monitored only when the modifiers in the mask are held. When the mask is an iterable over more than one integer, keys in the key_set will be monitored when any of the modifier combinations in the set are held. @@type mask: integer, iterable, None @@param kind: Kind of events to watch, KEY_PRESSED_EVENT or KEY_RELEASED_EVENT. @@type kind: list @@param synchronous: Should the callback notification be synchronous, giving the client the chance to consume the event? @@type synchronous: boolean @@param preemptive: Should the callback be allowed to preempt / consume the event? @@type preemptive: boolean @@param global_: Should callback occur even if an application not supporting AT-SPI is in the foreground? (requires xevie) @@type global_: boolean """ if not self.has_implementations: self._set_default_registry () try: listener = self.event_listeners[client] except: listener = self.event_listeners[client] = Atspi.DeviceListener.new(self.eventWrapper, client) syncFlag = self.makeSyncType(synchronous, preemptive, global_) if hasattr(mask, '__iter__'): masks = mask else: masks = [mask] for m in masks: Atspi.register_keystroke_listener(listener, key_set, m, self.makeKind (kind), syncFlag)
def __init__(self): self.display = GdkX11.X11Display.get_default() self.screen = Wnck.Screen.get_default() self.props = { "window": None, "fmt": "jpg", "x1": None, "y1": None, "x2": None, "y2": None, } Atspi.init()
def execute(self, filename: str, keyval: int): if keyval: Atspi.generate_keyboard_event(keyval, None, Atspi.KeySynthType.PRESSRELEASE) self.capture( filename=filename, window=self.props["window"], type=self.props["type"], x1=self.props["x1"], y1=self.props["y1"], x2=self.props["x2"], y2=self.props["y2"], )
def getLocalizedRoleName(self, obj, role=None): """Returns the localized name of the given Accessible object; the name is suitable to be brailled. Arguments: - obj: an Accessible object - role: an optional pyatspi role to use instead """ if _settingsManager.getSetting('brailleRolenameStyle') \ == settings.BRAILLE_ROLENAME_STYLE_SHORT: objRole = role or obj.getRole() rv = shortRoleNames.get(objRole) if rv: return rv if not isinstance(role, pyatspi.Role): try: return obj.getLocalizedRoleName() except: return '' if not role: return '' nonlocalized = Atspi.role_get_name(role) atkRole = Atk.role_for_name(nonlocalized) return Atk.role_get_localized_name(atkRole)
def generateMouseEvent(self, x, y, name): """ Generates a mouse event at the given absolute x and y coordinate. The kind of event generated is specified by the name. For example, MOUSE_B1P (button 1 press), MOUSE_REL (relative motion), MOUSE_B3D (butten 3 double-click). @@param x: Horizontal coordinate, usually left-hand oriented @@type x: integer @@param y: Vertical coordinate, usually left-hand oriented @@type y: integer @@param name: Name of the event to generate @@type name: string """ if not self.has_implementations: self._set_default_registry() Atspi.generate_mouse_event(x, y, name)
def setReferenceWIndow(accessible): """ Sets the reference window that will be used when generateMouseEvent is called. Coordinates will be assumed to be relative to this window. This * is needed because, due to Wayland's security model, it is not currently possible to possible to retrieve global coordinates. If NULL is passed, then AT-SPI will use the window that has focus at the time that atspi_generate_mouse_event is called. @@param accessible: the accessible corresponding to the window to select. should be a top-level window with a role of pyatspi.ROLE_APPLICATION. """ if not self.has_implementations: self._set_default_registry() Atspi.setReferenceWindow(accessible)
def generateMouseEvent(self, x, y, name): """ Generates a mouse event at the given absolute x and y coordinate. The kind of event generated is specified by the name. For example, MOUSE_B1P (button 1 press), MOUSE_REL (relative motion), MOUSE_B3D (butten 3 double-click). @@param x: Horizontal coordinate, usually left-hand oriented @@type x: integer @@param y: Vertical coordinate, usually left-hand oriented @@type y: integer @@param name: Name of the event to generate @@type name: string """ if not self.has_implementations: self._set_default_registry () Atspi.generate_mouse_event (x, y, name)
def find_program(pid): desktop = Atspi.get_desktop(0) child_count = desktop.get_child_count() for i in range(child_count): child = desktop.get_child_at_index(i) if child.get_process_id() == pid: return child
def generateKeyboardEvent(self, keycode, keysym, kind): """ Generates a keyboard event. One of the keycode or the keysym parameters should be specified and the other should be None. The kind parameter is required and should be one of the KEY_PRESS, KEY_RELEASE, KEY_PRESSRELEASE, KEY_SYM, or KEY_STRING. @@param keycode: Hardware keycode or None @@type keycode: integer @@param keysym: Symbolic key string or None @@type keysym: string @@param kind: Kind of event to synthesize @@type kind: integer """ if not self.has_implementations: self._set_default_registry () if keysym is None: keysym = "" Atspi.generate_keyboard_event (keycode, keysym, kind)
def generateKeyboardEvent(self, keycode, keysym, kind): """ Generates a keyboard event. One of the keycode or the keysym parameters should be specified and the other should be None. The kind parameter is required and should be one of the KEY_PRESS, KEY_RELEASE, KEY_PRESSRELEASE, KEY_SYM, KEY_STRING, KEY_LOCKMODIFIERS, or KEY_UNLOCKMODIFIERS. @@param keycode: Hardware keycode or None @@type keycode: integer @@param keysym: Symbolic key string or None @@type keysym: string @@param kind: Kind of event to synthesize @@type kind: integer """ if not self.has_implementations: self._set_default_registry() if keysym is None: keysym = "" Atspi.generate_keyboard_event(keycode, keysym, kind)
def start(self, asynchronous=False, gil=True, **kwargs): """ Enter the main loop to start receiving and dispatching events. @@param asynchronous: Should event dispatch be asynchronous (decoupled) from event receiving from the AT-SPI registry? @@type asynchronous: boolean @@param gil: Add an idle callback which releases the Python GIL for a few milliseconds to allow other threads to run? Necessary if other threads will be used in this process. @@type gil: boolean """ if 'async' in kwargs: # support previous API asynchronous = kwargs['async'] if not self.has_implementations: self._set_default_registry() self.started = True if gil: def releaseGIL(): try: time.sleep(1e-2) except KeyboardInterrupt as e: # store the exception for later releaseGIL.keyboard_exception = e self.stop() return True # make room for an exception if one occurs during the releaseGIL.keyboard_exception = None i = GLib.idle_add(releaseGIL) Atspi.event_main() GLib.source_remove(i) if releaseGIL.keyboard_exception is not None: # raise an keyboard exception we may have gotten earlier raise releaseGIL.keyboard_exception else: Atspi.event_main() self.started = False
def getDesktop(self, i): """ Gets a reference to the i-th desktop. @@param i: Which desktop to get @@type i: integer @@return: Desktop reference @@rtype: Accessibility.Desktop """ if not self.has_implementations: self._set_default_registry () return Atspi.get_desktop(i)
def get_app(name): desktop = Atspi.get_desktop(0) start = time.time() timeout = 5 app = None while app is None and (time.time() - start) < timeout: gen = (child for _i, child in children(desktop) if child and child.get_name() == name) app = next(gen, None) if app is None: time.sleep(0.6) return app
def getDesktop(self, i): """ Gets a reference to the i-th desktop. @@param i: Which desktop to get @@type i: integer @@return: Desktop reference @@rtype: Accessibility.Desktop """ if not self.has_implementations: self._set_default_registry() return Atspi.get_desktop(i)
def deregisterKeystrokeListener(self, client, key_set=[], mask=0, kind=(_KEY_PRESSED_EVENT, _KEY_RELEASED_EVENT)): """ Deregisters a listener for key stroke events. @@param client: Callable to be invoked when the event occurs @@type client: callable @@param key_set: Set of hardware key codes to stop monitoring. Leave empty to indicate all keys. @@type key_set: list of integer @@param mask: When the mask is None, the codes in the key_set will be monitored only when no modifier is held. When the mask is an integer, keys in the key_set will be monitored only when the modifiers in the mask are held. When the mask is an iterable over more than one integer, keys in the key_set will be monitored when any of the modifier combinations in the set are held. @@type mask: integer, iterable, None @@param kind: Kind of events to stop watching, KEY_PRESSED_EVENT or KEY_RELEASED_EVENT. @@type kind: list @@raise KeyError: When the client isn't already registered for events """ if not self.has_implementations: self._set_default_registry() try: listener = self.event_listeners[client] except: return if hasattr(mask, '__iter__'): masks = mask else: masks = [mask] for m in masks: Atspi.deregister_keystroke_listener(listener, key_set, m, self.makeKind(kind))
def start(self, asynchronous=False, gil=True, **kwargs): """ Enter the main loop to start receiving and dispatching events. @@param asynchronous: Should event dispatch be asynchronous (decoupled) from event receiving from the AT-SPI registry? @@type asynchronous: boolean @@param gil: Add an idle callback which releases the Python GIL for a few milliseconds to allow other threads to run? Necessary if other threads will be used in this process. @@type gil: boolean """ if 'async' in kwargs: # support previous API asynchronous = kwargs['async'] if not self.has_implementations: self._set_default_registry () self.started = True if gil: def releaseGIL(): try: time.sleep(1e-2) except KeyboardInterrupt as e: # store the exception for later releaseGIL.keyboard_exception = e self.stop() return True # make room for an exception if one occurs during the releaseGIL.keyboard_exception = None i = GLib.idle_add(releaseGIL) Atspi.event_main() GLib.source_remove(i) if releaseGIL.keyboard_exception is not None: # raise an keyboard exception we may have gotten earlier raise releaseGIL.keyboard_exception else: Atspi.event_main() self.started = False
def run(path, name= None): name = name or f"{path}-test-{str(random.randint(0, 100000000))}" process = subprocess.Popen([path, '--name', name]) desktop = Atspi.get_desktop(0) start = time.time() timeout = 5 app = None while app is None and (time.time() - start) < timeout: gen = (child for _i, child in children(desktop) if child and child.get_name() == name) app = next(gen, None) if app is None: time.sleep(0.6) return (process, app)
def _generateRoleName(self, obj, **args): """Returns an array of sounds indicating the role of obj.""" if not _settingsManager.getSetting('playSoundForRole'): return [] role = args.get('role', obj.getRole()) filename = Atspi.role_get_name(role).replace(' ', '_') result = self._convertFilenameToIcon(filename) if result: return [result] return []
self._set_default_registry () self.started = True if gil: def releaseGIL(): try: time.sleep(1e-2) except KeyboardInterrupt as e: # store the exception for later releaseGIL.keyboard_exception = e self.stop() return True # make room for an exception if one occurs during the releaseGIL.keyboard_exception = None i = GLib.idle_add(releaseGIL) Atspi.event_main() GLib.source_remove(i) if releaseGIL.keyboard_exception is not None: # raise an keyboard exception we may have gotten earlier raise releaseGIL.keyboard_exception else: Atspi.event_main() self.started = False def stop(self, *args): """ Quits the main loop. """ if not self.has_implementations: self._set_default_registry ()
self._set_default_registry () self.started = True if gil: def releaseGIL(): try: time.sleep(1e-2) except KeyboardInterrupt, e: # store the exception for later releaseGIL.keyboard_exception = e self.stop() return True # make room for an exception if one occurs during the releaseGIL.keyboard_exception = None i = GObject.idle_add(releaseGIL) Atspi.event_main() GObject.source_remove(i) if releaseGIL.keyboard_exception is not None: # raise an keyboard exception we may have gotten earlier raise releaseGIL.keyboard_exception else: Atspi.event_main() self.started = False def stop(self, *args): """ Quits the main loop. """ if not self.has_implementations: self._set_default_registry ()
def release(button=1, x_offset=0, y_offset=0): Atspi.generate_mouse_event(x_offset, y_offset, "b%sr" % button) return True
def click(self, button=1, x_offset=0, y_offset=0): point = self._accessible.get_position(Atspi.CoordType.SCREEN) Atspi.generate_mouse_event(point.x+x_offset, point.y+y_offset, "b%sc" % button)
def press_key_string(self, string): #print("press_key_string") Atspi.generate_keyboard_event(0, string, Atspi.KeySynthType.STRING)
def release_keycode(self, keycode): #print("release_keycode") Atspi.generate_keyboard_event(keycode, "", Atspi.KeySynthType.RELEASE)
def main(argv): if Atspi.init() != 0: eprint("could not init") return 1 pid = int(argv[1]) command = argv[2] command_args = argv[3:] start_time = time.perf_counter() (client, drawing_area) = find(pid) while (client is None or drawing_area is None) and command == "wait": time.sleep(0.1) if time.perf_counter() - start_time > TIMEOUT_IN_S: return 1 (client, drawing_area) = find(pid) if client is None: eprint("no such pid") return 1 if drawing_area is None: eprint("client has no drawing area") return 1 if command == "wait": return 0 # extents = obj.get_extents(Atspi.CoordType.SCREEN) # print(extents.x, extents.y, extents.width, extents.height) # testPy.grab_focus() # print(Atspi.generate_mouse_event(extents.x, extents.y, "b1c")) #button 1 click screen_extents = drawing_area.get_extents(Atspi.CoordType.SCREEN) window_extents = drawing_area.get_extents(Atspi.CoordType.WINDOW) #needs can_focus on target widget drawing_area.grab_focus() if command == "mouse": if command_args[0] == "m": relative = command_args[1] == "rel" x = int(command_args[2]) y = int(command_args[3]) if not relative: x += screen_extents.x y += screen_extents.y return move_mouse(relative, x, y) else: return generate_mouse_event(command_args[0]) elif command == "query-screen-size": eprint(window_extents.width, window_extents.height) return query_return_code( window_extents.width == int(command_args[0]) and window_extents.height == int(command_args[1])) elif command == "take-screenshot": dest = command_args[0] screen = Gdk.get_default_root_window() pixbuf = Gdk.pixbuf_get_from_window(screen, screen_extents.x, screen_extents.y, screen_extents.width, screen_extents.height) pixbuf.savev(dest, "png", [], []) elif command == "focus": time.sleep(0.5) drawing_area.grab_focus() time.sleep(0.5) elif command.startswith("key"): return generate_keyboard_event(command, int(command_args[0])) elif command == "resize": width, height = (s for s in command_args[:2]) if subprocess.run([ "xdotool", "getactivewindow", "windowsize", "--sync", width, height ]).returncode != 0: return False else: eprint("no such command") return 2 return 0
import logging import time from gi.repository import Atspi from gi.repository import GLib Atspi.set_timeout(-1, -1) def get_root(): return Node(Atspi.get_desktop(0)) def _retry_find(func): def wrapped(*args, **kwargs): result = None n_retries = 1 while n_retries <= 50: logging.info("Try %d, name=%s role_name=%s" % (n_retries, kwargs.get( "name", None), kwargs.get("role_name", None))) try: result = func(*args, **kwargs) except GLib.GError, e: # The application is not responding, try again if e.code == Atspi.Error.IPC: continue logging.error("GError code %d", e.code)
def press(button=1, x_offset=0, y_offset=0): Atspi.generate_mouse_event(x_offset, y_offset, "b%sp" % button) return True
def absMotion(x_offset=0, y_offset=0): Atspi.generate_mouse_event(x_offset, y_offset, "abs") return True
def click(self, button=1, x_offset=0, y_offset=0): point = self._accessible.get_position(Atspi.CoordType.SCREEN) Atspi.generate_mouse_event(point.x + x_offset, point.y + y_offset, "b%sc" % button)
def get_root(): return Node(Atspi.get_desktop(0))
def click(self, button=1): point = self._accessible.get_position(Atspi.CoordType.SCREEN) Atspi.generate_mouse_event(point.x, point.y, "b%sc" % button)
import logging import time from gi.repository import Atspi from gi.repository import GLib Atspi.set_timeout(-1, -1) def get_root(): return Node(Atspi.get_desktop(0)) def _retry_find(func): def wrapped(*args, **kwargs): result = None n_retries = 1 while n_retries <= 50: logging.info("Try %d, name=%s role_name=%s" % (n_retries, kwargs.get("name", None), kwargs.get("role_name", None))) try: result = func(*args, **kwargs) except GLib.GError, e: # The application is not responding, try again if e.code == Atspi.Error.IPC: continue
def press_keycode(self, keycode): #print("press_keycode") Atspi.generate_keyboard_event(keycode, "", Atspi.KeySynthType.PRESS)
def key_press(key_name): key_sym = name_to_sym(key_name) Atspi.generate_keyboard_event(key_sym, None, Atspi.KeySynthType.SYM)
def StateSet_getStates(self): ret = [] for i in range(0, 64): if (self.states & (1 << i)): ret.append(Atspi.StateType(i)) return ret