ignore BadMatch error for XConfigureWindow() calls
[awesome.git] / xutil.c
blob8e73e49b68740e7ad9d5d20324a94618bb0dda29
1 /*
2 * xutil.c - X-related useful functions
4 * Copyright © 2007 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>
24 #include <X11/extensions/Xinerama.h>
25 #include <sys/wait.h>
27 #include "xutil.h"
28 #include "client.h"
30 extern AwesomeConf globalconf;
32 /** Execute another process, replacing the current instance of Awesome
33 * \param screen Screen ID
34 * \param arg Command
35 * \ingroup ui_callback
37 void
38 uicb_exec(int screen __attribute__ ((unused)), char *arg)
40 Client *c;
41 char path[PATH_MAX];
43 /* remap all clients since some WM won't handle them otherwise */
44 for(c = globalconf.clients; c; c = c->next)
45 client_unban(c);
47 XSync(globalconf.display, False);
49 if(globalconf.display)
50 close(ConnectionNumber(globalconf.display));
52 sscanf(arg, "%s", path);
53 execlp(path, arg, NULL);
56 /** Spawn another process
57 * \param screen Screen ID
58 * \param arg Command
59 * \ingroup ui_callback
61 void
62 uicb_spawn(int screen, char *arg)
64 static char *shell = NULL;
65 char *display = NULL;
66 char *tmp, newdisplay[128];
68 if(!shell && !(shell = getenv("SHELL")))
69 shell = a_strdup("/bin/sh");
70 if(!arg)
71 return;
73 if(!XineramaIsActive(globalconf.display) && (tmp = getenv("DISPLAY")))
75 display = a_strdup(tmp);
76 if((tmp = strrchr(display, '.')))
77 *tmp = '\0';
78 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
79 setenv("DISPLAY", newdisplay, 1);
83 /* The double-fork construct avoids zombie processes and keeps the code
84 * clean from stupid signal handlers. */
85 if(fork() == 0)
87 if(fork() == 0)
89 if(globalconf.display)
90 close(ConnectionNumber(globalconf.display));
91 setsid();
92 execl(shell, shell, "-c", arg, (char *) NULL);
93 warn("execl '%s -c %s'", shell, arg);
94 perror(" failed");
96 exit(EXIT_SUCCESS);
98 wait(0);
101 Bool
102 xgettextprop(Window w, Atom atom, char *text, ssize_t textlen)
104 char **list = NULL;
105 int n;
106 XTextProperty name;
108 if(!text || !textlen)
109 return False;
111 text[0] = '\0';
112 XGetTextProperty(globalconf.display, w, &name, atom);
114 if(!name.nitems)
115 return False;
117 if(name.encoding == XA_STRING)
118 a_strncpy(text, textlen, (char *) name.value, textlen - 1);
120 else if(XmbTextPropertyToTextList(globalconf.display, &name, &list, &n) >= Success && n > 0 && *list)
122 a_strncpy(text, textlen, *list, textlen - 1);
123 XFreeStringList(list);
126 text[textlen - 1] = '\0';
127 XFree(name.value);
129 return True;
132 unsigned int
133 get_numlockmask(Display *disp)
135 XModifierKeymap *modmap;
136 unsigned int mask = 0;
137 int i, j;
139 modmap = XGetModifierMapping(disp);
140 for(i = 0; i < 8; i++)
141 for(j = 0; j < modmap->max_keypermod; j++)
142 if(modmap->modifiermap[i * modmap->max_keypermod + j]
143 == XKeysymToKeycode(disp, XK_Num_Lock))
144 mask = (1 << i);
146 XFreeModifiermap(modmap);
148 return mask;
151 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80