Use gdk-pixbuf instead of Imlib2 (FS#1028)
[awesome.git] / keygrabber.c
blobccf5bb45458c837499038d44de4b409bc98815d0
1 /*
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.
22 #include <unistd.h>
24 #include "globalconf.h"
25 #include "keygrabber.h"
26 #include "objects/key.h"
27 #include "luaa.h"
28 #include "keyresolv.h"
29 #include "common/xutil.h"
31 /** Grab the keyboard.
32 * \return True if keyboard was grabbed.
34 static bool
35 keygrabber_grab(void)
37 int i;
38 xcb_grab_keyboard_reply_t *xgb;
40 for(i = 1000; i; i--)
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,
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 = 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)))
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 * press, until keygrabber.stop is called.
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 either "press" or "release" to indicate the event type.
96 * \param L The Lua VM state.
97 * \return The number of elements pushed on stack.
99 * \luastack
101 * \lparam A callback function as described above.
103 static int
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");
117 return 0;
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);
129 return 0;
132 /** Check if keygrabber is running.
133 * \param L The Lua VM state.
134 * \return The number of elements pushed on stack.
135 * \luastack
136 * \lreturn A boolean value, true if keygrabber is running, false otherwise.
138 static int
139 luaA_keygrabber_isrunning(lua_State *L)
141 lua_pushboolean(L, globalconf.keygrabber != LUA_REFNIL);
142 return 1;
145 const struct luaL_Reg awesome_keygrabber_lib[] =
147 { "run", luaA_keygrabber_run },
148 { "stop", luaA_keygrabber_stop },
149 { "isrunning", luaA_keygrabber_isrunning },
150 { NULL, NULL }
153 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80