2008-06-10 16:50:55 +02:00
|
|
|
/*
|
|
|
|
* keygrabber.c - key grabbing
|
|
|
|
*
|
|
|
|
* Copyright © 2008 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2008-09-17 16:38:38 +02:00
|
|
|
#include "structs.h"
|
2008-06-10 16:50:55 +02:00
|
|
|
#include "keygrabber.h"
|
2008-12-18 11:33:19 +01:00
|
|
|
#include "key.h"
|
2009-04-17 16:52:25 +02:00
|
|
|
#include "common/xutil.h"
|
2008-06-10 16:50:55 +02:00
|
|
|
|
|
|
|
/** Grab the keyboard.
|
2008-06-20 08:32:46 +02:00
|
|
|
* \return True if keyboard was grabbed.
|
2008-06-10 16:50:55 +02:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
keygrabber_grab(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
xcb_grab_keyboard_reply_t *xgb;
|
|
|
|
|
|
|
|
for(i = 1000; i; i--)
|
|
|
|
{
|
|
|
|
if((xgb = xcb_grab_keyboard_reply(globalconf.connection,
|
|
|
|
xcb_grab_keyboard(globalconf.connection, true,
|
2008-06-17 16:55:24 +02:00
|
|
|
xutil_screen_get(globalconf.connection,
|
2008-06-10 16:50:55 +02:00
|
|
|
globalconf.default_screen)->root,
|
|
|
|
XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC,
|
|
|
|
XCB_GRAB_MODE_ASYNC),
|
|
|
|
NULL)))
|
|
|
|
{
|
|
|
|
p_delete(&xgb);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
usleep(1000);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Handle keypress event.
|
|
|
|
* \param L Lua stack to push the key pressed.
|
|
|
|
* \param e Received XKeyEvent.
|
2008-06-10 22:37:45 +02:00
|
|
|
* \return True if a key was succesfully get, false otherwise.
|
2008-06-10 16:50:55 +02:00
|
|
|
*/
|
2008-06-10 22:37:45 +02:00
|
|
|
bool
|
2008-06-10 16:50:55 +02:00
|
|
|
keygrabber_handlekpress(lua_State *L, xcb_key_press_event_t *e)
|
|
|
|
{
|
2009-04-29 10:43:54 +02:00
|
|
|
/* transfer event (keycode + modifiers) to keysym */
|
|
|
|
xcb_keysym_t ksym = key_getkeysym(e->detail, e->state);
|
2009-04-28 19:47:39 +02:00
|
|
|
|
2009-04-29 10:43:54 +02:00
|
|
|
/* convert keysym to string */
|
|
|
|
char buf[MAX(MB_LEN_MAX, 32)];
|
|
|
|
if(!key_press_lookup_string(ksym, buf, countof(buf)))
|
2009-04-28 19:47:39 +02:00
|
|
|
return false;
|
2008-06-10 16:50:55 +02:00
|
|
|
|
2009-04-27 21:04:15 +02:00
|
|
|
luaA_pushmodifiers(L, e->state);
|
2008-06-10 16:50:55 +02:00
|
|
|
|
2009-04-28 19:47:39 +02:00
|
|
|
lua_pushstring(L, buf);
|
2008-09-02 14:53:56 +02:00
|
|
|
|
2008-12-16 15:02:54 +01:00
|
|
|
switch(e->response_type)
|
|
|
|
{
|
|
|
|
case XCB_KEY_PRESS:
|
|
|
|
lua_pushliteral(L, "press");
|
|
|
|
break;
|
|
|
|
case XCB_KEY_RELEASE:
|
|
|
|
lua_pushliteral(L, "release");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-10 22:37:45 +02:00
|
|
|
return true;
|
2008-06-10 16:50:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Grab keyboard and read pressed keys, calling callback function at each key
|
|
|
|
* pressed. The callback function must return a boolean value: true to
|
|
|
|
* continue grabbing, false to stop.
|
2008-12-16 15:02:54 +01:00
|
|
|
* The function is called with 3 arguments:
|
|
|
|
* a table containing modifiers keys, a string with the key pressed and a
|
|
|
|
* string with eithe "press" or "release" to indicate the event type.
|
2008-06-10 16:50:55 +02:00
|
|
|
*
|
|
|
|
* \param L The Lua VM state.
|
2008-11-10 17:26:38 +01:00
|
|
|
* \return The number of elements pushed on stack.
|
2008-06-10 16:50:55 +02:00
|
|
|
*
|
|
|
|
* \luastack
|
|
|
|
*
|
|
|
|
* \lparam A callback function as described above.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_keygrabber_run(lua_State *L)
|
|
|
|
{
|
|
|
|
if(globalconf.keygrabber != LUA_REFNIL)
|
|
|
|
luaL_error(L, "keygrabber already running");
|
|
|
|
|
2008-11-10 17:26:38 +01:00
|
|
|
luaA_registerfct(L, 1, &globalconf.keygrabber);
|
2008-06-10 16:50:55 +02:00
|
|
|
|
|
|
|
if(!keygrabber_grab())
|
2008-11-10 17:26:38 +01:00
|
|
|
{
|
|
|
|
luaA_unregister(L, &globalconf.keygrabber);
|
2008-06-10 16:50:55 +02:00
|
|
|
luaL_error(L, "unable to grab keyboard");
|
2008-11-10 17:26:38 +01:00
|
|
|
}
|
2008-06-10 16:50:55 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-10 17:26:38 +01:00
|
|
|
/** Stop grabbing the keyboard.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_keygrabber_stop(lua_State *L)
|
|
|
|
{
|
|
|
|
xcb_ungrab_keyboard(globalconf.connection, XCB_CURRENT_TIME);
|
|
|
|
luaA_unregister(L, &globalconf.keygrabber);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:50:55 +02:00
|
|
|
const struct luaL_reg awesome_keygrabber_lib[] =
|
|
|
|
{
|
2008-11-10 17:26:38 +01:00
|
|
|
{ "run", luaA_keygrabber_run },
|
|
|
|
{ "stop", luaA_keygrabber_stop },
|
2008-06-10 16:50:55 +02:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|