i3
commands.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
7 * commands.c: all command functions (see commands_parser.c)
8 *
9 */
10#include "all.h"
11#include "shmlog.h"
12
13#include <fcntl.h>
14#include <stdint.h>
15#include <unistd.h>
16
17// Macros to make the YAJL API a bit easier to use.
18#define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
19#define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
20#define ysuccess(success) \
21 do { \
22 if (cmd_output->json_gen != NULL) { \
23 y(map_open); \
24 ystr("success"); \
25 y(bool, success); \
26 y(map_close); \
27 } \
28 } while (0)
29#define yerror(format, ...) \
30 do { \
31 if (cmd_output->json_gen != NULL) { \
32 char *message; \
33 sasprintf(&message, format, ##__VA_ARGS__); \
34 y(map_open); \
35 ystr("success"); \
36 y(bool, false); \
37 ystr("error"); \
38 ystr(message); \
39 y(map_close); \
40 free(message); \
41 } \
42 } while (0)
43
46#define HANDLE_INVALID_MATCH \
47 do { \
48 if (current_match->error != NULL) { \
49 yerror("Invalid match: %s", current_match->error); \
50 return; \
51 } \
52 } while (0)
53
59#define HANDLE_EMPTY_MATCH \
60 do { \
61 HANDLE_INVALID_MATCH; \
62 \
63 if (match_is_empty(current_match)) { \
64 while (!TAILQ_EMPTY(&owindows)) { \
65 owindow *ow = TAILQ_FIRST(&owindows); \
66 TAILQ_REMOVE(&owindows, ow, owindows); \
67 free(ow); \
68 } \
69 owindow *ow = smalloc(sizeof(owindow)); \
70 ow->con = focused; \
71 TAILQ_INIT(&owindows); \
72 TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
73 } \
74 } while (0)
75
76/*
77 * Checks whether we switched to a new workspace and returns false in that case,
78 * signaling that further workspace switching should be done by the calling function
79 * If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
80 * and return true, signaling that no further workspace switching should occur in the calling function.
81 *
82 */
83static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
85
86 /* If we switched to a different workspace, do nothing */
87 if (strcmp(ws->name, name) != 0)
88 return false;
89
90 DLOG("This workspace is already focused.\n");
93 cmd_output->needs_tree_render = true;
94 }
95 return true;
96}
97
98/*
99 * Return the passed workspace unless it is the current one and auto back and
100 * forth is enabled, in which case the back_and_forth workspace is returned.
101 */
103 Con *current, *baf;
104
106 return workspace;
107
108 current = con_get_workspace(focused);
109
110 if (current == workspace) {
112 if (baf != NULL) {
113 DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
114 return baf;
115 }
116 }
117
118 return workspace;
119}
120
121/*******************************************************************************
122 * Criteria functions.
123 ******************************************************************************/
124
125/*
126 * Helper data structure for an operation window (window on which the operation
127 * will be performed). Used to build the TAILQ owindows.
128 *
129 */
130typedef struct owindow {
132 TAILQ_ENTRY(owindow) owindows;
134
135typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
136
137static owindows_head owindows;
138
139/*
140 * Initializes the specified 'Match' data structure and the initial state of
141 * commands.c for matching target windows of a command.
142 *
143 */
145 Con *con;
146 owindow *ow;
147
148 DLOG("Initializing criteria, current_match = %p\n", current_match);
151 while (!TAILQ_EMPTY(&owindows)) {
152 ow = TAILQ_FIRST(&owindows);
153 TAILQ_REMOVE(&owindows, ow, owindows);
154 free(ow);
155 }
156 TAILQ_INIT(&owindows);
157 /* copy all_cons */
159 ow = smalloc(sizeof(owindow));
160 ow->con = con;
161 TAILQ_INSERT_TAIL(&owindows, ow, owindows);
162 }
163}
164
165/*
166 * A match specification just finished (the closing square bracket was found),
167 * so we filter the list of owindows.
168 *
169 */
171 owindow *next, *current;
172
173 DLOG("match specification finished, matching...\n");
174 /* copy the old list head to iterate through it and start with a fresh
175 * list which will contain only matching windows */
176 struct owindows_head old = owindows;
177 TAILQ_INIT(&owindows);
178 for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
179 /* make a copy of the next pointer and advance the pointer to the
180 * next element as we are going to invalidate the element’s
181 * next/prev pointers by calling TAILQ_INSERT_TAIL later */
182 current = next;
183 next = TAILQ_NEXT(next, owindows);
184
185 DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
186
187 /* We use this flag to prevent matching on window-less containers if
188 * only window-specific criteria were specified. */
189 bool accept_match = false;
190
191 if (current_match->con_id != NULL) {
192 accept_match = true;
193
194 if (current_match->con_id == current->con) {
195 DLOG("con_id matched.\n");
196 } else {
197 DLOG("con_id does not match.\n");
198 FREE(current);
199 continue;
200 }
201 }
202
203 if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
204 accept_match = true;
205 bool matched_by_mark = false;
206
207 mark_t *mark;
208 TAILQ_FOREACH (mark, &(current->con->marks_head), marks) {
209 if (!regex_matches(current_match->mark, mark->name))
210 continue;
211
212 DLOG("match by mark\n");
213 matched_by_mark = true;
214 break;
215 }
216
217 if (!matched_by_mark) {
218 DLOG("mark does not match.\n");
219 FREE(current);
220 continue;
221 }
222 }
223
224 if (current->con->window != NULL) {
226 DLOG("matches window!\n");
227 accept_match = true;
228 } else {
229 DLOG("doesn't match\n");
230 FREE(current);
231 continue;
232 }
233 }
234
235 if (accept_match) {
236 TAILQ_INSERT_TAIL(&owindows, current, owindows);
237 } else {
238 FREE(current);
239 continue;
240 }
241 }
242
243 TAILQ_FOREACH (current, &owindows, owindows) {
244 DLOG("matching: %p / %s\n", current->con, current->con->name);
245 }
246}
247
248/*
249 * Interprets a ctype=cvalue pair and adds it to the current match
250 * specification.
251 *
252 */
253void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
254 match_parse_property(current_match, ctype, cvalue);
255}
256
258 owindow *current;
259 TAILQ_FOREACH (current, &owindows, owindows) {
260 DLOG("matching: %p / %s\n", current->con, current->con->name);
261 con_move_to_workspace(current->con, ws, true, false, false);
262 }
263}
264
265#define CHECK_MOVE_CON_TO_WORKSPACE \
266 do { \
267 HANDLE_EMPTY_MATCH; \
268 if (TAILQ_EMPTY(&owindows)) { \
269 yerror("Nothing to move: specified criteria don't match any window"); \
270 return; \
271 } else { \
272 bool found = false; \
273 owindow *current = TAILQ_FIRST(&owindows); \
274 while (current) { \
275 owindow *next = TAILQ_NEXT(current, owindows); \
276 \
277 if (current->con->type == CT_WORKSPACE && !con_has_children(current->con)) { \
278 TAILQ_REMOVE(&owindows, current, owindows); \
279 } else { \
280 found = true; \
281 } \
282 \
283 current = next; \
284 } \
285 if (!found) { \
286 yerror("Nothing to move: workspace empty"); \
287 return; \
288 } \
289 } \
290 } while (0)
291
292/*
293 * Implementation of 'move [window|container] [to] workspace
294 * next|prev|next_on_output|prev_on_output|current'.
295 *
296 */
297void cmd_move_con_to_workspace(I3_CMD, const char *which) {
298 DLOG("which=%s\n", which);
299
301
302 /* get the workspace */
303 Con *ws;
304 if (strcmp(which, "next") == 0)
305 ws = workspace_next();
306 else if (strcmp(which, "prev") == 0)
307 ws = workspace_prev();
308 else if (strcmp(which, "next_on_output") == 0)
310 else if (strcmp(which, "prev_on_output") == 0)
312 else if (strcmp(which, "current") == 0)
314 else {
315 yerror("BUG: called with which=%s", which);
316 return;
317 }
318
320
321 cmd_output->needs_tree_render = true;
322 // XXX: default reply for now, make this a better reply
323 ysuccess(true);
324}
325
326/*
327 * Implementation of 'move [window|container] [to] workspace back_and_forth'.
328 *
329 */
332 if (ws == NULL) {
333 yerror("No workspace was previously active.");
334 return;
335 }
336
338
340
341 cmd_output->needs_tree_render = true;
342 // XXX: default reply for now, make this a better reply
343 ysuccess(true);
344}
345
346/*
347 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
348 *
349 */
350void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *no_auto_back_and_forth) {
351 if (strncasecmp(name, "__", strlen("__")) == 0) {
352 yerror("You cannot move containers to i3-internal workspaces (\"%s\").", name);
353 return;
354 }
355
357
358 LOG("should move window to workspace %s\n", name);
359 /* get the workspace */
360 Con *ws = workspace_get(name);
361
362 if (no_auto_back_and_forth == NULL) {
364 }
365
367
368 cmd_output->needs_tree_render = true;
369 // XXX: default reply for now, make this a better reply
370 ysuccess(true);
371}
372
373/*
374 * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
375 *
376 */
377void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *no_auto_back_and_forth) {
379
380 LOG("should move window to workspace %s\n", which);
381
382 long parsed_num = ws_name_to_number(which);
383 if (parsed_num == -1) {
384 LOG("Could not parse initial part of \"%s\" as a number.\n", which);
385 yerror("Could not parse number \"%s\"", which);
386 return;
387 }
388
389 Con *ws = get_existing_workspace_by_num(parsed_num);
390 if (!ws) {
391 ws = workspace_get(which);
392 }
393
394 if (no_auto_back_and_forth == NULL) {
396 }
397
399
400 cmd_output->needs_tree_render = true;
401 // XXX: default reply for now, make this a better reply
402 ysuccess(true);
403}
404
405/*
406 * Convert a string direction ("left", "right", etc.) to a direction_t. Assumes
407 * valid direction string.
408 */
409static direction_t parse_direction(const char *str) {
410 if (strcmp(str, "left") == 0) {
411 return D_LEFT;
412 } else if (strcmp(str, "right") == 0) {
413 return D_RIGHT;
414 } else if (strcmp(str, "up") == 0) {
415 return D_UP;
416 } else if (strcmp(str, "down") == 0) {
417 return D_DOWN;
418 } else {
419 ELOG("Invalid direction. This is a parser bug.\n");
420 assert(false);
421 }
422}
423
424static void cmd_resize_floating(I3_CMD, const char *direction_str, Con *floating_con, int px) {
425 Rect old_rect = floating_con->rect;
426 Con *focused_con = con_descend_focused(floating_con);
427
428 direction_t direction;
429 if (strcmp(direction_str, "height") == 0) {
430 direction = D_DOWN;
431 } else if (strcmp(direction_str, "width") == 0) {
432 direction = D_RIGHT;
433 } else {
434 direction = parse_direction(direction_str);
435 }
436 orientation_t orientation = orientation_from_direction(direction);
437
438 /* ensure that resize will take place even if pixel increment is smaller than
439 * height increment or width increment.
440 * fixes #1011 */
441 const i3Window *window = focused_con->window;
442 if (window != NULL) {
443 if (orientation == VERT) {
444 if (px < 0) {
445 px = (-px < window->height_increment) ? -window->height_increment : px;
446 } else {
447 px = (px < window->height_increment) ? window->height_increment : px;
448 }
449 } else {
450 if (px < 0) {
451 px = (-px < window->width_increment) ? -window->width_increment : px;
452 } else {
453 px = (px < window->width_increment) ? window->width_increment : px;
454 }
455 }
456 }
457
458 if (orientation == VERT) {
459 floating_con->rect.height += px;
460 } else {
461 floating_con->rect.width += px;
462 }
463 floating_check_size(floating_con, orientation == VERT);
464
465 /* Did we actually resize anything or did the size constraints prevent us?
466 * If we could not resize, exit now to not move the window. */
467 if (rect_equals(old_rect, floating_con->rect)) {
468 return;
469 }
470
471 if (direction == D_UP) {
472 floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
473 } else if (direction == D_LEFT) {
474 floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
475 }
476
477 /* If this is a scratchpad window, don't auto center it from now on. */
478 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH) {
479 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
480 }
481}
482
483static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *direction, int px, int ppt) {
484 Con *second = NULL;
485 Con *first = current;
486 direction_t search_direction = parse_direction(direction);
487
488 bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
489 if (!res) {
490 yerror("No second container found in this direction.");
491 return false;
492 }
493
494 if (ppt) {
495 /* For backwards compatibility, 'X px or Y ppt' means that ppt is
496 * preferred. */
497 px = 0;
498 }
499 return resize_neighboring_cons(first, second, px, ppt);
500}
501
502static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *direction, int px, double ppt) {
503 LOG("width/height resize\n");
504
505 /* get the appropriate current container (skip stacked/tabbed cons) */
506 Con *dummy = NULL;
507 direction_t search_direction = (strcmp(direction, "width") == 0 ? D_LEFT : D_DOWN);
508 bool search_result = resize_find_tiling_participants(&current, &dummy, search_direction, true);
509 if (search_result == false) {
510 yerror("Failed to find appropriate tiling containers for resize operation");
511 return false;
512 }
513
514 /* get the default percentage */
515 int children = con_num_children(current->parent);
516 LOG("ins. %d children\n", children);
517 double percentage = 1.0 / children;
518 LOG("default percentage = %f\n", percentage);
519
520 /* Ensure all the other children have a percentage set. */
521 Con *child;
522 TAILQ_FOREACH (child, &(current->parent->nodes_head), nodes) {
523 LOG("child->percent = %f (child %p)\n", child->percent, child);
524 if (child->percent == 0.0)
525 child->percent = percentage;
526 }
527
528 double new_current_percent;
529 double subtract_percent;
530 if (ppt != 0.0) {
531 new_current_percent = current->percent + ppt;
532 } else {
533 /* Convert px change to change in percentages */
534 ppt = (double)px / (double)con_rect_size_in_orientation(current->parent);
535 new_current_percent = current->percent + ppt;
536 }
537 subtract_percent = ppt / (children - 1);
538 if (ppt < 0.0 && new_current_percent < percent_for_1px(current)) {
539 yerror("Not resizing, container would end with less than 1px");
540 return false;
541 }
542
543 LOG("new_current_percent = %f\n", new_current_percent);
544 LOG("subtract_percent = %f\n", subtract_percent);
545 /* Ensure that the new percentages are positive. */
546 if (subtract_percent >= 0.0) {
547 TAILQ_FOREACH (child, &(current->parent->nodes_head), nodes) {
548 if (child == current) {
549 continue;
550 }
551 if (child->percent - subtract_percent < percent_for_1px(child)) {
552 yerror("Not resizing, already at minimum size (child %p would end up with a size of %.f", child, child->percent - subtract_percent);
553 return false;
554 }
555 }
556 }
557
558 current->percent = new_current_percent;
559 LOG("current->percent after = %f\n", current->percent);
560
561 TAILQ_FOREACH (child, &(current->parent->nodes_head), nodes) {
562 if (child == current)
563 continue;
564 child->percent -= subtract_percent;
565 LOG("child->percent after (%p) = %f\n", child, child->percent);
566 }
567
568 return true;
569}
570
571/*
572 * Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
573 *
574 */
575void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
576 DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
577 if (strcmp(way, "shrink") == 0) {
578 resize_px *= -1;
579 resize_ppt *= -1;
580 }
581
583
584 owindow *current;
585 TAILQ_FOREACH (current, &owindows, owindows) {
586 /* Don't handle dock windows (issue #1201) */
587 if (current->con->window && current->con->window->dock) {
588 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
589 continue;
590 }
591
592 Con *floating_con;
593 if ((floating_con = con_inside_floating(current->con))) {
594 cmd_resize_floating(current_match, cmd_output, direction, floating_con, resize_px);
595 } else {
596 if (strcmp(direction, "width") == 0 ||
597 strcmp(direction, "height") == 0) {
598 const double ppt = (double)resize_ppt / 100.0;
600 current->con, direction,
601 resize_px, ppt)) {
602 yerror("Cannot resize.");
603 return;
604 }
605 } else {
607 current->con, direction,
608 resize_px, resize_ppt)) {
609 yerror("Cannot resize.");
610 return;
611 }
612 }
613 }
614 }
615
616 cmd_output->needs_tree_render = true;
617 // XXX: default reply for now, make this a better reply
618 ysuccess(true);
619}
620
621static bool resize_set_tiling(I3_CMD, Con *target, orientation_t resize_orientation, bool is_ppt, long target_size) {
622 direction_t search_direction;
623 char *mode;
624 if (resize_orientation == HORIZ) {
625 search_direction = D_LEFT;
626 mode = "width";
627 } else {
628 search_direction = D_DOWN;
629 mode = "height";
630 }
631
632 /* Get the appropriate current container (skip stacked/tabbed cons) */
633 Con *dummy;
634 resize_find_tiling_participants(&target, &dummy, search_direction, true);
635
636 /* Calculate new size for the target container */
637 double ppt = 0.0;
638 int px = 0;
639 if (is_ppt) {
640 ppt = (double)target_size / 100.0 - target->percent;
641 } else {
642 px = target_size - (resize_orientation == HORIZ ? target->rect.width : target->rect.height);
643 }
644
645 /* Perform resizing and report failure if not possible */
647 target, mode, px, ppt);
648}
649
650/*
651 * Implementation of 'resize set <width> [px | ppt] <height> [px | ppt]'.
652 *
653 */
654void cmd_resize_set(I3_CMD, long cwidth, const char *mode_width, long cheight, const char *mode_height) {
655 DLOG("resizing to %ld %s x %ld %s\n", cwidth, mode_width, cheight, mode_height);
656 if (cwidth < 0 || cheight < 0) {
657 yerror("Dimensions cannot be negative.");
658 return;
659 }
660
662
663 owindow *current;
664 bool success = true;
665 TAILQ_FOREACH (current, &owindows, owindows) {
666 Con *floating_con;
667 if ((floating_con = con_inside_floating(current->con))) {
668 Con *output = con_get_output(floating_con);
669 if (cwidth == 0) {
670 cwidth = floating_con->rect.width;
671 } else if (mode_width && strcmp(mode_width, "ppt") == 0) {
672 cwidth = output->rect.width * ((double)cwidth / 100.0);
673 }
674 if (cheight == 0) {
675 cheight = floating_con->rect.height;
676 } else if (mode_height && strcmp(mode_height, "ppt") == 0) {
677 cheight = output->rect.height * ((double)cheight / 100.0);
678 }
679 floating_resize(floating_con, cwidth, cheight);
680 } else {
681 if (current->con->window && current->con->window->dock) {
682 DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
683 continue;
684 }
685
686 if (cwidth > 0) {
687 bool is_ppt = mode_width && strcmp(mode_width, "ppt") == 0;
688 success &= resize_set_tiling(current_match, cmd_output, current->con,
689 HORIZ, is_ppt, cwidth);
690 }
691 if (cheight > 0) {
692 bool is_ppt = mode_height && strcmp(mode_height, "ppt") == 0;
693 success &= resize_set_tiling(current_match, cmd_output, current->con,
694 VERT, is_ppt, cheight);
695 }
696 }
697 }
698
699 cmd_output->needs_tree_render = true;
700 ysuccess(success);
701}
702
703static int border_width_from_style(border_style_t border_style, long border_width, Con *con) {
704 if (border_style == BS_NONE) {
705 return 0;
706 }
707 if (border_width >= 0) {
708 return logical_px(border_width);
709 }
710
711 const bool is_floating = con_inside_floating(con) != NULL;
712 /* Load the configured defaults. */
713 if (is_floating && border_style == config.default_floating_border) {
715 } else if (!is_floating && border_style == config.default_border) {
717 } else {
718 /* Use some hardcoded values. */
719 return logical_px(border_style == BS_NORMAL ? 2 : 1);
720 }
721}
722
723/*
724 * Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
725 *
726 */
727void cmd_border(I3_CMD, const char *border_style_str, long border_width) {
728 DLOG("border style should be changed to %s with border width %ld\n", border_style_str, border_width);
729 owindow *current;
730
732
733 TAILQ_FOREACH (current, &owindows, owindows) {
734 DLOG("matching: %p / %s\n", current->con, current->con->name);
735
736 border_style_t border_style;
737 if (strcmp(border_style_str, "toggle") == 0) {
738 border_style = (current->con->border_style + 1) % 3;
739 } else if (strcmp(border_style_str, "normal") == 0) {
740 border_style = BS_NORMAL;
741 } else if (strcmp(border_style_str, "pixel") == 0) {
742 border_style = BS_PIXEL;
743 } else if (strcmp(border_style_str, "none") == 0) {
744 border_style = BS_NONE;
745 } else {
746 yerror("BUG: called with border_style=%s", border_style_str);
747 return;
748 }
749
750 /* User changed the border */
751 current->con->max_user_border_style = border_style;
752 const int con_border_width = border_width_from_style(border_style, border_width, current->con);
753 con_set_border_style(current->con, border_style, con_border_width);
754 }
755
756 cmd_output->needs_tree_render = true;
757 ysuccess(true);
758}
759
760/*
761 * Implementation of 'nop <comment>'.
762 *
763 */
764void cmd_nop(I3_CMD, const char *comment) {
765 LOG("-------------------------------------------------\n");
766 LOG(" NOP: %.4000s\n", comment);
767 LOG("-------------------------------------------------\n");
768 ysuccess(true);
769}
770
771/*
772 * Implementation of 'append_layout <path>'.
773 *
774 */
775void cmd_append_layout(I3_CMD, const char *cpath) {
776 LOG("Appending layout \"%s\"\n", cpath);
777
778 /* Make sure we allow paths like '~/.i3/layout.json' */
779 char *path = resolve_tilde(cpath);
780
781 char *buf = NULL;
782 ssize_t len;
783 if ((len = slurp(path, &buf)) < 0) {
784 yerror("Could not slurp \"%s\".", path);
785 /* slurp already logged an error. */
786 goto out;
787 }
788
789 if (!json_validate(buf, len)) {
790 ELOG("Could not parse \"%s\" as JSON, not loading.\n", path);
791 yerror("Could not parse \"%s\" as JSON.", path);
792 goto out;
793 }
794
795 json_content_t content = json_determine_content(buf, len);
796 LOG("JSON content = %d\n", content);
797 if (content == JSON_CONTENT_UNKNOWN) {
798 ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
799 yerror("Could not determine the contents of \"%s\".", path);
800 goto out;
801 }
802
803 Con *parent = focused;
804 if (content == JSON_CONTENT_WORKSPACE) {
805 parent = output_get_content(con_get_output(parent));
806 } else {
807 /* We need to append the layout to a split container, since a leaf
808 * container must not have any children (by definition).
809 * Note that we explicitly check for workspaces, since they are okay for
810 * this purpose, but con_accepts_window() returns false for workspaces. */
811 while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
812 parent = parent->parent;
813 }
814 DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
815 char *errormsg = NULL;
816 tree_append_json(parent, buf, len, &errormsg);
817 if (errormsg != NULL) {
818 yerror(errormsg);
819 free(errormsg);
820 /* Note that we continue executing since tree_append_json() has
821 * side-effects — user-provided layouts can be partly valid, partly
822 * invalid, leading to half of the placeholder containers being
823 * created. */
824 } else {
825 ysuccess(true);
826 }
827
828 // XXX: This is a bit of a kludge. Theoretically, render_con(parent,
829 // false); should be enough, but when sending 'workspace 4; append_layout
830 // /tmp/foo.json', the needs_tree_render == true of the workspace command
831 // is not executed yet and will be batched with append_layout’s
832 // needs_tree_render after the parser finished. We should check if that is
833 // necessary at all.
835
837
838 if (content == JSON_CONTENT_WORKSPACE)
839 ipc_send_workspace_event("restored", parent, NULL);
840
841 cmd_output->needs_tree_render = true;
842out:
843 free(path);
844 free(buf);
845}
846
847static void disable_global_fullscreen(void) {
849 if (fs) {
851 }
852}
853
854/*
855 * Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
856 *
857 */
858void cmd_workspace(I3_CMD, const char *which) {
859 Con *ws;
860
861 DLOG("which=%s\n", which);
862
864
865 if (strcmp(which, "next") == 0)
866 ws = workspace_next();
867 else if (strcmp(which, "prev") == 0)
868 ws = workspace_prev();
869 else if (strcmp(which, "next_on_output") == 0)
871 else if (strcmp(which, "prev_on_output") == 0)
873 else {
874 yerror("BUG: called with which=%s", which);
875 return;
876 }
877
878 workspace_show(ws);
879
880 cmd_output->needs_tree_render = true;
881 // XXX: default reply for now, make this a better reply
882 ysuccess(true);
883}
884
885/*
886 * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
887 *
888 */
889void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
890 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
891
893
894 long parsed_num = ws_name_to_number(which);
895 if (parsed_num == -1) {
896 yerror("Could not parse initial part of \"%s\" as a number.", which);
897 return;
898 }
899
900 Con *workspace = get_existing_workspace_by_num(parsed_num);
901 if (!workspace) {
902 LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
903 ysuccess(true);
905 cmd_output->needs_tree_render = true;
906 return;
907 }
908 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name)) {
909 ysuccess(true);
910 return;
911 }
912 workspace_show(workspace);
913
914 cmd_output->needs_tree_render = true;
915 // XXX: default reply for now, make this a better reply
916 ysuccess(true);
917}
918
919/*
920 * Implementation of 'workspace back_and_forth'.
921 *
922 */
925
927
928 cmd_output->needs_tree_render = true;
929 // XXX: default reply for now, make this a better reply
930 ysuccess(true);
931}
932
933/*
934 * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
935 *
936 */
937void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
938 const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
939
940 if (strncasecmp(name, "__", strlen("__")) == 0) {
941 yerror("You cannot switch to the i3-internal workspaces (\"%s\").", name);
942 return;
943 }
944
946
947 DLOG("should switch to workspace %s\n", name);
948 if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name)) {
949 ysuccess(true);
950 return;
951 }
953
954 cmd_output->needs_tree_render = true;
955 // XXX: default reply for now, make this a better reply
956 ysuccess(true);
957}
958
959/*
960 * Implementation of 'mark [--add|--replace] [--toggle] <mark>'
961 *
962 */
963void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
965
966 owindow *current = TAILQ_FIRST(&owindows);
967 if (current == NULL) {
968 yerror("Given criteria don't match a window");
969 return;
970 }
971
972 /* Marks must be unique, i.e., no two windows must have the same mark. */
973 if (current != TAILQ_LAST(&owindows, owindows_head)) {
974 yerror("A mark must not be put onto more than one window");
975 return;
976 }
977
978 DLOG("matching: %p / %s\n", current->con, current->con->name);
979
980 mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
981 if (toggle != NULL) {
982 con_mark_toggle(current->con, mark, mark_mode);
983 } else {
984 con_mark(current->con, mark, mark_mode);
985 }
986
987 cmd_output->needs_tree_render = true;
988 // XXX: default reply for now, make this a better reply
989 ysuccess(true);
990}
991
992/*
993 * Implementation of 'unmark [mark]'
994 *
995 */
996void cmd_unmark(I3_CMD, const char *mark) {
998 con_unmark(NULL, mark);
999 } else {
1000 owindow *current;
1001 TAILQ_FOREACH (current, &owindows, owindows) {
1002 con_unmark(current->con, mark);
1003 }
1004 }
1005
1006 cmd_output->needs_tree_render = true;
1007 // XXX: default reply for now, make this a better reply
1008 ysuccess(true);
1009}
1010
1011/*
1012 * Implementation of 'mode <string>'.
1013 *
1014 */
1015void cmd_mode(I3_CMD, const char *mode) {
1016 DLOG("mode=%s\n", mode);
1017 switch_mode(mode);
1018
1019 // XXX: default reply for now, make this a better reply
1020 ysuccess(true);
1021}
1022
1023typedef struct user_output_name {
1024 char *name;
1025 TAILQ_ENTRY(user_output_name) user_output_names;
1027typedef TAILQ_HEAD(user_output_names_head, user_output_name) user_output_names_head;
1028
1029static void user_output_names_add(user_output_names_head *list, const char *name) {
1030 const bool get_non_primary = (strcasecmp("nonprimary", name) == 0);
1031 if (get_non_primary || strcmp(name, "next") == 0) {
1032 /* "next" (or "nonprimary") here work like a wildcard: It "expands" to
1033 * all available (or non-primary) outputs. */
1034 Output *output;
1035 TAILQ_FOREACH (output, &outputs, outputs) {
1036 if (get_non_primary && output->primary) {
1037 continue;
1038 }
1039
1040 user_output_name *co = scalloc(sizeof(user_output_name), 1);
1041 co->name = sstrdup(output_primary_name(output));
1042 TAILQ_INSERT_TAIL(list, co, user_output_names);
1043 }
1044 return;
1045 }
1046
1047 user_output_name *co = scalloc(sizeof(user_output_name), 1);
1048 co->name = sstrdup(name);
1049 TAILQ_INSERT_TAIL(list, co, user_output_names);
1050 return;
1051}
1052
1053static Output *user_output_names_find_next(user_output_names_head *names, Output *current_output) {
1054 Output *target_output = NULL;
1055 user_output_name *uo;
1056 TAILQ_FOREACH (uo, names, user_output_names) {
1057 if (!target_output) {
1058 /* The first available output from the list is used in 2 cases:
1059 * 1. When we must wrap around the user list. For example, if user
1060 * specifies outputs A B C and C is `current_output`.
1061 * 2. When the current output is not in the user list. For example,
1062 * user specifies A B C and D is `current_output`. */
1063 target_output = get_output_from_string(current_output, uo->name);
1064 }
1065 if (strcasecmp(output_primary_name(current_output), uo->name) == 0) {
1066 /* The current output is in the user list */
1067 while (true) {
1068 /* This corrupts the outer loop but it is ok since we are going
1069 * to break anyway. */
1070 uo = TAILQ_NEXT(uo, user_output_names);
1071 if (!uo) {
1072 /* We reached the end of the list. We should use the first
1073 * available output that, if it exists, is already saved in
1074 * target_output. */
1075 break;
1076 }
1077 Output *out = get_output_from_string(current_output, uo->name);
1078 if (out) {
1079 return out;
1080 }
1081 }
1082 break;
1083 }
1084 }
1085 return target_output;
1086}
1087
1088static void user_output_names_free(user_output_names_head *names) {
1089 user_output_name *uo;
1090 while (!TAILQ_EMPTY(names)) {
1091 uo = TAILQ_FIRST(names);
1092 free(uo->name);
1093 TAILQ_REMOVE(names, uo, user_output_names);
1094 free(uo);
1095 }
1096}
1097
1098/*
1099 * Implementation of 'move [window|container|workspace] [to] output <strings>'.
1100 *
1101 */
1102void cmd_move_con_to_output(I3_CMD, const char *name, bool move_workspace) {
1103 /* Initialize a data structure that is used to save multiple user-specified
1104 * output names since this function is called multiple types for each
1105 * command call. */
1106 static user_output_names_head names = TAILQ_HEAD_INITIALIZER(names);
1107
1108 if (name) {
1109 user_output_names_add(&names, name);
1110 return;
1111 }
1112
1114
1115 if (TAILQ_EMPTY(&names)) {
1116 yerror("At least one output must be specified");
1117 return;
1118 }
1119
1120 bool success = false;
1121 owindow *current;
1122 TAILQ_FOREACH (current, &owindows, owindows) {
1123 Con *ws = con_get_workspace(current->con);
1124 if (con_is_internal(ws)) {
1125 continue;
1126 }
1127
1128 Output *current_output = get_output_for_con(ws);
1129 Output *target_output = user_output_names_find_next(&names, current_output);
1130 if (target_output) {
1131 if (move_workspace) {
1132 workspace_move_to_output(ws, target_output);
1133 } else {
1134 con_move_to_output(current->con, target_output, true);
1135 }
1136 success = true;
1137 }
1138 }
1139 user_output_names_free(&names);
1140
1141 cmd_output->needs_tree_render = success;
1142 if (success) {
1143 ysuccess(true);
1144 } else {
1145 yerror("No output matched");
1146 }
1147}
1148
1149/*
1150 * Implementation of 'move [container|window] [to] mark <str>'.
1151 *
1152 */
1153void cmd_move_con_to_mark(I3_CMD, const char *mark) {
1154 DLOG("moving window to mark \"%s\"\n", mark);
1155
1157
1158 bool result = true;
1159 owindow *current;
1160 TAILQ_FOREACH (current, &owindows, owindows) {
1161 DLOG("moving matched window %p / %s to mark \"%s\"\n", current->con, current->con->name, mark);
1162 result &= con_move_to_mark(current->con, mark);
1163 }
1164
1165 cmd_output->needs_tree_render = true;
1166 ysuccess(result);
1167}
1168
1169/*
1170 * Implementation of 'floating enable|disable|toggle'
1171 *
1172 */
1173void cmd_floating(I3_CMD, const char *floating_mode) {
1174 owindow *current;
1175
1176 DLOG("floating_mode=%s\n", floating_mode);
1177
1179
1180 TAILQ_FOREACH (current, &owindows, owindows) {
1181 DLOG("matching: %p / %s\n", current->con, current->con->name);
1182 if (strcmp(floating_mode, "toggle") == 0) {
1183 DLOG("should toggle mode\n");
1184 toggle_floating_mode(current->con, false);
1185 } else {
1186 DLOG("should switch mode to %s\n", floating_mode);
1187 if (strcmp(floating_mode, "enable") == 0) {
1188 floating_enable(current->con, false);
1189 } else {
1190 floating_disable(current->con);
1191 }
1192 }
1193 }
1194
1195 cmd_output->needs_tree_render = true;
1196 // XXX: default reply for now, make this a better reply
1197 ysuccess(true);
1198}
1199
1200/*
1201 * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
1202 *
1203 */
1204void cmd_split(I3_CMD, const char *direction) {
1206
1207 owindow *current;
1208 LOG("splitting in direction %c\n", direction[0]);
1209 TAILQ_FOREACH (current, &owindows, owindows) {
1210 if (con_is_docked(current->con)) {
1211 ELOG("Cannot split a docked container, skipping.\n");
1212 continue;
1213 }
1214
1215 DLOG("matching: %p / %s\n", current->con, current->con->name);
1216 if (direction[0] == 't') {
1217 layout_t current_layout;
1218 if (current->con->type == CT_WORKSPACE) {
1219 current_layout = current->con->layout;
1220 } else {
1221 current_layout = current->con->parent->layout;
1222 }
1223 /* toggling split orientation */
1224 if (current_layout == L_SPLITH) {
1225 tree_split(current->con, VERT);
1226 } else {
1227 tree_split(current->con, HORIZ);
1228 }
1229 } else {
1230 tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
1231 }
1232 }
1233
1234 cmd_output->needs_tree_render = true;
1235 // XXX: default reply for now, make this a better reply
1236 ysuccess(true);
1237}
1238
1239/*
1240 * Implementation of 'kill [window|client]'.
1241 *
1242 */
1243void cmd_kill(I3_CMD, const char *kill_mode_str) {
1244 if (kill_mode_str == NULL)
1245 kill_mode_str = "window";
1246
1247 DLOG("kill_mode=%s\n", kill_mode_str);
1248
1249 int kill_mode;
1250 if (strcmp(kill_mode_str, "window") == 0)
1251 kill_mode = KILL_WINDOW;
1252 else if (strcmp(kill_mode_str, "client") == 0)
1253 kill_mode = KILL_CLIENT;
1254 else {
1255 yerror("BUG: called with kill_mode=%s", kill_mode_str);
1256 return;
1257 }
1258
1260
1261 owindow *current;
1262 TAILQ_FOREACH (current, &owindows, owindows) {
1263 con_close(current->con, kill_mode);
1264 }
1265
1266 cmd_output->needs_tree_render = true;
1267 // XXX: default reply for now, make this a better reply
1268 ysuccess(true);
1269}
1270
1271/*
1272 * Implementation of 'exec [--no-startup-id] <command>'.
1273 *
1274 */
1275void cmd_exec(I3_CMD, const char *nosn, const char *command) {
1276 bool no_startup_id = (nosn != NULL);
1277
1279
1280 int count = 0;
1281 owindow *current;
1282 TAILQ_FOREACH (current, &owindows, owindows) {
1283 count++;
1284 }
1285
1286 if (count > 1) {
1287 LOG("WARNING: Your criteria for the exec command match %d containers, "
1288 "so the command will execute this many times.\n",
1289 count);
1290 }
1291
1292 TAILQ_FOREACH (current, &owindows, owindows) {
1293 DLOG("should execute %s, no_startup_id = %d\n", command, no_startup_id);
1294 start_application(command, no_startup_id);
1295 }
1296
1297 ysuccess(true);
1298}
1299
1300#define CMD_FOCUS_WARN_CHILDREN \
1301 do { \
1302 int count = 0; \
1303 owindow *current; \
1304 TAILQ_FOREACH (current, &owindows, owindows) { \
1305 count++; \
1306 } \
1307 \
1308 if (count > 1) { \
1309 LOG("WARNING: Your criteria for the focus command matches %d containers, " \
1310 "while only exactly one container can be focused at a time.\n", \
1311 count); \
1312 } \
1313 } while (0)
1314
1315/*
1316 * Implementation of 'focus left|right|up|down|next|prev'.
1317 *
1318 */
1319void cmd_focus_direction(I3_CMD, const char *direction_str) {
1322
1323 direction_t direction = D_LEFT;
1324 position_t position;
1325 bool auto_direction = true;
1326 if (strcmp(direction_str, "prev") == 0) {
1327 position = BEFORE;
1328 } else if (strcmp(direction_str, "next") == 0) {
1329 position = AFTER;
1330 } else {
1331 auto_direction = false;
1332 direction = parse_direction(direction_str);
1333 }
1334
1335 owindow *current;
1336 TAILQ_FOREACH (current, &owindows, owindows) {
1337 Con *ws = con_get_workspace(current->con);
1338 if (!ws || con_is_internal(ws)) {
1339 continue;
1340 }
1341 if (auto_direction) {
1342 orientation_t o = con_orientation(current->con->parent);
1343 direction = direction_from_orientation_position(o, position);
1344 }
1345 tree_next(current->con, direction);
1346 }
1347
1348 cmd_output->needs_tree_render = true;
1349 // XXX: default reply for now, make this a better reply
1350 ysuccess(true);
1351}
1352
1353/*
1354 * Implementation of 'focus next|prev sibling'
1355 *
1356 */
1357void cmd_focus_sibling(I3_CMD, const char *direction_str) {
1360
1361 const position_t direction = (STARTS_WITH(direction_str, "prev")) ? BEFORE : AFTER;
1362 owindow *current;
1363 TAILQ_FOREACH (current, &owindows, owindows) {
1364 Con *ws = con_get_workspace(current->con);
1365 if (!ws || con_is_internal(ws)) {
1366 continue;
1367 }
1368 Con *next = get_tree_next_sibling(current->con, direction);
1369 if (next) {
1370 if (next->type == CT_WORKSPACE) {
1371 /* On the workspace level, we need to make sure that the
1372 * workspace change happens properly. However, workspace_show
1373 * descends focus so we also have to put focus on the workspace
1374 * itself to maintain consistency. See #3997. */
1375 workspace_show(next);
1376 con_focus(next);
1377 } else {
1378 con_activate(next);
1379 }
1380 }
1381 }
1382
1383 cmd_output->needs_tree_render = true;
1384 // XXX: default reply for now, make this a better reply
1385 ysuccess(true);
1386}
1387
1388/*
1389 * Implementation of 'focus tiling|floating|mode_toggle'.
1390 *
1391 */
1392void cmd_focus_window_mode(I3_CMD, const char *window_mode) {
1393 DLOG("window_mode = %s\n", window_mode);
1394
1395 bool to_floating = false;
1396 if (strcmp(window_mode, "mode_toggle") == 0) {
1397 to_floating = !con_inside_floating(focused);
1398 } else if (strcmp(window_mode, "floating") == 0) {
1399 to_floating = true;
1400 } else if (strcmp(window_mode, "tiling") == 0) {
1401 to_floating = false;
1402 }
1403
1405 Con *current;
1406 bool success = false;
1407 TAILQ_FOREACH (current, &(ws->focus_head), focused) {
1408 if ((to_floating && current->type != CT_FLOATING_CON) ||
1409 (!to_floating && current->type == CT_FLOATING_CON))
1410 continue;
1411
1413 success = true;
1414 break;
1415 }
1416
1417 if (success) {
1418 cmd_output->needs_tree_render = true;
1419 ysuccess(true);
1420 } else {
1421 yerror("Failed to find a %s container in workspace.", to_floating ? "floating" : "tiling");
1422 }
1423}
1424
1425/*
1426 * Implementation of 'focus parent|child'.
1427 *
1428 */
1429void cmd_focus_level(I3_CMD, const char *level) {
1430 DLOG("level = %s\n", level);
1431 bool success = false;
1432
1433 /* Focusing the parent can only be allowed if the newly
1434 * focused container won't escape the fullscreen container. */
1435 if (strcmp(level, "parent") == 0) {
1436 if (focused && focused->parent) {
1438 success = level_up();
1439 else
1440 ELOG("'focus parent': Currently in fullscreen, not going up\n");
1441 }
1442 }
1443
1444 /* Focusing a child should always be allowed. */
1445 else
1446 success = level_down();
1447
1448 cmd_output->needs_tree_render = success;
1449 // XXX: default reply for now, make this a better reply
1450 ysuccess(success);
1451}
1452
1453/*
1454 * Implementation of 'focus'.
1455 *
1456 */
1457void cmd_focus(I3_CMD, bool focus_workspace) {
1458 DLOG("current_match = %p\n", current_match);
1459
1461 ELOG("You have to specify which window/container should be focused.\n");
1462 ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
1463
1464 yerror("You have to specify which window/container should be focused");
1465 return;
1466 } else if (TAILQ_EMPTY(&owindows)) {
1467 yerror("No window matches given criteria");
1468 return;
1469 }
1470
1472
1473 Con *__i3_scratch = workspace_get("__i3_scratch");
1474 owindow *current;
1475 TAILQ_FOREACH (current, &owindows, owindows) {
1476 Con *ws = con_get_workspace(current->con);
1477 /* If no workspace could be found, this was a dock window.
1478 * Just skip it, you cannot focus dock windows. */
1479 if (!ws) {
1480 continue;
1481 }
1482
1483 /* In case this is a scratchpad window, call scratchpad_show(). */
1484 if (ws == __i3_scratch && !focus_workspace) {
1485 scratchpad_show(current->con);
1486 /* While for the normal focus case we can change focus multiple
1487 * times and only a single window ends up focused, we could show
1488 * multiple scratchpad windows. So, rather break here. */
1489 break;
1490 }
1491
1492 if (focus_workspace) {
1493 /* Show the workspace of the matched container, without necessarily
1494 * focusing it. */
1495 LOG("focusing workspace %p / %s - %p / %s\n", current->con, current->con->name, ws, ws->name);
1496 workspace_show(ws);
1497 } else {
1498 LOG("focusing %p / %s\n", current->con, current->con->name);
1499 con_activate_unblock(current->con);
1500 }
1501 }
1502
1503 cmd_output->needs_tree_render = true;
1504 ysuccess(true);
1505}
1506
1507/*
1508 * Implementation of 'fullscreen enable|toggle [global]' and
1509 * 'fullscreen disable'
1510 *
1511 */
1512void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode) {
1513 fullscreen_mode_t mode = strcmp(fullscreen_mode, "global") == 0 ? CF_GLOBAL : CF_OUTPUT;
1514 DLOG("%s fullscreen, mode = %s\n", action, fullscreen_mode);
1515 owindow *current;
1516
1518
1519 TAILQ_FOREACH (current, &owindows, owindows) {
1520 DLOG("matching: %p / %s\n", current->con, current->con->name);
1521 if (strcmp(action, "toggle") == 0) {
1522 con_toggle_fullscreen(current->con, mode);
1523 } else if (strcmp(action, "enable") == 0) {
1524 con_enable_fullscreen(current->con, mode);
1525 } else if (strcmp(action, "disable") == 0) {
1526 con_disable_fullscreen(current->con);
1527 }
1528 }
1529
1530 cmd_output->needs_tree_render = true;
1531 // XXX: default reply for now, make this a better reply
1532 ysuccess(true);
1533}
1534
1535/*
1536 * Implementation of 'sticky enable|disable|toggle'.
1537 *
1538 */
1539void cmd_sticky(I3_CMD, const char *action) {
1540 DLOG("%s sticky on window\n", action);
1542
1543 owindow *current;
1544 TAILQ_FOREACH (current, &owindows, owindows) {
1545 if (current->con->window == NULL) {
1546 ELOG("only containers holding a window can be made sticky, skipping con = %p\n", current->con);
1547 continue;
1548 }
1549 DLOG("setting sticky for container = %p / %s\n", current->con, current->con->name);
1550
1551 bool sticky = false;
1552 if (strcmp(action, "enable") == 0)
1553 sticky = true;
1554 else if (strcmp(action, "disable") == 0)
1555 sticky = false;
1556 else if (strcmp(action, "toggle") == 0)
1557 sticky = !current->con->sticky;
1558
1559 current->con->sticky = sticky;
1560 ewmh_update_sticky(current->con->window->id, sticky);
1561 }
1562
1563 /* A window we made sticky might not be on a visible workspace right now, so we need to make
1564 * sure it gets pushed to the front now. */
1566
1568
1569 cmd_output->needs_tree_render = true;
1570 ysuccess(true);
1571}
1572
1573/*
1574 * Implementation of 'move <direction> [<amount> [px|ppt]]'.
1575 *
1576 */
1577void cmd_move_direction(I3_CMD, const char *direction_str, long amount, const char *mode) {
1578 owindow *current;
1580
1581 direction_t direction = parse_direction(direction_str);
1582
1583 const bool is_ppt = mode && strcmp(mode, "ppt") == 0;
1584
1585 DLOG("moving in direction %s, %ld %s\n", direction_str, amount, mode);
1586 TAILQ_FOREACH (current, &owindows, owindows) {
1587 if (con_is_floating(current->con)) {
1588 DLOG("floating move with %ld %s\n", amount, mode);
1589 Rect newrect = current->con->parent->rect;
1590 Con *output = con_get_output(current->con);
1591
1592 switch (direction) {
1593 case D_LEFT:
1594 newrect.x -= is_ppt ? output->rect.width * ((double)amount / 100.0) : amount;
1595 break;
1596 case D_RIGHT:
1597 newrect.x += is_ppt ? output->rect.width * ((double)amount / 100.0) : amount;
1598 break;
1599 case D_UP:
1600 newrect.y -= is_ppt ? output->rect.height * ((double)amount / 100.0) : amount;
1601 break;
1602 case D_DOWN:
1603 newrect.y += is_ppt ? output->rect.height * ((double)amount / 100.0) : amount;
1604 break;
1605 }
1606
1607 cmd_output->needs_tree_render = floating_reposition(current->con->parent, newrect);
1608 } else {
1609 tree_move(current->con, direction);
1610 cmd_output->needs_tree_render = true;
1611 }
1612 }
1613
1614 // XXX: default reply for now, make this a better reply
1615 ysuccess(true);
1616}
1617
1618/*
1619 * Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
1620 *
1621 */
1622void cmd_layout(I3_CMD, const char *layout_str) {
1624
1625 layout_t layout;
1626 if (!layout_from_name(layout_str, &layout)) {
1627 yerror("Unknown layout \"%s\", this is a mismatch between code and parser spec.", layout_str);
1628 return;
1629 }
1630
1631 DLOG("changing layout to %s (%d)\n", layout_str, layout);
1632
1633 owindow *current;
1634 TAILQ_FOREACH (current, &owindows, owindows) {
1635 if (con_is_docked(current->con)) {
1636 ELOG("cannot change layout of a docked container, skipping it.\n");
1637 continue;
1638 }
1639
1640 DLOG("matching: %p / %s\n", current->con, current->con->name);
1641 con_set_layout(current->con, layout);
1642 }
1643
1644 cmd_output->needs_tree_render = true;
1645 // XXX: default reply for now, make this a better reply
1646 ysuccess(true);
1647}
1648
1649/*
1650 * Implementation of 'layout toggle [all|split]'.
1651 *
1652 */
1653void cmd_layout_toggle(I3_CMD, const char *toggle_mode) {
1654 owindow *current;
1655
1656 if (toggle_mode == NULL)
1657 toggle_mode = "default";
1658
1659 DLOG("toggling layout (mode = %s)\n", toggle_mode);
1660
1661 /* check if the match is empty, not if the result is empty */
1663 con_toggle_layout(focused, toggle_mode);
1664 else {
1665 TAILQ_FOREACH (current, &owindows, owindows) {
1666 DLOG("matching: %p / %s\n", current->con, current->con->name);
1667 con_toggle_layout(current->con, toggle_mode);
1668 }
1669 }
1670
1671 cmd_output->needs_tree_render = true;
1672 // XXX: default reply for now, make this a better reply
1673 ysuccess(true);
1674}
1675
1676/*
1677 * Implementation of 'exit'.
1678 *
1679 */
1681 LOG("Exiting due to user command.\n");
1682 exit(EXIT_SUCCESS);
1683
1684 /* unreached */
1685}
1686
1687/*
1688 * Implementation of 'reload'.
1689 *
1690 */
1692 LOG("reloading\n");
1693
1696 /* start_nagbar() will refuse to start a new process if the passed pid is
1697 * set. This will happen when our child watcher is triggered by libev when
1698 * the loop re-starts. However, config errors might be detected before
1699 * that since we will read the config right now with load_configuration.
1700 * See #4104. */
1702
1705 /* Send an IPC event just in case the ws names have changed */
1706 ipc_send_workspace_event("reload", NULL, NULL);
1707 /* Send an update event for each barconfig just in case it has changed */
1708 Barconfig *current;
1709 TAILQ_FOREACH (current, &barconfigs, configs) {
1711 }
1712
1713 // XXX: default reply for now, make this a better reply
1714 ysuccess(true);
1715}
1716
1717/*
1718 * Implementation of 'restart'.
1719 *
1720 */
1722 LOG("restarting i3\n");
1723 int exempt_fd = -1;
1724 if (cmd_output->client != NULL) {
1725 exempt_fd = cmd_output->client->fd;
1726 LOG("Carrying file descriptor %d across restart\n", exempt_fd);
1727 int flags;
1728 if ((flags = fcntl(exempt_fd, F_GETFD)) < 0 ||
1729 fcntl(exempt_fd, F_SETFD, flags & ~FD_CLOEXEC) < 0) {
1730 ELOG("Could not disable FD_CLOEXEC on fd %d\n", exempt_fd);
1731 }
1732 char *fdstr = NULL;
1733 sasprintf(&fdstr, "%d", exempt_fd);
1734 setenv("_I3_RESTART_FD", fdstr, 1);
1735 }
1737 unlink(config.ipc_socket_path);
1738 if (current_log_stream_socket_path != NULL) {
1740 }
1741 /* We need to call this manually since atexit handlers don’t get called
1742 * when exec()ing */
1744 i3_restart(false);
1745 /* unreached */
1746 assert(false);
1747}
1748
1749/*
1750 * Implementation of 'open'.
1751 *
1752 */
1754 LOG("opening new container\n");
1755 Con *con = tree_open_con(NULL, NULL);
1756 con->layout = L_SPLITH;
1757 con_activate(con);
1758
1759 y(map_open);
1760 ystr("success");
1761 y(bool, true);
1762 ystr("id");
1763 y(integer, (uintptr_t)con);
1764 y(map_close);
1765
1766 cmd_output->needs_tree_render = true;
1767}
1768
1769/*
1770 * Implementation of 'focus output <output>'.
1771 *
1772 */
1773void cmd_focus_output(I3_CMD, const char *name) {
1774 static user_output_names_head names = TAILQ_HEAD_INITIALIZER(names);
1775 if (name) {
1776 user_output_names_add(&names, name);
1777 return;
1778 }
1779
1780 if (TAILQ_EMPTY(&names)) {
1781 yerror("At least one output must be specified");
1782 return;
1783 }
1784
1786
1787 if (TAILQ_EMPTY(&owindows)) {
1788 ysuccess(true);
1789 return;
1790 }
1791
1792 Output *current_output = get_output_for_con(TAILQ_FIRST(&owindows)->con);
1793 Output *target_output = user_output_names_find_next(&names, current_output);
1794 user_output_names_free(&names);
1795 bool success = false;
1796 if (target_output) {
1797 success = true;
1798
1799 /* get visible workspace on output */
1800 Con *ws = NULL;
1801 GREP_FIRST(ws, output_get_content(target_output->con), workspace_is_visible(child));
1802 if (!ws) {
1803 yerror("BUG: No workspace found on output.");
1804 return;
1805 }
1806
1807 workspace_show(ws);
1808 }
1809
1810 cmd_output->needs_tree_render = success;
1811 if (success) {
1812 ysuccess(true);
1813 } else {
1814 yerror("No output matched");
1815 }
1816}
1817
1818/*
1819 * Implementation of 'move [window|container] [to] [absolute] position [<pos_x> [px|ppt] <pos_y> [px|ppt]]
1820 *
1821 */
1822void cmd_move_window_to_position(I3_CMD, long x, const char *mode_x, long y, const char *mode_y) {
1823 bool has_error = false;
1824
1825 owindow *current;
1827
1828 TAILQ_FOREACH (current, &owindows, owindows) {
1829 if (!con_is_floating(current->con)) {
1830 ELOG("Cannot change position. The window/container is not floating\n");
1831
1832 if (!has_error) {
1833 yerror("Cannot change position of a window/container because it is not floating.");
1834 has_error = true;
1835 }
1836
1837 continue;
1838 }
1839
1840 Rect newrect = current->con->parent->rect;
1841 Con *output = con_get_output(current->con);
1842
1843 newrect.x = mode_x && strcmp(mode_x, "ppt") == 0 ? output->rect.width * ((double)x / 100.0) : x;
1844 newrect.y = mode_y && strcmp(mode_y, "ppt") == 0 ? output->rect.height * ((double)y / 100.0) : y;
1845 DLOG("moving to position %d %s %d %s\n", newrect.x, mode_x, newrect.y, mode_y);
1846
1847 if (!floating_reposition(current->con->parent, newrect)) {
1848 yerror("Cannot move window/container out of bounds.");
1849 has_error = true;
1850 }
1851 }
1852
1853 if (!has_error)
1854 ysuccess(true);
1855}
1856
1857/*
1858 * Implementation of 'move [window|container] [to] [absolute] position center
1859 *
1860 */
1861void cmd_move_window_to_center(I3_CMD, const char *method) {
1862 bool has_error = false;
1864
1865 owindow *current;
1866 TAILQ_FOREACH (current, &owindows, owindows) {
1867 Con *floating_con = con_inside_floating(current->con);
1868 if (floating_con == NULL) {
1869 ELOG("con %p / %s is not floating, cannot move it to the center.\n",
1870 current->con, current->con->name);
1871
1872 if (!has_error) {
1873 yerror("Cannot change position of a window/container because it is not floating.");
1874 has_error = true;
1875 }
1876
1877 continue;
1878 }
1879
1880 if (strcmp(method, "absolute") == 0) {
1881 DLOG("moving to absolute center\n");
1882 floating_center(floating_con, croot->rect);
1883
1884 floating_maybe_reassign_ws(floating_con);
1885 cmd_output->needs_tree_render = true;
1886 }
1887
1888 if (strcmp(method, "position") == 0) {
1889 DLOG("moving to center\n");
1890 floating_center(floating_con, con_get_workspace(floating_con)->rect);
1891
1892 cmd_output->needs_tree_render = true;
1893 }
1894 }
1895
1896 // XXX: default reply for now, make this a better reply
1897 if (!has_error)
1898 ysuccess(true);
1899}
1900
1901/*
1902 * Implementation of 'move [window|container] [to] position mouse'
1903 *
1904 */
1907
1908 owindow *current;
1909 TAILQ_FOREACH (current, &owindows, owindows) {
1910 Con *floating_con = con_inside_floating(current->con);
1911 if (floating_con == NULL) {
1912 DLOG("con %p / %s is not floating, cannot move it to the mouse position.\n",
1913 current->con, current->con->name);
1914 continue;
1915 }
1916
1917 DLOG("moving floating container %p / %s to cursor position\n", floating_con, floating_con->name);
1918 floating_move_to_pointer(floating_con);
1919 }
1920
1921 cmd_output->needs_tree_render = true;
1922 ysuccess(true);
1923}
1924
1925/*
1926 * Implementation of 'move scratchpad'.
1927 *
1928 */
1930 DLOG("should move window to scratchpad\n");
1931 owindow *current;
1932
1934
1935 TAILQ_FOREACH (current, &owindows, owindows) {
1936 DLOG("matching: %p / %s\n", current->con, current->con->name);
1937 scratchpad_move(current->con);
1938 }
1939
1940 cmd_output->needs_tree_render = true;
1941 // XXX: default reply for now, make this a better reply
1942 ysuccess(true);
1943}
1944
1945/*
1946 * Implementation of 'scratchpad show'.
1947 *
1948 */
1950 DLOG("should show scratchpad window\n");
1951 owindow *current;
1952 bool result = false;
1953
1955 result = scratchpad_show(NULL);
1956 } else {
1957 TAILQ_FOREACH (current, &owindows, owindows) {
1958 DLOG("matching: %p / %s\n", current->con, current->con->name);
1959 result |= scratchpad_show(current->con);
1960 }
1961 }
1962
1963 cmd_output->needs_tree_render = true;
1964
1965 ysuccess(result);
1966}
1967
1968/*
1969 * Implementation of 'swap [container] [with] id|con_id|mark <arg>'.
1970 *
1971 */
1972void cmd_swap(I3_CMD, const char *mode, const char *arg) {
1974
1975 owindow *match = TAILQ_FIRST(&owindows);
1976 if (match == NULL) {
1977 yerror("No match found for swapping.");
1978 return;
1979 }
1980 if (match->con == NULL) {
1981 yerror("Match %p has no container.", match);
1982 return;
1983 }
1984
1985 Con *con;
1986 if (strcmp(mode, "id") == 0) {
1987 long target;
1988 if (!parse_long(arg, &target, 0)) {
1989 yerror("Failed to parse %s into a window id.", arg);
1990 return;
1991 }
1992
1993 con = con_by_window_id(target);
1994 } else if (strcmp(mode, "con_id") == 0) {
1995 long target;
1996 if (!parse_long(arg, &target, 0)) {
1997 yerror("Failed to parse %s into a container id.", arg);
1998 return;
1999 }
2000
2001 con = con_by_con_id(target);
2002 } else if (strcmp(mode, "mark") == 0) {
2003 con = con_by_mark(arg);
2004 } else {
2005 yerror("Unhandled swap mode \"%s\". This is a bug.", mode);
2006 return;
2007 }
2008
2009 if (con == NULL) {
2010 yerror("Could not find container for %s = %s", mode, arg);
2011 return;
2012 }
2013
2014 if (match != TAILQ_LAST(&owindows, owindows_head)) {
2015 LOG("More than one container matched the swap command, only using the first one.");
2016 }
2017
2018 DLOG("Swapping %p with %p.\n", match->con, con);
2019 bool result = con_swap(match->con, con);
2020
2021 cmd_output->needs_tree_render = true;
2022 // XXX: default reply for now, make this a better reply
2023 ysuccess(result);
2024}
2025
2026/*
2027 * Implementation of 'title_format <format>'
2028 *
2029 */
2030void cmd_title_format(I3_CMD, const char *format) {
2031 DLOG("setting title_format to \"%s\"\n", format);
2033
2034 owindow *current;
2035 TAILQ_FOREACH (current, &owindows, owindows) {
2036 DLOG("setting title_format for %p / %s\n", current->con, current->con->name);
2037 FREE(current->con->title_format);
2038
2039 /* If we only display the title without anything else, we can skip the parsing step,
2040 * so we remove the title format altogether. */
2041 if (strcasecmp(format, "%title") != 0) {
2042 current->con->title_format = sstrdup(format);
2043
2044 if (current->con->window != NULL) {
2045 i3String *formatted_title = con_parse_title_format(current->con);
2046 ewmh_update_visible_name(current->con->window->id, i3string_as_utf8(formatted_title));
2047 I3STRING_FREE(formatted_title);
2048 }
2049 } else {
2050 if (current->con->window != NULL) {
2051 /* We can remove _NET_WM_VISIBLE_NAME since we don't display a custom title. */
2052 ewmh_update_visible_name(current->con->window->id, NULL);
2053 }
2054 }
2055
2056 if (current->con->window != NULL) {
2057 /* Make sure the window title is redrawn immediately. */
2058 current->con->window->name_x_changed = true;
2059 } else {
2060 /* For windowless containers we also need to force the redrawing. */
2061 FREE(current->con->deco_render_params);
2062 }
2063 }
2064
2065 cmd_output->needs_tree_render = true;
2066 ysuccess(true);
2067}
2068
2069/*
2070 * Implementation of 'title_window_icon <yes|no|toggle>' and 'title_window_icon padding <px>'
2071 *
2072 */
2073void cmd_title_window_icon(I3_CMD, const char *enable, int padding) {
2074 bool is_toggle = false;
2075 if (enable != NULL) {
2076 if (strcmp(enable, "toggle") == 0) {
2077 is_toggle = true;
2078 } else if (!boolstr(enable)) {
2079 padding = -1;
2080 }
2081 }
2082 DLOG("setting window_icon=%d\n", padding);
2084
2085 owindow *current;
2086 TAILQ_FOREACH (current, &owindows, owindows) {
2087 if (is_toggle) {
2088 const int current_padding = current->con->window_icon_padding;
2089 if (padding > 0) {
2090 if (current_padding < 0) {
2091 current->con->window_icon_padding = padding;
2092 } else {
2093 /* toggle off, but store padding given */
2094 current->con->window_icon_padding = -(padding + 1);
2095 }
2096 } else {
2097 /* Set to negative of (current value+1) to keep old padding when toggling */
2098 current->con->window_icon_padding = -(current_padding + 1);
2099 }
2100 } else {
2101 current->con->window_icon_padding = padding;
2102 }
2103 DLOG("Set window_icon for %p / %s to %d\n", current->con, current->con->name, current->con->window_icon_padding);
2104
2105 if (current->con->window != NULL) {
2106 /* Make sure the window title is redrawn immediately. */
2107 current->con->window->name_x_changed = true;
2108 } else {
2109 /* For windowless containers we also need to force the redrawing. */
2110 FREE(current->con->deco_render_params);
2111 }
2112 }
2113
2114 cmd_output->needs_tree_render = true;
2115 ysuccess(true);
2116}
2117
2118/*
2119 * Implementation of 'rename workspace [<name>] to <name>'
2120 *
2121 */
2122void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
2123 if (strncasecmp(new_name, "__", strlen("__")) == 0) {
2124 yerror("Cannot rename workspace to \"%s\": names starting with __ are i3-internal.", new_name);
2125 return;
2126 }
2127 if (old_name) {
2128 LOG("Renaming workspace \"%s\" to \"%s\"\n", old_name, new_name);
2129 } else {
2130 LOG("Renaming current workspace to \"%s\"\n", new_name);
2131 }
2132
2133 Con *workspace;
2134 if (old_name) {
2135 workspace = get_existing_workspace_by_name(old_name);
2136 } else {
2137 workspace = con_get_workspace(focused);
2138 old_name = workspace->name;
2139 }
2140
2141 if (!workspace) {
2142 yerror("Old workspace \"%s\" not found", old_name);
2143 return;
2144 }
2145
2146 Con *check_dest = get_existing_workspace_by_name(new_name);
2147
2148 /* If check_dest == workspace, the user might be changing the case of the
2149 * workspace, or it might just be a no-op. */
2150 if (check_dest != NULL && check_dest != workspace) {
2151 yerror("New workspace \"%s\" already exists", new_name);
2152 return;
2153 }
2154
2155 /* Change the name and try to parse it as a number. */
2156 /* old_name might refer to workspace->name, so copy it before free()ing */
2157 char *old_name_copy = sstrdup(old_name);
2158 FREE(workspace->name);
2159 workspace->name = sstrdup(new_name);
2160
2161 workspace->num = ws_name_to_number(new_name);
2162 LOG("num = %d\n", workspace->num);
2163
2164 /* By re-attaching, the sort order will be correct afterwards. */
2165 Con *previously_focused = focused;
2166 Con *previously_focused_content = focused->type == CT_WORKSPACE ? focused->parent : NULL;
2167 Con *parent = workspace->parent;
2168 con_detach(workspace);
2169 con_attach(workspace, parent, false);
2170 ipc_send_workspace_event("rename", workspace, NULL);
2171
2172 Con *assigned = get_assigned_output(workspace->name, workspace->num);
2173 if (assigned) {
2174 workspace_move_to_output(workspace, get_output_for_con(assigned));
2175 }
2176
2177 bool can_restore_focus = previously_focused != NULL;
2178 /* NB: If previously_focused is a workspace we can't work directly with it
2179 * since it might have been cleaned up by workspace_show() already,
2180 * depending on the focus order/number of other workspaces on the output.
2181 * Instead, we loop through the available workspaces and only focus
2182 * previously_focused if we still find it. */
2183 if (previously_focused_content) {
2184 Con *workspace = NULL;
2185 GREP_FIRST(workspace, previously_focused_content, child == previously_focused);
2186 can_restore_focus &= (workspace != NULL);
2187 }
2188
2189 if (can_restore_focus) {
2190 /* Restore the previous focus since con_attach messes with the focus. */
2191 workspace_show(con_get_workspace(previously_focused));
2192 con_focus(previously_focused);
2193 }
2194
2195 /* Let back-and-forth work after renaming the previous workspace.
2196 * See #3694. */
2197 if (previous_workspace_name && !strcmp(previous_workspace_name, old_name_copy)) {
2199 previous_workspace_name = sstrdup(new_name);
2200 }
2201
2202 cmd_output->needs_tree_render = true;
2203 ysuccess(true);
2204
2206
2207 startup_sequence_rename_workspace(old_name_copy, new_name);
2208 free(old_name_copy);
2209}
2210
2211/*
2212 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
2213 *
2214 */
2215void cmd_bar_mode(I3_CMD, const char *bar_mode, const char *bar_id) {
2216 int mode = M_DOCK;
2217 bool toggle = false;
2218 if (strcmp(bar_mode, "dock") == 0)
2219 mode = M_DOCK;
2220 else if (strcmp(bar_mode, "hide") == 0)
2221 mode = M_HIDE;
2222 else if (strcmp(bar_mode, "invisible") == 0)
2223 mode = M_INVISIBLE;
2224 else if (strcmp(bar_mode, "toggle") == 0)
2225 toggle = true;
2226 else {
2227 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2228 assert(false);
2229 }
2230
2231 if (TAILQ_EMPTY(&barconfigs)) {
2232 yerror("No bars found\n");
2233 return;
2234 }
2235
2236 Barconfig *current = NULL;
2237 TAILQ_FOREACH (current, &barconfigs, configs) {
2238 if (bar_id && strcmp(current->id, bar_id) != 0) {
2239 continue;
2240 }
2241
2242 if (toggle) {
2243 mode = (current->mode + 1) % 2;
2244 }
2245
2246 DLOG("Changing bar mode of bar_id '%s' from '%d' to '%s (%d)'\n",
2247 current->id, current->mode, bar_mode, mode);
2248 if ((int)current->mode != mode) {
2249 current->mode = mode;
2251 }
2252
2253 if (bar_id) {
2254 /* We are looking for a specific bar and we found it */
2255 ysuccess(true);
2256 return;
2257 }
2258 }
2259
2260 if (bar_id) {
2261 /* We are looking for a specific bar and we did not find it */
2262 yerror("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2263 } else {
2264 /* We have already handled the case of no bars, so we must have
2265 * updated all active bars now. */
2266 ysuccess(true);
2267 }
2268}
2269
2270/*
2271 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
2272 *
2273 */
2274void cmd_bar_hidden_state(I3_CMD, const char *bar_hidden_state, const char *bar_id) {
2275 int hidden_state = S_SHOW;
2276 bool toggle = false;
2277 if (strcmp(bar_hidden_state, "hide") == 0)
2278 hidden_state = S_HIDE;
2279 else if (strcmp(bar_hidden_state, "show") == 0)
2280 hidden_state = S_SHOW;
2281 else if (strcmp(bar_hidden_state, "toggle") == 0)
2282 toggle = true;
2283 else {
2284 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2285 assert(false);
2286 }
2287
2288 if (TAILQ_EMPTY(&barconfigs)) {
2289 yerror("No bars found\n");
2290 return;
2291 }
2292
2293 Barconfig *current = NULL;
2294 TAILQ_FOREACH (current, &barconfigs, configs) {
2295 if (bar_id && strcmp(current->id, bar_id) != 0) {
2296 continue;
2297 }
2298
2299 if (toggle) {
2300 hidden_state = (current->hidden_state + 1) % 2;
2301 }
2302
2303 DLOG("Changing bar hidden_state of bar_id '%s' from '%d' to '%s (%d)'\n",
2304 current->id, current->hidden_state, bar_hidden_state, hidden_state);
2305 if ((int)current->hidden_state != hidden_state) {
2306 current->hidden_state = hidden_state;
2308 }
2309
2310 if (bar_id) {
2311 /* We are looking for a specific bar and we found it */
2312 ysuccess(true);
2313 return;
2314 }
2315 }
2316
2317 if (bar_id) {
2318 /* We are looking for a specific bar and we did not find it */
2319 yerror("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2320 } else {
2321 /* We have already handled the case of no bars, so we must have
2322 * updated all active bars now. */
2323 ysuccess(true);
2324 }
2325}
2326
2327/*
2328 * Implementation of 'shmlog <size>|toggle|on|off'
2329 *
2330 */
2331void cmd_shmlog(I3_CMD, const char *argument) {
2332 if (!strcmp(argument, "toggle"))
2333 /* Toggle shm log, if size is not 0. If it is 0, set it to default. */
2335 else if (!strcmp(argument, "on"))
2337 else if (!strcmp(argument, "off"))
2338 shmlog_size = 0;
2339 else {
2340 long new_size = 0;
2341 if (!parse_long(argument, &new_size, 0)) {
2342 yerror("Failed to parse %s into a shmlog size.", argument);
2343 return;
2344 }
2345 /* If shm logging now, restart logging with the new size. */
2346 if (shmlog_size > 0) {
2347 shmlog_size = 0;
2348 LOG("Restarting shm logging...\n");
2349 init_logging();
2350 }
2351 shmlog_size = (int)new_size;
2352 }
2353 LOG("%s shm logging\n", shmlog_size > 0 ? "Enabling" : "Disabling");
2354 init_logging();
2356 ysuccess(true);
2357}
2358
2359/*
2360 * Implementation of 'debuglog toggle|on|off'
2361 *
2362 */
2363void cmd_debuglog(I3_CMD, const char *argument) {
2364 bool logging = get_debug_logging();
2365 if (!strcmp(argument, "toggle")) {
2366 LOG("%s debug logging\n", logging ? "Disabling" : "Enabling");
2367 set_debug_logging(!logging);
2368 } else if (!strcmp(argument, "on") && !logging) {
2369 LOG("Enabling debug logging\n");
2370 set_debug_logging(true);
2371 } else if (!strcmp(argument, "off") && logging) {
2372 LOG("Disabling debug logging\n");
2373 set_debug_logging(false);
2374 }
2375 // XXX: default reply for now, make this a better reply
2376 ysuccess(true);
2377}
2378
2379static int *gaps_inner(gaps_t *gaps) {
2380 return &(gaps->inner);
2381}
2382
2383static int *gaps_top(gaps_t *gaps) {
2384 return &(gaps->top);
2385}
2386
2387static int *gaps_left(gaps_t *gaps) {
2388 return &(gaps->left);
2389}
2390
2391static int *gaps_bottom(gaps_t *gaps) {
2392 return &(gaps->bottom);
2393}
2394
2395static int *gaps_right(gaps_t *gaps) {
2396 return &(gaps->right);
2397}
2398
2399typedef int *(*gap_accessor)(gaps_t *);
2400
2401static bool gaps_update(gap_accessor get, const char *scope, const char *mode, int pixels) {
2402 DLOG("gaps_update(scope=%s, mode=%s, pixels=%d)\n", scope, mode, pixels);
2403 Con *workspace = con_get_workspace(focused);
2404
2405 const int global_gap_size = *get(&(config.gaps));
2406 int current_value = global_gap_size;
2407 if (strcmp(scope, "current") == 0) {
2408 current_value += *get(&(workspace->gaps));
2409 }
2410 DLOG("global_gap_size=%d, current_value=%d\n", global_gap_size, current_value);
2411
2412 bool reset = false;
2413 if (strcmp(mode, "plus") == 0)
2414 current_value += pixels;
2415 else if (strcmp(mode, "minus") == 0)
2416 current_value -= pixels;
2417 else if (strcmp(mode, "set") == 0) {
2418 current_value = pixels;
2419 reset = true;
2420 } else if (strcmp(mode, "toggle") == 0) {
2421 current_value = !current_value * pixels;
2422 reset = true;
2423 } else {
2424 ELOG("Invalid mode %s when changing gaps", mode);
2425 return false;
2426 }
2427
2428 /* See https://github.com/Airblader/i3/issues/262 */
2429 int min_value = 0;
2430 const bool is_outer = get(&(config.gaps)) != gaps_inner(&(config.gaps));
2431 if (is_outer) {
2432 /* Outer gaps can compensate inner gaps. */
2433 if (strcmp(scope, "all") == 0) {
2434 min_value = -config.gaps.inner;
2435 } else {
2436 min_value = -config.gaps.inner - workspace->gaps.inner;
2437 }
2438 }
2439
2440 if (current_value < min_value) {
2441 current_value = min_value;
2442 }
2443
2444 if (strcmp(scope, "all") == 0) {
2445 Con *output = NULL;
2446 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
2447 Con *cur_ws = NULL;
2448 Con *content = output_get_content(output);
2449 TAILQ_FOREACH (cur_ws, &(content->nodes_head), nodes) {
2450 int *gaps_value = get(&(cur_ws->gaps));
2451 DLOG("current gaps_value = %d\n", *gaps_value);
2452
2453 if (reset) {
2454 *gaps_value = 0;
2455 } else {
2456 int max_compensate = 0;
2457 if (is_outer) {
2458 max_compensate = config.gaps.inner;
2459 }
2460 if (*gaps_value + current_value + max_compensate < 0) {
2461 /* Enforce new per-workspace gap size minimum value (in case
2462 current_value is smaller than before): the workspace can at most
2463 have a negative gap size of -current_value - max_compensate. */
2464 *gaps_value = -current_value - max_compensate;
2465 }
2466 }
2467 DLOG("gaps_value after fix = %d\n", *gaps_value);
2468 }
2469 }
2470
2471 *get(&(config.gaps)) = current_value;
2472 DLOG("global gaps value after fix = %d\n", *get(&(config.gaps)));
2473 } else {
2474 int *gaps_value = get(&(workspace->gaps));
2475 *gaps_value = current_value - global_gap_size;
2476 }
2477
2478 return true;
2479}
2480
2485void cmd_gaps(I3_CMD, const char *type, const char *scope, const char *mode, const char *value) {
2486 int pixels = logical_px(atoi(value));
2487
2488 if (!strcmp(type, "inner")) {
2489 if (!gaps_update(gaps_inner, scope, mode, pixels)) {
2490 goto error;
2491 }
2492 /* Update all workspaces with a no-op change (plus 0) so that the
2493 * minimum value is re-calculated and applied as a side effect. */
2494 if (!gaps_update(gaps_top, "all", "plus", 0) ||
2495 !gaps_update(gaps_bottom, "all", "plus", 0) ||
2496 !gaps_update(gaps_right, "all", "plus", 0) ||
2497 !gaps_update(gaps_left, "all", "plus", 0)) {
2498 goto error;
2499 }
2500 } else if (!strcmp(type, "outer")) {
2501 if (!gaps_update(gaps_top, scope, mode, pixels) ||
2502 !gaps_update(gaps_bottom, scope, mode, pixels) ||
2503 !gaps_update(gaps_right, scope, mode, pixels) ||
2504 !gaps_update(gaps_left, scope, mode, pixels)) {
2505 goto error;
2506 }
2507 } else if (!strcmp(type, "vertical")) {
2508 if (!gaps_update(gaps_top, scope, mode, pixels) ||
2509 !gaps_update(gaps_bottom, scope, mode, pixels)) {
2510 goto error;
2511 }
2512 } else if (!strcmp(type, "horizontal")) {
2513 if (!gaps_update(gaps_right, scope, mode, pixels) ||
2514 !gaps_update(gaps_left, scope, mode, pixels)) {
2515 goto error;
2516 }
2517 } else if (!strcmp(type, "top")) {
2518 if (!gaps_update(gaps_top, scope, mode, pixels)) {
2519 goto error;
2520 }
2521 } else if (!strcmp(type, "bottom")) {
2522 if (!gaps_update(gaps_bottom, scope, mode, pixels)) {
2523 goto error;
2524 }
2525 } else if (!strcmp(type, "right")) {
2526 if (!gaps_update(gaps_right, scope, mode, pixels)) {
2527 goto error;
2528 }
2529 } else if (!strcmp(type, "left")) {
2530 if (!gaps_update(gaps_left, scope, mode, pixels)) {
2531 goto error;
2532 }
2533 } else {
2534 ELOG("Invalid type %s when changing gaps", type);
2535 goto error;
2536 }
2537
2538 cmd_output->needs_tree_render = true;
2539 // XXX: default reply for now, make this a better reply
2540 ysuccess(true);
2541 return;
2542
2543error:
2544 ysuccess(false);
2545}
void switch_mode(const char *new_mode)
Switches the key bindings to the given mode, if the mode exists.
Definition bindings.c:620
pid_t command_error_nagbar_pid
Definition bindings.c:19
#define ysuccess(success)
Definition commands.c:20
static void cmd_resize_floating(I3_CMD, const char *direction_str, Con *floating_con, int px)
Definition commands.c:424
static int * gaps_bottom(gaps_t *gaps)
Definition commands.c:2391
#define CMD_FOCUS_WARN_CHILDREN
Definition commands.c:1300
static int * gaps_left(gaps_t *gaps)
Definition commands.c:2387
void cmd_move_con_to_mark(I3_CMD, const char *mark)
Implementation of 'move [window|container] [to] mark <str>'.
Definition commands.c:1153
void cmd_focus(I3_CMD, bool focus_workspace)
Implementation of 'focus'.
Definition commands.c:1457
static bool gaps_update(gap_accessor get, const char *scope, const char *mode, int pixels)
Definition commands.c:2401
#define y(x,...)
Definition commands.c:18
void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *no_auto_back_and_forth)
Implementation of 'move [–no-auto-back-and-forth] [window|container] [to] workspace number <number>'.
Definition commands.c:377
struct owindow owindow
void cmd_append_layout(I3_CMD, const char *cpath)
Implementation of 'append_layout <path>'.
Definition commands.c:775
void cmd_focus_sibling(I3_CMD, const char *direction_str)
Implementation of 'focus next|prev sibling'.
Definition commands.c:1357
static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *direction, int px, int ppt)
Definition commands.c:483
struct user_output_name user_output_name
void cmd_mode(I3_CMD, const char *mode)
Implementation of 'mode <string>'.
Definition commands.c:1015
void cmd_nop(I3_CMD, const char *comment)
Implementation of 'nop <comment>'.
Definition commands.c:764
void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name)
Implementation of 'rename workspace <name> to <name>'.
Definition commands.c:2122
void cmd_sticky(I3_CMD, const char *action)
Implementation of 'sticky enable|disable|toggle'.
Definition commands.c:1539
void cmd_bar_hidden_state(I3_CMD, const char *bar_hidden_state, const char *bar_id)
Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'.
Definition commands.c:2274
static int border_width_from_style(border_style_t border_style, long border_width, Con *con)
Definition commands.c:703
void cmd_bar_mode(I3_CMD, const char *bar_mode, const char *bar_id)
Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'.
Definition commands.c:2215
void cmd_floating(I3_CMD, const char *floating_mode)
Implementation of 'floating enable|disable|toggle'.
Definition commands.c:1173
static int * gaps_right(gaps_t *gaps)
Definition commands.c:2395
void cmd_criteria_match_windows(I3_CMD)
A match specification just finished (the closing square bracket was found), so we filter the list of ...
Definition commands.c:170
static direction_t parse_direction(const char *str)
Definition commands.c:409
static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name)
Definition commands.c:83
static Con * maybe_auto_back_and_forth_workspace(Con *workspace)
Definition commands.c:102
void cmd_focus_window_mode(I3_CMD, const char *window_mode)
Implementation of 'focus tiling|floating|mode_toggle'.
Definition commands.c:1392
void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle)
Implementation of 'mark [–add|–replace] [–toggle] <mark>'.
Definition commands.c:963
void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt)
Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
Definition commands.c:575
void cmd_move_con_to_output(I3_CMD, const char *name, bool move_workspace)
Implementation of 'move [window|container] [to] output <str>'.
Definition commands.c:1102
void cmd_exec(I3_CMD, const char *nosn, const char *command)
Implementation of 'exec [–no-startup-id] <command>'.
Definition commands.c:1275
void cmd_move_window_to_position(I3_CMD, long x, const char *mode_x, long y, const char *mode_y)
Implementation of 'move [window|container] [to] [absolute] position [<pos_x> [px|ppt] <pos_y> [px|ppt...
Definition commands.c:1822
void cmd_focus_direction(I3_CMD, const char *direction_str)
Implementation of 'focus left|right|up|down'.
Definition commands.c:1319
void cmd_move_con_to_workspace_back_and_forth(I3_CMD)
Implementation of 'move [window|container] [to] workspace back_and_forth'.
Definition commands.c:330
void cmd_restart(I3_CMD)
Implementation of 'restart'.
Definition commands.c:1721
void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *no_auto_back_and_forth)
Implementation of 'move [–no-auto-back-and-forth] [window|container] [to] workspace <name>'.
Definition commands.c:350
void cmd_move_window_to_mouse(I3_CMD)
Implementation of 'move [window|container] [to] position mouse'.
Definition commands.c:1905
void cmd_resize_set(I3_CMD, long cwidth, const char *mode_width, long cheight, const char *mode_height)
Implementation of 'resize set <width> [px | ppt] <height> [px | ppt]'.
Definition commands.c:654
#define CHECK_MOVE_CON_TO_WORKSPACE
Definition commands.c:265
void cmd_focus_level(I3_CMD, const char *level)
Implementation of 'focus parent|child'.
Definition commands.c:1429
void cmd_swap(I3_CMD, const char *mode, const char *arg)
Implementation of 'swap [container] [with] id|con_id|mark <arg>'.
Definition commands.c:1972
void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue)
Interprets a ctype=cvalue pair and adds it to the current match specification.
Definition commands.c:253
#define HANDLE_EMPTY_MATCH
When the command did not include match criteria (!), we use the currently focused container.
Definition commands.c:59
void cmd_layout(I3_CMD, const char *layout_str)
Implementation of 'layout default|stacked|stacking|tabbed|splitv|splith'.
Definition commands.c:1622
void cmd_title_window_icon(I3_CMD, const char *enable, int padding)
Implementation of 'title_window_icon <yes|no|toggle>' and 'title_window_icon <padding|toggle> <px>'.
Definition commands.c:2073
void cmd_workspace(I3_CMD, const char *which)
Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
Definition commands.c:858
void cmd_gaps(I3_CMD, const char *type, const char *scope, const char *mode, const char *value)
Implementation of 'gaps inner|outer|top|right|bottom|left|horizontal|vertical current|all set|plus|mi...
Definition commands.c:2485
void cmd_unmark(I3_CMD, const char *mark)
Implementation of 'unmark [mark]'.
Definition commands.c:996
void cmd_fullscreen(I3_CMD, const char *action, const char *fullscreen_mode)
Implementation of 'fullscreen [enable|disable|toggle] [global]'.
Definition commands.c:1512
void cmd_move_con_to_workspace(I3_CMD, const char *which)
Implementation of 'move [window|container] [to] workspace next|prev|next_on_output|prev_on_output'.
Definition commands.c:297
#define ystr(str)
Definition commands.c:19
void cmd_open(I3_CMD)
Implementation of 'open'.
Definition commands.c:1753
void cmd_shmlog(I3_CMD, const char *argument)
Implementation of 'shmlog <size>|toggle|on|off'.
Definition commands.c:2331
void cmd_debuglog(I3_CMD, const char *argument)
Implementation of 'debuglog toggle|on|off'.
Definition commands.c:2363
#define yerror(format,...)
Definition commands.c:29
void cmd_kill(I3_CMD, const char *kill_mode_str)
Implementation of 'kill [window|client]'.
Definition commands.c:1243
void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth)
Implementation of 'workspace [–no-auto-back-and-forth] number <number>'.
Definition commands.c:889
static bool resize_set_tiling(I3_CMD, Con *target, orientation_t resize_orientation, bool is_ppt, long target_size)
Definition commands.c:621
void cmd_move_direction(I3_CMD, const char *direction_str, long amount, const char *mode)
Implementation of 'move <direction> [<amount> [px|ppt]]'.
Definition commands.c:1577
void cmd_move_window_to_center(I3_CMD, const char *method)
Implementation of 'move [window|container] [to] [absolute] position center.
Definition commands.c:1861
void cmd_title_format(I3_CMD, const char *format)
Implementation of 'title_format <format>'.
Definition commands.c:2030
void cmd_layout_toggle(I3_CMD, const char *toggle_mode)
Implementation of 'layout toggle [all|split]'.
Definition commands.c:1653
void cmd_focus_output(I3_CMD, const char *name)
Implementation of 'focus output <output>'.
Definition commands.c:1773
static int * gaps_inner(gaps_t *gaps)
Definition commands.c:2379
void cmd_border(I3_CMD, const char *border_style_str, long border_width)
Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
Definition commands.c:727
static Output * user_output_names_find_next(user_output_names_head *names, Output *current_output)
Definition commands.c:1053
void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth)
Implementation of 'workspace [–no-auto-back-and-forth] <name>'.
Definition commands.c:937
static int * gaps_top(gaps_t *gaps)
Definition commands.c:2383
void cmd_scratchpad_show(I3_CMD)
Implementation of 'scratchpad show'.
Definition commands.c:1949
static void disable_global_fullscreen(void)
Definition commands.c:847
static void move_matches_to_workspace(Con *ws)
Definition commands.c:257
void cmd_split(I3_CMD, const char *direction)
Implementation of 'split v|h|t|vertical|horizontal|toggle'.
Definition commands.c:1204
static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *direction, int px, double ppt)
Definition commands.c:502
void cmd_workspace_back_and_forth(I3_CMD)
Implementation of 'workspace back_and_forth'.
Definition commands.c:923
static void user_output_names_free(user_output_names_head *names)
Definition commands.c:1088
void cmd_reload(I3_CMD)
Implementation of 'reload'.
Definition commands.c:1691
int *(* gap_accessor)(gaps_t *)
Definition commands.c:2399
void cmd_exit(I3_CMD)
Implementation of 'exit'.
Definition commands.c:1680
void cmd_move_scratchpad(I3_CMD)
Implementation of 'move scratchpad'.
Definition commands.c:1929
static Match current_match
void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp, bool ignore_focus)
Moves the given container to the currently focused container on the given workspace.
Definition con.c:1466
bool con_move_to_mark(Con *con, const char *mark)
Moves the given container to the given mark.
Definition con.c:1436
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
Definition con.c:525
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition con.c:596
void con_close(Con *con, kill_window_t kill_window)
Closes the given container.
Definition con.c:331
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition con.c:1517
void con_unmark(Con *con, const char *name)
Removes marks from containers.
Definition con.c:836
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:477
void con_disable_fullscreen(Con *con)
Disables fullscreen mode for the given container, if necessary.
Definition con.c:1195
bool con_is_docked(Con *con)
Returns true if the container is a docked container.
Definition con.c:605
void con_set_border_style(Con *con, border_style_t border_style, int border_width)
Sets the given border style on con, correctly keeping the position/size of a floating window.
Definition con.c:1845
Con * con_by_con_id(long target)
Returns the container with the given container ID or NULL if no such container exists.
Definition con.c:686
void con_mark(Con *con, const char *mark, mark_mode_t mode)
Assigns a mark to the container.
Definition con.c:806
Con * con_by_window_id(xcb_window_t window)
Returns the container with the given client window ID or NULL if no such container exists.
Definition con.c:671
void con_activate_unblock(Con *con)
Activates the container like in con_activate but removes fullscreen restrictions and properly warps t...
Definition con.c:297
Con * con_by_mark(const char *mark)
Returns the container with the given mark or NULL if no such container exists.
Definition con.c:726
void con_mark_toggle(Con *con, const char *mark, mark_mode_t mode)
Toggles the mark on a container.
Definition con.c:791
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition con.c:620
bool con_fullscreen_permits_focusing(Con *con)
Returns true if changing the focus to con would be allowed considering the fullscreen focus constrain...
Definition con.c:2184
bool con_swap(Con *first, Con *second)
Swaps the two containers.
Definition con.c:2425
bool con_accepts_window(Con *con)
Returns true if this node accepts a window (if the node swallows windows, it might already have swall...
Definition con.c:444
uint32_t con_rect_size_in_orientation(Con *con)
Returns given container's rect size depending on its orientation.
Definition con.c:2545
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:588
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition con.c:230
void con_move_to_output(Con *con, Output *output, bool fix_coordinates)
Moves the given container to the currently focused container on the visible workspace on the given ou...
Definition con.c:1484
void con_toggle_fullscreen(Con *con, int fullscreen_mode)
Toggles fullscreen mode for the given container.
Definition con.c:1095
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition con.c:222
i3String * con_parse_title_format(Con *con)
Returns the window title considering the current title format.
Definition con.c:2376
int con_num_children(Con *con)
Returns the number of children of this container.
Definition con.c:983
void con_set_layout(Con *con, layout_t layout)
This function changes the layout of a given container.
Definition con.c:1883
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition con.c:287
void con_toggle_layout(Con *con, const char *toggle_mode)
This function toggles the layout of a given container.
Definition con.c:1968
void con_enable_fullscreen(Con *con, fullscreen_mode_t fullscreen_mode)
Enables fullscreen mode for the given container, if necessary.
Definition con.c:1149
void con_focus(Con *con)
Sets input focus to the given container.
Definition con.c:246
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:463
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1590
Config config
Definition config.c:19
bool load_configuration(const char *override_configpath, config_load_t load_type)
(Re-)loads the configuration file (sets useful defaults before).
Definition config.c:167
struct barconfig_head barconfigs
Definition config.c:21
pid_t config_error_nagbar_pid
void ewmh_update_visible_name(xcb_window_t window, const char *name)
Updates _NET_WM_VISIBLE_NAME.
Definition ewmh.c:216
void ewmh_update_desktop_properties(void)
Updates all the EWMH desktop properties.
Definition ewmh.c:118
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Definition ewmh.c:184
void ewmh_update_sticky(xcb_window_t window, bool sticky)
Set or remove _NET_WM_STATE_STICKY on the window.
Definition ewmh.c:279
void floating_check_size(Con *floating_con, bool prefer_height)
Called when a floating window is created or resized.
Definition floating.c:75
void floating_disable(Con *con)
Disables floating mode for the given container by re-attaching the container to its old parent.
Definition floating.c:418
void floating_center(Con *con, Rect rect)
Centers a floating con above the specified rect.
Definition floating.c:527
void toggle_floating_mode(Con *con, bool automatic)
Calls floating_enable() for tiling containers and floating_disable() for floating containers.
Definition floating.c:455
void floating_resize(Con *floating_con, uint32_t x, uint32_t y)
Sets size of the CT_FLOATING_CON to specified dimensions.
Definition floating.c:769
bool floating_maybe_reassign_ws(Con *con)
Checks if con’s coordinates are within its workspace and re-assigns it to the actual workspace if not...
Definition floating.c:488
void floating_move_to_pointer(Con *con)
Moves the given floating con to the current pointer position.
Definition floating.c:536
bool floating_enable(Con *con, bool automatic)
Enables floating mode for the given container by detaching it from its parent, creating a new contain...
Definition floating.c:232
bool floating_reposition(Con *con, Rect newrect)
Repositions the CT_FLOATING_CON to have the coordinates specified by newrect, but only if the coordin...
Definition floating.c:742
struct pending_marks * marks
bool json_validate(const char *buf, const size_t len)
Returns true if the provided JSON could be parsed by yajl.
json_content_t json_determine_content(const char *buf, const size_t len)
void tree_append_json(Con *con, const char *buf, const size_t len, char **errormsg)
bool match_is_empty(Match *match)
Check if a match is empty.
Definition match.c:39
void match_init(Match *match)
Initializes the Match data structure.
Definition match.c:26
void match_free(Match *match)
Frees the given match.
Definition match.c:276
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition match.c:90
void match_parse_property(Match *match, const char *ctype, const char *cvalue)
Interprets a ctype=cvalue pair and adds it to the given match specification.
Definition match.c:292
void tree_move(Con *con, direction_t direction)
Moves the given container in the given direction.
Definition move.c:259
Output * get_output_from_string(Output *current_output, const char *output_str)
Returns an 'output' corresponding to one of left/right/down/up or a specific output name.
Definition output.c:33
void output_push_sticky_windows(Con *old_focus)
Iterates over all outputs and pushes sticky windows to the currently visible workspace on that output...
Definition output.c:77
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
Definition output.c:53
Output * get_output_for_con(Con *con)
Returns the output for the given con.
Definition output.c:57
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition output.c:16
struct outputs_head outputs
Definition randr.c:22
bool regex_matches(struct regex *regex, const char *input)
Checks if the given regular expression matches the given input and returns true if it does.
Definition regex.c:61
void render_con(Con *con)
"Renders" the given container (and its children), meaning that all rects are updated correctly.
Definition render.c:42
bool resize_find_tiling_participants(Con **current, Con **other, direction_t direction, bool both_sides)
Definition resize.c:70
double percent_for_1px(Con *con)
Calculate the minimum percent needed for the given container to be at least 1 pixel.
Definition resize.c:129
bool resize_neighboring_cons(Con *first, Con *second, int px, int ppt)
Resize the two given containers using the given amount of pixels or percentage points.
Definition resize.c:144
void restore_open_placeholder_windows(Con *parent)
Open placeholder windows for all children of parent.
bool scratchpad_show(Con *con)
Either shows the top-most scratchpad window (con == NULL) or shows the specified con (if it is scratc...
Definition scratchpad.c:85
void scratchpad_move(Con *con)
Moves the specified window to the __i3_scratch workspace, making it floating and setting the appropri...
Definition scratchpad.c:19
void start_application(const char *command, bool no_startup_id)
Starts the given application by passing it through a shell.
Definition startup.c:132
void startup_sequence_rename_workspace(const char *old_name, const char *new_name)
Renames workspaces that are mentioned in the startup sequences.
Definition startup.c:255
struct Con * focused
Definition tree.c:13
struct Con * croot
Definition tree.c:12
void tree_next(Con *con, direction_t direction)
Changes focus in the given direction.
Definition tree.c:591
bool level_up(void)
Moves focus one level up.
Definition tree.c:386
bool level_down(void)
Moves focus one level down.
Definition tree.c:409
Con * tree_open_con(Con *con, i3Window *window)
Opens an empty container in the current container.
Definition tree.c:149
struct all_cons_head all_cons
Definition tree.c:15
void tree_split(Con *con, orientation_t orientation)
Splits (horizontally or vertically) the given container by creating a new container which contains th...
Definition tree.c:327
Con * get_tree_next_sibling(Con *con, position_t direction)
Get the previous / next sibling.
Definition tree.c:633
orientation_t orientation_from_direction(direction_t direction)
Convert a direction to its corresponding orientation.
Definition util.c:456
int ws_name_to_number(const char *name)
Parses the workspace name as a number.
Definition util.c:109
void kill_nagbar(pid_t nagbar_pid, bool wait_for_it)
Kills the i3-nagbar process, if nagbar_pid != -1.
Definition util.c:387
void i3_restart(bool forget_layout)
Restart i3 in-place appends -a to argument list to disable autostart.
Definition util.c:278
bool parse_long(const char *str, long *out, int base)
Converts a string into a long using strtol().
Definition util.c:409
bool rect_equals(Rect a, Rect b)
Definition util.c:59
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...
Definition util.c:427
bool layout_from_name(const char *layout_str, layout_t *out)
Set 'out' to the layout_t value for the given layout.
Definition util.c:82
direction_t direction_from_orientation_position(orientation_t orientation, position_t position)
Convert orientation and position to the corresponding direction.
Definition util.c:472
Con * workspace_back_and_forth_get(void)
Returns the previously focused workspace con, or NULL if unavailable.
Definition workspace.c:822
Con * get_existing_workspace_by_name(const char *name)
Returns the workspace with the given name or NULL if such a workspace does not exist.
Definition workspace.c:30
Con * workspace_next_on_output(void)
Returns the next workspace on the same output.
Definition workspace.c:697
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition workspace.c:428
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
Definition workspace.c:314
Con * workspace_prev_on_output(void)
Returns the previous workspace on the same output.
Definition workspace.c:752
Con * workspace_get(const char *num)
Returns a pointer to the workspace with the given number (starting at 0), creating the workspace if n...
Definition workspace.c:131
void workspace_back_and_forth(void)
Focuses the previously focused workspace.
Definition workspace.c:809
void workspace_move_to_output(Con *ws, Output *output)
Move the given workspace to the specified output.
Definition workspace.c:974
Con * workspace_next(void)
Returns the next workspace.
Definition workspace.c:568
Con * get_existing_workspace_by_num(int num)
Returns the workspace with the given number or NULL if such a workspace does not exist.
Definition workspace.c:44
Con * workspace_prev(void)
Returns the previous workspace.
Definition workspace.c:631
void workspace_show_by_name(const char *num)
Looks up the workspace by name and switches to it.
Definition workspace.c:560
char * previous_workspace_name
Stores a copy of the name of the last used workspace for the workspace back-and-forth switching.
Definition workspace.c:19
Con * get_assigned_output(const char *name, long parsed_num)
Returns the first output that is assigned to a workspace specified by the given name or number.
Definition workspace.c:84
void update_shmlog_atom(void)
Set up the SHMLOG_PATH atom.
Definition x.c:1445
void x_set_i3_atoms(void)
Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
Definition x.c:1459
void ipc_shutdown(shutdown_reason_t reason, int exempt_fd)
Calls shutdown() on each socket and closes it.
Definition ipc.c:192
void ipc_send_workspace_event(const char *change, Con *current, Con *old)
For the workspace events we send, along with the usual "change" field, also the workspace container i...
Definition ipc.c:1618
void ipc_send_barconfig_update_event(Barconfig *barconfig)
For the barconfig update events, we send the serialized barconfig.
Definition ipc.c:1663
int shmlog_size
Definition log.c:47
bool get_debug_logging(void)
Checks if debug logging is active.
Definition log.c:212
void init_logging(void)
Initializes logging by creating an error logfile in /tmp (or XDG_RUNTIME_DIR, see get_process_filenam...
Definition log.c:95
void set_debug_logging(const bool _debug_logging)
Set debug logging.
Definition log.c:220
char * current_log_stream_socket_path
Definition log.c:380
void purge_zerobyte_logfile(void)
Deletes the unused log files.
Definition log.c:358
const int default_shmlog_size
Definition main.c:84
void cmd_criteria_init(I3_CMD)
Initializes the specified 'Match' data structure and the initial state of commands....
#define I3_CMD
The beginning of the prototype for every cmd_ function.
Definition commands.h:17
@ C_RELOAD
position_t
Definition data.h:63
@ AFTER
Definition data.h:64
@ BEFORE
Definition data.h:63
layout_t
Container layouts.
Definition data.h:105
@ L_SPLITH
Definition data.h:112
mark_mode_t
Definition data.h:99
@ MM_ADD
Definition data.h:100
@ MM_REPLACE
Definition data.h:99
orientation_t
Definition data.h:60
@ VERT
Definition data.h:62
@ HORIZ
Definition data.h:61
fullscreen_mode_t
Fullscreen modes.
Definition data.h:633
@ CF_OUTPUT
Definition data.h:634
@ CF_GLOBAL
Definition data.h:635
border_style_t
Definition data.h:65
@ BS_NONE
Definition data.h:66
@ BS_PIXEL
Definition data.h:67
@ BS_NORMAL
Definition data.h:68
@ KILL_CLIENT
Definition data.h:75
@ KILL_WINDOW
Definition data.h:74
direction_t
Definition data.h:56
@ D_RIGHT
Definition data.h:57
@ D_LEFT
Definition data.h:56
@ D_UP
Definition data.h:58
@ D_DOWN
Definition data.h:59
struct _i3String i3String
Opaque data structure for storing strings.
Definition libi3.h:49
int logical_px(const int logical)
Convert a logical amount of pixels (e.g.
char * resolve_tilde(const char *path)
This function resolves ~ in pathnames.
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define ELOG(fmt,...)
Definition libi3.h:100
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
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...
bool boolstr(const char *str)
Reports whether str represents the enabled state (1, yes, true, …).
#define I3STRING_FREE(str)
Securely i3string_free by setting the pointer to NULL to prevent accidentally using freed memory.
Definition libi3.h:243
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
json_content_t
Definition load_layout.h:15
@ JSON_CONTENT_UNKNOWN
Definition load_layout.h:18
@ JSON_CONTENT_WORKSPACE
Definition load_layout.h:27
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_END(head)
Definition queue.h:337
#define TAILQ_INIT(head)
Definition queue.h:360
#define TAILQ_HEAD(name, type)
Definition queue.h:318
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:376
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:402
#define TAILQ_NEXT(elm, field)
Definition queue.h:338
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
#define TAILQ_EMPTY(head)
Definition queue.h:344
#define TAILQ_LAST(head, headname)
Definition queue.h:339
#define TAILQ_ENTRY(type)
Definition queue.h:327
#define GREP_FIRST(dest, head, condition)
Definition util.h:38
#define FREE(pointer)
Definition util.h:47
#define STARTS_WITH(string, needle)
Definition util.h:25
@ SHUTDOWN_REASON_RESTART
Definition ipc.h:94
Con * con
Definition commands.c:131
Holds an intermediate representation of the result of a call to any command.
gaps_t gaps
bool workspace_auto_back_and_forth
Automatic workspace back and forth switching.
int default_border_width
int default_floating_border_width
char * ipc_socket_path
border_style_t default_border
The default border style for new windows.
border_style_t default_floating_border
The default border style for new floating windows.
Holds the status bar configuration (i3bar).
enum Barconfig::@8 hidden_state
char * id
Automatically generated ID for this bar config.
enum Barconfig::@7 mode
Bar display mode (hide unless modifier is pressed or show in dock mode or always hide in invisible mo...
Definition data.h:150
int inner
Definition data.h:151
int left
Definition data.h:155
int right
Definition data.h:153
int top
Definition data.h:152
int bottom
Definition data.h:154
Stores a rectangle, for example the size of a window, the child window etc.
Definition data.h:189
uint32_t height
Definition data.h:193
uint32_t x
Definition data.h:190
uint32_t y
Definition data.h:191
uint32_t width
Definition data.h:192
An Output is a physical output on your graphics driver.
Definition data.h:395
Con * con
Pointer to the Con which represents this output.
Definition data.h:415
bool primary
Definition data.h:407
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition data.h:428
int height_increment
Definition data.h:495
bool name_x_changed
Flag to force re-rendering the decoration upon changes.
Definition data.h:456
xcb_window_t id
Definition data.h:429
enum Window::@11 dock
Whether the window says it is a dock window.
int width_increment
Definition data.h:494
struct regex * mark
Definition data.h:541
Con * con_id
Definition data.h:566
Definition data.h:637
char * name
Definition data.h:638
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:647
struct Con * parent
Definition data.h:682
enum Con::@20 scratchpad_state
enum Con::@18 type
double percent
Definition data.h:716
struct Rect rect
Definition data.h:686
gaps_t gaps
Only applicable for containers of type CT_WORKSPACE.
Definition data.h:680
bool sticky
Definition data.h:743
border_style_t max_user_border_style
Definition data.h:768
layout_t layout
Definition data.h:759
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
Definition data.h:677
int window_icon_padding
Whether the window icon should be displayed, and with what padding.
Definition data.h:704
struct Window * window
Definition data.h:722
char * title_format
The format with which the window's name should be displayed.
Definition data.h:699
border_style_t border_style
Definition data.h:761
char * name
Definition data.h:696
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition data.h:728