awesomerc.lua: use nano by default if no editor found in env
[awesome.git] / mouse.c
blob4987b345e93906e93c26f8741c95f8bb3c105410
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 /** Snap an area to the outside of an area.
74 * \param geometry geometry of the area to snap
75 * \param snap_geometry geometry of snapping area
76 * \param snap snap trigger in pixel
77 * \return snapped geometry
79 static area_t
80 mouse_snapclienttogeometry_outside(area_t geometry, area_t snap_geometry, int snap)
82 if(geometry.x < snap + snap_geometry.x + snap_geometry.width
83 && geometry.x > snap_geometry.x + snap_geometry.width)
84 geometry.x = snap_geometry.x + snap_geometry.width;
85 else if(geometry.x + geometry.width < snap_geometry.x
86 && geometry.x + geometry.width > snap_geometry.x - snap)
87 geometry.x = snap_geometry.x - geometry.width;
89 if(geometry.y < snap + snap_geometry.y + snap_geometry.height
90 && geometry.y > snap_geometry.y + snap_geometry.height)
91 geometry.y = snap_geometry.y + snap_geometry.height;
92 else if(geometry.y + geometry.height < snap_geometry.y
93 && geometry.y + geometry.height > snap_geometry.y - snap)
94 geometry.y = snap_geometry.y - geometry.height;
96 return geometry;
99 /** Snap an area to the inside of an area.
100 * \param geometry geometry of the area to snap
101 * \param snap_geometry geometry of snapping area
102 * \param snap snap trigger in pixel
103 * \return snapped geometry
105 static area_t
106 mouse_snapclienttogeometry_inside(area_t geometry, area_t snap_geometry, int snap)
108 if(abs(geometry.x) < snap + snap_geometry.x && geometry.x > snap_geometry.x)
109 geometry.x = snap_geometry.x;
110 else if(abs((snap_geometry.x + snap_geometry.width) - (geometry.x + geometry.width))
111 < snap)
112 geometry.x = snap_geometry.x + snap_geometry.width - geometry.width;
113 if(abs(geometry.y) < snap + snap_geometry.y && geometry.y > snap_geometry.y)
114 geometry.y = snap_geometry.y;
115 else if(abs((snap_geometry.y + snap_geometry.height) - (geometry.y + geometry.height))
116 < snap)
117 geometry.y = snap_geometry.y + snap_geometry.height - geometry.height;
119 return geometry;
122 /** Snap a client with a future geometry to the screen and other clients.
123 * \param c The client.
124 * \param geometry Geometry the client will get.
125 * \param snap The maximum distance in pixels to trigger a "snap".
126 * \return Geometry to set to the client.
128 static area_t
129 mouse_snapclient(client_t *c, area_t geometry, int snap)
131 client_t *snapper;
132 area_t snapper_geometry;
133 area_t screen_geometry =
134 screen_area_get(c->screen,
135 &globalconf.screens[c->screen].wiboxes,
136 &globalconf.screens[c->screen].padding,
137 false);
139 area_t screen_geometry_barless =
140 screen_area_get(c->screen,
141 NULL,
142 &globalconf.screens[c->screen].padding,
143 false);
145 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
147 geometry =
148 mouse_snapclienttogeometry_inside(geometry, screen_geometry, snap);
150 geometry =
151 mouse_snapclienttogeometry_inside(geometry, screen_geometry_barless, snap);
153 for(snapper = globalconf.clients; snapper; snapper = snapper->next)
154 if(snapper != c && client_isvisible(snapper, c->screen))
156 snapper_geometry = snapper->geometry;
157 snapper_geometry = titlebar_geometry_add(snapper->titlebar, snapper->border, snapper_geometry);
158 geometry =
159 mouse_snapclienttogeometry_outside(geometry,
160 snapper_geometry,
161 snap);
164 return titlebar_geometry_remove(c->titlebar, c->border, geometry);
167 /** Set coordinates to a corner of an area.
169 * \param a The area.
170 * \param[in,out] x The x coordinate.
171 * \param[in,out] y The y coordinate.
172 * \param corner The corner to snap to.
173 * \return The corner the coordinates have been set to. If corner != AutoCorner
174 * this is always equal to \em corner.
176 * \todo rename/move this is still awkward and might be useful somewhere else.
178 static corner_t
179 mouse_snap_to_corner(area_t a, int *x, int *y, corner_t corner)
181 int top, bottom, left, right;
183 top = AREA_TOP(a);
184 bottom = AREA_BOTTOM(a);
185 left = AREA_LEFT(a);
186 right = AREA_RIGHT(a);
188 /* figure out the nearser corner */
189 if(corner == AutoCorner)
191 if(abs(top - *y) < abs(bottom - *y))
193 if(abs(left - *x) < abs(right - *x))
194 corner = TopLeftCorner;
195 else
196 corner = TopRightCorner;
198 else
200 if(abs(left - *x) < abs(right - *x))
201 corner = BottomLeftCorner;
202 else
203 corner = BottomRightCorner;
207 switch(corner)
209 case TopRightCorner:
210 *x = right;
211 *y = top;
212 break;
214 case TopLeftCorner:
215 *x = left;
216 *y = top;
217 break;
219 case BottomLeftCorner:
220 *x = left;
221 *y = bottom;
222 break;
224 case BottomRightCorner:
225 *x = right;
226 *y = bottom;
227 break;
229 default:
230 break;
233 return corner;
236 /** Redraw the infobox.
237 * \param sw The simple window.
238 * \param geometry The geometry to use for the box.
239 * \param border The client border size.
241 static void
242 mouse_infobox_draw(simple_window_t *sw, area_t geometry, int border)
244 area_t draw_geometry = { 0, 0, sw->ctx.width, sw->ctx.height };
245 char size[64];
246 size_t len;
248 len = snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
249 geometry.width, geometry.height, geometry.x, geometry.y);
250 draw_rectangle(&sw->ctx, draw_geometry, 1.0, true, &globalconf.colors.bg);
251 draw_text(&sw->ctx, globalconf.font, draw_geometry, size, len, NULL);
252 simplewindow_move(sw,
253 geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
254 geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);
255 simplewindow_refresh_pixmap(sw);
256 xcb_flush(globalconf.connection);
259 #define MOUSE_INFOBOX_STRING_DEFAULT "0000x0000+0000+0000"
261 /** Initialize the infobox window.
262 * \param sw The simple window to init.
263 * \param phys_screen Physical screen number.
264 * \param border Border size of the client.
265 * \param geometry Client geometry.
266 * \return The simple window.
268 static void
269 mouse_infobox_new(simple_window_t *sw, int phys_screen, int border, area_t geometry)
271 area_t geom;
272 draw_parser_data_t pdata;
274 draw_parser_data_init(&pdata);
276 geom = draw_text_extents(globalconf.default_screen,
277 globalconf.font,
278 MOUSE_INFOBOX_STRING_DEFAULT,
279 sizeof(MOUSE_INFOBOX_STRING_DEFAULT)-1,
280 &pdata);
281 geom.x = geometry.x + ((2 * border + geometry.width) - geom.width) / 2;
282 geom.y = geometry.y + ((2 * border + geometry.height) - geom.height) / 2;
284 p_clear(sw, 1);
285 simplewindow_init(sw, phys_screen, geom, 0, East,
286 &globalconf.colors.fg, &globalconf.colors.bg);
288 xcb_map_window(globalconf.connection, sw->window);
289 mouse_infobox_draw(sw, geometry, border);
291 draw_parser_data_wipe(&pdata);
294 /** Get the pointer position.
295 * \param window The window to get position on.
296 * \param x will be set to the Pointer-x-coordinate relative to window
297 * \param y will be set to the Pointer-y-coordinate relative to window
298 * \param mask will be set to the current buttons state
299 * \return true on success, false if an error occured
301 static bool
302 mouse_query_pointer(xcb_window_t window, int *x, int *y, uint16_t *mask)
304 xcb_query_pointer_cookie_t query_ptr_c;
305 xcb_query_pointer_reply_t *query_ptr_r;
307 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
308 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
310 if(!query_ptr_r || !query_ptr_r->same_screen)
311 return false;
313 *x = query_ptr_r->win_x;
314 *y = query_ptr_r->win_y;
315 if (mask)
316 *mask = query_ptr_r->mask;
318 p_delete(&query_ptr_r);
320 return true;
323 /** Get the pointer position on the screen.
324 * \param screen This will be set to the screen number the mouse is on.
325 * \param x This will be set to the Pointer-x-coordinate relative to window.
326 * \param y This will be set to the Pointer-y-coordinate relative to window.
327 * \param mask This will be set to the current buttons state.
328 * \return True on success, false if an error occured.
330 static bool
331 mouse_query_pointer_root(int *s, int *x, int *y, uint16_t *mask)
333 for(int screen = 0;
334 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
335 screen++)
337 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
339 if(mouse_query_pointer(root, x, y, mask))
341 *s = screen;
342 return true;
345 return false;
348 /** Grab the Pointer.
349 * \param window The window grabbed.
350 * \param cursor The cursor to display (see struct.h CurNormal, CurResize, etc).
351 * \return True on success, false if an error occured.
353 static bool
354 mouse_grab_pointer(xcb_window_t window, size_t cursor)
356 xcb_grab_pointer_cookie_t grab_ptr_c;
357 xcb_grab_pointer_reply_t *grab_ptr_r;
359 if(cursor >= CurLast)
360 cursor = CurNormal;
362 grab_ptr_c = xcb_grab_pointer_unchecked(globalconf.connection, false, window,
363 MOUSEMASK, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
364 window, globalconf.cursor[cursor], XCB_CURRENT_TIME);
365 grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL);
367 if(!grab_ptr_r)
368 return false;
370 p_delete(&grab_ptr_r);
372 return true;
375 /** Ungrab the Pointer
377 static inline void
378 mouse_ungrab_pointer(void)
380 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
383 /** Set the pointer position.
384 * \param window The destination window.
385 * \param x X-coordinate inside window.
386 * \param y Y-coordinate inside window.
388 static inline void
389 mouse_warp_pointer(xcb_window_t window, int x, int y)
391 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
392 0, 0, 0, 0, x, y );
395 /** Utility function to help with mouse-dragging
397 * \param x set to x-coordinate of the last event on return
398 * \param y set to y-coordinate of the last event on return
399 * \return true if an motion event was received
400 * false if an button release event was received
402 static bool
403 mouse_track_mouse_drag(int *x, int *y)
405 xcb_generic_event_t *ev;
406 xcb_motion_notify_event_t *ev_motion;
407 xcb_button_release_event_t *ev_button;
409 while(true)
410 while((ev = xcb_wait_for_event(globalconf.connection)))
411 switch((ev->response_type & 0x7F))
414 case XCB_MOTION_NOTIFY:
415 ev_motion = (xcb_motion_notify_event_t*) ev;
416 *x = ev_motion->event_x;
417 *y = ev_motion->event_y;
418 p_delete(&ev);
419 return true;
421 case XCB_BUTTON_RELEASE:
422 ev_button = (xcb_button_release_event_t*) ev;
423 *x = ev_button->event_x;
424 *y = ev_button->event_y;
425 p_delete(&ev);
426 return false;
428 default:
429 xcb_event_handle(&globalconf.evenths, ev);
430 p_delete(&ev);
431 break;
435 /** Get the client that contains the pointer.
437 * \return The client that contains the pointer or NULL.
439 static client_t *
440 mouse_get_client_under_pointer(int phys_screen)
442 xcb_window_t root;
443 xcb_query_pointer_cookie_t query_ptr_c;
444 xcb_query_pointer_reply_t *query_ptr_r;
445 client_t *c = NULL;
447 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
449 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, root);
450 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
452 if(query_ptr_r)
454 c = client_getbywin(query_ptr_r->child);
455 p_delete(&query_ptr_r);
458 return c;
461 /** Move the focused window with the mouse.
462 * \param c The client.
463 * \param snap The maximum distance in pixels to trigger a "snap".
464 * \param infobox Enable or disable the infobox.
466 static void
467 mouse_client_move(client_t *c, int snap, bool infobox)
469 /* current mouse postion */
470 int mouse_x, mouse_y;
471 /* last mouse position */
472 int last_x = 0, last_y = 0;
473 /* current layout */
474 layout_t *layout;
475 /* the infobox */
476 simple_window_t sw;
477 /* the root window */
478 xcb_window_t root;
480 layout = layout_get_current(c->screen);
481 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
483 /* get current pointer position */
484 mouse_query_pointer(root, &last_x, &last_y, NULL);
486 /* grab pointer */
487 if(c->isfullscreen
488 || c->type == WINDOW_TYPE_DESKTOP
489 || c->type == WINDOW_TYPE_SPLASH
490 || c->type == WINDOW_TYPE_DOCK
491 || !mouse_grab_pointer(root, CurMove))
492 return;
494 if(infobox && (client_isfloating(c) || layout == layout_floating))
495 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
496 else
497 infobox = false;
499 /* for each motion event */
500 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
501 if(client_isfloating(c) || layout == layout_floating)
503 area_t geometry;
505 /* calc new geometry */
506 geometry = c->geometry;
507 geometry.x += (mouse_x - last_x);
508 geometry.y += (mouse_y - last_y);
510 /* snap and move */
511 geometry = mouse_snapclient(c, geometry, snap);
512 c->ismoving = true;
513 client_resize(c, geometry, false);
514 xcb_flush(globalconf.connection);
515 c->ismoving = false;
517 /* draw the infobox */
518 if(infobox)
519 mouse_infobox_draw(&sw, c->geometry, c->border);
521 /* refresh live */
522 wibox_refresh();
523 xcb_flush(globalconf.connection);
525 /* keep track */
526 last_x = mouse_x;
527 last_y = mouse_y;
529 else
531 int newscreen;
532 client_t *target;
534 /* client moved to another screen? */
535 newscreen = screen_getbycoord(c->screen, mouse_x, mouse_y);
536 if(newscreen != c->screen)
538 screen_client_moveto(c, newscreen, true, true);
539 globalconf.screens[c->screen].need_arrange = true;
540 globalconf.screens[newscreen].need_arrange = true;
541 layout_refresh();
542 wibox_refresh();
543 xcb_flush(globalconf.connection);
546 /* find client to swap with */
547 target = mouse_get_client_under_pointer(c->phys_screen);
549 /* swap position */
550 if(target && target != c && !target->isfloating)
552 client_list_swap(&globalconf.clients, c, target);
553 globalconf.screens[c->screen].need_arrange = true;
554 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
555 layout_refresh();
556 wibox_refresh();
557 xcb_flush(globalconf.connection);
561 /* ungrab pointer */
562 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
564 /* free the infobox */
565 if(infobox)
566 simplewindow_wipe(&sw);
570 /** Resize a floating client with the mouse.
571 * \param c The client to resize.
572 * \param corner The corner to resize with.
573 * \param infobox Enable or disable the infobox.
575 static void
576 mouse_client_resize_floating(client_t *c, corner_t corner, bool infobox)
578 xcb_screen_t *screen;
579 /* one corner of the client has a fixed position */
580 int fixed_x, fixed_y;
581 /* the other is moved with the mouse */
582 int mouse_x = 0, mouse_y = 0;
583 /* the infobox */
584 simple_window_t sw;
585 size_t cursor = CurResize;
586 int top, bottom, left, right;
588 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
590 /* get current mouse position */
591 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
593 top = c->geometry.y;
594 bottom = top + c->geometry.height;
595 left = c->geometry.x;
596 right = left + c->geometry.width;
598 /* figure out which corner to move */
599 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
601 /* the opposite corner is fixed */
602 fixed_x = (mouse_x == left) ? right : left;
603 fixed_y = (mouse_y == top) ? bottom : top;
605 /* select cursor */
606 switch(corner)
608 default:
609 cursor = CurTopLeft;
610 break;
611 case TopRightCorner:
612 cursor = CurTopRight;
613 break;
614 case BottomLeftCorner:
615 cursor = CurBotLeft;
616 break;
617 case BottomRightCorner:
618 cursor = CurBotRight;
619 break;
622 /* grab the pointer */
623 if(!mouse_grab_pointer(screen->root, cursor))
624 return;
626 /* set pointer to the moveable corner */
627 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
629 /* create the infobox */
630 if(infobox)
631 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
633 /* for each motion event */
634 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
636 /* new client geometry */
637 area_t geo = { .x = MIN(fixed_x, mouse_x),
638 .y = MIN(fixed_y, mouse_y),
639 .width = (MAX(fixed_x, mouse_x) - MIN(fixed_x, mouse_x)),
640 .height = (MAX(fixed_y, mouse_y) - MIN(fixed_y, mouse_y)) };
641 /* new moveable corner */
642 corner_t new_corner;
644 if(mouse_x == AREA_LEFT(geo))
645 new_corner = (mouse_y == AREA_TOP(geo)) ? TopLeftCorner : BottomLeftCorner;
646 else
647 new_corner = (mouse_y == AREA_TOP(geo)) ? TopRightCorner : BottomRightCorner;
649 /* update cursor */
650 if(corner != new_corner)
652 corner = new_corner;
654 switch(corner)
656 default: cursor = CurTopLeft; break;
657 case TopRightCorner: cursor = CurTopRight; break;
658 case BottomLeftCorner: cursor = CurBotLeft; break;
659 case BottomRightCorner: cursor = CurBotRight; break;
662 xcb_change_active_pointer_grab(globalconf.connection, globalconf.cursor[cursor],
663 XCB_CURRENT_TIME, MOUSEMASK);
666 if(c->hassizehints && c->honorsizehints)
668 int dx, dy;
670 /* apply size hints */
671 geo = client_geometry_hints(c, geo);
673 /* get the nonmoveable corner back onto fixed_x,fixed_y */
674 switch(corner)
676 default /* TopLeftCorner */:
677 dx = fixed_x - AREA_RIGHT(geo);
678 dy = fixed_y - AREA_BOTTOM(geo);
679 break;
680 case TopRightCorner:
681 dx = fixed_x - AREA_LEFT(geo);
682 dy = fixed_y - AREA_BOTTOM(geo);
683 break;
684 case BottomRightCorner:
685 dx = fixed_x - AREA_LEFT(geo);
686 dy = fixed_y - AREA_TOP(geo);
687 break;
688 case BottomLeftCorner:
689 dx = fixed_x - AREA_RIGHT(geo);
690 dy = fixed_y - AREA_TOP(geo);
691 break;
694 geo.x += dx;
695 geo.y += dy;
698 /* resize the client */
699 client_resize(c, geo, false);
701 /* refresh live */
702 wibox_refresh();
703 xcb_flush(globalconf.connection);
705 /* draw the infobox */
706 if(infobox)
707 mouse_infobox_draw(&sw, c->geometry, c->border);
710 /* relase pointer */
711 mouse_ungrab_pointer();
713 /* free the infobox */
714 if(infobox)
715 simplewindow_wipe(&sw);
718 /** Resize the master column/row of a tiled layout
719 * \param c A client on the tag/layout to resize.
721 static void
722 mouse_client_resize_tiled(client_t *c)
724 xcb_screen_t *screen;
725 /* screen area modulo wibox */
726 area_t area;
727 /* current tag */
728 tag_t *tag;
729 /* current layout */
730 layout_t *layout;
732 int mouse_x = 0, mouse_y = 0;
733 size_t cursor = CurResize;
735 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
736 tag = tags_get_current(c->screen)[0];
737 layout = tag->layout;
739 area = screen_area_get(tag->screen,
740 &globalconf.screens[tag->screen].wiboxes,
741 &globalconf.screens[tag->screen].padding,
742 true);
744 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
746 /* select initial pointer position */
747 if(layout == layout_tile)
749 mouse_x = area.x + area.width * tag->mwfact;
750 cursor = CurResizeH;
752 else if(layout == layout_tileleft)
754 mouse_x = area.x + area.width * (1. - tag->mwfact);
755 cursor = CurResizeH;
757 else if(layout == layout_tilebottom)
759 mouse_y = area.y + area.height * tag->mwfact;
760 cursor = CurResizeV;
762 else if(layout == layout_tiletop)
764 mouse_y = area.y + area.height * (1. - tag->mwfact);
765 cursor = CurResizeV;
767 else
768 return;
770 /* grab the pointer */
771 if(!mouse_grab_pointer(screen->root, cursor))
772 return;
774 /* set pointer to the moveable border */
775 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
777 xcb_flush(globalconf.connection);
779 /* for each motion event */
780 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
782 double mwfact = 0, fact_x, fact_y;
784 /* calculate new master / rest ratio */
785 fact_x = (double) (mouse_x - area.x) / area.width;
786 fact_y = (double) (mouse_y - area.y) / area.height;
788 if(layout == layout_tile)
789 mwfact = fact_x;
790 else if(layout == layout_tileleft)
791 mwfact = 1. - fact_x;
792 else if(layout == layout_tilebottom)
793 mwfact = fact_y;
794 else if(layout == layout_tiletop)
795 mwfact = 1. - fact_y;
797 /* keep mwfact within reasonable bounds */
798 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
800 /* refresh layout */
801 if(fabs(tag->mwfact - mwfact) >= 0.01)
803 tag->mwfact = mwfact;
804 globalconf.screens[tag->screen].need_arrange = true;
805 layout_refresh();
806 wibox_refresh();
807 xcb_flush(globalconf.connection);
811 /* relase pointer */
812 mouse_ungrab_pointer();
815 /** Resize the master client in mangifier layout
816 * \param c The client to resize.
817 * \param infobox Enable or disable the infobox.
819 static void
820 mouse_client_resize_magnified(client_t *c, bool infobox)
822 /* screen area modulo wibox */
823 area_t area;
824 /* center of area */
825 int center_x, center_y;
826 /* max. distance from the center */
827 double maxdist;
828 /* mouse position */
829 int mouse_x = 0, mouse_y = 0;
830 /* cursor while grabbing */
831 size_t cursor = CurResize;
832 corner_t corner = AutoCorner;
833 /* current tag */
834 tag_t *tag;
835 /* the infobox */
836 simple_window_t sw;
837 xcb_window_t root;
839 tag = tags_get_current(c->screen)[0];
841 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
843 area = screen_area_get(tag->screen,
844 &globalconf.screens[tag->screen].wiboxes,
845 &globalconf.screens[tag->screen].padding,
846 true);
848 center_x = area.x + (round(area.width / 2.));
849 center_y = area.y + (round(area.height / 2.));
851 maxdist = round(sqrt((area.width*area.width) + (area.height*area.height)) / 2.);
853 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
855 if(!mouse_query_pointer(root, &mouse_x, &mouse_y, NULL))
856 return;
858 /* select corner */
859 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
861 /* select cursor */
862 switch(corner)
864 default:
865 cursor = CurTopLeft;
866 break;
867 case TopRightCorner:
868 cursor = CurTopRight;
869 break;
870 case BottomLeftCorner:
871 cursor = CurBotLeft;
872 break;
873 case BottomRightCorner:
874 cursor = CurBotRight;
875 break;
878 /* grab pointer */
879 if(!mouse_grab_pointer(root, cursor))
880 return;
882 /* move pointer to corner */
883 mouse_warp_pointer(root, mouse_x, mouse_y);
885 /* create the infobox */
886 if(infobox)
887 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
889 /* for each motion event */
890 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
892 /* \todo keep pointer on screen diagonals */
893 double mwfact, dist, dx, dy;
895 /* calc distance from the center */
896 dx = center_x - mouse_x;
897 dy = center_y - mouse_y;
898 dist = sqrt((dx * dx) + (dy * dy));
900 /* new master/rest ratio */
901 mwfact = (dist * dist) / (maxdist * maxdist);
903 /* keep mwfact within reasonable bounds */
904 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
906 /* refresh the layout */
907 if(fabs(tag->mwfact - mwfact) >= 0.01)
909 tag->mwfact = mwfact;
910 globalconf.screens[tag->screen].need_arrange = true;
911 layout_refresh();
912 wibox_refresh();
913 xcb_flush(globalconf.connection);
916 /* draw the infobox */
917 if(infobox)
918 mouse_infobox_draw(&sw, c->geometry, c->border);
921 /* ungrab pointer */
922 mouse_ungrab_pointer();
924 /* free the infobox */
925 if(infobox)
926 simplewindow_wipe(&sw);
929 /** Resize a client with the mouse.
930 * \param c The client to resize.
931 * \param corner The corner to use.
932 * \param infobox Enable or disable the info box.
934 static void
935 mouse_client_resize(client_t *c, corner_t corner, bool infobox)
937 int n, screen;
938 tag_t **curtags;
939 layout_t *layout;
940 xcb_screen_t *s;
942 if(c->isfullscreen
943 || c->type == WINDOW_TYPE_DESKTOP
944 || c->type == WINDOW_TYPE_SPLASH
945 || c->type == WINDOW_TYPE_DOCK)
946 return;
948 curtags = tags_get_current(c->screen);
949 layout = curtags[0]->layout;
950 s = xutil_screen_get(globalconf.connection, c->phys_screen);
952 /* only handle floating, tiled and magnifier layouts */
953 if(layout == layout_floating || client_isfloating(c))
954 mouse_client_resize_floating(c, corner, infobox);
955 else if(layout == layout_tile || layout == layout_tileleft
956 || layout == layout_tilebottom || layout == layout_tiletop)
958 screen = c->screen;
959 for(n = 0, c = globalconf.clients; c; c = c->next)
960 if(IS_TILED(c, screen))
961 n++;
963 /* only masters on this screen? */
964 if(n <= curtags[0]->nmaster)
965 goto bailout;
967 /* no tiled clients on this screen? */
968 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
969 if(!c)
970 goto bailout;
972 mouse_client_resize_tiled(c);
974 else if(layout == layout_magnifier)
975 mouse_client_resize_magnified(c, infobox);
977 bailout:
978 p_delete(&curtags);
981 /** Resize a client with mouse.
982 * \param L The Lua VM state.
984 * \luastack
985 * \lvalue A client.
986 * \lparam An optional table with keys: `corner', such as bottomleft,
987 * topright, etc, to specify which corner to grab (default to auto) and
988 * `infobox' to enable or disable the coordinates and dimensions box (default to
989 * enabled).
992 luaA_client_mouse_resize(lua_State *L)
994 client_t **c = luaA_checkudata(L, 1, "client");
995 corner_t corner = AutoCorner;
996 bool infobox = true;
997 size_t len;
998 const char *buf;
1000 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1002 luaA_checktable(L, 2);
1003 buf = luaA_getopt_lstring(L, 2, "corner", "auto", &len);
1004 corner = a_strtocorner(buf, len);
1005 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1008 mouse_client_resize(*c, corner, infobox);
1010 return 0;
1013 /** Move a client with mouse.
1014 * \param L The Lua VM state.
1016 * \luastack
1017 * \lvalue A client.
1018 * \lparam An optional table with keys: `snap' for pixel to snap (default to 8), and
1019 * `infobox' to enable or disable the coordinates and dimensions box (default to
1020 * enabled).
1023 luaA_client_mouse_move(lua_State *L)
1025 client_t **c = luaA_checkudata(L, 1, "client");
1026 int snap = 8;
1027 bool infobox = true;
1029 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1031 luaA_checktable(L, 2);
1032 snap = luaA_getopt_number(L, 2, "snap", 8);
1033 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1036 mouse_client_move(*c, snap, infobox);
1038 return 0;
1041 /** Create a new mouse button bindings.
1042 * \param L The Lua VM state.
1043 * \return The number of elements pushed on stack.
1044 * \luastack
1045 * \lparam A table with modifiers keys, or a button to clone.
1046 * \lparam A mouse button number.
1047 * \lparam A function to execute on click events.
1048 * \lparam A function to execute on release events.
1049 * \lreturn A mouse button binding.
1051 static int
1052 luaA_button_new(lua_State *L)
1054 int i, len;
1055 button_t *button, **orig;
1057 if((orig = luaA_toudata(L, 2, "button")))
1059 button_t *copy = p_new(button_t, 1);
1060 copy->mod = (*orig)->mod;
1061 copy->button = (*orig)->button;
1062 if((*orig)->press != LUA_REFNIL)
1064 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
1065 luaA_registerfct(L, -1, &copy->press);
1067 else
1068 copy->press = LUA_REFNIL;
1069 if((*orig)->release != LUA_REFNIL)
1071 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
1072 luaA_registerfct(L, -1, &copy->release);
1074 else
1075 copy->release = LUA_REFNIL;
1076 return luaA_button_userdata_new(L, copy);
1079 luaA_checktable(L, 2);
1080 /* arg 3 is mouse button */
1081 i = luaL_checknumber(L, 3);
1082 /* arg 4 and 5 are callback functions */
1083 if(!lua_isnil(L, 4))
1084 luaA_checkfunction(L, 4);
1085 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1086 luaA_checkfunction(L, 5);
1088 button = p_new(button_t, 1);
1089 button->button = xutil_button_fromint(i);
1091 if(lua_isnil(L, 4))
1092 button->press = LUA_REFNIL;
1093 else
1094 luaA_registerfct(L, 4, &button->press);
1096 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1097 luaA_registerfct(L, 5, &button->release);
1098 else
1099 button->release = LUA_REFNIL;
1101 len = lua_objlen(L, 2);
1102 for(i = 1; i <= len; i++)
1104 size_t blen;
1105 const char *buf;
1106 lua_rawgeti(L, 2, i);
1107 buf = luaL_checklstring(L, -1, &blen);
1108 button->mod |= xutil_key_mask_fromstr(buf, blen);
1111 return luaA_button_userdata_new(L, button);
1114 /** Return a formated string for a button.
1115 * \param L The Lua VM state.
1116 * \luastack
1117 * \lvalue A button.
1118 * \lreturn A string.
1120 static int
1121 luaA_button_tostring(lua_State *L)
1123 button_t **p = luaA_checkudata(L, 1, "button");
1124 lua_pushfstring(L, "[button udata(%p)]", *p);
1125 return 1;
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);
1149 lua_pop(L, 1);
1152 /** Push an array of button as an Lua table onto the stack.
1153 * \param L The Lua VM state.
1154 * \param buttons The button array to push.
1155 * \return The number of elements pushed on stack.
1158 luaA_button_array_get(lua_State *L, button_array_t *buttons)
1160 luaA_otable_new(L);
1161 for(int i = 0; i < buttons->len; i++)
1163 luaA_button_userdata_new(L, buttons->tab[i]);
1164 lua_rawseti(L, -2, i + 1);
1166 return 1;
1169 /** Button object.
1170 * \param L The Lua VM state.
1171 * \return The number of elements pushed on stack.
1172 * \luastack
1173 * \lfield press The function called when button press event is received.
1174 * \lfield release The function called when button release event is received.
1176 static int
1177 luaA_button_index(lua_State *L)
1179 if(luaA_usemetatable(L, 1, 2))
1180 return 1;
1182 size_t len;
1183 button_t **button = luaA_checkudata(L, 1, "button");
1184 const char *attr = luaL_checklstring(L, 2, &len);
1186 switch(a_tokenize(attr, len))
1188 case A_TK_PRESS:
1189 if((*button)->press != LUA_REFNIL)
1190 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
1191 else
1192 lua_pushnil(L);
1193 break;
1194 case A_TK_RELEASE:
1195 if((*button)->release != LUA_REFNIL)
1196 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
1197 else
1198 lua_pushnil(L);
1199 break;
1200 case A_TK_BUTTON:
1201 /* works fine, but not *really* neat */
1202 lua_pushnumber(L, (*button)->button);
1203 break;
1204 default:
1205 break;
1208 return 1;
1211 /** Button object.
1212 * \param L The Lua VM state.
1213 * \return The number of elements pushed on stack.
1214 * \luastack
1216 static int
1217 luaA_button_newindex(lua_State *L)
1219 if(luaA_usemetatable(L, 1, 2))
1220 return 1;
1222 size_t len;
1223 button_t **button = luaA_checkudata(L, 1, "button");
1224 const char *attr = luaL_checklstring(L, 2, &len);
1226 switch(a_tokenize(attr, len))
1228 case A_TK_PRESS:
1229 luaA_registerfct(L, 3, &(*button)->press);
1230 break;
1231 case A_TK_RELEASE:
1232 luaA_registerfct(L, 3, &(*button)->release);
1233 break;
1234 case A_TK_BUTTON:
1235 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
1236 break;
1237 default:
1238 break;
1241 return 0;
1244 const struct luaL_reg awesome_button_methods[] =
1246 { "__call", luaA_button_new },
1247 { NULL, NULL }
1249 const struct luaL_reg awesome_button_meta[] =
1251 { "__index", luaA_button_index },
1252 { "__newindex", luaA_button_newindex },
1253 { "__gc", luaA_button_gc },
1254 { "__eq", luaA_button_eq },
1255 { "__tostring", luaA_button_tostring },
1256 { NULL, NULL }
1259 /** Mouse library.
1260 * \param L The Lua VM state.
1261 * \return The number of elements pushed on stack.
1262 * \luastack
1263 * \lfield coords Mouse coordinates.
1264 * \lfield screen Mouse screen number.
1266 static int
1267 luaA_mouse_index(lua_State *L)
1269 size_t len;
1270 const char *attr = luaL_checklstring(L, 2, &len);
1271 int mouse_x, mouse_y, i;
1272 int screen;
1274 switch(a_tokenize(attr, len))
1276 case A_TK_SCREEN:
1277 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL))
1278 return 0;
1280 i = screen_getbycoord(screen, mouse_x, mouse_y);
1282 lua_pushnumber(L, i + 1);
1283 break;
1284 default:
1285 return 0;
1288 return 1;
1291 /** Newindex for mouse.
1292 * \param L The Lua VM state.
1293 * \return The number of elements pushed on stack.
1295 static int
1296 luaA_mouse_newindex(lua_State *L)
1298 size_t len;
1299 const char *attr = luaL_checklstring(L, 2, &len);
1300 int x, y = 0;
1301 xcb_window_t root;
1302 int screen, phys_screen;
1304 switch(a_tokenize(attr, len))
1306 case A_TK_SCREEN:
1307 screen = luaL_checknumber(L, 3) - 1;
1308 luaA_checkscreen(screen);
1310 /* we need the physical one to get the root window */
1311 phys_screen = screen_virttophys(screen);
1312 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
1314 x = globalconf.screens[screen].geometry.x;
1315 y = globalconf.screens[screen].geometry.y;
1317 mouse_warp_pointer(root, x, y);
1318 break;
1319 default:
1320 return 0;
1323 return 0;
1326 /** Get or set the mouse coords.
1327 * \param L The Lua VM state.
1328 * \return The number of elements pushed on stack.
1329 * \luastack
1330 * \lparam None or a table with x and y keys as mouse coordinates.
1331 * \lreturn A table with mouse coordinates.
1333 static int
1334 luaA_mouse_coords(lua_State *L)
1336 uint16_t mask, maski;
1337 int screen, x, y, mouse_x, mouse_y, i = 1;
1339 if(lua_gettop(L) == 1)
1341 xcb_window_t root;
1343 luaA_checktable(L, 1);
1345 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1346 return 0;
1348 x = luaA_getopt_number(L, 1, "x", mouse_x);
1349 y = luaA_getopt_number(L, 1, "y", mouse_y);
1351 root = xutil_screen_get(globalconf.connection, screen)->root;
1352 mouse_warp_pointer(root, x, y);
1353 lua_pop(L, 1);
1356 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1357 return 0;
1359 lua_newtable(L);
1360 lua_pushnumber(L, mouse_x);
1361 lua_setfield(L, -2, "x");
1362 lua_pushnumber(L, mouse_y);
1363 lua_setfield(L, -2, "y");
1364 lua_newtable(L);
1365 for(maski = XCB_BUTTON_MASK_1; i <= XCB_BUTTON_MASK_5; maski <<= 1, i++)
1366 if(mask & maski)
1368 lua_pushboolean(L, true);
1369 lua_rawseti(L, -2, i);
1371 lua_setfield(L, -2, "buttons");
1373 return 1;
1376 const struct luaL_reg awesome_mouse_methods[] =
1378 { "__index", luaA_mouse_index },
1379 { "__newindex", luaA_mouse_newindex },
1380 { "coords", luaA_mouse_coords },
1381 { NULL, NULL }
1383 const struct luaL_reg awesome_mouse_meta[] =
1385 { NULL, NULL }
1388 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80