naughty: icon_size added to config and notify()
[awesome.git] / mouse.c
blob5ccc5fe763832d53b0099008ee4337c4e5e5217a
1 /*
2 * mouse.c - mouse managing
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <math.h>
24 #include "common/tokenize.h"
25 #include "screen.h"
26 #include "tag.h"
27 #include "titlebar.h"
28 #include "layouts/floating.h"
29 #include "layouts/tile.h"
30 #include "layouts/magnifier.h"
32 #define MOUSEMASK (XCB_EVENT_MASK_BUTTON_PRESS \
33 | XCB_EVENT_MASK_BUTTON_RELEASE \
34 | XCB_EVENT_MASK_POINTER_MOTION)
36 extern awesome_t globalconf;
38 DO_LUA_NEW(extern, button_t, button, "button", button_ref)
39 DO_LUA_GC(button_t, button, "button", button_unref)
40 DO_LUA_EQ(button_t, button, "button")
42 /** Define corners. */
43 typedef enum
45 AutoCorner,
46 TopRightCorner,
47 TopLeftCorner,
48 BottomLeftCorner,
49 BottomRightCorner
50 } corner_t;
52 /** Convert a corner name into a corner type.
53 * \param str A string.
54 * \param len String length.
55 * \return A corner type.
57 static corner_t
58 a_strtocorner(const char *str, size_t len)
60 switch (a_tokenize(str, len))
62 case A_TK_BOTTOMRIGHT: return BottomRightCorner;
63 case A_TK_BOTTOMLEFT: return BottomLeftCorner;
64 case A_TK_TOPLEFT: return TopLeftCorner;
65 case A_TK_TOPRIGHT: return TopRightCorner;
66 default: return AutoCorner;
70 /** Delete a button.
71 * \param button The button to destroy.
73 void
74 button_delete(button_t **button)
76 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->press);
77 luaL_unref(globalconf.L, LUA_REGISTRYINDEX, (*button)->release);
78 p_delete(button);
81 /** Snap an area to the outside of an area.
82 * \param geometry geometry of the area to snap
83 * \param snap_geometry geometry of snapping area
84 * \param snap snap trigger in pixel
85 * \return snapped geometry
87 static area_t
88 mouse_snapclienttogeometry_outside(area_t geometry, area_t snap_geometry, int snap)
90 if(geometry.x < snap + snap_geometry.x + snap_geometry.width
91 && geometry.x > snap_geometry.x + snap_geometry.width)
92 geometry.x = snap_geometry.x + snap_geometry.width;
93 else if(geometry.x + geometry.width < snap_geometry.x
94 && geometry.x + geometry.width > snap_geometry.x - snap)
95 geometry.x = snap_geometry.x - geometry.width;
97 if(geometry.y < snap + snap_geometry.y + snap_geometry.height
98 && geometry.y > snap_geometry.y + snap_geometry.height)
99 geometry.y = snap_geometry.y + snap_geometry.height;
100 else if(geometry.y + geometry.height < snap_geometry.y
101 && geometry.y + geometry.height > snap_geometry.y - snap)
102 geometry.y = snap_geometry.y - geometry.height;
104 return geometry;
107 /** Snap an area to the inside of an area.
108 * \param geometry geometry of the area to snap
109 * \param snap_geometry geometry of snapping area
110 * \param snap snap trigger in pixel
111 * \return snapped geometry
113 static area_t
114 mouse_snapclienttogeometry_inside(area_t geometry, area_t snap_geometry, int snap)
116 if(abs(geometry.x) < snap + snap_geometry.x && geometry.x > snap_geometry.x)
117 geometry.x = snap_geometry.x;
118 else if(abs((snap_geometry.x + snap_geometry.width) - (geometry.x + geometry.width))
119 < snap)
120 geometry.x = snap_geometry.x + snap_geometry.width - geometry.width;
121 if(abs(geometry.y) < snap + snap_geometry.y && geometry.y > snap_geometry.y)
122 geometry.y = snap_geometry.y;
123 else if(abs((snap_geometry.y + snap_geometry.height) - (geometry.y + geometry.height))
124 < snap)
125 geometry.y = snap_geometry.y + snap_geometry.height - geometry.height;
127 return geometry;
130 /** Snap a client with a future geometry to the screen and other clients.
131 * \param c The client.
132 * \param geometry Geometry the client will get.
133 * \param snap The maximum distance in pixels to trigger a "snap".
134 * \return Geometry to set to the client.
136 static area_t
137 mouse_snapclient(client_t *c, area_t geometry, int snap)
139 client_t *snapper;
140 area_t snapper_geometry;
141 area_t screen_geometry =
142 screen_area_get(c->screen,
143 &globalconf.screens[c->screen].wiboxes,
144 &globalconf.screens[c->screen].padding,
145 false);
147 area_t screen_geometry_barless =
148 screen_area_get(c->screen,
149 NULL,
150 &globalconf.screens[c->screen].padding,
151 false);
153 geometry = titlebar_geometry_add(c->titlebar, c->border, geometry);
155 geometry =
156 mouse_snapclienttogeometry_inside(geometry, screen_geometry, snap);
158 geometry =
159 mouse_snapclienttogeometry_inside(geometry, screen_geometry_barless, snap);
161 for(snapper = globalconf.clients; snapper; snapper = snapper->next)
162 if(snapper != c && client_isvisible(snapper, c->screen))
164 snapper_geometry = snapper->geometry;
165 snapper_geometry = titlebar_geometry_add(snapper->titlebar, snapper->border, snapper_geometry);
166 geometry =
167 mouse_snapclienttogeometry_outside(geometry,
168 snapper_geometry,
169 snap);
172 return titlebar_geometry_remove(c->titlebar, c->border, geometry);
175 /** Set coordinates to a corner of an area.
177 * \param a The area.
178 * \param[in,out] x The x coordinate.
179 * \param[in,out] y The y coordinate.
180 * \param corner The corner to snap to.
181 * \return The corner the coordinates have been set to. If corner != AutoCorner
182 * this is always equal to \em corner.
184 * \todo rename/move this is still awkward and might be useful somewhere else.
186 static corner_t
187 mouse_snap_to_corner(area_t a, int *x, int *y, corner_t corner)
189 int top, bottom, left, right;
191 top = AREA_TOP(a);
192 bottom = AREA_BOTTOM(a);
193 left = AREA_LEFT(a);
194 right = AREA_RIGHT(a);
196 /* figure out the nearser corner */
197 if(corner == AutoCorner)
199 if(abs(top - *y) < abs(bottom - *y))
201 if(abs(left - *x) < abs(right - *x))
202 corner = TopLeftCorner;
203 else
204 corner = TopRightCorner;
206 else
208 if(abs(left - *x) < abs(right - *x))
209 corner = BottomLeftCorner;
210 else
211 corner = BottomRightCorner;
215 switch(corner)
217 case TopRightCorner:
218 *x = right;
219 *y = top;
220 break;
222 case TopLeftCorner:
223 *x = left;
224 *y = top;
225 break;
227 case BottomLeftCorner:
228 *x = left;
229 *y = bottom;
230 break;
232 case BottomRightCorner:
233 *x = right;
234 *y = bottom;
235 break;
237 default:
238 break;
241 return corner;
244 /** Redraw the infobox.
245 * \param sw The simple window.
246 * \param geometry The geometry to use for the box.
247 * \param border The client border size.
249 static void
250 mouse_infobox_draw(simple_window_t *sw, area_t geometry, int border)
252 area_t draw_geometry = { 0, 0, sw->ctx.width, sw->ctx.height };
253 char size[64];
254 size_t len;
255 xcolor_t color_without_alpha = globalconf.colors.bg;
256 color_without_alpha.alpha = 0xffff;
258 len = snprintf(size, sizeof(size), "<text align=\"center\"/>%dx%d+%d+%d",
259 geometry.width, geometry.height, geometry.x, geometry.y);
260 draw_rectangle(&sw->ctx, draw_geometry, 1.0, true, &color_without_alpha);
261 draw_text(&sw->ctx, globalconf.font, draw_geometry, size, len, NULL);
262 simplewindow_move(sw,
263 geometry.x + ((2 * border + geometry.width) - sw->geometry.width) / 2,
264 geometry.y + ((2 * border + geometry.height) - sw->geometry.height) / 2);
265 simplewindow_refresh_pixmap(sw);
266 xcb_flush(globalconf.connection);
269 #define MOUSE_INFOBOX_STRING_DEFAULT "0000x0000+0000+0000"
271 /** Initialize the infobox window.
272 * \param sw The simple window to init.
273 * \param phys_screen Physical screen number.
274 * \param border Border size of the client.
275 * \param geometry Client geometry.
276 * \return The simple window.
278 static void
279 mouse_infobox_new(simple_window_t *sw, int phys_screen, int border, area_t geometry)
281 area_t geom;
282 draw_parser_data_t pdata;
284 draw_parser_data_init(&pdata);
286 geom = draw_text_extents(globalconf.default_screen,
287 globalconf.font,
288 MOUSE_INFOBOX_STRING_DEFAULT,
289 sizeof(MOUSE_INFOBOX_STRING_DEFAULT)-1,
290 &pdata);
291 geom.x = geometry.x + ((2 * border + geometry.width) - geom.width) / 2;
292 geom.y = geometry.y + ((2 * border + geometry.height) - geom.height) / 2;
294 p_clear(sw, 1);
295 simplewindow_init(sw, phys_screen, geom, 0, East,
296 &globalconf.colors.fg, &globalconf.colors.bg);
298 xcb_map_window(globalconf.connection, sw->window);
299 mouse_infobox_draw(sw, geometry, border);
301 draw_parser_data_wipe(&pdata);
304 /** Get the pointer position.
305 * \param window The window to get position on.
306 * \param x will be set to the Pointer-x-coordinate relative to window
307 * \param y will be set to the Pointer-y-coordinate relative to window
308 * \param mask will be set to the current buttons state
309 * \return true on success, false if an error occured
311 static bool
312 mouse_query_pointer(xcb_window_t window, int *x, int *y, uint16_t *mask)
314 xcb_query_pointer_cookie_t query_ptr_c;
315 xcb_query_pointer_reply_t *query_ptr_r;
317 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
318 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
320 if(!query_ptr_r || !query_ptr_r->same_screen)
321 return false;
323 *x = query_ptr_r->win_x;
324 *y = query_ptr_r->win_y;
325 if (mask)
326 *mask = query_ptr_r->mask;
328 p_delete(&query_ptr_r);
330 return true;
333 /** Get the pointer position on the screen.
334 * \param screen This will be set to the screen number the mouse is on.
335 * \param x This will be set to the Pointer-x-coordinate relative to window.
336 * \param y This will be set to the Pointer-y-coordinate relative to window.
337 * \param mask This will be set to the current buttons state.
338 * \return True on success, false if an error occured.
340 static bool
341 mouse_query_pointer_root(int *s, int *x, int *y, uint16_t *mask)
343 for(int screen = 0;
344 screen < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
345 screen++)
347 xcb_window_t root = xutil_screen_get(globalconf.connection, screen)->root;
349 if(mouse_query_pointer(root, x, y, mask))
351 *s = screen;
352 return true;
355 return false;
358 /** Grab the Pointer.
359 * \param window The window grabbed.
360 * \param cursor The cursor to display (see struct.h CurNormal, CurResize, etc).
361 * \return True on success, false if an error occured.
363 static bool
364 mouse_grab_pointer(xcb_window_t window, size_t cursor)
366 xcb_grab_pointer_cookie_t grab_ptr_c;
367 xcb_grab_pointer_reply_t *grab_ptr_r;
369 if(cursor >= CurLast)
370 cursor = CurNormal;
372 grab_ptr_c = xcb_grab_pointer_unchecked(globalconf.connection, false, window,
373 MOUSEMASK, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
374 window, globalconf.cursor[cursor], XCB_CURRENT_TIME);
375 grab_ptr_r = xcb_grab_pointer_reply(globalconf.connection, grab_ptr_c, NULL);
377 if(!grab_ptr_r)
378 return false;
380 p_delete(&grab_ptr_r);
382 return true;
385 /** Ungrab the Pointer
387 static inline void
388 mouse_ungrab_pointer(void)
390 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
393 /** Set the pointer position.
394 * \param window The destination window.
395 * \param x X-coordinate inside window.
396 * \param y Y-coordinate inside window.
398 static inline void
399 mouse_warp_pointer(xcb_window_t window, int x, int y)
401 xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
402 0, 0, 0, 0, x, y );
405 /** Utility function to help with mouse-dragging
407 * \param x set to x-coordinate of the last event on return
408 * \param y set to y-coordinate of the last event on return
409 * \return true if an motion event was received
410 * false if an button release event was received
412 static bool
413 mouse_track_mouse_drag(int *x, int *y)
415 xcb_generic_event_t *ev;
416 xcb_motion_notify_event_t *ev_motion;
417 xcb_button_release_event_t *ev_button;
419 while(true)
420 while((ev = xcb_wait_for_event(globalconf.connection)))
421 switch((ev->response_type & 0x7F))
424 case XCB_MOTION_NOTIFY:
425 ev_motion = (xcb_motion_notify_event_t*) ev;
426 *x = ev_motion->event_x;
427 *y = ev_motion->event_y;
428 p_delete(&ev);
429 return true;
431 case XCB_BUTTON_RELEASE:
432 ev_button = (xcb_button_release_event_t*) ev;
433 *x = ev_button->event_x;
434 *y = ev_button->event_y;
435 p_delete(&ev);
436 return false;
438 default:
439 xcb_event_handle(&globalconf.evenths, ev);
440 p_delete(&ev);
441 break;
445 /** Get the client that contains the pointer.
447 * \return The client that contains the pointer or NULL.
449 static client_t *
450 mouse_get_client_under_pointer(int phys_screen)
452 xcb_window_t root;
453 xcb_query_pointer_cookie_t query_ptr_c;
454 xcb_query_pointer_reply_t *query_ptr_r;
455 client_t *c = NULL;
457 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
459 query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, root);
460 query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
462 if(query_ptr_r)
464 c = client_getbywin(query_ptr_r->child);
465 p_delete(&query_ptr_r);
468 return c;
471 /** Move the focused window with the mouse.
472 * \param c The client.
473 * \param snap The maximum distance in pixels to trigger a "snap".
474 * \param infobox Enable or disable the infobox.
476 static void
477 mouse_client_move(client_t *c, int snap, bool infobox)
479 /* current mouse postion */
480 int mouse_x, mouse_y;
481 /* last mouse position */
482 int last_x = 0, last_y = 0;
483 /* current layout */
484 layout_t *layout;
485 /* the infobox */
486 simple_window_t sw;
487 /* the root window */
488 xcb_window_t root;
490 layout = layout_get_current(c->screen);
491 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
493 /* get current pointer position */
494 mouse_query_pointer(root, &last_x, &last_y, NULL);
496 /* grab pointer */
497 if(c->isfullscreen
498 || c->type == WINDOW_TYPE_DESKTOP
499 || c->type == WINDOW_TYPE_SPLASH
500 || c->type == WINDOW_TYPE_DOCK
501 || !mouse_grab_pointer(root, CurMove))
502 return;
504 if(infobox && (client_isfloating(c) || layout == layout_floating))
505 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
506 else
507 infobox = false;
509 /* for each motion event */
510 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
511 if(client_isfloating(c) || layout == layout_floating)
513 area_t geometry;
515 /* calc new geometry */
516 geometry = c->geometry;
517 geometry.x += (mouse_x - last_x);
518 geometry.y += (mouse_y - last_y);
520 /* snap and move */
521 geometry = mouse_snapclient(c, geometry, snap);
522 c->ismoving = true;
523 client_resize(c, geometry, false);
524 xcb_flush(globalconf.connection);
525 c->ismoving = false;
527 /* draw the infobox */
528 if(infobox)
529 mouse_infobox_draw(&sw, c->geometry, c->border);
531 /* refresh live */
532 wibox_refresh();
533 xcb_flush(globalconf.connection);
535 /* keep track */
536 last_x = mouse_x;
537 last_y = mouse_y;
539 else
541 int newscreen;
542 client_t *target;
544 /* client moved to another screen? */
545 newscreen = screen_getbycoord(c->screen, mouse_x, mouse_y);
546 if(newscreen != c->screen)
548 screen_client_moveto(c, newscreen, true, true);
549 globalconf.screens[c->screen].need_arrange = true;
550 globalconf.screens[newscreen].need_arrange = true;
551 layout_refresh();
552 wibox_refresh();
553 xcb_flush(globalconf.connection);
556 /* find client to swap with */
557 target = mouse_get_client_under_pointer(c->phys_screen);
559 /* swap position */
560 if(target && target != c && !target->isfloating)
562 client_list_swap(&globalconf.clients, c, target);
563 globalconf.screens[c->screen].need_arrange = true;
564 luaA_dofunction(globalconf.L, globalconf.hooks.clients, 0, 0);
565 layout_refresh();
566 wibox_refresh();
567 xcb_flush(globalconf.connection);
571 /* ungrab pointer */
572 xcb_ungrab_pointer(globalconf.connection, XCB_CURRENT_TIME);
574 /* free the infobox */
575 if(infobox)
576 simplewindow_wipe(&sw);
580 /** Resize a floating client with the mouse.
581 * \param c The client to resize.
582 * \param corner The corner to resize with.
583 * \param infobox Enable or disable the infobox.
585 static void
586 mouse_client_resize_floating(client_t *c, corner_t corner, bool infobox)
588 xcb_screen_t *screen;
589 /* one corner of the client has a fixed position */
590 int fixed_x, fixed_y;
591 /* the other is moved with the mouse */
592 int mouse_x = 0, mouse_y = 0;
593 /* the infobox */
594 simple_window_t sw;
595 size_t cursor = CurResize;
596 int top, bottom, left, right;
598 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
600 /* get current mouse position */
601 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
603 top = c->geometry.y;
604 bottom = top + c->geometry.height;
605 left = c->geometry.x;
606 right = left + c->geometry.width;
608 /* figure out which corner to move */
609 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
611 /* the opposite corner is fixed */
612 fixed_x = (mouse_x == left) ? right : left;
613 fixed_y = (mouse_y == top) ? bottom : top;
615 /* select cursor */
616 switch(corner)
618 default:
619 cursor = CurTopLeft;
620 break;
621 case TopRightCorner:
622 cursor = CurTopRight;
623 break;
624 case BottomLeftCorner:
625 cursor = CurBotLeft;
626 break;
627 case BottomRightCorner:
628 cursor = CurBotRight;
629 break;
632 /* grab the pointer */
633 if(!mouse_grab_pointer(screen->root, cursor))
634 return;
636 /* set pointer to the moveable corner */
637 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
639 /* create the infobox */
640 if(infobox)
641 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
643 /* for each motion event */
644 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
646 /* new client geometry */
647 area_t geo = { .x = MIN(fixed_x, mouse_x),
648 .y = MIN(fixed_y, mouse_y),
649 .width = (MAX(fixed_x, mouse_x) - MIN(fixed_x, mouse_x)),
650 .height = (MAX(fixed_y, mouse_y) - MIN(fixed_y, mouse_y)) };
651 /* new moveable corner */
652 corner_t new_corner;
654 if(mouse_x == AREA_LEFT(geo))
655 new_corner = (mouse_y == AREA_TOP(geo)) ? TopLeftCorner : BottomLeftCorner;
656 else
657 new_corner = (mouse_y == AREA_TOP(geo)) ? TopRightCorner : BottomRightCorner;
659 /* update cursor */
660 if(corner != new_corner)
662 corner = new_corner;
664 switch(corner)
666 default: cursor = CurTopLeft; break;
667 case TopRightCorner: cursor = CurTopRight; break;
668 case BottomLeftCorner: cursor = CurBotLeft; break;
669 case BottomRightCorner: cursor = CurBotRight; break;
672 xcb_change_active_pointer_grab(globalconf.connection, globalconf.cursor[cursor],
673 XCB_CURRENT_TIME, MOUSEMASK);
676 if(c->hassizehints && c->honorsizehints)
678 int dx, dy;
680 /* apply size hints */
681 geo = client_geometry_hints(c, geo);
683 /* get the nonmoveable corner back onto fixed_x,fixed_y */
684 switch(corner)
686 default /* TopLeftCorner */:
687 dx = fixed_x - AREA_RIGHT(geo);
688 dy = fixed_y - AREA_BOTTOM(geo);
689 break;
690 case TopRightCorner:
691 dx = fixed_x - AREA_LEFT(geo);
692 dy = fixed_y - AREA_BOTTOM(geo);
693 break;
694 case BottomRightCorner:
695 dx = fixed_x - AREA_LEFT(geo);
696 dy = fixed_y - AREA_TOP(geo);
697 break;
698 case BottomLeftCorner:
699 dx = fixed_x - AREA_RIGHT(geo);
700 dy = fixed_y - AREA_TOP(geo);
701 break;
704 geo.x += dx;
705 geo.y += dy;
708 /* resize the client */
709 client_resize(c, geo, false);
711 /* refresh live */
712 wibox_refresh();
713 xcb_flush(globalconf.connection);
715 /* draw the infobox */
716 if(infobox)
717 mouse_infobox_draw(&sw, c->geometry, c->border);
720 /* relase pointer */
721 mouse_ungrab_pointer();
723 /* free the infobox */
724 if(infobox)
725 simplewindow_wipe(&sw);
728 /** Resize the master column/row of a tiled layout
729 * \param c A client on the tag/layout to resize.
731 static void
732 mouse_client_resize_tiled(client_t *c)
734 xcb_screen_t *screen;
735 /* screen area modulo wibox */
736 area_t area;
737 /* current tag */
738 tag_t *tag;
739 /* current layout */
740 layout_t *layout;
742 int mouse_x = 0, mouse_y = 0;
743 size_t cursor = CurResize;
745 screen = xutil_screen_get(globalconf.connection, c->phys_screen);
746 tag = tags_get_current(c->screen)[0];
747 layout = tag->layout;
749 area = screen_area_get(tag->screen,
750 &globalconf.screens[tag->screen].wiboxes,
751 &globalconf.screens[tag->screen].padding,
752 true);
754 mouse_query_pointer(screen->root, &mouse_x, &mouse_y, NULL);
756 /* select initial pointer position */
757 if(layout == layout_tile)
759 mouse_x = area.x + area.width * tag->mwfact;
760 cursor = CurResizeH;
762 else if(layout == layout_tileleft)
764 mouse_x = area.x + area.width * (1. - tag->mwfact);
765 cursor = CurResizeH;
767 else if(layout == layout_tilebottom)
769 mouse_y = area.y + area.height * tag->mwfact;
770 cursor = CurResizeV;
772 else if(layout == layout_tiletop)
774 mouse_y = area.y + area.height * (1. - tag->mwfact);
775 cursor = CurResizeV;
777 else
778 return;
780 /* grab the pointer */
781 if(!mouse_grab_pointer(screen->root, cursor))
782 return;
784 /* set pointer to the moveable border */
785 mouse_warp_pointer(screen->root, mouse_x, mouse_y);
787 xcb_flush(globalconf.connection);
789 /* for each motion event */
790 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
792 double mwfact = 0, fact_x, fact_y;
794 /* calculate new master / rest ratio */
795 fact_x = (double) (mouse_x - area.x) / area.width;
796 fact_y = (double) (mouse_y - area.y) / area.height;
798 if(layout == layout_tile)
799 mwfact = fact_x;
800 else if(layout == layout_tileleft)
801 mwfact = 1. - fact_x;
802 else if(layout == layout_tilebottom)
803 mwfact = fact_y;
804 else if(layout == layout_tiletop)
805 mwfact = 1. - fact_y;
807 /* keep mwfact within reasonable bounds */
808 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
810 /* refresh layout */
811 if(fabs(tag->mwfact - mwfact) >= 0.01)
813 tag->mwfact = mwfact;
814 globalconf.screens[tag->screen].need_arrange = true;
815 layout_refresh();
816 wibox_refresh();
817 xcb_flush(globalconf.connection);
821 /* relase pointer */
822 mouse_ungrab_pointer();
825 /** Resize the master client in mangifier layout
826 * \param c The client to resize.
827 * \param infobox Enable or disable the infobox.
829 static void
830 mouse_client_resize_magnified(client_t *c, bool infobox)
832 /* screen area modulo wibox */
833 area_t area;
834 /* center of area */
835 int center_x, center_y;
836 /* max. distance from the center */
837 double maxdist;
838 /* mouse position */
839 int mouse_x = 0, mouse_y = 0;
840 /* cursor while grabbing */
841 size_t cursor = CurResize;
842 corner_t corner = AutoCorner;
843 /* current tag */
844 tag_t *tag;
845 /* the infobox */
846 simple_window_t sw;
847 xcb_window_t root;
849 tag = tags_get_current(c->screen)[0];
851 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
853 area = screen_area_get(tag->screen,
854 &globalconf.screens[tag->screen].wiboxes,
855 &globalconf.screens[tag->screen].padding,
856 true);
858 center_x = area.x + (round(area.width / 2.));
859 center_y = area.y + (round(area.height / 2.));
861 maxdist = round(sqrt((area.width*area.width) + (area.height*area.height)) / 2.);
863 root = xutil_screen_get(globalconf.connection, c->phys_screen)->root;
865 if(!mouse_query_pointer(root, &mouse_x, &mouse_y, NULL))
866 return;
868 /* select corner */
869 corner = mouse_snap_to_corner(c->geometry, &mouse_x, &mouse_y, corner);
871 /* select cursor */
872 switch(corner)
874 default:
875 cursor = CurTopLeft;
876 break;
877 case TopRightCorner:
878 cursor = CurTopRight;
879 break;
880 case BottomLeftCorner:
881 cursor = CurBotLeft;
882 break;
883 case BottomRightCorner:
884 cursor = CurBotRight;
885 break;
888 /* grab pointer */
889 if(!mouse_grab_pointer(root, cursor))
890 return;
892 /* move pointer to corner */
893 mouse_warp_pointer(root, mouse_x, mouse_y);
895 /* create the infobox */
896 if(infobox)
897 mouse_infobox_new(&sw, c->phys_screen, c->border, c->geometry);
899 /* for each motion event */
900 while(mouse_track_mouse_drag(&mouse_x, &mouse_y))
902 /* \todo keep pointer on screen diagonals */
903 double mwfact, dist, dx, dy;
905 /* calc distance from the center */
906 dx = center_x - mouse_x;
907 dy = center_y - mouse_y;
908 dist = sqrt((dx * dx) + (dy * dy));
910 /* new master/rest ratio */
911 mwfact = (dist * dist) / (maxdist * maxdist);
913 /* keep mwfact within reasonable bounds */
914 mwfact = MIN(MAX( 0.01, mwfact), 0.99);
916 /* refresh the layout */
917 if(fabs(tag->mwfact - mwfact) >= 0.01)
919 tag->mwfact = mwfact;
920 globalconf.screens[tag->screen].need_arrange = true;
921 layout_refresh();
922 wibox_refresh();
923 xcb_flush(globalconf.connection);
926 /* draw the infobox */
927 if(infobox)
928 mouse_infobox_draw(&sw, c->geometry, c->border);
931 /* ungrab pointer */
932 mouse_ungrab_pointer();
934 /* free the infobox */
935 if(infobox)
936 simplewindow_wipe(&sw);
939 /** Resize a client with the mouse.
940 * \param c The client to resize.
941 * \param corner The corner to use.
942 * \param infobox Enable or disable the info box.
944 static void
945 mouse_client_resize(client_t *c, corner_t corner, bool infobox)
947 int n, screen;
948 tag_t **curtags;
949 layout_t *layout;
950 xcb_screen_t *s;
952 if(c->isfullscreen
953 || c->type == WINDOW_TYPE_DESKTOP
954 || c->type == WINDOW_TYPE_SPLASH
955 || c->type == WINDOW_TYPE_DOCK)
956 return;
958 curtags = tags_get_current(c->screen);
959 layout = curtags[0]->layout;
960 s = xutil_screen_get(globalconf.connection, c->phys_screen);
962 /* only handle floating, tiled and magnifier layouts */
963 if(layout == layout_floating || client_isfloating(c))
964 mouse_client_resize_floating(c, corner, infobox);
965 else if(layout == layout_tile || layout == layout_tileleft
966 || layout == layout_tilebottom || layout == layout_tiletop)
968 screen = c->screen;
969 for(n = 0, c = globalconf.clients; c; c = c->next)
970 if(IS_TILED(c, screen))
971 n++;
973 /* only masters on this screen? */
974 if(n <= curtags[0]->nmaster)
975 goto bailout;
977 /* no tiled clients on this screen? */
978 for(c = globalconf.clients; c && !IS_TILED(c, screen); c = c->next);
979 if(!c)
980 goto bailout;
982 mouse_client_resize_tiled(c);
984 else if(layout == layout_magnifier)
985 mouse_client_resize_magnified(c, infobox);
987 bailout:
988 p_delete(&curtags);
991 /** Resize a client with mouse.
992 * \param L The Lua VM state.
994 * \luastack
995 * \lvalue A client.
996 * \lparam An optional table with keys: `corner', such as bottomleft,
997 * topright, etc, to specify which corner to grab (default to auto) and
998 * `infobox' to enable or disable the coordinates and dimensions box (default to
999 * enabled).
1002 luaA_client_mouse_resize(lua_State *L)
1004 client_t **c = luaA_checkudata(L, 1, "client");
1005 corner_t corner = AutoCorner;
1006 bool infobox = true;
1007 size_t len;
1008 const char *buf;
1010 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1012 luaA_checktable(L, 2);
1013 buf = luaA_getopt_lstring(L, 2, "corner", "auto", &len);
1014 corner = a_strtocorner(buf, len);
1015 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1018 mouse_client_resize(*c, corner, infobox);
1020 return 0;
1023 /** Move a client with mouse.
1024 * \param L The Lua VM state.
1026 * \luastack
1027 * \lvalue A client.
1028 * \lparam An optional table with keys: `snap' for pixel to snap (default to 8), and
1029 * `infobox' to enable or disable the coordinates and dimensions box (default to
1030 * enabled).
1033 luaA_client_mouse_move(lua_State *L)
1035 client_t **c = luaA_checkudata(L, 1, "client");
1036 int snap = 8;
1037 bool infobox = true;
1039 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
1041 luaA_checktable(L, 2);
1042 snap = luaA_getopt_number(L, 2, "snap", 8);
1043 infobox = luaA_getopt_boolean(L, 2, "infobox", true);
1046 mouse_client_move(*c, snap, infobox);
1048 return 0;
1051 /** Create a new mouse button bindings.
1052 * \param L The Lua VM state.
1053 * \return The number of elements pushed on stack.
1054 * \luastack
1055 * \lparam A table with modifiers keys, or a button to clone.
1056 * \lparam A mouse button number.
1057 * \lparam A function to execute on click events.
1058 * \lparam A function to execute on release events.
1059 * \lreturn A mouse button binding.
1061 static int
1062 luaA_button_new(lua_State *L)
1064 int i, len;
1065 button_t *button, **orig;
1067 if((orig = luaA_toudata(L, 2, "button")))
1069 button_t *copy = p_new(button_t, 1);
1070 copy->mod = (*orig)->mod;
1071 copy->button = (*orig)->button;
1072 if((*orig)->press != LUA_REFNIL)
1074 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->press);
1075 luaA_registerfct(L, -1, &copy->press);
1077 else
1078 copy->press = LUA_REFNIL;
1079 if((*orig)->release != LUA_REFNIL)
1081 lua_rawgeti(L, LUA_REGISTRYINDEX, (*orig)->release);
1082 luaA_registerfct(L, -1, &copy->release);
1084 else
1085 copy->release = LUA_REFNIL;
1086 return luaA_button_userdata_new(L, copy);
1089 luaA_checktable(L, 2);
1090 /* arg 3 is mouse button */
1091 i = luaL_checknumber(L, 3);
1092 /* arg 4 and 5 are callback functions */
1093 if(!lua_isnil(L, 4))
1094 luaA_checkfunction(L, 4);
1095 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1096 luaA_checkfunction(L, 5);
1098 button = p_new(button_t, 1);
1099 button->button = xutil_button_fromint(i);
1101 if(lua_isnil(L, 4))
1102 button->press = LUA_REFNIL;
1103 else
1104 luaA_registerfct(L, 4, &button->press);
1106 if(lua_gettop(L) == 5 && !lua_isnil(L, 5))
1107 luaA_registerfct(L, 5, &button->release);
1108 else
1109 button->release = LUA_REFNIL;
1111 len = lua_objlen(L, 2);
1112 for(i = 1; i <= len; i++)
1114 size_t blen;
1115 const char *buf;
1116 lua_rawgeti(L, 2, i);
1117 buf = luaL_checklstring(L, -1, &blen);
1118 button->mod |= xutil_key_mask_fromstr(buf, blen);
1121 return luaA_button_userdata_new(L, button);
1124 /** Set a button array with a Lua table.
1125 * \param L The Lua VM state.
1126 * \param idx The index of the Lua table.
1127 * \param buttons The array button to fill.
1129 void
1130 luaA_button_array_set(lua_State *L, int idx, button_array_t *buttons)
1132 button_t **b;
1134 luaA_checktable(L, idx);
1135 button_array_wipe(buttons);
1136 button_array_init(buttons);
1137 lua_pushnil(L);
1138 while(lua_next(L, idx))
1140 b = luaA_checkudata(L, -1, "button");
1141 button_array_append(buttons, *b);
1142 button_ref(b);
1143 lua_pop(L, 1);
1147 /** Push an array of button as an Lua table onto the stack.
1148 * \param L The Lua VM state.
1149 * \param buttons The button array to push.
1150 * \return The number of elements pushed on stack.
1153 luaA_button_array_get(lua_State *L, button_array_t *buttons)
1155 luaA_otable_new(L);
1156 for(int i = 0; i < buttons->len; i++)
1158 luaA_button_userdata_new(L, buttons->tab[i]);
1159 lua_rawseti(L, -2, i + 1);
1161 return 1;
1164 /** Button object.
1165 * \param L The Lua VM state.
1166 * \return The number of elements pushed on stack.
1167 * \luastack
1168 * \lfield press The function called when button press event is received.
1169 * \lfield release The function called when button release event is received.
1171 static int
1172 luaA_button_index(lua_State *L)
1174 if(luaA_usemetatable(L, 1, 2))
1175 return 1;
1177 size_t len;
1178 button_t **button = luaA_checkudata(L, 1, "button");
1179 const char *attr = luaL_checklstring(L, 2, &len);
1181 switch(a_tokenize(attr, len))
1183 case A_TK_PRESS:
1184 if((*button)->press != LUA_REFNIL)
1185 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->press);
1186 else
1187 lua_pushnil(L);
1188 break;
1189 case A_TK_RELEASE:
1190 if((*button)->release != LUA_REFNIL)
1191 lua_rawgeti(L, LUA_REGISTRYINDEX, (*button)->release);
1192 else
1193 lua_pushnil(L);
1194 break;
1195 case A_TK_BUTTON:
1196 /* works fine, but not *really* neat */
1197 lua_pushnumber(L, (*button)->button);
1198 break;
1199 default:
1200 break;
1203 return 1;
1206 /** Button object.
1207 * \param L The Lua VM state.
1208 * \return The number of elements pushed on stack.
1209 * \luastack
1211 static int
1212 luaA_button_newindex(lua_State *L)
1214 if(luaA_usemetatable(L, 1, 2))
1215 return 1;
1217 size_t len;
1218 button_t **button = luaA_checkudata(L, 1, "button");
1219 const char *attr = luaL_checklstring(L, 2, &len);
1221 switch(a_tokenize(attr, len))
1223 case A_TK_PRESS:
1224 luaA_registerfct(L, 3, &(*button)->press);
1225 break;
1226 case A_TK_RELEASE:
1227 luaA_registerfct(L, 3, &(*button)->release);
1228 break;
1229 case A_TK_BUTTON:
1230 (*button)->button = xutil_button_fromint(luaL_checknumber(L, 3));
1231 break;
1232 default:
1233 break;
1236 return 0;
1239 const struct luaL_reg awesome_button_methods[] =
1241 { "__call", luaA_button_new },
1242 { NULL, NULL }
1244 const struct luaL_reg awesome_button_meta[] =
1246 { "__index", luaA_button_index },
1247 { "__newindex", luaA_button_newindex },
1248 { "__gc", luaA_button_gc },
1249 { "__eq", luaA_button_eq },
1250 { "__tostring", luaA_button_tostring },
1251 { NULL, NULL }
1254 /** Mouse library.
1255 * \param L The Lua VM state.
1256 * \return The number of elements pushed on stack.
1257 * \luastack
1258 * \lfield coords Mouse coordinates.
1259 * \lfield screen Mouse screen number.
1261 static int
1262 luaA_mouse_index(lua_State *L)
1264 size_t len;
1265 const char *attr = luaL_checklstring(L, 2, &len);
1266 int mouse_x, mouse_y, i;
1267 int screen;
1269 switch(a_tokenize(attr, len))
1271 case A_TK_SCREEN:
1272 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, NULL))
1273 return 0;
1275 i = screen_getbycoord(screen, mouse_x, mouse_y);
1277 lua_pushnumber(L, i + 1);
1278 break;
1279 default:
1280 return 0;
1283 return 1;
1286 /** Newindex for mouse.
1287 * \param L The Lua VM state.
1288 * \return The number of elements pushed on stack.
1290 static int
1291 luaA_mouse_newindex(lua_State *L)
1293 size_t len;
1294 const char *attr = luaL_checklstring(L, 2, &len);
1295 int x, y = 0;
1296 xcb_window_t root;
1297 int screen, phys_screen;
1299 switch(a_tokenize(attr, len))
1301 case A_TK_SCREEN:
1302 screen = luaL_checknumber(L, 3) - 1;
1303 luaA_checkscreen(screen);
1305 /* we need the physical one to get the root window */
1306 phys_screen = screen_virttophys(screen);
1307 root = xutil_screen_get(globalconf.connection, phys_screen)->root;
1309 x = globalconf.screens[screen].geometry.x;
1310 y = globalconf.screens[screen].geometry.y;
1312 mouse_warp_pointer(root, x, y);
1313 break;
1314 default:
1315 return 0;
1318 return 0;
1321 /** Get or set the mouse coords.
1322 * \param L The Lua VM state.
1323 * \return The number of elements pushed on stack.
1324 * \luastack
1325 * \lparam None or a table with x and y keys as mouse coordinates.
1326 * \lreturn A table with mouse coordinates.
1328 static int
1329 luaA_mouse_coords(lua_State *L)
1331 uint16_t mask, maski;
1332 int screen, x, y, mouse_x, mouse_y, i = 1;
1334 if(lua_gettop(L) == 1)
1336 xcb_window_t root;
1338 luaA_checktable(L, 1);
1340 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1341 return 0;
1343 x = luaA_getopt_number(L, 1, "x", mouse_x);
1344 y = luaA_getopt_number(L, 1, "y", mouse_y);
1346 root = xutil_screen_get(globalconf.connection, screen)->root;
1347 mouse_warp_pointer(root, x, y);
1348 lua_pop(L, 1);
1351 if(!mouse_query_pointer_root(&screen, &mouse_x, &mouse_y, &mask))
1352 return 0;
1354 lua_newtable(L);
1355 lua_pushnumber(L, mouse_x);
1356 lua_setfield(L, -2, "x");
1357 lua_pushnumber(L, mouse_y);
1358 lua_setfield(L, -2, "y");
1359 lua_newtable(L);
1360 for(maski = XCB_BUTTON_MASK_1; i <= XCB_BUTTON_MASK_5; maski <<= 1, i++)
1361 if(mask & maski)
1363 lua_pushboolean(L, true);
1364 lua_rawseti(L, -2, i);
1366 lua_setfield(L, -2, "buttons");
1368 return 1;
1371 const struct luaL_reg awesome_mouse_methods[] =
1373 { "__index", luaA_mouse_index },
1374 { "__newindex", luaA_mouse_newindex },
1375 { "coords", luaA_mouse_coords },
1376 { NULL, NULL }
1378 const struct luaL_reg awesome_mouse_meta[] =
1380 { NULL, NULL }
1383 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80