Mirror of Awesome WM window manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mouse.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * mouse.c - mouse managing
  3. *
  4. * Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. *
  20. */
  21. /** awesome mouse API.
  22. *
  23. * The mouse buttons are represented as index. The common ones are:
  24. *
  25. * ![Client geometry](../images/mouse.svg)
  26. *
  27. * It is possible to be notified of mouse events by connecting to various
  28. * `client`, `widget`s and `wibox` signals:
  29. *
  30. * * `mouse::enter`
  31. * * `mouse::leave`
  32. * * `mouse::press`
  33. * * `mouse::release`
  34. * * `mouse::move`
  35. *
  36. * It is also possible to add generic mouse button callbacks for `client`s,
  37. * `wiboxe`s and the `root` window. Those are set in the default `rc.lua` as such:
  38. *
  39. * **root**:
  40. *
  41. * root.buttons(awful.util.table.join(
  42. * awful.button({ }, 3, function () mymainmenu:toggle() end),
  43. * awful.button({ }, 4, awful.tag.viewnext),
  44. * awful.button({ }, 5, awful.tag.viewprev)
  45. * ))
  46. *
  47. * **client**:
  48. *
  49. * clientbuttons = awful.util.table.join(
  50. * awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
  51. * awful.button({ modkey }, 1, awful.mouse.client.move),
  52. * awful.button({ modkey }, 3, awful.mouse.client.resize)
  53. * )
  54. *
  55. * See also `mousegrabber`
  56. *
  57. * @author Julien Danjou &lt;julien@danjou.info&gt;
  58. * @copyright 2008-2009 Julien Danjou
  59. * @inputmodule mouse
  60. */
  61. #include "mouse.h"
  62. #include "math.h"
  63. #include "common/util.h"
  64. #include "common/xutil.h"
  65. #include "common/luaclass.h"
  66. #include "globalconf.h"
  67. #include "objects/client.h"
  68. #include "objects/drawin.h"
  69. #include "objects/screen.h"
  70. static int miss_index_handler = LUA_REFNIL;
  71. static int miss_newindex_handler = LUA_REFNIL;
  72. /**
  73. * The `screen` under the cursor
  74. * @property screen
  75. * @param screen
  76. */
  77. /** Get the pointer position.
  78. * \param window The window to get position on.
  79. * \param x will be set to the Pointer-x-coordinate relative to window
  80. * \param y will be set to the Pointer-y-coordinate relative to window
  81. * \param child Will be set to the window under the pointer.
  82. * \param mask will be set to the current buttons state
  83. * \return true on success, false if an error occurred
  84. **/
  85. bool
  86. mouse_query_pointer(xcb_window_t window, int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
  87. {
  88. xcb_query_pointer_cookie_t query_ptr_c;
  89. xcb_query_pointer_reply_t *query_ptr_r;
  90. query_ptr_c = xcb_query_pointer_unchecked(globalconf.connection, window);
  91. query_ptr_r = xcb_query_pointer_reply(globalconf.connection, query_ptr_c, NULL);
  92. if(!query_ptr_r || !query_ptr_r->same_screen)
  93. {
  94. p_delete(&query_ptr_r);
  95. return false;
  96. }
  97. *x = query_ptr_r->win_x;
  98. *y = query_ptr_r->win_y;
  99. if(mask)
  100. *mask = query_ptr_r->mask;
  101. if(child)
  102. *child = query_ptr_r->child;
  103. p_delete(&query_ptr_r);
  104. return true;
  105. }
  106. /** Get the pointer position on the screen.
  107. * \param x This will be set to the Pointer-x-coordinate relative to window.
  108. * \param y This will be set to the Pointer-y-coordinate relative to window.
  109. * \param child This will be set to the window under the pointer.
  110. * \param mask This will be set to the current buttons state.
  111. * \return True on success, false if an error occurred.
  112. */
  113. static bool
  114. mouse_query_pointer_root(int16_t *x, int16_t *y, xcb_window_t *child, uint16_t *mask)
  115. {
  116. xcb_window_t root = globalconf.screen->root;
  117. return mouse_query_pointer(root, x, y, child, mask);
  118. }
  119. /** Set the pointer position.
  120. * \param window The destination window.
  121. * \param x X-coordinate inside window.
  122. * \param y Y-coordinate inside window.
  123. */
  124. static inline void
  125. mouse_warp_pointer(xcb_window_t window, int16_t x, int16_t y)
  126. {
  127. xcb_warp_pointer(globalconf.connection, XCB_NONE, window,
  128. 0, 0, 0, 0, x, y);
  129. }
  130. /** Mouse library.
  131. * \param L The Lua VM state.
  132. * \return The number of elements pushed on stack.
  133. * \luastack
  134. * \lfield coords Mouse coordinates.
  135. * \lfield screen Mouse screen.
  136. */
  137. static int
  138. luaA_mouse_index(lua_State *L)
  139. {
  140. const char *attr = luaL_checkstring(L, 2);
  141. int16_t mouse_x, mouse_y;
  142. /* attr is not "screen"?! */
  143. if (A_STRNEQ(attr, "screen")) {
  144. if (miss_index_handler != LUA_REFNIL) {
  145. return luaA_call_handler(L, miss_index_handler);
  146. }
  147. else
  148. return luaA_default_index(L);
  149. }
  150. if (!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, NULL))
  151. {
  152. /* Nothing ever handles mouse.screen being nil. Lying is better than
  153. * having lots of lua errors in this case.
  154. */
  155. if (globalconf.focus.client)
  156. luaA_object_push(L, globalconf.focus.client->screen);
  157. else
  158. luaA_object_push(L, screen_get_primary());
  159. return 1;
  160. }
  161. luaA_object_push(L, screen_getbycoord(mouse_x, mouse_y));
  162. return 1;
  163. }
  164. /** Newindex for mouse.
  165. * \param L The Lua VM state.
  166. * \return The number of elements pushed on stack.
  167. */
  168. static int
  169. luaA_mouse_newindex(lua_State *L)
  170. {
  171. const char *attr = luaL_checkstring(L, 2);
  172. screen_t *screen;
  173. if (A_STRNEQ(attr, "screen")) {
  174. /* Call the lua mouse property handler */
  175. if (miss_newindex_handler != LUA_REFNIL) {
  176. return luaA_call_handler(L, miss_newindex_handler);
  177. }
  178. else
  179. return luaA_default_newindex(L);
  180. }
  181. screen = luaA_checkscreen(L, 3);
  182. mouse_warp_pointer(globalconf.screen->root, screen->geometry.x, screen->geometry.y);
  183. return 0;
  184. }
  185. /** Push a table with mouse status.
  186. * \param L The Lua VM state.
  187. * \param x The x coordinate.
  188. * \param y The y coordinate.
  189. * \param mask The button mask.
  190. */
  191. int
  192. luaA_mouse_pushstatus(lua_State *L, int x, int y, uint16_t mask)
  193. {
  194. lua_createtable(L, 0, 2);
  195. lua_pushinteger(L, x);
  196. lua_setfield(L, -2, "x");
  197. lua_pushinteger(L, y);
  198. lua_setfield(L, -2, "y");
  199. lua_createtable(L, 5, 0);
  200. int i = 1;
  201. for(uint16_t maski = XCB_BUTTON_MASK_1; maski <= XCB_BUTTON_MASK_5; maski <<= 1)
  202. {
  203. if(mask & maski)
  204. lua_pushboolean(L, true);
  205. else
  206. lua_pushboolean(L, false);
  207. lua_rawseti(L, -2, i++);
  208. }
  209. lua_setfield(L, -2, "buttons");
  210. return 1;
  211. }
  212. /* documented in lib/awful/mouse/init.lua */
  213. static int
  214. luaA_mouse_coords(lua_State *L)
  215. {
  216. uint16_t mask;
  217. int x, y;
  218. int16_t mouse_x, mouse_y;
  219. if(lua_gettop(L) >= 1)
  220. {
  221. luaA_checktable(L, 1);
  222. bool ignore_enter_notify = (lua_gettop(L) == 2 && luaA_checkboolean(L, 2));
  223. if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
  224. return 0;
  225. x = round(luaA_getopt_number_range(L, 1, "x", mouse_x, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
  226. y = round(luaA_getopt_number_range(L, 1, "y", mouse_y, MIN_X11_COORDINATE, MAX_X11_COORDINATE));
  227. if(ignore_enter_notify)
  228. client_ignore_enterleave_events();
  229. mouse_warp_pointer(globalconf.screen->root, x, y);
  230. if(ignore_enter_notify)
  231. client_restore_enterleave_events();
  232. lua_pop(L, 1);
  233. }
  234. if(!mouse_query_pointer_root(&mouse_x, &mouse_y, NULL, &mask))
  235. return 0;
  236. return luaA_mouse_pushstatus(L, mouse_x, mouse_y, mask);
  237. }
  238. /** Get the client or any object which is under the pointer.
  239. *
  240. * @treturn client.object|nil A client or nil.
  241. * @staticfct object_under_pointer
  242. */
  243. static int
  244. luaA_mouse_object_under_pointer(lua_State *L)
  245. {
  246. int16_t mouse_x, mouse_y;
  247. xcb_window_t child;
  248. if(!mouse_query_pointer_root(&mouse_x, &mouse_y, &child, NULL))
  249. return 0;
  250. drawin_t *drawin;
  251. client_t *client;
  252. if((drawin = drawin_getbywin(child)))
  253. return luaA_object_push(L, drawin);
  254. if((client = client_getbyframewin(child)))
  255. return luaA_object_push(L, client);
  256. return 0;
  257. }
  258. /**
  259. * Add a custom property handler (getter).
  260. */
  261. static int
  262. luaA_mouse_set_index_miss_handler(lua_State *L)
  263. {
  264. return luaA_registerfct(L, 1, &miss_index_handler);
  265. }
  266. /**
  267. * Add a custom property handler (setter).
  268. */
  269. static int
  270. luaA_mouse_set_newindex_miss_handler(lua_State *L)
  271. {
  272. return luaA_registerfct(L, 1, &miss_newindex_handler);
  273. }
  274. const struct luaL_Reg awesome_mouse_methods[] =
  275. {
  276. { "__index", luaA_mouse_index },
  277. { "__newindex", luaA_mouse_newindex },
  278. { "coords", luaA_mouse_coords },
  279. { "object_under_pointer", luaA_mouse_object_under_pointer },
  280. { "set_index_miss_handler", luaA_mouse_set_index_miss_handler},
  281. { "set_newindex_miss_handler", luaA_mouse_set_newindex_miss_handler},
  282. { NULL, NULL }
  283. };
  284. const struct luaL_Reg awesome_mouse_meta[] =
  285. {
  286. { NULL, NULL }
  287. };
  288. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80