%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/share/ibus-table/engine/__pycache__/
Upload File :
Create Path :
Current File : //usr/share/ibus-table/engine/__pycache__/it_active_window.cpython-312.pyc

�

��Xe�4���UdZddlmZddlmZddlZddlZddlZddlZddlZddl	Z	dZ
	ddlZdZ
e	jd�ZGd�d	�Zd
aeeefed<dd
�Zdeeeffd�Zdeeeffd�Zdeeeffd�Zedk(rxe	j0ej2��Zej7e	j8�ej;e�ej=de��e�ZejA�yy#ef$rdZ
Y��wxYw)u�Module to track the name of the program in the currently focused window

On Wayland desktops:
    Tries AT-SPI first and falls back to xprop
On X11 desktops:
    Uses only xprop

AT-SPI:

https://en.wikipedia.org/wiki/Assistive_Technology_Service_Provider_Interface

- AT-SPI seems to work reasonably well in Gnome Wayland, KDE (Plasma)
  Wayland, and in some other cases. It even works in most X11 desktops, even
  in i3 (But as getting the active window via AT-SPI can sometimes fail,
  it is better to use xprop for that on X11 desktops, xprop is very reliable
  on X11 desktops).

- Even in a Gnome Wayland or KDE (Plasma) Wayland session AT-SPI
  doesn‘t work for some old X11 programs like xterm, urxvt, ...
  But the fallback to xprop works in these cases.

- To make AT-SPI work with Firefox and google-chrome,
  GNOME_ACCESSIBILITY=1 needs to be set in the environment.

- To make AT-SPI work with Qt5, QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
  needs to be set in the environment

- To make AT-SPI work with Qt4, QT_ACCESSIBILITY=1 needs to be set
  in the environment

In some cases, the name returned by AT-SPI may slightly differ
from from the name acquired by ibus with or by xprop.
For example in case of google-chrome:

- AT-SPI: 'Google Chrome'
- ibus focus id: 'google-chrome'
- xprop: 'google-chrome'

To test this module execute

    sleep 5 && python3 it_active_window.py

in a terminal, and focus a different window and see whether
the information from that active window is fetched correctly

Then focus different windows and see whether the log output
seen in the terminal shows correctly which window is currently active
(This works only with AT-SPI).

�)�Any)�TupleNFTz
ibus-tablec�V�eZdZdZd
d�Zd
d�Zdeeeffd�Zde	ddfd�Z
de	ddfd	�Zy)�AtspiMonitorzK
    Class to monitor the program name in the currently focused window
    �returnNc���d|_d|_d|_tstjd�y	tjj|jd�tjj|jd�d|_tjd�y#t$r5}tjd	|jj|�Yd}~yd}~wwxYw)
�Initialization�Fu“import pyatspi” failed.Nzwindow:activatezwindow:deactivateTzAtspiMonitor events registered.z%s: %s )�_active_program_name�_active_window_title�_events_registered�IMPORT_PYATSPI_SUCCESSFUL�LOGGER�info�pyatspi�Registry�registerEventListener�_on_window_activate�_on_window_deactivate�	Exception�	exception�	__class__�__name__)�self�errors  �0/usr/share/ibus-table/engine/it_active_window.py�__init__zAtspiMonitor.__init__\s���$&��!�$&��!�"'���(��K�K�6�7��	I����2�2��(�(�*;�
=����2�2��*�*�,?�
A�&*�D�#��K�K�9�:���	I����Y����(@�(@�%�H�H��	I�s�A0B$�$	C"�-+C�C"c��|jr4tjd�tjj�yy)zStarts the monitoringzStarting AtspiMonitor.N)r
rrrr�start�rs rrzAtspiMonitor.startos/���"�"��K�K�0�1����"�"�$�#�c�2�|j|jfS)z�
        Gets information about the currently active window.

        :return: A tuple (program_name, window_title) giving
                 information about the currently focused window.
        )rrr s r�get_active_windowzAtspiMonitor.get_active_windowus���)�)�4�+D�+D�E�Er!�eventc��tjdt|��	|jj|_|j|_tjd|j
|j
|j�y#t$r5}tjd|jj|�Yd}~�pd}~wwxYw)z$Called when a window gets activated.�%s�%s: %sNz3window activated: %s currently active: %s title: %s)
r�debug�str�host_application�namer�source_namerrrrrr)rr$rs   rrz AtspiMonitor._on_window_activate~s������T�3�u�:�&�	H�(-�(>�(>�(C�(C�D�%�(-�(9�(9�D�%�	���I��-�-��-�-��-�-�	/���	H����X�u���'?�'?��G�G��	H�s�,B�	C�
+B=�=Cc��tjdt|��d}	|jj}|j|k(rd|_	d|_
tjd||j|j�y#t
$r5}tj
d|jj|�Yd}~��d}~wwxYw)z&Called when a window gets deactivated.r&r
r'Nz5window deactivated: %s currently active: %s title: %s)rr(r)r*r+rrrrrrr)rr$�program_namers    rrz"AtspiMonitor._on_window_deactivate�s������T�3�u�:�&���	H� �1�1�6�6�L�J�$�$��4�(*�D�%�(*�D�%����K� ��-�-��-�-�	/��O�	H����X�u���'?�'?��G�G��	H�s�B�	C�+B<�<C�rN)r�
__module__�__qualname__�__doc__rrrr)r#rrr�r!rrrXsN���I�&%�F�5��c��?�F�/��/��/�0/�3�0/�4�0/r!r�r
r
�_ACTIVE_WINDOWrc���	tjjd�}|D]}|j�j	tj
�r�1|D]J}|j
�j	tj�s�1|j|jfa	y��	da	y#t$r7}tjd|jj|�Yd}~da	yd}~wwxYw)z�
    Internal function to get information about the currently active window.

    :return: A tuple (program_name, window_title) giving
             information about the currently focused window.
    rNr'r4)rr�
getDesktop�getState�contains�
STATE_DEFUNCT�
get_state_set�STATE_ACTIVEr+r5rrrrr)�desktop�application�windowrs    r�_get_active_window_atspir@�s���
D��"�"�-�-�a�0��"�	�K��#�#�%�.�.�w�/D�/D�E��%�
���'�'�)�2�2�7�3G�3G�H�&1�&6�&6����%D�N��
�	��N���D�����5�?�?�#;�#;�U�C�C��N��D�s$�BB)�B)�"B)�)	C)�2+C$�$C)c���datsytjdt��}|j�|j
d��|j�rtjd�ytS)z�
    Get information about the currently active window.

    :return: A tuple (program_name, window_title) giving
             information about the currently focused window.
    r4T)�daemon�targetg�?)�timeoutztimeout getting active window.)
r5r�	threading�Threadr@r�join�is_aliverr)�active_window_threads r�get_active_window_atspirJ�se���N�$��$�+�+��4�6����� ����c��*��$�$�&����5�6���r!c	��d}d}dtjvstjds||fStjd�}|s||fS	t	j
|ddddddgd	d
d	��}t|jj��dkrtjd�||fS|jj�dd
d}|dk(r||fS	t	j
|d|ddddgd	d
d	��}d|jvsd|jvrtjd�||fS|jjdd��djd�dj!�ddj#�}	t	j
|d|ddddgd	d
d	��}d|jvrtjd�||fS|jjdd��dj!�dd}||fS#tj$rD}tjd|jj||j�||fcYd
}~Sd
}~wwxYw#tj$rD}tjd|jj||j�||fcYd
}~Sd
}~wwxYw#tj$rD}tjd|jj||j�||fcYd
}~Sd
}~wwxYw)a 
    Gets information about the currently active window.

    :return: A tuple (program_name, window_title) giving
             information about the currently focused window.

    Works only in X11 sessions (and for some X11 programs like xterm,
    urxvt, ...  in Wayland sessions)
    r
�DISPLAY�xpropz-rootz-f�_NET_ACTIVE_WINDOW�0xz $0Tzutf-8)�check�encoding�capture_outputz/Exception when calling xprop: %s: %s stderr: %sN�z/Unexpected xprop output for id of active window���r�0x0z-id�WM_CLASS�0s�=�,z9Unexpected xprop output for program name of active window�)�maxsplit�_NET_WM_NAME�0tz2Unexpected xprop output for title of active window)�os�environ�shutil�which�
subprocess�run�CalledProcessErrorrrrr�stderr�len�stdout�splitr�strip�lower)r.�window_title�xprop_binary�resultr�	window_ids      r�get_active_window_xpropro�s���L��L���
�
�"�"�*�*�Y�*?��l�+�+��<�<��(�L���l�+�+�	,����
�7�D�
!�4��0D�
F����?���6�=�=��� �!�A�%����F�G��l�+�+��
�
�#�#�%�b�c�*�1�-�I��E���l�+�+�	,����
�
�I�t�Z��z�
C����?���&�-�-��3�f�m�m�#;����G�	I��l�+�+��=�=�&�&��a�'����!�E�#�J�q�*�*/�%�'�!�B�8�8=����	,����
�
�I�t�^�T�>�
K����?���&�-�-�����I�J��l�+�+��=�=�&�&�s�Q�&�7��:�@�@�B�1�R�H�L��,�'�'��e�(�(�,����=�
�_�_�
%�
%�u�e�l�l�	<��l�+�+��	,��&�(�(�,����=�
�_�_�
%�
%�u�u�|�|�	=��l�+�+��	,��&�(�(�,����=�
�_�_�
%�
%�u�e�l�l�	<��l�+�+��	,�sZ�
 G6� I�: J*�6I
�	9I�I
�I
�J'�#9J"�J'�"J'�*L�=9K<�6L�<Lc�$�d\}}dtjvr1tjdj�dk(r
t�\}}|rtjd||f�||fSt
�\}}|rtjd||f�||fS)u�
    Gets information about the currently active window.

    :return: A tuple (program_name, window_title) giving
             information about the currently focused window.

    Tries AT-SPI first, if that doesn’t work falls back to xprop.

    r4�XDG_SESSION_TYPE�waylandz!Got active window from AT-SPI: %sz Got active window from xprop: %s)r^r_rjrJrr(ro)r.rks  rr#r#3s���$,� �\�<��b�j�j�(��J�J�)�*�0�0�2�i�?�'>�'@�$��|�����/�,��1M�	O��l�+�+�#:�#<� �\�<�����.��|�0L�	N��,�'�'r!�__main__)�streamr&r/)!r2�typingrr�sysr^rbr`rE�loggingrr�ImportError�	getLoggerrrr5r)�__annotations__r@rJror#r�
StreamHandlerre�LOG_HANDLER�setLevel�DEBUG�
addHandlerr�
atspi_monitorrr3r!r�<module>r�s4��(1�f��
�	��
���!��&�� $��
��	�	�<�	(��c/�c/�J#+���c�3�h��*��*��s�C�x���(H(��s�C�x��H(�V(�5��c��?�(�0�z��'�'�'�'�s�z�z�:�K�
�O�O�G�M�M�"�
���k�"�
�K�K��'�)�*� �N�M�����
��q	�~�&� %��&�s�C<�<D�D

Zerion Mini Shell 1.0