2015-02-23 17:08:24 +01:00
|
|
|
/*
|
|
|
|
* xkb.c - keyboard layout control functions
|
|
|
|
*
|
|
|
|
* Copyright © 2015 Aleksey Fedotov <lexa@cfotr.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
* @module awesome
|
|
|
|
*/
|
|
|
|
|
2015-02-23 17:08:24 +01:00
|
|
|
#include "xkb.h"
|
|
|
|
#include "globalconf.h"
|
2016-01-23 17:03:29 +01:00
|
|
|
#include "xwindow.h"
|
|
|
|
#include "objects/client.h"
|
2015-02-23 17:08:24 +01:00
|
|
|
#include <xcb/xkb.h>
|
2015-05-14 12:12:06 +02:00
|
|
|
#include <xkbcommon/xkbcommon.h>
|
|
|
|
#include <xkbcommon/xkbcommon-x11.h>
|
2015-02-23 17:08:24 +01:00
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/**
|
|
|
|
* Switch keyboard layout.
|
|
|
|
*
|
|
|
|
* @function xkb_set_layout_group
|
|
|
|
* @tparam integer num keyboard layout number, integer from 0 to 3
|
2015-02-23 17:08:24 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_xkb_set_layout_group(lua_State *L)
|
|
|
|
{
|
|
|
|
unsigned group = luaL_checkinteger(L, 1);
|
|
|
|
xcb_xkb_latch_lock_state (globalconf.connection, XCB_XKB_ID_USE_CORE_KBD,
|
|
|
|
0, 0, true, group, 0, 0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/**
|
|
|
|
* Get current layout number.
|
|
|
|
*
|
|
|
|
* @function xkb_get_layout_group
|
|
|
|
* @treturn integer num Current layout number, integer from 0 to 3.
|
2015-02-23 17:08:24 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_xkb_get_layout_group(lua_State *L)
|
|
|
|
{
|
|
|
|
xcb_xkb_get_state_cookie_t state_c;
|
|
|
|
state_c = xcb_xkb_get_state_unchecked (globalconf.connection,
|
|
|
|
XCB_XKB_ID_USE_CORE_KBD);
|
|
|
|
xcb_xkb_get_state_reply_t* state_r;
|
|
|
|
state_r = xcb_xkb_get_state_reply (globalconf.connection,
|
|
|
|
state_c, NULL);
|
|
|
|
if (!state_r)
|
|
|
|
{
|
|
|
|
free(state_r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
lua_pushinteger(L, state_r->group);
|
|
|
|
free(state_r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/**
|
|
|
|
* Get layout short names.
|
|
|
|
*
|
|
|
|
* @function xkb_get_group_names
|
|
|
|
* @treturn string A string describing the current layout settings,
|
|
|
|
* e.g.: 'pc+us+de:2+inet(evdev)+group(alt_shift_toggle)+ctrl(nocaps)'
|
2015-02-23 17:08:24 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_xkb_get_group_names(lua_State *L)
|
|
|
|
{
|
|
|
|
xcb_xkb_get_names_cookie_t name_c;
|
|
|
|
name_c = xcb_xkb_get_names_unchecked (globalconf.connection,
|
|
|
|
XCB_XKB_ID_USE_CORE_KBD,
|
|
|
|
XCB_XKB_NAME_DETAIL_SYMBOLS);
|
|
|
|
xcb_xkb_get_names_reply_t* name_r;
|
|
|
|
name_r = xcb_xkb_get_names_reply (globalconf.connection, name_c, NULL);
|
|
|
|
|
|
|
|
if (!name_r)
|
|
|
|
{
|
|
|
|
luaA_warn(L, "Failed to get xkb symbols name");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_xkb_get_names_value_list_t name_list;
|
|
|
|
void *buffer = xcb_xkb_get_names_value_list(name_r);
|
|
|
|
xcb_xkb_get_names_value_list_unpack (
|
|
|
|
buffer, name_r->nTypes, name_r->indicators,
|
|
|
|
name_r->virtualMods, name_r->groupNames, name_r->nKeys,
|
|
|
|
name_r->nKeyAliases, name_r->nRadioGroups, name_r->which,
|
|
|
|
&name_list);
|
|
|
|
|
|
|
|
xcb_get_atom_name_cookie_t atom_name_c;
|
|
|
|
atom_name_c = xcb_get_atom_name_unchecked(globalconf.connection, name_list.symbolsName);
|
|
|
|
xcb_get_atom_name_reply_t *atom_name_r;
|
|
|
|
atom_name_r = xcb_get_atom_name_reply(globalconf.connection, atom_name_c, NULL);
|
|
|
|
if (!atom_name_r) {
|
|
|
|
luaA_warn(L, "Failed to get atom symbols name");
|
|
|
|
free(name_r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *name = xcb_get_atom_name_name(atom_name_r);
|
|
|
|
size_t name_len = xcb_get_atom_name_name_length(atom_name_r);
|
|
|
|
lua_pushlstring(L, name, name_len);
|
|
|
|
|
|
|
|
free(atom_name_r);
|
|
|
|
free(name_r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-14 12:12:06 +02:00
|
|
|
/** Fill globalconf.xkb_state based on connection and context
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
xkb_fill_state(void)
|
|
|
|
{
|
|
|
|
xcb_connection_t *conn = globalconf.connection;
|
|
|
|
|
|
|
|
int32_t device_id = xkb_x11_get_core_keyboard_device_id(conn);
|
|
|
|
if (device_id == -1)
|
|
|
|
fatal("Failed while getting XKB device id");
|
2015-11-07 12:27:35 +01:00
|
|
|
|
2015-05-14 12:12:06 +02:00
|
|
|
struct xkb_keymap *xkb_keymap = xkb_x11_keymap_new_from_device(
|
|
|
|
globalconf.xkb_ctx,
|
|
|
|
conn,
|
|
|
|
device_id,
|
|
|
|
XKB_KEYMAP_COMPILE_NO_FLAGS);
|
|
|
|
|
|
|
|
|
|
|
|
if (!xkb_keymap)
|
|
|
|
fatal("Failed while getting XKB keymap from device");
|
2015-11-07 12:27:35 +01:00
|
|
|
|
2015-05-14 12:12:06 +02:00
|
|
|
globalconf.xkb_state = xkb_x11_state_new_from_device(xkb_keymap,
|
|
|
|
conn,
|
|
|
|
device_id);
|
|
|
|
if (!globalconf.xkb_state)
|
|
|
|
fatal("Failed while getting XKB state from device");
|
|
|
|
|
2015-05-21 21:21:34 +02:00
|
|
|
/* xkb_keymap is no longer referenced directly; decreasing refcount */
|
2015-05-14 12:12:06 +02:00
|
|
|
xkb_keymap_unref(xkb_keymap);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Loads xkb context, state and keymap to globalconf.
|
|
|
|
* These variables should be freed by xkb_free_keymap() afterwards
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
xkb_init_keymap(void)
|
|
|
|
{
|
|
|
|
globalconf.xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
|
|
|
if (!globalconf.xkb_ctx)
|
|
|
|
fatal("Failed while getting XKB context");
|
2015-11-07 12:27:35 +01:00
|
|
|
|
2015-05-21 21:21:34 +02:00
|
|
|
xkb_fill_state();
|
2015-05-14 12:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Frees xkb context, state and keymap from globalconf.
|
|
|
|
* This should be used when these variables will not be used anymore
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
xkb_free_keymap(void)
|
|
|
|
{
|
|
|
|
xkb_state_unref(globalconf.xkb_state);
|
|
|
|
xkb_context_unref(globalconf.xkb_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Rereads the state of keyboard from X.
|
2015-05-21 21:21:34 +02:00
|
|
|
* This call should be used after receiving NewKeyboardNotify or MapNotify,
|
|
|
|
* as written in http://xkbcommon.org/doc/current/group__x11.html
|
2015-05-14 12:12:06 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
xkb_reload_keymap(void)
|
|
|
|
{
|
|
|
|
xkb_state_unref(globalconf.xkb_state);
|
2015-05-21 21:21:34 +02:00
|
|
|
xkb_fill_state();
|
2016-01-23 17:03:29 +01:00
|
|
|
|
|
|
|
/* Free and then allocate the key symbols */
|
|
|
|
xcb_key_symbols_free(globalconf.keysyms);
|
|
|
|
globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
|
|
|
|
|
|
|
|
/* Regrab key bindings on the root window */
|
|
|
|
xcb_screen_t *s = globalconf.screen;
|
|
|
|
xwindow_grabkeys(s->root, &globalconf.keys);
|
|
|
|
|
|
|
|
/* Regrab key bindings on clients */
|
|
|
|
foreach(_c, globalconf.clients)
|
|
|
|
{
|
|
|
|
client_t *c = *_c;
|
|
|
|
xwindow_grabkeys(c->window, &c->keys);
|
|
|
|
}
|
2015-05-14 12:12:06 +02:00
|
|
|
}
|
|
|
|
|
2015-02-23 17:08:24 +01:00
|
|
|
/** The xkb notify event handler.
|
|
|
|
* \param event The event.
|
|
|
|
*/
|
2015-05-14 12:12:06 +02:00
|
|
|
void
|
|
|
|
event_handle_xkb_notify(xcb_generic_event_t* event)
|
2015-02-23 17:08:24 +01:00
|
|
|
{
|
|
|
|
lua_State *L = globalconf_get_lua_State();
|
2015-05-14 12:12:06 +02:00
|
|
|
|
2015-02-23 17:08:24 +01:00
|
|
|
/* The pad0 field of xcb_generic_event_t contains the event sub-type,
|
|
|
|
* unfortunately xkb doesn't provide a usable struct for getting this in a
|
|
|
|
* nicer way*/
|
|
|
|
switch (event->pad0)
|
|
|
|
{
|
|
|
|
case XCB_XKB_NEW_KEYBOARD_NOTIFY:
|
|
|
|
{
|
|
|
|
xcb_xkb_new_keyboard_notify_event_t *new_keyboard_event = (void*)event;
|
2015-05-21 21:21:34 +02:00
|
|
|
|
|
|
|
xkb_reload_keymap();
|
|
|
|
|
2015-02-23 17:08:24 +01:00
|
|
|
if (new_keyboard_event->changed & XCB_XKB_NKN_DETAIL_KEYCODES)
|
|
|
|
{
|
|
|
|
signal_object_emit(L, &global_signals, "xkb::map_changed", 0);
|
|
|
|
}
|
2015-05-21 21:21:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XCB_XKB_MAP_NOTIFY:
|
|
|
|
{
|
|
|
|
xkb_reload_keymap();
|
2015-02-23 17:08:24 +01:00
|
|
|
signal_object_emit(L, &global_signals, "xkb::map_changed", 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case XCB_XKB_STATE_NOTIFY:
|
|
|
|
{
|
|
|
|
xcb_xkb_state_notify_event_t *state_notify_event = (void*)event;
|
2015-05-14 12:12:06 +02:00
|
|
|
|
2015-11-07 12:27:35 +01:00
|
|
|
xkb_state_update_mask(globalconf.xkb_state,
|
2015-05-21 21:21:34 +02:00
|
|
|
state_notify_event->baseMods,
|
|
|
|
state_notify_event->latchedMods,
|
|
|
|
state_notify_event->lockedMods,
|
|
|
|
state_notify_event->baseGroup,
|
|
|
|
state_notify_event->latchedGroup,
|
|
|
|
state_notify_event->lockedGroup);
|
|
|
|
|
2015-02-23 17:08:24 +01:00
|
|
|
if (state_notify_event->changed & XCB_XKB_STATE_PART_GROUP_STATE)
|
|
|
|
{
|
2016-02-06 13:46:46 +01:00
|
|
|
lua_pushinteger(L, state_notify_event->group);
|
2015-02-23 17:08:24 +01:00
|
|
|
signal_object_emit(L, &global_signals, "xkb::group_changed", 1);
|
|
|
|
}
|
2015-05-14 12:12:06 +02:00
|
|
|
|
2015-02-23 17:08:24 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize XKB support
|
2015-05-14 12:12:06 +02:00
|
|
|
* This call allocates resources, that should be freed by calling xkb_free()
|
2015-02-23 17:08:24 +01:00
|
|
|
*/
|
2015-05-14 12:12:06 +02:00
|
|
|
void
|
|
|
|
xkb_init(void)
|
2015-02-23 17:08:24 +01:00
|
|
|
{
|
2015-05-14 12:12:06 +02:00
|
|
|
|
2015-05-27 02:09:34 +02:00
|
|
|
int success_xkb = xkb_x11_setup_xkb_extension(globalconf.connection,
|
|
|
|
XKB_X11_MIN_MAJOR_XKB_VERSION,
|
|
|
|
XKB_X11_MIN_MINOR_XKB_VERSION,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
2015-02-23 17:08:24 +01:00
|
|
|
|
2015-05-27 02:09:34 +02:00
|
|
|
|
|
|
|
if (!success_xkb) {
|
|
|
|
fatal("XKB not found or not supported");
|
2015-02-23 17:08:24 +01:00
|
|
|
}
|
2015-05-27 02:08:07 +02:00
|
|
|
|
|
|
|
uint16_t map = XCB_XKB_EVENT_TYPE_STATE_NOTIFY | XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY;
|
2015-05-27 01:59:34 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// These maps are provided to allow key remapping,
|
|
|
|
// that could be used in awesome
|
|
|
|
//
|
|
|
|
uint16_t map_parts = XCB_XKB_MAP_PART_KEY_TYPES |
|
|
|
|
XCB_XKB_MAP_PART_KEY_SYMS |
|
|
|
|
XCB_XKB_MAP_PART_MODIFIER_MAP |
|
|
|
|
XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
|
|
|
|
XCB_XKB_MAP_PART_KEY_ACTIONS |
|
|
|
|
XCB_XKB_MAP_PART_KEY_BEHAVIORS |
|
2015-11-07 12:27:35 +01:00
|
|
|
XCB_XKB_MAP_PART_VIRTUAL_MODS |
|
2015-05-27 01:59:34 +02:00
|
|
|
XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP;
|
|
|
|
|
Enable detectable autorepeat (#550)
Imagine the following snippet is run:
keygrabber.run(function(mod, key, event)
gears.debug.dump{mod, key, event}
end)
The above starts a keygrabber and prints the events that are received.
Currently, when a key is pressed and held down, this prints a series of press
and release, because that's what the X11 server sends us.
This commit enables detectable autorepeat. This means that the X11 server only
sends us a series of press events for autorepeat and a single release when the
key really is released.
Testing this is a bit hard, because detectable autorepeat does not seem to work
with Xephyr. Instead, a "real" Xorg instance is needed.
We do not check the response to the PerClientFlags request, because it doesn't
really tell us anything useful. If the server does not support detectable
autorepeat, we could print a warning, but so what? As I just said, this does not
work in Xephyr and yet Xephyr announced to support this.
Signed-off-by: Uli Schlachter <psychon@znc.in>
2015-11-07 12:45:04 +01:00
|
|
|
/* Enable detectable auto-repeat, but ignore failures */
|
|
|
|
xcb_discard_reply(globalconf.connection,
|
|
|
|
xcb_xkb_per_client_flags(globalconf.connection,
|
|
|
|
XCB_XKB_ID_USE_CORE_KBD,
|
|
|
|
XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
|
|
|
|
XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
.sequence);
|
|
|
|
|
2015-11-07 12:24:21 +01:00
|
|
|
xcb_xkb_select_events(globalconf.connection,
|
|
|
|
XCB_XKB_ID_USE_CORE_KBD,
|
|
|
|
map,
|
|
|
|
0,
|
|
|
|
map,
|
|
|
|
map_parts,
|
|
|
|
map_parts,
|
|
|
|
0);
|
2015-02-23 17:08:24 +01:00
|
|
|
|
2015-05-14 12:12:06 +02:00
|
|
|
/* load keymap to use when resolving keypresses */
|
|
|
|
xkb_init_keymap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Frees resources allocated by xkb_init()
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
xkb_free(void)
|
|
|
|
{
|
2015-05-27 02:08:07 +02:00
|
|
|
// unsubscribe from all events
|
2015-11-07 12:24:21 +01:00
|
|
|
xcb_xkb_select_events(globalconf.connection,
|
2015-05-27 02:08:07 +02:00
|
|
|
XCB_XKB_ID_USE_CORE_KBD,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0);
|
2015-05-14 12:12:06 +02:00
|
|
|
xkb_free_keymap();
|
2015-02-23 17:08:24 +01:00
|
|
|
}
|
2015-12-12 17:37:35 +01:00
|
|
|
|
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|