2008-06-10 16:50:55 +02:00
|
|
|
/*
|
|
|
|
* keygrabber.c - key grabbing
|
|
|
|
*
|
2009-10-02 17:50:13 +02:00
|
|
|
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
|
2008-06-10 16:50:55 +02:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2009-09-07 17:22:32 +02:00
|
|
|
#include "globalconf.h"
|
2008-06-10 16:50:55 +02:00
|
|
|
#include "keygrabber.h"
|
2009-09-14 20:27:56 +02:00
|
|
|
#include "objects/key.h"
|
2009-08-17 16:38:56 +02:00
|
|
|
#include "luaa.h"
|
2009-10-02 17:50:13 +02:00
|
|
|
#include "keyresolv.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,
|
2010-08-16 14:10:58 +02:00
|
|
|
globalconf.screen->root,
|
2008-06-10 16:50:55 +02:00
|
|
|
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.
|
2009-08-30 07:26:19 +02:00
|
|
|
* \return True if a key was successfully 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 */
|
2009-10-02 17:50:13 +02:00
|
|
|
xcb_keysym_t ksym = keyresolv_get_keysym(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)];
|
2009-10-02 17:50:13 +02:00
|
|
|
if(!keyresolv_keysym_to_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
|
2009-08-30 07:26:19 +02:00
|
|
|
* string with either "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 }
|
|
|
|
};
|
2009-08-21 16:44:30 +02:00
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|