146185892Smrg
246185892Smrg                      How to enhance XKB configuration
3154daed1Smrg
4154daed1Smrg                         Kamil Toman, Ivan U. Pascal
5154daed1Smrg
6154daed1Smrg                              25 November 2002
7154daed1Smrg
8154daed1Smrg                                  Abstract
9154daed1Smrg
1046185892Smrg     This guide is aimed at alleviating one's labour when creating a
1146185892Smrg     new (internationalized) keyboard layout. Unlike other documents,
1246185892Smrg     this guide emphasizes the keymap developer's point of view.
1346185892Smrg
14154daed1Smrg
15154daed1Smrg1.  Overview
16154daed1Smrg
1746185892SmrgThe developer of a new layout should read the XKB protocol specification
1846185892Smrg(The X Keyboard Extension: Protocol Specification [1]) at least to clarify
1946185892Smrgfor themselves some XKB-specific terms used in this document and elsewhere
2046185892Smrgin XKB configuration. It is also wise to understand how the X server and
2146185892Smrga client digest their keyboard inputs (with and without XKB).
2246185892Smrg
2346185892SmrgAnother useful source is Ivan Pascal's text about XKB configuration [2].
24154daed1Smrg
2546185892Smrg   [1] https://www.x.org/docs/XKB/XKBproto.pdf
2646185892Smrg   [2] http://pascal.tsu.ru/en/xkb/
27154daed1Smrg
28154daed1SmrgNote that this document covers only enhancements which are to be made to
2946185892SmrgXFree86 versions 4.3.x and newer.
3046185892Smrg
31154daed1Smrg
32154daed1Smrg2.  The Basics
33154daed1Smrg
3446185892SmrgAt boottime (or later at the user's command) the X server starts its xkb
3546185892Smrgkeyboard extension module and reads data from a compiled configuration file.
36154daed1Smrg
37154daed1SmrgThis compiled configuration file is prepared by the program xkbcomp which
3846185892Smrgbehaves altogether as an ordinary compiler (see man xkbcomp). Its input are
3946185892Smrghuman-readable xkb configuration files which are verified and then composed
40154daed1Smrginto a useful xkb configuration. Users don't need to mess with xkbcomp them-
4146185892Smrgselves, for them it is invisible. Usually, it is run upon X server startup.
42154daed1Smrg
4346185892SmrgAs you probably already know, XKB configuration consists of five main
44154daed1Smrgmodules:
45154daed1Smrg
46154daed1Smrg      Keycodes
4746185892Smrg            Tables that define the translation from keyboard scan codes into
4846185892Smrg            reasonably symbolic names, maximum and minimum valid keycodes,
4946185892Smrg            symbolic aliases, and a description of physically present LED-indica-
5046185892Smrg            tors. The primary sense of this component is to allow definitions
51154daed1Smrg            of maps of symbols (see below) to be independent of physical key-
5246185892Smrg            board scancodes. There are two main conventions for symbolic
5346185892Smrg            names (always four bytes long):
54154daed1Smrg
5546185892Smrg               o  names which express some traditional meaning, like <SPCE>
5646185892Smrg                  (which stands for space bar)
57154daed1Smrg
5846185892Smrg               o  names which express a relative position on the keyboard,
5946185892Smrg                  for example <AE01> (the exclamation mark on US keyboards),
6046185892Smrg                  with on its right the keys <AE02>, <AE03>, etc.
61154daed1Smrg
62154daed1Smrg      Types
6346185892Smrg            Types describe how the pressed key is affected by active modifiers
6446185892Smrg            (like Shift, Control, Alt, ...). There are several predefined
6546185892Smrg            types which cover most of the usual combinations.
66154daed1Smrg
67154daed1Smrg      Compat
6846185892Smrg            The compatibility component defines the internal behaviour of
6946185892Smrg            modifiers. Using the compat component you can assign various
7046185892Smrg            actions (elaborately described in the XKB specification) to key
7146185892Smrg            events. This is also the place where LED-indicators behaviour
7246185892Smrg            is defined.
73154daed1Smrg
74154daed1Smrg      Symbols
75154daed1Smrg            For i18n purposes, this is the most important table. It defines
76154daed1Smrg            what values (=symbols) are assigned to what keycodes (represented
7746185892Smrg            by their symbolic name, see above). More than one value may be
7846185892Smrg            defined for each key and then it depends on the key type and on
7946185892Smrg            the modifiers state (respective compat component) which value
8046185892Smrg            will be the resulting one when the key is pressed.
81154daed1Smrg
82154daed1Smrg      Geometry
83154daed1Smrg            Geometry files aren't used by xkb itself but they may be used by
84154daed1Smrg            some external programs to depict a keyboard image.
85154daed1Smrg
8646185892SmrgAll these components have their files located in the xkb configuration tree,
8746185892Smrgin subdirectories with the same name (usually in /usr/share/X11/xkb).
8846185892Smrg
89154daed1Smrg
9046185892Smrg3.  Enhancing the XKB Configuration
91154daed1Smrg
9246185892SmrgMost of XKB enhancements are about a need to define new output symbols for
93154daed1Smrgsome input key events. In other words, a need to define a new symbol map (for
9446185892Smrga new language, or standard, or just to feel more comfortable when typing text).
95154daed1Smrg
9646185892SmrgWhat do you need to do? Generally, you have to define the following things:
97154daed1Smrg
98154daed1Smrg   o  the map of symbols itself
99154daed1Smrg
100154daed1Smrg   o  the rules to allow users to select the new mapping
101154daed1Smrg
102154daed1Smrg   o  the description of the new layout
103154daed1Smrg
104154daed1SmrgFirst of all, it is good to go through existing layouts and to examine them
10546185892Smrgto see if there is something you could easily adjust to fit your needs. Even
10646185892Smrgif there is nothing similar, you may get some ideas about the basic concepts
10746185892Smrgand used tricks.
108154daed1Smrg
10946185892Smrg3.1  Levels and Groups
110154daed1Smrg
11146185892SmrgSince XFree86 4.3.0, you can use multiple layouts in the xkb configuration.
11246185892SmrgThough still within the boundaries of the xkb protocol and its general ideas,
11346185892Smrgthe keymap designer must obey new rules when creating new maps. In exchange
11446185892Smrgwe get a more powerful and cleaner configuration system.
115154daed1Smrg
116154daed1SmrgRemember that it is the application which must decide which symbol matches
11746185892Smrgwhich keycode according to the effective modifier state. The X server itself
11846185892Smrgsends only an input event message. Of course, usually the interpretation is
11946185892Smrgdone by Xlib, Xaw, Motif, Qt, Gtk, or similar libraries. The X server only
12046185892Smrgsupplies its mapping table (usually upon application startup).
121154daed1Smrg
12246185892SmrgYou can think of the X server's symbol table as of an irregular table where
123154daed1Smrgeach keycode has its row and where each combination of modifiers determines
124154daed1Smrgexactly one column. The resulting cell then gives the proper symbolic value.
12546185892SmrgNot all keycodes need to bind different values for different combinations of
12646185892Smrgmodifiers. The <ENTER> key, for instance, usually doesn't depend on any modi-
12746185892Smrgfiers so it has in its row only one column defined.
128154daed1Smrg
129154daed1SmrgNote that in XKB there is no prior assumption that certain modifiers are
13046185892Smrgbound to certain columns. By editing the proper files (see Key Types, below)
13146185892Smrgthis mapping can be changed as well.
132154daed1Smrg
13346185892SmrgUnlike the original X protocol, the XKB approach is far more flexible.
13446185892SmrgXKB introduces one additional term: the group. You can think of a group
13546185892Smrgas of a vector of columns per keycode (naturally the dimension of this
136154daed1Smrgvector may differ for different keycodes). What is it good for? The group is
137154daed1Smrgnot very useful unless you intend to use more than one logically different
138154daed1Smrgset of symbols (like more than one alphabet) defined in a single mapping ta-
13946185892Smrgble. But then the group has a natural meaning: each symbol set has its own
14046185892Smrggroup and changing it means selecting a different one. The XKB approach allows
141154daed1Smrgup to four different groups. The columns inside each group are called (shift)
14246185892Smrglevels. The X server knows what the current group is and reports it together
14346185892Smrgwith the modifier state and the keycode in key events.
144154daed1Smrg
145154daed1SmrgTo sum it up:
146154daed1Smrg
14746185892Smrg   o  for each keycode the XKB keyboard map contains up to four one-dimensional
14846185892Smrg      tables - groups (logically different symbol sets)
149154daed1Smrg
15046185892Smrg   o  for each group of a keycode the XKB keyboard map contains some columns -
15146185892Smrg      shift levels (values reached by combinations of Shift, Ctrl, Alt, ...
15246185892Smrg      modifiers)
153154daed1Smrg
154154daed1Smrg   o  different keycodes can have different number of groups
155154daed1Smrg
15646185892Smrg   o  different groups of one keycode can have different number of shift levels
157154daed1Smrg
15846185892Smrg   o  the current group number is tracked by the X server
159154daed1Smrg
16046185892SmrgIt is clear that if you sanely define levels and groups, and sanely bind modi-
16146185892Smrgfiers and associated actions, you can have loaded simultaneously up to four
162154daed1Smrgdifferent symbol sets where each of them would reside in its own group.
163154daed1Smrg
164154daed1SmrgThe multi-layout concept provides a facility to manipulate xkb groups and
16546185892Smrgsymbol definitions in a way that allows almost arbitrary composition of
16646185892Smrgpredefined symbol tables. To keep it fully functional you have to:
167154daed1Smrg
168154daed1Smrg   o  define all symbols only in the first group
169154daed1Smrg
170154daed1Smrg   o  (re)define any modifiers with extra care to avoid strange (anisometric)
17146185892Smrg      behaviour
17246185892Smrg
173154daed1Smrg
174154daed1Smrg4.  Defining New Layouts
175154daed1Smrg
17646185892SmrgSee "Some Words About XKB internals" [3] for an explanation of used XKB
17746185892Smrgterms and problems addressed by the XKB extension.
17846185892Smrg
17946185892SmrgSee "Common notes about XKB configuration files language" [4] for a more
18046185892Smrgprecise explanation of the syntax of XKB configuration files.
181154daed1Smrg
18246185892Smrg   [3] http://pascal.tsu.ru/en/xkb/internals.html
18346185892Smrg   [4] http://pascal.tsu.ru/en/xkb/gram-common.html
184154daed1Smrg
185154daed1Smrg4.1  Predefined XKB Symbol Sets
186154daed1Smrg
187154daed1SmrgIf you are about to define some European symbol map extension, you might want
18846185892Smrgto use one of four predefined Latin alphabet layouts.
189154daed1Smrg
19046185892SmrgOkay, let's assume you want to extend an existing keymap and you want to over-
191154daed1Smrgride a few keys. Let's take a simple U.K. keyboard as an example (defined in
192154daed1Smrgpc/gb):
193154daed1Smrg
1948157b447Smrg     default partial alphanumeric_keys
195154daed1Smrg     xkb_symbols "basic" {
19646185892Smrg
197154daed1Smrg       include "pc/latin"
198154daed1Smrg
199154daed1Smrg       name[Group1]="Great Britain";
200154daed1Smrg
201154daed1Smrg       key <AE02>  { [         2,   quotedbl,  twosuperior,    oneeighth ] };
202154daed1Smrg       key <AE03>  { [         3,   sterling, threesuperior,    sterling ] };
203154daed1Smrg       key <AC11>  { [apostrophe,         at, dead_circumflex, dead_caron] };
204154daed1Smrg       key <TLDE>  { [     grave,    notsign,          bar,          bar ] };
205154daed1Smrg       key <BKSL>  { [numbersign, asciitilde,   dead_grave,   dead_breve ] };
20646185892Smrg
207154daed1Smrg       key <RALT>  { type[Group1]="TWO_LEVEL",
20846185892Smrg                     [ ISO_Level3_Shift, Multi_key ] };
209154daed1Smrg
210154daed1Smrg       modifier_map Mod5   { <RALT> };
211154daed1Smrg     };
212154daed1Smrg
21346185892SmrgIt defines a new layout in the basic variant as an extension of a common latin
214154daed1Smrgalphabet layout. The layout (symbol set) name is set to "Great Britain".
21546185892SmrgThen there are redefinitions of a few keycodes and a modifier binding. As
21646185892Smrgyou can see, the number of shift levels is the same for the <AE02>, <AE03>,
21746185892Smrg<AC11>, <TLDE> and <BKSL> keys but it differs from the number of shift
21846185892Smrglevels of <RALT>.
219154daed1Smrg
220154daed1SmrgNote that the <RALT> key itself is a binding key for Mod5 and that it serves
22146185892Smrglike a shift modifier for LevelThree, and together with Shift as a Compose key.
22246185892SmrgIt is a good habit to respect this rule in a new similar layout.
223154daed1Smrg
224154daed1SmrgOkay, you could now define more variants of your new layout besides basic
225154daed1Smrgsimply by including (augmenting/overriding/...) the basic definition and
226154daed1Smrgaltering what may be needed.
227154daed1Smrg
228154daed1Smrg4.2  Key Types
229154daed1Smrg
23046185892SmrgThe differences in the number of columns (shift levels) are caused by the
23146185892Smrgdifferent types of the keys (see the Types definition in section The Basics).
23246185892SmrgMost keycodes have implicitly set the keytype in the included "pc/latin" file
23346185892Smrgto "FOUR_LEVEL_ALPHABETIC". The only exception is the <RALT> keycode which is
234154daed1Smrgexplicitly set "TWO_LEVEL" keytype.
235154daed1Smrg
236154daed1SmrgAll those names refer to pre-defined shift level schemes. Usually you can
23746185892Smrgchoose a suitable shift level scheme from the default types scheme list in
23846185892Smrgthe proper xkb component's subdirectory.
239154daed1Smrg
240154daed1SmrgThe most used schemes are:
241154daed1Smrg
242154daed1Smrg      ONE_LEVEL
24346185892Smrg            The key does not depend on any modifiers. The symbol from the
24446185892Smrg            first level is always chosen.
245154daed1Smrg
246154daed1Smrg      TWO_LEVEL
24746185892Smrg            The key uses the modifier Shift and may have two possible values.
24846185892Smrg            The second level is chosen by the Shift modifier. If the Lock
24946185892Smrg            modifier (usually Caps-lock) applies, the symbol is further
25046185892Smrg            processed using system-specific capitalization rules. If both
25146185892Smrg            the Shift and Lock modifiers apply, the symbol from the second
25246185892Smrg            level is taken and capitalization rules are applied (but usually
25346185892Smrg            have no effect).
254154daed1Smrg
255154daed1Smrg      ALPHABETIC
25646185892Smrg            The key uses the modifiers Shift and Lock. It may have two
25746185892Smrg            possible values. The second level is chosen by Shift. When the
258154daed1Smrg            Lock modifier applies, the symbol from the first level is taken
259154daed1Smrg            and further processed using system-specific capitalization rules.
26046185892Smrg            If both the Shift and Lock modifiers apply, the symbol from the
26146185892Smrg            first level is taken and no capitalization rules are applied.
26246185892Smrg            This is often called shift-cancels-caps behaviour.
263154daed1Smrg
264154daed1Smrg      THREE_LEVEL
26546185892Smrg            Is the same as TWO_LEVEL but it considers an extra modifier:
26646185892Smrg            LevelThree, which can be used to gain the symbol value from the
26746185892Smrg            third level. If both the Shift and LevelThree modifiers apply,
26846185892Smrg            the value from the third level is taken. As in TWO_LEVEL, the
26946185892Smrg            Lock modifier doesn't influence the resulting level - only Shift
27046185892Smrg            and LevelThree are taken into consideration. If the Lock modifier
27146185892Smrg            is active, capitalization rules are applied to the resulting
272154daed1Smrg            symbol.
273154daed1Smrg
274154daed1Smrg      FOUR_LEVEL
27546185892Smrg            Is the same as THREE_LEVEL but, unlike THREE_LEVEL, if both the
27646185892Smrg            Shift and LevelThree modifiers apply, the symbol is taken from
27746185892Smrg            the fourth level.
278154daed1Smrg
279154daed1Smrg      FOUR_LEVEL_ALPHABETIC
280154daed1Smrg            Is similar to FOUR_LEVEL but also defines shift-cancels-caps
28146185892Smrg            behaviour as in ALPHABETIC. If both Lock and LevelThree apply,
28246185892Smrg            the symbol from the third level is taken and the capitalization
28346185892Smrg            rules are applied. If all three modifiers (Lock and Shift and
28446185892Smrg            LevelThree) apply, the symbol from the third level is taken and
28546185892Smrg            no capitalization rules are applied
286154daed1Smrg
287154daed1Smrg      KEYPAD
28846185892Smrg            As the name suggest, this scheme is primarily used for numeric
28946185892Smrg            keypads. The scheme considers two modifiers: Shift and NumLock.
29046185892Smrg            If none of the modifiers applies, the symbol from the first level
29146185892Smrg            is taken. If either the Shift or the NumLock modifier apply, the
29246185892Smrg            symbol from the second level is taken. If both the Shift and the
29346185892Smrg            NumLock modifier apply, the symbol from the first level is taken.
29446185892Smrg            Again, a shift-cancels-caps variant.
295154daed1Smrg
296154daed1Smrg      FOUR_LEVEL_KEYPAD
29746185892Smrg            Is similar to the KEYPAD scheme but considers also the LevelThree
29846185892Smrg            modifier. If the LevelThree modifier applies, the symbol from the
29946185892Smrg            third level is taken. If both Shift and LevelThree or NumLock and
30046185892Smrg            LevelThree apply, the symbol from the fourth level is taken. If
30146185892Smrg            all three (Shift+NumLock+LevelThree) apply, the symbol from the
30246185892Smrg            third level is taken. This also is a shift-cancels-caps variant.
303154daed1Smrg
304154daed1Smrg      FOUR_LEVEL_MIXED_KEYPAD
30546185892Smrg            A four-level keypad scheme where the first two levels behave like
30646185892Smrg            the KEYPAD scheme (with Shift and NumLock). The LevelThree modifier
30746185892Smrg            acts as an override, providing access to two normally Shift-ed
30846185892Smrg            levels: when LevelThree is active we ignore the NumLock state.
30946185892Smrg            Intended for the digit area of the keypad.
310154daed1Smrg
311154daed1Smrg      FOUR_LEVEL_X
312154daed1Smrg            A four-level scheme where the base level accepts no modifier,
31346185892Smrg            LevelThree provides two more Shift-ed levels (levels 2 and 3),
31446185892Smrg            and Ctrl plus Alt command the fourth level. Intended for the
31546185892Smrg            operator part of a keypad, though since NumLock plays no part,
31646185892Smrg            it is not keypad-specific.
317154daed1Smrg
31846185892SmrgBesides that, there are some schemes for special purposes:
319154daed1Smrg
320154daed1Smrg      PC_CONTROL_LEVEL2
32146185892Smrg            Similar to the TWO_LEVEL scheme but it considers the Control
32246185892Smrg            modifier rather than Shift. That means, the symbol from the
32346185892Smrg            second level is chosen by Control rather than by Shift.
324154daed1Smrg
325154daed1Smrg      PC_ALT_LEVEL2
32646185892Smrg            Similar to the TWO_LEVEL scheme but it considers the Alt
32746185892Smrg            modifier rather than Shift. That means, the symbol from
32846185892Smrg            the second level is chosen by Alt rather than by Shift.
329154daed1Smrg
330154daed1Smrg      CTRL+ALT
33146185892Smrg            The key uses the modifiers Alt and Control. It may have two
33246185892Smrg            possible values. If just one modifier (Alt or Control) applies,
33346185892Smrg            the symbol from the first level is chosen. Only if both the Alt
33446185892Smrg            and Control modifiers apply, the symbol from the second level
33546185892Smrg            is chosen.
336154daed1Smrg
337154daed1Smrg      SHIFT+ALT
33846185892Smrg            The key uses the modifiers Shift and Alt. It may have two
33946185892Smrg            possible values. If just one modifier (Alt or Shift) applies,
34046185892Smrg            the symbol from the first level is chosen. Only if both the
34146185892Smrg            Alt and Shift modifiers apply, the symbol from the second
34246185892Smrg            level is chosen.
343154daed1Smrg
344154daed1SmrgIf needed, special caps schemes may be used. They redefine the standard
345154daed1Smrgbehaviour of all *ALPHABETIC types. The layouts (maps of symbols) with keys
346154daed1Smrgdefined in respective types then automatically change their behaviour accord-
347154daed1Smrgingly. Possible redefinitions are:
348154daed1Smrg
349154daed1Smrg   o internal
350154daed1Smrg
351154daed1Smrg   o internal_nocancel
352154daed1Smrg
353154daed1Smrg   o shift
354154daed1Smrg
355154daed1Smrg   o shift_nocancel
356154daed1Smrg
357154daed1SmrgNone of these schemes should be used directly. They are defined merely for
35846185892Smrgthe 'caps:' xkb option (used to globally change the layouts behaviour).
359154daed1Smrg
36046185892SmrgDon't alter any of the existing key types. If you need a different behaviour,
36146185892Smrgcreate a new type.
362154daed1Smrg
36346185892Smrg4.2.1  More on Definitions of Types
364154daed1Smrg
36546185892SmrgWhen the XKB software deals with a separate type description, it gets a com-
366154daed1Smrgplete list of modifiers that should be taken into account from the 'modi-
36746185892Smrgfiers=<list of modifiers>' list and expects a set of 'map[<combination of
36846185892Smrgmodifiers>]=<level indication>' instructions that contain the mapping for
369154daed1Smrgeach combination of modifiers mentioned in that list. Modifiers that are not
370154daed1Smrgexplicitly listed are NOT taken into account when the resulting shift level
37146185892Smrgis computed. If some combination is omitted, the program (subroutine) should
372154daed1Smrgchoose the first level for this combination (a quite reasonable behavior).
373154daed1Smrg
37446185892SmrgLet's consider an example with two modifiers, ModOne and ModTwo:
375154daed1Smrg
376154daed1Smrg     type "..." {
377154daed1Smrg         modifiers = ModOne+ModTwo;
378154daed1Smrg         map[None] = Level1;
379154daed1Smrg         map[ModOne] = Level2;
380154daed1Smrg     };
381154daed1Smrg
38246185892SmrgIn this case the map has a statement for ModOne only and ModOne+ModTwo is
38346185892Smrgomitted. This means that if ModTwo is active, the subroutine can't find an
38446185892Smrgexplicit mapping for this combination and will use the default level, i.e.
385154daed1SmrgLevel1.
386154daed1Smrg
38746185892SmrgBut in the case that the type is described as:
388154daed1Smrg
389154daed1Smrg     type "..." {
390154daed1Smrg         modifiers = ModOne;
391154daed1Smrg         map[None] = Level1;
392154daed1Smrg         map[ModOne] = Level2;
393154daed1Smrg     };
394154daed1Smrg
395154daed1Smrgthe ModTwo will not be taken into account and the resulting level depends on
396154daed1Smrgthe ModOne state only. That means, ModTwo alone produces the Level1 but the
39746185892Smrgcombination ModOne+ModTwo (as well as ModOne alone) produces the Level2.
398154daed1Smrg
39946185892SmrgWhat does it mean if the second modifier is not ModTwo but Lock? It means that
40046185892Smrgin the first case (Lock itself is included in the list of modifiers but combina-
401154daed1Smrgtions with this modifier aren't mentioned in the map statements) the internal
402154daed1Smrgcapitalization rules will be applied to the symbol from the first level. But
403154daed1Smrgin the second case the capitalization will be applied to the symbol chosen
40446185892Smrgaccordingly to the first modifier - and this can be the symbol from the first
405154daed1Smrgas well as from the second level.
406154daed1Smrg
407154daed1SmrgUsually, all modifiers introduced in 'modifiers=<list of modifiers>' list are
408154daed1Smrgused for shift level calculation and then discarded. Sometimes this is not
409154daed1Smrgdesirable. If you want to use a modifier for shift level calculation but you
41046185892Smrgdon't want to discard it, you may list it in 'preserve[<combination of modi-
411154daed1Smrgfiers>]=<list of modifiers>'. That means, for a given combination all listed
412154daed1Smrgmodifiers will be preserved. If the Lock modifier is preserved then the
41346185892Smrgresulting symbol is passed to the internal capitalization routine regardless
414154daed1Smrgwhether it has been used for a shift level calculation or not.
415154daed1Smrg
416154daed1SmrgAny key type description can use both real and virtual modifiers. Since real
417154daed1Smrgmodifiers always have standard names it is not necessary to explicitly
41846185892Smrgdeclare them. Virtual modifiers can have arbitrary names and must be declared
41946185892Smrg(prior to using them) directly in the key type definition:
420154daed1Smrg
42146185892Smrg     virtual_modifiers <comma-separated list of modifiers> ;
422154daed1Smrg
42346185892Smrgas seen in for example the basic, pc, or mousekeys key type definitions.
424154daed1Smrg
425154daed1Smrg4.3  Rules
426154daed1Smrg
42746185892SmrgOnce you are finished with your symbol map you need to add it to the rules file.
42846185892SmrgThe rules file describes how all the five basic components (keycodes, types,
42946185892Smrgcompat, symbols, and geometry) should be composed to give a sensible resulting
430154daed1Smrgxkb configuration.
431154daed1Smrg
43246185892SmrgThe main advantage of rules over formerly used keymaps is the possibility to
433154daed1Smrgsimply parameterize (once) fixed patterns of configurations and thus to ele-
434154daed1Smrggantly allow substitutions of various local configurations into predefined
435154daed1Smrgtemplates.
436154daed1Smrg
43746185892SmrgA pattern in a rules file (often located in /usr/share/X11/xkb/rules) can be
43846185892Smrgparameterized with four other arguments: Model, Layout, Variant, and Options.
43946185892SmrgFor most cases the parameters Model and Layout should be sufficient for choosing
440154daed1Smrga functional keyboard mapping.
441154daed1Smrg
44246185892SmrgThe rules file itself is composed of pattern lines and lines with rules. Each
44346185892Smrgpattern line starts with an exclamation mark ('!') and describes how XKB will
44446185892Smrginterpret the subsequent lines (rules). A sample rules file looks like this:
445154daed1Smrg
446154daed1Smrg     ! model                   =   keycodes
44746185892Smrg       macintosh_old           =   macintosh
448154daed1Smrg       ...
449154daed1Smrg       *                       =   xfree86
450154daed1Smrg
45146185892Smrg     ! model                   =   symbols
452154daed1Smrg       hp                      =   +inet(%m)
45346185892Smrg       microsoftpro            =   +inet(%m)
45446185892Smrg       geniuscomfy             =   +inet(%m)
455154daed1Smrg
45646185892Smrg     ! model       layout[1]   =   symbols
45746185892Smrg       macintosh   us          =   macintosh/us%(v[1])
45846185892Smrg       *           *           =   pc/pc(%m)+pc/%l[1]%(v[1])
459154daed1Smrg
46046185892Smrg     ! model       layout[2]   =   symbols
46146185892Smrg       macintosh   us          =   +macintosh/us[2]%(v[2]):2
46246185892Smrg       *           *           =   +pc/%l[2]%(v[2]):2
463154daed1Smrg
46446185892Smrg     ! option                  =   types
46546185892Smrg       caps:internal           =   +caps(internal)
46646185892Smrg       caps:internal_nocancel  =   +caps(internal_nocancel)
467154daed1Smrg
46846185892SmrgEach rule defines what a certain combination of values on the left side of the
46946185892Smrgequals sign ('=') results in. For example, a (keyboard) model macintosh_old
470154daed1Smrginstructs xkb to take definitions of keycodes from file keycodes/macintosh
47146185892Smrgwhile the rest of the models (represented by a wildcard '*') instructs it to
47246185892Smrgtake them from file keycodes/xfree86. The wildcard represents all possible
473154daed1Smrgvalues on the left side which were not found in any of the previous rules.
474154daed1SmrgThe more specialized (more complete) rules have higher precedence than gen-
475154daed1Smrgeral ones, i.e. the more general rules supply reasonable default values.
476154daed1Smrg
477154daed1SmrgAs you can see some lines contain substitution parameters - the parameters
478154daed1Smrgpreceded by the percent sign ('%'). The first alphabetical character after
479154daed1Smrgthe percent sign expands to the value which has been found on the left side.
480154daed1SmrgFor example +%l%(v) expands into +cz(bksl) if the respective values on the
481154daed1Smrgleft side were cz layout in its bksl variant. More, if the layout resp. vari-
482154daed1Smrgant  parameter is followed by a pair of brackets ('[', ']') it means that xkb
48346185892Smrgshould place the layout resp. variant into the specified xkb group. If the
48446185892Smrgbrackets are omitted, the first group is the default value.
485154daed1Smrg
486154daed1SmrgSo the second block of rules enhances symbol definitions for some particular
487154daed1Smrgkeyboard models with extra keys (for internet, multimedia, ...) . Other mod-
488154daed1Smrgels are left intact. Similarly, the last block overrides some key type defi-
489154daed1Smrgnitions, so the common global behaviour ''shift cancels caps'' or ''shift
49046185892Smrgdoesn't cancel caps'' can be selected. The rest of the rules produce special
49146185892Smrgsymbols for each US variant of the macintosh keyboard, and standard pc symbols
49246185892Smrgin appropriate variants as a default.
493154daed1Smrg
494154daed1Smrg4.4  Descriptive Files of Rules
495154daed1Smrg
49646185892SmrgNow you just need to add a detailed description to the <rules>.xml description
49746185892Smrgfile so that other users (and external programs which often parse this file)
49846185892Smrgknow what your work is about.
499154daed1Smrg
500154daed1Smrg4.4.1  Old Descriptive Files
501154daed1Smrg
50246185892SmrgThe formerly used descriptive files were named <rules>.lst. Its structure is
503154daed1Smrgvery simple and quite self descriptive but such simplicity had also some cav-
504154daed1Smrgities, for example there was no way how to describe local variants of layouts
505154daed1Smrgand there were problems with the localization of descriptions. To preserve
506154daed1Smrgcompatibility with some older programs, new XML descriptive files can be con-
50746185892Smrgverted to the old '.lst' format.
508154daed1Smrg
50946185892SmrgThe meaning of each possible parameter of the rules file should be described.
51046185892SmrgFor the sample rules file given above, the .lst file could look like this:
511154daed1Smrg
512154daed1Smrg     ! model
513154daed1Smrg       pc104        Generic 104-key PC
514154daed1Smrg       microsoft    Microsoft Natural
515154daed1Smrg       pc98         PC-98xx Series
51646185892Smrg       macintosh    Original Macintosh
517154daed1Smrg       ...
518154daed1Smrg
519154daed1Smrg     ! layout
520154daed1Smrg       us      U.S. English
521154daed1Smrg       cz      Czech
522154daed1Smrg       de      German
523154daed1Smrg       ...
524154daed1Smrg
525154daed1Smrg     ! option
52646185892Smrg       caps:internal           uses internal capitalization, Shift cancels Caps
52746185892Smrg       caps:internal_nocancel  uses internal capitalization, Shift doesn't cancel Caps
528154daed1Smrg
529154daed1SmrgAnd that should be it. Enjoy creating your own xkb mapping.
530