invaders: fix indention and spacing
[awesome.git] / mouse.c
blobc77396c3d544d1e5a0b1a5e858b5705f7926f274
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;
256 len = snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
257 geometry.width, geometry.height, geometry.x, geometry.y);
258 draw_rectangle(&sw->ctx, draw_geometry, 1.0, true, &globalconf.colors.bg);
259 draw_text(&sw->ctx, globalconf.font, draw_geometry, size, len, NULL);
260 simplewindow_move(sw,
261 geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
262 geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);
263 simplewindow_refresh_pixmap(sw);
264 xcb_flush(globalconf.connection);
267 #define MOUSE_INFOBOX_STRING_DEFAULT "0000x0000+0000+0000"
269 /** Initialize the infobox window.
270 * \param sw The simple window to init.
271 * \param phys_screen Physical screen number.
272 * \param border Border size of the client.
273 * \param geometry Client geometry.
274 * \return The simple window.
276 static void
277 mouse_infobox_new(simple_window_t *sw, int phys_screen, int border, area_t geometry)
279 area_t geom;
280 draw_parser_data_t pdata;
282 draw_parser_data_init(&pdata);
284 geom = draw_text_extents(globalconf.default_screen,
285 globalconf.font,
286 MOUSE_INFOBOX_STRING_DEFAULT,
287 sizeof(MOUSE_INFOBOX_STRING_DEFAULT)-1,
288 &pdata);
289 geom.x = geometry.x + ((2 * border + geometry.width) - geom.width) / 2;
290 geom.y = geometry.y + ((2 * border + geometry.height) - geom.height) / 2;
292 p_clear(sw, 1);
293 simplewindow_init(sw, phys_screen, geom, 0, East,
294 &globalconf.colors.fg, &globalconf.colors.bg);
296 xcb_map_window(globalconf.connection, sw->window);
297 mouse_infobox_draw(sw, geometry, border);
299 draw_parser_data_wipe(&pdata);
302 /** Get the pointer position.
303 * \param window The window to get position on.
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 || !query_ptr_r->same_screen)
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 /** Get the pointer position on the screen.
332 * \param screen This will be set to the screen number the mouse is on.
333 * \param x This will be set to the Pointer-x-coordinate relative to window.
334 * \param y This will be set to the Pointer-y-coordinate relative to window.
335 * \param mask This will be set to the current buttons state.
336 * \return True on success, false if an error occured.
338 static bool
339 mouse_query_pointer_root(int *s, int *x, int *y, uint16_t *mask)
341 for(int screen = 0;
342 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
343 screen++)
345 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
347 if(mouse_query_pointer(root, x, y, mask))
349 *s = screen;
350 return true;
353 return false;
356 /** Grab the Pointer.
357 * \param window The window grabbed.
358 * \param cursor The cursor to display (see struct.h CurNormal, CurResize, etc).
359 * \return True on success, false if an error occured.
361 static bool
362 mouse_grab_pointer(xcb_window_t window, size_t cursor)
364 xcb_grab_pointer_cookie_t grab_ptr_c;
365 xcb_grab_pointer_reply_t *grab_ptr_r;
367 if(cursor >= CurLast)
368 cursor = CurNormal;
370 grab_ptr_c = xcb_grab_pointer_unchecked(globalconf.connection, false, window,
371 MOUSEMASK, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
372 window, globalconf.cursor[cursor], XCB_CURRENT_TIME);
373 grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL);
375 if(!grab_ptr_r)
376 return false;
378 p_delete(&grab_ptr_r);
380 return true;
383 /** Ungrab the Pointer
385 static inline void
386 mouse_ungrab_pointer(void)
388 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
391 /** Set the pointer position.
392 * \param window The destination window.
393 * \param x X-coordinate inside window.
394 * \param y Y-coordinate inside window.
396 static inline void
397 mouse_warp_pointer(xcb_window_t window, int x, int y)
399 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
400 0, 0, 0, 0, x, y );
403 /** Utility function to help with mouse-dragging
405 * \param x set to x-coordinate of the last event on return
406 * \param y set to y-coordinate of the last event on return
407 * \return true if an motion event was received
408 * false if an button release event was received
410 static bool
411 mouse_track_mouse_drag(int *x, int *y)
413 xcb_generic_event_t *ev;
414 xcb_motion_notify_event_t *ev_motion;
415 xcb_button_release_event_t *ev_button;
417 while(true)
418 while((ev = xcb_wait_for_event(globalconf.connection)))
419 switch((ev->response_type & 0x7F))
422 case XCB_MOTION_NOTIFY:
423 ev_motion = (xcb_motion_notify_event_t*) ev;
424 *x = ev_motion->event_x;
425 *y = ev_motion->event_y;
426 p_delete(&ev);
427 return true;
429 case XCB_BUTTON_RELEASE:
430 ev_button = (xcb_button_release_event_t*) ev;
431 *x = ev_button->event_x;
432 *y = ev_button->event_y;
433 p_delete(&ev);
434 return false;
436 default:
437 xcb_event_handle(&globalconf.evenths, ev);
438 p_delete(&ev);
439 break;
443 /** Get the client that contains the pointer.
445 * \return The client that contains the pointer or NULL.
447 static client_t *
448 mouse_get_client_under_pointer(int phys_screen)
450 xcb_window_t root;
451 xcb_query_pointer_cookie_t query_ptr_c;
452 xcb_query_pointer_reply_t *query_ptr_r;
453 client_t *c = NULL;
455 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
457 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, root);
458 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
460 if(query_ptr_r)
462 c = client_getbywin(query_ptr_r->child);
463 p_delete(&query_ptr_r);
466 return c;
469 /** Move the focused window with the mouse.
470 * \param c The client.
471 * \param snap The maximum distance in pixels to trigger a "snap".
472 * \param infobox Enable or disable the infobox.
474 static void
475 mouse_client_move(client_t *c, int snap, bool infobox)
477 /* current mouse postion */
478 int mouse_x, mouse_y;
479 /* last mouse position */
480 int last_x = 0, last_y = 0;
481 /* current layout */
482 layout_t *layout;
483 /* the infobox */
484 simple_window_t sw;
485 /* the root window */
486 xcb_window_t root;
488 layout = layout_get_current(c->screen);
489 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
491 /* get current pointer position */
492 mouse_query_pointer(root, &last_x, &last_y, NULL);
494 /* grab pointer */
495 if(c->isfullscreen
496 || c->type == WINDOW_TYPE_DESKTOP
497 || c->type == WINDOW_TYPE_SPLASH
498 || c->type == WINDOW_TYPE_DOCK
499 || !mouse_grab_pointer(root, CurMove))
500 return;
502 if(infobox && (client_isfloating(c) || layout == layout_floating))
503 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
504 else
505 infobox = false;
507 /* for each motion event */
508 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
509 if(client_isfloating(c) || layout == layout_floating)
511 area_t geometry;
513 /* calc new geometry */
514 geometry = c->geometry;
515 geometry.x += (mouse_x - last_x);
516 geometry.y += (mouse_y - last_y);
518 /* snap and move */
519 geometry = mouse_snapclient(c, geometry, snap);
520 c->ismoving = true;
521 client_resize(c, geometry, false);
522 xcb_flush(globalconf.connection);
523 c->ismoving = false;
525 /* draw the infobox */
526 if(infobox)
527 mouse_infobox_draw(&sw, c->geometry, c->border);
529 /* refresh live */
530 wibox_refresh();
531 xcb_flush(globalconf.connection);
533 /* keep track */
534 last_x = mouse_x;
535 last_y = mouse_y;
537 else
539 int newscreen;
540 client_t *target;
542 /* client moved to another screen? */
543 newscreen = screen_getbycoord(c->screen, mouse_x, mouse_y);
544 if(newscreen != c->screen)
546 screen_client_moveto(c, newscreen, true, true);
547 globalconf.screens[c->screen].need_arrange = true;
548 globalconf.screens[newscreen].need_arrange = true;
549 layout_refresh();
550 wibox_refresh();
551 xcb_flush(globalconf.connection);
554 /* find client to swap with */
555 target = mouse_get_client_under_pointer(c->phys_screen);
557 /* swap position */
558 if(target && target != c && !target->isfloating)
560 client_list_swap(&globalconf.clients, c, target);
561 globalconf.screens[c->screen].need_arrange = true;
562 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
563 layout_refresh();
564 wibox_refresh();
565 xcb_flush(globalconf.connection);
569 /* ungrab pointer */
570 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
572 /* free the infobox */
573 if(infobox)
574 simplewindow_wipe(&sw);
578 /** Resize a floating client with the mouse.
579 * \param c The client to resize.
580 * \param corner The corner to resize with.
581 * \param infobox Enable or disable the infobox.
583 static void
584 mouse_client_resize_floating(client_t *c, corner_t corner, bool infobox)
586 xcb_screen_t *screen;
587 /* one corner of the client has a fixed position */
588 int fixed_x, fixed_y;
589 /* the other is moved with the mouse */
590 int mouse_x = 0, mouse_y = 0;
591 /* the infobox */
592 simple_window_t sw;
593 size_t cursor = CurResize;
594 int top, bottom, left, right;
596 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
598 /* get current mouse position */
599 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
601 top = c->geometry.y;
602 bottom = top + c->geometry.height;
603 left = c->geometry.x;
604 right = left + c->geometry.width;
606 /* figure out which corner to move */
607 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
609 /* the opposite corner is fixed */
610 fixed_x = (mouse_x == left) ? right : left;
611 fixed_y = (mouse_y == top) ? bottom : top;
613 /* select cursor */
614 switch(corner)
616 default:
617 cursor = CurTopLeft;
618 break;
619 case TopRightCorner:
620 cursor = CurTopRight;
621 break;
622 case BottomLeftCorner:
623 cursor = CurBotLeft;
624 break;
625 case BottomRightCorner:
626 cursor = CurBotRight;
627 break;
630 /* grab the pointer */
631 if(!mouse_grab_pointer(screen->root, cursor))
632 return;
634 /* set pointer to the moveable corner */
635 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
637 /* create the infobox */
638 if(infobox)
639 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
641 /* for each motion event */
642 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
644 /* new client geometry */
645 area_t geo = { .x = MIN(fixed_x, mouse_x),
646 .y = MIN(fixed_y, mouse_y),
647 .width = (MAX(fixed_x, mouse_x) - MIN(fixed_x, mouse_x)),
648 .height = (MAX(fixed_y, mouse_y) - MIN(fixed_y, mouse_y)) };
649 /* new moveable corner */
650 corner_t new_corner;
652 if(mouse_x == AREA_LEFT(geo))
653 new_corner = (mouse_y == AREA_TOP(geo)) ? TopLeftCorner : BottomLeftCorner;
654 else
655 new_corner = (mouse_y == AREA_TOP(geo)) ? TopRightCorner : BottomRightCorner;
657 /* update cursor */
658 if(corner != new_corner)
660 corner = new_corner;
662 switch(corner)
664 default: cursor = CurTopLeft; break;
665 case TopRightCorner: cursor = CurTopRight; break;
666 case BottomLeftCorner: cursor = CurBotLeft; break;
667 case BottomRightCorner: cursor = CurBotRight; break;
670 xcb_change_active_pointer_grab(globalconf.connection, globalconf.cursor[cursor],
671 XCB_CURRENT_TIME, MOUSEMASK);
674 if(c->hassizehints && c->honorsizehints)
676 int dx, dy;
678 /* apply size hints */
679 geo = client_geometry_hints(c, geo);
681 /* get the nonmoveable corner back onto fixed_x,fixed_y */
682 switch(corner)
684 default /* TopLeftCorner */:
685 dx = fixed_x - AREA_RIGHT(geo);
686 dy = fixed_y - AREA_BOTTOM(geo);
687 break;
688 case TopRightCorner:
689 dx = fixed_x - AREA_LEFT(geo);
690 dy = fixed_y - AREA_BOTTOM(geo);
691 break;
692 case BottomRightCorner:
693 dx = fixed_x - AREA_LEFT(geo);
694 dy = fixed_y - AREA_TOP(geo);
695 break;
696 case BottomLeftCorner:
697 dx = fixed_x - AREA_RIGHT(geo);
698 dy = fixed_y - AREA_TOP(geo);
699 break;
702 geo.x += dx;
703 geo.y += dy;
706 /* resize the client */
707 client_resize(c, geo, false);
709 /* refresh live */
710 wibox_refresh();
711 xcb_flush(globalconf.connection);
713 /* draw the infobox */
714 if(infobox)
715 mouse_infobox_draw(&sw, c->geometry, c->border);
718 /* relase pointer */
719 mouse_ungrab_pointer();
721 /* free the infobox */
722 if(infobox)
723 simplewindow_wipe(&sw);
726 /** Resize the master column/row of a tiled layout
727 * \param c A client on the tag/layout to resize.
729 static void
730 mouse_client_resize_tiled(client_t *c)
732 xcb_screen_t *screen;
733 /* screen area modulo wibox */
734 area_t area;
735 /* current tag */
736 tag_t *tag;
737 /* current layout */
738 layout_t *layout;
740 int mouse_x = 0, mouse_y = 0;
741 size_t cursor = CurResize;
743 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
744 tag = tags_get_current(c->screen)[0];
745 layout = tag->layout;
747 area = screen_area_get(tag->screen,
748 &globalconf.screens[tag->screen].wiboxes,
749 &globalconf.screens[tag->screen].padding,
750 true);
752 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
754 /* select initial pointer position */
755 if(layout == layout_tile)
757 mouse_x = area.x + area.width * tag->mwfact;
758 cursor = CurResizeH;
760 else if(layout == layout_tileleft)
762 mouse_x = area.x + area.width * (1. - tag->mwfact);
763 cursor = CurResizeH;
765 else if(layout == layout_tilebottom)
767 mouse_y = area.y + area.height * tag->mwfact;
768 cursor = CurResizeV;
770 else if(layout == layout_tiletop)
772 mouse_y = area.y + area.height * (1. - tag->mwfact);
773 cursor = CurResizeV;
775 else
776 return;
778 /* grab the pointer */
779 if(!mouse_grab_pointer(screen->root, cursor))
780 return;
782 /* set pointer to the moveable border */
783 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
785 xcb_flush(globalconf.connection);
787 /* for each motion event */
788 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
790 double mwfact = 0, fact_x, fact_y;
792 /* calculate new master / rest ratio */
793 fact_x = (double) (mouse_x - area.x) / area.width;
794 fact_y = (double) (mouse_y - area.y) / area.height;
796 if(layout == layout_tile)
797 mwfact = fact_x;
798 else if(layout == layout_tileleft)
799 mwfact = 1. - fact_x;
800 else if(layout == layout_tilebottom)
801 mwfact = fact_y;
802 else if(layout == layout_tiletop)
803 mwfact = 1. - fact_y;
805 /* keep mwfact within reasonable bounds */
806 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
808 /* refresh layout */
809 if(fabs(tag->mwfact - mwfact) >= 0.01)
811 tag->mwfact = mwfact;
812 globalconf.screens[tag->screen].need_arrange = true;
813 layout_refresh();
814 wibox_refresh();
815 xcb_flush(globalconf.connection);
819 /* relase pointer */
820 mouse_ungrab_pointer();
823 /** Resize the master client in mangifier layout
824 * \param c The client to resize.
825 * \param infobox Enable or disable the infobox.
827 static void
828 mouse_client_resize_magnified(client_t *c, bool infobox)
830 /* screen area modulo wibox */
831 area_t area;
832 /* center of area */
833 int center_x, center_y;
834 /* max. distance from the center */
835 double maxdist;
836 /* mouse position */
837 int mouse_x = 0, mouse_y = 0;
838 /* cursor while grabbing */
839 size_t cursor = CurResize;
840 corner_t corner = AutoCorner;
841 /* current tag */
842 tag_t *tag;
843 /* the infobox */
844 simple_window_t sw;
845 xcb_window_t root;
847 tag = tags_get_current(c->screen)[0];
849 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
851 area = screen_area_get(tag->screen,
852 &globalconf.screens[tag->screen].wiboxes,
853 &globalconf.screens[tag->screen].padding,
854 true);
856 center_x = area.x + (round(area.width / 2.));
857 center_y = area.y + (round(area.height / 2.));
859 maxdist = round(sqrt((area.width*area.width) + (area.height*area.height)) / 2.);
861 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
863 if(!mouse_query_pointer(root, &mouse_x, &mouse_y, NULL))
864 return;
866 /* select corner */
867 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
869 /* select cursor */
870 switch(corner)
872 default:
873 cursor = CurTopLeft;
874 break;
875 case TopRightCorner:
876 cursor = CurTopRight;
877 break;
878 case BottomLeftCorner:
879 cursor = CurBotLeft;
880 break;
881 case BottomRightCorner:
882 cursor = CurBotRight;
883 break;
886 /* grab pointer */
887 if(!mouse_grab_pointer(root, cursor))
888 return;
890 /* move pointer to corner */
891 mouse_warp_pointer(root, mouse_x, mouse_y);
893 /* create the infobox */
894 if(infobox)
895 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
897 /* for each motion event */
898 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
900 /* \todo keep pointer on screen diagonals */
901 double mwfact, dist, dx, dy;
903 /* calc distance from the center */
904 dx = center_x - mouse_x;
905 dy = center_y - mouse_y;
906 dist = sqrt((dx * dx) + (dy * dy));
908 /* new master/rest ratio */
909 mwfact = (dist * dist) / (maxdist * maxdist);
911 /* keep mwfact within reasonable bounds */
912 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
914 /* refresh the layout */
915 if(fabs(tag->mwfact - mwfact) >= 0.01)
917 tag->mwfact = mwfact;
918 globalconf.screens[tag->screen].need_arrange = true;
919 layout_refresh();
920 wibox_refresh();
921 xcb_flush(globalconf.connection);
924 /* draw the infobox */
925 if(infobox)
926 mouse_infobox_draw(&sw, c->geometry, c->border);
929 /* ungrab pointer */
930 mouse_ungrab_pointer();
932 /* free the infobox */
933 if(infobox)
934 simplewindow_wipe(&sw);
937 /** Resize a client with the mouse.
938 * \param c The client to resize.
939 * \param corner The corner to use.
940 * \param infobox Enable or disable the info box.
942 static void
943 mouse_client_resize(client_t *c, corner_t corner, bool infobox)
945 int n, screen;
946 tag_t **curtags;
947 layout_t *layout;
948 xcb_screen_t *s;
950 if(c->isfullscreen
951 || c->type == WINDOW_TYPE_DESKTOP
952 || c->type == WINDOW_TYPE_SPLASH
953 || c->type == WINDOW_TYPE_DOCK)
954 return;
956 curtags = tags_get_current(c->screen);
957 layout = curtags[0]->layout;
958 s = xutil_screen_get(globalconf.connection, c->phys_screen);
960 /* only handle floating, tiled and magnifier layouts */
961 if(layout == layout_floating || client_isfloating(c))
962 mouse_client_resize_floating(c, corner, infobox);
963 else if(layout == layout_tile || layout == layout_tileleft
964 || layout == layout_tilebottom || layout == layout_tiletop)
966 screen = c->screen;
967 for(n = 0, c = globalconf.clients; c; c = c->next)
968 if(IS_TILED(c, screen))
969 n++;
971 /* only masters on this screen? */
972 if(n <= curtags[0]->nmaster)
973 goto bailout;
975 /* no tiled clients on this screen? */
976 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
977 if(!c)
978 goto bailout;
980 mouse_client_resize_tiled(c);
982 else if(layout == layout_magnifier)
983 mouse_client_resize_magnified(c, infobox);
985 bailout:
986 p_delete(&curtags);
989 /** Resize a client with mouse.
990 * \param L The Lua VM state.
992 * \luastack
993 * \lvalue A client.
994 * \lparam An optional table with keys: `corner', such as bottomleft,
995 * topright, etc, to specify which corner to grab (default to auto) and
996 * `infobox' to enable or disable the coordinates and dimensions box (default to
997 * enabled).
1000 luaA_client_mouse_resize(lua_State *L)
1002 client_t **c = luaA_checkudata(L, 1, "client");
1003 corner_t corner = AutoCorner;
1004 bool infobox = true;
1005 size_t len;
1006 const char *buf;
1008 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1010 luaA_checktable(L, 2);
1011 buf = luaA_getopt_lstring(L, 2, "corner", "auto", &len);
1012 corner = a_strtocorner(buf, len);
1013 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1016 mouse_client_resize(*c, corner, infobox);
1018 return 0;
1021 /** Move a client with mouse.
1022 * \param L The Lua VM state.
1024 * \luastack
1025 * \lvalue A client.
1026 * \lparam An optional table with keys: `snap' for pixel to snap (default to 8), and
1027 * `infobox' to enable or disable the coordinates and dimensions box (default to
1028 * enabled).
1031 luaA_client_mouse_move(lua_State *L)
1033 client_t **c = luaA_checkudata(L, 1, "client");
1034 int snap = 8;
1035 bool infobox = true;
1037 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1039 luaA_checktable(L, 2);
1040 snap = luaA_getopt_number(L, 2, "snap", 8);
1041 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1044 mouse_client_move(*c, snap, infobox);
1046 return 0;
1049 /** Create a new mouse button bindings.
1050 * \param L The Lua VM state.
1051 * \return The number of elements pushed on stack.
1052 * \luastack
1053 * \lparam A table with modifiers keys, or a button to clone.
1054 * \lparam A mouse button number.
1055 * \lparam A function to execute on click events.
1056 * \lparam A function to execute on release events.
1057 * \lreturn A mouse button binding.
1059 static int
1060 luaA_button_new(lua_State *L)
1062 int i, len;
1063 button_t *button, **orig;
1065 if((orig = luaA_toudata(L, 2, "button")))
1067 button_t *copy = p_new(button_t, 1);
1068 copy->mod = (*orig)->mod;
1069 copy->button = (*orig)->button;
1070 if((*orig)->press != LUA_REFNIL)
1072 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
1073 luaA_registerfct(L, -1, &copy->press);
1075 else
1076 copy->press = LUA_REFNIL;
1077 if((*orig)->release != LUA_REFNIL)
1079 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
1080 luaA_registerfct(L, -1, &copy->release);
1082 else
1083 copy->release = LUA_REFNIL;
1084 return luaA_button_userdata_new(L, copy);
1087 luaA_checktable(L, 2);
1088 /* arg 3 is mouse button */
1089 i = luaL_checknumber(L, 3);
1090 /* arg 4 and 5 are callback functions */
1091 if(!lua_isnil(L, 4))
1092 luaA_checkfunction(L, 4);
1093 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1094 luaA_checkfunction(L, 5);
1096 button = p_new(button_t, 1);
1097 button->button = xutil_button_fromint(i);
1099 if(lua_isnil(L, 4))
1100 button->press = LUA_REFNIL;
1101 else
1102 luaA_registerfct(L, 4, &button->press);
1104 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1105 luaA_registerfct(L, 5, &button->release);
1106 else
1107 button->release = LUA_REFNIL;
1109 len = lua_objlen(L, 2);
1110 for(i = 1; i <= len; i++)
1112 size_t blen;
1113 const char *buf;
1114 lua_rawgeti(L, 2, i);
1115 buf = luaL_checklstring(L, -1, &blen);
1116 button->mod |= xutil_key_mask_fromstr(buf, blen);
1119 return luaA_button_userdata_new(L, button);
1122 /** Set a button array with a Lua table.
1123 * \param L The Lua VM state.
1124 * \param idx The index of the Lua table.
1125 * \param buttons The array button to fill.
1127 void
1128 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
1130 button_t **b;
1132 luaA_checktable(L, idx);
1133 button_array_wipe(buttons);
1134 button_array_init(buttons);
1135 lua_pushnil(L);
1136 while(lua_next(L, idx))
1138 b = luaA_checkudata(L, -1, "button");
1139 button_array_append(buttons, *b);
1140 button_ref(b);
1141 lua_pop(L, 1);
1145 /** Push an array of button as an Lua table onto the stack.
1146 * \param L The Lua VM state.
1147 * \param buttons The button array to push.
1148 * \return The number of elements pushed on stack.
1151 luaA_button_array_get(lua_State *L, button_array_t *buttons)
1153 luaA_otable_new(L);
1154 for(int i = 0; i < buttons->len; i++)
1156 luaA_button_userdata_new(L, buttons->tab[i]);
1157 lua_rawseti(L, -2, i + 1);
1159 return 1;
1162 /** Button object.
1163 * \param L The Lua VM state.
1164 * \return The number of elements pushed on stack.
1165 * \luastack
1166 * \lfield press The function called when button press event is received.
1167 * \lfield release The function called when button release event is received.
1169 static int
1170 luaA_button_index(lua_State *L)
1172 if(luaA_usemetatable(L, 1, 2))
1173 return 1;
1175 size_t len;
1176 button_t **button = luaA_checkudata(L, 1, "button");
1177 const char *attr = luaL_checklstring(L, 2, &len);
1179 switch(a_tokenize(attr, len))
1181 case A_TK_PRESS:
1182 if((*button)->press != LUA_REFNIL)
1183 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
1184 else
1185 lua_pushnil(L);
1186 break;
1187 case A_TK_RELEASE:
1188 if((*button)->release != LUA_REFNIL)
1189 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
1190 else
1191 lua_pushnil(L);
1192 break;
1193 case A_TK_BUTTON:
1194 /* works fine, but not *really* neat */
1195 lua_pushnumber(L, (*button)->button);
1196 break;
1197 default:
1198 break;
1201 return 1;
1204 /** Button object.
1205 * \param L The Lua VM state.
1206 * \return The number of elements pushed on stack.
1207 * \luastack
1209 static int
1210 luaA_button_newindex(lua_State *L)
1212 if(luaA_usemetatable(L, 1, 2))
1213 return 1;
1215 size_t len;
1216 button_t **button = luaA_checkudata(L, 1, "button");
1217 const char *attr = luaL_checklstring(L, 2, &len);
1219 switch(a_tokenize(attr, len))
1221 case A_TK_PRESS:
1222 luaA_registerfct(L, 3, &(*button)->press);
1223 break;
1224 case A_TK_RELEASE:
1225 luaA_registerfct(L, 3, &(*button)->release);
1226 break;
1227 case A_TK_BUTTON:
1228 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
1229 break;
1230 default:
1231 break;
1234 return 0;
1237 const struct luaL_reg awesome_button_methods[] =
1239 { "__call", luaA_button_new },
1240 { NULL, NULL }
1242 const struct luaL_reg awesome_button_meta[] =
1244 { "__index", luaA_button_index },
1245 { "__newindex", luaA_button_newindex },
1246 { "__gc", luaA_button_gc },
1247 { "__eq", luaA_button_eq },
1248 { "__tostring", luaA_button_tostring },
1249 { NULL, NULL }
1252 /** Mouse library.
1253 * \param L The Lua VM state.
1254 * \return The number of elements pushed on stack.
1255 * \luastack
1256 * \lfield coords Mouse coordinates.
1257 * \lfield screen Mouse screen number.
1259 static int
1260 luaA_mouse_index(lua_State *L)
1262 size_t len;
1263 const char *attr = luaL_checklstring(L, 2, &len);
1264 int mouse_x, mouse_y, i;
1265 int screen;
1267 switch(a_tokenize(attr, len))
1269 case A_TK_SCREEN:
1270 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL))
1271 return 0;
1273 i = screen_getbycoord(screen, mouse_x, mouse_y);
1275 lua_pushnumber(L, i + 1);
1276 break;
1277 default:
1278 return 0;
1281 return 1;
1284 /** Newindex for mouse.
1285 * \param L The Lua VM state.
1286 * \return The number of elements pushed on stack.
1288 static int
1289 luaA_mouse_newindex(lua_State *L)
1291 size_t len;
1292 const char *attr = luaL_checklstring(L, 2, &len);
1293 int x, y = 0;
1294 xcb_window_t root;
1295 int screen, phys_screen;
1297 switch(a_tokenize(attr, len))
1299 case A_TK_SCREEN:
1300 screen = luaL_checknumber(L, 3) - 1;
1301 luaA_checkscreen(screen);
1303 /* we need the physical one to get the root window */
1304 phys_screen = screen_virttophys(screen);
1305 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
1307 x = globalconf.screens[screen].geometry.x;
1308 y = globalconf.screens[screen].geometry.y;
1310 mouse_warp_pointer(root, x, y);
1311 break;
1312 default:
1313 return 0;
1316 return 0;
1319 /** Get or set the mouse coords.
1320 * \param L The Lua VM state.
1321 * \return The number of elements pushed on stack.
1322 * \luastack
1323 * \lparam None or a table with x and y keys as mouse coordinates.
1324 * \lreturn A table with mouse coordinates.
1326 static int
1327 luaA_mouse_coords(lua_State *L)
1329 uint16_t mask, maski;
1330 int screen, x, y, mouse_x, mouse_y, i = 1;
1332 if(lua_gettop(L) == 1)
1334 xcb_window_t root;
1336 luaA_checktable(L, 1);
1338 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1339 return 0;
1341 x = luaA_getopt_number(L, 1, "x", mouse_x);
1342 y = luaA_getopt_number(L, 1, "y", mouse_y);
1344 root = xutil_screen_get(globalconf.connection, screen)->root;
1345 mouse_warp_pointer(root, x, y);
1346 lua_pop(L, 1);
1349 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1350 return 0;
1352 lua_newtable(L);
1353 lua_pushnumber(L, mouse_x);
1354 lua_setfield(L, -2, "x");
1355 lua_pushnumber(L, mouse_y);
1356 lua_setfield(L, -2, "y");
1357 lua_newtable(L);
1358 for(maski = XCB_BUTTON_MASK_1; i <= XCB_BUTTON_MASK_5; maski <<= 1, i++)
1359 if(mask & maski)
1361 lua_pushboolean(L, true);
1362 lua_rawseti(L, -2, i);
1364 lua_setfield(L, -2, "buttons");
1366 return 1;
1369 const struct luaL_reg awesome_mouse_methods[] =
1371 { "__index", luaA_mouse_index },
1372 { "__newindex", luaA_mouse_newindex },
1373 { "coords", luaA_mouse_coords },
1374 { NULL, NULL }
1376 const struct luaL_reg awesome_mouse_meta[] =
1378 { NULL, NULL }
1381 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80