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.
29 #include <X11/Xutil.h>
30 #include <X11/Xatom.h>
31 #include <X11/extensions/Xinerama.h>
36 die(const char *fmt
, ...)
41 vfprintf(stderr
, fmt
, ap
);
47 eprint(const char *fmt
, ...)
52 vfprintf(stderr
, fmt
, ap
);
58 uicb_exec(awesome_config
* awesomeconf
,
62 if(awesomeconf
->display
)
63 close(ConnectionNumber(awesomeconf
->display
));
65 sscanf(arg
, "%s", path
);
66 execlp(path
, arg
, NULL
);
70 uicb_spawn(awesome_config
* awesomeconf
,
73 static char *shell
= NULL
;
75 char *tmp
, newdisplay
[128];
77 if(!shell
&& !(shell
= getenv("SHELL")))
78 shell
= a_strdup("/bin/sh");
82 if(!XineramaIsActive(awesomeconf
->display
) && (tmp
= getenv("DISPLAY")))
84 display
= a_strdup(tmp
);
85 if((tmp
= strrchr(display
, '.')))
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. */
98 if(awesomeconf
->display
)
99 close(ConnectionNumber(awesomeconf
->display
));
101 execl(shell
, shell
, "-c", arg
, (char *) NULL
);
102 fprintf(stderr
, "awesome: execl '%s -c %s'", shell
, arg
);
111 xgettextprop(Display
*disp
, Window w
, Atom atom
, char *text
, ssize_t textlen
)
118 if(!text
|| !textlen
)
122 XGetTextProperty(disp
, w
, &name
, atom
);
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';
143 compute_new_value_from_arg(const char *arg
, double current_value
)
147 if(arg
&& sscanf(arg
, "%lf", &delta
) == 1)
149 if(arg
[0] == '+' || arg
[0] == '-')
150 current_value
+= delta
;
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
166 name_func_lookup(const char *funcname
, const NameFuncLink
* list
)
171 for(i
= 0; list
[i
].name
; i
++)
172 if(!a_strcmp(funcname
, list
[i
].name
))
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
);
196 ssize_t dlen
= MIN(n
- 1, len
);
197 memcpy(dst
, src
, dlen
);
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
215 ssize_t
a_strcpy(char *dst
, ssize_t n
, const char *src
)
217 ssize_t len
= a_strlen(src
);
221 ssize_t dlen
= MIN(n
- 1, len
);
222 memcpy(dst
, src
, dlen
);
228 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99