stop using applyrules() and do some stuff ourselves
[awesome.git] / util.c
blob4721344f2347b984ed6f8277d38d5a16887a038d
1 /*
2 * util.c - useful functions
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <limits.h>
29 #include <X11/Xutil.h>
30 #include <X11/Xatom.h>
31 #include <X11/extensions/Xinerama.h>
33 #include "util.h"
35 void
36 die(const char *fmt, ...)
38 va_list ap;
40 va_start(ap, fmt);
41 vfprintf(stderr, fmt, ap);
42 va_end(ap);
43 abort();
46 void
47 eprint(const char *fmt, ...)
49 va_list ap;
51 va_start(ap, fmt);
52 vfprintf(stderr, fmt, ap);
53 va_end(ap);
54 exit(EXIT_FAILURE);
57 void
58 uicb_exec(awesome_config * awesomeconf,
59 const char *arg)
61 char path[PATH_MAX];
62 if(awesomeconf->display)
63 close(ConnectionNumber(awesomeconf->display));
65 sscanf(arg, "%s", path);
66 execlp(path, arg, NULL);
69 void
70 uicb_spawn(awesome_config * awesomeconf,
71 const char *arg)
73 static char *shell = NULL;
74 char *display = NULL;
75 char *tmp, newdisplay[128];
77 if(!shell && !(shell = getenv("SHELL")))
78 shell = a_strdup("/bin/sh");
79 if(!arg)
80 return;
82 if(!XineramaIsActive(awesomeconf->display) && (tmp = getenv("DISPLAY")))
84 display = a_strdup(tmp);
85 if((tmp = strrchr(display, '.')))
86 *tmp = '\0';
87 snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, awesomeconf->screen);
88 setenv("DISPLAY", newdisplay, 1);
92 /* The double-fork construct avoids zombie processes and keeps the code
93 * clean from stupid signal handlers. */
94 if(fork() == 0)
96 if(fork() == 0)
98 if(awesomeconf->display)
99 close(ConnectionNumber(awesomeconf->display));
100 setsid();
101 execl(shell, shell, "-c", arg, (char *) NULL);
102 fprintf(stderr, "awesome: execl '%s -c %s'", shell, arg);
103 perror(" failed");
105 exit(EXIT_SUCCESS);
107 wait(0);
110 Bool
111 xgettextprop(Display *disp, Window w, Atom atom, char *text, ssize_t textlen)
113 char **list = NULL;
114 int n;
116 XTextProperty name;
118 if(!text || !textlen)
119 return False;
121 text[0] = '\0';
122 XGetTextProperty(disp, w, &name, atom);
124 if(!name.nitems)
125 return False;
127 if(name.encoding == XA_STRING)
128 a_strncpy(text, textlen, (char *) name.value, textlen - 1);
130 else if(XmbTextPropertyToTextList(disp, &name, &list, &n) >= Success && n > 0 && *list)
132 a_strncpy(text, textlen, *list, textlen - 1);
133 XFreeStringList(list);
136 text[textlen - 1] = '\0';
137 XFree(name.value);
139 return True;
142 double
143 compute_new_value_from_arg(const char *arg, double current_value)
145 double delta;
147 if(arg && sscanf(arg, "%lf", &delta) == 1)
149 if(arg[0] == '+' || arg[0] == '-')
150 current_value += delta;
151 else
152 current_value = delta;
155 return current_value;
159 /** Lookup for a function pointer from its name
160 * in the given NameFuncLink list
161 * \param funcname Function name
162 * \param list Function and name link list
163 * \return function pointer
165 void *
166 name_func_lookup(const char *funcname, const NameFuncLink * list)
168 int i;
170 if(funcname && list)
171 for(i = 0; list[i].name; i++)
172 if(!a_strcmp(funcname, list[i].name))
173 return list[i].func;
175 return NULL;
178 /** \brief safe limited strcpy.
180 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
181 * always adding a final \c \\0 in \c dst.
183 * \param[in] dst destination buffer.
184 * \param[in] n size of the buffer. Negative sizes are allowed.
185 * \param[in] src source string.
186 * \param[in] l maximum number of chars to copy.
188 * \return minimum of \c src \e length and \c l.
190 ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
192 ssize_t len = MIN(a_strlen(src), l);
194 if (n > 0)
196 ssize_t dlen = MIN(n - 1, len);
197 memcpy(dst, src, dlen);
198 dst[dlen] = '\0';
201 return len;
204 /** \brief safe strcpy.
206 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
207 * adding a final \c \\0 in \c dst.
209 * \param[in] dst destination buffer.
210 * \param[in] n size of the buffer. Negative sizes are allowed.
211 * \param[in] src source string.
212 * \return \c src \e length. If this value is \>= \c n then the copy was
213 * truncated.
215 ssize_t a_strcpy(char *dst, ssize_t n, const char *src)
217 ssize_t len = a_strlen(src);
219 if (n > 0)
221 ssize_t dlen = MIN(n - 1, len);
222 memcpy(dst, src, dlen);
223 dst[dlen] = '\0';
226 return len;
228 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99