2 * mouse.c - mouse managing
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "common/tokenize.h"
31 #include "layouts/floating.h"
32 #include "layouts/tile.h"
33 #include "layouts/magnifier.h"
35 #define MOUSEMASK (XCB_EVENT_MASK_BUTTON_PRESS \
36 | XCB_EVENT_MASK_BUTTON_RELEASE \
37 | XCB_EVENT_MASK_POINTER_MOTION)
39 extern awesome_t globalconf
;
41 DO_LUA_NEW(extern, button_t
, button
, "button", button_ref
)
42 DO_LUA_GC(button_t
, button
, "button", button_unref
)
43 DO_LUA_EQ(button_t
, button
, "button")
45 /** Define corners. */
55 /** Convert a corner name into a corner type.
56 * \param str A string.
57 * \param len String length.
58 * \return A corner type.
61 a_strtocorner(const char *str
, size_t len
)
63 switch (a_tokenize(str
, len
))
65 case A_TK_BOTTOMRIGHT
: return BottomRightCorner
;
66 case A_TK_BOTTOMLEFT
: return BottomLeftCorner
;
67 case A_TK_TOPLEFT
: return TopLeftCorner
;
68 case A_TK_TOPRIGHT
: return TopRightCorner
;
69 default: return AutoCorner
;
74 * \param button The button to destroy.
77 button_delete(button_t
**button
)
79 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->press
);
80 luaL_unref(globalconf
.L
, LUA_REGISTRYINDEX
, (*button
)->release
);
84 /** Snap an area to the outside of an area.
85 * \param geometry geometry of the area to snap
86 * \param snap_geometry geometry of snapping area
87 * \param snap snap trigger in pixel
88 * \return snapped geometry
91 mouse_snapclienttogeometry_outside(area_t geometry
, area_t snap_geometry
, int snap
)
93 if(geometry
.x
< snap
+ snap_geometry
.x
+ snap_geometry
.width
94 && geometry
.x
> snap_geometry
.x
+ snap_geometry
.width
)
95 geometry
.x
= snap_geometry
.x
+ snap_geometry
.width
;
96 else if(geometry
.x
+ geometry
.width
< snap_geometry
.x
97 && geometry
.x
+ geometry
.width
> snap_geometry
.x
- snap
)
98 geometry
.x
= snap_geometry
.x
- geometry
.width
;
100 if(geometry
.y
< snap
+ snap_geometry
.y
+ snap_geometry
.height
101 && geometry
.y
> snap_geometry
.y
+ snap_geometry
.height
)
102 geometry
.y
= snap_geometry
.y
+ snap_geometry
.height
;
103 else if(geometry
.y
+ geometry
.height
< snap_geometry
.y
104 && geometry
.y
+ geometry
.height
> snap_geometry
.y
- snap
)
105 geometry
.y
= snap_geometry
.y
- geometry
.height
;
110 /** Snap an area to the inside of an area.
111 * \param geometry geometry of the area to snap
112 * \param snap_geometry geometry of snapping area
113 * \param snap snap trigger in pixel
114 * \return snapped geometry
117 mouse_snapclienttogeometry_inside(area_t geometry
, area_t snap_geometry
, int snap
)
119 if(abs(geometry
.x
) < snap
+ snap_geometry
.x
&& geometry
.x
> snap_geometry
.x
)
120 geometry
.x
= snap_geometry
.x
;
121 else if(abs((snap_geometry
.x
+ snap_geometry
.width
) - (geometry
.x
+ geometry
.width
))
123 geometry
.x
= snap_geometry
.x
+ snap_geometry
.width
- geometry
.width
;
124 if(abs(geometry
.y
) < snap
+ snap_geometry
.y
&& geometry
.y
> snap_geometry
.y
)
125 geometry
.y
= snap_geometry
.y
;
126 else if(abs((snap_geometry
.y
+ snap_geometry
.height
) - (geometry
.y
+ geometry
.height
))
128 geometry
.y
= snap_geometry
.y
+ snap_geometry
.height
- geometry
.height
;
133 /** Snap a client with a future geometry to the screen and other clients.
134 * \param c The client.
135 * \param geometry Geometry the client will get.
136 * \param snap The maximum distance in pixels to trigger a "snap".
137 * \return Geometry to set to the client.
140 mouse_snapclient(client_t
*c
, area_t geometry
, int snap
)
143 area_t snapper_geometry
;
144 area_t screen_geometry
=
145 screen_area_get(c
->screen
,
146 &globalconf
.screens
[c
->screen
].wiboxes
,
147 &globalconf
.screens
[c
->screen
].padding
,
150 area_t screen_geometry_barless
=
151 screen_area_get(c
->screen
,
153 &globalconf
.screens
[c
->screen
].padding
,
156 geometry
= titlebar_geometry_add(c
->titlebar
, c
->border
, geometry
);
159 mouse_snapclienttogeometry_inside(geometry
, screen_geometry
, snap
);
162 mouse_snapclienttogeometry_inside(geometry
, screen_geometry_barless
, snap
);
164 for(snapper
= globalconf
.clients
; snapper
; snapper
= snapper
->next
)
165 if(snapper
!= c
&& client_isvisible(snapper
, c
->screen
))
167 snapper_geometry
= snapper
->geometry
;
168 snapper_geometry
= titlebar_geometry_add(snapper
->titlebar
, snapper
->border
, snapper_geometry
);
170 mouse_snapclienttogeometry_outside(geometry
,
175 return titlebar_geometry_remove(c
->titlebar
, c
->border
, geometry
);
178 /** Set coordinates to a corner of an area.
181 * \param[in,out] x The x coordinate.
182 * \param[in,out] y The y coordinate.
183 * \param corner The corner to snap to.
184 * \return The corner the coordinates have been set to. If corner != AutoCorner
185 * this is always equal to \em corner.
187 * \todo rename/move this is still awkward and might be useful somewhere else.
190 mouse_snap_to_corner(area_t a
, int *x
, int *y
, corner_t corner
)
192 int top
, bottom
, left
, right
;
195 bottom
= AREA_BOTTOM(a
);
197 right
= AREA_RIGHT(a
);
199 /* figure out the nearser corner */
200 if(corner
== AutoCorner
)
202 if(abs(top
- *y
) < abs(bottom
- *y
))
204 if(abs(left
- *x
) < abs(right
- *x
))
205 corner
= TopLeftCorner
;
207 corner
= TopRightCorner
;
211 if(abs(left
- *x
) < abs(right
- *x
))
212 corner
= BottomLeftCorner
;
214 corner
= BottomRightCorner
;
230 case BottomLeftCorner
:
235 case BottomRightCorner
:
247 /** Redraw the infobox.
248 * \param sw The simple window.
249 * \param geometry The geometry to use for the box.
250 * \param border The client border size.
253 mouse_infobox_draw(simple_window_t
*sw
, area_t geometry
, int border
)
255 area_t draw_geometry
= { 0, 0, sw
->ctx
.width
, sw
->ctx
.height
};
259 len
= snprintf(size
, sizeof(size
), "<text align=\"center\"/>%dx%d+%d+%d",
260 geometry
.width
, geometry
.height
, geometry
.x
, geometry
.y
);
261 draw_rectangle(&sw
->ctx
, draw_geometry
, 1.0, true, &globalconf
.colors
.bg
);
262 draw_text(&sw
->ctx
, globalconf
.font
, draw_geometry
, size
, len
, NULL
);
263 simplewindow_move(sw
,
264 geometry
.x
+ ((2 * border
+ geometry
.width
) - sw
->geometry
.width
) / 2,
265 geometry
.y
+ ((2 * border
+ geometry
.height
) - sw
->geometry
.height
) / 2);
266 simplewindow_refresh_pixmap(sw
);
267 xcb_flush(globalconf
.connection
);
270 #define MOUSE_INFOBOX_STRING_DEFAULT "0000x0000+0000+0000"
272 /** Initialize the infobox window.
273 * \param sw The simple window to init.
274 * \param phys_screen Physical screen number.
275 * \param border Border size of the client.
276 * \param geometry Client geometry.
277 * \return The simple window.
280 mouse_infobox_new(simple_window_t
*sw
, int phys_screen
, int border
, area_t geometry
)
283 draw_parser_data_t pdata
;
285 draw_parser_data_init(&pdata
);
287 geom
= draw_text_extents(globalconf
.default_screen
,
289 MOUSE_INFOBOX_STRING_DEFAULT
,
290 sizeof(MOUSE_INFOBOX_STRING_DEFAULT
)-1,
292 geom
.x
= geometry
.x
+ ((2 * border
+ geometry
.width
) - geom
.width
) / 2;
293 geom
.y
= geometry
.y
+ ((2 * border
+ geometry
.height
) - geom
.height
) / 2;
296 simplewindow_init(sw
, phys_screen
, geom
, 0, East
,
297 &globalconf
.colors
.fg
, &globalconf
.colors
.bg
);
299 xcb_map_window(globalconf
.connection
, sw
->window
);
300 mouse_infobox_draw(sw
, geometry
, border
);
302 draw_parser_data_wipe(&pdata
);
305 /** Get the pointer position.
306 * \param window The window to get position on.
307 * \param x will be set to the Pointer-x-coordinate relative to window
308 * \param y will be set to the Pointer-y-coordinate relative to window
309 * \param mask will be set to the current buttons state
310 * \return true on success, false if an error occured
313 mouse_query_pointer(xcb_window_t window
, int *x
, int *y
, uint16_t *mask
)
315 xcb_query_pointer_cookie_t query_ptr_c
;
316 xcb_query_pointer_reply_t
*query_ptr_r
;
318 query_ptr_c
= xcb_query_pointer_unchecked(globalconf
.connection
, window
);
319 query_ptr_r
= xcb_query_pointer_reply(globalconf
.connection
, query_ptr_c
, NULL
);
321 if(!query_ptr_r
|| !query_ptr_r
->same_screen
)
324 *x
= query_ptr_r
->win_x
;
325 *y
= query_ptr_r
->win_y
;
327 *mask
= query_ptr_r
->mask
;
329 p_delete(&query_ptr_r
);
334 /** Get the pointer position on the screen.
335 * \param screen This will be set to the screen number the mouse is on.
336 * \param x This will be set to the Pointer-x-coordinate relative to window.
337 * \param y This will be set to the Pointer-y-coordinate relative to window.
338 * \param mask This will be set to the current buttons state.
339 * \return True on success, false if an error occured.
342 mouse_query_pointer_root(int *s
, int *x
, int *y
, uint16_t *mask
)
345 screen
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
348 xcb_window_t root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
350 if(mouse_query_pointer(root
, x
, y
, mask
))
359 /** Grab the Pointer.
360 * \param window The window grabbed.
361 * \param cursor The cursor to display (see struct.h CurNormal, CurResize, etc).
362 * \return True on success, false if an error occured.
365 mouse_grab_pointer(xcb_window_t window
, size_t cursor
)
367 xcb_grab_pointer_cookie_t grab_ptr_c
;
368 xcb_grab_pointer_reply_t
*grab_ptr_r
;
370 if(cursor
>= CurLast
)
373 grab_ptr_c
= xcb_grab_pointer_unchecked(globalconf
.connection
, false, window
,
374 MOUSEMASK
, XCB_GRAB_MODE_ASYNC
, XCB_GRAB_MODE_ASYNC
,
375 window
, globalconf
.cursor
[cursor
], XCB_CURRENT_TIME
);
376 grab_ptr_r
= xcb_grab_pointer_reply(globalconf
.connection
, grab_ptr_c
, NULL
);
381 p_delete(&grab_ptr_r
);
386 /** Ungrab the Pointer
389 mouse_ungrab_pointer(void)
391 xcb_ungrab_pointer(globalconf
.connection
, XCB_CURRENT_TIME
);
394 /** Set the pointer position.
395 * \param window The destination window.
396 * \param x X-coordinate inside window.
397 * \param y Y-coordinate inside window.
400 mouse_warp_pointer(xcb_window_t window
, int x
, int y
)
402 xcb_warp_pointer(globalconf
.connection
, XCB_NONE
, window
,
406 /** Utility function to help with mouse-dragging
408 * \param x set to x-coordinate of the last event on return
409 * \param y set to y-coordinate of the last event on return
410 * \return true if an motion event was received
411 * false if an button release event was received
414 mouse_track_mouse_drag(int *x
, int *y
)
416 xcb_generic_event_t
*ev
;
417 xcb_motion_notify_event_t
*ev_motion
;
418 xcb_button_release_event_t
*ev_button
;
421 while((ev
= xcb_wait_for_event(globalconf
.connection
)))
422 switch((ev
->response_type
& 0x7F))
425 case XCB_MOTION_NOTIFY
:
426 ev_motion
= (xcb_motion_notify_event_t
*) ev
;
427 *x
= ev_motion
->event_x
;
428 *y
= ev_motion
->event_y
;
432 case XCB_BUTTON_RELEASE
:
433 ev_button
= (xcb_button_release_event_t
*) ev
;
434 *x
= ev_button
->event_x
;
435 *y
= ev_button
->event_y
;
440 xcb_event_handle(&globalconf
.evenths
, ev
);
446 /** Get the client that contains the pointer.
448 * \return The client that contains the pointer or NULL.
451 mouse_get_client_under_pointer(int phys_screen
)
454 xcb_query_pointer_cookie_t query_ptr_c
;
455 xcb_query_pointer_reply_t
*query_ptr_r
;
458 root
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
460 query_ptr_c
= xcb_query_pointer_unchecked(globalconf
.connection
, root
);
461 query_ptr_r
= xcb_query_pointer_reply(globalconf
.connection
, query_ptr_c
, NULL
);
465 c
= client_getbywin(query_ptr_r
->child
);
466 p_delete(&query_ptr_r
);
472 /** Move the focused window with the mouse.
473 * \param c The client.
474 * \param snap The maximum distance in pixels to trigger a "snap".
475 * \param infobox Enable or disable the infobox.
478 mouse_client_move(client_t
*c
, int snap
, bool infobox
)
480 /* current mouse postion */
481 int mouse_x
, mouse_y
;
482 /* last mouse position */
483 int last_x
= 0, last_y
= 0;
488 /* the root window */
491 layout
= layout_get_current(c
->screen
);
492 root
= xutil_screen_get(globalconf
.connection
, c
->phys_screen
)->root
;
494 /* get current pointer position */
495 mouse_query_pointer(root
, &last_x
, &last_y
, NULL
);
499 || c
->type
== WINDOW_TYPE_DESKTOP
500 || c
->type
== WINDOW_TYPE_SPLASH
501 || c
->type
== WINDOW_TYPE_DOCK
502 || !mouse_grab_pointer(root
, CurMove
))
505 if(infobox
&& (client_isfloating(c
) || layout
== layout_floating
))
506 mouse_infobox_new(&sw
, c
->phys_screen
, c
->border
, c
->geometry
);
510 /* for each motion event */
511 while(mouse_track_mouse_drag(&mouse_x
, &mouse_y
))
512 if(client_isfloating(c
) || layout
== layout_floating
)
516 /* calc new geometry */
517 geometry
= c
->geometry
;
518 geometry
.x
+= (mouse_x
- last_x
);
519 geometry
.y
+= (mouse_y
- last_y
);
522 geometry
= mouse_snapclient(c
, geometry
, snap
);
524 client_resize(c
, geometry
, false);
525 xcb_flush(globalconf
.connection
);
528 /* draw the infobox */
530 mouse_infobox_draw(&sw
, c
->geometry
, c
->border
);
534 xcb_flush(globalconf
.connection
);
545 /* client moved to another screen? */
546 newscreen
= screen_getbycoord(c
->screen
, mouse_x
, mouse_y
);
547 if(newscreen
!= c
->screen
)
549 screen_client_moveto(c
, newscreen
, true, true);
550 globalconf
.screens
[c
->screen
].need_arrange
= true;
551 globalconf
.screens
[newscreen
].need_arrange
= true;
554 xcb_flush(globalconf
.connection
);
557 /* find client to swap with */
558 target
= mouse_get_client_under_pointer(c
->phys_screen
);
561 if(target
&& target
!= c
&& !target
->isfloating
)
563 client_list_swap(&globalconf
.clients
, c
, target
);
564 globalconf
.screens
[c
->screen
].need_arrange
= true;
565 luaA_dofunction(globalconf
.L
, globalconf
.hooks
.clients
, 0, 0);
568 xcb_flush(globalconf
.connection
);
573 xcb_ungrab_pointer(globalconf
.connection
, XCB_CURRENT_TIME
);
575 /* free the infobox */
577 simplewindow_wipe(&sw
);
581 /** Resize a floating client with the mouse.
582 * \param c The client to resize.
583 * \param corner The corner to resize with.
584 * \param infobox Enable or disable the infobox.
587 mouse_client_resize_floating(client_t
*c
, corner_t corner
, bool infobox
)
589 xcb_screen_t
*screen
;
590 /* one corner of the client has a fixed position */
591 int fixed_x
, fixed_y
;
592 /* the other is moved with the mouse */
593 int mouse_x
= 0, mouse_y
= 0;
596 size_t cursor
= CurResize
;
597 int top
, bottom
, left
, right
;
599 screen
= xutil_screen_get(globalconf
.connection
, c
->phys_screen
);
601 /* get current mouse position */
602 mouse_query_pointer(screen
->root
, &mouse_x
, &mouse_y
, NULL
);
605 bottom
= top
+ c
->geometry
.height
;
606 left
= c
->geometry
.x
;
607 right
= left
+ c
->geometry
.width
;
609 /* figure out which corner to move */
610 corner
= mouse_snap_to_corner(c
->geometry
, &mouse_x
, &mouse_y
, corner
);
612 /* the opposite corner is fixed */
613 fixed_x
= (mouse_x
== left
) ? right
: left
;
614 fixed_y
= (mouse_y
== top
) ? bottom
: top
;
623 cursor
= CurTopRight
;
625 case BottomLeftCorner
:
628 case BottomRightCorner
:
629 cursor
= CurBotRight
;
633 /* grab the pointer */
634 if(!mouse_grab_pointer(screen
->root
, cursor
))
637 /* set pointer to the moveable corner */
638 mouse_warp_pointer(screen
->root
, mouse_x
, mouse_y
);
640 /* create the infobox */
642 mouse_infobox_new(&sw
, c
->phys_screen
, c
->border
, c
->geometry
);
644 /* for each motion event */
645 while(mouse_track_mouse_drag(&mouse_x
, &mouse_y
))
647 /* new client geometry */
648 area_t geo
= { .x
= MIN(fixed_x
, mouse_x
),
649 .y
= MIN(fixed_y
, mouse_y
),
650 .width
= (MAX(fixed_x
, mouse_x
) - MIN(fixed_x
, mouse_x
)),
651 .height
= (MAX(fixed_y
, mouse_y
) - MIN(fixed_y
, mouse_y
)) };
652 /* new moveable corner */
655 if(mouse_x
== AREA_LEFT(geo
))
656 new_corner
= (mouse_y
== AREA_TOP(geo
)) ? TopLeftCorner
: BottomLeftCorner
;
658 new_corner
= (mouse_y
== AREA_TOP(geo
)) ? TopRightCorner
: BottomRightCorner
;
661 if(corner
!= new_corner
)
667 default: cursor
= CurTopLeft
; break;
668 case TopRightCorner
: cursor
= CurTopRight
; break;
669 case BottomLeftCorner
: cursor
= CurBotLeft
; break;
670 case BottomRightCorner
: cursor
= CurBotRight
; break;
673 xcb_change_active_pointer_grab(globalconf
.connection
, globalconf
.cursor
[cursor
],
674 XCB_CURRENT_TIME
, MOUSEMASK
);
677 if(c
->hassizehints
&& c
->honorsizehints
)
681 /* apply size hints */
682 geo
= client_geometry_hints(c
, geo
);
684 /* get the nonmoveable corner back onto fixed_x,fixed_y */
687 default /* TopLeftCorner */:
688 dx
= fixed_x
- AREA_RIGHT(geo
);
689 dy
= fixed_y
- AREA_BOTTOM(geo
);
692 dx
= fixed_x
- AREA_LEFT(geo
);
693 dy
= fixed_y
- AREA_BOTTOM(geo
);
695 case BottomRightCorner
:
696 dx
= fixed_x
- AREA_LEFT(geo
);
697 dy
= fixed_y
- AREA_TOP(geo
);
699 case BottomLeftCorner
:
700 dx
= fixed_x
- AREA_RIGHT(geo
);
701 dy
= fixed_y
- AREA_TOP(geo
);
709 /* resize the client */
710 client_resize(c
, geo
, false);
714 xcb_flush(globalconf
.connection
);
716 /* draw the infobox */
718 mouse_infobox_draw(&sw
, c
->geometry
, c
->border
);
722 mouse_ungrab_pointer();
724 /* free the infobox */
726 simplewindow_wipe(&sw
);
729 /** Resize the master column/row of a tiled layout
730 * \param c A client on the tag/layout to resize.
733 mouse_client_resize_tiled(client_t
*c
)
735 xcb_screen_t
*screen
;
736 /* screen area modulo wibox */
743 int mouse_x
= 0, mouse_y
= 0;
744 size_t cursor
= CurResize
;
746 screen
= xutil_screen_get(globalconf
.connection
, c
->phys_screen
);
747 tag
= tags_get_current(c
->screen
)[0];
748 layout
= tag
->layout
;
750 area
= screen_area_get(tag
->screen
,
751 &globalconf
.screens
[tag
->screen
].wiboxes
,
752 &globalconf
.screens
[tag
->screen
].padding
,
755 mouse_query_pointer(screen
->root
, &mouse_x
, &mouse_y
, NULL
);
757 /* select initial pointer position */
758 if(layout
== layout_tile
)
760 mouse_x
= area
.x
+ area
.width
* tag
->mwfact
;
763 else if(layout
== layout_tileleft
)
765 mouse_x
= area
.x
+ area
.width
* (1. - tag
->mwfact
);
768 else if(layout
== layout_tilebottom
)
770 mouse_y
= area
.y
+ area
.height
* tag
->mwfact
;
773 else if(layout
== layout_tiletop
)
775 mouse_y
= area
.y
+ area
.height
* (1. - tag
->mwfact
);
781 /* grab the pointer */
782 if(!mouse_grab_pointer(screen
->root
, cursor
))
785 /* set pointer to the moveable border */
786 mouse_warp_pointer(screen
->root
, mouse_x
, mouse_y
);
788 xcb_flush(globalconf
.connection
);
790 /* for each motion event */
791 while(mouse_track_mouse_drag(&mouse_x
, &mouse_y
))
793 double mwfact
= 0, fact_x
, fact_y
;
795 /* calculate new master / rest ratio */
796 fact_x
= (double) (mouse_x
- area
.x
) / area
.width
;
797 fact_y
= (double) (mouse_y
- area
.y
) / area
.height
;
799 if(layout
== layout_tile
)
801 else if(layout
== layout_tileleft
)
802 mwfact
= 1. - fact_x
;
803 else if(layout
== layout_tilebottom
)
805 else if(layout
== layout_tiletop
)
806 mwfact
= 1. - fact_y
;
808 /* keep mwfact within reasonable bounds */
809 mwfact
= MIN(MAX( 0.01, mwfact
), 0.99);
812 if(fabs(tag
->mwfact
- mwfact
) >= 0.01)
814 tag
->mwfact
= mwfact
;
815 globalconf
.screens
[tag
->screen
].need_arrange
= true;
818 xcb_flush(globalconf
.connection
);
823 mouse_ungrab_pointer();
826 /** Resize the master client in mangifier layout
827 * \param c The client to resize.
828 * \param infobox Enable or disable the infobox.
831 mouse_client_resize_magnified(client_t
*c
, bool infobox
)
833 /* screen area modulo wibox */
836 int center_x
, center_y
;
837 /* max. distance from the center */
840 int mouse_x
= 0, mouse_y
= 0;
841 /* cursor while grabbing */
842 size_t cursor
= CurResize
;
843 corner_t corner
= AutoCorner
;
850 tag
= tags_get_current(c
->screen
)[0];
852 root
= xutil_screen_get(globalconf
.connection
, c
->phys_screen
)->root
;
854 area
= screen_area_get(tag
->screen
,
855 &globalconf
.screens
[tag
->screen
].wiboxes
,
856 &globalconf
.screens
[tag
->screen
].padding
,
859 center_x
= area
.x
+ (round(area
.width
/ 2.));
860 center_y
= area
.y
+ (round(area
.height
/ 2.));
862 maxdist
= round(sqrt((area
.width
*area
.width
) + (area
.height
*area
.height
)) / 2.);
864 root
= xutil_screen_get(globalconf
.connection
, c
->phys_screen
)->root
;
866 if(!mouse_query_pointer(root
, &mouse_x
, &mouse_y
, NULL
))
870 corner
= mouse_snap_to_corner(c
->geometry
, &mouse_x
, &mouse_y
, corner
);
879 cursor
= CurTopRight
;
881 case BottomLeftCorner
:
884 case BottomRightCorner
:
885 cursor
= CurBotRight
;
890 if(!mouse_grab_pointer(root
, cursor
))
893 /* move pointer to corner */
894 mouse_warp_pointer(root
, mouse_x
, mouse_y
);
896 /* create the infobox */
898 mouse_infobox_new(&sw
, c
->phys_screen
, c
->border
, c
->geometry
);
900 /* for each motion event */
901 while(mouse_track_mouse_drag(&mouse_x
, &mouse_y
))
903 /* \todo keep pointer on screen diagonals */
904 double mwfact
, dist
, dx
, dy
;
906 /* calc distance from the center */
907 dx
= center_x
- mouse_x
;
908 dy
= center_y
- mouse_y
;
909 dist
= sqrt((dx
* dx
) + (dy
* dy
));
911 /* new master/rest ratio */
912 mwfact
= (dist
* dist
) / (maxdist
* maxdist
);
914 /* keep mwfact within reasonable bounds */
915 mwfact
= MIN(MAX( 0.01, mwfact
), 0.99);
917 /* refresh the layout */
918 if(fabs(tag
->mwfact
- mwfact
) >= 0.01)
920 tag
->mwfact
= mwfact
;
921 globalconf
.screens
[tag
->screen
].need_arrange
= true;
924 xcb_flush(globalconf
.connection
);
927 /* draw the infobox */
929 mouse_infobox_draw(&sw
, c
->geometry
, c
->border
);
933 mouse_ungrab_pointer();
935 /* free the infobox */
937 simplewindow_wipe(&sw
);
940 /** Resize a client with the mouse.
941 * \param c The client to resize.
942 * \param corner The corner to use.
943 * \param infobox Enable or disable the info box.
946 mouse_client_resize(client_t
*c
, corner_t corner
, bool infobox
)
954 || c
->type
== WINDOW_TYPE_DESKTOP
955 || c
->type
== WINDOW_TYPE_SPLASH
956 || c
->type
== WINDOW_TYPE_DOCK
)
959 curtags
= tags_get_current(c
->screen
);
960 layout
= curtags
[0]->layout
;
961 s
= xutil_screen_get(globalconf
.connection
, c
->phys_screen
);
963 /* only handle floating, tiled and magnifier layouts */
964 if(layout
== layout_floating
|| client_isfloating(c
))
965 mouse_client_resize_floating(c
, corner
, infobox
);
966 else if(layout
== layout_tile
|| layout
== layout_tileleft
967 || layout
== layout_tilebottom
|| layout
== layout_tiletop
)
970 for(n
= 0, c
= globalconf
.clients
; c
; c
= c
->next
)
971 if(IS_TILED(c
, screen
))
974 /* only masters on this screen? */
975 if(n
<= curtags
[0]->nmaster
)
978 /* no tiled clients on this screen? */
979 for(c
= globalconf
.clients
; c
&& !IS_TILED(c
, screen
); c
= c
->next
);
983 mouse_client_resize_tiled(c
);
985 else if(layout
== layout_magnifier
)
986 mouse_client_resize_magnified(c
, infobox
);
992 /** Resize a client with mouse.
993 * \param L The Lua VM state.
997 * \lparam An optional table with keys: `corner', such as bottomleft,
998 * topright, etc, to specify which corner to grab (default to auto) and
999 * `infobox' to enable or disable the coordinates and dimensions box (default to
1003 luaA_client_mouse_resize(lua_State
*L
)
1005 client_t
**c
= luaA_checkudata(L
, 1, "client");
1006 corner_t corner
= AutoCorner
;
1007 bool infobox
= true;
1011 if(lua_gettop(L
) == 2 && !lua_isnil(L
, 2))
1013 luaA_checktable(L
, 2);
1014 buf
= luaA_getopt_lstring(L
, 2, "corner", "auto", &len
);
1015 corner
= a_strtocorner(buf
, len
);
1016 infobox
= luaA_getopt_boolean(L
, 2, "infobox", true);
1019 mouse_client_resize(*c
, corner
, infobox
);
1024 /** Move a client with mouse.
1025 * \param L The Lua VM state.
1029 * \lparam An optional table with keys: `snap' for pixel to snap (default to 8), and
1030 * `infobox' to enable or disable the coordinates and dimensions box (default to
1034 luaA_client_mouse_move(lua_State
*L
)
1036 client_t
**c
= luaA_checkudata(L
, 1, "client");
1038 bool infobox
= true;
1040 if(lua_gettop(L
) == 2 && !lua_isnil(L
, 2))
1042 luaA_checktable(L
, 2);
1043 snap
= luaA_getopt_number(L
, 2, "snap", 8);
1044 infobox
= luaA_getopt_boolean(L
, 2, "infobox", true);
1047 mouse_client_move(*c
, snap
, infobox
);
1052 /** Create a new mouse button bindings.
1053 * \param L The Lua VM state.
1054 * \return The number of elements pushed on stack.
1056 * \lparam A table with modifiers keys, or a button to clone.
1057 * \lparam A mouse button number.
1058 * \lparam A function to execute on click events.
1059 * \lparam A function to execute on release events.
1060 * \lreturn A mouse button binding.
1063 luaA_button_new(lua_State
*L
)
1066 button_t
*button
, **orig
;
1068 if((orig
= luaA_toudata(L
, 2, "button")))
1070 button_t
*copy
= p_new(button_t
, 1);
1071 copy
->mod
= (*orig
)->mod
;
1072 copy
->button
= (*orig
)->button
;
1073 if((*orig
)->press
!= LUA_REFNIL
)
1075 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*orig
)->press
);
1076 luaA_registerfct(L
, -1, ©
->press
);
1079 copy
->press
= LUA_REFNIL
;
1080 if((*orig
)->release
!= LUA_REFNIL
)
1082 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*orig
)->release
);
1083 luaA_registerfct(L
, -1, ©
->release
);
1086 copy
->release
= LUA_REFNIL
;
1087 return luaA_button_userdata_new(L
, copy
);
1090 luaA_checktable(L
, 2);
1091 /* arg 3 is mouse button */
1092 i
= luaL_checknumber(L
, 3);
1093 /* arg 4 and 5 are callback functions */
1094 if(!lua_isnil(L
, 4))
1095 luaA_checkfunction(L
, 4);
1096 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
1097 luaA_checkfunction(L
, 5);
1099 button
= p_new(button_t
, 1);
1100 button
->button
= xutil_button_fromint(i
);
1103 button
->press
= LUA_REFNIL
;
1105 luaA_registerfct(L
, 4, &button
->press
);
1107 if(lua_gettop(L
) == 5 && !lua_isnil(L
, 5))
1108 luaA_registerfct(L
, 5, &button
->release
);
1110 button
->release
= LUA_REFNIL
;
1112 len
= lua_objlen(L
, 2);
1113 for(i
= 1; i
<= len
; i
++)
1117 lua_rawgeti(L
, 2, i
);
1118 buf
= luaL_checklstring(L
, -1, &blen
);
1119 button
->mod
|= xutil_key_mask_fromstr(buf
, blen
);
1122 return luaA_button_userdata_new(L
, button
);
1125 /** Return a formated string for a button.
1126 * \param L The Lua VM state.
1129 * \lreturn A string.
1132 luaA_button_tostring(lua_State
*L
)
1134 button_t
**p
= luaA_checkudata(L
, 1, "button");
1135 lua_pushfstring(L
, "[button udata(%p)]", *p
);
1139 /** Set a button array with a Lua table.
1140 * \param L The Lua VM state.
1141 * \param idx The index of the Lua table.
1142 * \param buttons The array button to fill.
1145 luaA_button_array_set(lua_State
*L
, int idx
, button_array_t
*buttons
)
1149 luaA_checktable(L
, idx
);
1150 button_array_wipe(buttons
);
1151 button_array_init(buttons
);
1153 while(lua_next(L
, idx
))
1155 b
= luaA_checkudata(L
, -1, "button");
1156 button_array_append(buttons
, *b
);
1162 /** Push an array of button as an Lua table onto the stack.
1163 * \param L The Lua VM state.
1164 * \param buttons The button array to push.
1165 * \return The number of elements pushed on stack.
1168 luaA_button_array_get(lua_State
*L
, button_array_t
*buttons
)
1171 for(int i
= 0; i
< buttons
->len
; i
++)
1173 luaA_button_userdata_new(L
, buttons
->tab
[i
]);
1174 lua_rawseti(L
, -2, i
+ 1);
1180 * \param L The Lua VM state.
1181 * \return The number of elements pushed on stack.
1183 * \lfield press The function called when button press event is received.
1184 * \lfield release The function called when button release event is received.
1187 luaA_button_index(lua_State
*L
)
1189 if(luaA_usemetatable(L
, 1, 2))
1193 button_t
**button
= luaA_checkudata(L
, 1, "button");
1194 const char *attr
= luaL_checklstring(L
, 2, &len
);
1196 switch(a_tokenize(attr
, len
))
1199 if((*button
)->press
!= LUA_REFNIL
)
1200 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->press
);
1205 if((*button
)->release
!= LUA_REFNIL
)
1206 lua_rawgeti(L
, LUA_REGISTRYINDEX
, (*button
)->release
);
1211 /* works fine, but not *really* neat */
1212 lua_pushnumber(L
, (*button
)->button
);
1222 * \param L The Lua VM state.
1223 * \return The number of elements pushed on stack.
1227 luaA_button_newindex(lua_State
*L
)
1229 if(luaA_usemetatable(L
, 1, 2))
1233 button_t
**button
= luaA_checkudata(L
, 1, "button");
1234 const char *attr
= luaL_checklstring(L
, 2, &len
);
1236 switch(a_tokenize(attr
, len
))
1239 luaA_registerfct(L
, 3, &(*button
)->press
);
1242 luaA_registerfct(L
, 3, &(*button
)->release
);
1245 (*button
)->button
= xutil_button_fromint(luaL_checknumber(L
, 3));
1254 const struct luaL_reg awesome_button_methods
[] =
1256 { "__call", luaA_button_new
},
1259 const struct luaL_reg awesome_button_meta
[] =
1261 { "__index", luaA_button_index
},
1262 { "__newindex", luaA_button_newindex
},
1263 { "__gc", luaA_button_gc
},
1264 { "__eq", luaA_button_eq
},
1265 { "__tostring", luaA_button_tostring
},
1270 * \param L The Lua VM state.
1271 * \return The number of elements pushed on stack.
1273 * \lfield coords Mouse coordinates.
1274 * \lfield screen Mouse screen number.
1277 luaA_mouse_index(lua_State
*L
)
1280 const char *attr
= luaL_checklstring(L
, 2, &len
);
1281 int mouse_x
, mouse_y
, i
;
1284 switch(a_tokenize(attr
, len
))
1287 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, NULL
))
1290 i
= screen_getbycoord(screen
, mouse_x
, mouse_y
);
1292 lua_pushnumber(L
, i
+ 1);
1301 /** Newindex for mouse.
1302 * \param L The Lua VM state.
1303 * \return The number of elements pushed on stack.
1306 luaA_mouse_newindex(lua_State
*L
)
1309 const char *attr
= luaL_checklstring(L
, 2, &len
);
1312 int screen
, phys_screen
;
1314 switch(a_tokenize(attr
, len
))
1317 screen
= luaL_checknumber(L
, 3) - 1;
1318 luaA_checkscreen(screen
);
1320 /* we need the physical one to get the root window */
1321 phys_screen
= screen_virttophys(screen
);
1322 root
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
1324 x
= globalconf
.screens
[screen
].geometry
.x
;
1325 y
= globalconf
.screens
[screen
].geometry
.y
;
1327 mouse_warp_pointer(root
, x
, y
);
1336 /** Get or set the mouse coords.
1337 * \param L The Lua VM state.
1338 * \return The number of elements pushed on stack.
1340 * \lparam None or a table with x and y keys as mouse coordinates.
1341 * \lreturn A table with mouse coordinates.
1344 luaA_mouse_coords(lua_State
*L
)
1346 uint16_t mask
, maski
;
1347 int screen
, x
, y
, mouse_x
, mouse_y
, i
= 1;
1349 if(lua_gettop(L
) == 1)
1353 luaA_checktable(L
, 1);
1355 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &mask
))
1358 x
= luaA_getopt_number(L
, 1, "x", mouse_x
);
1359 y
= luaA_getopt_number(L
, 1, "y", mouse_y
);
1361 root
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
1362 mouse_warp_pointer(root
, x
, y
);
1366 if(!mouse_query_pointer_root(&screen
, &mouse_x
, &mouse_y
, &mask
))
1370 lua_pushnumber(L
, mouse_x
);
1371 lua_setfield(L
, -2, "x");
1372 lua_pushnumber(L
, mouse_y
);
1373 lua_setfield(L
, -2, "y");
1375 for(maski
= XCB_BUTTON_MASK_1
; i
<= XCB_BUTTON_MASK_5
; maski
<<= 1, i
++)
1378 lua_pushboolean(L
, true);
1379 lua_rawseti(L
, -2, i
);
1381 lua_setfield(L
, -2, "buttons");
1386 const struct luaL_reg awesome_mouse_methods
[] =
1388 { "__index", luaA_mouse_index
},
1389 { "__newindex", luaA_mouse_newindex
},
1390 { "coords", luaA_mouse_coords
},
1393 const struct luaL_reg awesome_mouse_meta
[] =
1398 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80