-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmanage.c
More file actions
1407 lines (1206 loc) · 30.8 KB
/
manage.c
File metadata and controls
1407 lines (1206 loc) · 30.8 KB
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
/*
* Copyright 2020 joshua stein <jcs@jcs.org>
* Copyright 1998-2007 Decklin Foster <decklin@red-bean.com>.
*
* 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
* AUTHOR 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.
*/
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include "progman.h"
#include "atom.h"
static void do_iconify(client_t *);
static void do_shade(client_t *);
static void maybe_toolbar_click(client_t *, Window);
static void monitor_toolbar_click(client_t *, geom_t, int, int, int, int,
strut_t *, void *);
static struct {
struct timespec tv;
client_t *c;
Window win;
int button;
} last_click = { { 0, 0 }, NULL, 0 };
void
user_action(client_t *c, Window win, int x, int y, int button, int down)
{
struct timespec now;
long long tdiff;
int double_click = 0;
if (!down) {
clock_gettime(CLOCK_MONOTONIC, &now);
if (last_click.button == button && c == last_click.c &&
win == last_click.win) {
tdiff = (((now.tv_sec * 1000000000) + now.tv_nsec) -
((last_click.tv.tv_sec * 1000000000) +
last_click.tv.tv_nsec)) / 1000000;
if (tdiff <= DOUBLE_CLICK_MSEC)
double_click = 1;
}
last_click.button = button;
last_click.c = c;
last_click.win = win;
memcpy(&last_click.tv, &now, sizeof(now));
}
#ifdef DEBUG
printf("%s(\"%s\", %lx, %d, %d, %d, %d) double:%d, c state %d\n",
__func__, c->name, win, x, y, button, down, double_click, c->state);
dump_info(c);
#endif
if (c->state & STATE_ICONIFIED &&
(win == c->icon || win == c->icon_label)) {
if (down && !double_click) {
focus_client(c, FOCUS_NORMAL);
move_client(c);
get_pointer(&x, &y);
user_action(c, win, x, y, button, 0);
} else if (!down && double_click)
uniconify_client(c);
} else if (win == c->titlebar) {
if (button == 1 && down) {
if (!(c->state & (STATE_ZOOMED | STATE_FULLSCREEN))) {
move_client(c);
/* sweep() eats the ButtonRelease event */
get_pointer(&x, &y);
user_action(c, win, x, y, button, 0);
}
} else if (button == 1 && !down && double_click) {
if (c->state & STATE_ZOOMED)
unzoom_client(c);
else
zoom_client(c);
} else if (button == 3 && !down) {
if (c->state & STATE_SHADED)
unshade_client(c);
else
shade_client(c);
}
} else if (win == c->close) {
if (button == 1 && down) {
maybe_toolbar_click(c, win);
if (!c->close_pressed)
return;
c->close_pressed = False;
redraw_frame(c, c->close);
get_pointer(&x, &y);
user_action(c, win, x, y, button, 0);
}
if (double_click)
send_wm_delete(c);
} else if (IS_RESIZE_WIN(c, win)) {
if (button == 1 && down && !(c->state & STATE_SHADED))
resize_client(c, win);
} else if (win == c->iconify) {
if (button == 1 && down) {
maybe_toolbar_click(c, win);
if (c->iconify_pressed) {
c->iconify_pressed = False;
redraw_frame(c, c->iconify);
if (c->state & STATE_ICONIFIED)
uniconify_client(c);
else
iconify_client(c);
}
}
} else if (win == c->zoom) {
if (button == 1 && down) {
maybe_toolbar_click(c, win);
if (c->zoom_pressed) {
c->zoom_pressed = False;
redraw_frame(c, c->zoom);
if (c->state & STATE_ZOOMED)
unzoom_client(c);
else
zoom_client(c);
}
}
}
if (double_click)
/* don't let a 3rd click keep counting as a double click */
last_click.tv.tv_sec = last_click.tv.tv_nsec = 0;
}
Cursor
cursor_for_resize_win(client_t *c, Window win)
{
if (win == c->resize_nw)
return resize_nw_curs;
else if (win == c->resize_w)
return resize_w_curs;
else if (win == c->resize_sw)
return resize_sw_curs;
else if (win == c->resize_s)
return resize_s_curs;
else if (win == c->resize_se)
return resize_se_curs;
else if (win == c->resize_e)
return resize_e_curs;
else if (win == c->resize_ne)
return resize_ne_curs;
else if (win == c->resize_n)
return resize_n_curs;
else
return None;
}
/* This can't do anything dangerous. */
void
focus_client(client_t *c, int style)
{
client_t *prevfocused = NULL;
client_t *trans[10] = { NULL };
client_t *p;
int transcount = 0;
if (!c) {
warnx("%s with no c", __func__);
abort();
}
if (focused == c && style != FOCUS_FORCE)
return;
#ifdef DEBUG
dump_name(c, __func__, NULL, c->name);
#endif
if (c->state & STATE_ICONIFIED) {
set_atoms(root, net_active_window, XA_WINDOW, &c->icon, 1);
XSetInputFocus(dpy, c->icon, RevertToPointerRoot, CurrentTime);
} else {
set_atoms(root, net_active_window, XA_WINDOW, &c->win, 1);
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
XInstallColormap(dpy, c->cmap);
}
if (focused && focused != c) {
prevfocused = focused;
if (focused->state & STATE_ICONIFIED)
adjust_client_order(focused,
ORDER_ICONIFIED_TOP);
else
adjust_client_order(focused, ORDER_TOP);
}
adjust_client_order(c, ORDER_TOP);
/* raise any transients of this window */
for (p = focused; p; p = p->next) {
if (p->trans == c->win) {
trans[transcount++] = p;
if (transcount == sizeof(trans) / (sizeof(trans[0])))
break;
}
}
if (transcount != 0) {
for (transcount--; transcount >= 0; transcount--) {
#ifdef DEBUG
dump_name(c, __func__, "transient",
trans[transcount]->name);
#endif
adjust_client_order(trans[transcount], ORDER_TOP);
}
}
restack_clients();
if (prevfocused)
redraw_frame(prevfocused, None);
redraw_frame(c, None);
}
void
move_client(client_t *c)
{
strut_t s = { 0 };
if (c->state & (STATE_ZOOMED | STATE_FULLSCREEN | STATE_DOCK))
return;
collect_struts(c, &s);
dragging = c;
sweep(c, move_curs, recalc_move, NULL, &s);
dragging = NULL;
if (!(c->state & STATE_ICONIFIED))
redraw_frame(c, None);
send_config(c);
flush_expose_client(c);
}
/*
* If we are resizing a client that was zoomed, we have to put it in an
* unzoomed state, but we need to start sweeping from the effective geometry
* rather than the "real" geometry that unzooming will restore. We get around
* this by blatantly cheating.
*/
void
resize_client(client_t *c, Window resize_win)
{
strut_t hold = { 0, 0, 0, 0 };
XEvent junk;
if (c->state & STATE_ZOOMED) {
c->save = c->geom;
unzoom_client(c);
}
sweep(c, cursor_for_resize_win(c, resize_win), recalc_resize,
&resize_win, &hold);
if (c->shaped) {
/* flush ShapeNotify events */
while (XCheckTypedWindowEvent(dpy, c->win, shape_event, &junk))
;
}
}
/*
* The user has clicked on a toolbar button but may mouse off of it and then
* let go, so only consider it a click if the mouse is still there when the
* mouse button is released.
*/
void
maybe_toolbar_click(client_t *c, Window win)
{
if (win == c->iconify)
c->iconify_pressed = True;
else if (win == c->zoom)
c->zoom_pressed = True;
else if (win == c->close)
c->close_pressed = True;
else
return;
redraw_frame(c, win);
sweep(c, None, monitor_toolbar_click, &win, NULL);
redraw_frame(c, win);
}
void
monitor_toolbar_click(client_t *c, geom_t orig, int x0, int y0, int x1, int y1,
strut_t *s, void *arg)
{
Window win = *(Window *)arg;
geom_t *geom;
Bool was, *pr;
if (win == c->iconify) {
geom = &c->iconify_geom;
pr = &c->iconify_pressed;
} else if (win == c->zoom) {
geom = &c->zoom_geom;
pr = &c->zoom_pressed;
} else if (win == c->close) {
geom = &c->close_geom;
pr = &c->close_pressed;
} else
return;
was = *pr;
if (x1 >= (c->frame_geom.x + geom->x) &&
x1 <= (c->frame_geom.x + geom->x + geom->w) &&
y1 >= (c->frame_geom.y + geom->y) &&
y1 <= (c->frame_geom.y + geom->y + geom->h))
*pr = True;
else
*pr = False;
if (was != *pr)
redraw_frame(c, win);
}
/* Transients will be iconified when their owner is iconified. */
void
iconify_client(client_t *c)
{
client_t *p;
for (p = focused; p; p = p->next)
if (p->trans == c->win)
do_iconify(p);
do_iconify(c);
focus_client(focused, FOCUS_FORCE);
}
void
do_iconify(client_t *c)
{
XSetWindowAttributes attrs = { 0 };
XGCValues gv;
adjust_client_order(c, ORDER_ICONIFIED_TOP);
if (!c->ignore_unmap)
c->ignore_unmap++;
XUnmapWindow(dpy, c->frame);
XUnmapWindow(dpy, c->win);
c->state |= STATE_ICONIFIED;
set_wm_state(c, IconicState);
get_client_icon(c);
if (c->icon_name)
XFree(c->icon_name);
c->icon_name = get_wm_icon_name(c->win);
if (c->icon_geom.w < 1)
c->icon_geom.w = icon_size;
if (c->icon_geom.h < 1)
c->icon_geom.h = icon_size;
attrs.background_pixel = BlackPixel(dpy, screen);
attrs.event_mask = ButtonPressMask | ButtonReleaseMask |
VisibilityChangeMask | ExposureMask | KeyPressMask |
EnterWindowMask | FocusChangeMask;
place_icon(c);
c->icon = XCreateWindow(dpy, root, c->icon_geom.x, c->icon_geom.h,
c->icon_geom.w, c->icon_geom.h, 0, CopyFromParent, CopyFromParent,
CopyFromParent, CWBackPixel | CWEventMask, &attrs);
set_atoms(c->icon, net_wm_wintype, XA_ATOM, &net_wm_type_desk, 1);
XMapWindow(dpy, c->icon);
c->icon_label = XCreateWindow(dpy, root, 0, 0, c->icon_geom.w,
c->icon_geom.h, 0, CopyFromParent, CopyFromParent, CopyFromParent,
CWBackPixel | CWEventMask, &attrs);
set_atoms(c->icon_label, net_wm_wintype, XA_ATOM, &net_wm_type_desk, 1);
XMapWindow(dpy, c->icon_label);
c->icon_xftdraw = XftDrawCreate(dpy, (Drawable)c->icon_label,
DefaultVisual(dpy, screen), DefaultColormap(dpy, screen));
c->icon_gc = XCreateGC(dpy, c->icon, 0, &gv);
redraw_icon(c, None);
flush_expose_client(c);
}
void
uniconify_client(client_t *c)
{
if (c->desk != cur_desk) {
c->desk = cur_desk;
set_atoms(c->win, net_wm_desk, XA_CARDINAL, &cur_desk, 1);
}
XMapWindow(dpy, c->win);
XMapRaised(dpy, c->frame);
c->state &= ~STATE_ICONIFIED;
set_wm_state(c, NormalState);
c->ignore_unmap++;
XDestroyWindow(dpy, c->icon);
c->icon = None;
c->ignore_unmap++;
if (c->icon_xftdraw) {
XftDrawDestroy(c->icon_xftdraw);
c->icon_xftdraw = None;
}
XDestroyWindow(dpy, c->icon_label);
c->icon_label = None;
focus_client(c, FOCUS_FORCE);
}
void
place_icon(client_t *c)
{
strut_t s = { 0 };
client_t *p;
int x, y, isize;
collect_struts(c, &s);
s.right = DisplayWidth(dpy, screen) - s.right;
s.bottom = DisplayHeight(dpy, screen) - s.bottom;
isize = icon_size * 2.25;
for (y = s.bottom - isize; y >= s.top; y -= isize) {
for (x = s.left + icon_size; x < s.right - isize; x += isize) {
int overlap = 0;
for (p = focused; p; p = p->next) {
if (p == c || !(p->state & STATE_ICONIFIED))
continue;
if ((p->icon_geom.x + icon_size >= x &&
p->icon_geom.x <= x) &&
(p->icon_geom.y + icon_size >= y &&
p->icon_geom.y <= y)) {
overlap = 1;
break;
}
}
if (overlap)
continue;
c->icon_geom.x = x;
c->icon_geom.y = y;
#ifdef DEBUG
dump_geom(c, c->icon_geom, "place_icon");
#endif
return;
}
}
/* shrug */
c->icon_geom.x = s.left;
c->icon_geom.y = s.top;
}
void
shade_client(client_t *c)
{
if (c->state != STATE_NORMAL || (c->frame_style == FRAME_NONE))
return;
c->state |= STATE_SHADED;
append_atoms(c->win, net_wm_state, XA_ATOM, &net_wm_state_shaded, 1);
do_shade(c);
}
void
unshade_client(client_t *c)
{
if (!(c->state & STATE_SHADED))
return;
c->state &= ~(STATE_SHADED);
remove_atom(c->win, net_wm_state, XA_ATOM, net_wm_state_shaded);
do_shade(c);
}
static void
do_shade(client_t *c)
{
if (c->frame) {
redraw_frame(c, None);
if (c->state & STATE_SHADED) {
XUndefineCursor(dpy, c->resize_nw);
XUndefineCursor(dpy, c->resize_n);
XUndefineCursor(dpy, c->resize_ne);
XUndefineCursor(dpy, c->resize_s);
} else {
XDefineCursor(dpy, c->resize_nw, resize_nw_curs);
XDefineCursor(dpy, c->resize_n, resize_n_curs);
XDefineCursor(dpy, c->resize_ne, resize_ne_curs);
XDefineCursor(dpy, c->resize_s, resize_s_curs);
}
}
send_config(c);
flush_expose_client(c);
}
void
fullscreen_client(client_t *c)
{
int screen_x = DisplayWidth(dpy, screen);
int screen_y = DisplayHeight(dpy, screen);
#ifdef DEBUG
dump_name(c, __func__, NULL, c->name);
#endif
if (c->state & (STATE_FULLSCREEN | STATE_DOCK))
return;
if (c->state & STATE_SHADED)
unshade_client(c);
c->save = c->geom;
c->geom.x = 0;
c->geom.y = 0;
c->geom.w = screen_x;
c->geom.h = screen_y;
c->state |= STATE_FULLSCREEN;
redraw_frame(c, None);
send_config(c);
flush_expose_client(c);
}
void
unfullscreen_client(client_t *c)
{
#ifdef DEBUG
dump_name(c, __func__, NULL, c->name);
#endif
if (!(c->state & STATE_FULLSCREEN))
return;
c->geom = c->save;
c->state &= ~STATE_FULLSCREEN;
recalc_frame(c);
redraw_frame(c, None);
send_config(c);
flush_expose_client(c);
}
/*
* When zooming a window, the old geom gets stuffed into c->save. Once we
* unzoom, this should be considered garbage. Despite the existence of vertical
* and horizontal hints, we only provide both at once.
*
* Zooming implies unshading, but the inverse is not true.
*/
void
zoom_client(client_t *c)
{
strut_t s = { 0 };
if (c->state & STATE_DOCK)
return;
if (c->state & STATE_SHADED)
unshade_client(c);
c->save = c->geom;
c->state |= STATE_ZOOMED;
collect_struts(c, &s);
recalc_frame(c);
c->geom.x = s.left;
c->geom.y = s.top + c->titlebar_geom.h;
c->geom.w = DisplayWidth(dpy, screen) - s.left - s.right;
c->geom.h = DisplayHeight(dpy, screen) - s.top - s.bottom - c->geom.y;
append_atoms(c->win, net_wm_state, XA_ATOM, &net_wm_state_mv, 1);
append_atoms(c->win, net_wm_state, XA_ATOM, &net_wm_state_mh, 1);
redraw_frame(c, None);
send_config(c);
flush_expose_client(c);
}
void
unzoom_client(client_t *c)
{
if (!(c->state & STATE_ZOOMED))
return;
c->geom = c->save;
c->state &= ~STATE_ZOOMED;
remove_atom(c->win, net_wm_state, XA_ATOM, net_wm_state_mv);
remove_atom(c->win, net_wm_state, XA_ATOM, net_wm_state_mh);
redraw_frame(c, None);
send_config(c);
flush_expose_client(c);
}
/*
* The name of this function is a little misleading: if the client doesn't
* listen to WM_DELETE then we just terminate it with extreme prejudice.
*/
void
send_wm_delete(client_t *c)
{
int i, n, found = 0;
Atom *protocols;
if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
for (i = 0; i < n; i++)
if (protocols[i] == wm_delete)
found++;
XFree(protocols);
}
if (found)
send_xmessage(c->win, c->win, wm_protos, wm_delete,
NoEventMask);
else
XKillClient(dpy, c->win);
}
void
goto_desk(int new_desk)
{
client_t *c, *newfocus = NULL;
if (new_desk >= ndesks || new_desk < 0)
return;
cur_desk = new_desk;
set_atoms(root, net_cur_desk, XA_CARDINAL, &cur_desk, 1);
for (c = focused; c; c = c->next) {
if (dragging == c) {
c->desk = cur_desk;
set_atoms(c->win, net_wm_desk, XA_CARDINAL, &cur_desk,
1);
}
if (IS_ON_CUR_DESK(c)) {
if (c->state & STATE_ICONIFIED) {
XMapWindow(dpy, c->icon);
XMapWindow(dpy, c->icon_label);
} else {
if (!newfocus && !(c->state & STATE_DOCK))
newfocus = c;
XMapWindow(dpy, c->frame);
}
} else {
if (c->state & STATE_ICONIFIED) {
XUnmapWindow(dpy, c->icon);
XUnmapWindow(dpy, c->icon_label);
} else
XUnmapWindow(dpy, c->frame);
}
send_config(c);
}
restack_clients();
if (newfocus)
focus_client(newfocus, FOCUS_FORCE);
}
void
map_if_desk(client_t *c)
{
if (IS_ON_CUR_DESK(c) && get_wm_state(c->win) == NormalState)
XMapWindow(dpy, c->frame);
else
XUnmapWindow(dpy, c->frame);
}
static XEvent sweepev;
void
sweep(client_t *c, Cursor curs, sweep_func cb, void *cb_arg, strut_t *s)
{
geom_t orig = (c->state & STATE_ICONIFIED ? c->icon_geom : c->geom);
client_t *ec;
strut_t as = { 0 };
int x0, y0, done = 0;
get_pointer(&x0, &y0);
collect_struts(c, &as);
recalc_frame(c);
if (XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync,
GrabModeAsync, root, curs, CurrentTime) != GrabSuccess)
return;
cb(c, orig, x0, y0, x0, y0, s, cb_arg);
while (!done) {
XMaskEvent(dpy, ExposureMask | MouseMask | PointerMotionMask |
StructureNotifyMask | SubstructureNotifyMask |
KeyPressMask | KeyReleaseMask, &sweepev);
#ifdef DEBUG
show_event(sweepev);
#endif
switch (sweepev.type) {
case Expose:
if ((ec = find_client(sweepev.xexpose.window,
MATCH_FRAME)))
redraw_frame(ec, sweepev.xexpose.window);
break;
case MotionNotify:
cb(c, orig, x0, y0, sweepev.xmotion.x,
sweepev.xmotion.y, s, cb_arg);
break;
case ButtonRelease:
done = 1;
break;
case UnmapNotify:
if (c->win == sweepev.xunmap.window) {
done = 1;
XPutBackEvent(dpy, &sweepev);
break;
}
handle_unmap_event(&sweepev.xunmap);
break;
case KeyPress:
case KeyRelease:
/* to allow switching desktops while dragging */
handle_key_event(&sweepev.xkey);
break;
}
}
XUngrabPointer(dpy, CurrentTime);
}
/*
* This is simple and dumb: if the cursor is in the center of the screen,
* center the window on the available space. If it's at the top left, then at
* the top left. As you go between, and to other edges, scale it.
*/
void
recalc_map(client_t *c, geom_t orig, int x0, int y0, int x1, int y1,
strut_t *s, void *arg)
{
int screen_x = DisplayWidth(dpy, screen);
int screen_y = DisplayHeight(dpy, screen);
int wmax = screen_x - s->left - s->right;
int hmax = screen_y - s->top - s->bottom;
c->geom.x = s->left + ((float) x1 / (float) screen_x) *
(wmax + 1 - c->geom.w - (2 * c->resize_nw_geom.w));
c->geom.y = s->top + ((float) y1 / (float) screen_y) *
(hmax + 1 - c->geom.h - c->titlebar_geom.h -
(2 * c->resize_w_geom.w));
}
void
recalc_move(client_t *c, geom_t orig, int x0, int y0, int x1, int y1,
strut_t *s, void *arg)
{
int newx = orig.x + x1 - x0;
int newy = orig.y + y1 - y0;
int sw = DisplayWidth(dpy, screen);
int sh = DisplayHeight(dpy, screen);
geom_t tg;
if (c->state & STATE_ICONIFIED) {
int xd = newx - c->icon_geom.x;
int yd = newy - c->icon_geom.y;
c->icon_geom.x = newx;
c->icon_geom.y = newy;
c->icon_label_geom.x += xd;
c->icon_label_geom.y += yd;
XMoveWindow(dpy, c->icon,
c->icon_geom.x + ((icon_size - c->icon_geom.w) / 2),
c->icon_geom.y + ((icon_size - c->icon_geom.h) / 2));
XMoveWindow(dpy, c->icon_label, c->icon_label_geom.x,
c->icon_label_geom.y);
send_config(c);
flush_expose_client(c);
return;
}
sw -= s->right;
sh -= s->bottom;
memcpy(&tg, &c->frame_geom, sizeof(c->frame_geom));
/* provide some resistance at screen edges */
if (x1 < x0) {
/* left edge */
if (newx - c->resize_w_geom.w >= (long)s->left ||
newx - c->resize_w_geom.w < (long)s->left -
opt_edge_resist) {
c->geom.x = newx;
} else {
c->geom.x = (long)s->left + c->resize_w_geom.w;
}
} else {
/* right edge */
if (newx + c->geom.w + c->resize_e_geom.w <= sw ||
newx + c->geom.w + c->resize_e_geom.w > sw +
opt_edge_resist) {
c->geom.x = newx;
} else {
c->geom.x = sw - c->geom.w - c->resize_e_geom.w;
}
}
if (y1 < y0) {
/* top edge */
if (newy - c->resize_n_geom.h - c->titlebar_geom.h >=
(long)s->top ||
newy - c->resize_n_geom.h - c->titlebar_geom.h <
(long)s->top - opt_edge_resist) {
c->geom.y = newy;
} else {
c->geom.y = (long)s->top + c->resize_n_geom.h +
c->titlebar_geom.h;
}
} else {
/* bottom edge */
if (newy + c->geom.h + c->resize_s_geom.h <= sh ||
newy + c->geom.h + c->resize_s_geom.h > sh +
opt_edge_resist) {
c->geom.y = newy;
} else {
c->geom.y = sh - c->geom.h - c->resize_s_geom.h;
}
}
recalc_frame(c);
if (c->frame_geom.x == tg.x && c->frame_geom.y == tg.y)
return;
XMoveWindow(dpy, c->frame, c->frame_geom.x, c->frame_geom.y);
}
void
recalc_resize(client_t *c, geom_t orig, int x0, int y0, int x1, int y1,
strut_t *move, void *arg)
{
Window resize_pos = *(Window *)arg;
geom_t now = { c->geom.x, c->geom.y, c->geom.w, c->geom.h };
if (resize_pos == c->resize_nw)
move->top = move->left = 1;
else if (resize_pos == c->resize_n)
move->top = 1;
else if (resize_pos == c->resize_ne)
move->top = move->right = 1;
else if (resize_pos == c->resize_e)
move->right = 1;
else if (resize_pos == c->resize_se)
move->right = move->bottom = 1;
else if (resize_pos == c->resize_s)
move->bottom = 1;
else if (resize_pos == c->resize_sw)
move->bottom = move->left = 1;
else if (resize_pos == c->resize_w)
move->left = 1;
if (move->left)
c->geom.w = orig.w + (x0 - x1);
if (move->top)
c->geom.h = orig.h + (y0 - y1);
if (move->right) {
c->geom.w = orig.w - (x0 - x1);
c->geom.x = orig.x - (x0 - x1);
}
if (move->bottom) {
c->geom.h = orig.h - (y0 - y1);
c->geom.y = orig.y - (y0 - y1);
}
fix_size(c);
if (move->left)
c->geom.x = orig.x + orig.w - c->geom.w;
if (move->top)
c->geom.y = orig.y + orig.h - c->geom.h;
if (move->right)
c->geom.x = orig.x;
if (move->bottom)
c->geom.y = orig.y;
fix_size(c);
if (c->geom.w != now.w || c->geom.h != now.h) {
redraw_frame(c, None);
if (c->shaped)
set_shape(c);
send_config(c);
}
}
/*
* If the window in question has a ResizeInc hint, then it wants to be resized
* in multiples of some (x,y). We constrain the values in c->geom based on that
* and any min/max size hints.
*/
void
fix_size(client_t *c)
{
int width_inc, height_inc;
int base_width, base_height;
if (c->size_hints.flags & PMinSize) {
if (c->geom.w < c->size_hints.min_width)
c->geom.w = c->size_hints.min_width;
if (c->geom.h < c->size_hints.min_height)
c->geom.h = c->size_hints.min_height;
}
if (c->size_hints.flags & PMaxSize) {
if (c->geom.w > c->size_hints.max_width)
c->geom.w = c->size_hints.max_width;
if (c->geom.h > c->size_hints.max_height)
c->geom.h = c->size_hints.max_height;
}
if (c->size_hints.flags & PResizeInc) {
width_inc = c->size_hints.width_inc ?
c->size_hints.width_inc : 1;
height_inc = c->size_hints.height_inc ?
c->size_hints.height_inc : 1;
base_width = (c->size_hints.flags & PBaseSize) ?
c->size_hints.base_width :
((c->size_hints.flags & PMinSize) ?
c->size_hints.min_width : 0);
base_height = (c->size_hints.flags & PBaseSize) ?
c->size_hints.base_height :
(c->size_hints.flags & PMinSize) ?
c->size_hints.min_height : 0;
c->geom.w -= (c->geom.w - base_width) % width_inc;
c->geom.h -= (c->geom.h - base_height) % height_inc;
}
}
/* make sure a frame fits on the screen */
void
constrain_frame(client_t *c)
{
strut_t s = { 0 };
int h, w, delta;
if (c->state & STATE_FULLSCREEN)
return;
#ifdef DEBUG
dump_geom(c, c->geom, "constrain_frame initial");
#endif
recalc_frame(c);
fix_size(c);
collect_struts(c, &s);
if (c->frame_geom.x < s.left) {
delta = s.left - c->frame_geom.x;
c->frame_geom.x += delta;
c->geom.x += delta;
}
if (c->frame_geom.y < s.top) {
delta = s.top - c->frame_geom.y;