Skip to content

Commit

Permalink
Return empty program_name and window_title in get_active_window_xprop…
Browse files Browse the repository at this point in the history
…() when xprop results are unexpected

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2215466

See also this ibus-typing-booster bug: https://bugzilla.redhat.com/show_bug.cgi?id=2175009
  • Loading branch information
mike-fabian committed Jul 10, 2023
1 parent a39794d commit 9188fa7
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions engine/it_active_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(self) -> None:
self._on_window_deactivate, 'window:deactivate')
self._events_registered = True
LOGGER.info('AtspiMonitor events registered.')
except Exception as error:
except Exception as error: # pylint: disable=broad-except
LOGGER.exception('%s: %s ', error.__class__.__name__, error)


Expand All @@ -129,7 +129,7 @@ def _on_window_activate(self, event: Any) -> None:
try:
self._active_program_name = event.host_application.name
self._active_window_title = event.source_name
except Exception as error:
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,
Expand All @@ -142,7 +142,7 @@ def _on_window_deactivate(self, event: Any) -> None:
program_name = ''
try:
program_name = event.host_application.name
except Exception as error:
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
Expand Down Expand Up @@ -195,7 +195,7 @@ def _get_active_window_atspi() -> None:
:return: A tuple (program_name, window_title) giving
information about the currently focused window.
'''
global _ACTIVE_WINDOW
global _ACTIVE_WINDOW # pylint: disable=global-statement
try:
desktop = pyatspi.Registry.getDesktop(0) # pylint: disable=no-value-for-parameter
for application in desktop:
Expand All @@ -205,7 +205,7 @@ def _get_active_window_atspi() -> None:
if window.get_state_set().contains(pyatspi.STATE_ACTIVE):
_ACTIVE_WINDOW = (application.name, window.name)
return
except Exception as error:
except Exception as error: # pylint: disable=broad-except
LOGGER.exception('%s: %s', error.__class__.__name__, error)
_ACTIVE_WINDOW = ('', '')

Expand All @@ -216,7 +216,7 @@ def get_active_window_atspi() -> Tuple[str, str]:
:return: A tuple (program_name, window_title) giving
information about the currently focused window.
'''
global _ACTIVE_WINDOW
global _ACTIVE_WINDOW # pylint: disable=global-statement
_ACTIVE_WINDOW = ('', '')
if not IMPORT_PYATSPI_SUCCESSFUL:
return ('', '')
Expand Down Expand Up @@ -258,7 +258,10 @@ def get_active_window_xprop() -> Tuple[str, str]:
return (program_name, window_title)
# result now looks like in this example:
#
# b'_NET_ACTIVE_WINDOW(WINDOW) 0x1e02d79'
# '_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)
Expand All @@ -274,7 +277,11 @@ def get_active_window_xprop() -> Tuple[str, str]:
return (program_name, window_title)
# result now looks like in this example
#
# b'WM_CLASS(STRING) = "xfce4-terminal", "Xfce4-terminal"\n'
# '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:
Expand All @@ -289,7 +296,10 @@ def get_active_window_xprop() -> Tuple[str, str]:
return (program_name, window_title)
# result now looks like in this example
#
# b'_NET_WM_NAME(UTF8_STRING) = "☺foo = "bar"\n'
# '_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)

Expand Down

0 comments on commit 9188fa7

Please sign in to comment.