client: check for focus on unmanage before removing client
[awesome.git] / mouse.c
blob3e4a2d8e1094f94e477509a37a82c577bd0314df
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 "layouts/floating.h"
31 #include "layouts/tile.h"
32 #include "layouts/magnifier.h"
34 #define MOUSEMASK (XCB_EVENT_MASK_BUTTON_PRESS \
35 | XCB_EVENT_MASK_BUTTON_RELEASE \
36 | XCB_EVENT_MASK_POINTER_MOTION)
38 extern awesome_t globalconf;
40 DO_LUA_NEW(static, button_t, mouse, "mouse", button_ref)
41 DO_LUA_GC(button_t, mouse, "mouse", button_unref)
42 DO_LUA_EQ(button_t, mouse, "mouse")
44 /** Define corners. */
45 typedef enum
47 AutoCorner,
48 TopRightCorner,
49 TopLeftCorner,
50 BottomLeftCorner,
51 BottomRightCorner
52 } corner_t;
54 /** Convert a corner name into a corner type.
55 * \param str A string.
56 * \param len String length.
57 * \return A corner type.
59 static corner_t
60 a_strtocorner(const char *str, size_t len)
62 switch (a_tokenize(str, len))
64 case A_TK_BOTTOMRIGHT: return BottomRightCorner;
65 case A_TK_BOTTOMLEFT: return BottomLeftCorner;
66 case A_TK_TOPLEFT: return TopLeftCorner;
67 case A_TK_TOPRIGHT: return TopRightCorner;
68 default: return AutoCorner;
72 /** Snap an area to the outside of an area.
73 * \param geometry geometry of the area to snap
74 * \param snap_geometry geometry of snapping area
75 * \param snap snap trigger in pixel
76 * \return snapped geometry
78 static area_t
79 mouse_snapclienttogeometry_outside(area_t geometry, area_t snap_geometry, int snap)
81 if(geometry.x < snap + snap_geometry.x + snap_geometry.width
82 && geometry.x > snap_geometry.x + snap_geometry.width)
83 geometry.x = snap_geometry.x + snap_geometry.width;
84 else if(geometry.x + geometry.width < snap_geometry.x
85 && geometry.x + geometry.width > snap_geometry.x - snap)
86 geometry.x = snap_geometry.x - geometry.width;
88 if(geometry.y < snap + snap_geometry.y + snap_geometry.height
89 && geometry.y > snap_geometry.y + snap_geometry.height)
90 geometry.y = snap_geometry.y + snap_geometry.height;
91 else if(geometry.y + geometry.height < snap_geometry.y
92 && geometry.y + geometry.height > snap_geometry.y - snap)
93 geometry.y = snap_geometry.y - geometry.height;
95 return geometry;
98 /** Snap an area to the inside of an area.
99 * \param geometry geometry of the area to snap
100 * \param snap_geometry geometry of snapping area
101 * \param snap snap trigger in pixel
102 * \return snapped geometry
104 static area_t
105 mouse_snapclienttogeometry_inside(area_t geometry, area_t snap_geometry, int snap)
107 if(abs(geometry.x) < snap + snap_geometry.x && geometry.x > snap_geometry.x)
108 geometry.x = snap_geometry.x;
109 else if(abs((snap_geometry.x + snap_geometry.width) - (geometry.x + geometry.width))
110 < snap)
111 geometry.x = snap_geometry.x + snap_geometry.width - geometry.width;
112 if(abs(geometry.y) < snap + snap_geometry.y && geometry.y > snap_geometry.y)
113 geometry.y = snap_geometry.y;
114 else if(abs((snap_geometry.y + snap_geometry.height) - (geometry.y + geometry.height))
115 < snap)
116 geometry.y = snap_geometry.y + snap_geometry.height - geometry.height;
118 return geometry;
121 /** Snap a client with a future geometry to the screen and other clients.
122 * \param c The client.
123 * \param geometry Geometry the client will get.
124 * \param snap The maximum distance in pixels to trigger a "snap".
125 * \return Geometry to set to the client.
127 static area_t
128 mouse_snapclient(client_t *c, area_t geometry, int snap)
130 client_t *snapper;
131 area_t snapper_geometry;
132 area_t screen_geometry =
133 screen_area_get(c->screen,
134 globalconf.screens[c->screen].statusbar,
135 &globalconf.screens[c->screen].padding);
137 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
138 geometry.width += 2 * c->border;
139 geometry.height += 2 * c->border;
141 geometry =
142 mouse_snapclienttogeometry_inside(geometry, screen_geometry, snap);
144 for(snapper = globalconf.clients; snapper; snapper = snapper->next)
145 if(snapper != c && client_isvisible(c, c->screen))
147 snapper_geometry = snapper->geometry;
148 snapper_geometry.width += 2 * c->border;
149 snapper_geometry.height += 2 * c->border;
150 snapper_geometry = titlebar_geometry_add(c->titlebar, c->border, snapper_geometry);
151 geometry =
152 mouse_snapclienttogeometry_outside(geometry,
153 snapper_geometry,
154 snap);
157 geometry.width -= 2 * c->border;
158 geometry.height -= 2 * c->border;
159 return titlebar_geometry_remove(c->titlebar, c->border, geometry);
162 /** Set coordinates to a corner of an area.
164 * \param a The area.
165 * \param[in,out] x The x coordinate.
166 * \param[in,out] y The y coordinate.
167 * \param corner The corner to snap to.
168 * \return The corner the coordinates have been set to. If corner != AutoCorner
169 * this is always equal to \em corner.
171 * \todo rename/move this is still awkward and might be useful somewhere else.
173 static corner_t
174 mouse_snap_to_corner(area_t a, int *x, int *y, corner_t corner)
176 int top, bottom, left, right;
178 top = AREA_TOP(a);
179 bottom = AREA_BOTTOM(a);
180 left = AREA_LEFT(a);
181 right = AREA_RIGHT(a);
183 /* figure out the nearser corner */
184 if(corner == AutoCorner)
186 if(abs(top - *y) < abs(bottom - *y))
188 if(abs(left - *x) < abs(right - *x))
189 corner = TopLeftCorner;
190 else
191 corner = TopRightCorner;
193 else
195 if(abs(left - *x) < abs(right - *x))
196 corner = BottomLeftCorner;
197 else
198 corner = BottomRightCorner;
202 switch(corner)
204 case TopRightCorner:
205 *x = right;
206 *y = top;
207 break;
209 case TopLeftCorner:
210 *x = left;
211 *y = top;
212 break;
214 case BottomLeftCorner:
215 *x = left;
216 *y = bottom;
217 break;
219 case BottomRightCorner:
220 *x = right;
221 *y = bottom;
222 break;
224 default:
225 break;
228 return corner;
231 /** Redraw the infobox.
232 * \param ctx Draw context.
233 * \param sw The simple window.
234 * \param geometry The geometry to use for the box.
235 * \param border The client border size.
237 static void
238 mouse_infobox_draw(draw_context_t *ctx,
239 simple_window_t *sw,
240 area_t geometry, int border)
242 area_t draw_geometry = { 0, 0, ctx->width, ctx->height };
243 char size[64];
244 size_t len;
246 len = snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
247 geometry.width, geometry.height, geometry.x, geometry.y);
248 draw_rectangle(ctx, draw_geometry, 1.0, true, &globalconf.colors.bg);
249 draw_text(ctx, globalconf.font, draw_geometry, size, len, NULL);
250 simplewindow_move(sw,
251 geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
252 geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);
253 simplewindow_refresh_pixmap(sw);
256 #define MOUSE_INFOBOX_STRING_DEFAULT "0000x0000+0000+0000"
258 /** Initialize the infobox window.
259 * \param phys_screen Physical screen number.
260 * \param border Border size of the client.
261 * \param geometry Client geometry.
262 * \param ctx Draw context to create.
263 * \return The simple window.
265 static simple_window_t *
266 mouse_infobox_new(int phys_screen, int border, area_t geometry,
267 draw_context_t **ctx)
269 simple_window_t *sw;
270 area_t geom;
271 draw_parser_data_t pdata;
273 draw_parser_data_init(&pdata);
275 geom = draw_text_extents(globalconf.connection,
276 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 sw = simplewindow_new(globalconf.connection, phys_screen,
285 geom.x, geom.y,
286 geom.width, geom.height, 0);
288 *ctx = draw_context_new(globalconf.connection, sw->phys_screen,
289 sw->geometry.width, sw->geometry.height,
290 sw->pixmap,
291 &globalconf.colors.fg,
292 &globalconf.colors.bg);
294 xcb_map_window(globalconf.connection, sw->window);
295 mouse_infobox_draw(*ctx, sw, geometry, border);
297 draw_parser_data_wipe(&pdata);
299 return sw;
302 /** Get the Pointer position
303 * \param window
304 * \param x will be set to the Pointer-x-coordinate relative to window
305 * \param y will be set to the Pointer-y-coordinate relative to window
306 * \param mask will be set to the current buttons state
307 * \return true on success, false if an error occured
309 static bool
310 mouse_query_pointer(xcb_window_t window, int *x, int *y, uint16_t *mask)
312 xcb_query_pointer_cookie_t query_ptr_c;
313 xcb_query_pointer_reply_t *query_ptr_r;
315 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
316 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
318 if(!query_ptr_r)
319 return false;
321 *x = query_ptr_r->win_x;
322 *y = query_ptr_r->win_y;
323 if (mask)
324 *mask = query_ptr_r->mask;
326 p_delete(&query_ptr_r);
328 return true;
331 /** Grab the Pointer.
332 * \param window The window grabbed.
333 * \param cursor The cursor to display (see struct.h CurNormal, CurResize, etc).
334 * \return True on success, false if an error occured.
336 static bool
337 mouse_grab_pointer(xcb_window_t window, size_t cursor)
339 xcb_grab_pointer_cookie_t grab_ptr_c;
340 xcb_grab_pointer_reply_t *grab_ptr_r;
342 if(cursor >= CurLast)
343 cursor = CurNormal;
345 grab_ptr_c = xcb_grab_pointer_unchecked(globalconf.connection, false, window,
346 MOUSEMASK, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
347 window, globalconf.cursor[cursor], XCB_CURRENT_TIME);
348 grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL);
350 if(!grab_ptr_r)
351 return false;
353 p_delete(&grab_ptr_r);
355 return true;
358 /** Ungrab the Pointer
360 static inline void
361 mouse_ungrab_pointer(void)
363 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
366 /** Set the Pointer position
367 * \param window the destination window
368 * \param x x-coordinate inside window
369 * \param y y-coordinate inside window
371 static inline void
372 mouse_warp_pointer(xcb_window_t window, int x, int y)
374 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
375 0, 0, 0, 0, x, y );
378 /** Utility function to help with mouse-dragging
380 * \param x set to x-coordinate of the last event on return
381 * \param y set to y-coordinate of the last event on return
382 * \return true if an motion event was received
383 * false if an button release event was received
385 static bool
386 mouse_track_mouse_drag(int *x, int *y)
388 xcb_generic_event_t *ev;
389 xcb_motion_notify_event_t *ev_motion;
390 xcb_button_release_event_t *ev_button;
392 while(true)
394 while((ev = xcb_wait_for_event(globalconf.connection)))
396 switch((ev->response_type & 0x7F))
399 case XCB_MOTION_NOTIFY:
400 ev_motion = (xcb_motion_notify_event_t*) ev;
401 *x = ev_motion->event_x;
402 *y = ev_motion->event_y;
403 p_delete(&ev);
404 return true;
406 case XCB_BUTTON_RELEASE:
407 ev_button = (xcb_button_release_event_t*) ev;
408 *x = ev_button->event_x;
409 *y = ev_button->event_y;
410 p_delete(&ev);
411 return false;
413 default:
414 xcb_handle_event(globalconf.evenths, ev);
415 p_delete(&ev);
416 break;
423 /** Get the client that contains the Pointer.
425 * \return The client that contains the Pointer or NULL.
427 static client_t *
428 mouse_get_client_under_pointer(void)
430 xcb_window_t root;
431 xcb_query_pointer_cookie_t query_ptr_c;
432 xcb_query_pointer_reply_t *query_ptr_r;
433 client_t *c = NULL;
435 root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
437 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, root);
438 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
440 if(query_ptr_r)
442 c = client_getbywin(query_ptr_r->child);
443 p_delete(&query_ptr_r);
446 return c;
449 /** Move the focused window with the mouse.
450 * \param c The client.
451 * \param snap The maximum distance in pixels to trigger a "snap".
452 * \param infobox Enable or disable the infobox.
454 static void
455 mouse_client_move(client_t *c, int snap, bool infobox)
457 /* current mouse postion */
458 int mouse_x, mouse_y;
459 /* last mouse position */
460 int last_x = 0, last_y = 0;
461 /* current layout */
462 layout_t *layout;
463 /* the infobox */
464 simple_window_t *sw = NULL;
465 draw_context_t *ctx;
466 /* the root window */
467 xcb_window_t root;
469 layout = layout_get_current(c->screen);
470 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
472 /* get current pointer position */
473 mouse_query_pointer(root, &last_x, &last_y, NULL);
475 /* grab pointer */
476 if(!mouse_grab_pointer(root, CurMove))
477 return;
479 c->ismax = false;
481 if(infobox && (c->isfloating || layout == layout_floating))
483 sw = mouse_infobox_new(c->phys_screen, c->border, c->geometry, &ctx);
484 xcb_aux_sync(globalconf.connection);
487 /* for each motion event */
488 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
489 if(c->isfloating || layout == layout_floating)
491 area_t geometry;
493 /* calc new geometry */
494 geometry = c->geometry;
495 geometry.x += (mouse_x - last_x);
496 geometry.y += (mouse_y - last_y);
498 /* snap and move */
499 geometry = mouse_snapclient(c, geometry, snap);
500 c->ismoving = true;
501 client_resize(c, geometry, false);
502 c->ismoving = false;
504 /* draw the infobox */
505 if(sw)
507 mouse_infobox_draw(ctx, sw, c->geometry, c->border);
508 xcb_aux_sync(globalconf.connection);
511 /* keep track */
512 last_x = mouse_x;
513 last_y = mouse_y;
515 else
517 int newscreen;
518 client_t *target;
520 /* client moved to another screen? */
521 newscreen = screen_get_bycoord(globalconf.screens_info, c->screen,
522 mouse_x, mouse_y);
523 if(newscreen != c->screen)
525 screen_client_moveto(c, newscreen, true);
526 globalconf.screens[c->screen].need_arrange = true;
527 globalconf.screens[newscreen].need_arrange = true;
528 layout_refresh();
531 /* find client to swap with */
532 target = mouse_get_client_under_pointer();
534 /* swap position */
535 if(target && target != c && !target->isfloating)
537 client_list_swap(&globalconf.clients, c, target);
538 globalconf.screens[c->screen].need_arrange = true;
539 layout_refresh();
543 /* ungrab pointer */
544 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
546 /* free the infobox */
547 if(sw)
549 draw_context_delete(&ctx);
550 simplewindow_delete(&sw);
555 /** Resize a floating client with the mouse.
556 * \param c The client to resize.
557 * \param corner The corner to resize with.
558 * \param infobox Enable or disable the infobox.
560 static void
561 mouse_client_resize_floating(client_t *c, corner_t corner, bool infobox)
563 xcb_screen_t *screen;
564 /* one corner of the client has a fixed position */
565 int fixed_x, fixed_y;
566 /* the other is moved with the mouse */
567 int mouse_x = 0, mouse_y = 0;
568 /* the infobox */
569 simple_window_t *sw = NULL;
570 draw_context_t *ctx;
571 size_t cursor = CurResize;
572 int top, bottom, left, right;
574 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
576 /* get current mouse position */
577 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
579 top = c->geometry.y;
580 bottom = top + c->geometry.height;
581 left = c->geometry.x;
582 right = left + c->geometry.width;
584 /* figure out which corner to move */
585 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
587 /* the opposite corner is fixed */
588 fixed_x = (mouse_x == left) ? right : left;
589 fixed_y = (mouse_y == top) ? bottom : top;
591 /* select cursor */
592 switch(corner)
594 default:
595 cursor = CurTopLeft;
596 break;
597 case TopRightCorner:
598 cursor = CurTopRight;
599 break;
600 case BottomLeftCorner:
601 cursor = CurBotLeft;
602 break;
603 case BottomRightCorner:
604 cursor = CurBotRight;
605 break;
608 /* grab the pointer */
609 if(!mouse_grab_pointer(screen->root, cursor))
610 return;
612 /* set pointer to the moveable corner */
613 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
615 /* create the infobox */
616 if(infobox)
618 sw = mouse_infobox_new(c->phys_screen, c->border, c->geometry, &ctx);
619 xcb_aux_sync(globalconf.connection);
622 /* for each motion event */
623 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
625 /* new client geometry */
626 area_t geo = { .x = MIN(fixed_x, mouse_x),
627 .y = MIN(fixed_y, mouse_y),
628 .width = (MAX(fixed_x, mouse_x) - MIN(fixed_x, mouse_x)),
629 .height = (MAX(fixed_y, mouse_y) - MIN(fixed_y, mouse_y)) };
630 /* new moveable corner */
631 corner_t new_corner;
633 if(mouse_x == AREA_LEFT(geo))
634 new_corner = (mouse_y == AREA_TOP(geo)) ? TopLeftCorner : BottomLeftCorner;
635 else
636 new_corner = (mouse_y == AREA_TOP(geo)) ? TopRightCorner : BottomRightCorner;
638 /* update cursor */
639 if(corner != new_corner)
641 corner = new_corner;
643 switch(corner)
645 default: cursor = CurTopLeft; break;
646 case TopRightCorner: cursor = CurTopRight; break;
647 case BottomLeftCorner: cursor = CurBotLeft; break;
648 case BottomRightCorner: cursor = CurBotRight; break;
651 xcb_change_active_pointer_grab(globalconf.connection, globalconf.cursor[cursor],
652 XCB_CURRENT_TIME, MOUSEMASK);
655 if(c->hassizehints && c->honorsizehints)
657 int dx, dy;
659 /* apply size hints */
660 geo = client_geometry_hints(c, geo);
662 /* get the nonmoveable corner back onto fixed_x,fixed_y */
663 switch(corner)
665 default /* TopLeftCorner */:
666 dx = fixed_x - AREA_RIGHT(geo);
667 dy = fixed_y - AREA_BOTTOM(geo);
668 break;
669 case TopRightCorner:
670 dx = fixed_x - AREA_LEFT(geo);
671 dy = fixed_y - AREA_BOTTOM(geo);
672 break;
673 case BottomRightCorner:
674 dx = fixed_x - AREA_LEFT(geo);
675 dy = fixed_y - AREA_TOP(geo);
676 break;
677 case BottomLeftCorner:
678 dx = fixed_x - AREA_RIGHT(geo);
679 dy = fixed_y - AREA_TOP(geo);
680 break;
683 geo.x += dx;
684 geo.y += dy;
687 /* resize the client */
688 client_resize(c, geo, false);
690 /* draw the infobox */
691 if(sw)
692 mouse_infobox_draw(ctx, sw, c->geometry, c->border);
694 xcb_aux_sync(globalconf.connection);
697 /* relase pointer */
698 mouse_ungrab_pointer();
700 /* free the infobox */
701 if(sw)
703 draw_context_delete(&ctx);
704 simplewindow_delete(&sw);
708 /** Resize the master column/row of a tiled layout
709 * \param c A client on the tag/layout to resize.
711 static void
712 mouse_client_resize_tiled(client_t *c)
714 xcb_screen_t *screen;
715 /* screen area modulo statusbar */
716 area_t area;
717 /* current tag */
718 tag_t *tag;
719 /* current layout */
720 layout_t *layout;
722 int mouse_x = 0, mouse_y = 0;
723 size_t cursor = CurResize;
725 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
726 tag = tags_get_current(c->screen)[0];
727 layout = tag->layout;
729 area = screen_area_get(tag->screen,
730 globalconf.screens[tag->screen].statusbar,
731 &globalconf.screens[tag->screen].padding);
733 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
735 /* select initial pointer position */
736 if(layout == layout_tile)
738 mouse_x = area.x + area.width * tag->mwfact;
739 cursor = CurResizeH;
741 else if(layout == layout_tileleft)
743 mouse_x = area.x + area.width * (1. - tag->mwfact);
744 cursor = CurResizeH;
746 else if(layout == layout_tilebottom)
748 mouse_y = area.y + area.height * tag->mwfact;
749 cursor = CurResizeV;
751 else if(layout == layout_tiletop)
753 mouse_y = area.y + area.height * (1. - tag->mwfact);
754 cursor = CurResizeV;
756 else
757 return;
759 /* grab the pointer */
760 if(!mouse_grab_pointer(screen->root, cursor))
761 return;
763 /* set pointer to the moveable border */
764 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
766 xcb_aux_sync(globalconf.connection);
768 /* for each motion event */
769 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
771 double mwfact = 0, fact_x, fact_y;
773 /* calculate new master / rest ratio */
774 fact_x = (double) (mouse_x - area.x) / area.width;
775 fact_y = (double) (mouse_y - area.y) / area.height;
777 if(layout == layout_tile)
778 mwfact = fact_x;
779 else if(layout == layout_tileleft)
780 mwfact = 1. - fact_x;
781 else if(layout == layout_tilebottom)
782 mwfact = fact_y;
783 else if(layout == layout_tiletop)
784 mwfact = 1. - fact_y;
786 /* keep mwfact within reasonable bounds */
787 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
789 /* refresh layout */
790 if(fabs(tag->mwfact - mwfact) >= 0.01)
792 tag->mwfact = mwfact;
793 globalconf.screens[tag->screen].need_arrange = true;
794 layout_refresh();
795 xcb_aux_sync(globalconf.connection);
799 /* relase pointer */
800 mouse_ungrab_pointer();
802 xcb_aux_sync(globalconf.connection);
805 /** Resize the master client in mangifier layout
806 * \param c The client to resize.
807 * \param infobox Enable or disable the infobox.
809 static void
810 mouse_client_resize_magnified(client_t *c, bool infobox)
812 /* screen area modulo statusbar */
813 area_t area;
814 /* center of area */
815 int center_x, center_y;
816 /* max. distance from the center */
817 double maxdist;
818 /* mouse position */
819 int mouse_x = 0, mouse_y = 0;
820 /* cursor while grabbing */
821 size_t cursor = CurResize;
822 corner_t corner = AutoCorner;
823 /* current tag */
824 tag_t *tag;
825 /* the infobox */
826 simple_window_t *sw = NULL;
827 draw_context_t *ctx;
828 xcb_window_t root;
830 tag = tags_get_current(c->screen)[0];
832 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
834 area = screen_area_get(tag->screen,
835 globalconf.screens[tag->screen].statusbar,
836 &globalconf.screens[tag->screen].padding);
838 center_x = area.x + (round(area.width / 2.));
839 center_y = area.y + (round(area.height / 2.));
841 maxdist = round(sqrt((area.width*area.width) + (area.height*area.height)) / 2.);
843 /* get current mouse position */
844 mouse_query_pointer(root, &mouse_x, &mouse_y, NULL);
846 /* select corner */
847 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
849 /* select cursor */
850 switch(corner)
852 default:
853 cursor = CurTopLeft;
854 break;
855 case TopRightCorner:
856 cursor = CurTopRight;
857 break;
858 case BottomLeftCorner:
859 cursor = CurBotLeft;
860 break;
861 case BottomRightCorner:
862 cursor = CurBotRight;
863 break;
866 /* grab pointer */
867 if(!mouse_grab_pointer(root, cursor))
868 return;
870 /* move pointer to corner */
871 mouse_warp_pointer(root, mouse_x, mouse_y);
873 /* create the infobox */
874 if(infobox)
875 sw = mouse_infobox_new(c->phys_screen, c->border, c->geometry, &ctx);
877 xcb_aux_sync(globalconf.connection);
879 /* for each motion event */
880 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
882 /* \todo keep pointer on screen diagonals */
883 double mwfact, dist, dx, dy;
885 /* calc distance from the center */
886 dx = center_x - mouse_x;
887 dy = center_y - mouse_y;
888 dist = sqrt((dx*dx) + (dy*dy));
890 /* new master/rest ratio */
891 mwfact = dist / maxdist;
893 /* keep mwfact within reasonable bounds */
894 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
896 /* refresh the layout */
897 if(fabs(tag->mwfact - mwfact) >= 0.01)
899 tag->mwfact = mwfact;
900 globalconf.screens[tag->screen].need_arrange = true;
901 layout_refresh();
904 /* draw the infobox */
905 if(sw)
907 mouse_infobox_draw(ctx, sw, c->geometry, c->border);
908 xcb_aux_sync(globalconf.connection);
912 /* ungrab pointer */
913 mouse_ungrab_pointer();
915 /* free the infobox */
916 if(sw)
918 draw_context_delete(&ctx);
919 simplewindow_delete(&sw);
923 /** Resize a client with the mouse.
924 * \param c The client to resize.
925 * \param corner The corner to use.
926 * \param infobox Enable or disable the info box.
928 static void
929 mouse_client_resize(client_t *c, corner_t corner, bool infobox)
931 int n, screen;
932 tag_t **curtags;
933 layout_t *layout;
934 xcb_screen_t *s;
936 curtags = tags_get_current(c->screen);
937 layout = curtags[0]->layout;
938 s = xutil_screen_get(globalconf.connection, c->phys_screen);
940 /* only handle floating, tiled and magnifier layouts */
941 if(layout == layout_floating || c->isfloating)
943 if(c->isfixed)
944 return;
946 c->ismax = false;
948 mouse_client_resize_floating(c, corner, infobox);
950 else if(layout == layout_tile || layout == layout_tileleft
951 || layout == layout_tilebottom || layout == layout_tiletop)
953 screen = c->screen;
954 for(n = 0, c = globalconf.clients; c; c = c->next)
955 if(IS_TILED(c, screen))
956 n++;
958 /* only masters on this screen? */
959 if(n <= curtags[0]->nmaster) return;
961 /* no tiled clients on this screen? */
962 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
963 if(!c) return;
965 mouse_client_resize_tiled(c);
967 else if(layout == layout_magnifier)
969 mouse_client_resize_magnified(c, infobox);
973 /** Resize a client with mouse.
974 * \param L The Lua VM state.
976 * \luastack
977 * \lvalue A client.
978 * \lparam An optional table with keys: `corner', such as bottomleft,
979 * topright, etc, to specify which corner to grab (default to auto) and
980 * `infobox' to enable or disable the coordinates and dimensions box (default to
981 * enabled).
984 luaA_client_mouse_resize(lua_State *L)
986 client_t **c = luaA_checkudata(L, 1, "client");
987 corner_t corner = AutoCorner;
988 bool infobox = true;
989 size_t len;
990 const char *buf;
992 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
994 luaA_checktable(L, 2);
995 buf = luaA_getopt_lstring(L, 2, "corner", "auto", &len);
996 corner = a_strtocorner(buf, len);
997 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1000 mouse_client_resize(*c, corner, infobox);
1002 return 0;
1005 /** Move a client with mouse.
1006 * \param L The Lua VM state.
1008 * \luastack
1009 * \lvalue A client.
1010 * \lparam An optional table with keys: `snap' for pixel to snap (default to 8), and
1011 * `infobox' to enable or disable the coordinates and dimensions box (default to
1012 * enabled).
1015 luaA_client_mouse_move(lua_State *L)
1017 client_t **c = luaA_checkudata(L, 1, "client");
1018 int snap = 8;
1019 bool infobox = true;
1021 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1023 luaA_checktable(L, 2);
1024 snap = luaA_getopt_number(L, 2, "snap", 8);
1025 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1028 mouse_client_move(*c, snap, infobox);
1030 return 0;
1033 /** Create a new mouse button bindings.
1034 * \param L The Lua VM state.
1035 * \return The number of elements pushed on stack.
1036 * \luastack
1037 * \lparam A table with modifiers keys.
1038 * \lparam A mouse button number.
1039 * \lparam A function to execute on click events.
1040 * \lreturn A mouse button binding.
1042 static int
1043 luaA_mouse_new(lua_State *L)
1045 int i, len;
1046 button_t *button;
1048 luaA_checktable(L, 2);
1049 /* arg 3 is mouse button */
1050 i = luaL_checknumber(L, 3);
1051 /* arg 4 is cmd to run */
1052 luaA_checkfunction(L, 4);
1054 button = p_new(button_t, 1);
1055 button->button = xutil_button_fromint(i);
1056 button->fct = luaL_ref(L, LUA_REGISTRYINDEX);
1058 len = lua_objlen(L, 2);
1059 for(i = 1; i <= len; i++)
1061 lua_rawgeti(L, 2, i);
1062 button->mod |= xutil_keymask_fromstr(luaL_checkstring(L, -1));
1065 return luaA_mouse_userdata_new(L, button);
1068 /** Index for mouse.
1069 * \param L The Lua VM state.
1070 * \return The number of elements pushed on stack.
1071 * \luastack
1072 * \lfield coords Mouse coordinates.
1073 * \lfield screen Mouse screen number.
1075 static int
1076 luaA_mouse_index(lua_State *L)
1078 size_t len;
1079 const char *attr = luaL_checklstring(L, 2, &len);
1080 int mouse_x, mouse_y, i = 0;
1081 uint16_t mask;
1082 xcb_window_t root;
1084 switch(a_tokenize(attr, len))
1086 case A_TK_COORDS:
1087 root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
1089 if(!mouse_query_pointer(root, &mouse_x, &mouse_y, &mask))
1090 return 0;
1092 lua_newtable(L);
1093 lua_pushnumber(L, mouse_x);
1094 lua_setfield(L, -2, "x");
1095 lua_pushnumber(L, mouse_y);
1096 lua_setfield(L, -2, "y");
1097 lua_newtable(L);
1098 if (mask & XCB_BUTTON_MASK_1)
1100 lua_pushnumber(L, 1);
1101 lua_rawseti(L, -2, ++i);
1103 if (mask & XCB_BUTTON_MASK_2)
1105 lua_pushnumber(L, 2);
1106 lua_rawseti(L, -2, ++i);
1108 if (mask & XCB_BUTTON_MASK_3)
1110 lua_pushnumber(L, 3);
1111 lua_rawseti(L, -2, ++i);
1113 if (mask & XCB_BUTTON_MASK_4)
1115 lua_pushnumber(L, 4);
1116 lua_rawseti(L, -2, ++i);
1118 if (mask & XCB_BUTTON_MASK_5)
1120 lua_pushnumber(L, 5);
1121 lua_rawseti(L, -2, ++i);
1123 lua_setfield(L, -2, "buttons");
1124 break;
1125 case A_TK_SCREEN:
1126 root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
1128 if(!mouse_query_pointer(root, &mouse_x, &mouse_y, NULL))
1129 return 0;
1131 i = screen_get_bycoord(globalconf.screens_info,
1132 globalconf.default_screen,
1133 mouse_x, mouse_y);
1135 lua_pushnumber(L, i + 1);
1136 break;
1137 default:
1138 return 0;
1141 return 1;
1144 /** Newindex for mouse.
1145 * \param L The Lua VM state.
1146 * \return The number of elements pushed on stack.
1148 static int
1149 luaA_mouse_newindex(lua_State *L)
1151 size_t len;
1152 const char *attr = luaL_checklstring(L, 2, &len);
1153 int mouse_x, mouse_y, x, y = 0;
1154 uint16_t mask;
1155 xcb_window_t root;
1157 switch(a_tokenize(attr, len))
1159 case A_TK_COORDS:
1160 luaA_checktable(L, 3);
1162 root = xutil_screen_get(globalconf.connection, globalconf.default_screen)->root;
1163 if(!mouse_query_pointer(root, &mouse_x, &mouse_y, &mask))
1164 return 0;
1166 x = luaA_getopt_number(L, 3, "x", mouse_x);
1167 y = luaA_getopt_number(L, 3, "y", mouse_y);
1169 mouse_warp_pointer(root, x, y);
1170 break;
1171 default:
1172 return 0;
1175 return 0;
1178 const struct luaL_reg awesome_mouse_methods[] =
1180 { "__call", luaA_mouse_new },
1181 { "__index", luaA_mouse_index },
1182 { "__newindex", luaA_mouse_newindex },
1183 { NULL, NULL }
1185 const struct luaL_reg awesome_mouse_meta[] =
1187 { "__gc", luaA_mouse_gc },
1188 { "__eq", luaA_mouse_eq },
1189 { NULL, NULL }
1192 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80