doc: add keymap-format-text-v1.md to the HTML documentation
[platform/upstream/libxkbcommon.git] / doc / keymap-format-text-v1.md
1 # The XKB keymap text format, V1
2
3 This document describes the `XKB_KEYMAP_FORMAT_TEXT_V1` keymap format,
4 as implemented by libxkbcommon.
5
6 NOTE: This document is ever incomplete. Some additional resources are:
7
8 - [Ivan Pascal's XKB documentation](https://web.archive.org/web/20190724015820/http://pascal.tsu.ru/en/xkb/)
9 - [An Unreliable Guide to XKB Configuration](https://www.charvolant.org/doug/xkb/html/index.html)
10 - [ArchWiki XKB page](https://wiki.archlinux.org/index.php/X_keyboard_extension)
11
12 A keymap consists of a single top-level `xkb_keymap` block, underwhich
13 are nested the following sections.
14
15
16 ## The `xkb_keycodes` section
17
18 This is the simplest section type, and is the first one to be
19 compiled. The purpose of this is mostly to map between the
20 hardware/evdev scancodes and xkb keycodes. Each key is given a name
21 by which it can be referred to later, e.g. in the symbols section.
22
23 ### Keycode statements
24
25 Statements of the form:
26
27     <TLDE> = 49;
28     <AE01> = 10;
29
30 The above would let 49 and 10 be valid keycodes in the keymap, and
31 assign them the names `TLDE` and `AE01` respectively. The format
32 `<WXYZ>` is always used to refer to a key by name.
33
34 [The naming convention `<AE01>` just denotes the position of the key
35 in the main alphanumeric section of a standard QWERTY keyboard, with
36 the two letters specifying the row and the two digits specifying the
37 column, from the bottom left.]
38
39 In the common case this just maps to the evdev scancodes from
40 `/usr/include/linux/input.h`, e.g. the following definitions:
41
42      #define KEY_GRAVE            41
43      #define KEY_1                2
44
45 correspond to the ones above. Similar definitions appear in the
46 xf86-input-keyboard driver. Note that in all current keymaps there's a
47 constant offset of 8 (for historical reasons).
48
49 If there's a conflict, like the same name given to different keycodes,
50 or same keycode given different names, it is resolved according to the
51 merge mode which applies to the definitions.
52
53 ### Alias statements
54
55 Statements of the form:
56
57     alias <MENU> = <COMP>;
58
59 Allows to refer to a previously defined key (here `<COMP>`) by another
60 name (here `<MENU>`). Conflicts are handled similarly to keycode
61 statements.
62
63 ### LED name statements
64
65 Statements of the form:
66
67     indicator 1 = "Caps Lock";
68     indicator 2 = "Num Lock";
69     indicator 3 = "Scroll Lock";
70
71 Assigns a name to the keyboard LED (AKA indicator) with the given
72 index. The LED may be referred by this name later in the compat
73 section and by the user.
74
75
76 ## The `xkb_types` section
77
78 This section is the second to be processed, after `xkb_keycodes`.
79 However, it is completely independent and could have been the first to
80 be processed (it does not refer to specific keys as specified in the
81 `xkb_keycodes` section).
82
83 This section defines key types, which, given a key and a keyboard
84 state (i.e. modifier state and group), determine the shift level to be
85 used in translating the key to keysyms. These types are assigned to
86 each group in each key, in the `xkb_symbols` section.
87
88 Key types are called this way because, in a way, they really describe
89 the "type" of the key (or more correctly, a specific group of the
90 key). For example, an ordinary keymap will provide a type called
91 `KEYPAD`, which consists of two levels, with the second level being
92 chosen according to the state of the Num Lock (or Shift) modifiers.
93 Another example is a type called `ONE_LEVEL`, which is usually
94 assigned to keys such as Escape; these have just one level and are not
95 affected by the modifier state. Yet more common examples are
96 `TWO_LEVEL` (with Shift choosing the second level), `ALPHABETIC`
97 (where Caps Lock may also choose the second level), etc.
98
99 ### Type definitions
100
101 Statements of the form:
102
103     type "FOUR_LEVEL" { ... }
104
105 The above would create a new type named `FOUR_LEVEL`.
106 The body of the definition may include statements of the following
107 forms:
108
109 #### `level_name` statements
110
111     level_name[Level1] = "Base";
112
113 Mandatory for each level in the type.
114
115 Gives each level in this type a descriptive name. It isn't used
116 for anything.
117
118 Note: A level may be specified as Level[1-8] or just a number (can
119 be more than 8).
120
121 #### `modifiers` statement
122
123     modifiers = Shift+Lock+LevelThree;
124
125 Mandatory, should be specified only once.
126
127 A mask of real and virtual modifiers. These are the only modifiers
128 being considered when matching the modifier state against the type.
129 The other modifiers, whether active or not, are masked out in the
130 calculation.
131
132 #### `map` entry statements
133
134     map[Shift+LevelThree] = Level4;
135
136 Should have at least as many mappings as there are levels in the type.
137
138 If the active modifiers, masked with the type's modifiers (as stated
139 above), match (i.e. equal) the modifiers inside the `map[]` statement,
140 then the level in the right hand side is chosen. For example, in the
141 above, if in the current keyboard state the `Shift` and `LevelThree`
142 modifiers are active, while the `Lock` modifier is not, then the
143 keysym(s) in the 4th level of the group will be returned to the user.
144
145 #### `preserve` statements
146
147     map[Shift+Lock+LevelThree] = Level5;
148     preserve[Shift+Lock+LevelThree] = Lock;
149
150 When a key type is used for keysym translation, its modifiers are said
151 to be "consumed". For example, in a simple US keymap, the "g" "g" key
152 is assigned an ordinary `ALPHABETIC` key type, whose modifiers are
153 Shift and Lock; then for the "g" key, these two modifiers are consumed
154 by the translation. This information is relevant for applications
155 which further process the modifiers, since by then the consumed
156 modifiers have already "done their part" and should be masked out.
157
158 However, sometimes even if a modifier had already affected the key
159 translation through the type, it should *not* be reported as consumed,
160 for various reasons. In this case, a `preserve[]` statement can be
161 used to augment the map entry. The modifiers inside the square
162 brackets should match one of the map[] statements in the type (if
163 there is no matching map entry, one mapping to Level1 is implicitly
164 added). The right hand side should consists of modifiers from the
165 type's modifiers; these modifiers are then "preserved" and not
166 reported as consumed.
167
168
169 ## The `xkb_compat` section
170
171 This section is the third to be processed, after `xkb_keycodes` and
172 `xkb_types`.
173
174 ### Interpret statements
175
176 Statements of the form:
177
178     interpret Num_Lock+Any { ... }
179     interpret Shift_Lock+AnyOf(Shift+Lock) { ... }
180
181 The `xkb_symbols` section (see below) allows the keymap author to
182 perform, among other things, the following things for each key:
183
184 - Bind an action, like SetMods or LockGroup, to the key. Actions, like
185   symbols, are specified for each level of each group in the key
186   separately.
187
188 - Add a virtual modifier to the key's virtual modifier mapping
189   (vmodmap).
190
191 - Specify whether the key should repeat or not.
192
193 However, doing this for each key (or level) is tedious and inflexible.
194 Interpret's are a mechanism to apply these settings to a bunch of
195 keys/levels at once.
196
197 Each interpret specifies a condition by which it attaches to certain
198 levels. The condition consists of two parts:
199
200 - A keysym. If the level has a different (or more than one) keysym,
201   the match fails. Leaving out the keysym is equivalent to using the
202   `NoSymbol` keysym, which always matches successfully.
203
204 - A modifier predicate. The predicate consists of a matching operation
205   and a mask of (real) modifiers. The modifiers are matched against
206   the key's modifier map (modmap). The matching operation can be one
207   of the following:
208
209   * `AnyOfOrNone` - The modmap must either be empty or include at
210     least one of the specified modifiers.
211   * `AnyOf` - The modmap must include at least one of the specified
212     modifiers.
213   * `NoneOf` - The modmap must not include any of the specified
214     modifiers.
215   * `AllOf` - The modmap must include all of the specified modifiers
216     (but may include others as well).
217   * `Exactly` - The modmap must be exactly the same as the specified
218     modifiers.
219
220   Leaving out the predicate is equivalent to using `AnyOfOrNone` while
221   specifying all modifiers. Leaving out just the matching condition is
222   equivalent to using `Exactly`.
223
224 An interpret may also include `useModMapMods = level1;` - see below.
225
226 If a level fulfils the conditions of several interprets, only the
227 most specific one is used:
228
229 - A specific keysym will always match before a generic `NoSymbol`
230   condition.
231
232 - If the keysyms are the same, the interpret with the more specific
233   matching operation is used. The above list is sorted from least to
234   most specific.
235
236 - If both the keysyms and the matching operations are the same (but the
237   modifiers are different), the first interpret is used.
238
239 As described above, once an interpret "attaches" to a level, it can bind
240 an action to that level, add one virtual modifier to the key's vmodmap,
241 or set the key's repeat setting. You should note the following:
242
243 - The key repeat is a property of the entire key; it is not
244   level-specific. In order to avoid confusion, it is only inspected
245   for the first level of the first group; the interpret's repeat
246   setting is ignored when applied to other levels.
247
248 - If one of the above fields was set directly for a key in
249   `xkb_symbols`, the explicit setting takes precedence over the
250   interpret.
251
252 The body of the statement may include statements of the following
253 forms (all of which are optional):
254
255 #### `useModMapMods` statement
256
257     useModMapMods = level1;
258
259 When set to `level1`, the interpret will only match levels which are
260 the first level of the first group of the keys. This can be useful in
261 conjunction with e.g. a `virtualModifier` statement.
262
263 #### `action` statement
264
265     action = LockMods(modifiers=NumLock);
266
267 Bind this action to the matching levels.
268
269 #### `virtualModifier` statement
270
271     virtualModifier = NumLock;
272
273 Add this virtual modifier to the key's vmodmap. The given virtual
274 modifier must be declared at the top level of the file with a
275 `virtual_modifiers` statement, e.g.:
276
277     virtual_modifiers NumLock;
278
279 #### `repeat` statement
280
281     repeat = True;
282
283 Set whether the key should repeat or not. Must be a boolean value.
284
285 ### LED map statements
286
287 Statements of the form:
288
289     indicator "Shift Lock" { ... }
290
291 This statement specifies the behavior and binding of the LED (AKA
292 indicator) with the given name ("Shift Lock" above). The name should
293 have been declared previously in the `xkb_keycodes` section (see LED
294 name statement), and given an index there. If it wasn't, it is created
295 with the next free index.
296
297 The body of the statement describes the conditions of the keyboard
298 state which will cause the LED to be lit. It may include the following
299 statements:
300
301 #### `modifiers` statement
302
303     modifiers = ScrollLock;
304
305 If the given modifiers are in the required state (see below), the
306 LED is lit.
307
308 #### `whichModState` statement
309
310     whichModState = Latched+Locked;
311
312 Can be any combination of:
313
314 * `base`, `latched`, `locked`, `effective`
315 * `any` (i.e. all of the above)
316 * `none` (i.e. none of the above)
317 * `compat` (legacy value, treated as effective)
318
319 This will cause the respective portion of the modifier state (see
320 `struct xkb_state`) to be matched against the modifiers given in the
321 `modifiers` statement.
322
323 Here's a simple example:
324
325 indicator "Num Lock" {
326     modifiers = NumLock;
327     whichModState = Locked;
328 };
329
330 Whenever the NumLock modifier is locked, the Num Lock LED will light
331 up.
332
333 #### `groups` statement
334
335     groups = All - group1;
336
337 If the given groups are in the required state (see below), the LED is
338 lit.
339
340 #### `whichGroupState` statement
341
342     whichGroupState = Effective;
343
344 Can be any combination of:
345
346 * `base`, `latched`, `locked`, `effective`
347 * `any` (i.e. all of the above)
348 * `none` (i.e. none of the above)
349
350 This will cause the respective portion of the group state (see
351 `struct xkb_state`) to be matched against the groups given in the
352 `groups` statement.
353
354 Note: the above conditions are disjunctive, i.e. if any of them are
355 satisfied the LED is lit.
356
357
358 ## The `xkb_symbols` section
359
360 NOTE: The documentation of this section is incomplete.
361
362 This section is the fourth to be processed, after `xkb_keycodes`, `xkb_types`
363 and `xkb_compat`.
364
365 Statements of the form:
366
367     xkb_symbols "basic" {
368         ...
369     }
370
371 Declare a symbols map named `basic`. Statements inside the curly braces only
372 affect the symbols map.
373
374 A map can have various flags applied to it above the statement, separated by
375 whitespace:
376
377     partial alphanumeric_keys
378     xkb_symbols "basic" {
379         ...
380     }
381
382 The possible flags are:
383
384   * `partial` - Indicates that the map doesn't cover a complete keyboard.
385   * `default` - Marks the symbol map as the default map in the file when no
386     explicit map is specified. If no map is marked as a default, the first map
387     in the file is the default.
388   * `hidden` - Variant that can only be used internally
389   * `alphanumeric_keys` - Indicates that the map contains alphanumeric keys
390   * `modifier_keys` - Indicates that the map contains modifier keys
391   * `keypad_keys` - Indicates that the map contains keypad keys
392   * `function_keys` - Indicates that the map contains function keys
393   * `alternate_group` - Indicates that the map contains keys for an alternate
394     group
395
396 If no `*_keys` flags are supplied, then the map is assumed to cover a complete
397 keyboard.
398
399 At present, except for `default`, none of the flags affect key processing in
400 libxkbcommon, and only serve as metadata.
401
402 ### Name statements
403
404 Statements of the form:
405
406     name[Group1] = "US/ASCII";
407     groupName[1] = "US/ASCII";
408
409 Gives the name "US/ASCII" to the first group of symbols. Other groups can be
410 named using a different group index (ex: `Group2`), and with a different name.
411 A group must be named.
412
413 `group` and `groupName` mean the same thing, and the `Group` in `Group1` is
414 optional.
415
416 ### Include statements
417
418 Statements of the form:
419
420     include "nokia_vndr/rx-51(nordic_base)
421
422 Will include data from another `xkb_symbols` section, possibly located in
423 another file. Here it would include the `xkb_symbols` section called
424 `nordic_base`, from the file `rx-51` located in the `nokia_vndr` folder, itself
425 located in an XKB include path.
426
427 ### Key statement
428
429 Statements of the form:
430
431     key <AD01> { [ q, Q ] };
432
433 Describes the mapping of a keycode `<AD01>` to a given group of symbols. The
434 possible keycodes are the keycodes defined in the `xkb_keycodes` section.
435
436 Symbols are named using the symbolic names from the
437 `xkbcommon/xkbcommon-keysyms.h` file. A group of symbols is enclosed in brackets
438 and separated by commas. Each element of the symbol arrays corresponds to a
439 different modifier level. In this example, the symbol (keysym) `XKB_KEY_q` for
440 level 1 and `XKB_KEY_Q` for level 2.
441
442 #### Groups
443
444 Each group represents a list of symbols mapped to a keycode:
445
446     name[Group1]= "US/ASCII";
447     name[Group2]= "Russian";
448     ...
449     key <AD01> { [ q, Q ],
450                  [ Cyrillic_shorti, Cyrillic_SHORTI ] };
451
452 A long-form syntax can also be used:
453
454     key <AD01> {
455         symbols[Group1]= [ q, Q ],
456         symbols[Group2]= [ Cyrillic_shorti, Cyrillic_SHORTI ]
457     };
458
459 Groups can also be omitted, but the brackets must be present. The following
460 statement only defines the Group3 of a mapping:
461
462     key <AD01> { [], [], [ q, Q ] };
463
464 ## Virtual modifier statements
465
466 Statements of the form:
467
468     virtual_modifiers LControl;
469
470 Can appear in the `xkb_types`, `xkb_compat`, `xkb_symbols` sections.
471
472 TODO