forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.c
More file actions
1400 lines (1199 loc) · 39.5 KB
/
Copy pathmouse.c
File metadata and controls
1400 lines (1199 loc) · 39.5 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
/*
* mouse.c - mouse managing
*
* Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <math.h>
#include "common/tokenize.h"
#include "screen.h"
#include "tag.h"
#include "titlebar.h"
#include "layouts/floating.h"
#include "layouts/tile.h"
#include "layouts/magnifier.h"
#define MOUSEMASK (XCB_EVENT_MASK_BUTTON_PRESS \
| XCB_EVENT_MASK_BUTTON_RELEASE \
| XCB_EVENT_MASK_POINTER_MOTION)
extern awesome_t globalconf;
DO_LUA_NEW(extern, button_t, button, "button", button_ref)
DO_LUA_GC(button_t, button, "button", button_unref)
DO_LUA_EQ(button_t, button, "button")
/** Define corners. */
typedef enum
{
AutoCorner,
TopRightCorner,
TopLeftCorner,
BottomLeftCorner,
BottomRightCorner
} corner_t;
/** Convert a corner name into a corner type.
* \param str A string.
* \param len String length.
* \return A corner type.
*/
static corner_t
a_strtocorner(const char *str, size_t len)
{
switch (a_tokenize(str, len))
{
case A_TK_BOTTOMRIGHT: return BottomRightCorner;
case A_TK_BOTTOMLEFT: return BottomLeftCorner;
case A_TK_TOPLEFT: return TopLeftCorner;
case A_TK_TOPRIGHT: return TopRightCorner;
default: return AutoCorner;
}
}
/** Delete a button.
* \param button The button to destroy.
*/
void
button_delete(button_t **button)
{
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
p_delete(button);
}
/** Snap an area to the outside of an area.
* \param geometry geometry of the area to snap
* \param snap_geometry geometry of snapping area
* \param snap snap trigger in pixel
* \return snapped geometry
*/
static area_t
mouse_snapclienttogeometry_outside(area_t geometry, area_t snap_geometry, int snap)
{
if(geometry.x < snap + snap_geometry.x + snap_geometry.width
&& geometry.x > snap_geometry.x + snap_geometry.width)
geometry.x = snap_geometry.x + snap_geometry.width;
else if(geometry.x + geometry.width < snap_geometry.x
&& geometry.x + geometry.width > snap_geometry.x - snap)
geometry.x = snap_geometry.x - geometry.width;
if(geometry.y < snap + snap_geometry.y + snap_geometry.height
&& geometry.y > snap_geometry.y + snap_geometry.height)
geometry.y = snap_geometry.y + snap_geometry.height;
else if(geometry.y + geometry.height < snap_geometry.y
&& geometry.y + geometry.height > snap_geometry.y - snap)
geometry.y = snap_geometry.y - geometry.height;
return geometry;
}
/** Snap an area to the inside of an area.
* \param geometry geometry of the area to snap
* \param snap_geometry geometry of snapping area
* \param snap snap trigger in pixel
* \return snapped geometry
*/
static area_t
mouse_snapclienttogeometry_inside(area_t geometry, area_t snap_geometry, int snap)
{
if(abs(geometry.x) < snap + snap_geometry.x && geometry.x > snap_geometry.x)
geometry.x = snap_geometry.x;
else if(abs((snap_geometry.x + snap_geometry.width) - (geometry.x + geometry.width))
< snap)
geometry.x = snap_geometry.x + snap_geometry.width - geometry.width;
if(abs(geometry.y) < snap + snap_geometry.y && geometry.y > snap_geometry.y)
geometry.y = snap_geometry.y;
else if(abs((snap_geometry.y + snap_geometry.height) - (geometry.y + geometry.height))
< snap)
geometry.y = snap_geometry.y + snap_geometry.height - geometry.height;
return geometry;
}
/** Snap a client with a future geometry to the screen and other clients.
* \param c The client.
* \param geometry Geometry the client will get.
* \param snap The maximum distance in pixels to trigger a "snap".
* \return Geometry to set to the client.
*/
static area_t
mouse_snapclient(client_t *c, area_t geometry, int snap)
{
client_t *snapper;
area_t snapper_geometry;
area_t screen_geometry =
screen_area_get(c->screen,
&globalconf.screens[c->screen].wiboxes,
&globalconf.screens[c->screen].padding,
false);
area_t screen_geometry_barless =
screen_area_get(c->screen,
NULL,
&globalconf.screens[c->screen].padding,
false);
geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
geometry =
mouse_snapclienttogeometry_inside(geometry, screen_geometry, snap);
geometry =
mouse_snapclienttogeometry_inside(geometry, screen_geometry_barless, snap);
for(snapper = globalconf.clients; snapper; snapper = snapper->next)
if(snapper != c && client_isvisible(snapper, c->screen))
{
snapper_geometry = snapper->geometry;
snapper_geometry = titlebar_geometry_add(snapper->titlebar, snapper->border, snapper_geometry);
geometry =
mouse_snapclienttogeometry_outside(geometry,
snapper_geometry,
snap);
}
return titlebar_geometry_remove(c->titlebar, c->border, geometry);
}
/** Set coordinates to a corner of an area.
*
* \param a The area.
* \param[in,out] x The x coordinate.
* \param[in,out] y The y coordinate.
* \param corner The corner to snap to.
* \return The corner the coordinates have been set to. If corner != AutoCorner
* this is always equal to \em corner.
*
* \todo rename/move this is still awkward and might be useful somewhere else.
*/
static corner_t
mouse_snap_to_corner(area_t a, int *x, int *y, corner_t corner)
{
int top, bottom, left, right;
top = AREA_TOP(a);
bottom = AREA_BOTTOM(a);
left = AREA_LEFT(a);
right = AREA_RIGHT(a);
/* figure out the nearser corner */
if(corner == AutoCorner)
{
if(abs(top - *y) < abs(bottom - *y))
{
if(abs(left - *x) < abs(right - *x))
corner = TopLeftCorner;
else
corner = TopRightCorner;
}
else
{
if(abs(left - *x) < abs(right - *x))
corner = BottomLeftCorner;
else
corner = BottomRightCorner;
}
}
switch(corner)
{
case TopRightCorner:
*x = right;
*y = top;
break;
case TopLeftCorner:
*x = left;
*y = top;
break;
case BottomLeftCorner:
*x = left;
*y = bottom;
break;
case BottomRightCorner:
*x = right;
*y = bottom;
break;
default:
break;
}
return corner;
}
/** Redraw the infobox.
* \param sw The simple window.
* \param geometry The geometry to use for the box.
* \param border The client border size.
*/
static void
mouse_infobox_draw(simple_window_t *sw, area_t geometry, int border)
{
area_t draw_geometry = { 0, 0, sw->ctx.width, sw->ctx.height };
char size[64];
size_t len;
xcolor_t color_without_alpha = globalconf.colors.bg;
color_without_alpha.alpha = 0xffff;
len = snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
geometry.width, geometry.height, geometry.x, geometry.y);
draw_rectangle(&sw->ctx, draw_geometry, 1.0, true, &color_without_alpha);
draw_text(&sw->ctx, globalconf.font, PANGO_ELLIPSIZE_NONE, PANGO_WRAP_WORD, draw_geometry, size, len, NULL);
simplewindow_move(sw,
geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);
simplewindow_refresh_pixmap(sw);
xcb_flush(globalconf.connection);
}
#define MOUSE_INFOBOX_STRING_DEFAULT "0000x0000+0000+0000"
/** Initialize the infobox window.
* \param sw The simple window to init.
* \param phys_screen Physical screen number.
* \param border Border size of the client.
* \param geometry Client geometry.
* \return The simple window.
*/
static void
mouse_infobox_new(simple_window_t *sw, int phys_screen, int border, area_t geometry)
{
area_t geom;
draw_parser_data_t pdata;
draw_parser_data_init(&pdata);
geom = draw_text_extents(globalconf.font,
MOUSE_INFOBOX_STRING_DEFAULT,
sizeof(MOUSE_INFOBOX_STRING_DEFAULT)-1,
&pdata);
geom.x = geometry.x + ((2 * border + geometry.width) - geom.width) / 2;
geom.y = geometry.y + ((2 * border + geometry.height) - geom.height) / 2;
p_clear(sw, 1);
simplewindow_init(sw, phys_screen, geom, 0, East,
&globalconf.colors.fg, &globalconf.colors.bg);
xcb_map_window(globalconf.connection, sw->window);
mouse_infobox_draw(sw, geometry, border);
draw_parser_data_wipe(&pdata);
}
/** Get the pointer position.
* \param window The window to get position on.
* \param x will be set to the Pointer-x-coordinate relative to window
* \param y will be set to the Pointer-y-coordinate relative to window
* \param mask will be set to the current buttons state
* \return true on success, false if an error occured
**/
static bool
mouse_query_pointer(xcb_window_t window, int *x, int *y, uint16_t *mask)
{
xcb_query_pointer_cookie_t query_ptr_c;
xcb_query_pointer_reply_t *query_ptr_r;
query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
if(!query_ptr_r || !query_ptr_r->same_screen)
return false;
*x = query_ptr_r->win_x;
*y = query_ptr_r->win_y;
if (mask)
*mask = query_ptr_r->mask;
p_delete(&query_ptr_r);
return true;
}
/** Get the pointer position on the screen.
* \param screen This will be set to the screen number the mouse is on.
* \param x This will be set to the Pointer-x-coordinate relative to window.
* \param y This will be set to the Pointer-y-coordinate relative to window.
* \param mask This will be set to the current buttons state.
* \return True on success, false if an error occured.
*/
static bool
mouse_query_pointer_root(int *s, int *x, int *y, uint16_t *mask)
{
for(int screen = 0;
screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
screen++)
{
xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
if(mouse_query_pointer(root, x, y, mask))
{
*s = screen;
return true;
}
}
return false;
}
/** Grab the Pointer.
* \param window The window grabbed.
* \param cursor The cursor to display (see struct.h CurNormal, CurResize, etc).
* \return True on success, false if an error occured.
*/
static bool
mouse_grab_pointer(xcb_window_t window, size_t cursor)
{
xcb_grab_pointer_cookie_t grab_ptr_c;
xcb_grab_pointer_reply_t *grab_ptr_r;
if(cursor >= CurLast)
cursor = CurNormal;
grab_ptr_c = xcb_grab_pointer_unchecked(globalconf.connection, false, window,
MOUSEMASK, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
window, globalconf.cursor[cursor], XCB_CURRENT_TIME);
grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL);
if(!grab_ptr_r)
return false;
p_delete(&grab_ptr_r);
return true;
}
/** Ungrab the Pointer
*/
static inline void
mouse_ungrab_pointer(void)
{
xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
}
/** Set the pointer position.
* \param window The destination window.
* \param x X-coordinate inside window.
* \param y Y-coordinate inside window.
*/
static inline void
mouse_warp_pointer(xcb_window_t window, int x, int y)
{
xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
0, 0, 0, 0, x, y );
}
/** Utility function to help with mouse-dragging
*
* \param x set to x-coordinate of the last event on return
* \param y set to y-coordinate of the last event on return
* \return true if an motion event was received
* false if an button release event was received
*/
static bool
mouse_track_mouse_drag(int *x, int *y)
{
xcb_generic_event_t *ev;
xcb_motion_notify_event_t *ev_motion;
xcb_button_release_event_t *ev_button;
while(true)
while((ev = xcb_wait_for_event(globalconf.connection)))
switch((ev->response_type & 0x7F))
{
case XCB_MOTION_NOTIFY:
ev_motion = (xcb_motion_notify_event_t*) ev;
*x = ev_motion->event_x;
*y = ev_motion->event_y;
p_delete(&ev);
return true;
case XCB_BUTTON_RELEASE:
ev_button = (xcb_button_release_event_t*) ev;
*x = ev_button->event_x;
*y = ev_button->event_y;
p_delete(&ev);
return false;
default:
xcb_event_handle(&globalconf.evenths, ev);
p_delete(&ev);
break;
}
}
/** Get the client that contains the pointer.
*
* \return The client that contains the pointer or NULL.
*/
static client_t *
mouse_get_client_under_pointer(int phys_screen)
{
xcb_window_t root;
xcb_query_pointer_cookie_t query_ptr_c;
xcb_query_pointer_reply_t *query_ptr_r;
client_t *c = NULL;
root = xutil_screen_get(globalconf.connection, phys_screen)->root;
query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, root);
query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
if(query_ptr_r)
{
c = client_getbywin(query_ptr_r->child);
p_delete(&query_ptr_r);
}
return c;
}
/** Move the focused window with the mouse.
* \param c The client.
* \param snap The maximum distance in pixels to trigger a "snap".
* \param infobox Enable or disable the infobox.
*/
static void
mouse_client_move(client_t *c, int snap, bool infobox)
{
/* current mouse postion */
int mouse_x, mouse_y;
/* last mouse position */
int last_x = 0, last_y = 0;
/* current layout */
layout_t *layout;
/* the infobox */
simple_window_t sw;
/* the root window */
xcb_window_t root;
layout = layout_get_current(c->screen);
root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
/* get current pointer position */
mouse_query_pointer(root, &last_x, &last_y, NULL);
/* grab pointer */
if(c->isfullscreen
|| c->type == WINDOW_TYPE_DESKTOP
|| c->type == WINDOW_TYPE_SPLASH
|| c->type == WINDOW_TYPE_DOCK
|| !mouse_grab_pointer(root, CurMove))
return;
if(infobox && (client_isfloating(c) || layout == layout_floating))
mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
else
infobox = false;
/* for each motion event */
while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
if(client_isfloating(c) || layout == layout_floating)
{
area_t geometry;
/* calc new geometry */
geometry = c->geometry;
geometry.x += (mouse_x - last_x);
geometry.y += (mouse_y - last_y);
/* snap and move */
geometry = mouse_snapclient(c, geometry, snap);
c->ismoving = true;
client_resize(c, geometry, false);
xcb_flush(globalconf.connection);
c->ismoving = false;
/* draw the infobox */
if(infobox)
mouse_infobox_draw(&sw, c->geometry, c->border);
/* refresh live */
wibox_refresh();
xcb_flush(globalconf.connection);
/* keep track */
last_x = mouse_x;
last_y = mouse_y;
}
else
{
int newscreen;
client_t *target;
/* client moved to another screen? */
newscreen = screen_getbycoord(c->screen, mouse_x, mouse_y);
if(newscreen != c->screen)
{
screen_client_moveto(c, newscreen, true, true);
globalconf.screens[c->screen].need_arrange = true;
globalconf.screens[newscreen].need_arrange = true;
layout_refresh();
wibox_refresh();
xcb_flush(globalconf.connection);
}
/* find client to swap with */
target = mouse_get_client_under_pointer(c->phys_screen);
/* swap position */
if(target && target != c && !target->isfloating)
{
client_list_swap(&globalconf.clients, c, target);
globalconf.screens[c->screen].need_arrange = true;
if(globalconf.hooks.clients != LUA_REFNIL)
luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
layout_refresh();
wibox_refresh();
xcb_flush(globalconf.connection);
}
}
/* ungrab pointer */
xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
/* free the infobox */
if(infobox)
simplewindow_wipe(&sw);
}
/** Resize a floating client with the mouse.
* \param c The client to resize.
* \param corner The corner to resize with.
* \param infobox Enable or disable the infobox.
*/
static void
mouse_client_resize_floating(client_t *c, corner_t corner, bool infobox)
{
xcb_screen_t *screen;
/* one corner of the client has a fixed position */
int fixed_x, fixed_y;
/* the other is moved with the mouse */
int mouse_x = 0, mouse_y = 0;
/* the infobox */
simple_window_t sw;
size_t cursor = CurResize;
int top, bottom, left, right;
/* do not resize fixed client */
if(client_isfixed(c))
return;
screen = xutil_screen_get(globalconf.connection, c->phys_screen);
/* get current mouse position */
mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
top = c->geometry.y;
bottom = top + c->geometry.height;
left = c->geometry.x;
right = left + c->geometry.width;
/* figure out which corner to move */
corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
/* the opposite corner is fixed */
fixed_x = (mouse_x == left) ? right : left;
fixed_y = (mouse_y == top) ? bottom : top;
/* select cursor */
switch(corner)
{
default:
cursor = CurTopLeft;
break;
case TopRightCorner:
cursor = CurTopRight;
break;
case BottomLeftCorner:
cursor = CurBotLeft;
break;
case BottomRightCorner:
cursor = CurBotRight;
break;
}
/* grab the pointer */
if(!mouse_grab_pointer(screen->root, cursor))
return;
/* set pointer to the moveable corner */
mouse_warp_pointer(screen->root, mouse_x, mouse_y);
/* create the infobox */
if(infobox)
mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
/* for each motion event */
while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
{
/* new client geometry */
area_t geo = { .x = MIN(fixed_x, mouse_x),
.y = MIN(fixed_y, mouse_y),
.width = (MAX(fixed_x, mouse_x) - MIN(fixed_x, mouse_x)),
.height = (MAX(fixed_y, mouse_y) - MIN(fixed_y, mouse_y)) };
/* new moveable corner */
corner_t new_corner;
if(mouse_x == AREA_LEFT(geo))
new_corner = (mouse_y == AREA_TOP(geo)) ? TopLeftCorner : BottomLeftCorner;
else
new_corner = (mouse_y == AREA_TOP(geo)) ? TopRightCorner : BottomRightCorner;
/* update cursor */
if(corner != new_corner)
{
corner = new_corner;
switch(corner)
{
default: cursor = CurTopLeft; break;
case TopRightCorner: cursor = CurTopRight; break;
case BottomLeftCorner: cursor = CurBotLeft; break;
case BottomRightCorner: cursor = CurBotRight; break;
}
xcb_change_active_pointer_grab(globalconf.connection, globalconf.cursor[cursor],
XCB_CURRENT_TIME, MOUSEMASK);
}
if(c->hassizehints && c->honorsizehints)
{
int dx, dy;
/* apply size hints */
geo = client_geometry_hints(c, geo);
/* get the nonmoveable corner back onto fixed_x,fixed_y */
switch(corner)
{
default /* TopLeftCorner */:
dx = fixed_x - AREA_RIGHT(geo);
dy = fixed_y - AREA_BOTTOM(geo);
break;
case TopRightCorner:
dx = fixed_x - AREA_LEFT(geo);
dy = fixed_y - AREA_BOTTOM(geo);
break;
case BottomRightCorner:
dx = fixed_x - AREA_LEFT(geo);
dy = fixed_y - AREA_TOP(geo);
break;
case BottomLeftCorner:
dx = fixed_x - AREA_RIGHT(geo);
dy = fixed_y - AREA_TOP(geo);
break;
}
geo.x += dx;
geo.y += dy;
}
/* resize the client */
client_resize(c, geo, false);
/* refresh live */
wibox_refresh();
xcb_flush(globalconf.connection);
/* draw the infobox */
if(infobox)
mouse_infobox_draw(&sw, c->geometry, c->border);
}
/* relase pointer */
mouse_ungrab_pointer();
/* free the infobox */
if(infobox)
simplewindow_wipe(&sw);
}
/** Resize the master column/row of a tiled layout
* \param c A client on the tag/layout to resize.
*/
static void
mouse_client_resize_tiled(client_t *c)
{
xcb_screen_t *screen;
/* screen area modulo wibox */
area_t area;
/* current tag */
tag_t *tag;
/* current layout */
layout_t *layout;
int mouse_x = 0, mouse_y = 0;
size_t cursor = CurResize;
screen = xutil_screen_get(globalconf.connection, c->phys_screen);
tag = tags_get_current(c->screen)[0];
layout = tag->layout;
area = screen_area_get(tag->screen,
&globalconf.screens[tag->screen].wiboxes,
&globalconf.screens[tag->screen].padding,
true);
mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
/* select initial pointer position */
if(layout == layout_tile)
{
mouse_x = area.x + area.width * tag->mwfact;
cursor = CurResizeH;
}
else if(layout == layout_tileleft)
{
mouse_x = area.x + area.width * (1. - tag->mwfact);
cursor = CurResizeH;
}
else if(layout == layout_tilebottom)
{
mouse_y = area.y + area.height * tag->mwfact;
cursor = CurResizeV;
}
else if(layout == layout_tiletop)
{
mouse_y = area.y + area.height * (1. - tag->mwfact);
cursor = CurResizeV;
}
else
return;
/* grab the pointer */
if(!mouse_grab_pointer(screen->root, cursor))
return;
/* set pointer to the moveable border */
mouse_warp_pointer(screen->root, mouse_x, mouse_y);
xcb_flush(globalconf.connection);
/* for each motion event */
while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
{
double mwfact = 0, fact_x, fact_y;
/* calculate new master / rest ratio */
fact_x = (double) (mouse_x - area.x) / area.width;
fact_y = (double) (mouse_y - area.y) / area.height;
if(layout == layout_tile)
mwfact = fact_x;
else if(layout == layout_tileleft)
mwfact = 1. - fact_x;
else if(layout == layout_tilebottom)
mwfact = fact_y;
else if(layout == layout_tiletop)
mwfact = 1. - fact_y;
/* keep mwfact within reasonable bounds */
mwfact = MIN(MAX( 0.01, mwfact), 0.99);
/* refresh layout */
if(fabs(tag->mwfact - mwfact) >= 0.01)
{
tag->mwfact = mwfact;
globalconf.screens[tag->screen].need_arrange = true;
layout_refresh();
wibox_refresh();
xcb_flush(globalconf.connection);
}
}
/* relase pointer */
mouse_ungrab_pointer();
}
/** Resize the master client in mangifier layout
* \param c The client to resize.
* \param infobox Enable or disable the infobox.
*/
static void
mouse_client_resize_magnified(client_t *c, bool infobox)
{
/* screen area modulo wibox */
area_t area;
/* center of area */
int center_x, center_y;
/* max. distance from the center */
double maxdist;
/* mouse position */
int mouse_x = 0, mouse_y = 0;
/* cursor while grabbing */
size_t cursor = CurResize;
corner_t corner = AutoCorner;
/* current tag */
tag_t *tag;
/* the infobox */
simple_window_t sw;
xcb_window_t root;
tag = tags_get_current(c->screen)[0];
root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
area = screen_area_get(tag->screen,
&globalconf.screens[tag->screen].wiboxes,
&globalconf.screens[tag->screen].padding,
true);
center_x = area.x + (round(area.width / 2.));
center_y = area.y + (round(area.height / 2.));
maxdist = round(sqrt((area.width*area.width) + (area.height*area.height)) / 2.);
root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
if(!mouse_query_pointer(root, &mouse_x, &mouse_y, NULL))
return;
/* select corner */
corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
/* select cursor */
switch(corner)
{
default:
cursor = CurTopLeft;
break;
case TopRightCorner:
cursor = CurTopRight;
break;
case BottomLeftCorner:
cursor = CurBotLeft;
break;
case BottomRightCorner:
cursor = CurBotRight;
break;
}
/* grab pointer */
if(!mouse_grab_pointer(root, cursor))
return;
/* move pointer to corner */
mouse_warp_pointer(root, mouse_x, mouse_y);
/* create the infobox */
if(infobox)
mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
/* for each motion event */
while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
{
/* \todo keep pointer on screen diagonals */
double mwfact, dist, dx, dy;
/* calc distance from the center */
dx = center_x - mouse_x;
dy = center_y - mouse_y;
dist = sqrt((dx * dx) + (dy * dy));
/* new master/rest ratio */
mwfact = (dist * dist) / (maxdist * maxdist);
/* keep mwfact within reasonable bounds */
mwfact = MIN(MAX( 0.01, mwfact), 0.99);
/* refresh the layout */
if(fabs(tag->mwfact - mwfact) >= 0.01)
{
tag->mwfact = mwfact;
globalconf.screens[tag->screen].need_arrange = true;
layout_refresh();
wibox_refresh();
xcb_flush(globalconf.connection);
}
/* draw the infobox */
if(infobox)
mouse_infobox_draw(&sw, c->geometry, c->border);
}
/* ungrab pointer */
mouse_ungrab_pointer();
/* free the infobox */
if(infobox)
simplewindow_wipe(&sw);
}
/** Resize a client with the mouse.
* \param c The client to resize.
* \param corner The corner to use.
* \param infobox Enable or disable the info box.
*/
static void
mouse_client_resize(client_t *c, corner_t corner, bool infobox)
{
int n, screen;
tag_t **curtags;
layout_t *layout;
xcb_screen_t *s;
if(c->isfullscreen
|| c->type == WINDOW_TYPE_DESKTOP
|| c->type == WINDOW_TYPE_SPLASH
|| c->type == WINDOW_TYPE_DOCK)
return;
curtags = tags_get_current(c->screen);
layout = curtags[0]->layout;
s = xutil_screen_get(globalconf.connection, c->phys_screen);
/* only handle floating, tiled and magnifier layouts */
if(layout == layout_floating || client_isfloating(c))
mouse_client_resize_floating(c, corner, infobox);
else if(layout == layout_tile || layout == layout_tileleft
|| layout == layout_tilebottom || layout == layout_tiletop)
{
screen = c->screen;
for(n = 0, c = globalconf.clients; c; c = c->next)
if(IS_TILED(c, screen))
n++;
/* only masters on this screen? */
if(n <= curtags[0]->nmaster)
goto bailout;
/* no tiled clients on this screen? */
for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
if(!c)
goto bailout;
mouse_client_resize_tiled(c);
}
else if(layout == layout_magnifier)
mouse_client_resize_magnified(c, infobox);
bailout:
p_delete(&curtags);
}
/** Resize a client with mouse.
* \param L The Lua VM state.
*
* \luastack
* \lvalue A client.
* \lparam An optional table with keys: `corner', such as bottomleft,