optimize computing a bit
[awesome.git] / util.c
blobb247b7e16b350f20a4af60671d97a065cda0dda9
1 /*
2 * util.c - useful functions
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
6 *
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>
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
31 #include "util.h"
33 void
34 eprint(const char *fmt, ...)
36 va_list ap;
38 va_start(ap, fmt);
39 vfprintf(stderr, fmt, ap);
40 va_end(ap);
41 exit(EXIT_FAILURE);
44 void
45 uicb_spawn(Display * disp,
46 DC *drawcontext __attribute__ ((unused)),
47 awesome_config * awesomeconf __attribute__ ((unused)),
48 const char *arg)
50 static char *shell = NULL;
52 if(!shell && !(shell = getenv("SHELL")))
53 shell = strdup("/bin/sh");
54 if(!arg)
55 return;
56 /* The double-fork construct avoids zombie processes and keeps the code
57 * clean from stupid signal handlers. */
58 if(fork() == 0)
60 if(fork() == 0)
62 if(disp)
63 close(ConnectionNumber(disp));
64 setsid();
65 execl(shell, shell, "-c", arg, (char *) NULL);
66 fprintf(stderr, "awesome: execl '%s -c %s'", shell, arg);
67 perror(" failed");
69 exit(EXIT_SUCCESS);
71 wait(0);
74 Bool
75 xgettextprop(Display *disp, Window w, Atom atom, char *text, unsigned int size)
77 char **list = NULL;
78 int n;
80 XTextProperty name;
82 if(!text || size == 0)
83 return False;
85 text[0] = '\0';
86 XGetTextProperty(disp, w, &name, atom);
88 if(!name.nitems)
89 return False;
91 if(name.encoding == XA_STRING)
92 strncpy(text, (char *) name.value, size - 1);
94 else if(XmbTextPropertyToTextList(disp, &name, &list, &n) >= Success && n > 0 && *list)
96 strncpy(text, *list, size - 1);
97 XFreeStringList(list);
100 text[size - 1] = '\0';
101 XFree(name.value);
103 return True;
106 double
107 compute_new_value_from_arg(const char *arg, double current_value)
109 double delta;
111 if(arg && sscanf(arg, "%lf", &delta) == 1)
113 if(arg[0] == '+' || arg[0] == '-')
114 current_value += delta;
115 else
116 current_value = delta;
119 return current_value;