diff --git a/kiwmi/input/keyboard.c b/kiwmi/input/keyboard.c index ce4b26a..83543eb 100644 --- a/kiwmi/input/keyboard.c +++ b/kiwmi/input/keyboard.c @@ -127,11 +127,6 @@ keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device) keyboard->server = server; keyboard->device = device; - struct xkb_rule_names rules = {0}; - struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - struct xkb_keymap *keymap = - xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); - keyboard->modifiers.notify = keyboard_modifiers_notify; wl_signal_add(&device->keyboard->events.modifiers, &keyboard->modifiers); @@ -141,6 +136,10 @@ keyboard_create(struct kiwmi_server *server, struct wlr_input_device *device) keyboard->device_destroy.notify = keyboard_destroy_notify; wl_signal_add(&device->events.destroy, &keyboard->device_destroy); + struct xkb_rule_names rules = {0}; + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_keymap *keymap = + xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); wlr_keyboard_set_keymap(device->keyboard, keymap); xkb_keymap_unref(keymap); xkb_context_unref(context); diff --git a/kiwmi/luak/kiwmi_keyboard.c b/kiwmi/luak/kiwmi_keyboard.c index a009e76..24435a7 100644 --- a/kiwmi/luak/kiwmi_keyboard.c +++ b/kiwmi/luak/kiwmi_keyboard.c @@ -12,11 +12,63 @@ #include #include #include +#include #include "input/keyboard.h" #include "luak/kiwmi_lua_callback.h" #include "luak/lua_compat.h" +static int +l_kiwmi_keyboard_keymap(lua_State *L) +{ + struct kiwmi_object *obj = + *(struct kiwmi_object **)luaL_checkudata(L, 1, "kiwmi_keyboard"); + luaL_checktype(L, 2, LUA_TTABLE); + + if (!obj->valid) { + return luaL_error(L, "kiwmi_keyboard no longer valid"); + } + + struct kiwmi_keyboard *keyboard = obj->object; + struct xkb_rule_names settings = {0}; + + lua_getfield(L, 2, "rules"); + if (lua_isstring(L, -1)) { + settings.rules = luaL_checkstring(L, -1); + } + + lua_getfield(L, 2, "model"); + if (lua_isstring(L, -1)) { + settings.model = luaL_checkstring(L, -1); + } + + lua_getfield(L, 2, "layout"); + if (lua_isstring(L, -1)) { + settings.layout = luaL_checkstring(L, -1); + } + + lua_getfield(L, 2, "variant"); + if (lua_isstring(L, -1)) { + settings.variant = luaL_checkstring(L, -1); + } + + lua_getfield(L, 2, "options"); + if (lua_isstring(L, -1)) { + settings.options = luaL_checkstring(L, -1); + } + + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + struct xkb_keymap *keymap = xkb_keymap_new_from_names( + context, &settings, XKB_KEYMAP_COMPILE_NO_FLAGS); + + wlr_keyboard_set_keymap(keyboard->device->keyboard, keymap); + + xkb_keymap_unref(keymap); + xkb_context_unref(context); + + return 0; +} + static int l_kiwmi_keyboard_modifiers(lua_State *L) { @@ -61,6 +113,7 @@ l_kiwmi_keyboard_modifiers(lua_State *L) } static const luaL_Reg kiwmi_keyboard_methods[] = { + {"keymap", l_kiwmi_keyboard_keymap}, {"modifiers", l_kiwmi_keyboard_modifiers}, {"on", luaK_callback_register_dispatch}, {NULL, NULL}, diff --git a/lua_docs.md b/lua_docs.md index a587deb..264d1a8 100644 --- a/lua_docs.md +++ b/lua_docs.md @@ -112,6 +112,14 @@ A handle to a keyboard. ### Methods +#### keyboard:keymap(keymap) + +The funtcion takes a table as parameter. +The possible table indexes are "rules, model, layout, variant, options". +All the table parameters are optional and set to the system default if not set. +For the values to set have a look at the xkbcommon library. + + #### keyboard:modifiers() Returns a table with the state of all modifiers.