restack on max
[awesome.git] / xutil.c
blob0f118de8a4b35c30e1823ae341ed22086ed79195
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 "util.h"
28 #include "xutil.h"
29 #include "screen.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;
127 /** Initialize an X color
128 * \param screen Screen number
129 * \param colstr Color specification
131 XColor
132 initxcolor(int phys_screen, const char *colstr)
134 XColor screenColor, exactColor;
136 if(!XAllocNamedColor(globalconf.display,
137 DefaultColormap(globalconf.display, phys_screen),
138 colstr,
139 &screenColor,
140 &exactColor))
141 eprint("awesome: error, cannot allocate color '%s'\n", colstr);
143 return screenColor;
146 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80