| 1 |
#!/usr/bin/python |
| 2 |
# dump-window-events.py |
| 3 |
# |
| 4 |
# Command-line tool to dump window activation and deactivation events along with |
| 5 |
# the window(s) which claim to be active. |
| 6 |
# |
| 7 |
# Copyright 2025 Igalia, S.L. |
| 8 |
# Author: Joanmarie Diggs <[email protected]> |
| 9 |
# |
| 10 |
# This library is free software; you can redistribute it and/or |
| 11 |
# modify it under the terms of the GNU Lesser General Public |
| 12 |
# License as published by the Free Software Foundation; either |
| 13 |
# version 2.1 of the License, or (at your option) any later version. |
| 14 |
# |
| 15 |
# This library is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
# Lesser General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU Lesser General Public |
| 21 |
# License along with this library; if not, write to the |
| 22 |
# Free Software Foundation, Inc., Franklin Street, Fifth Floor, |
| 23 |
# Boston MA 02110-1301 USA. |
| 24 |
|
| 25 |
# pylint: disable=invalid-name |
| 26 |
# pylint: disable=wrong-import-position |
| 27 |
|
| 28 |
"""Command-line tool to dump window activation and deactivation events.""" |
| 29 |
|
| 30 |
import gi |
| 31 |
|
| 32 |
gi.require_version("Atspi", "2.0") |
| 33 |
from gi.repository import Atspi, GLib |
| 34 |
|
| 35 |
|
| 36 |
def clear_atspi_cache(obj): |
| 37 |
"""Clear the AT-SPI cache for a specific object.""" |
| 38 |
|
| 39 |
try: |
| 40 |
Atspi.Accessible.clear_cache_single(obj) |
| 41 |
except GLib.GError: |
| 42 |
pass |
| 43 |
else: |
| 44 |
return |
| 45 |
|
| 46 |
try: |
| 47 |
Atspi.Accessible.clear_cache(obj) |
| 48 |
except GLib.GError: |
| 49 |
pass |
| 50 |
else: |
| 51 |
return |
| 52 |
|
| 53 |
|
| 54 |
def get_all_applications(): |
| 55 |
"""Get all running accessible applications.""" |
| 56 |
|
| 57 |
apps = [] |
| 58 |
desktop = Atspi.get_desktop(0) |
| 59 |
for i in range(Atspi.Accessible.get_child_count(desktop)): |
| 60 |
try: |
| 61 |
child = Atspi.Accessible.get_child_at_index(desktop, i) |
| 62 |
# Children of mutter-x11-frames seem to mirror the actual apps, but are not what we |
| 63 |
# want. And Orca ignores them because we need to handle the real app; not some clone. |
| 64 |
if Atspi.Accessible.get_name(child) != "mutter-x11-frames": |
| 65 |
apps.append(child) |
| 66 |
except GLib.GError: |
| 67 |
continue |
| 68 |
|
| 69 |
return apps |
| 70 |
|
| 71 |
|
| 72 |
def find_all_active_windows(apps=None): |
| 73 |
"""Find all windows which claim to be active..""" |
| 74 |
|
| 75 |
active_windows = [] |
| 76 |
if apps is None: |
| 77 |
apps = get_all_applications() |
| 78 |
|
| 79 |
for app in apps: |
| 80 |
try: |
| 81 |
for i in range(Atspi.Accessible.get_child_count(app)): |
| 82 |
child = Atspi.Accessible.get_child_at_index(app, i) |
| 83 |
# To ensure we're not basing the decision on stale data. |
| 84 |
clear_atspi_cache(child) |
| 85 |
state_set = Atspi.Accessible.get_state_set(child) |
| 86 |
if state_set.contains(Atspi.StateType.ACTIVE): |
| 87 |
active_windows.append(child) |
| 88 |
except GLib.GError as error: |
| 89 |
print(f"(error finding active windows: {error})") |
| 90 |
|
| 91 |
return active_windows |
| 92 |
|
| 93 |
|
| 94 |
def get_name_and_role(obj): |
| 95 |
"""Get the name and role of an accessible object.""" |
| 96 |
|
| 97 |
try: |
| 98 |
name = Atspi.Accessible.get_name(obj) or "<unnamed>" |
| 99 |
role = Atspi.Accessible.get_role(obj).value_nick |
| 100 |
return f"'{name}' {role}" |
| 101 |
except GLib.GError as error: |
| 102 |
return f"(error getting name and role: {error})" |
| 103 |
|
| 104 |
|
| 105 |
def get_top_level_object(obj): |
| 106 |
"""Get the highest level accessible object for a given object.""" |
| 107 |
|
| 108 |
# In well-behaved applications, this should work. |
| 109 |
try: |
| 110 |
application = Atspi.Accessible.get_application(obj) |
| 111 |
except GLib.GError as error: |
| 112 |
print(f"(error getting application: {error})") |
| 113 |
else: |
| 114 |
if application is not None: |
| 115 |
return application |
| 116 |
|
| 117 |
# Walk up the tree as far as we can. We should be able to ascend all the way to the app. |
| 118 |
# But if we wound up here, we already have breakage and knowing how far we got may help |
| 119 |
# with debugging. |
| 120 |
desktop = Atspi.get_desktop(0) |
| 121 |
result = obj |
| 122 |
while result is not None: |
| 123 |
try: |
| 124 |
parent = Atspi.Accessible.get_parent(result) |
| 125 |
except GLib.GError: |
| 126 |
break |
| 127 |
if parent in (None, desktop): |
| 128 |
break |
| 129 |
result = parent |
| 130 |
|
| 131 |
return result |
| 132 |
|
| 133 |
|
| 134 |
def get_object_info(obj): |
| 135 |
"""Returns basic info about an accessible object.""" |
| 136 |
|
| 137 |
name_and_role = get_name_and_role(obj) |
| 138 |
top_level_name_and_role = get_name_and_role(get_top_level_object(obj)) |
| 139 |
return f"{name_and_role} from: {top_level_name_and_role}" |
| 140 |
|
| 141 |
|
| 142 |
def on_window_event(e): |
| 143 |
"""Handle window activation and deactivation events.""" |
| 144 |
|
| 145 |
event_type = e.type.split(":")[-1].upper() |
| 146 |
print(f"\n{event_type}: {get_object_info(e.source)}") |
| 147 |
|
| 148 |
apps = get_all_applications() |
| 149 |
active_windows = find_all_active_windows(apps) |
| 150 |
if not active_windows: |
| 151 |
print("No windows have the active state. The following applications are known to AT-SPI2:") |
| 152 |
for i, app in enumerate(apps): |
| 153 |
try: |
| 154 |
print(f"{i + 1:2}. {Atspi.Accessible.get_name(app)}") |
| 155 |
except GLib.GError as error: |
| 156 |
print(f"{i + 1:2}. {error}") |
| 157 |
return |
| 158 |
|
| 159 |
if len(active_windows) == 1: |
| 160 |
print(f"The active window is: {get_object_info(active_windows[0])}") |
| 161 |
return |
| 162 |
|
| 163 |
print(f"{len(active_windows)} windows claim they are active:") |
| 164 |
for i, win in enumerate(active_windows): |
| 165 |
try: |
| 166 |
print(f"{i + 1:2}. {get_object_info(win)}") |
| 167 |
except GLib.GError as error: |
| 168 |
print(f"{i + 1:2}. {error}") |
| 169 |
|
| 170 |
|
| 171 |
def on_focus_event(e): |
| 172 |
"""Handle focus events for exit condition.""" |
| 173 |
|
| 174 |
if not e.detail1: |
| 175 |
return |
| 176 |
|
| 177 |
try: |
| 178 |
if Atspi.Accessible.get_role(e.source) == Atspi.Role.TERMINAL: |
| 179 |
print("Exiting.") |
| 180 |
Atspi.event_quit() |
| 181 |
except GLib.GError: |
| 182 |
pass |
| 183 |
|
| 184 |
|
| 185 |
def main(): |
| 186 |
"""Starts the window event dumper and waits for events.""" |
| 187 |
|
| 188 |
window_listener = Atspi.EventListener.new(on_window_event) |
| 189 |
window_listener.register("window:activate") |
| 190 |
window_listener.register("window:deactivate") |
| 191 |
|
| 192 |
focus_listener = Atspi.EventListener.new(on_focus_event) |
| 193 |
focus_listener.register("object:state-changed:focused") |
| 194 |
|
| 195 |
print("Listening for window activation/deactivation events. Focus a terminal to exit.") |
| 196 |
Atspi.event_main() |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__": |
| 200 |
main() |