This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
/
con.c
2125 lines (1856 loc) · 68.9 KB
/
con.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)
*
* con.c: Functions which deal with containers directly (creating containers,
* searching containers, getting specific properties from containers,
* …).
*
*/
#include "all.h"
#include "yajl_utils.h"
static void con_on_remove_child(Con *con);
/*
* force parent split containers to be redrawn
*
*/
void con_force_split_parents_redraw(Con *con) {
Con *parent = con;
while (parent && parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
if (!con_is_leaf(parent))
FREE(parent->deco_render_params);
parent = parent->parent;
}
}
/*
* Create a new container (and attach it to the given parent, if not NULL).
* This function only initializes the data structures.
*
*/
Con *con_new_skeleton(Con *parent, i3Window *window) {
Con *new = scalloc(1, sizeof(Con));
new->on_remove_child = con_on_remove_child;
TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
new->type = CT_CON;
new->window = window;
new->border_style = config.default_border;
new->current_border_width = -1;
if (window) {
new->depth = window->depth;
new->window->aspect_ratio = 0.0;
} else {
new->depth = root_depth;
}
DLOG("opening window\n");
TAILQ_INIT(&(new->floating_head));
TAILQ_INIT(&(new->nodes_head));
TAILQ_INIT(&(new->focus_head));
TAILQ_INIT(&(new->swallow_head));
TAILQ_INIT(&(new->marks_head));
if (parent != NULL)
con_attach(new, parent, false);
return new;
}
/* A wrapper for con_new_skeleton, to retain the old con_new behaviour
*
*/
Con *con_new(Con *parent, i3Window *window) {
Con *new = con_new_skeleton(parent, window);
x_con_init(new);
return new;
}
static void _con_attach(Con *con, Con *parent, Con *previous, bool ignore_focus) {
con->parent = parent;
Con *loop;
Con *current = previous;
struct nodes_head *nodes_head = &(parent->nodes_head);
struct focus_head *focus_head = &(parent->focus_head);
/* Workspaces are handled differently: they need to be inserted at the
* right position. */
if (con->type == CT_WORKSPACE) {
DLOG("it's a workspace. num = %d\n", con->num);
if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
TAILQ_INSERT_TAIL(nodes_head, con, nodes);
} else {
current = TAILQ_FIRST(nodes_head);
if (con->num < current->num) {
/* we need to insert the container at the beginning */
TAILQ_INSERT_HEAD(nodes_head, con, nodes);
} else {
while (current->num != -1 && con->num > current->num) {
current = TAILQ_NEXT(current, nodes);
if (current == TAILQ_END(nodes_head)) {
current = NULL;
break;
}
}
/* we need to insert con after current, if current is not NULL */
if (current)
TAILQ_INSERT_BEFORE(current, con, nodes);
else
TAILQ_INSERT_TAIL(nodes_head, con, nodes);
}
}
goto add_to_focus_head;
}
if (con->type == CT_FLOATING_CON) {
DLOG("Inserting into floating containers\n");
TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
} else {
if (!ignore_focus) {
/* Get the first tiling container in focus stack */
TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
if (loop->type == CT_FLOATING_CON)
continue;
current = loop;
break;
}
}
/* When the container is not a split container (but contains a window)
* and is attached to a workspace, we check if the user configured a
* workspace_layout. This is done in workspace_attach_to, which will
* provide us with the container to which we should attach (either the
* workspace or a new split container with the configured
* workspace_layout).
*/
if (con->window != NULL &&
parent->type == CT_WORKSPACE &&
parent->workspace_layout != L_DEFAULT) {
DLOG("Parent is a workspace. Applying default layout...\n");
Con *target = workspace_attach_to(parent);
/* Attach the original con to this new split con instead */
nodes_head = &(target->nodes_head);
focus_head = &(target->focus_head);
con->parent = target;
current = NULL;
DLOG("done\n");
}
/* Insert the container after the tiling container, if found.
* When adding to a CT_OUTPUT, just append one after another. */
if (current && parent->type != CT_OUTPUT) {
DLOG("Inserting con = %p after con %p\n", con, current);
TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
} else
TAILQ_INSERT_TAIL(nodes_head, con, nodes);
}
add_to_focus_head:
/* We insert to the TAIL because con_focus() will correct this.
* This way, we have the option to insert Cons without having
* to focus them. */
TAILQ_INSERT_TAIL(focus_head, con, focused);
con_force_split_parents_redraw(con);
}
/*
* Attaches the given container to the given parent. This happens when moving
* a container or when inserting a new container at a specific place in the
* tree.
*
* ignore_focus is to just insert the Con at the end (useful when creating a
* new split container *around* some containers, that is, detaching and
* attaching them in order without wanting to mess with the focus in between).
*
*/
void con_attach(Con *con, Con *parent, bool ignore_focus) {
_con_attach(con, parent, NULL, ignore_focus);
}
/*
* Detaches the given container from its current parent
*
*/
void con_detach(Con *con) {
con_force_split_parents_redraw(con);
if (con->type == CT_FLOATING_CON) {
TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
} else {
TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
}
}
/*
* Sets input focus to the given container. Will be updated in X11 in the next
* run of x_push_changes().
*
*/
void con_focus(Con *con) {
assert(con != NULL);
DLOG("con_focus = %p\n", con);
/* 1: set focused-pointer to the new con */
/* 2: exchange the position of the container in focus stack of the parent all the way up */
TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
if (con->parent->parent != NULL)
con_focus(con->parent);
focused = con;
/* We can't blindly reset non-leaf containers since they might have
* other urgent children. Therefore we only reset leafs and propagate
* the changes upwards via con_update_parents_urgency() which does proper
* checks before resetting the urgency.
*/
if (con->urgent && con_is_leaf(con)) {
con_set_urgency(con, false);
con_update_parents_urgency(con);
workspace_update_urgent_flag(con_get_workspace(con));
ipc_send_window_event("urgent", con);
}
}
/*
* Closes the given container.
*
*/
void con_close(Con *con, kill_window_t kill_window) {
assert(con != NULL);
DLOG("Closing con = %p.\n", con);
/* We never close output or root containers. */
if (con->type == CT_OUTPUT || con->type == CT_ROOT) {
DLOG("con = %p is of type %d, not closing anything.\n", con, con->type);
return;
}
if (con->type == CT_WORKSPACE) {
DLOG("con = %p is a workspace, closing all children instead.\n", con);
Con *child, *nextchild;
for (child = TAILQ_FIRST(&(con->focus_head)); child;) {
nextchild = TAILQ_NEXT(child, focused);
DLOG("killing child = %p.\n", child);
tree_close_internal(child, kill_window, false, false);
child = nextchild;
}
return;
}
tree_close_internal(con, kill_window, false, false);
}
/*
* Returns true when this node is a leaf node (has no children)
*
*/
bool con_is_leaf(Con *con) {
return TAILQ_EMPTY(&(con->nodes_head));
}
/*
* Returns true when this con is a leaf node with a managed X11 window (e.g.,
* excluding dock containers)
*/
bool con_has_managed_window(Con *con) {
return (con != NULL && con->window != NULL && con->window->id != XCB_WINDOW_NONE && con_get_workspace(con) != NULL);
}
/**
* Returns true if this node has regular or floating children.
*
*/
bool con_has_children(Con *con) {
return (!con_is_leaf(con) || !TAILQ_EMPTY(&(con->floating_head)));
}
/*
* Returns true if a container should be considered split.
*
*/
bool con_is_split(Con *con) {
if (con_is_leaf(con))
return false;
switch (con->layout) {
case L_DOCKAREA:
case L_OUTPUT:
return false;
default:
return true;
}
}
/*
* This will only return true for containers which have some parent with
* a tabbed / stacked parent of which they are not the currently focused child.
*
*/
bool con_is_hidden(Con *con) {
Con *current = con;
/* ascend to the workspace level and memorize the highest-up container
* which is stacked or tabbed. */
while (current != NULL && current->type != CT_WORKSPACE) {
Con *parent = current->parent;
if (parent != NULL && (parent->layout == L_TABBED || parent->layout == L_STACKED)) {
if (TAILQ_FIRST(&(parent->focus_head)) != current)
return true;
}
current = parent;
}
return false;
}
/*
* Returns whether the container or any of its children is sticky.
*
*/
bool con_is_sticky(Con *con) {
if (con->sticky)
return true;
Con *child;
TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
if (con_is_sticky(child))
return true;
}
return false;
}
/*
* Returns true if this node accepts a window (if the node swallows windows,
* it might already have swallowed enough and cannot hold any more).
*
*/
bool con_accepts_window(Con *con) {
/* 1: workspaces never accept direct windows */
if (con->type == CT_WORKSPACE)
return false;
if (con_is_split(con)) {
DLOG("container %p does not accept windows, it is a split container.\n", con);
return false;
}
/* TODO: if this is a swallowing container, we need to check its max_clients */
return (con->window == NULL);
}
/*
* Gets the output container (first container with CT_OUTPUT in hierarchy) this
* node is on.
*
*/
Con *con_get_output(Con *con) {
Con *result = con;
while (result != NULL && result->type != CT_OUTPUT)
result = result->parent;
/* We must be able to get an output because focus can never be set higher
* in the tree (root node cannot be focused). */
assert(result != NULL);
return result;
}
/*
* Gets the workspace container this node is on.
*
*/
Con *con_get_workspace(Con *con) {
Con *result = con;
while (result != NULL && result->type != CT_WORKSPACE)
result = result->parent;
return result;
}
/*
* Searches parenst of the given 'con' until it reaches one with the specified
* 'orientation'. Aborts when it comes across a floating_con.
*
*/
Con *con_parent_with_orientation(Con *con, orientation_t orientation) {
DLOG("Searching for parent of Con %p with orientation %d\n", con, orientation);
Con *parent = con->parent;
if (parent->type == CT_FLOATING_CON)
return NULL;
while (con_orientation(parent) != orientation) {
DLOG("Need to go one level further up\n");
parent = parent->parent;
/* Abort when we reach a floating con, or an output con */
if (parent &&
(parent->type == CT_FLOATING_CON ||
parent->type == CT_OUTPUT ||
(parent->parent && parent->parent->type == CT_OUTPUT)))
parent = NULL;
if (parent == NULL)
break;
}
DLOG("Result: %p\n", parent);
return parent;
}
/*
* helper data structure for the breadth-first-search in
* con_get_fullscreen_con()
*
*/
struct bfs_entry {
Con *con;
TAILQ_ENTRY(bfs_entry)
entries;
};
/*
* Returns the first fullscreen node below this node.
*
*/
Con *con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode) {
Con *current, *child;
/* TODO: is breadth-first-search really appropriate? (check as soon as
* fullscreen levels and fullscreen for containers is implemented) */
TAILQ_HEAD(bfs_head, bfs_entry)
bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
entry->con = con;
TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
while (!TAILQ_EMPTY(&bfs_head)) {
entry = TAILQ_FIRST(&bfs_head);
current = entry->con;
if (current != con && current->fullscreen_mode == fullscreen_mode) {
/* empty the queue */
while (!TAILQ_EMPTY(&bfs_head)) {
entry = TAILQ_FIRST(&bfs_head);
TAILQ_REMOVE(&bfs_head, entry, entries);
free(entry);
}
return current;
}
TAILQ_REMOVE(&bfs_head, entry, entries);
free(entry);
TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
entry = smalloc(sizeof(struct bfs_entry));
entry->con = child;
TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
}
TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
entry = smalloc(sizeof(struct bfs_entry));
entry->con = child;
TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
}
}
return NULL;
}
/**
* Returns true if the container is internal, such as __i3_scratch
*
*/
bool con_is_internal(Con *con) {
return (con->name[0] == '_' && con->name[1] == '_');
}
/*
* Returns true if the node is floating.
*
*/
bool con_is_floating(Con *con) {
assert(con != NULL);
DLOG("checking if con %p is floating\n", con);
return (con->floating >= FLOATING_AUTO_ON);
}
/*
* Returns true if the container is a docked container.
*
*/
bool con_is_docked(Con *con) {
if (con->parent == NULL)
return false;
if (con->parent->type == CT_DOCKAREA)
return true;
return con_is_docked(con->parent);
}
/*
* Checks if the given container is either floating or inside some floating
* container. It returns the FLOATING_CON container.
*
*/
Con *con_inside_floating(Con *con) {
assert(con != NULL);
if (con->type == CT_FLOATING_CON)
return con;
if (con->floating >= FLOATING_AUTO_ON)
return con->parent;
if (con->type == CT_WORKSPACE || con->type == CT_OUTPUT)
return NULL;
return con_inside_floating(con->parent);
}
/*
* Checks if the given container is inside a focused container.
*
*/
bool con_inside_focused(Con *con) {
if (con == focused)
return true;
if (!con->parent)
return false;
return con_inside_focused(con->parent);
}
/*
* Returns the container with the given client window ID or NULL if no such
* container exists.
*
*/
Con *con_by_window_id(xcb_window_t window) {
Con *con;
TAILQ_FOREACH(con, &all_cons, all_cons)
if (con->window != NULL && con->window->id == window)
return con;
return NULL;
}
/*
* Returns the container with the given frame ID or NULL if no such container
* exists.
*
*/
Con *con_by_frame_id(xcb_window_t frame) {
Con *con;
TAILQ_FOREACH(con, &all_cons, all_cons)
if (con->frame.id == frame)
return con;
return NULL;
}
/*
* Returns the container with the given mark or NULL if no such container
* exists.
*
*/
Con *con_by_mark(const char *mark) {
Con *con;
TAILQ_FOREACH(con, &all_cons, all_cons) {
if (con_has_mark(con, mark))
return con;
}
return NULL;
}
/*
* Returns true if and only if the given containers holds the mark.
*
*/
bool con_has_mark(Con *con, const char *mark) {
mark_t *current;
TAILQ_FOREACH(current, &(con->marks_head), marks) {
if (strcmp(current->name, mark) == 0)
return true;
}
return false;
}
/*
* Toggles the mark on a container.
* If the container already has this mark, the mark is removed.
* Otherwise, the mark is assigned to the container.
*
*/
void con_mark_toggle(Con *con, const char *mark, mark_mode_t mode) {
assert(con != NULL);
DLOG("Toggling mark \"%s\" on con = %p.\n", mark, con);
if (con_has_mark(con, mark)) {
con_unmark(con, mark);
} else {
con_mark(con, mark, mode);
}
}
/*
* Assigns a mark to the container.
*
*/
void con_mark(Con *con, const char *mark, mark_mode_t mode) {
assert(con != NULL);
DLOG("Setting mark \"%s\" on con = %p.\n", mark, con);
con_unmark(NULL, mark);
if (mode == MM_REPLACE) {
DLOG("Removing all existing marks on con = %p.\n", con);
mark_t *current;
while (!TAILQ_EMPTY(&(con->marks_head))) {
current = TAILQ_FIRST(&(con->marks_head));
con_unmark(con, current->name);
}
}
mark_t *new = scalloc(1, sizeof(mark_t));
new->name = sstrdup(mark);
TAILQ_INSERT_TAIL(&(con->marks_head), new, marks);
ipc_send_window_event("mark", con);
con->mark_changed = true;
}
/*
* Removes marks from containers.
* If con is NULL, all containers are considered.
* If name is NULL, this removes all existing marks.
* Otherwise, it will only remove the given mark (if it is present).
*
*/
void con_unmark(Con *con, const char *name) {
Con *current;
if (name == NULL) {
DLOG("Unmarking all containers.\n");
TAILQ_FOREACH(current, &all_cons, all_cons) {
if (con != NULL && current != con)
continue;
if (TAILQ_EMPTY(&(current->marks_head)))
continue;
mark_t *mark;
while (!TAILQ_EMPTY(&(current->marks_head))) {
mark = TAILQ_FIRST(&(current->marks_head));
FREE(mark->name);
TAILQ_REMOVE(&(current->marks_head), mark, marks);
FREE(mark);
ipc_send_window_event("mark", current);
}
current->mark_changed = true;
}
} else {
DLOG("Removing mark \"%s\".\n", name);
current = (con == NULL) ? con_by_mark(name) : con;
if (current == NULL) {
DLOG("No container found with this mark, so there is nothing to do.\n");
return;
}
DLOG("Found mark on con = %p. Removing it now.\n", current);
current->mark_changed = true;
mark_t *mark;
TAILQ_FOREACH(mark, &(current->marks_head), marks) {
if (strcmp(mark->name, name) != 0)
continue;
FREE(mark->name);
TAILQ_REMOVE(&(current->marks_head), mark, marks);
FREE(mark);
ipc_send_window_event("mark", current);
break;
}
}
}
/*
* Returns the first container below 'con' which wants to swallow this window
* TODO: priority
*
*/
Con *con_for_window(Con *con, i3Window *window, Match **store_match) {
Con *child;
Match *match;
//DLOG("searching con for window %p starting at con %p\n", window, con);
//DLOG("class == %s\n", window->class_class);
TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
TAILQ_FOREACH(match, &(child->swallow_head), matches) {
if (!match_matches_window(match, window))
continue;
if (store_match != NULL)
*store_match = match;
return child;
}
Con *result = con_for_window(child, window, store_match);
if (result != NULL)
return result;
}
TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
TAILQ_FOREACH(match, &(child->swallow_head), matches) {
if (!match_matches_window(match, window))
continue;
if (store_match != NULL)
*store_match = match;
return child;
}
Con *result = con_for_window(child, window, store_match);
if (result != NULL)
return result;
}
return NULL;
}
/*
* Returns the number of children of this container.
*
*/
int con_num_children(Con *con) {
Con *child;
int children = 0;
TAILQ_FOREACH(child, &(con->nodes_head), nodes)
children++;
return children;
}
/**
* Returns the number of visible non-floating children of this container.
* For example, if the container contains a hsplit which has two children,
* this will return 2 instead of 1.
*/
int con_num_visible_children(Con *con) {
if (con == NULL)
return 0;
int children = 0;
Con *current = NULL;
TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
/* Visible leaf nodes are a child. */
if (!con_is_hidden(current) && con_is_leaf(current))
children++;
/* All other containers need to be recursed. */
else
children += con_num_visible_children(current);
}
return children;
}
/*
* Count the number of windows (i.e., leaf containers).
*
*/
int con_num_windows(Con *con) {
if (con == NULL)
return 0;
if (con_has_managed_window(con))
return 1;
int num = 0;
Con *current = NULL;
TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
num += con_num_windows(current);
}
return num;
}
/*
* Updates the percent attribute of the children of the given container. This
* function needs to be called when a window is added or removed from a
* container.
*
*/
void con_fix_percent(Con *con) {
Con *child;
int children = con_num_children(con);
// calculate how much we have distributed and how many containers
// with a percentage set we have
double total = 0.0;
int children_with_percent = 0;
TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
if (child->percent > 0.0) {
total += child->percent;
++children_with_percent;
}
}
// if there were children without a percentage set, set to a value that
// will make those children proportional to all others
if (children_with_percent != children) {
TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
if (child->percent <= 0.0) {
if (children_with_percent == 0)
total += (child->percent = 1.0);
else
total += (child->percent = total / children_with_percent);
}
}
}
// if we got a zero, just distribute the space equally, otherwise
// distribute according to the proportions we got
if (total == 0.0) {
TAILQ_FOREACH(child, &(con->nodes_head), nodes)
child->percent = 1.0 / children;
} else if (total != 1.0) {
TAILQ_FOREACH(child, &(con->nodes_head), nodes)
child->percent /= total;
}
}
/*
* Toggles fullscreen mode for the given container. If there already is a
* fullscreen container on this workspace, fullscreen will be disabled and then
* enabled for the container the user wants to have in fullscreen mode.
*
*/
void con_toggle_fullscreen(Con *con, int fullscreen_mode) {
if (con->type == CT_WORKSPACE) {
DLOG("You cannot make a workspace fullscreen.\n");
return;
}
DLOG("toggling fullscreen for %p / %s\n", con, con->name);
if (con->fullscreen_mode == CF_NONE)
con_enable_fullscreen(con, fullscreen_mode);
else
con_disable_fullscreen(con);
}
/*
* Sets the specified fullscreen mode for the given container, sends the
* “fullscreen_mode” event and changes the XCB fullscreen property of the
* container’s window, if any.
*
*/
static void con_set_fullscreen_mode(Con *con, fullscreen_mode_t fullscreen_mode) {
con->fullscreen_mode = fullscreen_mode;
DLOG("mode now: %d\n", con->fullscreen_mode);
/* Send an ipc window "fullscreen_mode" event */
ipc_send_window_event("fullscreen_mode", con);
/* update _NET_WM_STATE if this container has a window */
/* TODO: when a window is assigned to a container which is already
* fullscreened, this state needs to be pushed to the client, too */
if (con->window == NULL)
return;
if (con->fullscreen_mode != CF_NONE) {
DLOG("Setting _NET_WM_STATE_FULLSCREEN for con = %p / window = %d.\n", con, con->window->id);
xcb_add_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_FULLSCREEN);
} else {
DLOG("Removing _NET_WM_STATE_FULLSCREEN for con = %p / window = %d.\n", con, con->window->id);
xcb_remove_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_FULLSCREEN);
}
}
/*
* Enables fullscreen mode for the given container, if necessary.
*
* If the container’s mode is already CF_OUTPUT or CF_GLOBAL, the container is
* kept fullscreen but its mode is set to CF_GLOBAL and CF_OUTPUT,
* respectively.
*
* Other fullscreen containers will be disabled first, if they hide the new
* one.
*
*/
void con_enable_fullscreen(Con *con, fullscreen_mode_t fullscreen_mode) {
if (con->type == CT_WORKSPACE) {
DLOG("You cannot make a workspace fullscreen.\n");
return;
}
assert(fullscreen_mode == CF_GLOBAL || fullscreen_mode == CF_OUTPUT);
if (fullscreen_mode == CF_GLOBAL)
DLOG("enabling global fullscreen for %p / %s\n", con, con->name);
else
DLOG("enabling fullscreen for %p / %s\n", con, con->name);
if (con->fullscreen_mode == fullscreen_mode) {
DLOG("fullscreen already enabled for %p / %s\n", con, con->name);
return;
}
Con *con_ws = con_get_workspace(con);
/* Disable any fullscreen container that would conflict the new one. */
Con *fullscreen = con_get_fullscreen_con(croot, CF_GLOBAL);
if (fullscreen == NULL)
fullscreen = con_get_fullscreen_con(con_ws, CF_OUTPUT);
if (fullscreen != NULL)
con_disable_fullscreen(fullscreen);
/* Set focus to new fullscreen container. Unless in global fullscreen mode
* and on another workspace restore focus afterwards.
* Switch to the container’s workspace if mode is global. */
Con *cur_ws = con_get_workspace(focused);
Con *old_focused = focused;
if (fullscreen_mode == CF_GLOBAL && cur_ws != con_ws)
workspace_show(con_ws);
con_focus(con);
if (fullscreen_mode != CF_GLOBAL && cur_ws != con_ws)
con_focus(old_focused);
con_set_fullscreen_mode(con, fullscreen_mode);
}
/*
* Disables fullscreen mode for the given container regardless of the mode, if
* necessary.
*
*/
void con_disable_fullscreen(Con *con) {
if (con->type == CT_WORKSPACE) {
DLOG("You cannot make a workspace fullscreen.\n");
return;
}
DLOG("disabling fullscreen for %p / %s\n", con, con->name);
if (con->fullscreen_mode == CF_NONE) {
DLOG("fullscreen already disabled for %p / %s\n", con, con->name);
return;
}
con_set_fullscreen_mode(con, CF_NONE);
}
static bool _con_move_to_con(Con *con, Con *target, bool behind_focused, bool fix_coordinates, bool dont_warp, bool ignore_focus) {
Con *orig_target = target;
/* Prevent moving if this would violate the fullscreen focus restrictions. */
Con *target_ws = con_get_workspace(target);
if (!con_fullscreen_permits_focusing(target_ws)) {
LOG("Cannot move out of a fullscreen container.\n");
return false;
}
if (con_is_floating(con)) {
DLOG("Container is floating, using parent instead.\n");
con = con->parent;
}
Con *source_ws = con_get_workspace(con);
if (con->type == CT_WORKSPACE) {
/* Re-parent all of the old workspace's floating windows. */
Con *child;
while (!TAILQ_EMPTY(&(source_ws->floating_head))) {
child = TAILQ_FIRST(&(source_ws->floating_head));
con_move_to_workspace(child, target_ws, true, true, false);
}
/* If there are no non-floating children, ignore the workspace. */
if (con_is_leaf(con))
return false;
con = workspace_encapsulate(con);
if (con == NULL) {
ELOG("Workspace failed to move its contents into a container!\n");
return false;
}
}
/* Save the urgency state so that we can restore it. */
bool urgent = con->urgent;
/* Save the current workspace. So we can call workspace_show() by the end
* of this function. */
Con *current_ws = con_get_workspace(focused);
Con *source_output = con_get_output(con),
*dest_output = con_get_output(target_ws);
/* 1: save the container which is going to be focused after the current
* container is moved away */
Con *focus_next = con_next_focused(con);
/* 2: we go up one level, but only when target is a normal container */
if (target->type != CT_WORKSPACE) {
DLOG("target originally = %p / %s / type %d\n", target, target->name, target->type);
target = target->parent;