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]))
77 long parsed_num = strtol(name, &endptr, 10);
78 if (parsed_num == LONG_MIN ||
79 parsed_num == LONG_MAX ||
94 uint32_t old_value = *destination;
96 return ((*destination = new_value) != old_value);
116 char *migratepath = name;
117 argv[0] = migratepath;
118 execvp(migratepath, argv);
124 char *dir = dirname(pathbuf);
125 sasprintf(&migratepath,
"%s/%s", dir, name);
126 argv[0] = migratepath;
127 execvp(migratepath, argv);
129 #if defined(__linux__) 132 if (readlink(
"/proc/self/exe", buffer, BUFSIZ) == -1) {
133 warn(
"could not read /proc/self/exe");
136 dir = dirname(buffer);
137 sasprintf(&migratepath,
"%s/%s", dir, name);
138 argv[0] = migratepath;
139 execvp(migratepath, argv);
142 warn(
"Could not start %s", name);
152 return (stat(path, &buf) == 0);
159 static char **
add_argument(
char **original,
char *opt_char,
char *opt_arg,
char *opt_name) {
161 for (num_args = 0; original[num_args] != NULL; num_args++)
163 char **result =
scalloc(num_args + 3,
sizeof(
char *));
167 bool skip_next =
false;
168 for (
int i = 0; i < num_args; ++i) {
173 if (!strcmp(original[i], opt_char) ||
174 (opt_name && !strcmp(original[i], opt_name))) {
179 result[write_index++] = original[i];
183 result[write_index++] = opt_char;
184 result[write_index] = opt_arg;
189 #define y(x, ...) yajl_gen_##x(gen, ##__VA_ARGS__) 190 #define ystr(str) yajl_gen_string(gen, (unsigned char *)str, strlen(str)) 193 setlocale(LC_NUMERIC,
"C");
194 yajl_gen gen = yajl_gen_alloc(NULL);
198 setlocale(LC_NUMERIC,
"");
200 const unsigned char *payload;
202 y(get_buf, &payload, &length);
217 char *filenamecopy =
sstrdup(filename);
218 char *base = dirname(filenamecopy);
219 DLOG(
"Creating \"%s\" for storing the restart layout\n", base);
221 ELOG(
"Could not create \"%s\" for storing the restart layout, layout will be lost.\n", base);
224 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
231 if (
writeall(fd, payload, length) == -1) {
232 ELOG(
"Could not write restart layout to \"%s\", layout will be lost: %s\n", filename, strerror(errno));
241 DLOG(
"layout: %.*s\n", (
int)length, payload);
274 if (restart_filename != NULL) {
283 #if defined(__OpenBSD__) || defined(__APPLE__) 290 void *memmem(
const void *l,
size_t l_len,
const void *s,
size_t s_len) {
291 register char *cur, *last;
292 const char *cl = (
const char *)l;
293 const char *cs = (
const char *)s;
296 if (l_len == 0 || s_len == 0)
305 return memchr(l, (
int)*cs, l_len);
308 last = (
char *)cl + l_len - s_len;
310 for (cur = (
char *)cl; cur <= last; cur++)
311 if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
328 char *escaped = g_markup_escape_text(input, -1);
340 ev_child_stop(EV_A_ watcher);
342 if (!WIFEXITED(watcher->rstatus)) {
343 ELOG(
"ERROR: i3-nagbar did not exit normally.\n");
347 int exitcode = WEXITSTATUS(watcher->rstatus);
348 DLOG(
"i3-nagbar process exited with status %d\n", exitcode);
350 ELOG(
"ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
353 *((pid_t *)watcher->data) = -1;
362 pid_t *nagbar_pid = (pid_t *)watcher->data;
363 if (*nagbar_pid != -1) {
364 LOG(
"Sending SIGKILL (%d) to i3-nagbar with PID %d\n", SIGKILL, *nagbar_pid);
365 kill(*nagbar_pid, SIGKILL);
378 if (*nagbar_pid != -1) {
379 DLOG(
"i3-nagbar already running (PID %d), not starting again.\n", *nagbar_pid);
383 *nagbar_pid = fork();
384 if (*nagbar_pid == -1) {
385 warn(
"Could not fork()");
390 if (*nagbar_pid == 0)
393 DLOG(
"Starting i3-nagbar with PID %d\n", *nagbar_pid);
397 ev_child *child =
smalloc(
sizeof(ev_child));
399 child->data = nagbar_pid;
404 ev_cleanup *cleanup =
smalloc(
sizeof(ev_cleanup));
406 cleanup->data = nagbar_pid;
418 if (*nagbar_pid == -1)
421 if (kill(*nagbar_pid, SIGTERM) == -1)
422 warn(
"kill(configerror_nagbar) failed");
431 waitpid(*nagbar_pid, NULL, 0);
char * get_process_filename(const char *prefix)
Returns the name of a temporary file with the specified prefix.
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.
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 ...
static void nagbar_exited(EV_P_ ev_child *watcher, int revents)
void dump_node(yajl_gen gen, struct Con *con, bool inplace_restart)
bool path_exists(const char *path)
Checks if the given path exists by calling stat().
pid_t command_error_nagbar_pid
Stores a rectangle, for example the size of a window, the child window etc.
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent)
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...
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)
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(void)
Calls shutdown() on each socket and closes it.
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...
char * store_restart_layout(void)
char * restart_state_path
bool font_is_pango(void)
Returns true if and only if the current font is a pango font.
bool get_debug_logging(void)
Checks if debug logging is active.
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. ...
static char ** add_argument(char **original, char *opt_char, char *opt_arg, char *opt_name)
char * pango_escape_markup(char *input)
Escapes the given string if a pango font is currently used.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
bool rect_contains(Rect rect, uint32_t x, uint32_t y)
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
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
struct ev_loop * main_loop
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.