luaa: check that function is not NIL before pushing and calling
[awesome.git] / mouse.c
blobb7699661b38de15bca71bf548d573e676569590b
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 "screen.h"
26 #include "tag.h"
27 #include "titlebar.h"
28 #include "layouts/floating.h"
29 #include "layouts/tile.h"
30 #include "layouts/magnifier.h"
32 #define MOUSEMASK (XCB_EVENT_MASK_BUTTON_PRESS \
33 | XCB_EVENT_MASK_BUTTON_RELEASE \
34 | XCB_EVENT_MASK_POINTER_MOTION)
36 extern awesome_t globalconf;
38 DO_LUA_NEW(extern, button_t, button, "button", button_ref)
39 DO_LUA_GC(button_t, button, "button", button_unref)
40 DO_LUA_EQ(button_t, button, "button")
42 /** Define corners. */
43 typedef enum
45 AutoCorner,
46 TopRightCorner,
47 TopLeftCorner,
48 BottomLeftCorner,
49 BottomRightCorner
50 } corner_t;
52 /** Convert a corner name into a corner type.
53 * \param str A string.
54 * \param len String length.
55 * \return A corner type.
57 static corner_t
58 a_strtocorner(const char *str, size_t len)
60 switch (a_tokenize(str, len))
62 case A_TK_BOTTOMRIGHT: return BottomRightCorner;
63 case A_TK_BOTTOMLEFT: return BottomLeftCorner;
64 case A_TK_TOPLEFT: return TopLeftCorner;
65 case A_TK_TOPRIGHT: return TopRightCorner;
66 default: return AutoCorner;
70 /** Delete a button.
71 * \param button The button to destroy.
73 void
74 button_delete(button_t **button)
76 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
77 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
78 p_delete(button);
81 /** Snap an area to the outside of an area.
82 * \param geometry geometry of the area to snap
83 * \param snap_geometry geometry of snapping area
84 * \param snap snap trigger in pixel
85 * \return snapped geometry
87 static area_t
88 mouse_snapclienttogeometry_outside(area_t geometry, area_t snap_geometry, int snap)
90 if(geometry.x < snap + snap_geometry.x + snap_geometry.width
91 && geometry.x > snap_geometry.x + snap_geometry.width)
92 geometry.x = snap_geometry.x + snap_geometry.width;
93 else if(geometry.x + geometry.width < snap_geometry.x
94 && geometry.x + geometry.width > snap_geometry.x - snap)
95 geometry.x = snap_geometry.x - geometry.width;
97 if(geometry.y < snap + snap_geometry.y + snap_geometry.height
98 && geometry.y > snap_geometry.y + snap_geometry.height)
99 geometry.y = snap_geometry.y + snap_geometry.height;
100 else if(geometry.y + geometry.height < snap_geometry.y
101 && geometry.y + geometry.height > snap_geometry.y - snap)
102 geometry.y = snap_geometry.y - geometry.height;
104 return geometry;
107 /** Snap an area to the inside of an area.
108 * \param geometry geometry of the area to snap
109 * \param snap_geometry geometry of snapping area
110 * \param snap snap trigger in pixel
111 * \return snapped geometry
113 static area_t
114 mouse_snapclienttogeometry_inside(area_t geometry, area_t snap_geometry, int snap)
116 if(abs(geometry.x) < snap + snap_geometry.x && geometry.x > snap_geometry.x)
117 geometry.x = snap_geometry.x;
118 else if(abs((snap_geometry.x + snap_geometry.width) - (geometry.x + geometry.width))
119 < snap)
120 geometry.x = snap_geometry.x + snap_geometry.width - geometry.width;
121 if(abs(geometry.y) < snap + snap_geometry.y && geometry.y > snap_geometry.y)
122 geometry.y = snap_geometry.y;
123 else if(abs((snap_geometry.y + snap_geometry.height) - (geometry.y + geometry.height))
124 < snap)
125 geometry.y = snap_geometry.y + snap_geometry.height - geometry.height;
127 return geometry;
130 /** Snap a client with a future geometry to the screen and other clients.
131 * \param c The client.
132 * \param geometry Geometry the client will get.
133 * \param snap The maximum distance in pixels to trigger a "snap".
134 * \return Geometry to set to the client.
136 static area_t
137 mouse_snapclient(client_t *c, area_t geometry, int snap)
139 client_t *snapper;
140 area_t snapper_geometry;
141 area_t screen_geometry =
142 screen_area_get(c->screen,
143 &globalconf.screens[c->screen].wiboxes,
144 &globalconf.screens[c->screen].padding,
145 false);
147 area_t screen_geometry_barless =
148 screen_area_get(c->screen,
149 NULL,
150 &globalconf.screens[c->screen].padding,
151 false);
153 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
155 geometry =
156 mouse_snapclienttogeometry_inside(geometry, screen_geometry, snap);
158 geometry =
159 mouse_snapclienttogeometry_inside(geometry, screen_geometry_barless, snap);
161 for(snapper = globalconf.clients; snapper; snapper = snapper->next)
162 if(snapper != c && client_isvisible(snapper, c->screen))
164 snapper_geometry = snapper->geometry;
165 snapper_geometry = titlebar_geometry_add(snapper->titlebar, snapper->border, snapper_geometry);
166 geometry =
167 mouse_snapclienttogeometry_outside(geometry,
168 snapper_geometry,
169 snap);
172 return titlebar_geometry_remove(c->titlebar, c->border, geometry);
175 /** Set coordinates to a corner of an area.
177 * \param a The area.
178 * \param[in,out] x The x coordinate.
179 * \param[in,out] y The y coordinate.
180 * \param corner The corner to snap to.
181 * \return The corner the coordinates have been set to. If corner != AutoCorner
182 * this is always equal to \em corner.
184 * \todo rename/move this is still awkward and might be useful somewhere else.
186 static corner_t
187 mouse_snap_to_corner(area_t a, int *x, int *y, corner_t corner)
189 int top, bottom, left, right;
191 top = AREA_TOP(a);
192 bottom = AREA_BOTTOM(a);
193 left = AREA_LEFT(a);
194 right = AREA_RIGHT(a);
196 /* figure out the nearser corner */
197 if(corner == AutoCorner)
199 if(abs(top - *y) < abs(bottom - *y))
201 if(abs(left - *x) < abs(right - *x))
202 corner = TopLeftCorner;
203 else
204 corner = TopRightCorner;
206 else
208 if(abs(left - *x) < abs(right - *x))
209 corner = BottomLeftCorner;
210 else
211 corner = BottomRightCorner;
215 switch(corner)
217 case TopRightCorner:
218 *x = right;
219 *y = top;
220 break;
222 case TopLeftCorner:
223 *x = left;
224 *y = top;
225 break;
227 case BottomLeftCorner:
228 *x = left;
229 *y = bottom;
230 break;
232 case BottomRightCorner:
233 *x = right;
234 *y = bottom;
235 break;
237 default:
238 break;
241 return corner;
244 /** Redraw the infobox.
245 * \param sw The simple window.
246 * \param geometry The geometry to use for the box.
247 * \param border The client border size.
249 static void
250 mouse_infobox_draw(simple_window_t *sw, area_t geometry, int border)
252 area_t draw_geometry = { 0, 0, sw->ctx.width, sw->ctx.height };
253 char size[64];
254 size_t len;
255 xcolor_t color_without_alpha = globalconf.colors.bg;
256 color_without_alpha.alpha = 0xffff;
258 len = snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
259 geometry.width, geometry.height, geometry.x, geometry.y);
260 draw_rectangle(&sw->ctx, draw_geometry, 1.0, true, &color_without_alpha);
261 draw_text(&sw->ctx, globalconf.font, PANGO_ELLIPSIZE_NONE, PANGO_WRAP_WORD, draw_geometry, size, len, NULL);
262 simplewindow_move(sw,
263 geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
264 geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);
265 simplewindow_refresh_pixmap(sw);
266 xcb_flush(globalconf.connection);
269 #define MOUSE_INFOBOX_STRING_DEFAULT "0000x0000+0000+0000"
271 /** Initialize the infobox window.
272 * \param sw The simple window to init.
273 * \param phys_screen Physical screen number.
274 * \param border Border size of the client.
275 * \param geometry Client geometry.
276 * \return The simple window.
278 static void
279 mouse_infobox_new(simple_window_t *sw, int phys_screen, int border, area_t geometry)
281 area_t geom;
282 draw_parser_data_t pdata;
284 draw_parser_data_init(&pdata);
286 geom = draw_text_extents(globalconf.font,
287 MOUSE_INFOBOX_STRING_DEFAULT,
288 sizeof(MOUSE_INFOBOX_STRING_DEFAULT)-1,
289 &pdata);
290 geom.x = geometry.x + ((2 * border + geometry.width) - geom.width) / 2;
291 geom.y = geometry.y + ((2 * border + geometry.height) - geom.height) / 2;
293 p_clear(sw, 1);
294 simplewindow_init(sw, phys_screen, geom, 0, East,
295 &globalconf.colors.fg, &globalconf.colors.bg);
297 xcb_map_window(globalconf.connection, sw->window);
298 mouse_infobox_draw(sw, geometry, border);
300 draw_parser_data_wipe(&pdata);
303 /** Get the pointer position.
304 * \param window The window to get position on.
305 * \param x will be set to the Pointer-x-coordinate relative to window
306 * \param y will be set to the Pointer-y-coordinate relative to window
307 * \param mask will be set to the current buttons state
308 * \return true on success, false if an error occured
310 static bool
311 mouse_query_pointer(xcb_window_t window, int *x, int *y, uint16_t *mask)
313 xcb_query_pointer_cookie_t query_ptr_c;
314 xcb_query_pointer_reply_t *query_ptr_r;
316 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
317 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
319 if(!query_ptr_r || !query_ptr_r->same_screen)
320 return false;
322 *x = query_ptr_r->win_x;
323 *y = query_ptr_r->win_y;
324 if (mask)
325 *mask = query_ptr_r->mask;
327 p_delete(&query_ptr_r);
329 return true;
332 /** Get the pointer position on the screen.
333 * \param screen This will be set to the screen number the mouse is on.
334 * \param x This will be set to the Pointer-x-coordinate relative to window.
335 * \param y This will be set to the Pointer-y-coordinate relative to window.
336 * \param mask This will be set to the current buttons state.
337 * \return True on success, false if an error occured.
339 static bool
340 mouse_query_pointer_root(int *s, int *x, int *y, uint16_t *mask)
342 for(int screen = 0;
343 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
344 screen++)
346 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
348 if(mouse_query_pointer(root, x, y, mask))
350 *s = screen;
351 return true;
354 return false;
357 /** Grab the Pointer.
358 * \param window The window grabbed.
359 * \param cursor The cursor to display (see struct.h CurNormal, CurResize, etc).
360 * \return True on success, false if an error occured.
362 static bool
363 mouse_grab_pointer(xcb_window_t window, size_t cursor)
365 xcb_grab_pointer_cookie_t grab_ptr_c;
366 xcb_grab_pointer_reply_t *grab_ptr_r;
368 if(cursor >= CurLast)
369 cursor = CurNormal;
371 grab_ptr_c = xcb_grab_pointer_unchecked(globalconf.connection, false, window,
372 MOUSEMASK, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
373 window, globalconf.cursor[cursor], XCB_CURRENT_TIME);
374 grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL);
376 if(!grab_ptr_r)
377 return false;
379 p_delete(&grab_ptr_r);
381 return true;
384 /** Ungrab the Pointer
386 static inline void
387 mouse_ungrab_pointer(void)
389 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
392 /** Set the pointer position.
393 * \param window The destination window.
394 * \param x X-coordinate inside window.
395 * \param y Y-coordinate inside window.
397 static inline void
398 mouse_warp_pointer(xcb_window_t window, int x, int y)
400 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
401 0, 0, 0, 0, x, y );
404 /** Utility function to help with mouse-dragging
406 * \param x set to x-coordinate of the last event on return
407 * \param y set to y-coordinate of the last event on return
408 * \return true if an motion event was received
409 * false if an button release event was received
411 static bool
412 mouse_track_mouse_drag(int *x, int *y)
414 xcb_generic_event_t *ev;
415 xcb_motion_notify_event_t *ev_motion;
416 xcb_button_release_event_t *ev_button;
418 while(true)
419 while((ev = xcb_wait_for_event(globalconf.connection)))
420 switch((ev->response_type & 0x7F))
423 case XCB_MOTION_NOTIFY:
424 ev_motion = (xcb_motion_notify_event_t*) ev;
425 *x = ev_motion->event_x;
426 *y = ev_motion->event_y;
427 p_delete(&ev);
428 return true;
430 case XCB_BUTTON_RELEASE:
431 ev_button = (xcb_button_release_event_t*) ev;
432 *x = ev_button->event_x;
433 *y = ev_button->event_y;
434 p_delete(&ev);
435 return false;
437 default:
438 xcb_event_handle(&globalconf.evenths, ev);
439 p_delete(&ev);
440 break;
444 /** Get the client that contains the pointer.
446 * \return The client that contains the pointer or NULL.
448 static client_t *
449 mouse_get_client_under_pointer(int phys_screen)
451 xcb_window_t root;
452 xcb_query_pointer_cookie_t query_ptr_c;
453 xcb_query_pointer_reply_t *query_ptr_r;
454 client_t *c = NULL;
456 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
458 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, root);
459 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
461 if(query_ptr_r)
463 c = client_getbywin(query_ptr_r->child);
464 p_delete(&query_ptr_r);
467 return c;
470 /** Move the focused window with the mouse.
471 * \param c The client.
472 * \param snap The maximum distance in pixels to trigger a "snap".
473 * \param infobox Enable or disable the infobox.
475 static void
476 mouse_client_move(client_t *c, int snap, bool infobox)
478 /* current mouse postion */
479 int mouse_x, mouse_y;
480 /* last mouse position */
481 int last_x = 0, last_y = 0;
482 /* current layout */
483 layout_t *layout;
484 /* the infobox */
485 simple_window_t sw;
486 /* the root window */
487 xcb_window_t root;
489 layout = layout_get_current(c->screen);
490 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
492 /* get current pointer position */
493 mouse_query_pointer(root, &last_x, &last_y, NULL);
495 /* grab pointer */
496 if(c->isfullscreen
497 || c->type == WINDOW_TYPE_DESKTOP
498 || c->type == WINDOW_TYPE_SPLASH
499 || c->type == WINDOW_TYPE_DOCK
500 || !mouse_grab_pointer(root, CurMove))
501 return;
503 if(infobox && (client_isfloating(c) || layout == layout_floating))
504 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
505 else
506 infobox = false;
508 /* for each motion event */
509 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
510 if(client_isfloating(c) || layout == layout_floating)
512 area_t geometry;
514 /* calc new geometry */
515 geometry = c->geometry;
516 geometry.x += (mouse_x - last_x);
517 geometry.y += (mouse_y - last_y);
519 /* snap and move */
520 geometry = mouse_snapclient(c, geometry, snap);
521 c->ismoving = true;
522 client_resize(c, geometry, false);
523 xcb_flush(globalconf.connection);
524 c->ismoving = false;
526 /* draw the infobox */
527 if(infobox)
528 mouse_infobox_draw(&sw, c->geometry, c->border);
530 /* refresh live */
531 wibox_refresh();
532 xcb_flush(globalconf.connection);
534 /* keep track */
535 last_x = mouse_x;
536 last_y = mouse_y;
538 else
540 int newscreen;
541 client_t *target;
543 /* client moved to another screen? */
544 newscreen = screen_getbycoord(c->screen, mouse_x, mouse_y);
545 if(newscreen != c->screen)
547 screen_client_moveto(c, newscreen, true, true);
548 globalconf.screens[c->screen].need_arrange = true;
549 globalconf.screens[newscreen].need_arrange = true;
550 layout_refresh();
551 wibox_refresh();
552 xcb_flush(globalconf.connection);
555 /* find client to swap with */
556 target = mouse_get_client_under_pointer(c->phys_screen);
558 /* swap position */
559 if(target && target != c && !target->isfloating)
561 client_list_swap(&globalconf.clients, c, target);
562 globalconf.screens[c->screen].need_arrange = true;
563 if(globalconf.hooks.clients != LUA_REFNIL)
564 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
565 layout_refresh();
566 wibox_refresh();
567 xcb_flush(globalconf.connection);
571 /* ungrab pointer */
572 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
574 /* free the infobox */
575 if(infobox)
576 simplewindow_wipe(&sw);
580 /** Resize a floating client with the mouse.
581 * \param c The client to resize.
582 * \param corner The corner to resize with.
583 * \param infobox Enable or disable the infobox.
585 static void
586 mouse_client_resize_floating(client_t *c, corner_t corner, bool infobox)
588 xcb_screen_t *screen;
589 /* one corner of the client has a fixed position */
590 int fixed_x, fixed_y;
591 /* the other is moved with the mouse */
592 int mouse_x = 0, mouse_y = 0;
593 /* the infobox */
594 simple_window_t sw;
595 size_t cursor = CurResize;
596 int top, bottom, left, right;
598 /* do not resize fixed client */
599 if(client_isfixed(c))
600 return;
602 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
604 /* get current mouse position */
605 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
607 top = c->geometry.y;
608 bottom = top + c->geometry.height;
609 left = c->geometry.x;
610 right = left + c->geometry.width;
612 /* figure out which corner to move */
613 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
615 /* the opposite corner is fixed */
616 fixed_x = (mouse_x == left) ? right : left;
617 fixed_y = (mouse_y == top) ? bottom : top;
619 /* select cursor */
620 switch(corner)
622 default:
623 cursor = CurTopLeft;
624 break;
625 case TopRightCorner:
626 cursor = CurTopRight;
627 break;
628 case BottomLeftCorner:
629 cursor = CurBotLeft;
630 break;
631 case BottomRightCorner:
632 cursor = CurBotRight;
633 break;
636 /* grab the pointer */
637 if(!mouse_grab_pointer(screen->root, cursor))
638 return;
640 /* set pointer to the moveable corner */
641 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
643 /* create the infobox */
644 if(infobox)
645 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
647 /* for each motion event */
648 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
650 /* new client geometry */
651 area_t geo = { .x = MIN(fixed_x, mouse_x),
652 .y = MIN(fixed_y, mouse_y),
653 .width = (MAX(fixed_x, mouse_x) - MIN(fixed_x, mouse_x)),
654 .height = (MAX(fixed_y, mouse_y) - MIN(fixed_y, mouse_y)) };
655 /* new moveable corner */
656 corner_t new_corner;
658 if(mouse_x == AREA_LEFT(geo))
659 new_corner = (mouse_y == AREA_TOP(geo)) ? TopLeftCorner : BottomLeftCorner;
660 else
661 new_corner = (mouse_y == AREA_TOP(geo)) ? TopRightCorner : BottomRightCorner;
663 /* update cursor */
664 if(corner != new_corner)
666 corner = new_corner;
668 switch(corner)
670 default: cursor = CurTopLeft; break;
671 case TopRightCorner: cursor = CurTopRight; break;
672 case BottomLeftCorner: cursor = CurBotLeft; break;
673 case BottomRightCorner: cursor = CurBotRight; break;
676 xcb_change_active_pointer_grab(globalconf.connection, globalconf.cursor[cursor],
677 XCB_CURRENT_TIME, MOUSEMASK);
680 if(c->hassizehints && c->honorsizehints)
682 int dx, dy;
684 /* apply size hints */
685 geo = client_geometry_hints(c, geo);
687 /* get the nonmoveable corner back onto fixed_x,fixed_y */
688 switch(corner)
690 default /* TopLeftCorner */:
691 dx = fixed_x - AREA_RIGHT(geo);
692 dy = fixed_y - AREA_BOTTOM(geo);
693 break;
694 case TopRightCorner:
695 dx = fixed_x - AREA_LEFT(geo);
696 dy = fixed_y - AREA_BOTTOM(geo);
697 break;
698 case BottomRightCorner:
699 dx = fixed_x - AREA_LEFT(geo);
700 dy = fixed_y - AREA_TOP(geo);
701 break;
702 case BottomLeftCorner:
703 dx = fixed_x - AREA_RIGHT(geo);
704 dy = fixed_y - AREA_TOP(geo);
705 break;
708 geo.x += dx;
709 geo.y += dy;
712 /* resize the client */
713 client_resize(c, geo, false);
715 /* refresh live */
716 wibox_refresh();
717 xcb_flush(globalconf.connection);
719 /* draw the infobox */
720 if(infobox)
721 mouse_infobox_draw(&sw, c->geometry, c->border);
724 /* relase pointer */
725 mouse_ungrab_pointer();
727 /* free the infobox */
728 if(infobox)
729 simplewindow_wipe(&sw);
732 /** Resize the master column/row of a tiled layout
733 * \param c A client on the tag/layout to resize.
735 static void
736 mouse_client_resize_tiled(client_t *c)
738 xcb_screen_t *screen;
739 /* screen area modulo wibox */
740 area_t area;
741 /* current tag */
742 tag_t *tag;
743 /* current layout */
744 layout_t *layout;
746 int mouse_x = 0, mouse_y = 0;
747 size_t cursor = CurResize;
749 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
750 tag = tags_get_current(c->screen)[0];
751 layout = tag->layout;
753 area = screen_area_get(tag->screen,
754 &globalconf.screens[tag->screen].wiboxes,
755 &globalconf.screens[tag->screen].padding,
756 true);
758 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
760 /* select initial pointer position */
761 if(layout == layout_tile)
763 mouse_x = area.x + area.width * tag->mwfact;
764 cursor = CurResizeH;
766 else if(layout == layout_tileleft)
768 mouse_x = area.x + area.width * (1. - tag->mwfact);
769 cursor = CurResizeH;
771 else if(layout == layout_tilebottom)
773 mouse_y = area.y + area.height * tag->mwfact;
774 cursor = CurResizeV;
776 else if(layout == layout_tiletop)
778 mouse_y = area.y + area.height * (1. - tag->mwfact);
779 cursor = CurResizeV;
781 else
782 return;
784 /* grab the pointer */
785 if(!mouse_grab_pointer(screen->root, cursor))
786 return;
788 /* set pointer to the moveable border */
789 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
791 xcb_flush(globalconf.connection);
793 /* for each motion event */
794 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
796 double mwfact = 0, fact_x, fact_y;
798 /* calculate new master / rest ratio */
799 fact_x = (double) (mouse_x - area.x) / area.width;
800 fact_y = (double) (mouse_y - area.y) / area.height;
802 if(layout == layout_tile)
803 mwfact = fact_x;
804 else if(layout == layout_tileleft)
805 mwfact = 1. - fact_x;
806 else if(layout == layout_tilebottom)
807 mwfact = fact_y;
808 else if(layout == layout_tiletop)
809 mwfact = 1. - fact_y;
811 /* keep mwfact within reasonable bounds */
812 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
814 /* refresh layout */
815 if(fabs(tag->mwfact - mwfact) >= 0.01)
817 tag->mwfact = mwfact;
818 globalconf.screens[tag->screen].need_arrange = true;
819 layout_refresh();
820 wibox_refresh();
821 xcb_flush(globalconf.connection);
825 /* relase pointer */
826 mouse_ungrab_pointer();
829 /** Resize the master client in mangifier layout
830 * \param c The client to resize.
831 * \param infobox Enable or disable the infobox.
833 static void
834 mouse_client_resize_magnified(client_t *c, bool infobox)
836 /* screen area modulo wibox */
837 area_t area;
838 /* center of area */
839 int center_x, center_y;
840 /* max. distance from the center */
841 double maxdist;
842 /* mouse position */
843 int mouse_x = 0, mouse_y = 0;
844 /* cursor while grabbing */
845 size_t cursor = CurResize;
846 corner_t corner = AutoCorner;
847 /* current tag */
848 tag_t *tag;
849 /* the infobox */
850 simple_window_t sw;
851 xcb_window_t root;
853 tag = tags_get_current(c->screen)[0];
855 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
857 area = screen_area_get(tag->screen,
858 &globalconf.screens[tag->screen].wiboxes,
859 &globalconf.screens[tag->screen].padding,
860 true);
862 center_x = area.x + (round(area.width / 2.));
863 center_y = area.y + (round(area.height / 2.));
865 maxdist = round(sqrt((area.width*area.width) + (area.height*area.height)) / 2.);
867 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
869 if(!mouse_query_pointer(root, &mouse_x, &mouse_y, NULL))
870 return;
872 /* select corner */
873 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
875 /* select cursor */
876 switch(corner)
878 default:
879 cursor = CurTopLeft;
880 break;
881 case TopRightCorner:
882 cursor = CurTopRight;
883 break;
884 case BottomLeftCorner:
885 cursor = CurBotLeft;
886 break;
887 case BottomRightCorner:
888 cursor = CurBotRight;
889 break;
892 /* grab pointer */
893 if(!mouse_grab_pointer(root, cursor))
894 return;
896 /* move pointer to corner */
897 mouse_warp_pointer(root, mouse_x, mouse_y);
899 /* create the infobox */
900 if(infobox)
901 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
903 /* for each motion event */
904 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
906 /* \todo keep pointer on screen diagonals */
907 double mwfact, dist, dx, dy;
909 /* calc distance from the center */
910 dx = center_x - mouse_x;
911 dy = center_y - mouse_y;
912 dist = sqrt((dx * dx) + (dy * dy));
914 /* new master/rest ratio */
915 mwfact = (dist * dist) / (maxdist * maxdist);
917 /* keep mwfact within reasonable bounds */
918 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
920 /* refresh the layout */
921 if(fabs(tag->mwfact - mwfact) >= 0.01)
923 tag->mwfact = mwfact;
924 globalconf.screens[tag->screen].need_arrange = true;
925 layout_refresh();
926 wibox_refresh();
927 xcb_flush(globalconf.connection);
930 /* draw the infobox */
931 if(infobox)
932 mouse_infobox_draw(&sw, c->geometry, c->border);
935 /* ungrab pointer */
936 mouse_ungrab_pointer();
938 /* free the infobox */
939 if(infobox)
940 simplewindow_wipe(&sw);
943 /** Resize a client with the mouse.
944 * \param c The client to resize.
945 * \param corner The corner to use.
946 * \param infobox Enable or disable the info box.
948 static void
949 mouse_client_resize(client_t *c, corner_t corner, bool infobox)
951 int n, screen;
952 tag_t **curtags;
953 layout_t *layout;
954 xcb_screen_t *s;
956 if(c->isfullscreen
957 || c->type == WINDOW_TYPE_DESKTOP
958 || c->type == WINDOW_TYPE_SPLASH
959 || c->type == WINDOW_TYPE_DOCK)
960 return;
962 curtags = tags_get_current(c->screen);
963 layout = curtags[0]->layout;
964 s = xutil_screen_get(globalconf.connection, c->phys_screen);
966 /* only handle floating, tiled and magnifier layouts */
967 if(layout == layout_floating || client_isfloating(c))
968 mouse_client_resize_floating(c, corner, infobox);
969 else if(layout == layout_tile || layout == layout_tileleft
970 || layout == layout_tilebottom || layout == layout_tiletop)
972 screen = c->screen;
973 for(n = 0, c = globalconf.clients; c; c = c->next)
974 if(IS_TILED(c, screen))
975 n++;
977 /* only masters on this screen? */
978 if(n <= curtags[0]->nmaster)
979 goto bailout;
981 /* no tiled clients on this screen? */
982 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
983 if(!c)
984 goto bailout;
986 mouse_client_resize_tiled(c);
988 else if(layout == layout_magnifier)
989 mouse_client_resize_magnified(c, infobox);
991 bailout:
992 p_delete(&curtags);
995 /** Resize a client with mouse.
996 * \param L The Lua VM state.
998 * \luastack
999 * \lvalue A client.
1000 * \lparam An optional table with keys: `corner', such as bottomleft,
1001 * topright, etc, to specify which corner to grab (default to auto) and
1002 * `infobox' to enable or disable the coordinates and dimensions box (default to
1003 * enabled).
1006 luaA_client_mouse_resize(lua_State *L)
1008 client_t **c = luaA_checkudata(L, 1, "client");
1009 corner_t corner = AutoCorner;
1010 bool infobox = true;
1011 size_t len;
1012 const char *buf;
1014 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1016 luaA_checktable(L, 2);
1017 buf = luaA_getopt_lstring(L, 2, "corner", "auto", &len);
1018 corner = a_strtocorner(buf, len);
1019 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1022 mouse_client_resize(*c, corner, infobox);
1024 return 0;
1027 /** Move a client with mouse.
1028 * \param L The Lua VM state.
1030 * \luastack
1031 * \lvalue A client.
1032 * \lparam An optional table with keys: `snap' for pixel to snap (default to 8), and
1033 * `infobox' to enable or disable the coordinates and dimensions box (default to
1034 * enabled).
1037 luaA_client_mouse_move(lua_State *L)
1039 client_t **c = luaA_checkudata(L, 1, "client");
1040 int snap = 8;
1041 bool infobox = true;
1043 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1045 luaA_checktable(L, 2);
1046 snap = luaA_getopt_number(L, 2, "snap", 8);
1047 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1050 mouse_client_move(*c, snap, infobox);
1052 return 0;
1055 /** Create a new mouse button bindings.
1056 * \param L The Lua VM state.
1057 * \return The number of elements pushed on stack.
1058 * \luastack
1059 * \lparam A table with modifiers keys, or a button to clone.
1060 * \lparam A mouse button number.
1061 * \lparam A function to execute on click events.
1062 * \lparam A function to execute on release events.
1063 * \lreturn A mouse button binding.
1065 static int
1066 luaA_button_new(lua_State *L)
1068 int i, len;
1069 button_t *button, **orig;
1071 if((orig = luaA_toudata(L, 2, "button")))
1073 button_t *copy = p_new(button_t, 1);
1074 copy->mod = (*orig)->mod;
1075 copy->button = (*orig)->button;
1076 if((*orig)->press != LUA_REFNIL)
1078 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
1079 luaA_registerfct(L, -1, &copy->press);
1081 else
1082 copy->press = LUA_REFNIL;
1083 if((*orig)->release != LUA_REFNIL)
1085 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
1086 luaA_registerfct(L, -1, &copy->release);
1088 else
1089 copy->release = LUA_REFNIL;
1090 return luaA_button_userdata_new(L, copy);
1093 luaA_checktable(L, 2);
1094 /* arg 3 is mouse button */
1095 i = luaL_checknumber(L, 3);
1096 /* arg 4 and 5 are callback functions */
1097 if(!lua_isnil(L, 4))
1098 luaA_checkfunction(L, 4);
1099 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1100 luaA_checkfunction(L, 5);
1102 button = p_new(button_t, 1);
1103 button->button = xutil_button_fromint(i);
1105 if(lua_isnil(L, 4))
1106 button->press = LUA_REFNIL;
1107 else
1108 luaA_registerfct(L, 4, &button->press);
1110 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1111 luaA_registerfct(L, 5, &button->release);
1112 else
1113 button->release = LUA_REFNIL;
1115 len = lua_objlen(L, 2);
1116 for(i = 1; i <= len; i++)
1118 size_t blen;
1119 const char *buf;
1120 lua_rawgeti(L, 2, i);
1121 buf = luaL_checklstring(L, -1, &blen);
1122 button->mod |= xutil_key_mask_fromstr(buf, blen);
1125 return luaA_button_userdata_new(L, button);
1128 /** Set a button array with a Lua table.
1129 * \param L The Lua VM state.
1130 * \param idx The index of the Lua table.
1131 * \param buttons The array button to fill.
1133 void
1134 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
1136 button_t **b;
1138 luaA_checktable(L, idx);
1139 button_array_wipe(buttons);
1140 button_array_init(buttons);
1141 lua_pushnil(L);
1142 while(lua_next(L, idx))
1144 b = luaA_checkudata(L, -1, "button");
1145 button_array_append(buttons, *b);
1146 button_ref(b);
1147 lua_pop(L, 1);
1151 /** Push an array of button as an Lua table onto the stack.
1152 * \param L The Lua VM state.
1153 * \param buttons The button array to push.
1154 * \return The number of elements pushed on stack.
1157 luaA_button_array_get(lua_State *L, button_array_t *buttons)
1159 luaA_otable_new(L);
1160 for(int i = 0; i < buttons->len; i++)
1162 luaA_button_userdata_new(L, buttons->tab[i]);
1163 lua_rawseti(L, -2, i + 1);
1165 return 1;
1168 /** Button object.
1169 * \param L The Lua VM state.
1170 * \return The number of elements pushed on stack.
1171 * \luastack
1172 * \lfield press The function called when button press event is received.
1173 * \lfield release The function called when button release event is received.
1175 static int
1176 luaA_button_index(lua_State *L)
1178 if(luaA_usemetatable(L, 1, 2))
1179 return 1;
1181 size_t len;
1182 button_t **button = luaA_checkudata(L, 1, "button");
1183 const char *attr = luaL_checklstring(L, 2, &len);
1185 switch(a_tokenize(attr, len))
1187 case A_TK_PRESS:
1188 if((*button)->press != LUA_REFNIL)
1189 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
1190 else
1191 lua_pushnil(L);
1192 break;
1193 case A_TK_RELEASE:
1194 if((*button)->release != LUA_REFNIL)
1195 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
1196 else
1197 lua_pushnil(L);
1198 break;
1199 case A_TK_BUTTON:
1200 /* works fine, but not *really* neat */
1201 lua_pushnumber(L, (*button)->button);
1202 break;
1203 default:
1204 break;
1207 return 1;
1210 /** Button object.
1211 * \param L The Lua VM state.
1212 * \return The number of elements pushed on stack.
1213 * \luastack
1215 static int
1216 luaA_button_newindex(lua_State *L)
1218 if(luaA_usemetatable(L, 1, 2))
1219 return 1;
1221 size_t len;
1222 button_t **button = luaA_checkudata(L, 1, "button");
1223 const char *attr = luaL_checklstring(L, 2, &len);
1225 switch(a_tokenize(attr, len))
1227 case A_TK_PRESS:
1228 luaA_registerfct(L, 3, &(*button)->press);
1229 break;
1230 case A_TK_RELEASE:
1231 luaA_registerfct(L, 3, &(*button)->release);
1232 break;
1233 case A_TK_BUTTON:
1234 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
1235 break;
1236 default:
1237 break;
1240 return 0;
1243 const struct luaL_reg awesome_button_methods[] =
1245 { "__call", luaA_button_new },
1246 { NULL, NULL }
1248 const struct luaL_reg awesome_button_meta[] =
1250 { "__index", luaA_button_index },
1251 { "__newindex", luaA_button_newindex },
1252 { "__gc", luaA_button_gc },
1253 { "__eq", luaA_button_eq },
1254 { "__tostring", luaA_button_tostring },
1255 { NULL, NULL }
1258 /** Mouse library.
1259 * \param L The Lua VM state.
1260 * \return The number of elements pushed on stack.
1261 * \luastack
1262 * \lfield coords Mouse coordinates.
1263 * \lfield screen Mouse screen number.
1265 static int
1266 luaA_mouse_index(lua_State *L)
1268 size_t len;
1269 const char *attr = luaL_checklstring(L, 2, &len);
1270 int mouse_x, mouse_y, i;
1271 int screen;
1273 switch(a_tokenize(attr, len))
1275 case A_TK_SCREEN:
1276 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL))
1277 return 0;
1279 i = screen_getbycoord(screen, mouse_x, mouse_y);
1281 lua_pushnumber(L, i + 1);
1282 break;
1283 default:
1284 return 0;
1287 return 1;
1290 /** Newindex for mouse.
1291 * \param L The Lua VM state.
1292 * \return The number of elements pushed on stack.
1294 static int
1295 luaA_mouse_newindex(lua_State *L)
1297 size_t len;
1298 const char *attr = luaL_checklstring(L, 2, &len);
1299 int x, y = 0;
1300 xcb_window_t root;
1301 int screen, phys_screen;
1303 switch(a_tokenize(attr, len))
1305 case A_TK_SCREEN:
1306 screen = luaL_checknumber(L, 3) - 1;
1307 luaA_checkscreen(screen);
1309 /* we need the physical one to get the root window */
1310 phys_screen = screen_virttophys(screen);
1311 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
1313 x = globalconf.screens[screen].geometry.x;
1314 y = globalconf.screens[screen].geometry.y;
1316 mouse_warp_pointer(root, x, y);
1317 break;
1318 default:
1319 return 0;
1322 return 0;
1325 /** Get or set the mouse coords.
1326 * \param L The Lua VM state.
1327 * \return The number of elements pushed on stack.
1328 * \luastack
1329 * \lparam None or a table with x and y keys as mouse coordinates.
1330 * \lreturn A table with mouse coordinates.
1332 static int
1333 luaA_mouse_coords(lua_State *L)
1335 uint16_t mask, maski;
1336 int screen, x, y, mouse_x, mouse_y, i = 1;
1338 if(lua_gettop(L) == 1)
1340 xcb_window_t root;
1342 luaA_checktable(L, 1);
1344 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1345 return 0;
1347 x = luaA_getopt_number(L, 1, "x", mouse_x);
1348 y = luaA_getopt_number(L, 1, "y", mouse_y);
1350 root = xutil_screen_get(globalconf.connection, screen)->root;
1351 mouse_warp_pointer(root, x, y);
1352 lua_pop(L, 1);
1355 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1356 return 0;
1358 lua_newtable(L);
1359 lua_pushnumber(L, mouse_x);
1360 lua_setfield(L, -2, "x");
1361 lua_pushnumber(L, mouse_y);
1362 lua_setfield(L, -2, "y");
1363 lua_newtable(L);
1364 for(maski = XCB_BUTTON_MASK_1; i <= XCB_BUTTON_MASK_5; maski <<= 1, i++)
1365 if(mask & maski)
1367 lua_pushboolean(L, true);
1368 lua_rawseti(L, -2, i);
1370 lua_setfield(L, -2, "buttons");
1372 return 1;
1375 /** Deprecated
1376 * \param L The Lua VM state.
1377 * \return The number of elements pushed on stack.
1379 static int
1380 luaA_mouse_new(lua_State *L)
1382 deprecate(L, "button()");
1383 return luaA_button_new(L);
1386 const struct luaL_reg awesome_mouse_methods[] =
1388 /* deprecated */
1389 { "__call", luaA_mouse_new },
1390 { "__index", luaA_mouse_index },
1391 { "__newindex", luaA_mouse_newindex },
1392 { "coords", luaA_mouse_coords },
1393 { NULL, NULL }
1395 const struct luaL_reg awesome_mouse_meta[] =
1397 { NULL, NULL }
1400 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80