This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
/
commands.c
2193 lines (1869 loc) · 68.6 KB
/
commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
* commands.c: all command functions (see commands_parser.c)
*
*/
#include "all.h"
#include <stdint.h>
#include <float.h>
#include <stdarg.h>
#ifdef I3_ASAN_ENABLED
#include <sanitizer/lsan_interface.h>
#endif
#include "shmlog.h"
// Macros to make the YAJL API a bit easier to use.
#define y(x, ...) (cmd_output->json_gen != NULL ? yajl_gen_##x(cmd_output->json_gen, ##__VA_ARGS__) : 0)
#define ystr(str) (cmd_output->json_gen != NULL ? yajl_gen_string(cmd_output->json_gen, (unsigned char *)str, strlen(str)) : 0)
#define ysuccess(success) \
do { \
if (cmd_output->json_gen != NULL) { \
y(map_open); \
ystr("success"); \
y(bool, success); \
y(map_close); \
} \
} while (0)
#define yerror(format, ...) \
do { \
if (cmd_output->json_gen != NULL) { \
char *message; \
sasprintf(&message, format, ##__VA_ARGS__); \
y(map_open); \
ystr("success"); \
y(bool, false); \
ystr("error"); \
ystr(message); \
y(map_close); \
free(message); \
} \
} while (0)
/** If an error occurred during parsing of the criteria, we want to exit instead
* of relying on fallback behavior. See #2091. */
#define HANDLE_INVALID_MATCH \
do { \
if (current_match->error != NULL) { \
yerror("Invalid match: %s", current_match->error); \
return; \
} \
} while (0)
/** When the command did not include match criteria (!), we use the currently
* focused container. Do not confuse this case with a command which included
* criteria but which did not match any windows. This macro has to be called in
* every command.
*/
#define HANDLE_EMPTY_MATCH \
do { \
HANDLE_INVALID_MATCH; \
\
if (match_is_empty(current_match)) { \
while (!TAILQ_EMPTY(&owindows)) { \
owindow *ow = TAILQ_FIRST(&owindows); \
TAILQ_REMOVE(&owindows, ow, owindows); \
free(ow); \
} \
owindow *ow = smalloc(sizeof(owindow)); \
ow->con = focused; \
TAILQ_INIT(&owindows); \
TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
} \
} while (0)
/*
* Checks whether we switched to a new workspace and returns false in that case,
* signaling that further workspace switching should be done by the calling function
* If not, calls workspace_back_and_forth() if workspace_auto_back_and_forth is set
* and return true, signaling that no further workspace switching should occur in the calling function.
*
*/
static bool maybe_back_and_forth(struct CommandResultIR *cmd_output, const char *name) {
Con *ws = con_get_workspace(focused);
/* If we switched to a different workspace, do nothing */
if (strcmp(ws->name, name) != 0)
return false;
DLOG("This workspace is already focused.\n");
if (config.workspace_auto_back_and_forth) {
workspace_back_and_forth();
cmd_output->needs_tree_render = true;
}
return true;
}
/*
* Return the passed workspace unless it is the current one and auto back and
* forth is enabled, in which case the back_and_forth workspace is returned.
*/
static Con *maybe_auto_back_and_forth_workspace(Con *workspace) {
Con *current, *baf;
if (!config.workspace_auto_back_and_forth)
return workspace;
current = con_get_workspace(focused);
if (current == workspace) {
baf = workspace_back_and_forth_get();
if (baf != NULL) {
DLOG("Substituting workspace with back_and_forth, as it is focused.\n");
return baf;
}
}
return workspace;
}
/*******************************************************************************
* Criteria functions.
******************************************************************************/
/*
* Helper data structure for an operation window (window on which the operation
* will be performed). Used to build the TAILQ owindows.
*
*/
typedef struct owindow {
Con *con;
TAILQ_ENTRY(owindow)
owindows;
} owindow;
typedef TAILQ_HEAD(owindows_head, owindow) owindows_head;
static owindows_head owindows;
/*
* Initializes the specified 'Match' data structure and the initial state of
* commands.c for matching target windows of a command.
*
*/
void cmd_criteria_init(I3_CMD) {
Con *con;
owindow *ow;
DLOG("Initializing criteria, current_match = %p\n", current_match);
match_free(current_match);
match_init(current_match);
while (!TAILQ_EMPTY(&owindows)) {
ow = TAILQ_FIRST(&owindows);
TAILQ_REMOVE(&owindows, ow, owindows);
free(ow);
}
TAILQ_INIT(&owindows);
/* copy all_cons */
TAILQ_FOREACH(con, &all_cons, all_cons) {
ow = smalloc(sizeof(owindow));
ow->con = con;
TAILQ_INSERT_TAIL(&owindows, ow, owindows);
}
}
/*
* A match specification just finished (the closing square bracket was found),
* so we filter the list of owindows.
*
*/
void cmd_criteria_match_windows(I3_CMD) {
owindow *next, *current;
DLOG("match specification finished, matching...\n");
/* copy the old list head to iterate through it and start with a fresh
* list which will contain only matching windows */
struct owindows_head old = owindows;
TAILQ_INIT(&owindows);
for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
/* make a copy of the next pointer and advance the pointer to the
* next element as we are going to invalidate the element’s
* next/prev pointers by calling TAILQ_INSERT_TAIL later */
current = next;
next = TAILQ_NEXT(next, owindows);
DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
/* We use this flag to prevent matching on window-less containers if
* only window-specific criteria were specified. */
bool accept_match = false;
if (current_match->con_id != NULL) {
accept_match = true;
if (current_match->con_id == current->con) {
DLOG("con_id matched.\n");
} else {
DLOG("con_id does not match.\n");
FREE(current);
continue;
}
}
if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
accept_match = true;
bool matched_by_mark = false;
mark_t *mark;
TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
if (!regex_matches(current_match->mark, mark->name))
continue;
DLOG("match by mark\n");
matched_by_mark = true;
break;
}
if (!matched_by_mark) {
DLOG("mark does not match.\n");
FREE(current);
continue;
}
}
if (current->con->window != NULL) {
if (match_matches_window(current_match, current->con->window)) {
DLOG("matches window!\n");
accept_match = true;
} else {
DLOG("doesn't match\n");
FREE(current);
continue;
}
}
if (accept_match) {
TAILQ_INSERT_TAIL(&owindows, current, owindows);
} else {
FREE(current);
continue;
}
}
TAILQ_FOREACH(current, &owindows, owindows) {
DLOG("matching: %p / %s\n", current->con, current->con->name);
}
}
/*
* Interprets a ctype=cvalue pair and adds it to the current match
* specification.
*
*/
void cmd_criteria_add(I3_CMD, const char *ctype, const char *cvalue) {
match_parse_property(current_match, ctype, cvalue);
}
static void move_matches_to_workspace(Con *ws) {
owindow *current;
TAILQ_FOREACH(current, &owindows, owindows) {
DLOG("matching: %p / %s\n", current->con, current->con->name);
con_move_to_workspace(current->con, ws, true, false, false);
}
}
#define CHECK_MOVE_CON_TO_WORKSPACE \
do { \
HANDLE_EMPTY_MATCH; \
if (TAILQ_EMPTY(&owindows)) { \
yerror("Nothing to move: specified criteria don't match any window"); \
return; \
} else { \
bool found = false; \
owindow *current = TAILQ_FIRST(&owindows); \
while (current) { \
owindow *next = TAILQ_NEXT(current, owindows); \
\
if (current->con->type == CT_WORKSPACE && !con_has_children(current->con)) { \
TAILQ_REMOVE(&owindows, current, owindows); \
} else { \
found = true; \
} \
\
current = next; \
} \
if (!found) { \
yerror("Nothing to move: workspace empty"); \
return; \
} \
} \
} while (0)
/*
* Implementation of 'move [window|container] [to] workspace
* next|prev|next_on_output|prev_on_output|current'.
*
*/
void cmd_move_con_to_workspace(I3_CMD, const char *which) {
DLOG("which=%s\n", which);
CHECK_MOVE_CON_TO_WORKSPACE;
/* get the workspace */
Con *ws;
if (strcmp(which, "next") == 0)
ws = workspace_next();
else if (strcmp(which, "prev") == 0)
ws = workspace_prev();
else if (strcmp(which, "next_on_output") == 0)
ws = workspace_next_on_output();
else if (strcmp(which, "prev_on_output") == 0)
ws = workspace_prev_on_output();
else if (strcmp(which, "current") == 0)
ws = con_get_workspace(focused);
else {
yerror("BUG: called with which=%s", which);
return;
}
move_matches_to_workspace(ws);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/**
* Implementation of 'move [window|container] [to] workspace back_and_forth'.
*
*/
void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
Con *ws = workspace_back_and_forth_get();
if (ws == NULL) {
yerror("No workspace was previously active.");
return;
}
HANDLE_EMPTY_MATCH;
move_matches_to_workspace(ws);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
*
*/
void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *no_auto_back_and_forth) {
if (strncasecmp(name, "__", strlen("__")) == 0) {
yerror("You cannot move containers to i3-internal workspaces (\"%s\").", name);
return;
}
CHECK_MOVE_CON_TO_WORKSPACE;
LOG("should move window to workspace %s\n", name);
/* get the workspace */
Con *ws = workspace_get(name, NULL);
if (no_auto_back_and_forth == NULL) {
ws = maybe_auto_back_and_forth_workspace(ws);
}
move_matches_to_workspace(ws);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
*
*/
void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *no_auto_back_and_forth) {
CHECK_MOVE_CON_TO_WORKSPACE;
LOG("should move window to workspace %s\n", which);
long parsed_num = ws_name_to_number(which);
if (parsed_num == -1) {
LOG("Could not parse initial part of \"%s\" as a number.\n", which);
yerror("Could not parse number \"%s\"", which);
return;
}
Con *ws = get_existing_workspace_by_num(parsed_num);
if (!ws) {
ws = workspace_get(which, NULL);
}
if (no_auto_back_and_forth == NULL) {
ws = maybe_auto_back_and_forth_workspace(ws);
}
move_matches_to_workspace(ws);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Convert a string direction ("left", "right", etc.) to a direction_t. Assumes
* valid direction string.
*/
static direction_t parse_direction(const char *str) {
if (strcmp(str, "left") == 0) {
return D_LEFT;
} else if (strcmp(str, "right") == 0) {
return D_RIGHT;
} else if (strcmp(str, "up") == 0) {
return D_UP;
} else if (strcmp(str, "down") == 0) {
return D_DOWN;
} else {
ELOG("Invalid direction. This is a parser bug.\n");
assert(false);
}
}
static void cmd_resize_floating(I3_CMD, const char *way, const char *direction_str, Con *floating_con, int px) {
Rect old_rect = floating_con->rect;
Con *focused_con = con_descend_focused(floating_con);
direction_t direction;
if (strcmp(direction_str, "height") == 0) {
direction = D_DOWN;
} else if (strcmp(direction_str, "width") == 0) {
direction = D_RIGHT;
} else {
direction = parse_direction(direction_str);
}
orientation_t orientation = orientation_from_direction(direction);
/* ensure that resize will take place even if pixel increment is smaller than
* height increment or width increment.
* fixes #1011 */
const i3Window *window = focused_con->window;
if (window != NULL) {
if (orientation == VERT) {
if (px < 0) {
px = (-px < window->height_increment) ? -window->height_increment : px;
} else {
px = (px < window->height_increment) ? window->height_increment : px;
}
} else {
if (px < 0) {
px = (-px < window->width_increment) ? -window->width_increment : px;
} else {
px = (px < window->width_increment) ? window->width_increment : px;
}
}
}
if (orientation == VERT) {
floating_con->rect.height += px;
} else {
floating_con->rect.width += px;
}
floating_check_size(floating_con);
/* Did we actually resize anything or did the size constraints prevent us?
* If we could not resize, exit now to not move the window. */
if (memcmp(&old_rect, &(floating_con->rect), sizeof(Rect)) == 0) {
return;
}
if (direction == D_UP) {
floating_con->rect.y -= (floating_con->rect.height - old_rect.height);
} else if (direction == D_LEFT) {
floating_con->rect.x -= (floating_con->rect.width - old_rect.width);
}
/* If this is a scratchpad window, don't auto center it from now on. */
if (floating_con->scratchpad_state == SCRATCHPAD_FRESH) {
floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
}
}
static bool cmd_resize_tiling_direction(I3_CMD, Con *current, const char *direction, int px, int ppt) {
Con *second = NULL;
Con *first = current;
direction_t search_direction = parse_direction(direction);
bool res = resize_find_tiling_participants(&first, &second, search_direction, false);
if (!res) {
yerror("No second container found in this direction.");
return false;
}
if (ppt) {
/* For backwards compatibility, 'X px or Y ppt' means that ppt is
* preferred. */
px = 0;
}
return resize_neighboring_cons(first, second, px, ppt);
}
static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *direction, int px, double ppt) {
LOG("width/height resize\n");
/* get the appropriate current container (skip stacked/tabbed cons) */
Con *dummy = NULL;
direction_t search_direction = (strcmp(direction, "width") == 0 ? D_LEFT : D_DOWN);
bool search_result = resize_find_tiling_participants(¤t, &dummy, search_direction, true);
if (search_result == false) {
yerror("Failed to find appropriate tiling containers for resize operation");
return false;
}
/* get the default percentage */
int children = con_num_children(current->parent);
LOG("ins. %d children\n", children);
double percentage = 1.0 / children;
LOG("default percentage = %f\n", percentage);
/* Ensure all the other children have a percentage set. */
Con *child;
TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
LOG("child->percent = %f (child %p)\n", child->percent, child);
if (child->percent == 0.0)
child->percent = percentage;
}
double new_current_percent;
double subtract_percent;
if (ppt != 0.0) {
new_current_percent = current->percent + ppt;
} else {
new_current_percent = px_resize_to_percent(current, px);
ppt = new_current_percent - current->percent;
}
subtract_percent = ppt / (children - 1);
if (ppt < 0.0 && new_current_percent < percent_for_1px(current)) {
yerror("Not resizing, container would end with less than 1px");
return false;
}
LOG("new_current_percent = %f\n", new_current_percent);
LOG("subtract_percent = %f\n", subtract_percent);
/* Ensure that the new percentages are positive. */
if (subtract_percent >= 0.0) {
TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
if (child == current) {
continue;
}
if (child->percent - subtract_percent < percent_for_1px(child)) {
yerror("Not resizing, already at minimum size (child %p would end up with a size of %.f", child, child->percent - subtract_percent);
return false;
}
}
}
current->percent = new_current_percent;
LOG("current->percent after = %f\n", current->percent);
TAILQ_FOREACH(child, &(current->parent->nodes_head), nodes) {
if (child == current)
continue;
child->percent -= subtract_percent;
LOG("child->percent after (%p) = %f\n", child, child->percent);
}
return true;
}
/*
* Implementation of 'resize grow|shrink <direction> [<px> px] [or <ppt> ppt]'.
*
*/
void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, long resize_ppt) {
DLOG("resizing in way %s, direction %s, px %ld or ppt %ld\n", way, direction, resize_px, resize_ppt);
if (strcmp(way, "shrink") == 0) {
resize_px *= -1;
resize_ppt *= -1;
}
HANDLE_EMPTY_MATCH;
owindow *current;
TAILQ_FOREACH(current, &owindows, owindows) {
/* Don't handle dock windows (issue #1201) */
if (current->con->window && current->con->window->dock) {
DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
continue;
}
Con *floating_con;
if ((floating_con = con_inside_floating(current->con))) {
cmd_resize_floating(current_match, cmd_output, way, direction, floating_con, resize_px);
} else {
if (strcmp(direction, "width") == 0 ||
strcmp(direction, "height") == 0) {
const double ppt = (double)resize_ppt / 100.0;
if (!cmd_resize_tiling_width_height(current_match, cmd_output,
current->con, direction,
resize_px, ppt))
return;
} else {
if (!cmd_resize_tiling_direction(current_match, cmd_output,
current->con, direction,
resize_px, resize_ppt))
return;
}
}
}
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
static bool resize_set_tiling(I3_CMD, Con *target, orientation_t resize_orientation, bool is_ppt, long target_size) {
direction_t search_direction;
char *mode;
if (resize_orientation == HORIZ) {
search_direction = D_LEFT;
mode = "width";
} else {
search_direction = D_DOWN;
mode = "height";
}
/* Get the appropriate current container (skip stacked/tabbed cons) */
Con *dummy;
resize_find_tiling_participants(&target, &dummy, search_direction, true);
/* Calculate new size for the target container */
double ppt = 0.0;
int px = 0;
if (is_ppt) {
ppt = (double)target_size / 100.0 - target->percent;
} else {
px = target_size - (resize_orientation == HORIZ ? target->rect.width : target->rect.height);
}
/* Perform resizing and report failure if not possible */
return cmd_resize_tiling_width_height(current_match, cmd_output,
target, mode, px, ppt);
}
/*
* Implementation of 'resize set <width> [px | ppt] <height> [px | ppt]'.
*
*/
void cmd_resize_set(I3_CMD, long cwidth, const char *mode_width, long cheight, const char *mode_height) {
DLOG("resizing to %ld %s x %ld %s\n", cwidth, mode_width, cheight, mode_height);
if (cwidth < 0 || cheight < 0) {
ELOG("Resize failed: dimensions cannot be negative (was %ld %s x %ld %s)\n", cwidth, mode_width, cheight, mode_height);
return;
}
HANDLE_EMPTY_MATCH;
owindow *current;
bool success = true;
TAILQ_FOREACH(current, &owindows, owindows) {
Con *floating_con;
if ((floating_con = con_inside_floating(current->con))) {
Con *output = con_get_output(floating_con);
if (cwidth == 0) {
cwidth = floating_con->rect.width;
} else if (mode_width && strcmp(mode_width, "ppt") == 0) {
cwidth = output->rect.width * ((double)cwidth / 100.0);
}
if (cheight == 0) {
cheight = floating_con->rect.height;
} else if (mode_height && strcmp(mode_height, "ppt") == 0) {
cheight = output->rect.height * ((double)cheight / 100.0);
}
floating_resize(floating_con, cwidth, cheight);
} else {
if (current->con->window && current->con->window->dock) {
DLOG("This is a dock window. Not resizing (con = %p)\n)", current->con);
continue;
}
if (cwidth > 0) {
bool is_ppt = mode_width && strcmp(mode_width, "ppt") == 0;
success &= resize_set_tiling(current_match, cmd_output, current->con,
HORIZ, is_ppt, cwidth);
}
if (cheight > 0) {
bool is_ppt = mode_height && strcmp(mode_height, "ppt") == 0;
success &= resize_set_tiling(current_match, cmd_output, current->con,
VERT, is_ppt, cheight);
}
}
}
cmd_output->needs_tree_render = true;
ysuccess(success);
}
static int border_width_from_style(border_style_t border_style, long border_width, Con *con) {
if (border_style == BS_NONE) {
return 0;
}
if (border_width >= 0) {
return logical_px(border_width);
}
const bool is_floating = con_inside_floating(con) != NULL;
/* Load the configured defaults. */
if (is_floating && border_style == config.default_floating_border) {
return config.default_floating_border_width;
} else if (!is_floating && border_style == config.default_border) {
return config.default_border_width;
} else {
/* Use some hardcoded values. */
return logical_px(border_style == BS_NORMAL ? 2 : 1);
}
}
/*
* Implementation of 'border normal|pixel [<n>]', 'border none|1pixel|toggle'.
*
*/
void cmd_border(I3_CMD, const char *border_style_str, long border_width) {
DLOG("border style should be changed to %s with border width %ld\n", border_style_str, border_width);
owindow *current;
HANDLE_EMPTY_MATCH;
TAILQ_FOREACH(current, &owindows, owindows) {
DLOG("matching: %p / %s\n", current->con, current->con->name);
border_style_t border_style;
if (strcmp(border_style_str, "toggle") == 0) {
border_style = (current->con->border_style + 1) % 3;
} else if (strcmp(border_style_str, "normal") == 0) {
border_style = BS_NORMAL;
} else if (strcmp(border_style_str, "pixel") == 0) {
border_style = BS_PIXEL;
} else if (strcmp(border_style_str, "none") == 0) {
border_style = BS_NONE;
} else {
yerror("BUG: called with border_style=%s", border_style_str);
return;
}
const int con_border_width = border_width_from_style(border_style, border_width, current->con);
con_set_border_style(current->con, border_style, con_border_width);
}
cmd_output->needs_tree_render = true;
ysuccess(true);
}
/*
* Implementation of 'nop <comment>'.
*
*/
void cmd_nop(I3_CMD, const char *comment) {
LOG("-------------------------------------------------\n");
LOG(" NOP: %s\n", comment);
LOG("-------------------------------------------------\n");
ysuccess(true);
}
/*
* Implementation of 'append_layout <path>'.
*
*/
void cmd_append_layout(I3_CMD, const char *cpath) {
LOG("Appending layout \"%s\"\n", cpath);
/* Make sure we allow paths like '~/.i3/layout.json' */
char *path = resolve_tilde(cpath);
char *buf = NULL;
ssize_t len;
if ((len = slurp(path, &buf)) < 0) {
/* slurp already logged an error. */
goto out;
}
if (!json_validate(buf, len)) {
ELOG("Could not parse \"%s\" as JSON, not loading.\n", path);
yerror("Could not parse \"%s\" as JSON.", path);
goto out;
}
json_content_t content = json_determine_content(buf, len);
LOG("JSON content = %d\n", content);
if (content == JSON_CONTENT_UNKNOWN) {
ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
yerror("Could not determine the contents of \"%s\".", path);
goto out;
}
Con *parent = focused;
if (content == JSON_CONTENT_WORKSPACE) {
parent = output_get_content(con_get_output(parent));
} else {
/* We need to append the layout to a split container, since a leaf
* container must not have any children (by definition).
* Note that we explicitly check for workspaces, since they are okay for
* this purpose, but con_accepts_window() returns false for workspaces. */
while (parent->type != CT_WORKSPACE && !con_accepts_window(parent))
parent = parent->parent;
}
DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
char *errormsg = NULL;
tree_append_json(parent, buf, len, &errormsg);
if (errormsg != NULL) {
yerror(errormsg);
free(errormsg);
/* Note that we continue executing since tree_append_json() has
* side-effects — user-provided layouts can be partly valid, partly
* invalid, leading to half of the placeholder containers being
* created. */
} else {
ysuccess(true);
}
// XXX: This is a bit of a kludge. Theoretically, render_con(parent,
// false); should be enough, but when sending 'workspace 4; append_layout
// /tmp/foo.json', the needs_tree_render == true of the workspace command
// is not executed yet and will be batched with append_layout’s
// needs_tree_render after the parser finished. We should check if that is
// necessary at all.
render_con(croot, false);
restore_open_placeholder_windows(parent);
if (content == JSON_CONTENT_WORKSPACE)
ipc_send_workspace_event("restored", parent, NULL);
cmd_output->needs_tree_render = true;
out:
free(path);
free(buf);
}
/*
* Implementation of 'workspace next|prev|next_on_output|prev_on_output'.
*
*/
void cmd_workspace(I3_CMD, const char *which) {
Con *ws;
DLOG("which=%s\n", which);
if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
yerror("Cannot switch workspace while in global fullscreen");
return;
}
if (strcmp(which, "next") == 0)
ws = workspace_next();
else if (strcmp(which, "prev") == 0)
ws = workspace_prev();
else if (strcmp(which, "next_on_output") == 0)
ws = workspace_next_on_output();
else if (strcmp(which, "prev_on_output") == 0)
ws = workspace_prev_on_output();
else {
yerror("BUG: called with which=%s", which);
return;
}
workspace_show(ws);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
*
*/
void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
yerror("Cannot switch workspace while in global fullscreen");
return;
}
long parsed_num = ws_name_to_number(which);
if (parsed_num == -1) {
yerror("Could not parse initial part of \"%s\" as a number.", which);
return;
}
Con *workspace = get_existing_workspace_by_num(parsed_num);
if (!workspace) {
LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
ysuccess(true);
workspace_show_by_name(which);
cmd_output->needs_tree_render = true;
return;
}
if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name)) {
ysuccess(true);
return;
}
workspace_show(workspace);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Implementation of 'workspace back_and_forth'.
*
*/
void cmd_workspace_back_and_forth(I3_CMD) {
if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
yerror("Cannot switch workspace while in global fullscreen");
return;
}
workspace_back_and_forth();
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Implementation of 'workspace [--no-auto-back-and-forth] <name>'
*
*/
void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
if (strncasecmp(name, "__", strlen("__")) == 0) {
yerror("You cannot switch to the i3-internal workspaces (\"%s\").", name);
return;
}
if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
yerror("Cannot switch workspace while in global fullscreen");
return;
}
DLOG("should switch to workspace %s\n", name);
if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name)) {
ysuccess(true);
return;
}
workspace_show_by_name(name);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Implementation of 'mark [--add|--replace] [--toggle] <mark>'
*
*/
void cmd_mark(I3_CMD, const char *mark, const char *mode, const char *toggle) {
HANDLE_EMPTY_MATCH;
owindow *current = TAILQ_FIRST(&owindows);
if (current == NULL) {
yerror("Given criteria don't match a window");
return;
}
/* Marks must be unique, i.e., no two windows must have the same mark. */
if (current != TAILQ_LAST(&owindows, owindows_head)) {
yerror("A mark must not be put onto more than one window");
return;
}
DLOG("matching: %p / %s\n", current->con, current->con->name);
mark_mode_t mark_mode = (mode == NULL || strcmp(mode, "--replace") == 0) ? MM_REPLACE : MM_ADD;
if (toggle != NULL) {
con_mark_toggle(current->con, mark, mark_mode);
} else {
con_mark(current->con, mark, mark_mode);
}
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
ysuccess(true);
}
/*
* Implementation of 'unmark [mark]'
*
*/
void cmd_unmark(I3_CMD, const char *mark) {