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);
169 static glob_t globbuf;
170 char *head, *tail, *result;
172 tail = strchr(path,
'/');
173 head = strndup(path, tail ? (
size_t)(tail - path) : strlen(path));
175 int res = glob(head, GLOB_TILDE, NULL, &globbuf);
178 if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1)
181 die(
"glob() failed");
183 head = globbuf.gl_pathv[0];
184 result =
scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1);
185 strncpy(result, head, strlen(head));
187 strncat(result, tail, strlen(tail));
200 return (stat(path, &buf) == 0);
211 for (num_args = 0; original[num_args] != NULL; num_args++) {
212 DLOG(
"original argument: \"%s\"\n", original[num_args]);
214 if (strcmp(original[num_args], argument) == 0)
218 char **result =
smalloc((num_args + 2) *
sizeof(
char *));
219 memcpy(result, original, num_args *
sizeof(
char *));
220 result[num_args] = argument;
221 result[num_args + 1] = NULL;
226 #define y(x, ...) yajl_gen_##x(gen, ##__VA_ARGS__)
227 #define ystr(str) yajl_gen_string(gen, (unsigned char *)str, strlen(str))
230 setlocale(LC_NUMERIC,
"C");
231 yajl_gen gen = yajl_gen_alloc(NULL);
235 setlocale(LC_NUMERIC,
"");
237 const unsigned char *payload;
239 y(get_buf, &payload, &length);
252 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
260 while (written < length) {
261 int n = write(fd, payload + written, length - written);
270 DLOG(
"write == 0?\n");
276 DLOG(
"written: %zd of %zd\n", written, length);
281 DLOG(
"layout: %.*s\n", (
int)length, payload);
309 if (restart_filename != NULL) {
312 for (num_args = 0;
start_argv[num_args] != NULL; num_args++)
314 char **new_argv =
scalloc((num_args + 3) *
sizeof(
char *));
318 bool skip_next =
false;
319 for (
int i = 0; i < num_args; ++i) {
330 new_argv[write_index++] =
"--restart";
331 new_argv[write_index] = restart_filename;
341 #if defined(__OpenBSD__) || defined(__APPLE__)
348 void *memmem(
const void *l,
size_t l_len,
const void *s,
size_t s_len) {
349 register char *cur, *last;
350 const char *cl = (
const char *)l;
351 const char *cs = (
const char *)s;
354 if (l_len == 0 || s_len == 0)
363 return memchr(l, (
int)*cs, l_len);
366 last = (
char *)cl + l_len - s_len;
368 for (cur = (
char *)cl; cur <= last; cur++)
369 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
383 ev_child_stop(EV_A_ watcher);
385 if (!WIFEXITED(watcher->rstatus)) {
386 ELOG(
"ERROR: i3-nagbar did not exit normally.\n");
390 int exitcode = WEXITSTATUS(watcher->rstatus);
391 DLOG(
"i3-nagbar process exited with status %d\n", exitcode);
393 ELOG(
"ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
396 *((pid_t *)watcher->data) = -1;
405 pid_t *nagbar_pid = (pid_t *)watcher->data;
406 if (*nagbar_pid != -1) {
407 LOG(
"Sending SIGKILL (%d) to i3-nagbar with PID %d\n", SIGKILL, *nagbar_pid);
408 kill(*nagbar_pid, SIGKILL);
421 if (*nagbar_pid != -1) {
422 DLOG(
"i3-nagbar already running (PID %d), not starting again.\n", *nagbar_pid);
426 *nagbar_pid = fork();
427 if (*nagbar_pid == -1) {
428 warn(
"Could not fork()");
433 if (*nagbar_pid == 0)
436 DLOG(
"Starting i3-nagbar with PID %d\n", *nagbar_pid);
440 ev_child *child =
smalloc(
sizeof(ev_child));
442 child->data = nagbar_pid;
447 ev_cleanup *cleanup =
smalloc(
sizeof(ev_cleanup));
449 cleanup->data = nagbar_pid;
461 if (*nagbar_pid == -1)
464 if (kill(*nagbar_pid, SIGTERM) == -1)
465 warn(
"kill(configerror_nagbar) failed");
474 waitpid(*nagbar_pid, NULL, 0);
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...
struct reservedpx __attribute__
static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent)
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
void ipc_shutdown(void)
Calls shutdown() on each socket and closes it.
Rect rect_add(Rect a, Rect b)
void exec_i3_utility(char *name, char *argv[])
exec()s an i3 utility, for example the config file migration script or i3-nagbar. ...
char * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
char * store_restart_layout(void)
Stores a rectangle, for example the size of a window, the child window etc.
struct ev_loop * main_loop
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
static char ** append_argument(char **original, char *argument)
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
pid_t command_error_nagbar_pid
pid_t config_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...
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...
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
const char * restart_state_path
static void nagbar_exited(EV_P_ ev_child *watcher, int revents)
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
void restore_geometry(void)
Restores the geometry of each window by reparenting it to the root window at the position of its fram...
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...