Skip to content

Commit

Permalink
tools: Enable using keymap file as a positional argument
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Jan 28, 2025
1 parent 691bc08 commit 6a064be
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
30 changes: 28 additions & 2 deletions tools/compile-keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
#include "tools-common.h"
#include "src/utils.h"

#ifdef _WIN32
#include <io.h>
#define isatty _isatty
#endif

#define DEFAULT_INCLUDE_PATH_PLACEHOLDER "__defaults__"

static bool verbose = false;
Expand Down Expand Up @@ -189,8 +194,6 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)
} else {
*path = optarg;
}
if (isempty(*path) || strcmp(*path, "-") == 0)
*path = NULL;
break;
case OPT_INCLUDE:
if (num_includes >= ARRAY_SIZE(includes)) {
Expand Down Expand Up @@ -243,6 +246,29 @@ parse_options(int argc, char **argv, char **path, struct xkb_rule_names *names)

}

if (optind < argc && !isempty(argv[optind])) {
/* Some positional arguments left: use as a keymap input */
if (output_format != FORMAT_KEYMAP_FROM_RMLVO)
goto output_format_error;
if (has_rmlvo_options)
goto too_much_arguments;
output_format = FORMAT_KEYMAP_FROM_XKB;
*path = argv[optind++];
if (optind < argc) {
too_much_arguments:
fprintf(stderr, "ERROR: Too much positional arguments\n");
usage(argv);
exit(EXIT_INVALID_USAGE);
}
} else if (!isatty(STDIN_FILENO) && !has_rmlvo_options &&
output_format != FORMAT_KEYMAP_FROM_XKB) {
/* No positional argument: detect piping */
output_format = FORMAT_KEYMAP_FROM_XKB;
}

if (isempty(*path) || strcmp(*path, "-") == 0)
*path = NULL;

return true;
output_format_error:
fprintf(stderr, "ERROR: Cannot mix output formats\n");
Expand Down
37 changes: 37 additions & 0 deletions tools/interactive-evdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include "xkbcommon/xkbcommon.h"

#include "src/utils.h"
#include "tools-common.h"

struct keyboard {
Expand Down Expand Up @@ -459,6 +460,7 @@ main(int argc, char *argv[])

setlocale(LC_ALL, "");

bool has_rmlvo_options = false;
while (1) {
int opt;
int option_index = 0;
Expand Down Expand Up @@ -486,21 +488,38 @@ main(int argc, char *argv[])
includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
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;
keymap_path = optarg;
break;
case OPT_WITHOUT_X11_OFFSET:
Expand Down Expand Up @@ -540,6 +559,19 @@ main(int argc, char *argv[])
}
}

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]);
exit(EXIT_INVALID_USAGE);
}
}

ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
if (!ctx) {
fprintf(stderr, "Couldn't create xkb context\n");
Expand Down Expand Up @@ -643,4 +675,9 @@ main(int argc, char *argv[])
xkb_context_unref(ctx);

return ret;

input_format_error:
fprintf(stderr, "ERROR: Cannot use RMLVO options with keymap input\n");
usage(stderr, argv[0]);
exit(EXIT_INVALID_USAGE);
}
6 changes: 6 additions & 0 deletions tools/xkbcli-compile-keymap.1
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
.Sh SYNOPSIS
.Nm
.Op Ar options
.Op Ar KEYMAP_PATH
.
.Sh DESCRIPTION
.Nm
compiles and prints a keymap based on the given options.
.
.Bl -tag -width Ds
.It Ar KEYMAP_PATH
Path to a keymap file, or
.Dq \-
to read the standard input
.
.It Fl \-help
Print help and exit
.
Expand Down
6 changes: 6 additions & 0 deletions tools/xkbcli-interactive-evdev.1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
.Sh SYNOPSIS
.Nm
.Op Ar options
.Op Ar KEYMAP_PATH
.
.Sh DESCRIPTION
.Nm
Expand All @@ -34,6 +35,11 @@ key to exit.
This is a debugging tool, its behavior or output is not guaranteed to be stable.
.
.Bl -tag -width Ds
.It Ar KEYMAP_PATH
Path to a keymap file, or
.Dq \-
to read the standard input
.
.It Fl \-help
Print help and exit
.
Expand Down

0 comments on commit 6a064be

Please sign in to comment.