/usr/share/pyshared/pyatspi/deviceevent.py is in python-pyatspi2 2.4.0+dfsg-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | #Copyright (C) 2008 Codethink Ltd
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License version 2 as published by the Free Software Foundation.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU Lesser General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from interfaces import *
import registry
import dbus as _dbus
import dbus.service as _service
from enum import Enum as _Enum
import traceback
#------------------------------------------------------------------------------
class PressedEventType(_Enum):
_enum_lookup = {
0:'KEY_PRESSED_EVENT',
1:'KEY_RELEASED_EVENT',
2:'BUTTON_PRESSED_EVENT',
3:'BUTTON_RELEASED_EVENT',
}
KEY_PRESSED_EVENT = PressedEventType(0)
KEY_RELEASED_EVENT = PressedEventType(1)
BUTTON_PRESSED_EVENT = PressedEventType(2)
BUTTON_RELEASED_EVENT = PressedEventType(3)
#------------------------------------------------------------------------------
class ControllerEventMask(_Enum):
_enum_lookup = {
1:'KEY_PRESSED_EVENT_MASK',
2:'KEY_RELEASED_EVENT_MASK',
4:'BUTTON_PRESSED_EVENT_MASK',
8:'BUTTON_RELEASED_EVENT_MASK',
}
KEY_PRESSED_EVENT_MASK = ControllerEventMask(1)
KEY_RELEASED_EVENT_MASK = ControllerEventMask(2)
BUTTON_PRESSED_EVENT_MASK = ControllerEventMask(4)
BUTTON_RELEASED_EVENT_MASK = ControllerEventMask(8)
#------------------------------------------------------------------------------
class KeyEventType(_Enum):
_enum_lookup = {
0:'KEY_PRESSED',
1:'KEY_RELEASED',
}
KEY_PRESSED = KeyEventType(0)
KEY_RELEASED = KeyEventType(1)
#------------------------------------------------------------------------------
class KeySynthType(_Enum):
_enum_lookup = {
0:'KEY_PRESS',
1:'KEY_RELEASE',
2:'KEY_PRESSRELEASE',
3:'KEY_SYM',
4:'KEY_STRING',
}
KEY_PRESS = KeySynthType(0)
KEY_PRESSRELEASE = KeySynthType(2)
KEY_RELEASE = KeySynthType(1)
KEY_STRING = KeySynthType(4)
KEY_SYM = KeySynthType(3)
#------------------------------------------------------------------------------
class ModifierType(_Enum):
_enum_lookup = {
0:'MODIFIER_SHIFT',
1:'MODIFIER_SHIFTLOCK',
2:'MODIFIER_CONTROL',
3:'MODIFIER_ALT',
4:'MODIFIER_META',
5:'MODIFIER_META2',
6:'MODIFIER_META3',
7:'MODIFIER_NUMLOCK',
}
MODIFIER_ALT = ModifierType(3)
MODIFIER_CONTROL = ModifierType(2)
MODIFIER_META = ModifierType(4)
MODIFIER_META2 = ModifierType(5)
MODIFIER_META3 = ModifierType(6)
MODIFIER_NUMLOCK = ModifierType(7)
MODIFIER_SHIFT = ModifierType(0)
MODIFIER_SHIFTLOCK = ModifierType(1)
def allModifiers():
"""
Generates all possible keyboard modifiers for use with
L{registry.Registry.registerKeystrokeListener}.
"""
mask = 0
while mask <= (1 << MODIFIER_NUMLOCK):
yield mask
mask += 1
#------------------------------------------------------------------------------
class DeviceEvent(list):
"""
Wraps an AT-SPI device event with a more Pythonic interface. Primarily adds
a consume attribute which can be used to cease propagation of a device event.
@ivar consume: Should this event be consumed and not allowed to pass on to
observers further down the dispatch chain in this process or possibly
system wide?
@type consume: boolean
@ivar type: Kind of event, KEY_PRESSED_EVENT or KEY_RELEASED_EVENT
@type type: Accessibility.EventType
@ivar id: Serial identifier for this key event
@type id: integer
@ivar hw_code: Hardware scan code for the key
@type hw_code: integer
@ivar modifiers: Modifiers held at the time of the key event
@type modifiers: integer
@ivar timestamp: Time at which the event occurred relative to some platform
dependent starting point (e.g. XWindows start time)
@type timestamp: integer
@ivar event_string: String describing the key pressed (e.g. keysym)
@type event_string: string
@ivar is_text: Is the event representative of text to be inserted (True), or
of a control key (False)?
@type is_text: boolean
"""
def __new__(cls, type, id, hw_code, modifiers, timestamp, event_string, is_text):
return list.__new__(cls, (type, id, hw_code, modifiers, timestamp, event_string, is_text))
def __init__(self, type, id, hw_code, modifiers, timestamp, event_string, is_text):
list.__init__(self, (type, id, hw_code, modifiers, timestamp, event_string, is_text))
self.consume = False
def _get_type(self):
return self[0]
def _set_type(self, val):
self[0] = val
type = property(fget=_get_type, fset=_set_type)
def _get_id(self):
return self[1]
def _set_id(self, val):
self[1] = val
id = property(fget=_get_id, fset=_set_id)
def _get_hw_code(self):
return self[2]
def _set_hw_code(self, val):
self[2] = val
hw_code = property(fget=_get_hw_code, fset=_set_hw_code)
def _get_modifiers(self):
return self[3]
def _set_modifiers(self, val):
self[3] = val
modifiers = property(fget=_get_modifiers, fset=_set_modifiers)
def _get_timestamp(self):
return self[4]
def _set_timestamp(self, val):
self[4] = val
timestamp = property(fget=_get_timestamp, fset=_set_timestamp)
def _get_event_string(self):
return self[5]
def _set_event_string(self, val):
self[5] = val
event_string = property(fget=_get_event_string, fset=_set_event_string)
def _get_is_text(self):
return self[6]
def _set_is_text(self, val):
self[6] = val
is_text = property(fget=_get_is_text, fset=_set_is_text)
def __str__(self):
"""
Builds a human readable representation of the event.
@return: Event description
@rtype: string
"""
import constants
if self.type == constants.KEY_PRESSED_EVENT:
kind = 'pressed'
elif self.type == constants.KEY_RELEASED_EVENT:
kind = 'released'
return """\
%s
\thw_code: %d
\tevent_string: %s
\tmodifiers: %d
\tid: %d
\ttimestamp: %d
\tis_text: %s""" % (kind, self.hw_code, self.event_string, self.modifiers,
self.id, self.timestamp, self.is_text)
#------------------------------------------------------------------------------
class EventListenerMode(list):
def __new__(cls, synchronous, preemptive, global_):
return list.__new__(cls, (synchronous, preemptive, global_))
def __init__(self, synchronous, preemptive, global_):
list.__init__(self, (synchronous, preemptive, global_))
def _get_synchronous(self):
return self[0]
def _set_synchronous(self, val):
self[0] = val
synchronous = property(fget=_get_synchronous, fset=_set_synchronous)
def _get_preemptive(self):
return self[1]
def _set_preemptive(self, val):
self[1] = val
preemptive = property(fget=_get_preemptive, fset=_set_preemptive)
def _get_global_(self):
return self[2]
def _set_global_(self, val):
self[2] = val
global_ = property(fget=_get_global_, fset=_set_global_)
#------------------------------------------------------------------------------
class KeyDefinition(list):
def __new__(cls, keycode, keysym, keystring, unused):
return list.__new__(cls, (keycode, keysym, keystring, unused))
def __init__(self, keycode, keysym, keystring, unused):
list.__init__(self, (keycode, keysym, keystring, unused))
def _get_keycode(self):
return self[0]
def _set_keycode(self, val):
self[0] = val
keycode = property(fget=_get_keycode, fset=_set_keycode)
def _get_keysym(self):
return self[1]
def _set_keysym(self, val):
self[1] = val
keysym = property(fget=_get_keysym, fset=_set_keysym)
def _get_keystring(self):
return self[2]
def _set_keystring(self, val):
self[2] = val
keystring = property(fget=_get_keystring, fset=_set_keystring)
def _get_unused(self):
return self[3]
def _set_unused(self, val):
self[3] = val
unused = property(fget=_get_unused, fset=_set_unused)
#------------------------------------------------------------------------------
class DeviceEventController(object):
"""
The interface via which clients request notification of device
events, and through which device events may be simulated.
"""
def __init__ (self, connection):
dec_object = connection.get_object(ATSPI_REGISTRY_NAME,
ATSPI_DEVICE_EVENT_CONTROLLER_PATH)
self._dec = _dbus.Interface(dec_object, ATSPI_DEVICE_EVENT_CONTROLLER_INTERFACE)
def registerKeystrokeListener(self,
event_listener,
keys,
event_mask,
key_event_types,
event_listener_mode):
"""
Register to intercept keyboard events, and either pass them on
or consume them.
@param : listener
A DeviceEventListener which will intercept key events.
@param : keys
A list of KeyDefinition indicating which keys to intercept, or KEYSET_ALL_KEYS.
@param : mask
A ControllerEventMask bitmask for filtering the intercepted key events.
@param : type
A list of KeyEventType
@param : mode
An EventListenerMode indicating whether the listener should receive
the events synchronously, potentially consuming them, or just
be notified asynchronously of those events that have been generated.
@return True if the DeviceEventListener was successfully registered
for the requested KeySet, ControllerEventMask, event types, and
EventListenerMode; otherwise returns False.
"""
func = self._dec.get_dbus_method("RegisterKeystrokeListener")
return func(event_listener,
_dbus.Array(keys, signature="(iisi)"),
event_mask,
key_event_types,
event_listener_mode)
def deregisterKeystrokeListener(self,
event_listener,
keys,
event_mask,
key_event_types):
"""
De-register a previously registered keyboard eventlistener.
@param : listener
A DeviceEventListener which will intercept key events.
@param : keys
A list of KeyDefinition indicating which keys to intercept, or KEYSET_ALL_KEYS.
@param : mask
A ControllerEventMask filtering the intercepted key events.
@param : type
A list of KeyEventType
"""
func = self._dec.get_dbus_method("DeregisterKeystrokeListener")
return func(event_listener,
keys,
event_mask,
key_event_types)
def registerDeviceEventListener(self,
event_listener,
event_types):
"""
Register to intercept events, and either pass them on or consume
them. To listen to keyboard events use registerKeystrokeListener
instead.
@param : listener
A DeviceEventListener which will intercept events.
@param : typeseq
A list of EventType indicating which event types to listen for.
@return True if successful, False if not
"""
func = self._dec.get_dbus_method("RegisterDeviceEventListener")
return func(event_listener, event_types)
def deregisterDeviceEventListener(self,
event_listener,
event_types):
"""
De-register a previously registered keyboard eventlistener.
@param : listener
A DeviceEventListener which will intercept events.
@param : typeseq
A List of EventType indicating which event types to stop listening
for.
"""
func = self._dec.get_dbus_method("DeregisterDeviceEventListener")
return func(event_listener, event_types)
def notifyListenersSync(self, event):
"""
Notify the Registry instance that a device event has taken place,
and allow pre-emptive listeners the opportunity to 'consume'
the event and thus prevent its further issuance/forwarding. This
is the method used by accessibility bridges to forward "toolkit
dependent" device events to the Registry from the application's
process space.
@return True if the event was consumed by a (pre-emptive) listener,
False if not (in which case the device event will be forwarded
as normal to any application which would normally receive it,
e.g. the currently active application in the case of mouse or
keyboard events).
"""
func = self._dec.get_dbus_method("NotifyListenersSync")
return func(event)
def notifyListenersAsync(self, event):
"""
Notify the Registry instance that a device event has taken place
in an asynchronous manner. This is the method used by accessibility
bridges to forward "toolkit dependent" device events to the Registry
from the application's process space. If the event in question
is potentially pre-emptible. notifyListenersSync should be used
instead.
"""
func = self._dec.get_dbus_method("NotifyListenersAsync")
return func(event)
def generateKeyboardEvent(self, keycode, keystring, type):
"""
Synthesize a keyboard event.
@param : keycode
A long integer indicating the keycode of the keypress to be synthesized.
@param : keystring
an optional UTF-8 string indicating a complex keyboard input
event.
@param : type
A KeySynthType indicating the type of event(s) to be synthesized:
a key press, release, press-release pair, or a complex input
string (for instance from an internationalized or complex text
input method, or a composed character).
"""
func = self._dec.get_dbus_method("GenerateKeyboardEvent")
return func(keycode, keystring, type)
def generateMouseEvent(self, x, y, name):
"""
Synthesize a mouse event.
@param : x
A long integer indicating the screen x coord for the mouse event.
@param : y
A long integer indicating the screen y coord for the mouse event.
@param : name
A string indicating the type of mouse event, e.g. "button1up"
"""
func = self._dec.get_dbus_method("GenerateMouseEvent")
return func(x, y, name)
#------------------------------------------------------------------------------
class KeyboardDeviceEventListener(_service.Object):
"""
Observes keyboard press and release events.
@ivar registry: The L{Registry} that created this observer
@type registry: L{Registry}
@ivar key_set: Set of keys to monitor
@type key_set: list of integer
@ivar mask: Watch for key events while these modifiers are held
@type mask: integer
@ivar kind: Kind of events to monitor
@type kind: integer
@ivar mode: Keyboard event mode
@type mode: Accessibility.EventListenerMode
"""
_next_listener_id = 0
def _get_unique_path (self):
KeyboardDeviceEventListener._next_listener_id += 1
return "/org/a11y/atspi/keyeventlistener/%d" % (KeyboardDeviceEventListener._next_listener_id,)
def __init__(self, registry, synchronous, preemptive, global_):
"""
Creates a mode object that defines when key events will be received from
the system. Stores all other information for later registration.
@param registry: The L{Registry} that created this observer
@type registry: L{Registry}
@param synchronous: Handle the key event synchronously?
@type synchronous: boolean
@param preemptive: Allow event to be consumed?
@type preemptive: boolean
@param global_: Watch for events on inaccessible applications too?
@type global_: boolean
"""
self._upath = self._get_unique_path()
_service.Object.__init__(self, registry._bus, self._upath)
self.mode = EventListenerMode(synchronous, preemptive, global_)
self._registry = registry
def register(self, dc, key_set, mask, kind):
"""
Starts keyboard event monitoring.
@param dc: Reference to a device controller
@type dc: Accessibility.DeviceEventController
@param key_set: Set of keys to monitor
@type key_set: list of integer
@param mask: Integer modifier mask or an iterable over multiple masks to
unapply all at once
@type mask: integer, iterable, or None
@param kind: Kind of events to monitor
@type kind: integer
"""
try:
# check if the mask is iterable
iter(mask)
except TypeError:
# register a single integer if not
dc.registerKeystrokeListener(self._upath, key_set, mask, kind, self.mode)
else:
for m in mask:
dc.registerKeystrokeListener(self._upath, key_set, m, kind, self.mode)
def unregister(self, dc, key_set, mask, kind):
"""
Stops keyboard event monitoring.
@param dc: Reference to a device controller
@type dc: Accessibility.DeviceEventController
@param key_set: Set of keys to monitor
@type key_set: list of integer
@param mask: Integer modifier mask or an iterable over multiple masks to
unapply all at once
@type mask: integer, iterable, or None
@param kind: Kind of events to monitor
@type kind: integer
"""
try:
# check if the mask is iterable
iter(mask)
except TypeError:
# unregister a single integer if not
dc.deregisterKeystrokeListener(self._upath, key_set, mask, kind)
else:
for m in mask:
dc.deregisterKeystrokeListener(self._upath, key_set, m, kind)
@_service.method(dbus_interface=ATSPI_DEVICE_EVENT_LISTENER_INTERFACE,
in_signature="(uinnisb)",
out_signature="b")
def NotifyEvent(self, ev):
"""
Notifies the L{Registry} that an event has occurred. Wraps the raw event
object in our L{Event} class to support automatic ref and unref calls. An
observer can return True to indicate this event should not be allowed to pass
to other AT-SPI observers or the underlying application.
@param ev: Keyboard event
@type ev: Accessibility.DeviceEvent
@return: Should the event be consumed (True) or allowed to pass on to other
AT-SPI observers (False)?
@rtype: boolean
"""
# TODO Find out where the exceptions are falling in to.
try:
# wrap the device event
event = DeviceEvent(*ev)
ret = self._registry._handleDeviceEvent(event, self)
return ret
except Exception, e:
import traceback
traceback.print_exc()
return False
#------------------------------------------------------------------------------
class _DeviceEventRegister (object):
def __init__ (self):
self._bus = SyncAccessibilityBus (registry.Registry())
self.dev = DeviceEventController (self._bus)
self.deviceClients = {}
def registerKeystrokeListener(self,
client,
key_set=[],
mask=0,
kind=(KEY_PRESSED_EVENT, KEY_RELEASED_EVENT),
synchronous=True,
preemptive=True,
global_=False):
try:
# see if we already have an observer for this client
ob = self.deviceClients[client]
except KeyError:
# create a new device observer for this client
ob = KeyboardDeviceEventListener(self, synchronous, preemptive, global_)
# store the observer to client mapping, and the inverse
self.deviceClients[ob] = client
self.deviceClients[client] = ob
if mask is None:
# None means all modifier combinations
mask = allModifiers()
# register for new keystrokes on the observer
ob.register(self.dev, key_set, mask, kind)
def deregisterKeystrokeListener(self,
client,
key_set=[],
mask=0,
kind=(KEY_PRESSED_EVENT, KEY_RELEASED_EVENT)):
# see if we already have an observer for this client
ob = self.deviceClients[client]
if mask is None:
# None means all modifier combinations
mask = allModifiers()
# register for new keystrokes on the observer
ob.unregister(self.dev, key_set, mask, kind)
def _handleDeviceEvent(self, event, ob):
try:
# try to get the client registered for this event type
client = self.deviceClients[ob]
except KeyError:
# client may have unregistered recently, ignore event
return False
# make the call to the client
try:
return client(event) or event.consume
except Exception:
# print the exception, but don't let it stop notification
traceback.print_exc()
def generateKeyboardEvent(self, keycode, keysym, kind):
if keysym is None:
self.dev.generateKeyboardEvent(keycode, '', kind)
else:
self.dev.generateKeyboardEvent(None, keysym, kind)
def generateMouseEvent(self, x, y, name):
self.dev.generateMouseEvent(_dbus.Int32(x), _dbus.Int32(y), name)
#------------------------------------------------------------------------------
class _NullDeviceEventRegister (object):
def registerKeystrokeListener(self,
client,
key_set=[],
mask=0,
kind=(KEY_PRESSED_EVENT, KEY_RELEASED_EVENT),
synchronous=True,
preemptive=True,
global_=False):
pass
def deregisterKeystrokeListener(self,
client,
key_set=[],
mask=0,
kind=(KEY_PRESSED_EVENT, KEY_RELEASED_EVENT)):
pass
def generateKeyboardEvent(self, keycode, keysym, kind):
pass
def generateMouseEvent(self, x, y, name):
pass
#END---------------------------------------------------------------------------
|