def get_window_name(window: Window) -> str: """ After some annoying debugging I resorted to pretty much copying selfspy. Source: https://github.com/gurgeh/selfspy/blob/8a34597f81000b3a1be12f8cde092a40604e49cf/selfspy/sniff_x.py#L165 """ d = window.get_full_property(NET_WM_NAME, UTF8_STRING) if d is None or d.format != 8: # Fallback. r = window.get_wm_name() if type(r) == str: return r else: logger.warning( "I don't think this case will ever happen, but not sure so leaving this message here just in case." ) return r.decode('latin1') # WM_NAME with type=STRING. else: # Fixing utf8 issue on Ubuntu (https://github.com/gurgeh/selfspy/issues/133) # Thanks to https://github.com/gurgeh/selfspy/issues/133#issuecomment-142943681 try: return d.value.decode('utf8') except UnicodeError: logger.warning( "Failed to decode one or more characters which will be skipped, bytes are: {}" .format(d.value)) if isinstance(d.value, bytes): return d.value.decode('utf8', 'ignore') else: return d.value.encode('utf8').decode('utf8', 'ignore')
def get_window_name(window: Window) -> str: """ After some annoying debugging I resorted to pretty much copying selfspy. Source: https://github.com/gurgeh/selfspy/blob/8a34597f81000b3a1be12f8cde092a40604e49cf/selfspy/sniff_x.py#L165 """ try: d = window.get_full_property(NET_WM_NAME, UTF8_STRING) except Xlib.error.XError as e: logger.warning("Unable to get window property NET_WM_NAME, got a {} exception from Xlib".format(type(e).__name__)) # I strongly suspect window.get_wm_name() will also fail and we should return "unknown" right away. # But I don't know, so I pass the thing on, for now. d = None if d is None or d.format != 8: # Fallback. r = window.get_wm_name() if type(r) == str: return r else: logger.warning("I don't think this case will ever happen, but not sure so leaving this message here just in case.") return r.decode('latin1') # WM_NAME with type=STRING. else: # Fixing utf8 issue on Ubuntu (https://github.com/gurgeh/selfspy/issues/133) # Thanks to https://github.com/gurgeh/selfspy/issues/133#issuecomment-142943681 try: return d.value.decode('utf8') except UnicodeError: logger.warning("Failed to decode one or more characters which will be skipped, bytes are: {}".format(d.value)) if isinstance(d.value, bytes): return d.value.decode('utf8', 'ignore') else: return d.value.encode('utf8').decode('utf8', 'ignore')
def find_window(name: str, window: Window) -> Optional[Window]: """Recursively locate a window with name `name` in the tree, starting at `window`.""" if window.get_wm_name() == name: return window children = window.query_tree().children for child in children: val = find_window(name, child) if val: return val