2012-04-07 17:20:39 -06:00
|
|
|
/*
|
|
|
|
* Copyright © 2012 Ran Benita <ran234@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-27 04:03:20 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
2020-06-22 22:01:48 -06:00
|
|
|
#include <assert.h>
|
2012-04-07 17:20:39 -06:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <fnmatch.h>
|
2020-07-12 20:13:23 -06:00
|
|
|
#include <getopt.h>
|
2012-04-07 17:20:39 -06:00
|
|
|
#include <limits.h>
|
|
|
|
#include <locale.h>
|
2021-03-28 03:55:08 -06:00
|
|
|
#include <poll.h>
|
2012-04-07 17:20:39 -06:00
|
|
|
#include <signal.h>
|
2020-06-22 22:01:48 -06:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-04-07 17:20:39 -06:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <linux/input.h>
|
|
|
|
|
2020-06-22 22:01:48 -06:00
|
|
|
#include "xkbcommon/xkbcommon.h"
|
|
|
|
|
|
|
|
#include "tools-common.h"
|
2012-04-07 17:20:39 -06:00
|
|
|
|
|
|
|
struct keyboard {
|
|
|
|
char *path;
|
|
|
|
int fd;
|
|
|
|
struct xkb_state *state;
|
2014-01-29 04:46:42 -07:00
|
|
|
struct xkb_compose_state *compose_state;
|
2012-04-07 17:20:39 -06:00
|
|
|
struct keyboard *next;
|
|
|
|
};
|
|
|
|
|
2024-02-13 17:23:15 -07:00
|
|
|
static bool verbose = false;
|
2012-09-27 12:44:16 -06:00
|
|
|
static bool terminate;
|
|
|
|
static int evdev_offset = 8;
|
2012-10-22 13:16:35 -06:00
|
|
|
static bool report_state_changes;
|
2014-01-29 04:46:42 -07:00
|
|
|
static bool with_compose;
|
2016-02-27 13:31:16 -07:00
|
|
|
static enum xkb_consumed_mode consumed_mode = XKB_CONSUMED_MODE_XKB;
|
2023-09-18 04:17:11 -06:00
|
|
|
|
|
|
|
#ifdef ENABLE_PRIVATE_APIS
|
|
|
|
#define DEFAULT_PRINT_FIELDS (PRINT_ALL_FIELDS & ~PRINT_MODMAPS)
|
|
|
|
#else
|
2023-09-18 04:15:06 -06:00
|
|
|
#define DEFAULT_PRINT_FIELDS PRINT_ALL_FIELDS
|
2023-09-18 04:17:11 -06:00
|
|
|
#endif
|
2023-09-18 04:15:06 -06:00
|
|
|
print_state_fields_mask_t print_fields = DEFAULT_PRINT_FIELDS;
|
2012-04-07 17:20:39 -06:00
|
|
|
|
2023-06-16 01:54:09 -06:00
|
|
|
#define DEFAULT_INCLUDE_PATH_PLACEHOLDER "__defaults__"
|
2012-04-07 17:20:39 -06:00
|
|
|
#define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
|
|
|
|
|
|
|
|
static bool
|
|
|
|
evdev_bit_is_set(const unsigned long *array, int bit)
|
|
|
|
{
|
2014-06-01 06:24:10 -06:00
|
|
|
return array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT));
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Some heuristics to see if the device is a keyboard. */
|
|
|
|
static bool
|
|
|
|
is_keyboard(int fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
|
|
|
|
unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
|
|
|
|
if (errno)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!evdev_bit_is_set(evbits, EV_KEY))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
|
|
|
|
if (errno)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
|
|
|
|
if (evdev_bit_is_set(keybits, i))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-02-17 13:50:12 -07:00
|
|
|
keyboard_new(struct dirent *ent, struct xkb_keymap *keymap,
|
2014-01-29 04:46:42 -07:00
|
|
|
struct xkb_compose_table *compose_table, struct keyboard **out)
|
2012-04-07 17:20:39 -06:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *path;
|
|
|
|
int fd;
|
|
|
|
struct xkb_state *state;
|
2014-01-29 04:46:42 -07:00
|
|
|
struct xkb_compose_state *compose_state = NULL;
|
2012-04-07 17:20:39 -06:00
|
|
|
struct keyboard *kbd;
|
|
|
|
|
|
|
|
ret = asprintf(&path, "/dev/input/%s", ent->d_name);
|
|
|
|
if (ret < 0)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
goto err_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_keyboard(fd)) {
|
|
|
|
/* Dummy "skip this device" value. */
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
goto err_fd;
|
|
|
|
}
|
|
|
|
|
2013-02-17 13:50:12 -07:00
|
|
|
state = xkb_state_new(keymap);
|
2012-04-07 17:20:39 -06:00
|
|
|
if (!state) {
|
|
|
|
fprintf(stderr, "Couldn't create xkb state for %s\n", path);
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto err_fd;
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:46:42 -07:00
|
|
|
if (with_compose) {
|
|
|
|
compose_state = xkb_compose_state_new(compose_table,
|
|
|
|
XKB_COMPOSE_STATE_NO_FLAGS);
|
|
|
|
if (!compose_state) {
|
|
|
|
fprintf(stderr, "Couldn't create compose state for %s\n", path);
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto err_state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-07 17:20:39 -06:00
|
|
|
kbd = calloc(1, sizeof(*kbd));
|
|
|
|
if (!kbd) {
|
|
|
|
ret = -ENOMEM;
|
2014-01-29 04:46:42 -07:00
|
|
|
goto err_compose_state;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
kbd->path = path;
|
|
|
|
kbd->fd = fd;
|
|
|
|
kbd->state = state;
|
2014-01-29 04:46:42 -07:00
|
|
|
kbd->compose_state = compose_state;
|
2012-04-07 17:20:39 -06:00
|
|
|
*out = kbd;
|
|
|
|
return 0;
|
|
|
|
|
2014-01-29 04:46:42 -07:00
|
|
|
err_compose_state:
|
|
|
|
xkb_compose_state_unref(compose_state);
|
2012-04-07 17:20:39 -06:00
|
|
|
err_state:
|
|
|
|
xkb_state_unref(state);
|
|
|
|
err_fd:
|
|
|
|
close(fd);
|
|
|
|
err_path:
|
|
|
|
free(path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keyboard_free(struct keyboard *kbd)
|
|
|
|
{
|
|
|
|
if (!kbd)
|
|
|
|
return;
|
|
|
|
if (kbd->fd >= 0)
|
|
|
|
close(kbd->fd);
|
|
|
|
free(kbd->path);
|
|
|
|
xkb_state_unref(kbd->state);
|
2014-01-29 04:46:42 -07:00
|
|
|
xkb_compose_state_unref(kbd->compose_state);
|
2012-04-07 17:20:39 -06:00
|
|
|
free(kbd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
filter_device_name(const struct dirent *ent)
|
|
|
|
{
|
|
|
|
return !fnmatch("event*", ent->d_name, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct keyboard *
|
2014-01-29 04:46:42 -07:00
|
|
|
get_keyboards(struct xkb_keymap *keymap,
|
|
|
|
struct xkb_compose_table *compose_table)
|
2012-04-07 17:20:39 -06:00
|
|
|
{
|
|
|
|
int ret, i, nents;
|
|
|
|
struct dirent **ents;
|
|
|
|
struct keyboard *kbds = NULL, *kbd = NULL;
|
|
|
|
|
|
|
|
nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
|
|
|
|
if (nents < 0) {
|
|
|
|
fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nents; i++) {
|
2014-01-29 04:46:42 -07:00
|
|
|
ret = keyboard_new(ents[i], keymap, compose_table, &kbd);
|
2012-04-07 17:20:39 -06:00
|
|
|
if (ret) {
|
|
|
|
if (ret == -EACCES) {
|
|
|
|
fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
|
|
|
|
"You probably need root to run this.\n",
|
|
|
|
ents[i]->d_name, strerror(-ret));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret != -ENOTSUP) {
|
|
|
|
fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
|
|
|
|
ents[i]->d_name, strerror(-ret));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:18:54 -06:00
|
|
|
assert(kbd != NULL);
|
2012-04-07 17:20:39 -06:00
|
|
|
kbd->next = kbds;
|
|
|
|
kbds = kbd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!kbds) {
|
|
|
|
fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
for (i = 0; i < nents; i++)
|
|
|
|
free(ents[i]);
|
|
|
|
free(ents);
|
|
|
|
return kbds;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_keyboards(struct keyboard *kbds)
|
|
|
|
{
|
|
|
|
struct keyboard *next;
|
|
|
|
|
|
|
|
while (kbds) {
|
|
|
|
next = kbds->next;
|
|
|
|
keyboard_free(kbds);
|
|
|
|
kbds = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The meaning of the input_event 'value' field. */
|
|
|
|
enum {
|
|
|
|
KEY_STATE_RELEASE = 0,
|
|
|
|
KEY_STATE_PRESS = 1,
|
|
|
|
KEY_STATE_REPEAT = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
|
|
|
|
{
|
|
|
|
xkb_keycode_t keycode;
|
|
|
|
struct xkb_keymap *keymap;
|
2012-10-22 13:16:35 -06:00
|
|
|
enum xkb_state_component changed;
|
2014-01-29 04:46:42 -07:00
|
|
|
enum xkb_compose_status status;
|
2012-04-07 17:20:39 -06:00
|
|
|
|
|
|
|
if (type != EV_KEY)
|
|
|
|
return;
|
|
|
|
|
2012-09-27 12:44:16 -06:00
|
|
|
keycode = evdev_offset + code;
|
2012-09-21 05:44:17 -06:00
|
|
|
keymap = xkb_state_get_keymap(kbd->state);
|
2012-04-07 17:20:39 -06:00
|
|
|
|
2012-09-21 05:44:17 -06:00
|
|
|
if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
|
2012-04-07 17:20:39 -06:00
|
|
|
return;
|
|
|
|
|
2014-01-29 04:46:42 -07:00
|
|
|
if (with_compose && value != KEY_STATE_RELEASE) {
|
|
|
|
xkb_keysym_t keysym = xkb_state_key_get_one_sym(kbd->state, keycode);
|
|
|
|
xkb_compose_state_feed(kbd->compose_state, keysym);
|
|
|
|
}
|
|
|
|
|
2023-09-18 04:15:06 -06:00
|
|
|
if (value != KEY_STATE_RELEASE) {
|
|
|
|
tools_print_keycode_state(
|
2023-11-01 06:06:38 -06:00
|
|
|
NULL, kbd->state, kbd->compose_state, keycode,
|
2023-09-18 04:15:06 -06:00
|
|
|
consumed_mode, print_fields
|
|
|
|
);
|
|
|
|
}
|
2014-01-29 04:46:42 -07:00
|
|
|
|
|
|
|
if (with_compose) {
|
|
|
|
status = xkb_compose_state_get_status(kbd->compose_state);
|
|
|
|
if (status == XKB_COMPOSE_CANCELLED || status == XKB_COMPOSE_COMPOSED)
|
|
|
|
xkb_compose_state_reset(kbd->compose_state);
|
|
|
|
}
|
2012-04-07 17:20:39 -06:00
|
|
|
|
|
|
|
if (value == KEY_STATE_RELEASE)
|
2012-10-22 13:16:35 -06:00
|
|
|
changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
|
2012-04-07 17:20:39 -06:00
|
|
|
else
|
2012-10-22 13:16:35 -06:00
|
|
|
changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
|
|
|
|
|
|
|
|
if (report_state_changes)
|
2020-06-22 22:01:48 -06:00
|
|
|
tools_print_state_changes(changed);
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
read_keyboard(struct keyboard *kbd)
|
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
struct input_event evs[16];
|
|
|
|
|
|
|
|
/* No fancy error checking here. */
|
|
|
|
while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
|
2014-01-11 07:40:42 -07:00
|
|
|
const size_t nevs = len / sizeof(struct input_event);
|
|
|
|
for (size_t i = 0; i < nevs; i++)
|
2012-04-07 17:20:39 -06:00
|
|
|
process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0 && errno != EWOULDBLOCK) {
|
2014-01-11 07:40:42 -07:00
|
|
|
fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
|
2020-06-23 00:06:10 -06:00
|
|
|
return 1;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
loop(struct keyboard *kbds)
|
|
|
|
{
|
2021-03-28 07:10:52 -06:00
|
|
|
int ret = -1;
|
2012-04-07 17:20:39 -06:00
|
|
|
struct keyboard *kbd;
|
2021-03-28 03:55:08 -06:00
|
|
|
nfds_t nfds, i;
|
|
|
|
struct pollfd *fds = NULL;
|
2012-04-07 17:20:39 -06:00
|
|
|
|
2021-03-28 03:55:08 -06:00
|
|
|
for (kbd = kbds, nfds = 0; kbd; kbd = kbd->next, nfds++) {}
|
|
|
|
fds = calloc(nfds, sizeof(*fds));
|
|
|
|
if (fds == NULL) {
|
|
|
|
fprintf(stderr, "Out of memory");
|
2020-06-23 00:06:10 -06:00
|
|
|
goto out;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
2021-03-28 03:55:08 -06:00
|
|
|
for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
|
|
|
|
fds[i].fd = kbd->fd;
|
|
|
|
fds[i].events = POLLIN;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!terminate) {
|
2021-03-28 03:55:08 -06:00
|
|
|
ret = poll(fds, nfds, -1);
|
2012-04-07 17:20:39 -06:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
fprintf(stderr, "Couldn't poll for events: %s\n",
|
|
|
|
strerror(errno));
|
2020-06-23 00:06:10 -06:00
|
|
|
goto out;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
2021-03-28 03:55:08 -06:00
|
|
|
for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
|
|
|
|
if (fds[i].revents != 0) {
|
|
|
|
ret = read_keyboard(kbd);
|
|
|
|
if (ret) {
|
|
|
|
goto out;
|
|
|
|
}
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 00:06:10 -06:00
|
|
|
ret = 0;
|
|
|
|
out:
|
2021-03-28 03:55:08 -06:00
|
|
|
free(fds);
|
2012-04-07 17:20:39 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigintr_handler(int signum)
|
|
|
|
{
|
|
|
|
terminate = true;
|
|
|
|
}
|
|
|
|
|
2020-07-12 20:13:23 -06:00
|
|
|
static void
|
|
|
|
usage(FILE *fp, char *progname)
|
|
|
|
{
|
2023-06-16 01:54:09 -06:00
|
|
|
fprintf(fp, "Usage: %s [--include=<path>] [--include-defaults] "
|
|
|
|
"[--rules=<rules>] [--model=<model>] [--layout=<layout>] "
|
|
|
|
"[--variant=<variant>] [--options=<options>]\n",
|
2020-07-12 20:13:23 -06:00
|
|
|
progname);
|
2024-02-06 17:06:15 -07:00
|
|
|
fprintf(fp, " or: %s --keymap <path to keymap file>\n",
|
2020-07-12 20:13:23 -06:00
|
|
|
progname);
|
|
|
|
fprintf(fp, "For both:\n"
|
2024-02-13 17:23:15 -07:00
|
|
|
" --verbose (enable verbose debugging output)\n"
|
2023-09-18 04:17:11 -06:00
|
|
|
#ifdef ENABLE_PRIVATE_APIS
|
|
|
|
" --print-modmaps (print real & virtual key modmaps)\n"
|
|
|
|
#endif
|
2023-09-18 04:15:06 -06:00
|
|
|
" --short (do not print layout nor Unicode keysym translation)\n"
|
2020-07-12 20:13:23 -06:00
|
|
|
" --report-state-changes (report changes to the state)\n"
|
2020-07-25 02:17:11 -06:00
|
|
|
" --enable-compose (enable Compose)\n"
|
|
|
|
" --consumed-mode={xkb|gtk} (select the consumed modifiers mode, default: xkb)\n"
|
2020-07-27 02:51:53 -06:00
|
|
|
" --without-x11-offset (don't add X11 keycode offset)\n"
|
2023-10-03 02:28:47 -06:00
|
|
|
"Other:\n"
|
|
|
|
" --help (display this help and exit)\n"
|
2020-07-25 02:17:11 -06:00
|
|
|
);
|
2020-07-12 20:13:23 -06:00
|
|
|
}
|
|
|
|
|
2012-04-07 17:20:39 -06:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2020-06-23 00:06:10 -06:00
|
|
|
int ret = EXIT_FAILURE;
|
2012-04-07 17:20:39 -06:00
|
|
|
struct keyboard *kbds;
|
2020-06-23 00:06:10 -06:00
|
|
|
struct xkb_context *ctx = NULL;
|
|
|
|
struct xkb_keymap *keymap = NULL;
|
2014-01-29 04:46:42 -07:00
|
|
|
struct xkb_compose_table *compose_table = NULL;
|
2023-06-16 01:54:09 -06:00
|
|
|
const char *includes[64];
|
|
|
|
size_t num_includes = 0;
|
2012-08-02 18:51:40 -06:00
|
|
|
const char *rules = NULL;
|
|
|
|
const char *model = NULL;
|
|
|
|
const char *layout = NULL;
|
|
|
|
const char *variant = NULL;
|
|
|
|
const char *options = NULL;
|
2012-08-02 00:54:38 -06:00
|
|
|
const char *keymap_path = NULL;
|
2014-01-29 04:46:42 -07:00
|
|
|
const char *locale;
|
2012-04-07 17:20:39 -06:00
|
|
|
struct sigaction act;
|
2020-07-12 20:13:23 -06:00
|
|
|
enum options {
|
2024-02-13 17:23:15 -07:00
|
|
|
OPT_VERBOSE,
|
2023-06-16 01:54:09 -06:00
|
|
|
OPT_INCLUDE,
|
|
|
|
OPT_INCLUDE_DEFAULTS,
|
2020-07-12 20:13:23 -06:00
|
|
|
OPT_RULES,
|
|
|
|
OPT_MODEL,
|
|
|
|
OPT_LAYOUT,
|
|
|
|
OPT_VARIANT,
|
|
|
|
OPT_OPTION,
|
|
|
|
OPT_KEYMAP,
|
2020-07-27 02:51:53 -06:00
|
|
|
OPT_WITHOUT_X11_OFFSET,
|
2020-07-12 20:13:23 -06:00
|
|
|
OPT_CONSUMED_MODE,
|
|
|
|
OPT_COMPOSE,
|
2023-09-18 04:15:06 -06:00
|
|
|
OPT_SHORT,
|
2020-07-12 20:13:23 -06:00
|
|
|
OPT_REPORT_STATE,
|
2023-09-18 04:17:11 -06:00
|
|
|
#ifdef ENABLE_PRIVATE_APIS
|
|
|
|
OPT_PRINT_MODMAPS,
|
|
|
|
#endif
|
2020-07-12 20:13:23 -06:00
|
|
|
};
|
|
|
|
static struct option opts[] = {
|
|
|
|
{"help", no_argument, 0, 'h'},
|
2024-02-13 17:23:15 -07:00
|
|
|
{"verbose", no_argument, 0, OPT_VERBOSE},
|
2023-06-16 01:54:09 -06:00
|
|
|
{"include", required_argument, 0, OPT_INCLUDE},
|
|
|
|
{"include-defaults", no_argument, 0, OPT_INCLUDE_DEFAULTS},
|
2020-07-12 20:13:23 -06:00
|
|
|
{"rules", required_argument, 0, OPT_RULES},
|
|
|
|
{"model", required_argument, 0, OPT_MODEL},
|
|
|
|
{"layout", required_argument, 0, OPT_LAYOUT},
|
|
|
|
{"variant", required_argument, 0, OPT_VARIANT},
|
|
|
|
{"options", required_argument, 0, OPT_OPTION},
|
|
|
|
{"keymap", required_argument, 0, OPT_KEYMAP},
|
|
|
|
{"consumed-mode", required_argument, 0, OPT_CONSUMED_MODE},
|
|
|
|
{"enable-compose", no_argument, 0, OPT_COMPOSE},
|
2023-09-18 04:15:06 -06:00
|
|
|
{"short", no_argument, 0, OPT_SHORT},
|
2020-07-12 20:13:23 -06:00
|
|
|
{"report-state-changes", no_argument, 0, OPT_REPORT_STATE},
|
2020-07-27 02:57:24 -06:00
|
|
|
{"without-x11-offset", no_argument, 0, OPT_WITHOUT_X11_OFFSET},
|
2023-09-18 04:17:11 -06:00
|
|
|
#ifdef ENABLE_PRIVATE_APIS
|
|
|
|
{"print-modmaps", no_argument, 0, OPT_PRINT_MODMAPS},
|
|
|
|
#endif
|
2020-07-12 20:13:23 -06:00
|
|
|
{0, 0, 0, 0},
|
|
|
|
};
|
2012-04-07 17:20:39 -06:00
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2020-07-12 20:13:23 -06:00
|
|
|
while (1) {
|
|
|
|
int opt;
|
|
|
|
int option_index = 0;
|
|
|
|
|
|
|
|
opt = getopt_long(argc, argv, "h", opts, &option_index);
|
|
|
|
if (opt == -1)
|
|
|
|
break;
|
|
|
|
|
2012-04-07 17:20:39 -06:00
|
|
|
switch (opt) {
|
2024-02-13 17:23:15 -07:00
|
|
|
case OPT_VERBOSE:
|
|
|
|
verbose = true;
|
|
|
|
break;
|
2023-06-16 01:54:09 -06:00
|
|
|
case OPT_INCLUDE:
|
|
|
|
if (num_includes >= ARRAY_SIZE(includes)) {
|
|
|
|
fprintf(stderr, "error: too many includes\n");
|
|
|
|
exit(EXIT_INVALID_USAGE);
|
|
|
|
}
|
|
|
|
includes[num_includes++] = optarg;
|
|
|
|
break;
|
|
|
|
case OPT_INCLUDE_DEFAULTS:
|
|
|
|
if (num_includes >= ARRAY_SIZE(includes)) {
|
|
|
|
fprintf(stderr, "error: too many includes\n");
|
|
|
|
exit(EXIT_INVALID_USAGE);
|
|
|
|
}
|
|
|
|
includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
|
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_RULES:
|
2012-08-02 18:51:40 -06:00
|
|
|
rules = optarg;
|
2012-04-07 17:20:39 -06:00
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_MODEL:
|
2012-08-02 18:51:40 -06:00
|
|
|
model = optarg;
|
2012-04-07 17:20:39 -06:00
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_LAYOUT:
|
2012-08-02 18:51:40 -06:00
|
|
|
layout = optarg;
|
2012-04-07 17:20:39 -06:00
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_VARIANT:
|
2012-08-02 18:51:40 -06:00
|
|
|
variant = optarg;
|
2012-04-07 17:20:39 -06:00
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_OPTION:
|
2012-08-02 18:51:40 -06:00
|
|
|
options = optarg;
|
2012-04-07 17:20:39 -06:00
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_KEYMAP:
|
2012-08-02 00:54:38 -06:00
|
|
|
keymap_path = optarg;
|
|
|
|
break;
|
2020-07-27 02:51:53 -06:00
|
|
|
case OPT_WITHOUT_X11_OFFSET:
|
|
|
|
evdev_offset = 0;
|
2020-07-25 02:17:11 -06:00
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_REPORT_STATE:
|
2012-10-22 13:16:35 -06:00
|
|
|
report_state_changes = true;
|
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_COMPOSE:
|
2014-01-29 04:46:42 -07:00
|
|
|
with_compose = true;
|
|
|
|
break;
|
2023-09-18 04:15:06 -06:00
|
|
|
case OPT_SHORT:
|
|
|
|
print_fields &= ~PRINT_VERBOSE_FIELDS;
|
|
|
|
break;
|
2020-07-12 20:13:23 -06:00
|
|
|
case OPT_CONSUMED_MODE:
|
|
|
|
if (strcmp(optarg, "gtk") == 0) {
|
|
|
|
consumed_mode = XKB_CONSUMED_MODE_GTK;
|
|
|
|
} else if (strcmp(optarg, "xkb") == 0) {
|
|
|
|
consumed_mode = XKB_CONSUMED_MODE_XKB;
|
|
|
|
} else {
|
2020-07-25 02:17:11 -06:00
|
|
|
fprintf(stderr, "error: invalid --consumed-mode \"%s\"\n", optarg);
|
2020-07-12 20:13:23 -06:00
|
|
|
usage(stderr, argv[0]);
|
|
|
|
return EXIT_INVALID_USAGE;
|
|
|
|
}
|
2016-02-27 13:31:16 -07:00
|
|
|
break;
|
2023-09-18 04:17:11 -06:00
|
|
|
#ifdef ENABLE_PRIVATE_APIS
|
|
|
|
case OPT_PRINT_MODMAPS:
|
|
|
|
print_fields |= PRINT_MODMAPS;
|
|
|
|
break;
|
|
|
|
#endif
|
2020-07-12 20:13:23 -06:00
|
|
|
case 'h':
|
|
|
|
usage(stdout, argv[0]);
|
|
|
|
return EXIT_SUCCESS;
|
2012-04-07 17:20:39 -06:00
|
|
|
case '?':
|
2020-07-12 20:13:23 -06:00
|
|
|
usage(stderr, argv[0]);
|
|
|
|
return EXIT_INVALID_USAGE;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-16 01:54:09 -06:00
|
|
|
ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
|
2012-08-02 00:54:38 -06:00
|
|
|
if (!ctx) {
|
2012-04-07 17:20:39 -06:00
|
|
|
fprintf(stderr, "Couldn't create xkb context\n");
|
2020-06-23 00:06:10 -06:00
|
|
|
goto out;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:23:15 -07:00
|
|
|
if (verbose) {
|
|
|
|
xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_DEBUG);
|
|
|
|
xkb_context_set_log_verbosity(ctx, 10);
|
|
|
|
}
|
|
|
|
|
2023-06-16 01:54:09 -06:00
|
|
|
if (num_includes == 0)
|
|
|
|
includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_includes; i++) {
|
|
|
|
const char *include = includes[i];
|
|
|
|
if (strcmp(include, DEFAULT_INCLUDE_PATH_PLACEHOLDER) == 0)
|
|
|
|
xkb_context_include_path_append_default(ctx);
|
|
|
|
else
|
|
|
|
xkb_context_include_path_append(ctx, include);
|
|
|
|
}
|
|
|
|
|
2012-09-30 02:42:30 -06:00
|
|
|
if (keymap_path) {
|
2019-12-28 04:49:40 -07:00
|
|
|
FILE *file = fopen(keymap_path, "rb");
|
2012-09-30 02:42:30 -06:00
|
|
|
if (!file) {
|
|
|
|
fprintf(stderr, "Couldn't open '%s': %s\n",
|
|
|
|
keymap_path, strerror(errno));
|
2020-06-23 00:06:10 -06:00
|
|
|
goto out;
|
2012-09-30 02:42:30 -06:00
|
|
|
}
|
|
|
|
keymap = xkb_keymap_new_from_file(ctx, file,
|
2020-03-25 02:43:41 -06:00
|
|
|
XKB_KEYMAP_FORMAT_TEXT_V1,
|
|
|
|
XKB_KEYMAP_COMPILE_NO_FLAGS);
|
2012-09-30 02:42:30 -06:00
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
else {
|
2020-06-22 22:01:48 -06:00
|
|
|
struct xkb_rule_names rmlvo = {
|
2020-06-28 00:50:47 -06:00
|
|
|
.rules = (rules == NULL || rules[0] == '\0') ? NULL : rules,
|
|
|
|
.model = (model == NULL || model[0] == '\0') ? NULL : model,
|
|
|
|
.layout = (layout == NULL || layout[0] == '\0') ? NULL : layout,
|
|
|
|
.variant = (variant == NULL || variant[0] == '\0') ? NULL : variant,
|
|
|
|
.options = (options == NULL || options[0] == '\0') ? NULL : options
|
2020-06-22 22:01:48 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!rules && !model && !layout && !variant && !options)
|
|
|
|
keymap = xkb_keymap_new_from_names(ctx, NULL, 0);
|
|
|
|
else
|
|
|
|
keymap = xkb_keymap_new_from_names(ctx, &rmlvo, 0);
|
|
|
|
|
|
|
|
if (!keymap) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
|
|
|
|
rules, model, layout, variant, options);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-09-30 02:42:30 -06:00
|
|
|
}
|
|
|
|
|
2012-08-02 00:54:38 -06:00
|
|
|
if (!keymap) {
|
2012-04-07 17:20:39 -06:00
|
|
|
fprintf(stderr, "Couldn't create xkb keymap\n");
|
2020-06-23 00:06:10 -06:00
|
|
|
goto out;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
2014-01-29 04:46:42 -07:00
|
|
|
if (with_compose) {
|
|
|
|
locale = setlocale(LC_CTYPE, NULL);
|
|
|
|
compose_table =
|
|
|
|
xkb_compose_table_new_from_locale(ctx, locale,
|
|
|
|
XKB_COMPOSE_COMPILE_NO_FLAGS);
|
|
|
|
if (!compose_table) {
|
|
|
|
fprintf(stderr, "Couldn't create compose from locale\n");
|
2020-06-23 00:06:10 -06:00
|
|
|
goto out;
|
2014-01-29 04:46:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kbds = get_keyboards(keymap, compose_table);
|
2012-04-07 17:20:39 -06:00
|
|
|
if (!kbds) {
|
2020-06-23 00:06:10 -06:00
|
|
|
goto out;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|
|
|
|
|
2023-09-18 04:17:11 -06:00
|
|
|
#ifdef ENABLE_PRIVATE_APIS
|
|
|
|
if (print_fields & PRINT_MODMAPS) {
|
|
|
|
print_keys_modmaps(keymap);
|
|
|
|
putchar('\n');
|
|
|
|
print_keymap_modmaps(keymap);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-07 17:20:39 -06:00
|
|
|
act.sa_handler = sigintr_handler;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
sigaction(SIGINT, &act, NULL);
|
|
|
|
sigaction(SIGTERM, &act, NULL);
|
|
|
|
|
2020-06-22 22:01:48 -06:00
|
|
|
tools_disable_stdin_echo();
|
2012-04-07 17:20:39 -06:00
|
|
|
ret = loop(kbds);
|
2020-06-22 22:01:48 -06:00
|
|
|
tools_enable_stdin_echo();
|
2012-04-07 17:20:39 -06:00
|
|
|
|
|
|
|
free_keyboards(kbds);
|
2020-06-23 00:06:10 -06:00
|
|
|
out:
|
2014-01-29 04:46:42 -07:00
|
|
|
xkb_compose_table_unref(compose_table);
|
2012-09-21 05:44:17 -06:00
|
|
|
xkb_keymap_unref(keymap);
|
2012-08-02 00:54:38 -06:00
|
|
|
xkb_context_unref(ctx);
|
2020-06-23 00:06:10 -06:00
|
|
|
|
|
|
|
return ret;
|
2012-04-07 17:20:39 -06:00
|
|
|
}
|