interactive: add support to run from keymap file
This is useful for quickly testing a random keymap file. Use -k <PATH>. Signed-off-by: Ran Benita <ran234@gmail.com>master
parent
cfd978b89d
commit
1d570a6d46
|
@ -157,7 +157,7 @@ filter_device_name(const struct dirent *ent)
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct keyboard *
|
static struct keyboard *
|
||||||
get_keyboards(struct xkb_keymap *xkb)
|
get_keyboards(struct xkb_keymap *keymap)
|
||||||
{
|
{
|
||||||
int ret, i, nents;
|
int ret, i, nents;
|
||||||
struct dirent **ents;
|
struct dirent **ents;
|
||||||
|
@ -170,7 +170,7 @@ get_keyboards(struct xkb_keymap *xkb)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < nents; i++) {
|
for (i = 0; i < nents; i++) {
|
||||||
ret = keyboard_new(ents[i], xkb, &kbd);
|
ret = keyboard_new(ents[i], keymap, &kbd);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
if (ret == -EACCES) {
|
if (ret == -EACCES) {
|
||||||
fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
|
fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
|
||||||
|
@ -217,7 +217,7 @@ static void
|
||||||
print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
|
print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
struct xkb_keymap *xkb;
|
struct xkb_keymap *keymap;
|
||||||
struct xkb_state *state;
|
struct xkb_state *state;
|
||||||
|
|
||||||
const xkb_keysym_t *syms;
|
const xkb_keysym_t *syms;
|
||||||
|
@ -229,7 +229,7 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
|
||||||
xkb_led_index_t led;
|
xkb_led_index_t led;
|
||||||
|
|
||||||
state = kbd->state;
|
state = kbd->state;
|
||||||
xkb = xkb_state_get_map(state);
|
keymap = xkb_state_get_map(state);
|
||||||
|
|
||||||
nsyms = xkb_key_get_syms(state, keycode, &syms);
|
nsyms = xkb_key_get_syms(state, keycode, &syms);
|
||||||
|
|
||||||
|
@ -257,26 +257,26 @@ print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
printf("groups [ ");
|
printf("groups [ ");
|
||||||
for (group = 0; group < xkb_map_num_groups(xkb); group++) {
|
for (group = 0; group < xkb_map_num_groups(keymap); group++) {
|
||||||
if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
|
if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
|
||||||
continue;
|
continue;
|
||||||
printf("%s (%d) ", xkb_map_group_get_name(xkb, group), group);
|
printf("%s (%d) ", xkb_map_group_get_name(keymap, group), group);
|
||||||
}
|
}
|
||||||
printf("] ");
|
printf("] ");
|
||||||
|
|
||||||
printf("mods [ ");
|
printf("mods [ ");
|
||||||
for (mod = 0; mod < xkb_map_num_mods(xkb); mod++) {
|
for (mod = 0; mod < xkb_map_num_mods(keymap); mod++) {
|
||||||
if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
|
if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
|
||||||
continue;
|
continue;
|
||||||
printf("%s ", xkb_map_mod_get_name(xkb, mod));
|
printf("%s ", xkb_map_mod_get_name(keymap, mod));
|
||||||
}
|
}
|
||||||
printf("] ");
|
printf("] ");
|
||||||
|
|
||||||
printf("leds [ ");
|
printf("leds [ ");
|
||||||
for (led = 0; led < xkb_map_num_leds(xkb); led++) {
|
for (led = 0; led < xkb_map_num_leds(keymap); led++) {
|
||||||
if (!xkb_state_led_index_is_active(state, led))
|
if (!xkb_state_led_index_is_active(state, led))
|
||||||
continue;
|
continue;
|
||||||
printf("%s ", xkb_map_led_get_name(xkb, led));
|
printf("%s ", xkb_map_led_get_name(keymap, led));
|
||||||
}
|
}
|
||||||
printf("] ");
|
printf("] ");
|
||||||
|
|
||||||
|
@ -409,8 +409,8 @@ main(int argc, char *argv[])
|
||||||
int ret;
|
int ret;
|
||||||
int opt;
|
int opt;
|
||||||
struct keyboard *kbds;
|
struct keyboard *kbds;
|
||||||
struct xkb_context *context;
|
struct xkb_context *ctx;
|
||||||
struct xkb_keymap *xkb;
|
struct xkb_keymap *keymap;
|
||||||
struct xkb_rule_names names = {
|
struct xkb_rule_names names = {
|
||||||
.rules = "evdev",
|
.rules = "evdev",
|
||||||
.model = "evdev",
|
.model = "evdev",
|
||||||
|
@ -418,11 +418,13 @@ main(int argc, char *argv[])
|
||||||
.variant = "",
|
.variant = "",
|
||||||
.options = "",
|
.options = "",
|
||||||
};
|
};
|
||||||
|
const char *keymap_path = NULL;
|
||||||
|
FILE *file;
|
||||||
struct sigaction act;
|
struct sigaction act;
|
||||||
|
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "r:m:l:v:o:")) != -1) {
|
while ((opt = getopt(argc, argv, "r:m:l:v:o:k:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'r':
|
case 'r':
|
||||||
names.rules = optarg;
|
names.rules = optarg;
|
||||||
|
@ -439,29 +441,45 @@ main(int argc, char *argv[])
|
||||||
case 'o':
|
case 'o':
|
||||||
names.options = optarg;
|
names.options = optarg;
|
||||||
break;
|
break;
|
||||||
|
case 'k':
|
||||||
|
keymap_path = optarg;
|
||||||
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
|
fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
|
||||||
"[-l <layout>] [-v <variant>] [-o <options>]\n",
|
"[-l <layout>] [-v <variant>] [-o <options>]\n",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
|
fprintf(stderr, " or: %s -k <path to keymap file>\n",
|
||||||
|
argv[0]);
|
||||||
exit(EX_USAGE);
|
exit(EX_USAGE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context = xkb_context_new(0);
|
ctx = xkb_context_new(0);
|
||||||
if (!context) {
|
if (!ctx) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
fprintf(stderr, "Couldn't create xkb context\n");
|
fprintf(stderr, "Couldn't create xkb context\n");
|
||||||
goto err_out;
|
goto err_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
xkb = xkb_map_new_from_names(context, &names, 0);
|
if (keymap_path) {
|
||||||
if (!xkb) {
|
file = fopen(keymap_path, "r");
|
||||||
|
if (!file) {
|
||||||
|
fprintf(stderr, "Couldn't open file %s: %s\n",
|
||||||
|
keymap_path, strerror(errno));
|
||||||
|
goto err_ctx;
|
||||||
|
}
|
||||||
|
keymap = xkb_map_new_from_file(ctx, file,
|
||||||
|
XKB_KEYMAP_FORMAT_TEXT_V1, 0);
|
||||||
|
} else {
|
||||||
|
keymap = xkb_map_new_from_names(ctx, &names, 0);
|
||||||
|
}
|
||||||
|
if (!keymap) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
fprintf(stderr, "Couldn't create xkb keymap\n");
|
fprintf(stderr, "Couldn't create xkb keymap\n");
|
||||||
goto err_ctx;
|
goto err_ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
kbds = get_keyboards(xkb);
|
kbds = get_keyboards(keymap);
|
||||||
if (!kbds) {
|
if (!kbds) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto err_xkb;
|
goto err_xkb;
|
||||||
|
@ -485,9 +503,9 @@ err_stty:
|
||||||
system("stty echo");
|
system("stty echo");
|
||||||
free_keyboards(kbds);
|
free_keyboards(kbds);
|
||||||
err_xkb:
|
err_xkb:
|
||||||
xkb_map_unref(xkb);
|
xkb_map_unref(keymap);
|
||||||
err_ctx:
|
err_ctx:
|
||||||
xkb_context_unref(context);
|
xkb_context_unref(ctx);
|
||||||
err_out:
|
err_out:
|
||||||
exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue