rename some client functions
[awesome.git] / xutil.c
blobca2ac1cf6d10821c3d5f1dc2dde9c8ad374940e3
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.
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <X11/extensions/Xinerama.h>
24 #include <sys/wait.h>
26 #include "config.h"
27 #include "xutil.h"
28 #include "screen.h"
29 #include "common/util.h"
31 extern AwesomeConf globalconf;
33 /** Execute another process, replacing the current instance of Awesome
34 * \param screen Screen ID
35 * \param arg Command
36 * \ingroup ui_callback
38 void
39 uicb_exec(int screen __attribute__ ((unused)), char *arg)
41 char path[PATH_MAX];
42 if(globalconf.display)
43 close(ConnectionNumber(globalconf.display));
45 sscanf(arg, "%s", path);
46 execlp(path, arg, NULL);
49 /** Spawn another process
50 * \param screen Screen ID
51 * \param arg Command
52 * \ingroup ui_callback
54 void
55 uicb_spawn(int screen, char *arg)
57 static char *shell = NULL;
58 char *display = NULL;
59 char *tmp, newdisplay[128];
61 if(!shell && !(shell = getenv("SHELL")))
62 shell = a_strdup("/bin/sh");
63 if(!arg)
64 return;
66 if(!XineramaIsActive(globalconf.display) && (tmp = getenv("DISPLAY")))
68 display = a_strdup(tmp);
69 if((tmp = strrchr(display, '.')))
70 *tmp = '\0';
71 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
72 setenv("DISPLAY", newdisplay, 1);
76 /* The double-fork construct avoids zombie processes and keeps the code
77 * clean from stupid signal handlers. */
78 if(fork() == 0)
80 if(fork() == 0)
82 if(globalconf.display)
83 close(ConnectionNumber(globalconf.display));
84 setsid();
85 execl(shell, shell, "-c", arg, (char *) NULL);
86 warn("execl '%s -c %s'", shell, arg);
87 perror(" failed");
89 exit(EXIT_SUCCESS);
91 wait(0);
94 Bool
95 xgettextprop(Window w, Atom atom, char *text, ssize_t textlen)
97 char **list = NULL;
98 int n;
100 XTextProperty name;
102 if(!text || !textlen)
103 return False;
105 text[0] = '\0';
106 XGetTextProperty(globalconf.display, w, &name, atom);
108 if(!name.nitems)
109 return False;
111 if(name.encoding == XA_STRING)
112 a_strncpy(text, textlen, (char *) name.value, textlen - 1);
114 else if(XmbTextPropertyToTextList(globalconf.display, &name, &list, &n) >= Success && n > 0 && *list)
116 a_strncpy(text, textlen, *list, textlen - 1);
117 XFreeStringList(list);
120 text[textlen - 1] = '\0';
121 XFree(name.value);
123 return True;
126 unsigned int
127 get_numlockmask(Display *disp)
129 XModifierKeymap *modmap;
130 unsigned int mask = 0;
131 int i, j;
133 modmap = XGetModifierMapping(disp);
134 for(i = 0; i < 8; i++)
135 for(j = 0; j < modmap->max_keypermod; j++)
136 if(modmap->modifiermap[i * modmap->max_keypermod + j]
137 == XKeysymToKeycode(disp, XK_Num_Lock))
138 mask = (1 << i);
140 XFreeModifiermap(modmap);
142 return mask;
145 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80