Add a common error infratructure for widget_tell
[awesome.git] / common / xutil.c
blobb1ee741b59b65be8611a75659ba0862bb0e19f47
1 /*
2 * common/xutil.c - X-related useful functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
25 #include "common/util.h"
26 #include "common/xutil.h"
28 Bool
29 xgettextprop(Display *disp, Window w, Atom atom, char *text, ssize_t textlen)
31 char **list = NULL;
32 int n;
33 XTextProperty name;
35 if(!text || !textlen)
36 return False;
38 text[0] = '\0';
39 XGetTextProperty(disp, w, &name, atom);
41 if(!name.nitems)
42 return False;
44 if(name.encoding == XA_STRING)
45 a_strncpy(text, textlen, (char *) name.value, textlen - 1);
47 else if(XmbTextPropertyToTextList(disp, &name, &list, &n) >= Success && n > 0 && *list)
49 a_strncpy(text, textlen, *list, textlen - 1);
50 XFreeStringList(list);
53 text[textlen - 1] = '\0';
54 XFree(name.value);
56 return True;
59 unsigned int
60 get_numlockmask(Display *disp)
62 XModifierKeymap *modmap;
63 unsigned int mask = 0;
64 int i, j;
66 modmap = XGetModifierMapping(disp);
67 for(i = 0; i < 8; i++)
68 for(j = 0; j < modmap->max_keypermod; j++)
69 if(modmap->modifiermap[i * modmap->max_keypermod + j]
70 == XKeysymToKeycode(disp, XK_Num_Lock))
71 mask = (1 << i);
73 XFreeModifiermap(modmap);
75 return mask;
78 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80