simplify buf handling
[awesome.git] / xutil.c
blob11319cb5bb38c30ab2b65b1ac361f15e1e62dd60
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;
107 XTextProperty name;
109 if(!text || !textlen)
110 return False;
112 text[0] = '\0';
113 XGetTextProperty(globalconf.display, w, &name, atom);
115 if(!name.nitems)
116 return False;
118 if(name.encoding == XA_STRING)
119 a_strncpy(text, textlen, (char *) name.value, textlen - 1);
121 else if(XmbTextPropertyToTextList(globalconf.display, &name, &list, &n) >= Success && n > 0 && *list)
123 a_strncpy(text, textlen, *list, textlen - 1);
124 XFreeStringList(list);
127 text[textlen - 1] = '\0';
128 XFree(name.value);
130 return True;
133 unsigned int
134 get_numlockmask(Display *disp)
136 XModifierKeymap *modmap;
137 unsigned int mask = 0;
138 int i, j;
140 modmap = XGetModifierMapping(disp);
141 for(i = 0; i < 8; i++)
142 for(j = 0; j < modmap->max_keypermod; j++)
143 if(modmap->modifiermap[i * modmap->max_keypermod + j]
144 == XKeysymToKeycode(disp, XK_Num_Lock))
145 mask = (1 << i);
147 XFreeModifiermap(modmap);
149 return mask;
152 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80