-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxcontainer.c
1871 lines (1725 loc) · 45.3 KB
/
xcontainer.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
#include "shod.h"
#define DIV 15 /* see containerplace() for details */
static void unmanagedialog(struct Object *);
static void
restackdocks(void)
{
struct Object *obj;
dockstack();
TAILQ_FOREACH(obj, &wm.barq, entry)
barstack((struct Bar *)obj);
}
/* get next focused container after old on given monitor and desktop */
struct Container *
getnextfocused(struct Monitor *mon, int desk)
{
struct Container *c;
TAILQ_FOREACH(c, &wm.focusq, entry)
if (containerisvisible(c, mon, desk))
return c;
return NULL;
}
/* snap to edge */
static void
snaptoedge(int *x, int *y, int w, int h)
{
struct Container *c;
if (config.snap <= 0)
return;
if (abs(*y - wm.selmon->wy) < config.snap)
*y = wm.selmon->wy;
if (abs(*y + h - wm.selmon->wy - wm.selmon->wh) < config.snap)
*y = wm.selmon->wy + wm.selmon->wh - h;
if (abs(*x - wm.selmon->wx) < config.snap)
*x = wm.selmon->wx;
if (abs(*x + w - wm.selmon->wx - wm.selmon->ww) < config.snap)
*x = wm.selmon->wx + wm.selmon->ww - w;
TAILQ_FOREACH(c, &wm.focusq, entry) {
if (containerisvisible(c, wm.selmon, wm.selmon->seldesk)) {
if (*x + w >= c->x && *x <= c->x + c->w) {
if (abs(*y + h - c->y) < config.snap) {
*y = c->y - h;
}
if (abs(*y - c->y) < config.snap) {
*y = c->y;
}
if (abs(*y + h - c->y - c->h) < config.snap) {
*y = c->y + c->h - h;
}
if (abs(*y - c->y - c->h) < config.snap) {
*y = c->y + c->h;
}
}
if (*y + h >= c->y && *y <= c->y + c->h) {
if (abs(*x + w - c->x) < config.snap) {
*x = c->x - w;
}
if (abs(*x - c->x) < config.snap) {
*x = c->x;
}
if (abs(*x + w - c->x - c->w) < config.snap) {
*x = c->x + c->w - w;
}
if (abs(*x - c->x - c->w) < config.snap) {
*x = c->x + c->w;
}
}
}
}
}
/* increment number of clients */
static void
clientsincr(void)
{
wm.nclients++;
}
/* decrement number of clients */
static void
clientsdecr(void)
{
wm.nclients--;
}
/* get decoration style (and state) of container */
static int
containergetstyle(struct Container *c)
{
return (c == wm.focused) ? FOCUSED : UNFOCUSED;
}
/* get tab decoration style */
static int
tabgetstyle(struct Tab *t)
{
if (t == NULL)
return UNFOCUSED;
if (t->isurgent)
return URGENT;
if (t->row->col->c == wm.focused)
return FOCUSED;
return UNFOCUSED;
}
/* clear window urgency */
static void
tabclearurgency(struct Tab *tab)
{
XWMHints wmh = {0};
XSetWMHints(dpy, tab->obj.win, &wmh);
tab->isurgent = 0;
}
/* commit tab size and position */
static void
tabmoveresize(struct Tab *t)
{
XMoveResizeWindow(dpy, t->title, t->x, 0, t->w, config.titlewidth);
if (t->ptw != t->w) {
tabdecorate(t, 0);
}
winnotify(t->obj.win, t->row->col->c->x + t->row->col->x, t->row->col->c->y + t->row->y + config.titlewidth, t->winw, t->winh);
}
/* commit titlebar size and position */
static void
titlebarmoveresize(struct Row *row, int x, int y, int w)
{
XMoveResizeWindow(dpy, row->bar, x, y, w, config.titlewidth);
XMoveWindow(dpy, row->bl, 0, 0);
XMoveWindow(dpy, row->br, w - config.titlewidth, 0);
}
/* calculate size of dialogs of a tab */
static void
dialogcalcsize(struct Dialog *dial)
{
struct Tab *tab;
tab = dial->tab;
dial->w = max(1, min(dial->maxw, tab->winw - 2 * config.borderwidth));
dial->h = max(1, min(dial->maxh, tab->winh - 2 * config.borderwidth));
dial->x = tab->winw / 2 - dial->w / 2;
dial->y = tab->winh / 2 - dial->h / 2;
}
static struct Dialog *
dialognew(Window win, int maxw, int maxh)
{
struct Dialog *dial;
dial = emalloc(sizeof(*dial));
*dial = (struct Dialog){
.pix = None,
.maxw = maxw,
.maxh = maxh,
.obj.win = win,
.obj.class = dialog_class,
};
dial->frame = createframe((XRectangle){0, 0, maxw, maxh});
XReparentWindow(dpy, dial->obj.win, dial->frame, 0, 0);
XMapWindow(dpy, dial->obj.win);
return dial;
}
/* calculate position and width of tabs of a row */
static void
rowcalctabs(struct Row *row)
{
struct Object *p, *q;
struct Dialog *d;
struct Tab *t;
int i, x;
if (TAILQ_EMPTY(&row->tabq))
return;
x = config.titlewidth;
i = 0;
TAILQ_FOREACH(p, &row->tabq, entry) {
t = (struct Tab *)p;
t->winh = max(1, row->h - config.titlewidth);
t->winw = row->col->w;
t->w = max(1, ((i + 1) * (t->winw - 2 * config.titlewidth) / row->ntabs) - (i * (t->winw - 2 * config.titlewidth) / row->ntabs));
t->x = x;
x += t->w;
TAILQ_FOREACH(q, &t->dialq, entry) {
d = (struct Dialog *)q;
dialogcalcsize(d);
}
i++;
}
}
/* calculate position and height of rows of a column */
static void
colcalcrows(struct Column *col, int recalcfact)
{
struct Container *c;
struct Row *row;
int i, y, h, sumh;
int content;
int recalc;
c = col->c;
if (TAILQ_EMPTY(&col->rowq))
return;
if (col->c->state & FULLSCREEN) {
TAILQ_FOREACH(row, &col->rowq, entry) {
row->y = -config.titlewidth;
row->h = col->c->h + config.titlewidth;
rowcalctabs(row);
}
return;
}
/* check if rows sum up the height of the container */
content = columncontentheight(col);
sumh = 0;
recalc = 0;
TAILQ_FOREACH(row, &col->rowq, entry) {
if (!recalcfact) {
if (TAILQ_NEXT(row, entry) == NULL) {
row->h = content - sumh + config.titlewidth;
} else {
row->h = row->fact * content + config.titlewidth;
}
if (row->h < config.titlewidth) {
recalc = 1;
}
}
sumh += row->h - config.titlewidth;
}
if (sumh != content)
recalc = 1;
h = col->c->h - 2 * c->b - (col->nrows - 1) * config.divwidth;
y = c->b;
i = 0;
TAILQ_FOREACH(row, &col->rowq, entry) {
if (recalc)
row->h = max(config.titlewidth, ((i + 1) * h / col->nrows) - (i * h / col->nrows));
if (recalc || recalcfact)
row->fact = (double)(row->h - config.titlewidth) / (double)(content);
row->y = y;
y += row->h + config.divwidth;
rowcalctabs(row);
i++;
}
}
static struct Tab *
tabnew(Window win, Window leader)
{
struct Tab *tab;
tab = emalloc(sizeof(*tab));
*tab = (struct Tab){
.pix = None,
.pixtitle = None,
.title = None,
.leader = leader,
.obj.win = win,
.obj.class = tab_class,
};
TAILQ_INIT(&tab->dialq);
tab->frame = createframe((XRectangle){0, 0, 1, 1});
tab->title = createdecoration(
root, (XRectangle){0, 0, 1, 1},
None, NorthWestGravity
);
tab->pid = getcardprop(tab->obj.win, atoms[_NET_WM_PID]);
XReparentWindow(dpy, tab->obj.win, tab->frame, 0, 0);
mapwin(tab->obj.win);
clientsincr();
return tab;
}
/* remove tab from row's tab queue */
static void
tabremove(struct Row *row, struct Tab *tab)
{
if (row->seltab == tab) {
row->seltab = (struct Tab *)TAILQ_PREV((struct Object *)tab, Queue, entry);
if (row->seltab == NULL) {
row->seltab = (struct Tab *)TAILQ_NEXT((struct Object *)tab, entry);
}
}
row->ntabs--;
TAILQ_REMOVE(&row->tabq, (struct Object *)tab, entry);
tab->row = NULL;
}
/* delete tab */
static void
tabdel(struct Tab *tab)
{
struct Dialog *dial;
while ((dial = (struct Dialog *)TAILQ_FIRST(&tab->dialq)) != NULL) {
XDestroyWindow(dpy, dial->obj.win);
unmanagedialog((struct Object *)dial);
}
tabremove(tab->row, tab);
if (tab->pixtitle != None)
XFreePixmap(dpy, tab->pixtitle);
if (tab->pix != None)
XFreePixmap(dpy, tab->pix);
icccmdeletestate(tab->obj.win);
XReparentWindow(dpy, tab->obj.win, root, 0, 0);
XDestroyWindow(dpy, tab->title);
XDestroyWindow(dpy, tab->frame);
clientsdecr();
free(tab->name);
free(tab);
}
/* create new row */
struct Row *
rownew(void)
{
struct Row *row;
row = emalloc(sizeof(*row));
*row = (struct Row){
.isunmapped = 0,
};
TAILQ_INIT(&row->tabq);
row->frame = createframe((XRectangle){0, 0, 1, 1});
row->bar = createdecoration(
root,
(XRectangle){0, 0, config.titlewidth, config.titlewidth},
None, NorthGravity
);
row->bl = createdecoration(
row->bar,
(XRectangle){0, 0, config.titlewidth, config.titlewidth},
wm.cursors[CURSOR_HAND], WestGravity
);
row->br = createdecoration(
row->bar,
(XRectangle){0, 0, config.titlewidth, config.titlewidth},
wm.cursors[CURSOR_PIRATE], EastGravity
);
row->div = createdecoration(
root,
(XRectangle){0, 0, 1, 1},
wm.cursors[CURSOR_V], WestGravity
);
XMapWindow(dpy, row->bl);
XMapWindow(dpy, row->br);
return row;
}
/* detach row from column */
static void
rowdetach(struct Row *row, int recalc)
{
struct Column *col;
col = row->col;
if (col->selrow == row) {
col->selrow = TAILQ_PREV(row, RowQueue, entry);
if (col->selrow == NULL) {
col->selrow = TAILQ_NEXT(row, entry);
}
}
col->nrows--;
TAILQ_REMOVE(&col->rowq, row, entry);
if (recalc) {
colcalcrows(row->col, 1);
}
}
/* delete row */
static void
rowdel(struct Row *row)
{
struct Tab *tab;
while ((tab = (struct Tab *)TAILQ_FIRST(&row->tabq)) != NULL)
tabdel(tab);
rowdetach(row, 1);
XDestroyWindow(dpy, row->frame);
XDestroyWindow(dpy, row->bar);
XDestroyWindow(dpy, row->bl);
XDestroyWindow(dpy, row->br);
XDestroyWindow(dpy, row->div);
free(row);
}
/* create new column */
static struct Column *
colnew(void)
{
struct Column *col;
col = emalloc(sizeof(*col));
*col = (struct Column){ 0 };
TAILQ_INIT(&col->rowq);
col->div = createdecoration(
root,
(XRectangle){0, 0, 1, 1},
wm.cursors[CURSOR_H], NorthGravity
);
return col;
}
/* detach column from container */
static void
coldetach(struct Column *col)
{
struct Container *c;
c = col->c;
if (c->selcol == col) {
c->selcol = TAILQ_PREV(col, ColumnQueue, entry);
if (c->selcol == NULL) {
c->selcol = TAILQ_NEXT(col, entry);
}
}
c->ncols--;
TAILQ_REMOVE(&c->colq, col, entry);
containercalccols(col->c);
}
/* delete column */
static void
coldel(struct Column *col)
{
struct Row *row;
while ((row = TAILQ_FIRST(&col->rowq)) != NULL)
rowdel(row);
coldetach(col);
XDestroyWindow(dpy, col->div);
free(col);
}
/* add row to column */
static void
coladdrow(struct Column *col, struct Row *row, struct Row *prev)
{
struct Container *c;
struct Column *oldcol;
c = col->c;
oldcol = row->col;
row->col = col;
col->selrow = row;
col->nrows++;
if (prev == NULL || TAILQ_EMPTY(&col->rowq))
TAILQ_INSERT_HEAD(&col->rowq, row, entry);
else
TAILQ_INSERT_AFTER(&col->rowq, prev, row, entry);
colcalcrows(col, 1); /* set row->y, row->h, etc */
XReparentWindow(dpy, row->div, c->frame, col->x + col->w, c->b);
XReparentWindow(dpy, row->bar, c->frame, col->x, row->y);
XReparentWindow(dpy, row->frame, c->frame, col->x, row->y);
XMapWindow(dpy, row->bar);
XMapWindow(dpy, row->frame);
if (oldcol != NULL && oldcol->nrows == 0) {
coldel(oldcol);
}
}
/* add tab to row */
static void
rowaddtab(struct Row *row, struct Tab *tab, struct Tab *prev)
{
struct Row *oldrow;
oldrow = tab->row;
tab->row = row;
row->seltab = tab;
row->ntabs++;
if (prev == NULL || TAILQ_EMPTY(&row->tabq))
TAILQ_INSERT_HEAD(&row->tabq, (struct Object *)tab, entry);
else
TAILQ_INSERT_AFTER(&row->tabq, (struct Object *)prev, (struct Object *)tab, entry);
rowcalctabs(row); /* set tab->x, tab->w, etc */
XReparentWindow(dpy, tab->title, row->bar, tab->x, 0);
XReparentWindow(dpy, tab->frame, row->frame, 0, 0);
XMapWindow(dpy, tab->frame);
XMapWindow(dpy, tab->title);
if (oldrow != NULL) { /* deal with the row this tab came from */
if (oldrow->ntabs == 0) {
rowdel(oldrow);
} else {
rowcalctabs(oldrow);
}
}
}
/* decorate dialog window */
static void
dialogdecorate(struct Dialog *d)
{
int fullw, fullh; /* size of dialog window + borders */
fullw = d->w + 2 * config.borderwidth;
fullh = d->h + 2 * config.borderwidth;
updatepixmap(&d->pix, &d->pw, &d->ph, fullw, fullh);
drawborders(d->pix, fullw, fullh, tabgetstyle(d->tab));
drawcommit(d->pix, d->frame);
}
/* add container into head of focus queue */
static void
containerinsertfocus(struct Container *c)
{
TAILQ_INSERT_HEAD(&wm.focusq, c, entry);
}
/* add container into head of focus queue */
static void
containerinsertraise(struct Container *c)
{
TAILQ_INSERT_HEAD(&wm.stackq, c, raiseentry);
}
/* remove container from the focus list */
static void
containerdelfocus(struct Container *c)
{
TAILQ_REMOVE(&wm.focusq, c, entry);
}
/* put container on beginning of focus list */
static void
containeraddfocus(struct Container *c)
{
if (c == NULL || c->state & MINIMIZED)
return;
containerdelfocus(c);
containerinsertfocus(c);
}
/* remove container from the raise list */
static void
containerdelraise(struct Container *c)
{
TAILQ_REMOVE(&wm.stackq, c, raiseentry);
}
/* hide container */
void
containerhide(struct Container *c, int hide)
{
struct Object *t, *d;
if (c == NULL)
return;
c->ishidden = hide;
if (hide) {
XUnmapWindow(dpy, c->frame);
} else {
XMapWindow(dpy, c->frame);
}
TAB_FOREACH_BEGIN(c, t) {
icccmwmstate(t->win, (hide ? IconicState : NormalState));
TAILQ_FOREACH(d, &((struct Tab *)t)->dialq, entry) {
icccmwmstate(d->win, (hide ? IconicState : NormalState));
}
}TAB_FOREACH_END
}
/* add column to container */
static void
containeraddcol(struct Container *c, struct Column *col, struct Column *prev)
{
struct Container *oldc;
oldc = col->c;
col->c = c;
c->selcol = col;
c->ncols++;
if (prev == NULL || TAILQ_EMPTY(&c->colq))
TAILQ_INSERT_HEAD(&c->colq, col, entry);
else
TAILQ_INSERT_AFTER(&c->colq, prev, col, entry);
XReparentWindow(dpy, col->div, c->frame, 0, 0);
containercalccols(c);
if (oldc != NULL && oldc->ncols == 0) {
containerdel(oldc);
}
}
/* send container to desktop and raise it; return nonzero if it was actually sent anywhere */
static int
containersendtodesk(struct Container *c, struct Monitor *mon, unsigned long desk)
{
void containerstick(struct Container *c, int stick);
if (c == NULL || c->state & MINIMIZED)
return 0;
if (desk == 0xFFFFFFFF) {
containerstick(c, ADD);
} else if ((int)desk < config.ndesktops) {
c->desk = (int)desk;
if (c->mon != mon)
containerplace(c, mon, desk, 1);
c->mon = mon;
c->state &= ~STICKY;
if ((int)desk != mon->seldesk) /* container was sent to invisible desktop */
containerhide(c, 1);
else
containerhide(c, 0);
containerraise(c, c->state);
} else {
return 0;
}
wm.setclientlist = True;
ewmhsetwmdesktop(c);
ewmhsetstate(c);
return 1;
}
/* make a container occupy the whole monitor */
static void
containerfullscreen(struct Container *c, int fullscreen)
{
if (fullscreen == REMOVE && !(c->state & FULLSCREEN))
return; /* already unset */
if (fullscreen == ADD && (c->state & FULLSCREEN))
return; /* already set */
c->state ^= FULLSCREEN;
containercalccols(c);
containermoveresize(c, 1);
containerdecorate(c);
ewmhsetstate(c);
if (wm.focused == c) {
restackdocks();
}
}
/* maximize a container on the monitor */
static void
containermaximize(struct Container *c, int maximize)
{
if (maximize == REMOVE && !(c->state & MAXIMIZED))
return; /* already unset */
if (maximize == ADD && (c->state & MAXIMIZED))
return; /* already set */
c->state ^= MAXIMIZED;
containercalccols(c);
containermoveresize(c, 1);
containerdecorate(c);
}
/* minimize container; optionally focus another container */
static void
containerminimize(struct Container *c, int minimize, int focus)
{
struct Container *tofocus;
if (minimize != REMOVE && !(c->state & MINIMIZED)) {
c->state |= MINIMIZED;
containerhide(c, 1);
if (focus) {
if ((tofocus = getnextfocused(c->mon, c->desk)) != NULL) {
tabfocus(tofocus->selcol->selrow->seltab, 0);
containerraise(c, c->state);
} else {
tabfocus(NULL, 0);
}
}
} else if (minimize != ADD && (c->state & MINIMIZED)) {
(void)containersendtodesk(c, wm.selmon, wm.selmon->seldesk);
containermoveresize(c, 1);
tabfocus(c->selcol->selrow->seltab, 0);
containerraise(c, c->state);
}
}
/* shade container title bar */
static void
containershade(struct Container *c, int shade)
{
if (shade != REMOVE && !(c->state & SHADED)) {
XDefineCursor(dpy, c->curswin[BORDER_NW], wm.cursors[CURSOR_W]);
XDefineCursor(dpy, c->curswin[BORDER_SW], wm.cursors[CURSOR_W]);
XDefineCursor(dpy, c->curswin[BORDER_NE], wm.cursors[CURSOR_E]);
XDefineCursor(dpy, c->curswin[BORDER_SE], wm.cursors[CURSOR_E]);
} else if (shade != ADD && (c->state & SHADED)) {
XDefineCursor(dpy, c->curswin[BORDER_NW], wm.cursors[CURSOR_NW]);
XDefineCursor(dpy, c->curswin[BORDER_SW], wm.cursors[CURSOR_SW]);
XDefineCursor(dpy, c->curswin[BORDER_NE], wm.cursors[CURSOR_NE]);
XDefineCursor(dpy, c->curswin[BORDER_SE], wm.cursors[CURSOR_SE]);
} else {
return;
}
c->state ^= SHADED;
containercalccols(c);
containermoveresize(c, 1);
containerdecorate(c);
if (c == wm.focused) {
tabfocus(c->selcol->selrow->seltab, 0);
}
}
/* stick a container on the monitor */
void
containerstick(struct Container *c, int stick)
{
if (stick != REMOVE && !(c->state & STICKY)) {
c->state |= STICKY;
ewmhsetwmdesktop(c);
} else if (stick != ADD && (c->state & STICKY)) {
c->state &= ~STICKY;
(void)containersendtodesk(c, c->mon, c->mon->seldesk);
}
}
/* raise container above others */
static void
containerabove(struct Container *c, int above)
{
enum State state;
state = c->state & ~(ABOVE|BELOW);
if (above != REMOVE && !(c->state & ABOVE))
containerraise(c, state | ABOVE);
else if (above != ADD && (c->state & ABOVE))
containerraise(c, state);
}
/* lower container below others */
static void
containerbelow(struct Container *c, int below)
{
enum State state;
state = c->state & ~(ABOVE|BELOW);
if (below != REMOVE && !(c->state & BELOW))
containerraise(c, state | BELOW);
else if (below != ADD && (c->state & BELOW))
containerraise(c, state);
}
/* create new container */
struct Container *
containernew(int x, int y, int w, int h, enum State state)
{
struct Container *c;
int corner = config.corner + config.shadowthickness;
struct { int window, cursor, gravity, x, y; } table[] = {
{ BORDER_N, CURSOR_N, NorthWestGravity, 0, 0, },
{ BORDER_W, CURSOR_W, NorthWestGravity, 0, 0, },
{ BORDER_S, CURSOR_S, SouthWestGravity, 0, config.borderwidth, },
{ BORDER_E, CURSOR_E, NorthEastGravity, config.borderwidth, 0, },
{ BORDER_NW, CURSOR_NW, NorthWestGravity, 0, 0, },
{ BORDER_SW, CURSOR_SW, SouthWestGravity, 0, corner, },
{ BORDER_NE, CURSOR_NE, NorthEastGravity, corner, 0, },
{ BORDER_SE, CURSOR_SE, SouthEastGravity, corner, corner, },
};
x -= config.borderwidth,
y -= config.borderwidth,
w += 2 * config.borderwidth,
h += 2 * config.borderwidth + config.titlewidth,
c = emalloc(sizeof *c);
*c = (struct Container) {
.x = x, .y = y, .w = w, .h = h,
.nx = x, .ny = y, .nw = w, .nh = h,
.b = config.borderwidth,
.state = state,
.ishidden = 0,
.isobscured = 0,
};
TAILQ_INIT(&c->colq);
c->frame = createframe((XRectangle){c->x, c->y, c->w, c->h});
for (size_t i = 0; i < LEN(table); i++) {
int x = table[i].x != 0 ? c->w - table[i].x : 0;
int y = table[i].y != 0 ? c->h - table[i].y : 0;
c->curswin[table[i].window] = createdecoration(
c->frame,
(XRectangle){
/*
* Corners have fixed size, set it now.
* Edges are resized at will.
*/
x, y, corner, corner
},
wm.cursors[table[i].cursor], table[i].gravity
);
XMapRaised(dpy, c->curswin[table[i].window]);
}
containerinsertfocus(c);
containerinsertraise(c);
return c;
}
/* delete container */
void
containerdel(struct Container *c)
{
struct Column *col;
int i;
containerdelfocus(c);
containerdelraise(c);
if (wm.focused == c)
wm.focused = NULL;
TAILQ_REMOVE(&wm.focusq, c, entry);
while ((col = TAILQ_FIRST(&c->colq)) != NULL)
coldel(col);
XDestroyWindow(dpy, c->frame);
for (i = 0; i < BORDER_LAST; i++)
XDestroyWindow(dpy, c->curswin[i]);
free(c);
}
/* commit container size and position */
void
containermoveresize(struct Container *c, int checkstack)
{
struct Object *t, *d;
struct Column *col;
struct Row *row;
struct Tab *tab;
struct Dialog *dial;
int rowy;
int isshaded;
if (c == NULL)
return;
XMoveResizeWindow(dpy, c->frame, c->x, c->y, c->w, c->h);
XResizeWindow(dpy, c->curswin[BORDER_N], c->w, config.borderwidth);
XResizeWindow(dpy, c->curswin[BORDER_S], c->w, config.borderwidth);
XResizeWindow(dpy, c->curswin[BORDER_W], config.borderwidth, c->h);
XResizeWindow(dpy, c->curswin[BORDER_E], config.borderwidth, c->h);
isshaded = containerisshaded(c);
TAILQ_FOREACH(col, &c->colq, entry) {
rowy = c->b;
if (TAILQ_NEXT(col, entry) != NULL) {
XMoveResizeWindow(dpy, col->div, col->x + col->w, c->b, config.divwidth, c->h - 2 * c->b);
XMapWindow(dpy, col->div);
} else {
XUnmapWindow(dpy, col->div);
}
TAILQ_FOREACH(row, &col->rowq, entry) {
if (!isshaded) {
if (TAILQ_NEXT(row, entry) != NULL) {
XMoveResizeWindow(dpy, row->div, col->x, row->y + row->h, col->w, config.divwidth);
XMapWindow(dpy, row->div);
} else {
XUnmapWindow(dpy, row->div);
}
titlebarmoveresize(row, col->x, row->y, col->w);
if (row->h - config.titlewidth > 0) {
XMoveResizeWindow(dpy, row->frame, col->x, row->y + config.titlewidth, col->w, row->h - config.titlewidth);
XMapWindow(dpy, row->frame);
row->isunmapped = 0;
} else {
XUnmapWindow(dpy, row->frame);
row->isunmapped = 1;
}
} else {
titlebarmoveresize(row, col->x, rowy, col->w);
XUnmapWindow(dpy, row->frame);
XUnmapWindow(dpy, row->div);
row->isunmapped = 1;
}
rowy += config.titlewidth;
TAILQ_FOREACH(t, &row->tabq, entry) {
tab = (struct Tab *)t;
XMoveResizeWindow(dpy, tab->frame, 0, 0, tab->winw, tab->winh);
TAILQ_FOREACH(d, &tab->dialq, entry) {
dial = (struct Dialog *)d;
dialogmoveresize(dial);
ewmhsetframeextents(dial->obj.win, c->b, 0);
}
XResizeWindow(dpy, tab->obj.win, tab->winw, tab->winh);
ewmhsetframeextents(tab->obj.win, config.borderwidth, config.titlewidth);
tabmoveresize(tab);
}
}
}
if (!config.disablehidden && checkstack) {
wm.setclientlist = True;
}
}
void
containerdecorate(struct Container *c)
{
struct Object *t, *d;
struct Column *col;
struct Row *row;
int style;
if (c == NULL)
return;
style = containergetstyle(c);
backgroundcommit(c->frame, style);
drawcommit(wm.decorations[style].bar_horz, c->curswin[BORDER_N]);
drawcommit(wm.decorations[style].bar_horz, c->curswin[BORDER_S]);
drawcommit(wm.decorations[style].bar_vert, c->curswin[BORDER_W]);
drawcommit(wm.decorations[style].bar_vert, c->curswin[BORDER_E]);
drawcommit(wm.decorations[style].corner_nw, c->curswin[BORDER_NW]);
drawcommit(wm.decorations[style].corner_ne, c->curswin[BORDER_NE]);
drawcommit(wm.decorations[style].corner_sw, c->curswin[BORDER_SW]);
drawcommit(wm.decorations[style].corner_se, c->curswin[BORDER_SE]);
TAILQ_FOREACH(col, &c->colq, entry) {
drawcommit(wm.decorations[style].bar_vert, col->div);
TAILQ_FOREACH(row, &col->rowq, entry) {
drawcommit(wm.decorations[style].bar_horz, row->div);
drawcommit(wm.decorations[style].btn_left, row->bl);
drawcommit(wm.decorations[style].btn_right, row->br);
backgroundcommit(row->bar, style);
TAILQ_FOREACH(t, &row->tabq, entry) {
tabdecorate((struct Tab *)t, 0);
TAILQ_FOREACH(d, &((struct Tab *)t)->dialq, entry) {
dialogdecorate((struct Dialog *)d);
}
}
}
}
}
/* calculate position and width of columns of a container */
void
containercalccols(struct Container *c)
{
struct Column *col;
int i, x, w;
int sumw;
int content;
int recalc;
if (c->state & FULLSCREEN) {
c->x = c->mon->mx;
c->y = c->mon->my;
c->w = c->mon->mw;
c->h = c->mon->mh;
c->b = 0;
TAILQ_FOREACH(col, &c->colq, entry) {
col->x = 0;
col->w = c->w;
colcalcrows(col, 0);
}
return;
} else if (c->state & MAXIMIZED) {
c->x = c->mon->wx;
c->y = c->mon->wy;
c->w = c->mon->ww;
c->h = c->mon->wh;
c->b = config.borderwidth;
} else {
c->x = c->nx;
c->y = c->ny;
c->w = c->nw;
c->h = c->nh;
c->b = config.borderwidth;
}
if (containerisshaded(c)) {
c->h = 0;
}
/* check if columns sum up the width of the container */
content = containercontentwidth(c);
sumw = 0;
recalc = 0;
TAILQ_FOREACH(col, &c->colq, entry) {
if (TAILQ_NEXT(col, entry) == NULL) {
col->w = content - sumw;
} else {
col->w = col->fact * content;
}
if (col->w == 0) {
recalc = 1;
}
sumw += col->w;
}
if (sumw != content)
recalc = 1;
w = c->w - 2 * c->b - (c->ncols - 1) * config.divwidth;
x = c->b;
i = 0;
TAILQ_FOREACH(col, &c->colq, entry) {
if (containerisshaded(c))
c->h = max(c->h, col->nrows * config.titlewidth);
if (recalc)
col->w = max(1, ((i + 1) * w / c->ncols) - (i * w / c->ncols));
if (recalc)
col->fact = (double)col->w/(double)c->w;
col->x = x;
x += col->w + config.divwidth;