Skip to content

Commit

Permalink
Merge pull request #126 from blueyed/fix-sniff-x-wm-name
Browse files Browse the repository at this point in the history
sniff_x: fix getting utf8 window titles, via _NET_WM_NAME
  • Loading branch information
gurgeh committed Sep 1, 2015
2 parents d918a19 + c3f445e commit ef6eaee
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions selfspy/sniff_x.py
Expand Up @@ -54,6 +54,9 @@ def __init__(self):
self.record_display = display.Display()
self.keymap = self.the_display._keymap_codes

self.atom_NET_WM_NAME = self.the_display.intern_atom('_NET_WM_NAME')
self.atom_UTF8_STRING = self.the_display.intern_atom('UTF8_STRING')

def run(self):
# Check if the extension is present
if not self.record_display.has_extension("RECORD"):
Expand Down Expand Up @@ -159,6 +162,24 @@ def lookup_keysym(self, keysym):
return self.keysymdict[keysym]
return "[%d]" % keysym

def get_wm_name(self, win):
"""
Custom method to query for _NET_WM_NAME first, before falling back to
python-xlib's method, which (currently) only queries WM_NAME with
type=STRING."""

# Alternatively, we could also try WM_NAME with "UTF8_STRING" and
# "COMPOUND_TEXT", but _NET_WM_NAME should be good.

d = win.get_full_property(self.atom_NET_WM_NAME, self.atom_UTF8_STRING)
if d is None or d.format != 8:
# Fallback.
r = win.get_wm_name()
if r:
return r.decode('latin1') # WM_NAME with type=STRING.
else:
return d.value.decode('utf8')

def get_cur_window(self):
i = 0
cur_class = None
Expand All @@ -173,7 +194,7 @@ def get_cur_window(self):
if type(cur_window) is int:
return None, None, None

cur_name = cur_window.get_wm_name()
cur_name = self.get_wm_name(cur_window)
cur_class = cur_window.get_wm_class()

if cur_class:
Expand All @@ -186,7 +207,7 @@ def get_cur_window(self):
break
cur_class = cur_class or ''
cur_name = cur_name or ''
return cur_class.decode('latin1'), cur_window, cur_name.decode('latin1')
return cur_class.decode('latin1'), cur_window, cur_name

def get_geometry(self, cur_window):
i = 0
Expand Down

0 comments on commit ef6eaee

Please sign in to comment.