2008-12-18 11:33:19 +01:00
|
|
|
/*
|
|
|
|
* key.c - Key bindings configuration management
|
|
|
|
*
|
2009-04-29 10:45:16 +02:00
|
|
|
* Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
|
2008-12-18 11:33:19 +01:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/** 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
|
|
|
|
* @release @AWESOME_VERSION@
|
|
|
|
* @classmod key
|
|
|
|
*/
|
|
|
|
|
2014-03-30 20:07:48 +02:00
|
|
|
#include "objects/key.h"
|
|
|
|
#include "common/xutil.h"
|
2015-05-14 12:12:06 +02:00
|
|
|
#include "xkb.h"
|
2014-03-30 20:07:48 +02:00
|
|
|
|
2009-04-30 00:11:13 +02:00
|
|
|
/* XStringToKeysym() and XKeysymToString */
|
2008-12-18 11:33:19 +01:00
|
|
|
#include <X11/Xlib.h>
|
2015-05-21 21:21:34 +02:00
|
|
|
#include <xkbcommon/xkbcommon.h>
|
2015-10-17 22:20:52 +02:00
|
|
|
#include <glib.h>
|
2008-12-18 11:33:19 +01:00
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/** 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Get the number of instances.
|
|
|
|
*
|
|
|
|
* @return The number of key objects alive.
|
|
|
|
* @function instances
|
|
|
|
*/
|
|
|
|
|
2008-12-18 11:33:19 +01:00
|
|
|
static void
|
2009-06-24 17:44:06 +02:00
|
|
|
luaA_keystore(lua_State *L, int ud, const char *str, ssize_t len)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2015-10-17 22:20:52 +02:00
|
|
|
if(len <= 0 || !str)
|
|
|
|
return;
|
|
|
|
|
2009-08-17 15:52:41 +02:00
|
|
|
keyb_t *key = luaA_checkudata(L, ud, &key_class);
|
2015-10-17 22:20:52 +02:00
|
|
|
|
|
|
|
if(len == 1)
|
|
|
|
{
|
|
|
|
key->keycode = 0;
|
|
|
|
key->keysym = str[0];
|
|
|
|
}
|
|
|
|
else if(str[0] == '#')
|
|
|
|
{
|
|
|
|
key->keycode = atoi(str + 1);
|
|
|
|
key->keysym = 0;
|
|
|
|
}
|
|
|
|
else
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2015-10-17 22:20:52 +02:00
|
|
|
key->keycode = 0;
|
|
|
|
|
|
|
|
if((key->keysym = XStringToKeysym(str)) == NoSymbol )
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2015-10-17 22:20:52 +02:00
|
|
|
glong length;
|
|
|
|
gunichar unicode;
|
|
|
|
|
|
|
|
if(!g_utf8_validate(str, -1, NULL))
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2015-10-17 22:20:52 +02:00
|
|
|
warn("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)
|
|
|
|
{
|
|
|
|
warn("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);
|
|
|
|
warn("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)
|
|
|
|
{
|
|
|
|
warn("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
|
|
|
|
{
|
|
|
|
warn("failed to convert \"%s\" into keysym (unicode out of range): \"%u\"", str, unicode);
|
|
|
|
return;
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
2009-06-24 17:44:06 +02:00
|
|
|
}
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
2015-10-17 22:20:52 +02:00
|
|
|
|
|
|
|
luaA_object_emit_signal(L, ud, "property::key", 0);
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
|
|
|
|
2009-06-24 17:44:06 +02:00
|
|
|
/** Create a new key object.
|
2008-12-18 11:33:19 +01:00
|
|
|
* \param L The Lua VM state.
|
2009-06-24 17:44:06 +02:00
|
|
|
* \return The number of elements pushed on stack.
|
2008-12-18 11:33:19 +01:00
|
|
|
*/
|
2009-04-11 11:59:10 +02:00
|
|
|
static int
|
2008-12-18 11:33:19 +01:00
|
|
|
luaA_key_new(lua_State *L)
|
|
|
|
{
|
2009-06-24 17:44:06 +02:00
|
|
|
return luaA_class_new(L, &key_class);
|
2008-12-18 11:33:19 +01:00
|
|
|
}
|
|
|
|
|
2009-08-14 16:46:35 +02:00
|
|
|
/** 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))
|
2009-08-17 15:52:41 +02:00
|
|
|
if(luaA_toudata(L, -1, &key_class))
|
2009-08-14 16:46:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-04-26 19:41:53 +02:00
|
|
|
/** Push a modifier set to a Lua table.
|
|
|
|
* \param L The Lua VM state.
|
2009-07-27 06:20:00 +02:00
|
|
|
* \param modifiers The modifier.
|
2009-04-26 19:41:53 +02:00
|
|
|
* \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;
|
|
|
|
}
|
|
|
|
|
2009-06-17 17:05:47 +02:00
|
|
|
/** Take a modifier table from the stack and return modifiers mask.
|
2009-04-27 14:12:24 +02:00
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \param ud The index of the table.
|
2009-06-17 17:05:47 +02:00
|
|
|
* \return The mask value.
|
2009-04-27 14:12:24 +02:00
|
|
|
*/
|
2009-06-17 17:05:47 +02:00
|
|
|
uint16_t
|
|
|
|
luaA_tomodifiers(lua_State *L, int ud)
|
2009-04-27 14:12:24 +02:00
|
|
|
{
|
2009-04-27 14:13:54 +02:00
|
|
|
luaA_checktable(L, ud);
|
2012-06-07 09:27:10 +02:00
|
|
|
ssize_t len = luaA_rawlen(L, ud);
|
2009-06-17 17:05:47 +02:00
|
|
|
uint16_t mod = XCB_NONE;
|
2009-04-27 14:12:24 +02:00
|
|
|
for(int i = 1; i <= len; i++)
|
|
|
|
{
|
|
|
|
lua_rawgeti(L, ud, i);
|
2010-09-02 19:12:21 +02:00
|
|
|
const char *key = luaL_checkstring(L, -1);
|
|
|
|
mod |= xutil_key_mask_fromstr(key);
|
2009-04-27 14:12:24 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2009-06-17 17:05:47 +02:00
|
|
|
return mod;
|
2009-04-27 14:12:24 +02:00
|
|
|
}
|
|
|
|
|
2009-04-25 16:00:08 +02:00
|
|
|
static int
|
2009-06-24 17:44:06 +02:00
|
|
|
luaA_key_set_modifiers(lua_State *L, keyb_t *k)
|
2009-04-25 16:00:08 +02:00
|
|
|
{
|
2009-06-24 17:44:06 +02:00
|
|
|
k->modifiers = luaA_tomodifiers(L, -1);
|
|
|
|
luaA_object_emit_signal(L, -3, "property::modifiers", 0);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-25 16:00:08 +02:00
|
|
|
|
2009-06-24 17:44:06 +02:00
|
|
|
LUA_OBJECT_EXPORT_PROPERTY(key, keyb_t, modifiers, luaA_pushmodifiers)
|
2009-04-25 16:00:08 +02:00
|
|
|
|
2015-10-17 22:20:52 +02:00
|
|
|
/* It's caller's responsibility to release the returned string. */
|
|
|
|
static char *
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-24 17:44:06 +02:00
|
|
|
static int
|
|
|
|
luaA_key_get_key(lua_State *L, keyb_t *k)
|
|
|
|
{
|
|
|
|
if(k->keycode)
|
2009-04-25 16:00:08 +02:00
|
|
|
{
|
2009-06-24 17:44:06 +02:00
|
|
|
char buf[12];
|
|
|
|
int slen = snprintf(buf, sizeof(buf), "#%u", k->keycode);
|
|
|
|
lua_pushlstring(L, buf, slen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-17 22:20:52 +02:00
|
|
|
char *name = get_keysym_name(k->keysym);
|
|
|
|
if(!name)
|
2009-04-30 00:11:13 +02:00
|
|
|
return 0;
|
2015-10-17 22:20:52 +02:00
|
|
|
lua_pushstring(L, name);
|
|
|
|
p_delete(&name);
|
2009-04-25 16:00:08 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-27 14:31:57 +02:00
|
|
|
static int
|
2009-06-24 17:44:06 +02:00
|
|
|
luaA_key_get_keysym(lua_State *L, keyb_t *k)
|
2009-04-27 14:31:57 +02:00
|
|
|
{
|
2015-10-17 22:20:52 +02:00
|
|
|
char *name = get_keysym_name(k->keysym);
|
|
|
|
if(!name)
|
|
|
|
return 0;
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
p_delete(&name);
|
2009-06-24 17:44:06 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2009-04-27 14:31:57 +02:00
|
|
|
|
2009-06-24 17:44:06 +02:00
|
|
|
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);
|
2009-04-27 14:31:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-24 17:44:06 +02:00
|
|
|
void
|
|
|
|
key_class_setup(lua_State *L)
|
2008-12-18 11:33:19 +01:00
|
|
|
{
|
2012-06-06 23:07:07 +02:00
|
|
|
static const struct luaL_Reg key_methods[] =
|
2009-06-24 17:44:06 +02:00
|
|
|
{
|
|
|
|
LUA_CLASS_METHODS(key)
|
|
|
|
{ "__call", luaA_key_new },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
2008-12-18 11:33:19 +01:00
|
|
|
|
2012-06-06 23:07:07 +02:00
|
|
|
static const struct luaL_Reg key_meta[] =
|
2009-06-24 17:44:06 +02:00
|
|
|
{
|
|
|
|
LUA_OBJECT_META(key)
|
|
|
|
LUA_CLASS_META
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2009-09-30 14:21:25 +02:00
|
|
|
luaA_class_setup(L, &key_class, "key", NULL,
|
2009-10-02 15:48:32 +02:00
|
|
|
(lua_class_allocator_t) key_new, NULL, NULL,
|
2009-08-20 17:37:46 +02:00
|
|
|
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
|
2009-06-24 17:44:06 +02:00
|
|
|
key_methods, key_meta);
|
2010-09-01 15:41:41 +02:00
|
|
|
luaA_class_add_property(&key_class, "key",
|
2009-06-24 17:44:06 +02:00
|
|
|
(lua_class_propfunc_t) luaA_key_set_key,
|
|
|
|
(lua_class_propfunc_t) luaA_key_get_key,
|
|
|
|
(lua_class_propfunc_t) luaA_key_set_key);
|
2010-09-01 15:41:41 +02:00
|
|
|
luaA_class_add_property(&key_class, "keysym",
|
2009-06-24 17:44:06 +02:00
|
|
|
NULL,
|
|
|
|
(lua_class_propfunc_t) luaA_key_get_keysym,
|
|
|
|
NULL);
|
2010-09-01 15:41:41 +02:00
|
|
|
luaA_class_add_property(&key_class, "modifiers",
|
2009-06-24 17:44:06 +02:00
|
|
|
(lua_class_propfunc_t) luaA_key_set_modifiers,
|
|
|
|
(lua_class_propfunc_t) luaA_key_get_modifiers,
|
|
|
|
(lua_class_propfunc_t) luaA_key_set_modifiers);
|
2010-08-25 23:00:36 +02:00
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/**
|
|
|
|
* @signal .press
|
|
|
|
*/
|
2010-08-25 23:00:36 +02:00
|
|
|
signal_add(&key_class.signals, "press");
|
2015-02-27 00:24:23 +01:00
|
|
|
/**
|
|
|
|
* @signal property::key
|
|
|
|
*/
|
2010-08-26 17:46:17 +02:00
|
|
|
signal_add(&key_class.signals, "property::key");
|
2015-02-27 00:24:23 +01:00
|
|
|
/**
|
|
|
|
* @signal property::modifiers
|
|
|
|
*/
|
2010-08-26 17:46:17 +02:00
|
|
|
signal_add(&key_class.signals, "property::modifiers");
|
2015-02-27 00:24:23 +01:00
|
|
|
/**
|
|
|
|
* @signal .release
|
|
|
|
*/
|
2010-08-25 23:00:36 +02:00
|
|
|
signal_add(&key_class.signals, "release");
|
2009-06-24 17:44:06 +02:00
|
|
|
}
|
2009-08-21 16:44:30 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|