xref: /aosp_15_r20/external/libxkbcommon/doc/user-configuration.md (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker# User-configuration
2*2b949d04SAndroid Build Coastguard Worker
3*2b949d04SAndroid Build Coastguard WorkerThis page describes how to add a custom layout or option so that it will be
4*2b949d04SAndroid Build Coastguard Workerparsed by libxkbcommon.
5*2b949d04SAndroid Build Coastguard Worker
6*2b949d04SAndroid Build Coastguard Worker**The below requires libxkbcommon as keymap compiler and does not work in X**.
7*2b949d04SAndroid Build Coastguard Worker
8*2b949d04SAndroid Build Coastguard Worker## Data locations
9*2b949d04SAndroid Build Coastguard Worker
10*2b949d04SAndroid Build Coastguard Workerlibxkbcommon searches the following paths for XKB configuration files:
11*2b949d04SAndroid Build Coastguard Worker- `$XDG_CONFIG_HOME/xkb/`, or `$HOME/.config/xkb/` if the `$XDG_CONFIG_HOME`
12*2b949d04SAndroid Build Coastguard Worker  environment variable is not defined
13*2b949d04SAndroid Build Coastguard Worker- `$HOME/.xkb/`
14*2b949d04SAndroid Build Coastguard Worker- `$XKB_CONFIG_EXTRA_PATH` if set, otherswise `<sysconfdir>/xkb` (on most
15*2b949d04SAndroid Build Coastguard Worker  distributions this is `/etc/xkb`)
16*2b949d04SAndroid Build Coastguard Worker- `$XKB_CONFIG_ROOT` if set, otherwise `<datadir>/X11/xkb/` (path defined by the
17*2b949d04SAndroid Build Coastguard Worker  `xkeyboard-config` package, on most distributions this is
18*2b949d04SAndroid Build Coastguard Worker  `/usr/share/X11/xkb`)
19*2b949d04SAndroid Build Coastguard Worker
20*2b949d04SAndroid Build Coastguard WorkerA keymap created with `xkb_keymap_new_from_names()` will look up those paths in
21*2b949d04SAndroid Build Coastguard Workerorder until the required data is found.
22*2b949d04SAndroid Build Coastguard Worker
23*2b949d04SAndroid Build Coastguard Worker**Note: Where libxkbcommon runs in a privileged context, only the system
24*2b949d04SAndroid Build Coastguard Worker(datadir) path is available.**
25*2b949d04SAndroid Build Coastguard Worker
26*2b949d04SAndroid Build Coastguard WorkerEach directory should have one or more of the following subdirectories:
27*2b949d04SAndroid Build Coastguard Worker- `compat`
28*2b949d04SAndroid Build Coastguard Worker- `geometry` (libxkbcommon ignores this directory)
29*2b949d04SAndroid Build Coastguard Worker- `keycodes`
30*2b949d04SAndroid Build Coastguard Worker- `rules`
31*2b949d04SAndroid Build Coastguard Worker- `symbols`
32*2b949d04SAndroid Build Coastguard Worker- `types`
33*2b949d04SAndroid Build Coastguard Worker
34*2b949d04SAndroid Build Coastguard WorkerThe majority of user-specific configuration involve modifying key symbols and
35*2b949d04SAndroid Build Coastguard Workerthis is what this document focuses on. For use-cases where a user may need to
36*2b949d04SAndroid Build Coastguard Workeradd new key types or compat entries the general approach remains the same. A
37*2b949d04SAndroid Build Coastguard Workerdetailed description for how to add those types or compat entries is out of
38*2b949d04SAndroid Build Coastguard Workerscope for this document.
39*2b949d04SAndroid Build Coastguard Worker
40*2b949d04SAndroid Build Coastguard WorkerYou should never need to add user-specific keycodes. Where a keycode is missing,
41*2b949d04SAndroid Build Coastguard Workerthe addition should be filed in the upstream xkeyboard-config project.
42*2b949d04SAndroid Build Coastguard Worker
43*2b949d04SAndroid Build Coastguard Worker## RMLVO vs KcCGST
44*2b949d04SAndroid Build Coastguard Worker
45*2b949d04SAndroid Build Coastguard WorkerDue to how XKB is configured, there is no such thing as a "layout" in XKB
46*2b949d04SAndroid Build Coastguard Workeritself, or, indeed, any of the rules, models, variant, options (RMLVO) decribed
47*2b949d04SAndroid Build Coastguard Workerin `struct xkb_rule_names`. RMLVO names are merely lookup keys in the
48*2b949d04SAndroid Build Coastguard Workerrules file provided by xkeyboard-config to map to the correct keycode, compat,
49*2b949d04SAndroid Build Coastguard Workergeometry (ignored by libxkbcommon), symbols and types (KcCGST). The KcCGST data
50*2b949d04SAndroid Build Coastguard Workeris the one used by XKB and libxbkcommon to map keys to actual symbols.
51*2b949d04SAndroid Build Coastguard Worker
52*2b949d04SAndroid Build Coastguard WorkerFor example, a common RMLVO configuration is layout "us", variant "dvorak" and
53*2b949d04SAndroid Build Coastguard Workeroption "terminate:ctrl_alt_bksp". Using the default rules file and model
54*2b949d04SAndroid Build Coastguard Workerthis maps into the following KcCGST components:
55*2b949d04SAndroid Build Coastguard Worker
56*2b949d04SAndroid Build Coastguard Worker```
57*2b949d04SAndroid Build Coastguard Workerxkb_keymap {
58*2b949d04SAndroid Build Coastguard Worker	xkb_keycodes  { include "evdev+aliases(qwerty)"	};
59*2b949d04SAndroid Build Coastguard Worker	xkb_types     { include "complete"	};
60*2b949d04SAndroid Build Coastguard Worker	xkb_compat    { include "complete"	};
61*2b949d04SAndroid Build Coastguard Worker	xkb_symbols   { include "pc+us(dvorak)+inet(evdev)+terminate(ctrl_alt_bksp)"	};
62*2b949d04SAndroid Build Coastguard Worker	xkb_geometry  { include "pc(pc105)"	};
63*2b949d04SAndroid Build Coastguard Worker};
64*2b949d04SAndroid Build Coastguard Worker```
65*2b949d04SAndroid Build Coastguard Worker
66*2b949d04SAndroid Build Coastguard WorkerA detailed explanation of how rules files convert RMLVO to KcCGST is out of
67*2b949d04SAndroid Build Coastguard Workerscope for this document. See [the rules file](md_doc_rules-format.html) page
68*2b949d04SAndroid Build Coastguard Workerinstead.
69*2b949d04SAndroid Build Coastguard Worker
70*2b949d04SAndroid Build Coastguard Worker
71*2b949d04SAndroid Build Coastguard Worker## Adding a layout
72*2b949d04SAndroid Build Coastguard Worker
73*2b949d04SAndroid Build Coastguard WorkerAdding a layout requires that the user adds **symbols** in the correct location.
74*2b949d04SAndroid Build Coastguard Worker
75*2b949d04SAndroid Build Coastguard WorkerThe default rules files (usually `evdev`) have a catch-all to map a layout, say
76*2b949d04SAndroid Build Coastguard Worker"foo", and a variant, say "bar", into the "bar" section in the file
77*2b949d04SAndroid Build Coastguard Worker`$xkb_base_dir/symbols/foo`.
78*2b949d04SAndroid Build Coastguard WorkerThis is sufficient to define a new keyboard layout. The example below defines
79*2b949d04SAndroid Build Coastguard Workerthe keyboard layout "banana" with an optional variant "orange"
80*2b949d04SAndroid Build Coastguard Worker
81*2b949d04SAndroid Build Coastguard Worker```
82*2b949d04SAndroid Build Coastguard Worker$ cat $XDG_CONFIG_HOME/xkb/symbols/banana
83*2b949d04SAndroid Build Coastguard Worker// Like a US layout but swap the top row so numbers are on Shift
84*2b949d04SAndroid Build Coastguard Workerdefault partial alphanumeric_keys
85*2b949d04SAndroid Build Coastguard Workerxkb_symbols "basic" {
86*2b949d04SAndroid Build Coastguard Worker    include "us(basic)"
87*2b949d04SAndroid Build Coastguard Worker    name[Group1]= "Banana (US)";
88*2b949d04SAndroid Build Coastguard Worker
89*2b949d04SAndroid Build Coastguard Worker    key <AE01> { [ exclam,          1]     };
90*2b949d04SAndroid Build Coastguard Worker    key <AE02> { [ at,              2]     };
91*2b949d04SAndroid Build Coastguard Worker    key <AE03> { [ numbersign,      3]     };
92*2b949d04SAndroid Build Coastguard Worker    key <AE04> { [ dollar,          4]     };
93*2b949d04SAndroid Build Coastguard Worker    key <AE05> { [ percent,         5]     };
94*2b949d04SAndroid Build Coastguard Worker    key <AE06> { [ asciicircum,     6]     };
95*2b949d04SAndroid Build Coastguard Worker    key <AE07> { [ ampersand,       7]     };
96*2b949d04SAndroid Build Coastguard Worker    key <AE08> { [ asterisk,        8]     };
97*2b949d04SAndroid Build Coastguard Worker    key <AE09> { [ parenleft,       9]     };
98*2b949d04SAndroid Build Coastguard Worker    key <AE10> { [ parenright,      0]     };
99*2b949d04SAndroid Build Coastguard Worker    key <AE11> { [ underscore,      minus] };
100*2b949d04SAndroid Build Coastguard Worker    key <AE12> { [ plus,            equal] };
101*2b949d04SAndroid Build Coastguard Worker};
102*2b949d04SAndroid Build Coastguard Worker
103*2b949d04SAndroid Build Coastguard Worker// Same as banana but map the euro sign to the 5 key
104*2b949d04SAndroid Build Coastguard Workerpartial alphanumeric_keys
105*2b949d04SAndroid Build Coastguard Workerxkb_symbols "orange" {
106*2b949d04SAndroid Build Coastguard Worker    include "banana(basic)"
107*2b949d04SAndroid Build Coastguard Worker    name[Group1] = "Banana (Eurosign on 5)";
108*2b949d04SAndroid Build Coastguard Worker    include "eurosign(5)"
109*2b949d04SAndroid Build Coastguard Worker};
110*2b949d04SAndroid Build Coastguard Worker```
111*2b949d04SAndroid Build Coastguard Worker
112*2b949d04SAndroid Build Coastguard WorkerThe `default` section is loaded when no variant is given. The first example
113*2b949d04SAndroid Build Coastguard Workersections uses ``include`` to populate with a symbols list defined elsewhere
114*2b949d04SAndroid Build Coastguard Worker(here: section `basic` from the file `symbols/us`, aka. the default US keyboard
115*2b949d04SAndroid Build Coastguard Workerlayout) and overrides parts of these
116*2b949d04SAndroid Build Coastguard Workersymbols. The effect of this section is to swap the numbers and symbols in the
117*2b949d04SAndroid Build Coastguard Workertop-most row (compared to the US layout) but otherwise use the US layout.
118*2b949d04SAndroid Build Coastguard Worker
119*2b949d04SAndroid Build Coastguard WorkerThe "orange" variant uses the "banana" symbols and includes a different section
120*2b949d04SAndroid Build Coastguard Workerto define the eurosign. It does not specificially override any symbols.
121*2b949d04SAndroid Build Coastguard Worker
122*2b949d04SAndroid Build Coastguard WorkerThe exact details of how `xkb_symbols` work is out of scope for this document.
123*2b949d04SAndroid Build Coastguard Worker
124*2b949d04SAndroid Build Coastguard Worker## Adding an option
125*2b949d04SAndroid Build Coastguard Worker
126*2b949d04SAndroid Build Coastguard WorkerFor technical reasons, options do **not** have a catch-all to map option names
127*2b949d04SAndroid Build Coastguard Workerto files and sections and must be specifically mapped by the user. This requires
128*2b949d04SAndroid Build Coastguard Workera custom rules file. As the `evdev` ruleset is hardcoded in many clients, the
129*2b949d04SAndroid Build Coastguard Workercustom rules file must usually be named `evdev`.
130*2b949d04SAndroid Build Coastguard Worker
131*2b949d04SAndroid Build Coastguard Worker```
132*2b949d04SAndroid Build Coastguard Worker$ cat $XDG_CONFIG_HOME/xkb/rules/evdev
133*2b949d04SAndroid Build Coastguard Worker! option = symbols
134*2b949d04SAndroid Build Coastguard Worker  custom:foo    = +custom(bar)
135*2b949d04SAndroid Build Coastguard Worker  custom:baz    = +other(baz)
136*2b949d04SAndroid Build Coastguard Worker
137*2b949d04SAndroid Build Coastguard Worker! include %S/evdev
138*2b949d04SAndroid Build Coastguard Worker```
139*2b949d04SAndroid Build Coastguard Worker
140*2b949d04SAndroid Build Coastguard WorkerThis rules file maps the RMLVO option "custom:foo" to the "bar" section in the
141*2b949d04SAndroid Build Coastguard Worker`symbols/custom` file and the "custom:baz" option to the "baz" section in the
142*2b949d04SAndroid Build Coastguard Worker`symbols/other` file. Note how the RMLVO option name may be different to the
143*2b949d04SAndroid Build Coastguard Workerfile or section name.
144*2b949d04SAndroid Build Coastguard Worker
145*2b949d04SAndroid Build Coastguard WorkerThe `include` statement includes the system-provided `evdev` ruleset. This
146*2b949d04SAndroid Build Coastguard Workerallows users to only override those options they need.
147*2b949d04SAndroid Build Coastguard Worker
148*2b949d04SAndroid Build Coastguard WorkerThe files themselves are similar to the layout examples in the previous section:
149*2b949d04SAndroid Build Coastguard Worker
150*2b949d04SAndroid Build Coastguard Worker```
151*2b949d04SAndroid Build Coastguard Worker$ cat $XDG_CONFIG_HOME/xkb/symbols/custom
152*2b949d04SAndroid Build Coastguard Worker// map the Tilde key to nothing on the first shift level
153*2b949d04SAndroid Build Coastguard Workerpartial alphanumeric_keys
154*2b949d04SAndroid Build Coastguard Workerxkb_symbols "bar" {
155*2b949d04SAndroid Build Coastguard Worker    key <TLDE> {        [      VoidSymbol ]       };
156*2b949d04SAndroid Build Coastguard Worker};
157*2b949d04SAndroid Build Coastguard Worker
158*2b949d04SAndroid Build Coastguard Worker$ cat $XDG_CONFIG_HOME/xkb/symbols/other
159*2b949d04SAndroid Build Coastguard Worker// map first key in bottom row (Z in the US layout) to k/K
160*2b949d04SAndroid Build Coastguard Workerpartial alphanumeric_keys
161*2b949d04SAndroid Build Coastguard Workerxkb_symbols "baz" {
162*2b949d04SAndroid Build Coastguard Worker    key <AB01> {        [      k, K ]       };
163*2b949d04SAndroid Build Coastguard Worker};
164*2b949d04SAndroid Build Coastguard Worker```
165*2b949d04SAndroid Build Coastguard Worker
166*2b949d04SAndroid Build Coastguard WorkerWith these in place, a user may select any layout/variant together with
167*2b949d04SAndroid Build Coastguard Workerthe "custom:foo" and/or "custom:baz" options.
168*2b949d04SAndroid Build Coastguard Worker
169*2b949d04SAndroid Build Coastguard Worker## Discoverable layouts
170*2b949d04SAndroid Build Coastguard Worker
171*2b949d04SAndroid Build Coastguard Worker**The below requires libxkbregistry as XKB lookup tool and does not work where
172*2b949d04SAndroid Build Coastguard Workerclients parse the XML file directly**.
173*2b949d04SAndroid Build Coastguard Worker
174*2b949d04SAndroid Build Coastguard WorkerThe above sections apply only to the data files and require that the user knows
175*2b949d04SAndroid Build Coastguard Workerabout the existence of the new entries. To make custom entries discoverable by
176*2b949d04SAndroid Build Coastguard Workerthe configuration tools (e.g. the GNOME Control Center), the new entries must
177*2b949d04SAndroid Build Coastguard Workeralso be added to the XML file that is parsed by libxkbregistry. In most cases,
178*2b949d04SAndroid Build Coastguard Workerthis is the `evdev.xml` file in the rules directory. The example below shows the
179*2b949d04SAndroid Build Coastguard WorkerXML file that would add the custom layout and custom options as outlined above
180*2b949d04SAndroid Build Coastguard Workerto the XKB registry:
181*2b949d04SAndroid Build Coastguard Worker
182*2b949d04SAndroid Build Coastguard Worker```
183*2b949d04SAndroid Build Coastguard Worker$ cat $XDG_CONFIG_HOME/xkb/rules/evdev.xml
184*2b949d04SAndroid Build Coastguard Worker<?xml version="1.0" encoding="UTF-8"?>
185*2b949d04SAndroid Build Coastguard Worker<!DOCTYPE xkbConfigRegistry SYSTEM "xkb.dtd">
186*2b949d04SAndroid Build Coastguard Worker<xkbConfigRegistry version="1.1">
187*2b949d04SAndroid Build Coastguard Worker  <layoutList>
188*2b949d04SAndroid Build Coastguard Worker    <layout>
189*2b949d04SAndroid Build Coastguard Worker      <configItem>
190*2b949d04SAndroid Build Coastguard Worker        <name>banana</name>
191*2b949d04SAndroid Build Coastguard Worker        <shortDescription>ban</shortDescription>
192*2b949d04SAndroid Build Coastguard Worker        <description>Banana</description>
193*2b949d04SAndroid Build Coastguard Worker      </configItem>
194*2b949d04SAndroid Build Coastguard Worker      <variantList>
195*2b949d04SAndroid Build Coastguard Worker        <variant>
196*2b949d04SAndroid Build Coastguard Worker          <configItem>
197*2b949d04SAndroid Build Coastguard Worker            <name>orange</name>
198*2b949d04SAndroid Build Coastguard Worker            <shortDescription>or</shortDescription>
199*2b949d04SAndroid Build Coastguard Worker            <description>Orange (Banana)</description>
200*2b949d04SAndroid Build Coastguard Worker          </configItem>
201*2b949d04SAndroid Build Coastguard Worker        </variant>
202*2b949d04SAndroid Build Coastguard Worker      </variantList>
203*2b949d04SAndroid Build Coastguard Worker    </layout>
204*2b949d04SAndroid Build Coastguard Worker  </layoutList>
205*2b949d04SAndroid Build Coastguard Worker  <optionList>
206*2b949d04SAndroid Build Coastguard Worker    <group allowMultipleSelection="true">
207*2b949d04SAndroid Build Coastguard Worker      <configItem>
208*2b949d04SAndroid Build Coastguard Worker        <name>custom</name>
209*2b949d04SAndroid Build Coastguard Worker        <description>Custom options</description>
210*2b949d04SAndroid Build Coastguard Worker      </configItem>
211*2b949d04SAndroid Build Coastguard Worker      <option>
212*2b949d04SAndroid Build Coastguard Worker      <configItem>
213*2b949d04SAndroid Build Coastguard Worker        <name>custom:foo</name>
214*2b949d04SAndroid Build Coastguard Worker        <description>Map Tilde to nothing</description>
215*2b949d04SAndroid Build Coastguard Worker      </configItem>
216*2b949d04SAndroid Build Coastguard Worker      </option>
217*2b949d04SAndroid Build Coastguard Worker      <option>
218*2b949d04SAndroid Build Coastguard Worker      <configItem>
219*2b949d04SAndroid Build Coastguard Worker        <name>custom:baz</name>
220*2b949d04SAndroid Build Coastguard Worker        <description>Map Z to K</description>
221*2b949d04SAndroid Build Coastguard Worker      </configItem>
222*2b949d04SAndroid Build Coastguard Worker      </option>
223*2b949d04SAndroid Build Coastguard Worker    </group>
224*2b949d04SAndroid Build Coastguard Worker  </optionList>
225*2b949d04SAndroid Build Coastguard Worker</xkbConfigRegistry>
226*2b949d04SAndroid Build Coastguard Worker```
227*2b949d04SAndroid Build Coastguard Worker
228*2b949d04SAndroid Build Coastguard WorkerThe default behavior of libxkbregistry ensures that the new layout and options
229*2b949d04SAndroid Build Coastguard Workerare added to the system-provided layouts and options.
230*2b949d04SAndroid Build Coastguard Worker
231*2b949d04SAndroid Build Coastguard WorkerFor details on the XML format, see DTD in `<datadir>/X11/xkb/rules/xkb.dtd`
232*2b949d04SAndroid Build Coastguard Workerand the system-provided XML files in `<datadir>/X11/xkb/rulies/xkb.dtd`.
233