gchar *get_window_title (Display *disp, Window win)
{
gchar *title_utf8;
gchar *wm_name;
gchar *net_wm_name;
wm_name = get_property(disp, win, XA_STRING, "WM_NAME", NULL);
net_wm_name = get_property(disp, win,
XInternAtom(disp, "UTF8_STRING", False), "_NET_WM_NAME", NULL);
if (net_wm_name)
{
title_utf8 = g_strdup(net_wm_name);
}
else
{
if (wm_name)
{
title_utf8 = g_locale_to_utf8(wm_name, -1, NULL, NULL, NULL);
}
else
{
title_utf8 = NULL;
}
}
g_free(wm_name);
g_free(net_wm_name);
return title_utf8;
}
gchar *get_property (Display *disp, Window win,
Atom xa_prop_type, gchar *prop_name,
unsigned long *size)
{
Atom xa_prop_name;
Atom xa_ret_type;
int ret_format;
unsigned long ret_nitems;
unsigned long ret_bytes_after;
unsigned long tmp_size;
unsigned char *ret_prop;
gchar *ret;
xa_prop_name = XInternAtom(disp, prop_name, False);
/* MAX_PROPERTY_VALUE_LEN / 4 explanation (XGetWindowProperty manpage):
*
* long_length = Specifies the length in 32-bit multiples of the
* data to be retrieved.
*/
if (XGetWindowProperty(disp, win, xa_prop_name, 0, MAX_PROPERTY_VALUE_LEN / 4, False,
xa_prop_type, &xa_ret_type, &ret_format,
&ret_nitems, &ret_bytes_after, &ret_prop) != Success)
{
return NULL;
}
if (xa_ret_type != xa_prop_type)
{
XFree(ret_prop);
return NULL;
}
/* null terminate the result to make string handling easier */
tmp_size = (ret_format / 8) * ret_nitems;
ret = (gchar *)g_malloc(tmp_size + 1);
memcpy(ret, ret_prop, tmp_size);
ret[tmp_size] = '\0';
if (size)
{
*size = tmp_size;
}
XFree(ret_prop);
return ret;
}