awful.client: rename focus and swap bydirection
[awesome.git] / mouse.c
blobab8a958ddad2222b34ffaffe8d5fcb449397d49f
1 /*
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.
22 #include <math.h>
24 #include "common/tokenize.h"
25 #include "mouse.h"
26 #include "screen.h"
27 #include "tag.h"
28 #include "client.h"
29 #include "titlebar.h"
30 #include "wibox.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. */
46 typedef enum
48 AutoCorner,
49 TopRightCorner,
50 TopLeftCorner,
51 BottomLeftCorner,
52 BottomRightCorner
53 } corner_t;
55 /** Convert a corner name into a corner type.
56 * \param str A string.
57 * \param len String length.
58 * \return A corner type.
60 static corner_t
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;
73 /** Delete a button.
74 * \param button The button to destroy.
76 void
77 button_delete(button_t **button)
79 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
80 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
81 p_delete(button);
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
90 static area_t
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;
107 return geometry;
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
116 static area_t
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))
122 < snap)
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))
127 < snap)
128 geometry.y = snap_geometry.y + snap_geometry.height - geometry.height;
130 return geometry;
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.
139 static area_t
140 mouse_snapclient(client_t *c, area_t geometry, int snap)
142 client_t *snapper;
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,
148 false);
150 area_t screen_geometry_barless =
151 screen_area_get(c->screen,
152 NULL,
153 &globalconf.screens[c->screen].padding,
154 false);
156 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
158 geometry =
159 mouse_snapclienttogeometry_inside(geometry, screen_geometry, snap);
161 geometry =
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);
169 geometry =
170 mouse_snapclienttogeometry_outside(geometry,
171 snapper_geometry,
172 snap);
175 return titlebar_geometry_remove(c->titlebar, c->border, geometry);
178 /** Set coordinates to a corner of an area.
180 * \param a The 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.
189 static corner_t
190 mouse_snap_to_corner(area_t a, int *x, int *y, corner_t corner)
192 int top, bottom, left, right;
194 top = AREA_TOP(a);
195 bottom = AREA_BOTTOM(a);
196 left = AREA_LEFT(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;
206 else
207 corner = TopRightCorner;
209 else
211 if(abs(left - *x) < abs(right - *x))
212 corner = BottomLeftCorner;
213 else
214 corner = BottomRightCorner;
218 switch(corner)
220 case TopRightCorner:
221 *x = right;
222 *y = top;
223 break;
225 case TopLeftCorner:
226 *x = left;
227 *y = top;
228 break;
230 case BottomLeftCorner:
231 *x = left;
232 *y = bottom;
233 break;
235 case BottomRightCorner:
236 *x = right;
237 *y = bottom;
238 break;
240 default:
241 break;
244 return corner;
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.
252 static void
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 };
256 char size[64];
257 size_t len;
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.
279 static void
280 mouse_infobox_new(simple_window_t *sw, int phys_screen, int border, area_t geometry)
282 area_t geom;
283 draw_parser_data_t pdata;
285 draw_parser_data_init(&pdata);
287 geom = draw_text_extents(globalconf.default_screen,
288 globalconf.font,
289 MOUSE_INFOBOX_STRING_DEFAULT,
290 sizeof(MOUSE_INFOBOX_STRING_DEFAULT)-1,
291 &pdata);
292 geom.x = geometry.x + ((2 * border + geometry.width) - geom.width) / 2;
293 geom.y = geometry.y + ((2 * border + geometry.height) - geom.height) / 2;
295 p_clear(sw, 1);
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
312 static bool
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)
322 return false;
324 *x = query_ptr_r->win_x;
325 *y = query_ptr_r->win_y;
326 if (mask)
327 *mask = query_ptr_r->mask;
329 p_delete(&query_ptr_r);
331 return true;
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.
341 static bool
342 mouse_query_pointer_root(int *s, int *x, int *y, uint16_t *mask)
344 for(int screen = 0;
345 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
346 screen++)
348 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
350 if(mouse_query_pointer(root, x, y, mask))
352 *s = screen;
353 return true;
356 return false;
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.
364 static bool
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)
371 cursor = CurNormal;
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);
378 if(!grab_ptr_r)
379 return false;
381 p_delete(&grab_ptr_r);
383 return true;
386 /** Ungrab the Pointer
388 static inline void
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.
399 static inline void
400 mouse_warp_pointer(xcb_window_t window, int x, int y)
402 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
403 0, 0, 0, 0, x, y );
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
413 static bool
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;
420 while(true)
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;
429 p_delete(&ev);
430 return true;
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;
436 p_delete(&ev);
437 return false;
439 default:
440 xcb_event_handle(&globalconf.evenths, ev);
441 p_delete(&ev);
442 break;
446 /** Get the client that contains the pointer.
448 * \return The client that contains the pointer or NULL.
450 static client_t *
451 mouse_get_client_under_pointer(int phys_screen)
453 xcb_window_t root;
454 xcb_query_pointer_cookie_t query_ptr_c;
455 xcb_query_pointer_reply_t *query_ptr_r;
456 client_t *c = NULL;
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);
463 if(query_ptr_r)
465 c = client_getbywin(query_ptr_r->child);
466 p_delete(&query_ptr_r);
469 return c;
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.
477 static void
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;
484 /* current layout */
485 layout_t *layout;
486 /* the infobox */
487 simple_window_t sw;
488 /* the root window */
489 xcb_window_t root;
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);
497 /* grab pointer */
498 if(c->isfullscreen
499 || c->type == WINDOW_TYPE_DESKTOP
500 || c->type == WINDOW_TYPE_SPLASH
501 || c->type == WINDOW_TYPE_DOCK
502 || !mouse_grab_pointer(root, CurMove))
503 return;
505 if(infobox && (client_isfloating(c) || layout == layout_floating))
506 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
507 else
508 infobox = false;
510 /* for each motion event */
511 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
512 if(client_isfloating(c) || layout == layout_floating)
514 area_t geometry;
516 /* calc new geometry */
517 geometry = c->geometry;
518 geometry.x += (mouse_x - last_x);
519 geometry.y += (mouse_y - last_y);
521 /* snap and move */
522 geometry = mouse_snapclient(c, geometry, snap);
523 c->ismoving = true;
524 client_resize(c, geometry, false);
525 xcb_flush(globalconf.connection);
526 c->ismoving = false;
528 /* draw the infobox */
529 if(infobox)
530 mouse_infobox_draw(&sw, c->geometry, c->border);
532 /* refresh live */
533 wibox_refresh();
534 xcb_flush(globalconf.connection);
536 /* keep track */
537 last_x = mouse_x;
538 last_y = mouse_y;
540 else
542 int newscreen;
543 client_t *target;
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;
552 layout_refresh();
553 wibox_refresh();
554 xcb_flush(globalconf.connection);
557 /* find client to swap with */
558 target = mouse_get_client_under_pointer(c->phys_screen);
560 /* swap position */
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);
566 layout_refresh();
567 wibox_refresh();
568 xcb_flush(globalconf.connection);
572 /* ungrab pointer */
573 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
575 /* free the infobox */
576 if(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.
586 static void
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;
594 /* the infobox */
595 simple_window_t sw;
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);
604 top = c->geometry.y;
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;
616 /* select cursor */
617 switch(corner)
619 default:
620 cursor = CurTopLeft;
621 break;
622 case TopRightCorner:
623 cursor = CurTopRight;
624 break;
625 case BottomLeftCorner:
626 cursor = CurBotLeft;
627 break;
628 case BottomRightCorner:
629 cursor = CurBotRight;
630 break;
633 /* grab the pointer */
634 if(!mouse_grab_pointer(screen->root, cursor))
635 return;
637 /* set pointer to the moveable corner */
638 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
640 /* create the infobox */
641 if(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 */
653 corner_t new_corner;
655 if(mouse_x == AREA_LEFT(geo))
656 new_corner = (mouse_y == AREA_TOP(geo)) ? TopLeftCorner : BottomLeftCorner;
657 else
658 new_corner = (mouse_y == AREA_TOP(geo)) ? TopRightCorner : BottomRightCorner;
660 /* update cursor */
661 if(corner != new_corner)
663 corner = new_corner;
665 switch(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)
679 int dx, dy;
681 /* apply size hints */
682 geo = client_geometry_hints(c, geo);
684 /* get the nonmoveable corner back onto fixed_x,fixed_y */
685 switch(corner)
687 default /* TopLeftCorner */:
688 dx = fixed_x - AREA_RIGHT(geo);
689 dy = fixed_y - AREA_BOTTOM(geo);
690 break;
691 case TopRightCorner:
692 dx = fixed_x - AREA_LEFT(geo);
693 dy = fixed_y - AREA_BOTTOM(geo);
694 break;
695 case BottomRightCorner:
696 dx = fixed_x - AREA_LEFT(geo);
697 dy = fixed_y - AREA_TOP(geo);
698 break;
699 case BottomLeftCorner:
700 dx = fixed_x - AREA_RIGHT(geo);
701 dy = fixed_y - AREA_TOP(geo);
702 break;
705 geo.x += dx;
706 geo.y += dy;
709 /* resize the client */
710 client_resize(c, geo, false);
712 /* refresh live */
713 wibox_refresh();
714 xcb_flush(globalconf.connection);
716 /* draw the infobox */
717 if(infobox)
718 mouse_infobox_draw(&sw, c->geometry, c->border);
721 /* relase pointer */
722 mouse_ungrab_pointer();
724 /* free the infobox */
725 if(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.
732 static void
733 mouse_client_resize_tiled(client_t *c)
735 xcb_screen_t *screen;
736 /* screen area modulo wibox */
737 area_t area;
738 /* current tag */
739 tag_t *tag;
740 /* current layout */
741 layout_t *layout;
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,
753 true);
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;
761 cursor = CurResizeH;
763 else if(layout == layout_tileleft)
765 mouse_x = area.x + area.width * (1. - tag->mwfact);
766 cursor = CurResizeH;
768 else if(layout == layout_tilebottom)
770 mouse_y = area.y + area.height * tag->mwfact;
771 cursor = CurResizeV;
773 else if(layout == layout_tiletop)
775 mouse_y = area.y + area.height * (1. - tag->mwfact);
776 cursor = CurResizeV;
778 else
779 return;
781 /* grab the pointer */
782 if(!mouse_grab_pointer(screen->root, cursor))
783 return;
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)
800 mwfact = fact_x;
801 else if(layout == layout_tileleft)
802 mwfact = 1. - fact_x;
803 else if(layout == layout_tilebottom)
804 mwfact = fact_y;
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);
811 /* refresh layout */
812 if(fabs(tag->mwfact - mwfact) >= 0.01)
814 tag->mwfact = mwfact;
815 globalconf.screens[tag->screen].need_arrange = true;
816 layout_refresh();
817 wibox_refresh();
818 xcb_flush(globalconf.connection);
822 /* relase pointer */
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.
830 static void
831 mouse_client_resize_magnified(client_t *c, bool infobox)
833 /* screen area modulo wibox */
834 area_t area;
835 /* center of area */
836 int center_x, center_y;
837 /* max. distance from the center */
838 double maxdist;
839 /* mouse position */
840 int mouse_x = 0, mouse_y = 0;
841 /* cursor while grabbing */
842 size_t cursor = CurResize;
843 corner_t corner = AutoCorner;
844 /* current tag */
845 tag_t *tag;
846 /* the infobox */
847 simple_window_t sw;
848 xcb_window_t root;
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,
857 true);
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))
867 return;
869 /* select corner */
870 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
872 /* select cursor */
873 switch(corner)
875 default:
876 cursor = CurTopLeft;
877 break;
878 case TopRightCorner:
879 cursor = CurTopRight;
880 break;
881 case BottomLeftCorner:
882 cursor = CurBotLeft;
883 break;
884 case BottomRightCorner:
885 cursor = CurBotRight;
886 break;
889 /* grab pointer */
890 if(!mouse_grab_pointer(root, cursor))
891 return;
893 /* move pointer to corner */
894 mouse_warp_pointer(root, mouse_x, mouse_y);
896 /* create the infobox */
897 if(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;
922 layout_refresh();
923 wibox_refresh();
924 xcb_flush(globalconf.connection);
927 /* draw the infobox */
928 if(infobox)
929 mouse_infobox_draw(&sw, c->geometry, c->border);
932 /* ungrab pointer */
933 mouse_ungrab_pointer();
935 /* free the infobox */
936 if(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.
945 static void
946 mouse_client_resize(client_t *c, corner_t corner, bool infobox)
948 int n, screen;
949 tag_t **curtags;
950 layout_t *layout;
951 xcb_screen_t *s;
953 if(c->isfullscreen
954 || c->type == WINDOW_TYPE_DESKTOP
955 || c->type == WINDOW_TYPE_SPLASH
956 || c->type == WINDOW_TYPE_DOCK)
957 return;
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)
969 screen = c->screen;
970 for(n = 0, c = globalconf.clients; c; c = c->next)
971 if(IS_TILED(c, screen))
972 n++;
974 /* only masters on this screen? */
975 if(n <= curtags[0]->nmaster)
976 goto bailout;
978 /* no tiled clients on this screen? */
979 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
980 if(!c)
981 goto bailout;
983 mouse_client_resize_tiled(c);
985 else if(layout == layout_magnifier)
986 mouse_client_resize_magnified(c, infobox);
988 bailout:
989 p_delete(&curtags);
992 /** Resize a client with mouse.
993 * \param L The Lua VM state.
995 * \luastack
996 * \lvalue A client.
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
1000 * enabled).
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;
1008 size_t len;
1009 const char *buf;
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);
1021 return 0;
1024 /** Move a client with mouse.
1025 * \param L The Lua VM state.
1027 * \luastack
1028 * \lvalue A client.
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
1031 * enabled).
1034 luaA_client_mouse_move(lua_State *L)
1036 client_t **c = luaA_checkudata(L, 1, "client");
1037 int snap = 8;
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);
1049 return 0;
1052 /** Create a new mouse button bindings.
1053 * \param L The Lua VM state.
1054 * \return The number of elements pushed on stack.
1055 * \luastack
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.
1062 static int
1063 luaA_button_new(lua_State *L)
1065 int i, len;
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, &copy->press);
1078 else
1079 copy->press = LUA_REFNIL;
1080 if((*orig)->release != LUA_REFNIL)
1082 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
1083 luaA_registerfct(L, -1, &copy->release);
1085 else
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);
1102 if(lua_isnil(L, 4))
1103 button->press = LUA_REFNIL;
1104 else
1105 luaA_registerfct(L, 4, &button->press);
1107 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1108 luaA_registerfct(L, 5, &button->release);
1109 else
1110 button->release = LUA_REFNIL;
1112 len = lua_objlen(L, 2);
1113 for(i = 1; i <= len; i++)
1115 size_t blen;
1116 const char *buf;
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.
1127 * \luastack
1128 * \lvalue A button.
1129 * \lreturn A string.
1131 static int
1132 luaA_button_tostring(lua_State *L)
1134 button_t **p = luaA_checkudata(L, 1, "button");
1135 lua_pushfstring(L, "[button udata(%p)]", *p);
1136 return 1;
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.
1144 void
1145 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
1147 button_t **b;
1149 luaA_checktable(L, idx);
1150 button_array_wipe(buttons);
1151 button_array_init(buttons);
1152 lua_pushnil(L);
1153 while(lua_next(L, idx))
1155 b = luaA_checkudata(L, -1, "button");
1156 button_array_append(buttons, *b);
1157 button_ref(b);
1158 lua_pop(L, 1);
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)
1170 luaA_otable_new(L);
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);
1176 return 1;
1179 /** Button object.
1180 * \param L The Lua VM state.
1181 * \return The number of elements pushed on stack.
1182 * \luastack
1183 * \lfield press The function called when button press event is received.
1184 * \lfield release The function called when button release event is received.
1186 static int
1187 luaA_button_index(lua_State *L)
1189 if(luaA_usemetatable(L, 1, 2))
1190 return 1;
1192 size_t len;
1193 button_t **button = luaA_checkudata(L, 1, "button");
1194 const char *attr = luaL_checklstring(L, 2, &len);
1196 switch(a_tokenize(attr, len))
1198 case A_TK_PRESS:
1199 if((*button)->press != LUA_REFNIL)
1200 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
1201 else
1202 lua_pushnil(L);
1203 break;
1204 case A_TK_RELEASE:
1205 if((*button)->release != LUA_REFNIL)
1206 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
1207 else
1208 lua_pushnil(L);
1209 break;
1210 case A_TK_BUTTON:
1211 /* works fine, but not *really* neat */
1212 lua_pushnumber(L, (*button)->button);
1213 break;
1214 default:
1215 break;
1218 return 1;
1221 /** Button object.
1222 * \param L The Lua VM state.
1223 * \return The number of elements pushed on stack.
1224 * \luastack
1226 static int
1227 luaA_button_newindex(lua_State *L)
1229 if(luaA_usemetatable(L, 1, 2))
1230 return 1;
1232 size_t len;
1233 button_t **button = luaA_checkudata(L, 1, "button");
1234 const char *attr = luaL_checklstring(L, 2, &len);
1236 switch(a_tokenize(attr, len))
1238 case A_TK_PRESS:
1239 luaA_registerfct(L, 3, &(*button)->press);
1240 break;
1241 case A_TK_RELEASE:
1242 luaA_registerfct(L, 3, &(*button)->release);
1243 break;
1244 case A_TK_BUTTON:
1245 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
1246 break;
1247 default:
1248 break;
1251 return 0;
1254 const struct luaL_reg awesome_button_methods[] =
1256 { "__call", luaA_button_new },
1257 { NULL, NULL }
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 },
1266 { NULL, NULL }
1269 /** Mouse library.
1270 * \param L The Lua VM state.
1271 * \return The number of elements pushed on stack.
1272 * \luastack
1273 * \lfield coords Mouse coordinates.
1274 * \lfield screen Mouse screen number.
1276 static int
1277 luaA_mouse_index(lua_State *L)
1279 size_t len;
1280 const char *attr = luaL_checklstring(L, 2, &len);
1281 int mouse_x, mouse_y, i;
1282 int screen;
1284 switch(a_tokenize(attr, len))
1286 case A_TK_SCREEN:
1287 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL))
1288 return 0;
1290 i = screen_getbycoord(screen, mouse_x, mouse_y);
1292 lua_pushnumber(L, i + 1);
1293 break;
1294 default:
1295 return 0;
1298 return 1;
1301 /** Newindex for mouse.
1302 * \param L The Lua VM state.
1303 * \return The number of elements pushed on stack.
1305 static int
1306 luaA_mouse_newindex(lua_State *L)
1308 size_t len;
1309 const char *attr = luaL_checklstring(L, 2, &len);
1310 int x, y = 0;
1311 xcb_window_t root;
1312 int screen, phys_screen;
1314 switch(a_tokenize(attr, len))
1316 case A_TK_SCREEN:
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);
1328 break;
1329 default:
1330 return 0;
1333 return 0;
1336 /** Get or set the mouse coords.
1337 * \param L The Lua VM state.
1338 * \return The number of elements pushed on stack.
1339 * \luastack
1340 * \lparam None or a table with x and y keys as mouse coordinates.
1341 * \lreturn A table with mouse coordinates.
1343 static int
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)
1351 xcb_window_t root;
1353 luaA_checktable(L, 1);
1355 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1356 return 0;
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);
1363 lua_pop(L, 1);
1366 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1367 return 0;
1369 lua_newtable(L);
1370 lua_pushnumber(L, mouse_x);
1371 lua_setfield(L, -2, "x");
1372 lua_pushnumber(L, mouse_y);
1373 lua_setfield(L, -2, "y");
1374 lua_newtable(L);
1375 for(maski = XCB_BUTTON_MASK_1; i <= XCB_BUTTON_MASK_5; maski <<= 1, i++)
1376 if(mask & maski)
1378 lua_pushboolean(L, true);
1379 lua_rawseti(L, -2, i);
1381 lua_setfield(L, -2, "buttons");
1383 return 1;
1386 const struct luaL_reg awesome_mouse_methods[] =
1388 { "__index", luaA_mouse_index },
1389 { "__newindex", luaA_mouse_newindex },
1390 { "coords", luaA_mouse_coords },
1391 { NULL, NULL }
1393 const struct luaL_reg awesome_mouse_meta[] =
1395 { NULL, NULL }
1398 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80