PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/awesome-3.4-rc3/root.c

https://github.com/Elv13/Patched-Awesome
C | 251 lines | 157 code | 31 blank | 63 comment | 14 complexity | 30b3e5f63352781e71da5d1a1a7af677 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * root.c - root window management
  3. *
  4. * Copyright © 2008-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. #include <xcb/xtest.h>
  22. #include "globalconf.h"
  23. #include "button.h"
  24. #include "wibox.h"
  25. #include "luaa.h"
  26. #include "window.h"
  27. #include "common/xcursor.h"
  28. #include "common/tokenize.h"
  29. #include "common/xutil.h"
  30. /** Send fake events. Usually the current focused client will get it.
  31. * \param L The Lua VM state.
  32. * \return The number of element pushed on stack.
  33. * \luastack
  34. * \lparam The event type: key_press, key_release, button_press, button_release
  35. * or motion_notify.
  36. * \lparam The detail: in case of a key event, this is the keycode to send, in
  37. * case of a button event this is the number of the button. In case of a motion
  38. * event, this is a boolean value which if true make the coordinates relatives.
  39. * \lparam In case of a motion event, this is the X coordinate.
  40. * \lparam In case of a motion event, this is the Y coordinate.
  41. * \lparam In case of a motion event, this is the screen number to move on.
  42. * If not specified, the current one is used.
  43. */
  44. static int
  45. luaA_root_fake_input(lua_State *L)
  46. {
  47. if(!globalconf.have_xtest)
  48. {
  49. luaA_warn(L, "XTest extension is not available, cannot fake input.");
  50. return 0;
  51. }
  52. size_t tlen;
  53. const char *stype = luaL_checklstring(L, 1, &tlen);
  54. uint8_t type, detail;
  55. int x = 0, y = 0;
  56. xcb_window_t root = XCB_NONE;
  57. switch(a_tokenize(stype, tlen))
  58. {
  59. case A_TK_KEY_PRESS:
  60. type = XCB_KEY_PRESS;
  61. detail = luaL_checknumber(L, 2); /* keycode */
  62. break;
  63. case A_TK_KEY_RELEASE:
  64. type = XCB_KEY_RELEASE;
  65. detail = luaL_checknumber(L, 2); /* keycode */
  66. break;
  67. case A_TK_BUTTON_PRESS:
  68. type = XCB_BUTTON_PRESS;
  69. detail = luaL_checknumber(L, 2); /* button number */
  70. break;
  71. case A_TK_BUTTON_RELEASE:
  72. type = XCB_BUTTON_RELEASE;
  73. detail = luaL_checknumber(L, 2); /* button number */
  74. break;
  75. case A_TK_MOTION_NOTIFY:
  76. type = XCB_MOTION_NOTIFY;
  77. detail = luaA_checkboolean(L, 2); /* relative to the current position or not */
  78. x = luaL_checknumber(L, 3);
  79. y = luaL_checknumber(L, 4);
  80. if(lua_gettop(L) == 5 && !globalconf.xinerama_is_active)
  81. {
  82. int screen = luaL_checknumber(L, 5) - 1;
  83. luaA_checkscreen(screen);
  84. root = xutil_screen_get(globalconf.connection, screen)->root;
  85. }
  86. break;
  87. default:
  88. return 0;
  89. }
  90. xcb_test_fake_input(globalconf.connection,
  91. type,
  92. detail,
  93. XCB_CURRENT_TIME,
  94. root,
  95. x, y,
  96. 0);
  97. return 0;
  98. }
  99. /** Get or set global key bindings.
  100. * This binding will be available when you'll press keys on root window.
  101. * \param L The Lua VM state.
  102. * \return The number of element pushed on stack.
  103. * \luastack
  104. * \lparam An array of key bindings objects, or nothing.
  105. * \lreturn The array of key bindings objects of this client.
  106. */
  107. static int
  108. luaA_root_keys(lua_State *L)
  109. {
  110. if(lua_gettop(L) == 1)
  111. {
  112. luaA_checktable(L, 1);
  113. foreach(key, globalconf.keys)
  114. luaA_object_unref(globalconf.L, *key);
  115. key_array_wipe(&globalconf.keys);
  116. key_array_init(&globalconf.keys);
  117. lua_pushnil(L);
  118. while(lua_next(L, 1))
  119. key_array_append(&globalconf.keys, luaA_object_ref_class(L, -1, &key_class));
  120. int nscreen = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
  121. for(int phys_screen = 0; phys_screen < nscreen; phys_screen++)
  122. {
  123. xcb_screen_t *s = xutil_screen_get(globalconf.connection, phys_screen);
  124. xcb_ungrab_key(globalconf.connection, XCB_GRAB_ANY, s->root, XCB_BUTTON_MASK_ANY);
  125. window_grabkeys(s->root, &globalconf.keys);
  126. }
  127. return 1;
  128. }
  129. lua_createtable(L, globalconf.keys.len, 0);
  130. for(int i = 0; i < globalconf.keys.len; i++)
  131. {
  132. luaA_object_push(L, globalconf.keys.tab[i]);
  133. lua_rawseti(L, -2, i + 1);
  134. }
  135. return 1;
  136. }
  137. /** Get or set global mouse bindings.
  138. * This binding will be available when you'll click on root window.
  139. * \param L The Lua VM state.
  140. * \return The number of element pushed on stack.
  141. * \luastack
  142. * \lparam An array of mouse button bindings objects, or nothing.
  143. * \lreturn The array of mouse button bindings objects.
  144. */
  145. static int
  146. luaA_root_buttons(lua_State *L)
  147. {
  148. if(lua_gettop(L) == 1)
  149. {
  150. luaA_checktable(L, 1);
  151. foreach(button, globalconf.buttons)
  152. luaA_object_unref(globalconf.L, *button);
  153. button_array_wipe(&globalconf.buttons);
  154. button_array_init(&globalconf.buttons);
  155. lua_pushnil(L);
  156. while(lua_next(L, 1))
  157. button_array_append(&globalconf.buttons, luaA_object_ref(L, -1));
  158. return 1;
  159. }
  160. lua_createtable(L, globalconf.buttons.len, 0);
  161. for(int i = 0; i < globalconf.buttons.len; i++)
  162. {
  163. luaA_object_push(L, globalconf.buttons.tab[i]);
  164. lua_rawseti(L, -2, i + 1);
  165. }
  166. return 1;
  167. }
  168. /** Set the root cursor.
  169. * \param L The Lua VM state.
  170. * \return The number of element pushed on stack.
  171. * \luastack
  172. * \lparam A X cursor name.
  173. */
  174. static int
  175. luaA_root_cursor(lua_State *L)
  176. {
  177. const char *cursor_name = luaL_checkstring(L, 1);
  178. uint16_t cursor_font = xcursor_font_fromstr(cursor_name);
  179. if(cursor_font)
  180. {
  181. uint32_t change_win_vals[] = { xcursor_new(globalconf.connection, cursor_font) };
  182. for(int screen_nbr = 0;
  183. screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
  184. screen_nbr++)
  185. xcb_change_window_attributes(globalconf.connection,
  186. xutil_screen_get(globalconf.connection, screen_nbr)->root,
  187. XCB_CW_CURSOR,
  188. change_win_vals);
  189. }
  190. else
  191. luaA_warn(L, "invalid cursor %s", cursor_name);
  192. return 0;
  193. }
  194. /** Get the wiboxes attached to a screen.
  195. * \param L The Lua VM state.
  196. * \return The number of element pushed on stack.
  197. * \luastack
  198. * \lreturn A table with all wiboxes.
  199. */
  200. static int
  201. luaA_root_wiboxes(lua_State *L)
  202. {
  203. lua_createtable(L, globalconf.wiboxes.len, 0);
  204. for(int i = 0; i < globalconf.wiboxes.len; i++)
  205. {
  206. luaA_object_push(L, globalconf.wiboxes.tab[i]);
  207. lua_rawseti(L, -2, i + 1);
  208. }
  209. return 1;
  210. }
  211. const struct luaL_reg awesome_root_lib[] =
  212. {
  213. { "buttons", luaA_root_buttons },
  214. { "keys", luaA_root_keys },
  215. { "cursor", luaA_root_cursor },
  216. { "fake_input", luaA_root_fake_input },
  217. { "wiboxes", luaA_root_wiboxes },
  218. { NULL, NULL }
  219. };
  220. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80