20#if defined(__OpenBSD__)
24int min(
int a,
int b) {
25 return (a < b ? a : b);
28int max(
int a,
int b) {
29 return (a > b ? a : b);
33 return (x >= rect.
x &&
34 x <= (rect.
x + rect.
width) &&
69 for (
size_t i = 0; i < strlen(name); i++) {
70 if (!isdigit(name[i])) {
85 if (strcmp(layout_str,
"default") == 0) {
88 }
else if (strcasecmp(layout_str,
"stacked") == 0 ||
89 strcasecmp(layout_str,
"stacking") == 0) {
92 }
else if (strcasecmp(layout_str,
"tabbed") == 0) {
95 }
else if (strcasecmp(layout_str,
"splitv") == 0) {
98 }
else if (strcasecmp(layout_str,
"splith") == 0) {
115 long long parsed_num = strtoll(name, &endptr, 10);
116 if (errno != 0 || parsed_num > INT32_MAX || parsed_num < 0 || endptr == name) {
129 uint32_t old_value = *destination;
131 return ((*destination = new_value) != old_value);
153 execvp(binary, argv);
159 char *dir = dirname(pathbuf);
162 execvp(binary, argv);
164#if defined(__linux__)
167 if (readlink(
"/proc/self/exe", buffer, BUFSIZ) == -1) {
168 warn(
"could not read /proc/self/exe");
171 dir = dirname(buffer);
174 execvp(binary, argv);
177 warn(
"Could not start %s", name);
185static char **
add_argument(
char **original,
char *opt_char,
char *opt_arg,
char *opt_name) {
187 for (num_args = 0; original[num_args] != NULL; num_args++) {
189 char **result =
scalloc(num_args + 3,
sizeof(
char *));
193 bool skip_next =
false;
194 for (
int i = 0; i < num_args; ++i) {
199 if (!strcmp(original[i], opt_char) ||
200 (opt_name && !strcmp(original[i], opt_name))) {
206 result[write_index++] = original[i];
210 result[write_index++] = opt_char;
211 result[write_index] = opt_arg;
216#define y(x, ...) yajl_gen_##x(gen, ##__VA_ARGS__)
217#define ystr(str) yajl_gen_string(gen, (unsigned char *)str, strlen(str))
220 setlocale(LC_NUMERIC,
"C");
221 yajl_gen gen = yajl_gen_alloc(NULL);
225 setlocale(LC_NUMERIC,
"");
227 const unsigned char *payload;
229 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);
253 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
260 if (
writeall(fd, payload, length) == -1) {
261 ELOG(
"Could not write restart layout to \"%s\", layout will be lost: %s\n", filename, strerror(errno));
270 DLOG(
"layout: %.*s\n", (
int)length, payload);
303 if (restart_filename != NULL) {
322 char *escaped = g_markup_escape_text(input, -1);
334 ev_child_stop(EV_A_ watcher);
336 int exitcode = WEXITSTATUS(watcher->rstatus);
337 if (!WIFEXITED(watcher->rstatus)) {
338 ELOG(
"i3-nagbar (%d) did not exit normally. This is not an error if the config was reloaded while a nagbar was active.\n", watcher->pid);
339 }
else if (exitcode != 0) {
340 ELOG(
"i3-nagbar (%d) process exited with status %d\n", watcher->pid, exitcode);
342 DLOG(
"i3-nagbar (%d) process exited with status %d\n", watcher->pid, exitcode);
345 pid_t *nagbar_pid = watcher->data;
346 if (*nagbar_pid == watcher->pid) {
361 if (*nagbar_pid != -1) {
362 DLOG(
"i3-nagbar already running (PID %d), not starting again.\n", *nagbar_pid);
366 *nagbar_pid = fork();
367 if (*nagbar_pid == -1) {
368 warn(
"Could not fork()");
373 if (*nagbar_pid == 0) {
377 DLOG(
"Starting i3-nagbar with PID %d\n", *nagbar_pid);
381 ev_child *child =
smalloc(
sizeof(ev_child));
383 child->data = nagbar_pid;
395 if (nagbar_pid == -1) {
399 if (kill(nagbar_pid, SIGTERM) == -1) {
400 warn(
"kill(configerror_nagbar) failed");
411 waitpid(nagbar_pid, NULL, 0);
421 long result = strtol(str, &end, base);
422 if (result == LONG_MIN || result == LONG_MAX || result < 0 || (end != NULL && *end !=
'\0')) {
437ssize_t
slurp(
const char *path,
char **buf) {
439 if ((f = fopen(path,
"r")) == NULL) {
440 ELOG(
"Cannot open file \"%s\": %s\n", path, strerror(errno));
444 if (fstat(fileno(f), &stbuf) != 0) {
445 ELOG(
"Cannot fstat() \"%s\": %s\n", path, strerror(errno));
451 *buf =
scalloc(stbuf.st_size + 1, 1);
452 size_t n = fread(*buf, 1, stbuf.st_size, f);
454 if ((ssize_t)n != stbuf.st_size) {
455 ELOG(
"File \"%s\" could not be read entirely: got %zd, want %" PRIi64
"\n", path, n, (int64_t)stbuf.st_size);
483 if (orientation ==
HORIZ) {
pid_t command_error_nagbar_pid
pid_t config_error_nagbar_pid
void restore_geometry(void)
Restores the geometry of each window by reparenting it to the root window at the position of its fram...
orientation_t orientation_from_direction(direction_t direction)
Convert a direction to its corresponding orientation.
position_t position_from_direction(direction_t direction)
Convert a direction to its corresponding position.
int 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 rect_contains(Rect rect, uint32_t x, uint32_t y)
static char * store_restart_layout(void)
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
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.
void kill_nagbar(pid_t nagbar_pid, bool wait_for_it)
Kills the i3-nagbar process, if nagbar_pid != -1.
static char ** add_argument(char **original, char *opt_char, char *opt_arg, char *opt_name)
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
Rect rect_add(Rect a, Rect b)
char * pango_escape_markup(char *input)
Escapes the given string if a pango font is currently used.
bool parse_long(const char *str, long *out, int base)
Converts a string into a long using strtol().
bool rect_equals(Rect a, Rect b)
const char * position_to_string(position_t position)
Converts position to a string representation.
const char * direction_to_string(direction_t direction)
Converts direction to a string representation.
static void nagbar_exited(EV_P_ ev_child *watcher, int revents)
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...
bool layout_from_name(const char *layout_str, layout_t *out)
Set 'out' to the layout_t value for the given layout.
direction_t direction_from_orientation_position(orientation_t orientation, position_t position)
Convert orientation and position to the corresponding direction.
Rect rect_sub(Rect a, Rect b)
Rect rect_sanitize_dimensions(Rect rect)
void ipc_shutdown(shutdown_reason_t reason, int exempt_fd)
Calls shutdown() on each socket and closes it.
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
bool get_debug_logging(void)
Checks if debug logging is active.
struct ev_loop * main_loop
layout_t
Container layouts.
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
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 * sstrdup(const char *str)
Safe-wrapper around strdup 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...
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 * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
int mkdirp(const char *path, mode_t mode)
Emulates mkdir -p (creates any missing folders)
bool font_is_pango(void)
Returns true if and only if the current font is a pango font.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
@ SHUTDOWN_REASON_RESTART
char * restart_state_path
Stores a rectangle, for example the size of a window, the child window etc.