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]))
83 if (strcmp(layout_str,
"default") == 0) {
86 }
else if (strcasecmp(layout_str,
"stacked") == 0 ||
87 strcasecmp(layout_str,
"stacking") == 0) {
90 }
else if (strcasecmp(layout_str,
"tabbed") == 0) {
93 }
else if (strcasecmp(layout_str,
"splitv") == 0) {
96 }
else if (strcasecmp(layout_str,
"splith") == 0) {
113 long long parsed_num = strtoll(name, &endptr, 10);
114 if (errno != 0 || parsed_num > INT32_MAX || parsed_num < 0 || endptr == name) {
127 uint32_t old_value = *destination;
129 return ((*destination = new_value) != old_value);
149 char *migratepath = name;
150 argv[0] = migratepath;
151 execvp(migratepath, argv);
157 char *dir = dirname(pathbuf);
158 sasprintf(&migratepath,
"%s/%s", dir, name);
159 argv[0] = migratepath;
160 execvp(migratepath, argv);
162#if defined(__linux__)
165 if (readlink(
"/proc/self/exe", buffer, BUFSIZ) == -1) {
166 warn(
"could not read /proc/self/exe");
169 dir = dirname(buffer);
170 sasprintf(&migratepath,
"%s/%s", dir, name);
171 argv[0] = migratepath;
172 execvp(migratepath, argv);
175 warn(
"Could not start %s", name);
183static char **
add_argument(
char **original,
char *opt_char,
char *opt_arg,
char *opt_name) {
185 for (num_args = 0; original[num_args] != NULL; num_args++)
187 char **result =
scalloc(num_args + 3,
sizeof(
char *));
191 bool skip_next =
false;
192 for (
int i = 0; i < num_args; ++i) {
197 if (!strcmp(original[i], opt_char) ||
198 (opt_name && !strcmp(original[i], opt_name))) {
203 result[write_index++] = original[i];
207 result[write_index++] = opt_char;
208 result[write_index] = opt_arg;
213#define y(x, ...) yajl_gen_##x(gen, ##__VA_ARGS__)
214#define ystr(str) yajl_gen_string(gen, (unsigned char *)str, strlen(str))
217 setlocale(LC_NUMERIC,
"C");
218 yajl_gen gen = yajl_gen_alloc(NULL);
222 setlocale(LC_NUMERIC,
"");
224 const unsigned char *payload;
226 y(get_buf, &payload, &length);
241 char *filenamecopy =
sstrdup(filename);
242 char *base = dirname(filenamecopy);
243 DLOG(
"Creating \"%s\" for storing the restart layout\n", base);
245 ELOG(
"Could not create \"%s\" for storing the restart layout, layout will be lost.\n", base);
248 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
255 if (
writeall(fd, payload, length) == -1) {
256 ELOG(
"Could not write restart layout to \"%s\", layout will be lost: %s\n", filename, strerror(errno));
265 DLOG(
"layout: %.*s\n", (
int)length, payload);
298 if (restart_filename != NULL) {
316 char *escaped = g_markup_escape_text(input, -1);
328 ev_child_stop(EV_A_ watcher);
330 int exitcode = WEXITSTATUS(watcher->rstatus);
331 if (!WIFEXITED(watcher->rstatus)) {
332 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);
333 }
else if (exitcode != 0) {
334 ELOG(
"i3-nagbar (%d) process exited with status %d\n", watcher->pid, exitcode);
336 DLOG(
"i3-nagbar (%d) process exited with status %d\n", watcher->pid, exitcode);
339 pid_t *nagbar_pid = watcher->data;
340 if (*nagbar_pid == watcher->pid) {
355 if (*nagbar_pid != -1) {
356 DLOG(
"i3-nagbar already running (PID %d), not starting again.\n", *nagbar_pid);
360 *nagbar_pid = fork();
361 if (*nagbar_pid == -1) {
362 warn(
"Could not fork()");
367 if (*nagbar_pid == 0)
370 DLOG(
"Starting i3-nagbar with PID %d\n", *nagbar_pid);
374 ev_child *child =
smalloc(
sizeof(ev_child));
376 child->data = nagbar_pid;
388 if (nagbar_pid == -1)
391 if (kill(nagbar_pid, SIGTERM) == -1)
392 warn(
"kill(configerror_nagbar) failed");
401 waitpid(nagbar_pid, NULL, 0);
411 long result = strtol(str, &end, base);
412 if (result == LONG_MIN || result == LONG_MAX || result < 0 || (end != NULL && *end !=
'\0')) {
427ssize_t
slurp(
const char *path,
char **buf) {
429 if ((f = fopen(path,
"r")) == NULL) {
430 ELOG(
"Cannot open file \"%s\": %s\n", path, strerror(errno));
434 if (fstat(fileno(f), &stbuf) != 0) {
435 ELOG(
"Cannot fstat() \"%s\": %s\n", path, strerror(errno));
441 *buf =
scalloc(stbuf.st_size + 1, 1);
442 size_t n = fread(*buf, 1, stbuf.st_size, f);
444 if ((ssize_t)n != stbuf.st_size) {
445 ELOG(
"File \"%s\" could not be read entirely: got %zd, want %" PRIi64
"\n", path, n, (int64_t)stbuf.st_size);
473 if (orientation ==
HORIZ) {
struct ev_loop * main_loop
bool get_debug_logging(void)
Checks if debug logging is active.
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)
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 restore_geometry(void)
Restores the geometry of each window by reparenting it to the root window at the position of its fram...
pid_t config_error_nagbar_pid
pid_t command_error_nagbar_pid
@ SHUTDOWN_REASON_RESTART
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...
layout_t
Container layouts.
char * restart_state_path
Stores a rectangle, for example the size of a window, the child window etc.