+
   /F]c                   s  a  R t 0 t R t^ RIHt ^ RIHt ^ RIt^ RIt^ RIt^ RIt^ RI	t	^ RI
t
Rt ^ RItRt]
P                  ! R4      t ! R R4      tRs] ^ k R	 R
 ltR R ltR R ltR R lt]R8X  dz   ]
P.                  ! ]P0                  R7      t]P5                  ]
P6                  4       ]P9                  ]4       ]P;                  R]! 4       4       ]! 4       t]P?                  4        R# R#   ]3 d    Rt Li ; i)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 itb_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-typing-boosterc                   s~   a  ] tR t^Wt o RtV 3R lR ltV 3R lR ltV 3R lR ltV 3R lR	 ltV 3R
 lR lt	Rt
V tR# )AtspiMonitorzC
Class to monitor the program name in the currently focused window
c                   < V ^8  d   QhRR/#    returnN format__classdict__   ":/usr/share/ibus-typing-booster/engine/itb_active_window.py__annotate__AtspiMonitor.__annotate__[   s     I I$ I    c                s   RV n         RV n        RV n        \        '       g   \        P                  R4       R#  \        P                  P                  V P                  R4       \        P                  P                  V P                  R4       RV n        \        P                  R4       R#   \         d7   p\        P                  R	TP                  P                  T4        Rp?R# Rp?ii ; i)
ZInitialization Fu   “import pyatspi” failed.Nzwindow:activatezwindow:deactivateTzAtspiMonitor events registered.z%s: %s )_active_program_name_active_window_title_events_registeredIMPORT_PYATSPI_SUCCESSFULLOGGERinfopyatspiRegistryZregisterEventListener_on_window_activate_on_window_deactivate	Exception	exception	__class____name__)selferrors   & r   __init__ZAtspiMonitor.__init__[   s    $&!$&!"'((KK67	I22((*;=22**,?A&*D#KK9: 	IY(@(@%HH	Is   A0B, ,C-7+C((C-c                r   r   r   r	   r   r   r   r   n   s     % %t %r   c                s    V P                   '       d6   \        P                  R4       \        P                  P                  4        R# R# )zStarts the monitoringzStarting AtspiMonitor.N)r   r   r   r   r   startr       &r   r#   ZAtspiMonitor.startn   s2    """KK01""$ #r   c                s6   < V ^8  d   QhRS[ S[S[3,          /# r   r   r   strr	   r   r   r   r   t   s     F F5c? Fr   c                s2    V P                   V P                  3# )z
Gets information about the currently active window.

:return: A tuple (program_name, window_title) giving
         information about the currently focused window.
)r   r   r$   r%   r   get_active_windowZAtspiMonitor.get_active_windowt   s     ))4+D+DEEr   c                $   < V ^8  d   QhRS[ RR/# r   eventr   Nr    r	   r   r   r   r   }   s     / / / /r   c                s   \         P                  R\        V4      4        VP                  P                  V n        VP                  V n        \         P                  RV P
                  V P
                  V P                  4       R#   \         d6   p\         P                  RTP                  P                  T4        Rp?LsRp?ii ; i)z$Called when a window gets activated.%s%s: %sNz3window activated: %s currently active: %s title: %s)r   debugr(   host_applicationnamer   Zsource_namer   r   r   r   r   r   )r    r,   r!   s   && r   r   Z AtspiMonitor._on_window_activate}   s    T3u:&	H(-(>(>(C(CD%(-(9(9D% 	I------	/  	HXu'?'?GG	Hs   ,B C+C  Cc                r*   r+   r    r	   r   r   r   r      s     0/ 0/3 0/4 0/r   c                s   \         P                  R\        V4      4       Rp VP                  P                  pV P                  V8X  d   RV n	        RV n
        \         P                  RVV P                  V P                  4       R#   \
         d6   p\         P                  RTP                  P                  T4        Rp?LRp?ii ; i)z&Called when a window gets deactivated.r-   r   r.   Nz5window deactivated: %s currently active: %s title: %s)r   r/   r(   r0   r1   r   r   r   r   r   r   r   )r    r,   program_namer!   s   &&  r   r   Z"AtspiMonitor._on_window_deactivate   s    T3u:&	H 1166LJ $$4(*D%(*D%K ----	/O  	HXu'?'?GG	Hs   B C+CC)r   r   r   N)r   Z
__module__Z__qualname__Z__firstlineno____doc__r"   r#   r)   r   r   Z__static_attributes__Z__classdictcell__)r      @r   r   r   W   s@     I I&% %F F/ /0/ 0/r   r   c                s    V ^8  d   QhRR/# r   r   r
      "r   r   r      s      $ r   c                 s    \         P                  P                  ^ 4      p V  F  pVP                  4       P	                  \         P
                  4      '       d   K8  V FR  pVP                  4       P	                  \         P                  4      '       g   K8  VP                  VP                  3s	  R# 	  K  	  Rs	R#   \         d6   p\        P                  RTP                  P                  T4        Rp?L?Rp?ii ; i)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.
Nr.   r   r   )r   r   Z
getDesktopZgetStateZcontainsZSTATE_DEFUNCTZget_state_setZSTATE_ACTIVEr1   _ACTIVE_WINDOWr   r   r   r   r   )ZdesktopZapplicationZwindowr!   s       r   _get_active_window_atspir9      s    
D""--a0"K##%..w/D/DEE%'')2273G3GHH&1&6&6%DN & # N  D5??#;#;UCCDs$   BB; B; 1B; ;C;+C66C;c                F    V ^8  d   QhR\         \        \        3,          /# r&   r'   r5   r6   r   r   r      s      sCx r   c                 s   Rs \        '       g   R# \        P                  ! R\        R7      p V P                  4        V P                  RR7       V P                  4       '       d   \        P                  R4       R# \         # )z
Get information about the currently active window.

:return: A tuple (program_name, window_title) giving
         information about the currently focused window.
T)ZdaemonZtargetg      ?)Ztimeoutztimeout getting active window.r7   )
r8   r   	threadingZThreadr9   r#   ZjoinZis_aliver   r!   )Zactive_window_threads    r   get_active_window_atspir<      so     N$$$++46 c*$$&&56r   c                r:   r&   r'   r5   r6   r   r   r      s     N( N(sCx N(r   c            	     s   Rp RpR\         P                  9  g   \         P                  R,          '       g   W3# \        P                  ! R4      pV'       g   W3#  \        P
                  ! VRRRRRR.\        P                  \        P                  R	R
R7      p\        TP                  P                  4       4      ^8  d   \        P!                  R4       Y3# TP                  P                  4       RR ^ ,          pTR8X  d   Y3#  \        P
                  ! TRTRRRR.\        P                  \        P                  R	R
R7      pRTP                  9  g   RTP                  9  d   \        P!                  R4       Y3# TP                  P                  R^R7      ^,          P                  R4      ^,          P#                  4       ^R P%                  4       p  \        P
                  ! TRTRRRR.\        P                  \        P                  R	R
R7      pRTP                  9  d   \        P!                  R4       Y3# TP                  P                  R^R7      ^,          P#                  4       ^R pY3#   \        P                   dC   p\        P                  RTP                  P                  YDP                  4       Y3u Rp?# Rp?ii ; i  \        P                   dC   p\        P                  RTP                  P                  YDP                  4       Y3u Rp?# Rp?ii ; i  \        P                   dC   p\        P                  RTP                  P                  YDP                  4       Y3u Rp?# Rp?ii ; i)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   ZDISPLAYZxpropz-rootz-fZ_NET_ACTIVE_WINDOWZ0xz $0Tzutf-8)stdoutstderrZcheckZencodingz/Exception when calling xprop: %s: %s stderr: %sNz/Unexpected xprop output for id of active windowZ0x0z-idZWM_CLASSZ0sZ=Z,z9Unexpected xprop output for program name of active window)ZmaxsplitZ_NET_WM_NAMEZ0tz2Unexpected xprop output for title of active windowi)osenvironshutilZwhich
subprocessZrunZPIPEZCalledProcessErrorr   r   r   r   r>   Zlenr=   Zsplitr!   Zstriplower)r2   window_titleZxprop_binaryZresultr!   Z	window_ids         r   get_active_window_xproprE      s    LL

""**Y*?*?++<<(L++,7D!40DF????* 6== !A%FG++##%bc*1-IE++,ItZzC????* &--3fmm#;G	I++==&&a ' !E#Jq**/%'!B88= ,It^T>K????* &--IJ++==&&sQ&7:@@B1RHL''m (( ,=__%%ull	< ++	,* (( ,=__%%u||	= ++	,* (( ,=__%%ull	< ++	,sZ   >I: 	>K >L. :K7KKKL+)7L& L+&L+.N7N :N Nc                r:   r&   r'   r5   r6   r   r   r   8  s     ( (5c? (r   c                 sh   RRrR\         P                  9   dL   R\         P                  9   d7   \         P                  R,          P                  4       R8X  d   \        4       w  rV '       d   \        P                  RW34       W3# \        4       w  rV '       d   \        P                  RW34       W3# )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.

r   ZWAYLAND_DISPLAYZXDG_SESSION_TYPEZwaylandz!Got active window from AT-SPI: %sz Got active window from xprop: %s)r?   r@   rC   r<   r   r/   rE   )r2   rD   s     r   r)   r)   8  s     %'<RZZ'"**,JJ)*002i?'>'@$/,1M	O++#:#< \.0L	N''r   Z__main__)Zstreamr-   c                sb    V ^8  d   Qh/ ^ \         9   d   \        \        \        3,          ;R&   # )r   r8   )__conditional_annotations__r   r(   r5   r6   r   r   r      s(     1 1R + *c3h *S1r   r7   )!rF   r3   Ztypingr   r   Zsysr?   rB   rA   r;   Zloggingr   r   ZImportErrorZ	getLoggerr   r   r8   r9   r<   rE   r)   r   ZStreamHandlerr>   ZLOG_HANDLERZsetLevelZDEBUGZ
addHandlerr   Zatspi_monitorr#   r   )rF   r4   r   <module>rG      s   &1f   
 	    ! & $ 
		0	1c/ c/J #+ **(N(b(2 z''szz:K
OOGMM"
k"
KK')* NM  	~ & %&s   C2 2
C?>C?