2 * keygrabber.c - key grabbing
4 * Copyright © 2008-2009 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.
24 #include "globalconf.h"
25 #include "keygrabber.h"
26 #include "objects/key.h"
28 #include "keyresolv.h"
29 #include "common/xutil.h"
31 /** Grab the keyboard.
32 * \return True if keyboard was grabbed.
38 xcb_grab_keyboard_reply_t
*xgb
;
42 if((xgb
= xcb_grab_keyboard_reply(globalconf
.connection
,
43 xcb_grab_keyboard(globalconf
.connection
, true,
44 globalconf
.screen
->root
,
45 XCB_CURRENT_TIME
, XCB_GRAB_MODE_ASYNC
,
57 /** Handle keypress event.
58 * \param L Lua stack to push the key pressed.
59 * \param e Received XKeyEvent.
60 * \return True if a key was successfully get, false otherwise.
63 keygrabber_handlekpress(lua_State
*L
, xcb_key_press_event_t
*e
)
65 /* transfer event (keycode + modifiers) to keysym */
66 xcb_keysym_t ksym
= keyresolv_get_keysym(e
->detail
, e
->state
);
68 /* convert keysym to string */
69 char buf
[MAX(MB_LEN_MAX
, 32)];
70 if(!keyresolv_keysym_to_string(ksym
, buf
, countof(buf
)))
73 luaA_pushmodifiers(L
, e
->state
);
75 lua_pushstring(L
, buf
);
77 switch(e
->response_type
)
80 lua_pushliteral(L
, "press");
83 lua_pushliteral(L
, "release");
90 /** Grab keyboard and read pressed keys, calling callback function at each key
91 * pressed. The callback function must return a boolean value: true to
92 * continue grabbing, false to stop.
93 * The function is called with 3 arguments:
94 * a table containing modifiers keys, a string with the key pressed and a
95 * string with either "press" or "release" to indicate the event type.
97 * \param L The Lua VM state.
98 * \return The number of elements pushed on stack.
102 * \lparam A callback function as described above.
105 luaA_keygrabber_run(lua_State
*L
)
107 if(globalconf
.keygrabber
!= LUA_REFNIL
)
108 luaL_error(L
, "keygrabber already running");
110 luaA_registerfct(L
, 1, &globalconf
.keygrabber
);
112 if(!keygrabber_grab())
114 luaA_unregister(L
, &globalconf
.keygrabber
);
115 luaL_error(L
, "unable to grab keyboard");
121 /** Stop grabbing the keyboard.
122 * \param L The Lua VM state.
123 * \return The number of elements pushed on stack.
126 luaA_keygrabber_stop(lua_State
*L
)
128 xcb_ungrab_keyboard(globalconf
.connection
, XCB_CURRENT_TIME
);
129 luaA_unregister(L
, &globalconf
.keygrabber
);
133 const struct luaL_reg awesome_keygrabber_lib
[] =
135 { "run", luaA_keygrabber_run
},
136 { "stop", luaA_keygrabber_stop
},
140 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80