1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
* key.c - Key bindings configuration management
*
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
* Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
*
* 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.
*
*/
/** awesome key API
*
* Furthermore to the classes described here, one can also use signals as
* described in @{signals}.
*
* Some signal names are starting with a dot. These dots are artefacts from
* the documentation generation, you get the real signal name by
* removing the starting dot.
*
* @author Julien Danjou <julien@danjou.info>
* @copyright 2008-2009 Julien Danjou
* @classmod key
*/
#include "objects/key.h"
#include "common/xutil.h"
#include "xkb.h"
/* XStringToKeysym() */
#include <X11/Xlib.h>
#include <xkbcommon/xkbcommon.h>
#include <glib.h>
lua_class_t key_class;
/** Key object.
*
* @tfield string key The key to trigger an event.
* @tfield string keysym Same as key, but return the name of the key symbol. It
* can be identical to key, but for characters like '.' it will return
* 'period'.
* @tfield table modifiers The modifier key that should be pressed while the
* key is pressed. An array with all the modifiers. Valid modifiers are: Any,
* Mod1, Mod2, Mod3, Mod4, Mod5, Shift, Lock and Control.
* @table key
*/
/**
* @signal press
*/
/**
* @signal property::key
*/
/**
* @signal property::modifiers
*/
/**
* @signal release
*/
/** Get the number of instances.
*
* @return The number of key objects alive.
* @function instances
*/
/** Set a __index metamethod for all key instances.
* @tparam function cb The meta-method
* @function set_index_miss_handler
*/
/** Set a __newindex metamethod for all key instances.
* @tparam function cb The meta-method
* @function set_newindex_miss_handler
*/
static void
luaA_keystore(lua_State *L, int ud, const char *str, ssize_t len)
{
if(len <= 0 || !str)
return;
keyb_t *key = luaA_checkudata(L, ud, &key_class);
if(len == 1)
{
key->keycode = 0;
key->keysym = str[0];
}
else if(str[0] == '#')
{
key->keycode = atoi(str + 1);
key->keysym = 0;
}
else
{
key->keycode = 0;
if((key->keysym = XStringToKeysym(str)) == NoSymbol )
{
glong length;
gunichar unicode;
if(!g_utf8_validate(str, -1, NULL))
{
luaA_warn(L, "failed to convert \"%s\" into keysym (invalid UTF-8 string)", str);
return;
}
length = g_utf8_strlen(str, -1); /* This function counts combining characters. */
if(length <= 0)
{
luaA_warn(L, "failed to convert \"%s\" into keysym (empty UTF-8 string)", str);
return;
}
else if(length > 1)
{
gchar *composed = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT_COMPOSE);
if(g_utf8_strlen(composed, -1) != 1)
{
p_delete(&composed);
luaA_warn(L, "failed to convert \"%s\" into keysym (failed to compose a single character)", str);
return;
}
unicode = g_utf8_get_char(composed);
p_delete(&composed);
}
else
unicode = g_utf8_get_char(str);
if(unicode == (gunichar)-1 || unicode == (gunichar)-2)
{
luaA_warn(L, "failed to convert \"%s\" into keysym (neither keysym nor single unicode)", str);
return;
}
/* Unicode-to-Keysym Conversion
*
* http://www.x.org/releases/X11R7.7/doc/xproto/x11protocol.html#keysym_encoding
*/
if(unicode <= 0x0ff)
key->keysym = unicode;
else if(unicode >= 0x100 && unicode <= 0x10ffff)
key->keysym = unicode | (1 << 24);
else
{
luaA_warn(L, "failed to convert \"%s\" into keysym (unicode out of range): \"%u\"", str, unicode);
return;
}
}
}
luaA_object_emit_signal(L, ud, "property::key", 0);
}
/** Create a new key object.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_key_new(lua_State *L)
{
return luaA_class_new(L, &key_class);
}
/** Set a key array with a Lua table.
* \param L The Lua VM state.
* \param oidx The index of the object to store items into.
* \param idx The index of the Lua table.
* \param keys The array key to fill.
*/
void
luaA_key_array_set(lua_State *L, int oidx, int idx, key_array_t *keys)
{
luaA_checktable(L, idx);
foreach(key, *keys)
luaA_object_unref_item(L, oidx, *key);
key_array_wipe(keys);
key_array_init(keys);
lua_pushnil(L);
while(lua_next(L, idx))
if(luaA_toudata(L, -1, &key_class))
key_array_append(keys, luaA_object_ref_item(L, oidx, -1));
else
lua_pop(L, 1);
}
/** Push an array of key as an Lua table onto the stack.
* \param L The Lua VM state.
* \param oidx The index of the object to get items from.
* \param keys The key array to push.
* \return The number of elements pushed on stack.
*/
int
luaA_key_array_get(lua_State *L, int oidx, key_array_t *keys)
{
lua_createtable(L, keys->len, 0);
for(int i = 0; i < keys->len; i++)
{
luaA_object_push_item(L, oidx, keys->tab[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
/** Push a modifier set to a Lua table.
* \param L The Lua VM state.
* \param modifiers The modifier.
* \return The number of elements pushed on stack.
*/
int
luaA_pushmodifiers(lua_State *L, uint16_t modifiers)
{
lua_newtable(L);
{
int i = 1;
for(uint32_t maski = XCB_MOD_MASK_SHIFT; maski <= XCB_BUTTON_MASK_ANY; maski <<= 1)
if(maski & modifiers)
{
const char *mod;
size_t slen;
xutil_key_mask_tostr(maski, &mod, &slen);
lua_pushlstring(L, mod, slen);
lua_rawseti(L, -2, i++);
}
}
return 1;
}
/** Take a modifier table from the stack and return modifiers mask.
* \param L The Lua VM state.
* \param ud The index of the table.
* \return The mask value.
*/
uint16_t
luaA_tomodifiers(lua_State *L, int ud)
{
luaA_checktable(L, ud);
ssize_t len = luaA_rawlen(L, ud);
uint16_t mod = XCB_NONE;
for(int i = 1; i <= len; i++)
{
lua_rawgeti(L, ud, i);
const char *key = luaL_checkstring(L, -1);
mod |= xutil_key_mask_fromstr(key);
lua_pop(L, 1);
}
return mod;
}
static int
luaA_key_set_modifiers(lua_State *L, keyb_t *k)
{
k->modifiers = luaA_tomodifiers(L, -1);
luaA_object_emit_signal(L, -3, "property::modifiers", 0);
return 0;
}
LUA_OBJECT_EXPORT_PROPERTY(key, keyb_t, modifiers, luaA_pushmodifiers)
/* It's caller's responsibility to release the returned string. */
char *
key_get_keysym_name(xkb_keysym_t keysym)
{
const ssize_t bufsize = 64;
char *buf = p_new(char, bufsize);
ssize_t len;
if((len = xkb_keysym_get_name(keysym, buf, bufsize)) == -1)
{
p_delete(&buf);
return NULL;
}
if(len + 1 > bufsize)
{
p_realloc(&buf, len + 1);
if(xkb_keysym_get_name(keysym, buf, len + 1) != len)
{
p_delete(&buf);
return NULL;
}
}
return buf;
}
static int
luaA_key_get_key(lua_State *L, keyb_t *k)
{
if(k->keycode)
{
char buf[12];
int slen = snprintf(buf, sizeof(buf), "#%u", k->keycode);
lua_pushlstring(L, buf, slen);
}
else
{
char *name = key_get_keysym_name(k->keysym);
if(!name)
return 0;
lua_pushstring(L, name);
p_delete(&name);
}
return 1;
}
static int
luaA_key_get_keysym(lua_State *L, keyb_t *k)
{
char *name = key_get_keysym_name(k->keysym);
if(!name)
return 0;
lua_pushstring(L, name);
p_delete(&name);
return 1;
}
static int
luaA_key_set_key(lua_State *L, keyb_t *k)
{
size_t klen;
const char *key = luaL_checklstring(L, -1, &klen);
luaA_keystore(L, -3, key, klen);
return 0;
}
void
key_class_setup(lua_State *L)
{
static const struct luaL_Reg key_methods[] =
{
LUA_CLASS_METHODS(key)
{ "__call", luaA_key_new },
{ NULL, NULL }
};
static const struct luaL_Reg key_meta[] =
{
LUA_OBJECT_META(key)
LUA_CLASS_META
{ NULL, NULL },
};
luaA_class_setup(L, &key_class, "key", NULL,
(lua_class_allocator_t) key_new, NULL, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
key_methods, key_meta);
luaA_class_add_property(&key_class, "key",
(lua_class_propfunc_t) luaA_key_set_key,
(lua_class_propfunc_t) luaA_key_get_key,
(lua_class_propfunc_t) luaA_key_set_key);
luaA_class_add_property(&key_class, "keysym",
NULL,
(lua_class_propfunc_t) luaA_key_get_keysym,
NULL);
luaA_class_add_property(&key_class, "modifiers",
(lua_class_propfunc_t) luaA_key_set_modifiers,
(lua_class_propfunc_t) luaA_key_get_modifiers,
(lua_class_propfunc_t) luaA_key_set_modifiers);
}
/* @DOC_cobject_COMMON@ */
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|