rules: add include statements to rules files
authorPeter Hutterer <peter.hutterer@who-t.net>
Tue, 3 Sep 2019 01:23:14 +0000 (11:23 +1000)
committerRan Benita <ran234@gmail.com>
Tue, 24 Dec 2019 07:50:03 +0000 (09:50 +0200)
commitca033a29d2ca910fd17b1ae287cb420205bdddc8
treea9a709167a4d6853f5e05c46729fd76374966585
parent67b538ddc0459732ddb9ef0961994ba3ae7c5cda
rules: add include statements to rules files

The majority use-case for extending XKB on a machine is to override one or a
few keys with custom keycodes, not to define whole layouts.

Previously, we relied on the rules file to be a single file, making it hard to
extend. libxkbcommon parses $XDG_CONFIG_HOME/xkb/ but that only works as long
as there is a rule that matches the user-specified RMLVO. This works for MLV
but not for options which don't have a wildcard defined. Users have to copy
the whole rules file and then work from there - not something easy to extend
and maintain.

This patch adds a new ! include directive to rules files that allows including
another file. The file path must be without quotes and may not start with the
literal "include". Two directives are supported, %H to $HOME and %S for the
system-installed rules directory (usually /usr/share/X11/xkb/rules).

A user would typically use a custom rules file like this:

! option                =       symbols
  custom:foo            =       +custom(foo)
  custom:bar            =       +custom(baz)

! include %S/evdev

Where the above defines the two options and then includes the system-installed
evdev rule. Since most current implementations default to loading the "evdev"
ruleset, it's best to name this $XDG_CONFIG_HOME/xkb/rules/evdev, but any
valid name is allowed.

The include functionally replaces the line with the content of the included
file which means the behavior of rules files is maintained. Specifically,
custom options must be defined before including another file because the first
match usually wins. In other words, the following ruleset will not assign
my_model as one would expect:

! include %S/evdev

! model                 =  symbols
  my_model              =  +custom(foo)

The default evdev ruleset has wildcards for model and those match before the
my_model is hit.

The actual resolved components need only be in one of the XKB lookup
directories, e.g. for the example above:

$ cat $XDG_CONFIG_HOME/xkb/symbols/custom
partial alphanumeric_keys
xkb_symbols "foo" {
    key <TLDE> {        [      VoidSymbol ]       };
};

partial alphanumeric_keys
xkb_symbols "baz" {
    key <AB01> {        [      k, K ]       };
};

This can then be loaded with the XKB option "custom:foo,custom:bar".

The use of "custom" is just as an example, there are no naming requirements
beyond avoiding already-used ones. Also note the bar/baz above - the option
names don't have to match the component names.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
meson.build
src/xkbcomp/rules.c
test/data/rules/inc-dst-loop-twice [new file with mode: 0644]
test/data/rules/inc-dst-simple [new file with mode: 0644]
test/data/rules/inc-src-before-after [new file with mode: 0644]
test/data/rules/inc-src-loop-twice [new file with mode: 0644]
test/data/rules/inc-src-looped [new file with mode: 0644]
test/data/rules/inc-src-nested [new file with mode: 0644]
test/data/rules/inc-src-options [new file with mode: 0644]
test/data/rules/inc-src-simple [new file with mode: 0644]
test/rules-file-includes.c [new file with mode: 0644]