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.
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
34 die(const char *fmt
, ...)
39 vfprintf(stderr
, fmt
, ap
);
45 eprint(const char *fmt
, ...)
50 vfprintf(stderr
, fmt
, ap
);
56 uicb_spawn(Display
* disp
,
57 DC
*drawcontext
__attribute__ ((unused
)),
58 awesome_config
* awesomeconf
__attribute__ ((unused
)),
61 static char *shell
= NULL
;
63 char *tmp
, newdisplay
[128];
65 if(!shell
&& !(shell
= getenv("SHELL")))
66 shell
= strdup("/bin/sh");
70 if((tmp
= getenv("DISPLAY")))
72 display
= strdup(tmp
);
73 if((tmp
= strrchr(display
, '.')))
75 snprintf(newdisplay
, sizeof(newdisplay
), "%s.%d", display
, awesomeconf
->screen
);
76 setenv("DISPLAY", newdisplay
, 1);
80 /* The double-fork construct avoids zombie processes and keeps the code
81 * clean from stupid signal handlers. */
87 close(ConnectionNumber(disp
));
89 execl(shell
, shell
, "-c", arg
, (char *) NULL
);
90 fprintf(stderr
, "awesome: execl '%s -c %s'", shell
, arg
);
99 xgettextprop(Display
*disp
, Window w
, Atom atom
, char *text
, unsigned int size
)
106 if(!text
|| size
== 0)
110 XGetTextProperty(disp
, w
, &name
, atom
);
115 if(name
.encoding
== XA_STRING
)
116 strncpy(text
, (char *) name
.value
, size
- 1);
118 else if(XmbTextPropertyToTextList(disp
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
)
120 strncpy(text
, *list
, size
- 1);
121 XFreeStringList(list
);
124 text
[size
- 1] = '\0';
131 compute_new_value_from_arg(const char *arg
, double current_value
)
135 if(arg
&& sscanf(arg
, "%lf", &delta
) == 1)
137 if(arg
[0] == '+' || arg
[0] == '-')
138 current_value
+= delta
;
140 current_value
= delta
;
143 return current_value
;