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.
25 #include "keygrabber.h"
27 #include "common/xutil.h"
29 /** Grab the keyboard.
30 * \return True if keyboard was grabbed.
36 xcb_grab_keyboard_reply_t
*xgb
;
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
,
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.
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
)))
72 luaA_pushmodifiers(L
, e
->state
);
74 lua_pushstring(L
, buf
);
76 switch(e
->response_type
)
79 lua_pushliteral(L
, "press");
82 lua_pushliteral(L
, "release");
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.
101 * \lparam A callback function as described above.
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");
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
);
132 const struct luaL_reg awesome_keygrabber_lib
[] =
134 { "run", luaA_keygrabber_run
},
135 { "stop", luaA_keygrabber_stop
},