15 #if defined(__OpenBSD__) 16 #include <sys/cdefs.h> 20 #include <yajl/yajl_version.h> 24 #define SN_API_NOT_YET_FROZEN 1 25 #include <libsn/sn-launcher.h> 27 int min(
int a,
int b) {
28 return (a < b ? a : b);
31 int max(
int a,
int b) {
32 return (a > b ? a : b);
36 return (x >= rect.
x &&
37 x <= (rect.
x + rect.
width) &&
62 for (
size_t i = 0; i < strlen(name); i++)
63 if (!isdigit(name[i]))
76 if (strcmp(layout_str,
"default") == 0) {
79 }
else if (strcasecmp(layout_str,
"stacked") == 0 ||
80 strcasecmp(layout_str,
"stacking") == 0) {
83 }
else if (strcasecmp(layout_str,
"tabbed") == 0) {
86 }
else if (strcasecmp(layout_str,
"splitv") == 0) {
89 }
else if (strcasecmp(layout_str,
"splith") == 0) {
105 long parsed_num = strtol(name, &endptr, 10);
106 if (parsed_num == LONG_MIN ||
107 parsed_num == LONG_MAX ||
122 uint32_t old_value = *destination;
124 return ((*destination = new_value) != old_value);
144 char *migratepath = name;
145 argv[0] = migratepath;
146 execvp(migratepath, argv);
152 char *dir = dirname(pathbuf);
153 sasprintf(&migratepath,
"%s/%s", dir, name);
154 argv[0] = migratepath;
155 execvp(migratepath, argv);
157 #if defined(__linux__) 160 if (readlink(
"/proc/self/exe", buffer, BUFSIZ) == -1) {
161 warn(
"could not read /proc/self/exe");
164 dir = dirname(buffer);
165 sasprintf(&migratepath,
"%s/%s", dir, name);
166 argv[0] = migratepath;
167 execvp(migratepath, argv);
170 warn(
"Could not start %s", name);
180 return (stat(path, &buf) == 0);
187 static char **
add_argument(
char **original,
char *opt_char,
char *opt_arg,
char *opt_name) {
189 for (num_args = 0; original[num_args] != NULL; num_args++)
191 char **result =
scalloc(num_args + 3,
sizeof(
char *));
195 bool skip_next =
false;
196 for (
int i = 0; i < num_args; ++i) {
201 if (!strcmp(original[i], opt_char) ||
202 (opt_name && !strcmp(original[i], opt_name))) {
207 result[write_index++] = original[i];
211 result[write_index++] = opt_char;
212 result[write_index] = opt_arg;
217 #define y(x, ...) yajl_gen_##x(gen, ##__VA_ARGS__) 218 #define ystr(str) yajl_gen_string(gen, (unsigned char *)str, strlen(str)) 221 setlocale(LC_NUMERIC,
"C");
222 yajl_gen gen = yajl_gen_alloc(NULL);
226 setlocale(LC_NUMERIC,
"");
228 const unsigned char *payload;
230 y(get_buf, &payload, &length);
245 char *filenamecopy =
sstrdup(filename);
246 char *base = dirname(filenamecopy);
247 DLOG(
"Creating \"%s\" for storing the restart layout\n", base);
249 ELOG(
"Could not create \"%s\" for storing the restart layout, layout will be lost.\n", base);
252 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
259 if (
writeall(fd, payload, length) == -1) {
260 ELOG(
"Could not write restart layout to \"%s\", layout will be lost: %s\n", filename, strerror(errno));
269 DLOG(
"layout: %.*s\n", (
int)length, payload);
302 if (restart_filename != NULL) {
311 #if defined(__OpenBSD__) || defined(__APPLE__) 318 void *memmem(
const void *l,
size_t l_len,
const void *s,
size_t s_len) {
319 register char *cur, *last;
320 const char *cl = (
const char *)l;
321 const char *cs = (
const char *)s;
324 if (l_len == 0 || s_len == 0)
333 return memchr(l, (
int)*cs, l_len);
336 last = (
char *)cl + l_len - s_len;
338 for (cur = (
char *)cl; cur <= last; cur++)
339 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
356 char *escaped = g_markup_escape_text(input, -1);
368 ev_child_stop(EV_A_ watcher);
370 if (!WIFEXITED(watcher->rstatus)) {
371 ELOG(
"ERROR: i3-nagbar did not exit normally.\n");
375 int exitcode = WEXITSTATUS(watcher->rstatus);
376 DLOG(
"i3-nagbar process exited with status %d\n", exitcode);
378 ELOG(
"ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
381 *((pid_t *)watcher->data) = -1;
390 pid_t *nagbar_pid = (pid_t *)watcher->data;
391 if (*nagbar_pid != -1) {
392 LOG(
"Sending SIGKILL (%d) to i3-nagbar with PID %d\n", SIGKILL, *nagbar_pid);
393 kill(*nagbar_pid, SIGKILL);
406 if (*nagbar_pid != -1) {
407 DLOG(
"i3-nagbar already running (PID %d), not starting again.\n", *nagbar_pid);
411 *nagbar_pid = fork();
412 if (*nagbar_pid == -1) {
413 warn(
"Could not fork()");
418 if (*nagbar_pid == 0)
421 DLOG(
"Starting i3-nagbar with PID %d\n", *nagbar_pid);
425 ev_child *child =
smalloc(
sizeof(ev_child));
427 child->data = nagbar_pid;
432 ev_cleanup *cleanup =
smalloc(
sizeof(ev_cleanup));
434 cleanup->data = nagbar_pid;
446 if (*nagbar_pid == -1)
449 if (kill(*nagbar_pid, SIGTERM) == -1)
450 warn(
"kill(configerror_nagbar) failed");
459 waitpid(*nagbar_pid, NULL, 0);
469 long result = strtol(str, &end, base);
470 if (result == LONG_MIN || result == LONG_MAX || result < 0 || (end != NULL && *end !=
'\0')) {
485 ssize_t
slurp(
const char *path,
char **buf) {
487 if ((f = fopen(path,
"r")) == NULL) {
488 ELOG(
"Cannot open file \"%s\": %s\n", path, strerror(errno));
492 if (fstat(fileno(f), &stbuf) != 0) {
493 ELOG(
"Cannot fstat() \"%s\": %s\n", path, strerror(errno));
499 *buf =
scalloc(stbuf.st_size + 1, 1);
500 size_t n = fread(*buf, 1, stbuf.st_size, f);
502 if ((ssize_t)n != stbuf.st_size) {
503 ELOG(
"File \"%s\" could not be read entirely: got %zd, want %zd\n", path, n, stbuf.st_size);
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...
bool layout_from_name(const char *layout_str, layout_t *out)
Set 'out' to the layout_t value for the given layout.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
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 ...
char * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
bool get_debug_logging(void)
Checks if debug logging is active.
static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent)
bool font_is_pango(void)
Returns true if and only if the current font is a pango font.
pid_t command_error_nagbar_pid
static char ** add_argument(char **original, char *opt_char, char *opt_arg, char *opt_name)
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
layout_t
Container layouts.
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
bool parse_long(const char *str, long *out, int base)
Converts a string into a long using strtol().
static void nagbar_exited(EV_P_ ev_child *watcher, int revents)
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
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...
void ipc_shutdown(shutdown_reason_t reason)
Calls shutdown() on each socket and closes it.
void exec_i3_utility(char *name, char *argv[])
exec()s an i3 utility, for example the config file migration script or i3-nagbar. ...
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
void kill_nagbar(pid_t *nagbar_pid, bool wait_for_it)
Kills the i3-nagbar process, if *nagbar_pid != -1.
char * restart_state_path
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...
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
int mkdirp(const char *path, mode_t mode)
Emulates mkdir -p (creates any missing folders)
struct ev_loop * main_loop
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
Rect rect_add(Rect a, Rect b)
void restore_geometry(void)
Restores the geometry of each window by reparenting it to the root window at the position of its fram...
char * store_restart_layout(void)
Stores a rectangle, for example the size of a window, the child window etc.
char * pango_escape_markup(char *input)
Escapes the given string if a pango font is currently used.
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
pid_t config_error_nagbar_pid
Rect rect_sub(Rect a, Rect b)
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
ssize_t slurp(const char *path, char **buf)
Slurp reads path in its entirety into buf, returning the length of the file or -1 if the file could n...