-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathclient.c
2264 lines (2035 loc) · 77.7 KB
/
client.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
/* GoomwWM, Get out of my way, Window Manager!
MIT/X11 License
Copyright (c) 2012 Sean Pringle <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// manipulate client _NET_WM_STATE_*
void client_flush_state(client *c)
{
window_set_atom_prop(c->window, netatoms[_NET_WM_STATE], c->state, c->states);
}
int client_has_state(client *c, Atom state)
{
int i; for (i = 0; i < c->states; i++) if (c->state[i] == state) return 1;
return 0;
}
void client_add_state(client *c, Atom state)
{
if (c->states < CLIENTSTATE && !client_has_state(c, state))
{
c->state[c->states++] = state;
client_flush_state(c);
}
}
void client_remove_state(client *c, Atom state)
{
if (!client_has_state(c, state)) return;
Atom newstate[CLIENTSTATE]; int i, n;
for (i = 0, n = 0; i < c->states; i++) if (c->state[i] != state) newstate[n++] = c->state[i];
memmove(c->state, newstate, sizeof(Atom)*n); c->states = n;
client_flush_state(c);
}
void client_remove_all_states(client *c)
{
memset(&c->state, 0, sizeof(Atom)*CLIENTSTATE);
c->states = 0; client_flush_state(c);
}
void client_set_state(client *c, Atom state, int on)
{
if (on) client_add_state(c, state); else client_remove_state(c, state);
}
// extend client data
void client_descriptive_data(client *c)
{
if (!c || c->is_described) return;
char *name;
if ((name = window_get_text_prop(c->window, netatoms[_NET_WM_NAME])) && name)
c->title = name;
else
if (XFetchName(display, c->window, &name))
{
c->title = strdup(name);
XFree(name);
}
XClassHint chint;
if (XGetClassHint(display, c->window, &chint))
{
c->class = strdup(chint.res_class);
c->name = strdup(chint.res_name);
XFree(chint.res_class); XFree(chint.res_name);
}
c->is_described = 1;
}
// extend client data
// necessary for anything that is going to move/resize/stack, but expensive to do
// every time in client_create()
void client_extended_data(client *c)
{
if (!c || c->is_extended) return;
long sr; XGetWMNormalHints(display, c->window, &c->xsize, &sr);
monitor_dimensions_struts(c->x+c->w/2, c->y+c->h/2, &c->monitor);
int screen_x = c->monitor.x, screen_y = c->monitor.y;
int screen_width = c->monitor.w, screen_height = c->monitor.h;
int vague = MAX(screen_width/100, screen_height/100);
// window co-ords translated to 0-based on screen
// co-ords are x,y upper left outsize border, w,h inside border
int x = c->xattr.x - screen_x - c->border_width;
int y = c->xattr.y - screen_y - c->border_width - c->titlebar_height;
int w = c->xattr.width + c->border_width*2;
int h = c->xattr.height + c->border_width*2 + c->titlebar_height;
c->x = screen_x + x;
c->y = screen_y + y;
c->w = w; c->h = h;
// gather info on the current window position, so we can try and resize and move nicely
c->is_full = (x < 1 && y < 1 && w >= screen_width && h >= screen_height) ? 1:0;
c->is_left = c->is_full || NEAR(0, vague, x);
c->is_top = c->is_full || NEAR(0, vague, y);
c->is_right = c->is_full || NEAR(screen_width, vague, x+w);
c->is_bottom = c->is_full || NEAR(screen_height, vague, y+h);
c->is_xcenter = c->is_full || NEAR((screen_width-w)/2, vague, x) ? 1:0;
c->is_ycenter = c->is_full || NEAR((screen_height-h)/2, vague, y) ? 1:0;
c->is_maxh = c->is_full || (c->is_left && w >= screen_width-2);
c->is_maxv = c->is_full || (c->is_top && h >= screen_height-2);
c->is_extended = 1;
}
// true if a client window matches a rule pattern
int client_rule_match(client *c, winrule *r)
{
if (c->trans
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_DESKTOP]
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_DOCK]
// EWMH seems to often equate dialogs with transient_for, and we already ignore transients...
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_DIALOG]
// following should never be children of the root window anyway? hence never managed or ruled. ignore anyway...
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_DROPDOWN_MENU]
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_POPUP_MENU]
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_TOOLTIP]
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_NOTIFICATION]
|| c->type == netatoms[_NET_WM_WINDOW_TYPE_COMBO]
) return 0;
// _NET_WM_WINDOW_TYPE_SPLASH can be annoying, so we do let them be ruled
// _NET_WM_WINDOW_TYPE_UTILITY and TOOLBAR are both persistent and may be managed and ruled
client_descriptive_data(c);
if (strchr(r->pattern, ':') && strchr("cnte", r->pattern[0]))
{
if (r->pattern[0] == 'c') return regexec(&r->re, c->class, 0, NULL, 0) ?0:1;
if (r->pattern[0] == 'n') return regexec(&r->re, c->name, 0, NULL, 0) ?0:1;
if (r->pattern[0] == 't') return regexec(&r->re, c->title, 0, NULL, 0) ?0:1;
// check if window on edge:(top|left|bottom|right)
if (r->pattern[0] == 'e')
{
client_extended_data(c);
char *p = strchr(r->pattern, ':')+1;
if (!strcasecmp(p, "top") && c->is_top) return 1;
if (!strcasecmp(p, "bottom") && c->is_bottom) return 1;
if (!strcasecmp(p, "left") && c->is_left) return 1;
if (!strcasecmp(p, "right") && c->is_right) return 1;
}
return 0;
}
return (
regexec(&r->re, c->class, 0, NULL, 0) == 0 ||
regexec(&r->re, c->name, 0, NULL, 0) == 0 ||
regexec(&r->re, c->title, 0, NULL, 0) == 0) ?1:0;
}
// find a client's rule, optionally filtered by flags
winrule* client_rule(client *c, bitmap flags)
{
if (!c->is_ruled)
{
c->rule = config_rules; while (c->rule && !client_rule_match(c, c->rule)) c->rule = c->rule->next;
c->is_ruled = 1;
}
return (!c->rule || (flags && !(flags & c->rule->flags))) ? NULL: c->rule;
}
// collect info on any window
// doesn't have to be a window we'll end up managing
client* client_create(Window win)
{
if (win == None) return NULL;
int idx = winlist_find(cache_client, win);
if (idx >= 0) return cache_client->data[idx];
// if this fails, we're up that creek
XWindowAttributes *attr = window_get_attributes(win);
if (!attr) return NULL;
client *c = allocate_clear(sizeof(client));
c->window = win; c->title = c->name = c->class = empty;
// copy xattr so we don't have to care when stuff is freed
memmove(&c->xattr, attr, sizeof(XWindowAttributes));
XGetTransientForHint(display, win, &c->trans);
// find last known state
wincache *cache = NULL;
idx = winlist_find(windows, c->window);
if (idx < 0)
{
cache = allocate_clear(sizeof(wincache));
winlist_append(windows, c->window, cache);
idx = windows->len-1;
}
// the cache is not tightly linked to the window at all
// if it's populated, it gets used to make behaviour appear logically
// if it's empty, nothing cares that much, or it gets initialized
c->cache = windows->data[idx];
c->visible = c->xattr.map_state == IsViewable ?1:0;
c->states = window_get_atom_prop(win, netatoms[_NET_WM_STATE], c->state, CLIENTSTATE);
window_get_atom_prop(win, netatoms[_NET_WM_WINDOW_TYPE], &c->type, 1);
if (c->type == None) c->type = (c->trans != None)
// trasients default to dialog
? netatoms[_NET_WM_WINDOW_TYPE_DIALOG]
// non-transients default to normal
: netatoms[_NET_WM_WINDOW_TYPE_NORMAL];
c->manage = c->xattr.override_redirect == False && !c->cache->is_ours
&& c->type != netatoms[_NET_WM_WINDOW_TYPE_DESKTOP]
&& c->type != netatoms[_NET_WM_WINDOW_TYPE_NOTIFICATION]
&& c->type != netatoms[_NET_WM_WINDOW_TYPE_DOCK]
&& c->type != netatoms[_NET_WM_WINDOW_TYPE_SPLASH]
?1:0;
c->active = c->manage && c->visible && window_is_active(c->window) ?1:0;
c->minimized = winlist_find(windows_minimized, c->window) >= 0 ? 1:0;
c->shaded = winlist_find(windows_shaded, c->window) >= 0 ? 1:0;
c->urgent = c->manage && client_has_state(c, netatoms[_NET_WM_STATE_DEMANDS_ATTENTION]) ? 1:0;
// extra checks for managed windows
if (c->manage && client_rule(c, RULE_IGNORE)) c->manage = 0;
// focus seems a really dodgy way to determine the "active" window, but in some
// cases checking both ->active and ->focus is necessary to bahave logically
Window focus; int rev;
XGetInputFocus(display, &focus, &rev);
c->focus = focus == win ? 1:0;
XWMHints *hints = XGetWMHints(display, win);
if (hints)
{
c->input = hints->flags & InputHint && hints->input ? 1: 0;
c->initial_state = hints->flags & StateHint ? hints->initial_state: NormalState;
c->urgent = c->urgent || hints->flags & XUrgencyHint ? 1: 0;
XFree(hints);
}
c->decorate = c->manage;
// can't get away with ignoring old motif stuff, as some apps use it
Atom motif_type; int motif_items; motif_hints mhints;
if (window_get_prop(c->window, atoms[_MOTIF_WM_HINTS], &motif_type, &motif_items, &mhints, sizeof(mhints)) && motif_items)
if (mhints.flags & 2 && mhints.decorations == 0) c->decorate = 0;
// co-ords include borders
c->x = c->xattr.x; c->y = c->xattr.y; c->w = c->xattr.width; c->h = c->xattr.height;
c->border_width = c->decorate && !client_has_state(c, netatoms[_NET_WM_STATE_FULLSCREEN]) ? config_border_width: 0;
c->titlebar_height = c->decorate && !client_has_state(c, netatoms[_NET_WM_STATE_FULLSCREEN]) ? config_titlebar_height: 0;
// compenstate for borders on non-fullscreen windows
if (c->decorate)
{
c->x -= c->border_width;
c->y -= c->border_width + c->titlebar_height;
c->w += c->border_width*2;
c->h += c->border_width*2 + c->titlebar_height;
}
// check whether the frame should be created
if (c->decorate && !c->cache->frame)
{
c->cache->frame = box_create(root, 0, c->x, c->y, c->w, c->h, config_border_blur);
cache = allocate_clear(sizeof(wincache));
winlist_append(windows, c->cache->frame->window, cache);
cache->is_ours = 1;
// associate with the window (see handle_buttonpress)
cache->app = c->window;
XSelectInput(display, c->cache->frame->window, ExposureMask);
// stack frame under client window
Window wins[2] = { c->window, c->cache->frame->window };
XRestackWindows(display, wins, 2);
// ...and same for titlebar
if (config_titlebar_height)
{
client_extended_data(c);
c->cache->title = textbox_create(c->cache->frame->window,
TB_CENTER, 0, c->border_width, c->w, config_titlebar_height,
config_titlebar_font, config_titlebar_focus, config_border_focus,
c->title, NULL);
XSelectInput(display, c->cache->title->window, ExposureMask);
textbox_show(c->cache->title);
}
}
winlist_append(cache_client, c->window, c);
return c;
}
// refresh client_cache
client* client_recreate(Window w)
{
int idx = winlist_find(cache_client, w);
if (idx >= 0)
{
client_free(cache_client->data[idx]);
cache_client->data[idx] = NULL;
winlist_forget(cache_client, w);
}
return client_create(w);
}
// release client memory. this should only be called during the global cache resets
void client_free(client *c)
{
if (!c) return;
if (c->title != empty) free(c->title);
if (c->class != empty) free(c->class);
if (c->name != empty) free(c->name);
free(c);
}
// true if client windows overlap
int clients_intersect(client *a, client *b)
{
client_extended_data(a); client_extended_data(b);
return INTERSECT(a->x, a->y, a->w, a->h, b->x, b->y, b->w, b->h) ?1:0;
}
// if a client supports a WM_PROTOCOLS type atom, dispatch an event
int client_protocol_event(client *c, Atom protocol)
{
Atom *protocols = NULL;
int i, found = 0, num_pro = 0;
if (XGetWMProtocols(display, c->window, &protocols, &num_pro))
for (i = 0; i < num_pro && !found; i++)
if (protocols[i] == protocol) found = 1;
if (found)
window_send_message(c->window, c->window, atoms[WM_PROTOCOLS], protocol, NoEventMask);
if (protocols) XFree(protocols);
return found;
}
// close a window politely if possible, else kill it
void client_close(client *c)
{
// prevent frame flash
c->active = 0;
client_redecorate(c);
if (c->cache->frame) box_hide(c->cache->frame);
if (c->cache->have_closed || !client_protocol_event(c, atoms[WM_DELETE_WINDOW]))
XKillClient(display, c->window);
c->cache->have_closed = 1;
}
// true if x/y is over a visible portion of the client window
int client_warp_check(client *c, int x, int y)
{
int i, ok = 1; Window w; client *o;
managed_descend(i, w, o)
{
if (!ok || w == c->window) break;
if (INTERSECT(o->x, o->y, o->w, o->h, x, y, 1, 1)) ok = 0;
}
return ok;
}
// ensure the pointer is over a specific client
void client_warp_pointer(client *c)
{
// needs the updated stacking mode, so clear cache
XSync(display, False);
reset_cache_inplay();
client_extended_data(c);
int vague = MAX(c->monitor.w/100, c->monitor.h/100);
int x, y; if (!pointer_get(&x, &y)) return;
int mx = x, my = y;
// if pointer is not already over the client...
if (!INTERSECT(c->x, c->y, c->w, c->h, x, y, 1, 1) || !client_warp_check(c, x, y))
{
int overlap_x = OVERLAP(c->x, c->w, x, 1);
int overlap_y = OVERLAP(c->y, c->h, y, 1);
int xd = 0, yd = 0;
if (overlap_y && x < c->x) { x = c->x; xd = vague; }
if (overlap_y && x > c->x) { x = MIN(x, c->x+c->w-1); xd = 0-vague; }
if (overlap_x && y < c->y) { y = c->y; yd = vague; }
if (overlap_x && y > c->y) { y = MIN(y, c->y+c->h-1); yd = 0-vague; }
// step toward client window
while ((xd || yd ) && INTERSECT(c->x, c->y, c->w, c->h, x, y, 1, 1) && !client_warp_check(c, x, y))
{ x += xd; y += yd; }
// ensure pointer is slightly inside border
x = MIN(c->x+c->w-vague, MAX(c->x+vague, x));
y = MIN(c->y+c->h-vague, MAX(c->y+vague, y));
XWarpPointer(display, None, None, 0, 0, 0, 0, x-mx, y-my);
}
}
// adjust co-ordinates to take hints into account, ready for move/resize
void client_process_size_hints(client *c, int *x, int *y, int *w, int *h)
{
// fw/fh still include borders here
int fx = *x, fy = *y, fw = *w, fh = *h;
int dec_w = c->border_width*2, dec_h = c->border_width*2+c->titlebar_height;
int basew = 0, baseh = 0;
if (c->xsize.flags & PBaseSize)
{
basew = c->xsize.base_width;
baseh = c->xsize.base_height;
}
if (c->xsize.flags & PMinSize)
{
// fw/fh still include borders here
fw = MAX(fw, c->xsize.min_width + dec_w);
fh = MAX(fh, c->xsize.min_height + dec_h);
}
if (c->xsize.flags & PMaxSize)
{
// fw/fh still include borders here
fw = MIN(fw, c->xsize.max_width + dec_w);
fh = MIN(fh, c->xsize.max_height + dec_h);
}
if (config_resize_inc && c->xsize.flags & PResizeInc)
{
client_descriptive_data(c);
if (config_resize_inc == RESIZEINC
|| (config_resize_inc == SMARTRESIZEINC && !regquick(SMARTRESIZEINC_IGNORE, c->class)))
{
// fw/fh still include borders here
fw -= basew + dec_w; fh -= baseh + dec_h;
fw -= fw % c->xsize.width_inc;
fh -= fh % c->xsize.height_inc;
fw += basew + dec_w; fh += baseh + dec_h;
}
}
if (c->xsize.flags & PAspect)
{
double ratio = (double) fw / fh;
double minr = (double) c->xsize.min_aspect.x / c->xsize.min_aspect.y;
double maxr = (double) c->xsize.max_aspect.x / c->xsize.max_aspect.y;
if (ratio < minr) fh = (int)(fw / minr);
else if (ratio > maxr) fw = (int)(fh * maxr);
}
*x = fx; *y = fy; *w = fw; *h = fh;
}
// move & resize a window nicely, respecting hints and EWMH states
void client_moveresize(client *c, unsigned int flags, int fx, int fy, int fw, int fh)
{
client_extended_data(c);
int vague = MAX(c->monitor.w/100, c->monitor.h/100);
int i; Window win; client *o;
int xsnap = 0, ysnap = 0;
// this many be different to the client's current c->monitor...
workarea monitor; monitor_dimensions_struts(MAX(fx, 0), MAX(fy, 0), &monitor);
// horz/vert size locks
if (c->cache->vlock) { fy = c->y; fh = c->h; }
if (c->cache->hlock) { fx = c->x; fw = c->w; }
// ensure we match fullscreen/maxv/maxh mode. these override above locks!
if (client_has_state(c, netatoms[_NET_WM_STATE_FULLSCREEN]))
{
fx = monitor.x-monitor.l; fy = monitor.y-monitor.t;
fw = monitor.w+monitor.l+monitor.r; fh = monitor.h+monitor.t+monitor.b;
}
else
{
if (client_has_state(c, netatoms[_NET_WM_STATE_MAXIMIZED_HORZ]))
{ fx = monitor.x; fw = monitor.w; }
if (client_has_state(c, netatoms[_NET_WM_STATE_MAXIMIZED_VERT]))
{ fy = monitor.y; fh = monitor.h; }
// shrink onto screen
if (!(flags & MR_UNCONSTRAIN))
{
fw = MAX(MINWINDOW, MIN(fw, monitor.w));
fh = MAX(MINWINDOW, MIN(fh, monitor.h));
}
client_process_size_hints(c, &fx, &fy, &fw, &fh);
// bump onto screen
if (!(flags & MR_UNCONSTRAIN))
{
fx = MAX(MIN(fx, monitor.x + monitor.w - fw), monitor.x);
fy = MAX(MIN(fy, monitor.y + monitor.h - fh), monitor.y);
}
}
// put the window in same general position it was before
if (flags & MR_SMART)
{
// shrinking w. check if we were once in a corner previous-to-last
// expanding w is already covered by bumping above
if (c->cache && c->cache->last_corner && c->w > fw)
{
if (c->cache->last_corner == TOPLEFT || c->cache->last_corner == BOTTOMLEFT || c->cache->last_corner == CENTERLEFT)
fx = monitor.x;
if (c->cache->last_corner == TOPRIGHT || c->cache->last_corner == BOTTOMRIGHT || c->cache->last_corner == CENTERRIGHT)
fx = monitor.x + monitor.w - fw;
if (c->cache->last_corner == CENTERTOP || c->cache->last_corner == CENTERBOTTOM)
fx = monitor.x + (monitor.w - fw)/2;
}
// screen center always wins
else if (c->is_xcenter) fx = monitor.x + ((monitor.w - fw) / 2);
else if (c->is_left) fx = monitor.x;
else if (c->is_right) fx = monitor.x + monitor.w - fw;
// shrinking h. check if we were once in a corner previous-to-last
// expanding h is already covered by bumping above
if (c->cache && c->cache->last_corner && c->h > fh)
{
if (c->cache->last_corner == TOPLEFT || c->cache->last_corner == TOPRIGHT || c->cache->last_corner == CENTERTOP)
fy = monitor.y;
if (c->cache->last_corner == BOTTOMLEFT || c->cache->last_corner == BOTTOMRIGHT || c->cache->last_corner == CENTERBOTTOM)
fy = monitor.y + monitor.h - fh;
if (c->cache->last_corner == CENTERLEFT || c->cache->last_corner == CENTERRIGHT)
fy = monitor.y + (monitor.h - fh)/2;
}
// screen center always wins
else if (c->is_ycenter) fy = monitor.y + ((monitor.h - fh) / 2);
else if (c->is_top) fy = monitor.y;
else if (c->is_bottom) fy = monitor.y + monitor.h - fh;
}
// snap all edges by moving window
// built for MotionNotify Button1
if (flags & MR_SNAP)
{
// snap to monitor edges
if (NEAR(c->monitor.x, vague, fx)) { fx = c->monitor.x; xsnap = 1; }
if (NEAR(c->monitor.y, vague, fy)) { fy = c->monitor.y; ysnap = 1; }
if (!xsnap && NEAR(c->monitor.x+c->monitor.w, vague, fx+fw)) { fx = c->monitor.x+c->monitor.w-fw; xsnap = 1; }
if (!ysnap && NEAR(c->monitor.y+c->monitor.h, vague, fy+fh)) { fy = c->monitor.y+c->monitor.h-fh; ysnap = 1; }
// snap to window edges
if (!xsnap || !ysnap)
{
winlist *visible = clients_partly_visible(&monitor, 0, c->window);
clients_descend(visible, i, win, o)
{
if (!xsnap && NEAR(o->x, vague, fx)) { fx = o->x; xsnap = 1; }
if (!ysnap && NEAR(o->y, vague, fy)) { fy = o->y; ysnap = 1; }
if (!xsnap && NEAR(o->x+o->w, vague, fx)) { fx = o->x+o->w; xsnap = 1; }
if (!ysnap && NEAR(o->y+o->h, vague, fy)) { fy = o->y+o->h; ysnap = 1; }
if (!xsnap && NEAR(o->x, vague, fx+fw)) { fx = o->x+-fw; xsnap = 1; }
if (!ysnap && NEAR(o->y, vague, fy+fh)) { fy = o->y+-fh; ysnap = 1; }
if (!xsnap && NEAR(o->x+o->w, vague, fx+fw)) { fx = o->x+o->w-fw; xsnap = 1; }
if (!ysnap && NEAR(o->y+o->h, vague, fy+fh)) { fy = o->y+o->h-fh; ysnap = 1; }
if (xsnap && ysnap) break;
}
winlist_free(visible);
}
}
else
// snap right and bottom edges by resizing window
// built for MotionNotify Button3
if (flags & MR_SNAPWH)
{
// snap to monitor edges
if (NEAR(c->monitor.x+c->monitor.w, vague, fx+fw)) { fw = c->monitor.x+c->monitor.w-fx; xsnap = 1; }
if (NEAR(c->monitor.y+c->monitor.h, vague, fy+fh)) { fh = c->monitor.y+c->monitor.h-fy; ysnap = 1; }
// snap to window edges
if (!xsnap || !ysnap)
{
winlist *visible = clients_partly_visible(&monitor, 0, c->window);
clients_descend(visible, i, win, o)
{
if (!xsnap && NEAR(o->x, vague, fx+fw)) { fw = o->x-fx; xsnap = 1; }
if (!ysnap && NEAR(o->y, vague, fy+fh)) { fh = o->y-fy; ysnap = 1; }
if (!xsnap && NEAR(o->x+o->w, vague, fx+fw)) { fw = o->x+o->w-fx; xsnap = 1; }
if (!ysnap && NEAR(o->y+o->h, vague, fy+fh)) { fh = o->y+o->h-fy; ysnap = 1; }
if (xsnap && ysnap) break;
}
winlist_free(visible);
}
}
// this needs to occur despite MR_UNCONSTRAIN
fw = MAX(MINWINDOW, fw);
fh = MAX(MINWINDOW, fh);
// update window co-ords for subsequent operations before caches are reset
c->x = fx; c->y = fy; c->w = fw; c->h = fh;
memmove(&c->monitor, &monitor, sizeof(workarea));
// compensate for border on non-fullscreen windows
if (c->decorate && !client_has_state(c, netatoms[_NET_WM_STATE_FULLSCREEN]))
{
fx += c->border_width;
fy += c->border_width + c->titlebar_height;
fw = MAX(1, fw - c->border_width*2);
fh = MAX(1, fh - c->border_width*2 - c->titlebar_height);
}
if (c->decorate) box_moveresize(c->cache->frame, c->x, c->y, c->w, c->h);
XMoveResizeWindow(display, c->window, fx, fy, fw, fh);
client_redecorate(c);
}
// record a window's size and position in the undo log
void client_commit(client *c)
{
client_extended_data(c);
int levels = 0; winundo *undo = c->cache->undo;
// count current undo chain length for this window
while (undo) { levels++; undo = undo->next; }
if (levels > 0)
{
// check if the most recent undo state matches current state. if so, no point recording
undo = c->cache->undo;
if (undo->x == c->x && undo->y == c->y && undo->w == c->w && undo->h == c->h) return;
}
// LIFO up to UNDO cells deep
if (levels == UNDO)
{
undo = c->cache->undo;
// find second last link
while (undo->next && undo->next->next) undo = undo->next;
// chop of the last link
free(undo->next); undo->next = NULL;
}
undo = allocate_clear(sizeof(winundo));
undo->next = c->cache->undo; c->cache->undo = undo;
// do the actual snapshot
undo->x = c->x; undo->y = c->y; undo->w = c->w; undo->h = c->h;
for (undo->states = 0; undo->states < c->states; undo->states++)
undo->state[undo->states] = c->state[undo->states];
}
// move/resize a window back to it's last known size and position
void client_rollback(client *c)
{
if (c->cache->undo)
{
// remove most recent winundo from the undo chain
winundo *undo = c->cache->undo; c->cache->undo = undo->next;
// do the actual rollback
for (c->states = 0; c->states < undo->states; c->states++)
c->state[c->states] = undo->state[c->states];
client_flush_state(c);
client_moveresize(c, 0, undo->x, undo->y, undo->w, undo->h);
free(undo);
}
}
// save co-ords for later flip-back
// these may MAY BE dulicated in the undo log, but they must remain separate
// to allow proper toggle behaviour for maxv/maxh
void client_save_position(client *c)
{
client_extended_data(c);
if (!c->cache) return;
if (!c->cache->ewmh)
c->cache->ewmh = allocate_clear(sizeof(winundo));
winundo *undo = c->cache->ewmh;
undo->x = c->x; undo->y = c->y;
undo->w = c->w; undo->h = c->h;
}
// save co-ords for later flip-back
void client_save_position_horz(client *c)
{
client_extended_data(c); if (!c->cache) return;
if (!c->cache->ewmh) client_save_position(c);
winundo *undo = c->cache->ewmh;
undo->x = c->x; undo->w = c->w;
}
// save co-ords for later flip-back
void client_save_position_vert(client *c)
{
client_extended_data(c); if (!c->cache) return;
if (!c->cache->ewmh) client_save_position(c);
winundo *undo = c->cache->ewmh;
undo->y = c->y; undo->h = c->h;
}
// revert to saved co-ords
void client_restore_position(client *c, unsigned int smart, int x, int y, int w, int h)
{
client_extended_data(c);
client_moveresize(c, smart,
c->cache && c->cache->ewmh ? c->cache->ewmh->x: x,
c->cache && c->cache->ewmh ? c->cache->ewmh->y: y,
c->cache && c->cache->ewmh ? c->cache->ewmh->w: w,
c->cache && c->cache->ewmh ? c->cache->ewmh->h: h);
}
// revert to saved co-ords
void client_restore_position_horz(client *c, unsigned int smart, int x, int w)
{
client_extended_data(c);
client_moveresize(c, smart,
c->cache && c->cache->ewmh ? c->cache->ewmh->x: x, c->y,
c->cache && c->cache->ewmh ? c->cache->ewmh->w: w, c->h);
}
// revert to saved co-ords
void client_restore_position_vert(client *c, unsigned int smart, int y, int h)
{
client_extended_data(c);
client_moveresize(c, smart,
c->x, c->cache && c->cache->ewmh ? c->cache->ewmh->y: y,
c->w, c->cache && c->cache->ewmh ? c->cache->ewmh->h: h);
}
// build list of unobscured windows within a workarea
winlist* clients_fully_visible(workarea *zone, unsigned int tag, Window ignore)
{
winlist *hits = winlist_new();
winlist *inplay = windows_in_play();
// list of coords/sizes for all windows on this desktop
workarea *allregions = allocate_clear(sizeof(workarea) * inplay->len);
int i; Window win; client *o;
tag_descend(i, win, o, tag)
{
client_extended_data(o);
// only concerned about windows in the zone
if (ignore != o->window && INTERSECT(o->x, o->y, o->w, o->h, zone->x, zone->y, zone->w, zone->h))
{
int j, obscured = 0;
for (j = inplay->len-1; j > i; j--)
{
// if the window intersects with any other window higher in the stack order, it must be at least partially obscured
if (allregions[j].w && INTERSECT(o->x, o->y, o->w, o->h,
allregions[j].x, allregions[j].y, allregions[j].w, allregions[j].h))
{ obscured = 1; break; }
}
// record a full visible window
if (!obscured && o->x >= zone->x && o->y >= zone->y && (o->x + o->w) <= (zone->x + zone->w) && (o->y + o->h) <= (zone->y + zone->h))
winlist_append(hits, o->window, NULL);
allregions[i].x = o->x; allregions[i].y = o->y;
allregions[i].w = o->w; allregions[i].h = o->h;
}
}
// return it in stacking order, bottom to top
winlist_reverse(hits);
free(allregions);
return hits;
}
// build list of unobscured windows within a workarea
winlist* clients_partly_visible(workarea *zone, unsigned int tag, Window ignore)
{
winlist *hits = winlist_new();
winlist *inplay = windows_in_play();
// list of coords/sizes for all windows on this desktop
workarea *allregions = allocate_clear(sizeof(workarea) * inplay->len);
int i; Window win; client *o;
tag_descend(i, win, o, tag)
{
client_extended_data(o);
// only concerned about windows in the zone
if (ignore != o->window && INTERSECT(o->x, o->y, o->w, o->h, zone->x, zone->y, zone->w, zone->h))
{
int j, c1 = 0, c2 = 0, c3 = 0, c4 = 0;
for (j = inplay->len-1; j > i; j--)
{
if (!allregions[j].w) continue;
// if the window's corners intersects with any other window higher in the stack order, assume it is covered
if (INTERSECT(o->x, o->y, 1, 1, allregions[j].x, allregions[j].y, allregions[j].w, allregions[j].h)) c1 = 1;
if (INTERSECT(o->x, o->y+o->h-1, 1, 1, allregions[j].x, allregions[j].y, allregions[j].w, allregions[j].h)) c2 = 1;
if (INTERSECT(o->x+o->w-1, o->y, 1, 1, allregions[j].x, allregions[j].y, allregions[j].w, allregions[j].h)) c3 = 1;
if (INTERSECT(o->x+o->w-1, o->y+o->h-1, 1, 1, allregions[j].x, allregions[j].y, allregions[j].w, allregions[j].h)) c4 = 1;
if (c1 && c2 && c3 && c4) break;
}
// record a full visible window
if ((!c1 || !c2 || !c3 || !c4) && o->x >= zone->x && o->y >= zone->y && (o->x + o->w) <= (zone->x + zone->w) && (o->y + o->h) <= (zone->y + zone->h))
winlist_append(hits, o->window, NULL);
allregions[i].x = o->x; allregions[i].y = o->y;
allregions[i].w = o->w; allregions[i].h = o->h;
}
}
// return it in stacking order, bottom to top
winlist_reverse(hits);
free(allregions);
return hits;
}
// expand a window to take up available space around it on the current monitor
// do not cover any window that is entirely visible (snap to surrounding edges)
void client_expand(client *c, int directions, int x1, int y1, int w1, int h1, int mx, int my, int mw, int mh)
{
client_extended_data(c);
// hlock/vlock reduce the area we should look at
if (c->cache->hlock) { mx = c->x; mw = c->w; if (!mh) { my = c->monitor.y; mh = c->monitor.h; } }
if (c->cache->vlock) { my = c->y; mh = c->h; if (!mw) { mx = c->monitor.x; mw = c->monitor.w; } }
// expand only cares about fully visible windows. partially or full obscured windows == free space
winlist *visible = clients_fully_visible(&c->monitor, c->cache->tags, c->window);
// list of coords/sizes for fully visible windows on this desktop
workarea *regions = allocate_clear(sizeof(workarea) * visible->len);
int i, n = 0, relevant = visible->len; Window win; client *o;
clients_descend(visible, i, win, o)
{
client_extended_data(o);
if ((mw || mh) && !INTERSECT(o->x, o->y, o->w, o->h, mx, my, mw, mh)) continue;
regions[n].x = o->x; regions[n].y = o->y;
regions[n].w = o->w; regions[n].h = o->h;
n++;
}
int x = c->x, y = c->y, w = c->w, h = c->h;
if (w1 || h1) { x = x1; y = y1; w = w1; h = h1; }
if (directions & VERTICAL)
{
// try to grow upward. locate the lower edge of the nearest fully visible window
for (n = c->monitor.y, i = 0; i < relevant; i++)
if (regions[i].y + regions[i].h <= y && OVERLAP(x, w, regions[i].x, regions[i].w))
n = MAX(n, regions[i].y + regions[i].h);
h += y-n; y = n;
// try to grow downward. locate the upper edge of the nearest fully visible window
for (n = c->monitor.y + c->monitor.h, i = 0; i < relevant; i++)
if (regions[i].y >= y+h && OVERLAP(x, w, regions[i].x, regions[i].w))
n = MIN(n, regions[i].y);
h = n-y;
}
if (directions & HORIZONTAL)
{
// try to grow left. locate the right edge of the nearest fully visible window
for (n = c->monitor.x, i = 0; i < relevant; i++)
if (regions[i].x + regions[i].w <= x && OVERLAP(y, h, regions[i].y, regions[i].h))
n = MAX(n, regions[i].x + regions[i].w);
w += x-n; x = n;
// try to grow right. locate the left edge of the nearest fully visible window
for (n = c->monitor.x + c->monitor.w, i = 0; i < relevant; i++)
if (regions[i].x >= x+w && OVERLAP(y, h, regions[i].y, regions[i].h))
n = MIN(n, regions[i].x);
w = n-x;
}
// optionally limit final size to a bounding box
if (mw || mh)
{
if (x < mx) { w -= mx-x; x = mx; }
if (y < my) { h -= my-y; y = my; }
w = MIN(w, mw);
h = MIN(h, mh);
}
client_commit(c);
client_moveresize(c, 0, x, y, w, h);
// if we looked like we could expand, but couldn't due to some condition in client_moveresize(),
// act like a toggle and rollback instead
winundo *undo = c->cache->undo;
if (undo->x == c->x && undo->y == c->y && undo->w == c->w && undo->h == c->h)
{
// yes, twice!
client_rollback(c);
client_rollback(c);
}
free(regions);
winlist_free(visible);
}
// shrink to fit into an empty gap underneath. opposite of client_expand()
void client_contract(client *c, int directions)
{
client_extended_data(c);
// cheat and shrink the window absurdly so it becomes just another expansion
if (directions & VERTICAL && directions & HORIZONTAL)
client_expand(c, directions, c->x+(c->w/2), c->y+(c->h/2), 5, 5, c->x, c->y, c->w, c->h);
else
if (directions & VERTICAL)
client_expand(c, directions, c->x, c->y+(c->h/2), c->w, 5, c->x, c->y, c->w, c->h);
else
if (directions & HORIZONTAL)
client_expand(c, directions, c->x+(c->w/2), c->y, 5, c->h, c->x, c->y, c->w, c->h);
}
// move or resize a client window to snap to someone else's leading or trailing edge
void client_snapto(client *c, int direction)
{
client_extended_data(c);
// hlock/vlock may block this
if (c->cache->hlock && (direction == SNAPLEFT || direction == SNAPRIGHT)) return;
if (c->cache->vlock && (direction == SNAPUP || direction == SNAPDOWN )) return;
// expand only cares about fully visible windows. partially or full obscured windows == free space
winlist *visible = clients_partly_visible(&c->monitor, c->cache->tags, c->window);
// list of coords/sizes for fully visible windows on this desktop
workarea *regions = allocate_clear(sizeof(workarea) * visible->len);
int i, n = 0, relevant = visible->len; Window win; client *o;
clients_descend(visible, i, win, o)
{
client_extended_data(o);
regions[n].x = o->x; regions[n].y = o->y;
regions[n].w = o->w; regions[n].h = o->h;
n++;
}
int x = c->x, y = c->y, w = c->w, h = c->h;
if (direction == SNAPUP)
{
y--;
for (n = c->monitor.y, i = 0; i < relevant; i++)
{
if (!OVERLAP(c->x-1, c->w+2, regions[i].x, regions[i].w)) continue;
if (regions[i].y + regions[i].h <= y) n = MAX(n, regions[i].y + regions[i].h);
if (regions[i].y <= y) n = MAX(n, regions[i].y);
if (regions[i].y + regions[i].h <= y+h) n = MAX(n, regions[i].y + regions[i].h - h);
if (regions[i].y <= y+h) n = MAX(n, regions[i].y - h);
}
y = n;
}
if (direction == SNAPDOWN)
{
y++;
for (n = c->monitor.y + c->monitor.h, i = 0; i < relevant; i++)
{
if (!OVERLAP(c->x-1, c->w+2, regions[i].x, regions[i].w)) continue;
if (regions[i].y + regions[i].h >= y+h) n = MIN(n, regions[i].y + regions[i].h);
if (regions[i].y >= y+h) n = MIN(n, regions[i].y);
if (regions[i].y + regions[i].h >= y) n = MIN(n, regions[i].y + regions[i].h + h);
if (regions[i].y >= y) n = MIN(n, regions[i].y + h);
}
y = n-h;
}
if (direction == SNAPLEFT)
{
x--;
for (n = c->monitor.x, i = 0; i < relevant; i++)
{
if (!OVERLAP(c->y-1, c->h+2, regions[i].y, regions[i].h)) continue;
if (regions[i].x + regions[i].w <= x) n = MAX(n, regions[i].x + regions[i].w);
if (regions[i].x <= x) n = MAX(n, regions[i].x);
if (regions[i].x + regions[i].w <= x+w) n = MAX(n, regions[i].x + regions[i].w - w);
if (regions[i].x <= x+w) n = MAX(n, regions[i].x - w);
}
x = n;
}
if (direction == SNAPRIGHT)
{
x++;
for (n = c->monitor.x + c->monitor.w, i = 0; i < relevant; i++)
{
if (!OVERLAP(c->y-1, c->h+2, regions[i].y, regions[i].h)) continue;
if (regions[i].x + regions[i].w >= x+w) n = MIN(n, regions[i].x + regions[i].w);
if (regions[i].x >= x+w) n = MIN(n, regions[i].x);
if (regions[i].x + regions[i].w >= x) n = MIN(n, regions[i].x + regions[i].w + w);
if (regions[i].x >= x) n = MIN(n, regions[i].x + w);
}
x = n-w;
}
client_commit(c);
client_moveresize(c, 0, x, y, w, h);
free(regions);
winlist_free(visible);
}
// make a window take up 2/3 of a monitor
void client_toggle_large(client *c, int side)
{
int vague = MAX(c->monitor.w/100, c->monitor.h/100);
int width3 = c->monitor.w - c->monitor.w/3;
int height4 = c->monitor.h;
int is_largeleft = c->is_left && c->is_maxv && NEAR(width3, vague, c->w) ?1:0;
int is_largeright = c->is_right && c->is_maxv && NEAR(width3, vague, c->w) ?1:0;
c->cache->hlock = 0; c->cache->vlock = 0;
client_remove_state(c, netatoms[_NET_WM_STATE_MAXIMIZED_HORZ]);
if (side == LARGELEFT)
{
// act like a toggle
if (is_largeleft)
client_rollback(c);
else {
client_commit(c);
client_moveresize(c, 0, c->monitor.x, c->monitor.y, width3, height4);
}
}
else