2 #define I3__FILE__ "util.c"
17 #if defined(__OpenBSD__)
18 #include <sys/cdefs.h>
22 #include <yajl/yajl_version.h>
26 #define SN_API_NOT_YET_FROZEN 1
27 #include <libsn/sn-launcher.h>
29 int min(
int a,
int b) {
30 return (a < b ? a : b);
33 int max(
int a,
int b) {
34 return (a > b ? a : b);
38 return (x >= rect.
x &&
39 x <= (rect.
x + rect.
width) &&
64 for (
size_t i = 0; i < strlen(name); i++)
65 if (!isdigit(name[i]))
79 long parsed_num = strtol(name, &endptr, 10);
80 if (parsed_num == LONG_MIN ||
81 parsed_num == LONG_MAX ||
96 uint32_t old_value = *destination;
98 return ((*destination = new_value) != old_value);
118 char *migratepath = name;
119 argv[0] = migratepath;
120 execvp(migratepath, argv);
126 char *dir = dirname(pathbuf);
127 sasprintf(&migratepath,
"%s/%s", dir, name);
128 argv[0] = migratepath;
129 execvp(migratepath, argv);
131 #if defined(__linux__)
134 if (readlink(
"/proc/self/exe", buffer, BUFSIZ) == -1) {
135 warn(
"could not read /proc/self/exe");
138 dir = dirname(buffer);
139 sasprintf(&migratepath,
"%s/%s", dir, name);
140 argv[0] = migratepath;
141 execvp(migratepath, argv);
144 warn(
"Could not start %s", name);
153 void check_error(xcb_connection_t *
conn, xcb_void_cookie_t cookie,
char *err_message) {
154 xcb_generic_error_t *error = xcb_request_check(conn, cookie);
156 fprintf(stderr,
"ERROR: %s (X error %d)\n", err_message, error->error_code);
157 xcb_disconnect(conn);
168 return (stat(path, &buf) == 0);
175 static char **
add_argument(
char **original,
char *opt_char,
char *opt_arg,
char *opt_name) {
177 for (num_args = 0; original[num_args] != NULL; num_args++)
179 char **result =
scalloc(num_args + 3,
sizeof(
char *));
183 bool skip_next =
false;
184 for (
int i = 0; i < num_args; ++i) {
189 if (!strcmp(original[i], opt_char) ||
190 (opt_name && !strcmp(original[i], opt_name))) {
195 result[write_index++] = original[i];
199 result[write_index++] = opt_char;
200 result[write_index] = opt_arg;
205 #define y(x, ...) yajl_gen_##x(gen, ##__VA_ARGS__)
206 #define ystr(str) yajl_gen_string(gen, (unsigned char *)str, strlen(str))
209 setlocale(LC_NUMERIC,
"C");
210 yajl_gen gen = yajl_gen_alloc(NULL);
214 setlocale(LC_NUMERIC,
"");
216 const unsigned char *payload;
218 y(get_buf, &payload, &length);
233 char *filenamecopy =
sstrdup(filename);
234 char *base = dirname(filenamecopy);
235 DLOG(
"Creating \"%s\" for storing the restart layout\n", base);
237 ELOG(
"Could not create \"%s\" for storing the restart layout, layout will be lost.\n", base);
240 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
247 if (
writeall(fd, payload, length) == -1) {
248 ELOG(
"Could not write restart layout to \"%s\", layout will be lost: %s\n", filename, strerror(errno));
257 DLOG(
"layout: %.*s\n", (
int)length, payload);
290 if (restart_filename != NULL) {
299 #if defined(__OpenBSD__) || defined(__APPLE__)
306 void *memmem(
const void *l,
size_t l_len,
const void *s,
size_t s_len) {
307 register char *cur, *last;
308 const char *cl = (
const char *)l;
309 const char *cs = (
const char *)s;
312 if (l_len == 0 || s_len == 0)
321 return memchr(l, (
int)*cs, l_len);
324 last = (
char *)cl + l_len - s_len;
326 for (cur = (
char *)cl; cur <= last; cur++)
327 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
344 char *escaped = g_markup_escape_text(input, -1);
356 ev_child_stop(EV_A_ watcher);
358 if (!WIFEXITED(watcher->rstatus)) {
359 ELOG(
"ERROR: i3-nagbar did not exit normally.\n");
363 int exitcode = WEXITSTATUS(watcher->rstatus);
364 DLOG(
"i3-nagbar process exited with status %d\n", exitcode);
366 ELOG(
"ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
369 *((pid_t *)watcher->data) = -1;
378 pid_t *nagbar_pid = (pid_t *)watcher->data;
379 if (*nagbar_pid != -1) {
380 LOG(
"Sending SIGKILL (%d) to i3-nagbar with PID %d\n", SIGKILL, *nagbar_pid);
381 kill(*nagbar_pid, SIGKILL);
394 if (*nagbar_pid != -1) {
395 DLOG(
"i3-nagbar already running (PID %d), not starting again.\n", *nagbar_pid);
399 *nagbar_pid = fork();
400 if (*nagbar_pid == -1) {
401 warn(
"Could not fork()");
406 if (*nagbar_pid == 0)
409 DLOG(
"Starting i3-nagbar with PID %d\n", *nagbar_pid);
413 ev_child *child =
smalloc(
sizeof(ev_child));
415 child->data = nagbar_pid;
420 ev_cleanup *cleanup =
smalloc(
sizeof(ev_cleanup));
422 cleanup->data = nagbar_pid;
434 if (*nagbar_pid == -1)
437 if (kill(*nagbar_pid, SIGTERM) == -1)
438 warn(
"kill(configerror_nagbar) failed");
447 waitpid(*nagbar_pid, NULL, 0);
void restore_geometry(void)
Restores the geometry of each window by reparenting it to the root window at the position of its fram...
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
bool get_debug_logging(void)
Checks if debug logging is active.
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
Rect rect_add(Rect a, Rect b)
xcb_connection_t * conn
XCB connection and root screen.
char * store_restart_layout(void)
static char ** add_argument(char **original, char *opt_char, char *opt_arg, char *opt_name)
char * pango_escape_markup(char *input)
Escapes the given string if a pango font is currently used.
pid_t config_error_nagbar_pid
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
Rect rect_sub(Rect a, Rect b)
void kill_nagbar(pid_t *nagbar_pid, bool wait_for_it)
Kills the i3-nagbar process, if *nagbar_pid != -1.
void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie, char *err_message)
Checks a generic cookie for errors and quits with the given message if there was an error...
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
ssize_t writeall(int fd, const void *buf, size_t count)
Wrapper around correct write which returns -1 (meaning that write failed) or count (meaning that all ...
static void nagbar_exited(EV_P_ ev_child *watcher, int revents)
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
Stores a rectangle, for example the size of a window, the child window etc.
void ipc_shutdown(void)
Calls shutdown() on each socket and closes it.
struct ev_loop * main_loop
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
pid_t command_error_nagbar_pid
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
int mkdirp(const char *path, mode_t mode)
Emulates mkdir -p (creates any missing folders)
bool update_if_necessary(uint32_t *destination, const uint32_t new_value)
Updates *destination with new_value and returns true if it was changed or false if it was the same...
char * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent)
struct reservedpx __attribute__
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
void exec_i3_utility(char *name, char *argv[])
exec()s an i3 utility, for example the config file migration script or i3-nagbar. ...
bool font_is_pango(void)
Returns true if and only if the current font is a pango font.
char * restart_state_path