| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907 |
- /*
- * Copyright © 2012 Ran Benita <ran234@gmail.com>
- * SPDX-License-Identifier: MIT
- */
- #include "config.h"
- #include <assert.h>
- #include <dirent.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <fnmatch.h>
- #include <getopt.h>
- #include <limits.h>
- #include <locale.h>
- #include <poll.h>
- #include <signal.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <linux/input.h>
- #include "xkbcommon/xkbcommon.h"
- #include "src/utils.h"
- #include "src/keymap-formats.h"
- #include "tools-common.h"
- struct keyboard {
- char *path;
- int fd;
- struct xkb_machine *machine;
- struct xkb_events *events;
- struct xkb_state *state;
- struct xkb_compose_state *compose_state;
- struct keyboard *next;
- };
- static bool verbose = false;
- static bool terminate = false;
- static xkb_keycode_t evdev_offset = 8;
- static bool use_events_api = true;
- static bool report_state_changes = true;
- static bool with_compose = false;
- static enum xkb_consumed_mode consumed_mode = XKB_CONSUMED_MODE_XKB;
- static enum print_state_options print_options = DEFAULT_PRINT_OPTIONS;
- #define DEFAULT_INCLUDE_PATH_PLACEHOLDER "__defaults__"
- #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
- static bool
- evdev_bit_is_set(const unsigned long *array, int bit)
- {
- return array[bit / LONG_BIT] & (1ULL << (bit % LONG_BIT));
- }
- /* 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
- keyboard_new(struct dirent *ent,
- struct xkb_context *ctx, struct xkb_keymap *keymap,
- const struct xkb_machine_builder *builder,
- const struct xkb_machine_options *options,
- struct xkb_compose_table *compose_table, struct keyboard **out)
- {
- int ret;
- char *path;
- int fd;
- struct xkb_machine *machine = NULL;
- struct xkb_events *events = NULL;
- struct xkb_state *state = NULL;
- struct xkb_compose_state *compose_state = NULL;
- struct keyboard *kbd = NULL;
- 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;
- }
- if (use_events_api) {
- machine = xkb_machine_new(builder);
- if (!machine) {
- fprintf(stderr, "Couldn't create xkb state machine for %s\n", path);
- ret = -EFAULT;
- goto err_machine;
- }
- events = xkb_events_new_batch(ctx, XKB_EVENTS_NO_FLAGS);
- if (!events) {
- fprintf(stderr, "Couldn't create xkb events for %s\n", path);
- ret = -EFAULT;
- goto err_xkb_events;
- }
- }
- state = xkb_state_new(keymap);
- if (!state) {
- fprintf(stderr, "Couldn't create xkb state for %s\n", path);
- ret = -EFAULT;
- goto err_state;
- }
- const struct xkb_state_components_update components = {
- .size = sizeof(components),
- .components = XKB_STATE_CONTROLS,
- .affect_controls = options->controls.boolean.affect,
- .controls = options->controls.boolean.flags,
- };
- const struct xkb_state_update update = {
- .size = sizeof(update),
- .components = &components,
- };
- if (use_events_api) {
- // FIXME: handle error
- xkb_machine_process_synthetic(machine, &update, events);
- const struct xkb_event *event;
- while ((event = xkb_events_next(events))) {
- xkb_state_update_event(state, event);
- }
- } else {
- // FIXME: handle error
- xkb_state_update_synthetic(state, &update, NULL);
- }
- 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_compose_state;
- }
- }
- kbd = calloc(1, sizeof(*kbd));
- if (!kbd) {
- ret = -ENOMEM;
- goto err_kbd;
- }
- kbd->path = path;
- kbd->fd = fd;
- kbd->machine = machine;
- kbd->events = events;
- kbd->state = state;
- kbd->compose_state = compose_state;
- *out = kbd;
- return 0;
- err_kbd:
- xkb_compose_state_unref(compose_state);
- err_compose_state:
- xkb_state_unref(state);
- err_state:
- xkb_events_destroy(events);
- err_xkb_events:
- xkb_machine_unref(machine);
- err_machine:
- 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_machine_unref(kbd->machine);
- xkb_events_destroy(kbd->events);
- xkb_state_unref(kbd->state);
- xkb_compose_state_unref(kbd->compose_state);
- free(kbd);
- }
- static int
- filter_device_name(const struct dirent *ent)
- {
- return !fnmatch("event*", ent->d_name, 0);
- }
- static struct keyboard *
- get_keyboards(struct xkb_context *ctx, struct xkb_keymap *keymap,
- const struct xkb_machine_builder *builder,
- const struct xkb_machine_options *options,
- struct xkb_compose_table *compose_table)
- {
- 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++) {
- ret = keyboard_new(ents[i], ctx, keymap, builder, options,
- compose_table, &kbd);
- 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;
- }
- assert(kbd != NULL);
- 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)
- {
- if (type != EV_KEY)
- return;
- const xkb_keycode_t keycode = evdev_offset + code;
- struct xkb_keymap * const keymap = xkb_state_get_keymap(kbd->state);
- if ((value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode)) ||
- (value != KEY_STATE_PRESS && value != KEY_STATE_REPEAT &&
- value != KEY_STATE_RELEASE))
- return;
- const enum xkb_key_direction direction = (value == KEY_STATE_RELEASE)
- ? XKB_KEY_UP
- : (value == KEY_STATE_REPEAT)
- ? XKB_KEY_REPEATED
- : XKB_KEY_DOWN;
- if (use_events_api) {
- /* Use the xkb_machine API */
- const int ret = xkb_machine_process_key(kbd->machine, keycode, direction,
- kbd->events);
- if (ret) {
- fprintf(stderr, "ERROR: could not update the state machine\n");
- // TODO: better error handling
- } else {
- tools_print_events(NULL, kbd->state, kbd->events,
- kbd->compose_state, consumed_mode,
- print_options, report_state_changes);
- }
- } else {
- /* Use the legacy state API */
- if (with_compose && direction == XKB_KEY_DOWN) {
- xkb_keysym_t keysym = xkb_state_key_get_one_sym(kbd->state, keycode);
- xkb_compose_state_feed(kbd->compose_state, keysym);
- }
- tools_print_keycode_state(
- NULL, kbd->state, kbd->compose_state, keycode, direction,
- consumed_mode, print_options
- );
- if (with_compose) {
- const enum xkb_compose_status 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);
- }
- const enum xkb_state_component changed =
- xkb_state_update_key(kbd->state, keycode, direction);
- if (changed && report_state_changes)
- tools_print_state_changes(NULL, kbd->state, changed, print_options);
- }
- }
- 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) {
- const size_t nevs = len / sizeof(struct input_event);
- for (size_t i = 0; i < nevs; i++)
- process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
- }
- if (len < 0 && errno != EWOULDBLOCK) {
- fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
- return 1;
- }
- return 0;
- }
- static int
- loop(struct keyboard *kbds)
- {
- int ret = -1;
- struct keyboard *kbd;
- nfds_t nfds, i;
- for (kbd = kbds, nfds = 0; kbd; kbd = kbd->next, nfds++) {}
- struct pollfd *fds = calloc(nfds, sizeof(*fds));
- if (!fds) {
- fprintf(stderr, "Out of memory");
- goto out;
- }
- for (i = 0, kbd = kbds; kbd && i < nfds; kbd = kbd->next, i++) {
- fds[i].fd = kbd->fd;
- fds[i].events = POLLIN;
- }
- while (!terminate) {
- ret = poll(fds, nfds, -1);
- if (ret < 0) {
- if (errno == EINTR)
- continue;
- fprintf(stderr, "Couldn't poll for events: %s\n",
- strerror(errno));
- goto out;
- }
- for (i = 0, kbd = kbds; kbd && i < nfds; kbd = kbd->next, i++) {
- if (fds[i].revents != 0) {
- ret = read_keyboard(kbd);
- if (ret) {
- goto out;
- }
- }
- }
- }
- ret = 0;
- out:
- free(fds);
- return ret;
- }
- static void
- sigintr_handler(int signum)
- {
- terminate = true;
- }
- static void
- usage(FILE *fp, char *progname)
- {
- fprintf(fp, "Usage: %s [--include=<path>] [--include-defaults] [--format=<format>]"
- "[--rules=<rules>] [--model=<model>] [--layout=<layout>] "
- "[--variant=<variant>] [--options=<options>] "
- "[--enable-environment-names]\n"
- " or: %s --keymap <path to keymap file>\n"
- "Options:\n"
- " --format <FORMAT>\n"
- " Use keymap format FORMAT (default: '%s')\n"
- " --strict\n"
- " Parse using strict mode\n"
- " --verbose\n"
- " Enable verbose debugging output\n"
- " -1, --uniline\n"
- " Enable uniline event output\n"
- " -*, --multiline\n"
- " Enable uniline event output\n"
- " --short\n"
- " Shorter event output\n"
- " --report-state-changes\n"
- " Report changes to the state\n"
- " --no-state-report\n"
- " Do not report changes to the state\n"
- " --legacy-state-api[=true|false]\n"
- " Use legacy state API instead of event API\n"
- " --controls\n"
- " Keyboard controls: sticky-keys, sticky-keys-no-simultaneous,\n"
- " sticky-keys-latch-to-lock, latch-simultaneous, overlay{1-8}\n"
- " --modifiers-mapping <MAPPING>\n"
- " Remap the modifiers\n"
- " --shortcuts-mask <MASK>\n"
- " Set the modifier mask for shortcuts tweaks\n"
- " --shortcuts-mapping <MAPPING>\n"
- " Set the layout indices mapping for shortcuts tweaks\n"
- " --enable-compose\n"
- " Enable Compose\n"
- " --consumed-mode={xkb|gtk}\n"
- " Select the consumed modifiers mode (default: xkb)\n"
- " --without-x11-offset\n"
- " Don't add X11 keycode offset\n"
- "Other:\n"
- " --help\n"
- " Display this help and exit\n"
- " --version\n"
- " Print version information and exit\n",
- progname, progname,
- xkb_keymap_get_format_label(DEFAULT_INPUT_KEYMAP_FORMAT)
- );
- }
- int
- main(int argc, char *argv[])
- {
- int ret = EXIT_FAILURE;
- struct keyboard *kbds;
- struct xkb_context *ctx = NULL;
- struct xkb_keymap *keymap = NULL;
- struct xkb_compose_table *compose_table = NULL;
- struct xkb_machine_builder *machine_builder = NULL;
- const char *includes[64];
- size_t num_includes = 0;
- bool use_env_names = false;
- enum xkb_keymap_format keymap_format = DEFAULT_INPUT_KEYMAP_FORMAT;
- static enum xkb_keymap_compile_flags compile_flags =
- (enum xkb_keymap_compile_flags) DEFAULT_KEYMAP_COMPILE_FLAGS;
- const char *rules = NULL;
- const char *model = NULL;
- const char *layout = NULL;
- const char *variant = NULL;
- const char *options = NULL;
- const char *keymap_path = NULL;
- bool with_keymap_file = false;
- const char *locale;
- struct sigaction act;
- enum options {
- OPT_VERBOSE,
- OPT_UNILINE,
- OPT_MULTILINE,
- OPT_INCLUDE,
- OPT_INCLUDE_DEFAULTS,
- OPT_ENABLE_ENV_NAMES,
- OPT_KEYMAP_FORMAT,
- OPT_KEYMAP_STRICT_PARSER,
- OPT_RULES,
- OPT_MODEL,
- OPT_LAYOUT,
- OPT_VARIANT,
- OPT_OPTION,
- OPT_KEYMAP,
- OPT_WITHOUT_X11_OFFSET,
- OPT_LEGACY_STATE_API,
- OPT_CONTROLS,
- OPT_MODIFIERS_TWEAK_MAPPING,
- OPT_SHORTCUTS_TWEAK_MASK,
- OPT_SHORTCUTS_TWEAK_MAPPING,
- OPT_CONSUMED_MODE,
- OPT_COMPOSE,
- OPT_SHORT,
- OPT_REPORT_STATE,
- OPT_NO_STATE_REPORT,
- };
- static struct option opts[] = {
- {"help", no_argument, 0, 'h'},
- {"version", no_argument, 0, 'V'},
- {"verbose", no_argument, 0, OPT_VERBOSE},
- {"uniline", no_argument, 0, OPT_UNILINE},
- {"multiline", no_argument, 0, OPT_MULTILINE},
- {"include", required_argument, 0, OPT_INCLUDE},
- {"include-defaults", no_argument, 0, OPT_INCLUDE_DEFAULTS},
- {"enable-environment-names", no_argument, 0, OPT_ENABLE_ENV_NAMES},
- {"format", required_argument, 0, OPT_KEYMAP_FORMAT},
- {"strict", no_argument, 0, OPT_KEYMAP_STRICT_PARSER},
- {"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},
- {"legacy-state-api", optional_argument, 0, OPT_LEGACY_STATE_API},
- {"controls", required_argument, 0, OPT_CONTROLS},
- {"modifiers-mapping", required_argument, 0, OPT_MODIFIERS_TWEAK_MAPPING},
- {"shortcuts-mask", required_argument, 0, OPT_SHORTCUTS_TWEAK_MASK},
- {"shortcuts-mapping", required_argument, 0, OPT_SHORTCUTS_TWEAK_MAPPING},
- {"consumed-mode", required_argument, 0, OPT_CONSUMED_MODE},
- {"enable-compose", no_argument, 0, OPT_COMPOSE},
- {"short", no_argument, 0, OPT_SHORT},
- {"report-state-changes", no_argument, 0, OPT_REPORT_STATE},
- {"no-state-report", no_argument, 0, OPT_NO_STATE_REPORT},
- {"without-x11-offset", no_argument, 0, OPT_WITHOUT_X11_OFFSET},
- {0, 0, 0, 0},
- };
- setlocale(LC_ALL, "");
- bool has_rmlvo_options = false;
- /* Initialize machine options */
- struct xkb_machine_options machine_options = xkb_machine_options_new();
- /* Ensure synced with usage() and man page */
- assert(consumed_mode == XKB_CONSUMED_MODE_XKB);
- while (1) {
- int option_index = 0;
- int opt = getopt_long(argc, argv, "*1hV", opts, &option_index);
- if (opt == -1)
- break;
- switch (opt) {
- case OPT_VERBOSE:
- verbose = true;
- break;
- case '1':
- case OPT_UNILINE:
- print_options |= PRINT_UNILINE;
- break;
- case '*':
- case OPT_MULTILINE:
- print_options &= ~PRINT_UNILINE;
- break;
- case OPT_INCLUDE:
- if (num_includes >= ARRAY_SIZE(includes))
- goto too_many_includes;
- includes[num_includes++] = optarg;
- break;
- case OPT_INCLUDE_DEFAULTS:
- if (num_includes >= ARRAY_SIZE(includes))
- goto too_many_includes;
- includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
- break;
- case OPT_ENABLE_ENV_NAMES:
- use_env_names = true;
- break;
- case OPT_KEYMAP_FORMAT:
- keymap_format = xkb_keymap_parse_format(optarg);
- if (!keymap_format) {
- fprintf(stderr, "ERROR: invalid --format \"%s\"\n", optarg);
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- break;
- case OPT_KEYMAP_STRICT_PARSER:
- compile_flags |= XKB_KEYMAP_COMPILE_STRICT_MODE;
- break;
- case OPT_RULES:
- if (keymap_path)
- goto input_format_error;
- rules = optarg;
- has_rmlvo_options = true;
- break;
- case OPT_MODEL:
- if (keymap_path)
- goto input_format_error;
- model = optarg;
- has_rmlvo_options = true;
- break;
- case OPT_LAYOUT:
- if (keymap_path)
- goto input_format_error;
- layout = optarg;
- has_rmlvo_options = true;
- break;
- case OPT_VARIANT:
- if (keymap_path)
- goto input_format_error;
- variant = optarg;
- has_rmlvo_options = true;
- break;
- case OPT_OPTION:
- if (keymap_path)
- goto input_format_error;
- options = optarg;
- has_rmlvo_options = true;
- break;
- case OPT_KEYMAP:
- if (has_rmlvo_options)
- goto input_format_error;
- with_keymap_file = true;
- keymap_path = optarg;
- break;
- case OPT_WITHOUT_X11_OFFSET:
- evdev_offset = 0;
- break;
- case OPT_REPORT_STATE:
- report_state_changes = true;
- break;
- case OPT_NO_STATE_REPORT:
- report_state_changes = false;
- break;
- case OPT_COMPOSE:
- with_compose = true;
- break;
- case OPT_SHORT:
- print_options &= ~PRINT_VERBOSE;
- break;
- 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 {
- fprintf(stderr, "ERROR: invalid --consumed-mode \"%s\"\n", optarg);
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- break;
- #ifdef ENABLE_PRIVATE_APIS
- case OPT_PRINT_MODMAPS:
- print_modmaps = true;
- break;
- #endif
- case OPT_LEGACY_STATE_API: {
- bool legacy_api = true;
- if (!tools_parse_bool(optarg, TOOLS_ARG_OPTIONAL, &legacy_api)) {
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- use_events_api = !legacy_api;
- break;
- }
- case OPT_CONTROLS:
- if (!tools_parse_controls(optarg, &machine_options)) {
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- /* --legacy-state-api=false is implied */
- use_events_api = true;
- break;
- case OPT_MODIFIERS_TWEAK_MAPPING:
- if (!tools_parse_modifiers_mappings(optarg, &machine_options)) {
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- /* --legacy-state-api=false is implied */
- use_events_api = true;
- break;
- case OPT_SHORTCUTS_TWEAK_MASK:
- if (!tools_parse_shortcuts_mask(optarg, &machine_options)) {
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- /* --legacy-state-api=false is implied */
- use_events_api = true;
- break;
- case OPT_SHORTCUTS_TWEAK_MAPPING:
- if (!tools_parse_shortcuts_mappings(optarg, &machine_options)) {
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- /* --legacy-state-api=false is implied */
- use_events_api = true;
- break;
- case 'h':
- usage(stdout, argv[0]);
- ret = EXIT_SUCCESS;
- goto error_parse_args;
- case 'V':
- printf("%s\n", LIBXKBCOMMON_VERSION);
- ret = EXIT_SUCCESS;
- goto error_parse_args;
- default:
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- }
- if (optind < argc && !isempty(argv[optind])) {
- /* Some positional arguments left: use as a keymap input */
- if (keymap_path || has_rmlvo_options)
- goto too_much_arguments;
- keymap_path = argv[optind++];
- if (optind < argc) {
- too_much_arguments:
- fprintf(stderr, "ERROR: Too much positional arguments\n");
- usage(stderr, argv[0]);
- ret = EXIT_INVALID_USAGE;
- goto error_parse_args;
- }
- with_keymap_file = true;
- } else if (is_pipe_or_regular_file(STDIN_FILENO) && !with_keymap_file) {
- /* No positional argument: piping detected */
- with_keymap_file = true;
- }
- if (isempty(keymap_path) || strcmp(keymap_path, "-") == 0)
- keymap_path = NULL;
- if (!(print_options & PRINT_VERBOSE) && (print_options & PRINT_UNILINE)) {
- print_options &= ~PRINT_VERBOSE_ONE_LINE_FIELDS;
- }
- enum xkb_context_flags ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES;
- if (!use_env_names)
- ctx_flags |= XKB_CONTEXT_NO_ENVIRONMENT_NAMES;
- ctx = xkb_context_new(ctx_flags);
- if (!ctx) {
- fprintf(stderr, "ERROR: Couldn't create xkb context\n");
- goto out;
- }
- if (verbose)
- tools_enable_verbose_logging(ctx);
- 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);
- }
- if (with_keymap_file) {
- FILE *file = NULL;
- if (keymap_path) {
- /* Read from regular file */
- file = fopen(keymap_path, "rb");
- } else {
- /* Read from stdin */
- file = tools_read_stdin();
- }
- if (!file) {
- fprintf(stderr, "ERROR: Failed to open keymap file \"%s\": %s\n",
- keymap_path ? keymap_path : "stdin", strerror(errno));
- goto out;
- }
- keymap = xkb_keymap_new_from_file(ctx, file, keymap_format,
- compile_flags);
- fclose(file);
- }
- else {
- struct xkb_rule_names rmlvo = {
- .rules = (isempty(rules)) ? NULL : rules,
- .model = (isempty(model)) ? NULL : model,
- .layout = (isempty(layout)) ? NULL : layout,
- .variant = (isempty(variant)) ? NULL : variant,
- .options = (isempty(options)) ? NULL : options
- };
- if (!rules && !model && !layout && !variant && !options)
- keymap = xkb_keymap_new_from_names2(ctx, NULL, keymap_format,
- compile_flags);
- else
- keymap = xkb_keymap_new_from_names2(ctx, &rmlvo, keymap_format,
- compile_flags);
- if (!keymap) {
- fprintf(stderr,
- "ERROR: Failed to compile RMLVO: "
- "'%s', '%s', '%s', '%s', '%s'\n",
- rules, model, layout, variant, options);
- goto out;
- }
- }
- if (!keymap) {
- fprintf(stderr, "ERROR: Couldn't create xkb keymap\n");
- goto out;
- }
- machine_builder = xkb_machine_builder_new_from_options(keymap,
- &machine_options);
- if (!machine_builder) {
- fprintf(stderr, "ERROR: Failed to create state machine builder\n");
- ret = EXIT_FAILURE;
- goto out;
- }
- 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, "ERROR: Couldn't create compose from locale\n");
- goto out;
- }
- }
- kbds = get_keyboards(ctx, keymap, machine_builder, &machine_options,
- compose_table);
- if (!kbds) {
- goto out;
- }
- #ifdef ENABLE_PRIVATE_APIS
- if (print_modmaps) {
- print_keys_modmaps(keymap);
- putchar('\n');
- print_modifiers_encodings(keymap);
- putchar('\n');
- }
- #endif
- act.sa_handler = sigintr_handler;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
- sigaction(SIGINT, &act, NULL);
- sigaction(SIGTERM, &act, NULL);
- tools_disable_stdin_echo();
- ret = loop(kbds);
- tools_enable_stdin_echo();
- free_keyboards(kbds);
- out:
- xkb_compose_table_unref(compose_table);
- xkb_machine_builder_destroy(machine_builder);
- xkb_keymap_unref(keymap);
- error_parse_args:
- xkb_machine_options_free(&machine_options);
- xkb_context_unref(ctx);
- return ret;
- too_many_includes:
- fprintf(stderr, "ERROR: too many includes (max: %zu)\n",
- ARRAY_SIZE(includes));
- xkb_machine_options_free(&machine_options);
- exit(EXIT_INVALID_USAGE);
- input_format_error:
- fprintf(stderr, "ERROR: Cannot use RMLVO options with keymap input\n");
- usage(stderr, argv[0]);
- xkb_machine_options_free(&machine_options);
- exit(EXIT_INVALID_USAGE);
- }
|