File: itb_active_window.py

package info (click to toggle)
ibus-typing-booster 2.30.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 141,128 kB
  • sloc: xml: 1,123,826; python: 46,964; sh: 5,183; makefile: 373; sed: 16
file content (343 lines) | stat: -rw-r--r-- 13,659 bytes parent folder | download | duplicates (2)
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
# vim:et sts=4 sw=4
#
# ibus-typing-booster - A completion input method for IBus
#
# Copyright (c) 2022 Mike FABIAN <mfabian@redhat.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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 General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>
'''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).

'''

from typing import Any
from typing import Tuple
import sys
import os
import subprocess
import shutil
import threading
import logging
IMPORT_PYATSPI_SUCCESSFUL = False
try:
    import pyatspi # type: ignore
    IMPORT_PYATSPI_SUCCESSFUL = True
except (ImportError,):
    IMPORT_PYATSPI_SUCCESSFUL = False

LOGGER = logging.getLogger('ibus-typing-booster')

class AtspiMonitor:
    '''
    Class to monitor the program name in the currently focused window
    '''
    def __init__(self) -> None:
        '''Initialization'''
        self._active_program_name = ''
        self._active_window_title = ''
        self._events_registered = False
        if not IMPORT_PYATSPI_SUCCESSFUL:
            LOGGER.info('“import pyatspi” failed.')
            return
        try:
            pyatspi.Registry.registerEventListener(
                self._on_window_activate, 'window:activate')
            pyatspi.Registry.registerEventListener(
                self._on_window_deactivate, 'window:deactivate')
            self._events_registered = True
            LOGGER.info('AtspiMonitor events registered.')
        except Exception as error: # pylint: disable=broad-except
            LOGGER.exception('%s: %s ', error.__class__.__name__, error)


    def start(self) -> None:
        '''Starts the monitoring'''
        if self._events_registered:
            LOGGER.info('Starting AtspiMonitor.')
            pyatspi.Registry.start() # pylint: disable=no-value-for-parameter

    def get_active_window(self) -> Tuple[str, str]:
        '''
        Gets information about the currently active window.

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

    def _on_window_activate(self, event: Any) -> None:
        '''Called when a window gets activated.'''
        LOGGER.debug('%s', str(event))
        try:
            self._active_program_name = event.host_application.name
            self._active_window_title = event.source_name
        except Exception as error: # pylint: disable=broad-except
            LOGGER.exception('%s: %s', error.__class__.__name__, error)
        LOGGER.info('window activated: %s currently active: %s title: %s',
                    self._active_program_name,
                    self._active_program_name,
                    self._active_window_title)

    def _on_window_deactivate(self, event: Any) -> None:
        '''Called when a window gets deactivated.'''
        LOGGER.debug('%s', str(event))
        program_name = ''
        try:
            program_name = event.host_application.name
        except Exception as error: # pylint: disable=broad-except
            LOGGER.exception('%s: %s', error.__class__.__name__, error)
        # There are some windows where the 'window:activate',
        # 'window:deactivate' signals do not work, for example windows
        # containing old X11 programs like xterm behave like that.
        #
        # If the focus moves from a window where the signals work to a
        # window where they do not, only the 'window:deactivate'
        # occurs.
        #
        # Therefore, we may want to set self._active_program_name = ''
        # when a window is deactivated because we may not be able to
        # get the true name of the program in the window which is
        # activated next and it is better to have an empty string than
        # a wrong program name left over from the previously active
        # program.
        #
        # *But*, sometimes the 'window:deactivate' signal for the old
        # window occurs *after* the 'window:activate' signal of the
        # new window! Weird!  Therefore, set self._active_program_name
        # = '' only if the program name is still unchanged!
        #
        # Theoretically if there are two windows for the same program,
        # for example two 'soffice' windows (Libreoffice), moving from
        # one to the other could then reset the program name from
        # 'soffice' to '' if the 'window:deactivate' signal comes
        # last.
        #
        # Luckily, when testing this I never saw that the
        # 'window:deactivate' signal came last when moving the focus
        # between two windows for the same program, i.e. when moving
        # from 'soffice' to 'soffice' or from 'firefox' to 'firefox',
        # this never happened.  But when moving from 'soffice' to
        # 'gnome-terminal' or from 'firefox' to 'gnome-terminal', it
        # *always* happened. This might be just an accident, but I
        # don’t have a better idea for a workaround at the moment.
        if self._active_program_name == program_name:
            self._active_program_name = ''
            self._active_window_title = ''
        LOGGER.info('window deactivated: %s currently active: %s title: %s',
                    program_name,
                    self._active_program_name,
                    self._active_window_title)

_ACTIVE_WINDOW: Tuple[str, str] = ('', '')

def _get_active_window_atspi() -> None:
    '''
    Internal function to get information about the currently active window.

    :return: A tuple (program_name, window_title) giving
             information about the currently focused window.
    '''
    global _ACTIVE_WINDOW # pylint: disable=global-statement
    try:
        desktop = pyatspi.Registry.getDesktop(0) # pylint: disable=no-value-for-parameter
        for application in desktop:
            if application.getState().contains(pyatspi.STATE_DEFUNCT):
                continue
            for window in application:
                if window.get_state_set().contains(pyatspi.STATE_ACTIVE):
                    _ACTIVE_WINDOW = (application.name, window.name)
                    return
    except Exception as error: # pylint: disable=broad-except
        LOGGER.exception('%s: %s', error.__class__.__name__, error)
    _ACTIVE_WINDOW = ('', '')

def get_active_window_atspi() -> Tuple[str, str]:
    '''
    Get information about the currently active window.

    :return: A tuple (program_name, window_title) giving
             information about the currently focused window.
    '''
    global _ACTIVE_WINDOW # pylint: disable=global-statement
    _ACTIVE_WINDOW = ('', '')
    if not IMPORT_PYATSPI_SUCCESSFUL:
        return ('', '')
    active_window_thread = threading.Thread(
        daemon=True, target=_get_active_window_atspi)
    active_window_thread.start()
    active_window_thread.join(timeout=0.5)
    if active_window_thread.is_alive():
        LOGGER.error('timeout getting active window.')
        return ('', '')
    return _ACTIVE_WINDOW

def get_active_window_xprop() -> Tuple[str, str]:
    '''
    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)
    '''
    program_name = ''
    window_title = ''
    if 'DISPLAY' not in os.environ or not os.environ['DISPLAY']:
        return (program_name, window_title)
    xprop_binary = shutil.which('xprop')
    if not xprop_binary:
        return (program_name, window_title)
    try:
        result = subprocess.run(
            [xprop_binary, '-root', '-f',
             '_NET_ACTIVE_WINDOW', '0x', ' $0', '_NET_ACTIVE_WINDOW'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True, encoding='utf-8')
    except subprocess.CalledProcessError as error:
        LOGGER.exception(
            'Exception when calling xprop: %s: %s stderr: %s',
             error.__class__.__name__, error, error.stderr)
        return (program_name, window_title)
    # result now looks like in this example:
    #
    # '_NET_ACTIVE_WINDOW(WINDOW) 0x1e02d79'
    if len(result.stdout.split()) < 2:
        LOGGER.error('Unexpected xprop output for id of active window')
        return (program_name, window_title)
    window_id = result.stdout.split()[-1:][0]
    if window_id == '0x0':
        return (program_name, window_title)
    try:
        result = subprocess.run(
            [xprop_binary,
             '-id', window_id, '-f', 'WM_CLASS', '0s', 'WM_CLASS'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True, encoding='utf-8')
    except subprocess.CalledProcessError as error:
        LOGGER.exception(
            'Exception when calling xprop: %s: %s stderr: %s',
             error.__class__.__name__, error,  error.stderr)
        return (program_name, window_title)
    # result now looks like in this example
    #
    # 'WM_CLASS(STRING) = "xfce4-terminal", "Xfce4-terminal"\n'
    if '=' not in result.stdout or ',' not in result.stdout:
        LOGGER.error(
            'Unexpected xprop output for program name of active window')
        return (program_name, window_title)
    program_name = result.stdout.split(
        '=', maxsplit=1)[1].split(',')[1].strip()[1:-1].lower()
    try:
        result = subprocess.run(
            [xprop_binary,
             '-id', window_id, '-f', '_NET_WM_NAME', '0t', '_NET_WM_NAME'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True, encoding='utf-8')
    except subprocess.CalledProcessError as error:
        LOGGER.exception(
            'Exception when calling xprop: %s: %s stderr: %s',
             error.__class__.__name__, error, error.stderr)
        return (program_name, window_title)
    # result now looks like in this example
    #
    # '_NET_WM_NAME(UTF8_STRING) = "☺foo = "bar"\n'
    if '=' not in result.stdout:
        LOGGER.error('Unexpected xprop output for title of active window')
        return (program_name, window_title)
    window_title = result.stdout.split('=', maxsplit=1)[1].strip()[1:-1]
    return (program_name, window_title)


def get_active_window() -> Tuple[str, str]:
    '''
    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.

    '''
    (program_name, window_title) = ('', '')
    if ('WAYLAND_DISPLAY' in os.environ
        and 'XDG_SESSION_TYPE' in os.environ
        and os.environ['XDG_SESSION_TYPE'].lower() == 'wayland'):
        (program_name, window_title) = get_active_window_atspi()
    if program_name:
        LOGGER.debug(
            'Got active window from AT-SPI: %s', (program_name, window_title))
        return (program_name, window_title)
    (program_name, window_title) = get_active_window_xprop()
    if program_name:
        LOGGER.debug(
            'Got active window from xprop: %s', (program_name, window_title))
    return (program_name, window_title)

if __name__ == "__main__":
    LOG_HANDLER = logging.StreamHandler(stream=sys.stderr)
    LOGGER.setLevel(logging.DEBUG)
    LOGGER.addHandler(LOG_HANDLER)
    LOGGER.info('%s', get_active_window())
    atspi_monitor = AtspiMonitor()
    atspi_monitor.start()