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