change codename
[awesome.git] / keygrabber.c
blob177eef2ec06c71007c648d0596b48bd6fc4d6c84
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 "globalconf.h"
25 #include "keygrabber.h"
26 #include "key.h"
27 #include "luaa.h"
28 #include "common/xutil.h"
30 /** Grab the keyboard.
31 * \return True if keyboard was grabbed.
33 static bool
34 keygrabber_grab(void)
36 int i;
37 xcb_grab_keyboard_reply_t *xgb;
39 for(i = 1000; i; i--)
41 if((xgb = xcb_grab_keyboard_reply(globalconf.connection,
42 xcb_grab_keyboard(globalconf.connection, true,
43 xutil_screen_get(globalconf.connection,
44 globalconf.default_screen)->root,
45 XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC,
46 XCB_GRAB_MODE_ASYNC),
47 NULL)))
49 p_delete(&xgb);
50 return true;
52 usleep(1000);
54 return false;
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.
62 bool
63 keygrabber_handlekpress(lua_State *L, xcb_key_press_event_t *e)
65 /* transfer event (keycode + modifiers) to keysym */
66 xcb_keysym_t ksym = key_getkeysym(e->detail, e->state);
68 /* convert keysym to string */
69 char buf[MAX(MB_LEN_MAX, 32)];
70 if(!key_press_lookup_string(ksym, buf, countof(buf)))
71 return false;
73 luaA_pushmodifiers(L, e->state);
75 lua_pushstring(L, buf);
77 switch(e->response_type)
79 case XCB_KEY_PRESS:
80 lua_pushliteral(L, "press");
81 break;
82 case XCB_KEY_RELEASE:
83 lua_pushliteral(L, "release");
84 break;
87 return true;
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.
100 * \luastack
102 * \lparam A callback function as described above.
104 static int
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");
118 return 0;
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);
130 return 0;
133 const struct luaL_reg awesome_keygrabber_lib[] =
135 { "run", luaA_keygrabber_run },
136 { "stop", luaA_keygrabber_stop },
137 { NULL, NULL }
140 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80