ta{g,sk}list: fix memory leak on removal
[awesome.git] / keygrabber.c
blobc5b33bb20b23c4977a2b3d2844ce0f7f0f1df020
1 /*
2 * keygrabber.c - key grabbing
4 * Copyright © 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 <unistd.h>
24 #include "structs.h"
25 #include "keygrabber.h"
26 #include "key.h"
27 #include "common/xutil.h"
29 /** Grab the keyboard.
30 * \return True if keyboard was grabbed.
32 static bool
33 keygrabber_grab(void)
35 int i;
36 xcb_grab_keyboard_reply_t *xgb;
38 for(i = 1000; i; i--)
40 if((xgb = xcb_grab_keyboard_reply(globalconf.connection,
41 xcb_grab_keyboard(globalconf.connection, true,
42 xutil_screen_get(globalconf.connection,
43 globalconf.default_screen)->root,
44 XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC,
45 XCB_GRAB_MODE_ASYNC),
46 NULL)))
48 p_delete(&xgb);
49 return true;
51 usleep(1000);
53 return false;
56 /** Handle keypress event.
57 * \param L Lua stack to push the key pressed.
58 * \param e Received XKeyEvent.
59 * \return True if a key was succesfully get, false otherwise.
61 bool
62 keygrabber_handlekpress(lua_State *L, xcb_key_press_event_t *e)
64 /* transfer event (keycode + modifiers) to keysym */
65 xcb_keysym_t ksym = key_getkeysym(e->detail, e->state);
67 /* convert keysym to string */
68 char buf[MAX(MB_LEN_MAX, 32)];
69 if(!key_press_lookup_string(ksym, buf, countof(buf)))
70 return false;
72 luaA_pushmodifiers(L, e->state);
74 lua_pushstring(L, buf);
76 switch(e->response_type)
78 case XCB_KEY_PRESS:
79 lua_pushliteral(L, "press");
80 break;
81 case XCB_KEY_RELEASE:
82 lua_pushliteral(L, "release");
83 break;
86 return true;
89 /** Grab keyboard and read pressed keys, calling callback function at each key
90 * pressed. The callback function must return a boolean value: true to
91 * continue grabbing, false to stop.
92 * The function is called with 3 arguments:
93 * a table containing modifiers keys, a string with the key pressed and a
94 * string with eithe "press" or "release" to indicate the event type.
96 * \param L The Lua VM state.
97 * \return The number of elements pushed on stack.
99 * \luastack
101 * \lparam A callback function as described above.
103 static int
104 luaA_keygrabber_run(lua_State *L)
106 if(globalconf.keygrabber != LUA_REFNIL)
107 luaL_error(L, "keygrabber already running");
109 luaA_registerfct(L, 1, &globalconf.keygrabber);
111 if(!keygrabber_grab())
113 luaA_unregister(L, &globalconf.keygrabber);
114 luaL_error(L, "unable to grab keyboard");
117 return 0;
120 /** Stop grabbing the keyboard.
121 * \param L The Lua VM state.
122 * \return The number of elements pushed on stack.
125 luaA_keygrabber_stop(lua_State *L)
127 xcb_ungrab_keyboard(globalconf.connection, XCB_CURRENT_TIME);
128 luaA_unregister(L, &globalconf.keygrabber);
129 return 0;
132 const struct luaL_reg awesome_keygrabber_lib[] =
134 { "run", luaA_keygrabber_run },
135 { "stop", luaA_keygrabber_stop },
136 { NULL, NULL }