Add --(help|version) to awesome-message
[awesome.git] / xutil.c
blobdf74f12c1995c0e3390995f70083ec9fb98f615e
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 "structs.h"
28 #include "xutil.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 char path[PATH_MAX];
41 if(globalconf.display)
42 close(ConnectionNumber(globalconf.display));
44 sscanf(arg, "%s", path);
45 execlp(path, arg, NULL);
48 /** Spawn another process
49 * \param screen Screen ID
50 * \param arg Command
51 * \ingroup ui_callback
53 void
54 uicb_spawn(int screen, char *arg)
56 static char *shell = NULL;
57 char *display = NULL;
58 char *tmp, newdisplay[128];
60 if(!shell && !(shell = getenv("SHELL")))
61 shell = a_strdup("/bin/sh");
62 if(!arg)
63 return;
65 if(!XineramaIsActive(globalconf.display) && (tmp = getenv("DISPLAY")))
67 display = a_strdup(tmp);
68 if((tmp = strrchr(display, '.')))
69 *tmp = '\0';
70 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, screen);
71 setenv("DISPLAY", newdisplay, 1);
75 /* The double-fork construct avoids zombie processes and keeps the code
76 * clean from stupid signal handlers. */
77 if(fork() == 0)
79 if(fork() == 0)
81 if(globalconf.display)
82 close(ConnectionNumber(globalconf.display));
83 setsid();
84 execl(shell, shell, "-c", arg, (char *) NULL);
85 warn("execl '%s -c %s'", shell, arg);
86 perror(" failed");
88 exit(EXIT_SUCCESS);
90 wait(0);
93 Bool
94 xgettextprop(Window w, Atom atom, char *text, ssize_t textlen)
96 char **list = NULL;
97 int n;
99 XTextProperty name;
101 if(!text || !textlen)
102 return False;
104 text[0] = '\0';
105 XGetTextProperty(globalconf.display, w, &name, atom);
107 if(!name.nitems)
108 return False;
110 if(name.encoding == XA_STRING)
111 a_strncpy(text, textlen, (char *) name.value, textlen - 1);
113 else if(XmbTextPropertyToTextList(globalconf.display, &name, &list, &n) >= Success && n > 0 && *list)
115 a_strncpy(text, textlen, *list, textlen - 1);
116 XFreeStringList(list);
119 text[textlen - 1] = '\0';
120 XFree(name.value);
122 return True;
125 unsigned int
126 get_numlockmask(Display *disp)
128 XModifierKeymap *modmap;
129 unsigned int mask = 0;
130 int i, j;
132 modmap = XGetModifierMapping(disp);
133 for(i = 0; i < 8; i++)
134 for(j = 0; j < modmap->max_keypermod; j++)
135 if(modmap->modifiermap[i * modmap->max_keypermod + j]
136 == XKeysymToKeycode(disp, XK_Num_Lock))
137 mask = (1 << i);
139 XFreeModifiermap(modmap);
141 return mask;
144 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80