Go to the documentation of this file.
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) &&
66 for (
size_t i = 0; i < strlen(name); i++)
67 if (!isdigit(name[i]))
80 if (strcmp(layout_str,
"default") == 0) {
83 }
else if (strcasecmp(layout_str,
"stacked") == 0 ||
84 strcasecmp(layout_str,
"stacking") == 0) {
87 }
else if (strcasecmp(layout_str,
"tabbed") == 0) {
90 }
else if (strcasecmp(layout_str,
"splitv") == 0) {
93 }
else if (strcasecmp(layout_str,
"splith") == 0) {
109 long parsed_num = strtol(name, &endptr, 10);
110 if (parsed_num == LONG_MIN ||
111 parsed_num == LONG_MAX ||
126 uint32_t old_value = *destination;
128 return ((*destination = new_value) != old_value);
148 char *migratepath = name;
149 argv[0] = migratepath;
150 execvp(migratepath, argv);
156 char *dir = dirname(pathbuf);
157 sasprintf(&migratepath,
"%s/%s", dir, name);
158 argv[0] = migratepath;
159 execvp(migratepath, argv);
161 #if defined(__linux__)
164 if (readlink(
"/proc/self/exe", buffer, BUFSIZ) == -1) {
165 warn(
"could not read /proc/self/exe");
168 dir = dirname(buffer);
169 sasprintf(&migratepath,
"%s/%s", dir, name);
170 argv[0] = migratepath;
171 execvp(migratepath, argv);
174 warn(
"Could not start %s", name);
184 return (stat(path, &buf) == 0);
191 static char **
add_argument(
char **original,
char *opt_char,
char *opt_arg,
char *opt_name) {
193 for (num_args = 0; original[num_args] != NULL; num_args++)
195 char **result =
scalloc(num_args + 3,
sizeof(
char *));
199 bool skip_next =
false;
200 for (
int i = 0; i < num_args; ++i) {
205 if (!strcmp(original[i], opt_char) ||
206 (opt_name && !strcmp(original[i], opt_name))) {
211 result[write_index++] = original[i];
215 result[write_index++] = opt_char;
216 result[write_index] = opt_arg;
221 #define y(x, ...) yajl_gen_##x(gen, ##__VA_ARGS__)
222 #define ystr(str) yajl_gen_string(gen, (unsigned char *)str, strlen(str))
225 setlocale(LC_NUMERIC,
"C");
226 yajl_gen gen = yajl_gen_alloc(NULL);
230 setlocale(LC_NUMERIC,
"");
232 const unsigned char *payload;
234 y(get_buf, &payload, &length);
249 char *filenamecopy =
sstrdup(filename);
250 char *base = dirname(filenamecopy);
251 DLOG(
"Creating \"%s\" for storing the restart layout\n", base);
253 ELOG(
"Could not create \"%s\" for storing the restart layout, layout will be lost.\n", base);
256 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
263 if (
writeall(fd, payload, length) == -1) {
264 ELOG(
"Could not write restart layout to \"%s\", layout will be lost: %s\n", filename, strerror(errno));
273 DLOG(
"layout: %.*s\n", (
int)length, payload);
306 if (restart_filename != NULL) {
324 char *escaped = g_markup_escape_text(input, -1);
336 ev_child_stop(EV_A_ watcher);
338 if (!WIFEXITED(watcher->rstatus)) {
339 ELOG(
"ERROR: i3-nagbar did not exit normally.\n");
343 int exitcode = WEXITSTATUS(watcher->rstatus);
344 DLOG(
"i3-nagbar process exited with status %d\n", exitcode);
346 ELOG(
"ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
349 *((pid_t *)watcher->data) = -1;
358 pid_t *nagbar_pid = (pid_t *)watcher->data;
359 if (*nagbar_pid != -1) {
360 LOG(
"Sending SIGKILL (%d) to i3-nagbar with PID %d\n", SIGKILL, *nagbar_pid);
361 kill(*nagbar_pid, SIGKILL);
374 if (*nagbar_pid != -1) {
375 DLOG(
"i3-nagbar already running (PID %d), not starting again.\n", *nagbar_pid);
379 *nagbar_pid = fork();
380 if (*nagbar_pid == -1) {
381 warn(
"Could not fork()");
386 if (*nagbar_pid == 0)
389 DLOG(
"Starting i3-nagbar with PID %d\n", *nagbar_pid);
393 ev_child *child =
smalloc(
sizeof(ev_child));
395 child->data = nagbar_pid;
400 ev_cleanup *cleanup =
smalloc(
sizeof(ev_cleanup));
402 cleanup->data = nagbar_pid;
414 if (*nagbar_pid == -1)
417 if (kill(*nagbar_pid, SIGTERM) == -1)
418 warn(
"kill(configerror_nagbar) failed");
427 waitpid(*nagbar_pid, NULL, 0);
437 long result = strtol(str, &end, base);
438 if (result == LONG_MIN || result == LONG_MAX || result < 0 || (end != NULL && *end !=
'\0')) {
453 ssize_t
slurp(
const char *path,
char **buf) {
455 if ((f = fopen(path,
"r")) == NULL) {
456 ELOG(
"Cannot open file \"%s\": %s\n", path, strerror(errno));
460 if (fstat(fileno(f), &stbuf) != 0) {
461 ELOG(
"Cannot fstat() \"%s\": %s\n", path, strerror(errno));
467 *buf =
scalloc(stbuf.st_size + 1, 1);
468 size_t n = fread(*buf, 1, stbuf.st_size, f);
470 if ((ssize_t)n != stbuf.st_size) {
471 ELOG(
"File \"%s\" could not be read entirely: got %zd, want %" PRIi64
"\n", path, n, (int64_t)stbuf.st_size);
499 if (orientation ==
HORIZ) {
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent)
pid_t config_error_nagbar_pid
bool get_debug_logging(void)
Checks if debug logging is active.
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 ...
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
position_t position_from_direction(direction_t direction)
Convert a direction to its corresponding position.
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...
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.
pid_t command_error_nagbar_pid
orientation_t orientation_from_direction(direction_t direction)
Convert a direction to its corresponding orientation.
layout_t
Container layouts.
bool font_is_pango(void)
Returns true if and only if the current font is a pango font.
direction_t direction_from_orientation_position(orientation_t orientation, position_t position)
Convert orientation and position to the corresponding direction.
long ws_name_to_number(const char *name)
Parses the workspace name as a number.
static char ** add_argument(char **original, char *opt_char, char *opt_arg, char *opt_name)
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
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...
static void nagbar_exited(EV_P_ ev_child *watcher, int revents)
char * sstrdup(const char *str)
Safe-wrapper around strdup 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...
void kill_nagbar(pid_t *nagbar_pid, bool wait_for_it)
Kills the i3-nagbar process, if *nagbar_pid != -1.
bool rect_equals(Rect a, Rect b)
bool layout_from_name(const char *layout_str, layout_t *out)
Set 'out' to the layout_t value for the given layout.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
void exec_i3_utility(char *name, char *argv[])
exec()s an i3 utility, for example the config file migration script or i3-nagbar.
bool parse_long(const char *str, long *out, int base)
Converts a string into a long using strtol().
struct ev_loop * main_loop
char * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
static char * store_restart_layout(void)
Rect rect_sub(Rect a, Rect b)
int mkdirp(const char *path, mode_t mode)
Emulates mkdir -p (creates any missing folders)
char * pango_escape_markup(char *input)
Escapes the given string if a pango font is currently used.
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...
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)
@ SHUTDOWN_REASON_RESTART
Stores a rectangle, for example the size of a window, the child window etc.
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.