3
WY2G                 @   s^   d dddgZ ddlZddlmZ ddlmZ ddlZdZd	Zd
Z	G dd  d e
ZdddZdS )RegistryMAIN_LOOP_GLIBMAIN_LOOP_NONEset_default_registry    N)Atspi)GLibr   ZQtNonec               @   s   e Zd ZdZi Zdd Zdd Zd,ddZd	d
 Zd-ddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdZdZdd Zdd  Zg d!eefdddfd"d#Zg d!eeffd$d%Zd&d' Zd(d) Zd*d+ ZdS ).r   a  
        Wraps the Accessibility.Registry to provide more Pythonic registration for
        events.

        This object should be treated as a singleton, but such treatment is not
        enforced. You can construct another instance of this object and give it a
        reference to the Accessibility.Registry singleton. Doing so is harmless and
        has no point.

        @@ivar async: Should event dispatch to local listeners be decoupled from event
                receiving from the registry?
        @@type async: boolean
        @@ivar reg: Reference to the real, wrapped registry object
        @@type reg: Accessibility.Registry
        @@ivar dev: Reference to the device controller
        @@type dev: Accessibility.DeviceEventController
        @@ivar clients: Map of event names to client listeners
        @@type clients: dictionary
        @@ivar observers: Map of event names to AT-SPI L{_Observer} objects
        @@type observers: dictionary
        c             C   sR   | j | _y| jrd S W n tk
r*   Y nX d| _d | _d | _d | _tj | _	d S )NF)
_Registry__shared_state__dict__has_implementationsAttributeErrorZdevice_event_registerZapp_event_registerZdesktopr   ZMainLoop	main_loop)self r   2/usr/lib/python3/dist-packages/pyatspi/registry.py__init__C   s    zRegistry.__init__c             C   s   | S )zn
                @@return: This instance of the registry
                @@rtype: L{Registry}
                r   )r   r   r   r   __call__T   s    zRegistry.__call__Nc             C   s   d| _ d| _d| _t | _dS )a  
                Creates a new 'Registry' object and sets this object
                as the default returned by pyatspi.Registry.

                The default registry (without calling this function) uses the
                GLib main loop with caching. It connects to a registry daemon.

                This function should be called before pyatspi is used if you
                wish to change these defaults.

                @@param main_loop_type: 'GLib', 'None' or 'Qt'. If 'None' is selected then caching
                                       is disabled.

                @@param use_registry: Whether to connect to a registry daemon for device events.
                                     Without this the application to connect to must be declared in the
                                     app_name parameter.

                @@param app_name: D-Bus name of the application to connect to when not using the registry daemon.
                TFN)r   asyncstarteddictevent_listeners)r   Zmain_loop_typeapp_namer   r   r   _set_registry[   s    zRegistry._set_registryc             C   s   | j t d S )N)r   r   )r   r   r   r   _set_default_registryv   s    zRegistry._set_default_registryFTc                sl   j sj  d_|rZ fdd d _tj }tj  tj|  jdk	rb jntj  d_dS )a3  
                Enter the main loop to start receiving and dispatching events.

                @@param async: Should event dispatch be asynchronous (decoupled) from 
                        event receiving from the AT-SPI registry?
                @@type async: 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
                Tc                 sD   yt jd W n0 tk
r> }  z|  _j  W Y d d } ~ X nX dS )Ng{Gz?T)timeZsleepKeyboardInterruptkeyboard_exceptionstop)e)
releaseGILr   r   r   r      s    z"Registry.start.<locals>.releaseGILNF)	r   r   r   r   r   Zidle_addr   Z
event_mainZsource_remove)r   r   Zgilir   )r   r   r   starty   s    	


zRegistry.startc             G   s   | j s| j  tj  dS )z6
                Quits the main loop.
                N)r   r   r   Z
event_quit)r   argsr   r   r   r      s    zRegistry.stopc             C   s   dS )z
                Gets the number of available desktops.

                @@return: Number of desktops
                @@rtype: integer
                   r   )r   r   r   r   getDesktopCount   s    zRegistry.getDesktopCountc             C   s   | j s| j  tj|S )z
                Gets a reference to the i-th desktop.

                @@param i: Which desktop to get
                @@type i: integer
                @@return: Desktop reference
                @@rtype: Accessibility.Desktop
                )r   r   r   Zget_desktop)r   r    r   r   r   
getDesktop   s    	zRegistry.getDesktopc             C   s   ||S )Nr   )r   Zeventcallbackr   r   r   eventWrapper   s    zRegistry.eventWrapperc          
   G   sd   | j s| j  y| j| }W n&   tjj| j| }| j|< Y nX x|D ]}tjj|| qJW dS )a  
                Registers a new client callback for the given event names. Supports 
                registration for all subevents if only partial event name is specified.
                Do not include a trailing colon.

                For example, 'object' will register for all object events, 
                'object:property-change' will register for all property change events,
                and 'object:property-change:accessible-parent' will register only for the
                parent property change event.

                Registered clients will not be automatically removed when the client dies.
                To ensure the client is properly garbage collected, call 
                L{deregisterEventListener}.

                @@param client: Callable to be invoked when the event occurs
                @@type client: callable
                @@param names: List of full or partial event names
                @@type names: list of string
                N)r   r   r   r   EventListenernewr'   register)r   clientnameslistenernamer   r   r   registerEventListener   s     
zRegistry.registerEventListenerc          
   G   sH   | j s| j  y| j| }W n
   dS x|D ]}tjj|| q.W dS )a  
                Unregisters an existing client callback for the given event names. Supports 
                unregistration for all subevents if only partial event name is specified.
                Do not include a trailing colon.

                This method must be called to ensure a client registered by
                L{registerEventListener} is properly garbage collected.

                @@param client: Client callback to remove
                @@type client: callable
                @@param names: List of full or partial event names
                @@type names: list of string
                @@return: Were event names specified for which the given client was not
                        registered?
                @@rtype: boolean
                N)r   r   r   r   r(   Z
deregister)r   r+   r,   r-   r.   r   r   r   deregisterEventListener   s    
z Registry.deregisterEventListenerr#      c             C   s<   t jj}|r|t jjO }|r(|t jjO }|r8|t jjO }|S )N)r   ZKeyListenerSyncTypeZNOSYNCZSYNCHRONOUSZ
CANCONSUMEZALL_WINDOWS)r   synchronous
preemptiveglobal_resultr   r   r   makeSyncType   s    zRegistry.makeSyncTypec             C   s"   d}x|D ]}|d|> O }q
W |S )Nr   r#   r   )r   kindr5   r    r   r   r   makeKind  s    
zRegistry.makeKindr   c             C   s   | j s| j  y| j| }W n&   tjj| j| }| j|< Y nX | j|||}	t|drb|}
n|g}
x$|
D ]}tj	|||| j
||	 qnW dS )a  
                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
                __iter__N)r   r   r   r   ZDeviceListenerr)   r'   r6   hasattrZregister_keystroke_listenerr8   )r   r+   key_setmaskr7   r2   r3   r4   r-   ZsyncFlagmasksmr   r   r   registerKeystrokeListener  s     $ 

z"Registry.registerKeystrokeListenerc             C   sf   | j s| j  y| j| }W n
   dS t|dr8|}n|g}x"|D ]}tj|||| j| qDW dS )a  
                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
                Nr9   )r   r   r   r:   r   Zderegister_keystroke_listenerr8   )r   r+   r;   r<   r7   r-   r=   r>   r   r   r   deregisterKeystrokeListenerC  s    

z$Registry.deregisterKeystrokeListenerc             C   s,   | j s| j  |dkrd}tj||| dS )aj  
                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
                N )r   r   r   Zgenerate_keyboard_event)r   ZkeycodeZkeysymr7   r   r   r   generateKeyboardEventm  s
    zRegistry.generateKeyboardEventc             C   s    | j s| j  tj||| dS )aq  
                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
                N)r   r   r   Zgenerate_mouse_event)r   xyr.   r   r   r   generateMouseEvent  s    zRegistry.generateMouseEventc             C   s   dS )zH
                Dispatch events that have been queued.
                Nr   )r   r   r   r   pumpQueuedEvents  s    zRegistry.pumpQueuedEvents)N)FT)__name__
__module____qualname____doc__r	   r   r   r   r   r!   r   r$   r%   r'   r/   r0   Z_KEY_PRESSED_EVENTZ_KEY_RELEASED_EVENTr6   r8   r?   r@   rB   rE   rF   r   r   r   r   r   +   s:   

&	
1&c             C   s   t  }|j| | d S )N)r   r   )r   r   registryr   r   r   r     s    )N)__all__os_osZgi.repositoryr   r   r   r   ZMAIN_LOOP_QTr   objectr   r   r   r   r   r   <module>   s     q