CI: Use git master for xkeyboard-config on Linux
[platform/upstream/libxkbcommon.git] / src / keymap.h
1 /*
2  * Copyright 1985, 1987, 1990, 1998  The Open Group
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Except as contained in this notice, the names of the authors or their
22  * institutions shall not be used in advertising or otherwise to promote the
23  * sale, use or other dealings in this Software without prior written
24  * authorization from the authors.
25  */
26
27 /************************************************************
28  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
29  *
30  * Permission to use, copy, modify, and distribute this
31  * software and its documentation for any purpose and without
32  * fee is hereby granted, provided that the above copyright
33  * notice appear in all copies and that both that copyright
34  * notice and this permission notice appear in supporting
35  * documentation, and that the name of Silicon Graphics not be
36  * used in advertising or publicity pertaining to distribution
37  * of the software without specific prior written permission.
38  * Silicon Graphics makes no representation about the suitability
39  * of this software for any purpose. It is provided "as is"
40  * without any express or implied warranty.
41  *
42  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
43  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
44  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
45  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
46  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
47  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
48  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
49  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
50  *
51  ********************************************************/
52
53 /*
54  * Copyright © 2009 Dan Nicholson
55  * Copyright © 2012 Intel Corporation
56  * Copyright © 2012 Ran Benita
57  *
58  * Permission is hereby granted, free of charge, to any person obtaining a
59  * copy of this software and associated documentation files (the "Software"),
60  * to deal in the Software without restriction, including without limitation
61  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
62  * and/or sell copies of the Software, and to permit persons to whom the
63  * Software is furnished to do so, subject to the following conditions:
64  *
65  * The above copyright notice and this permission notice (including the next
66  * paragraph) shall be included in all copies or substantial portions of the
67  * Software.
68  *
69  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
70  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
71  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
72  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
73  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
74  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
75  * DEALINGS IN THE SOFTWARE.
76  *
77  * Author: Daniel Stone <daniel@fooishbar.org>
78  *         Dan Nicholson <dbn.lists@gmail.com>
79  */
80
81 #ifndef KEYMAP_H
82 #define KEYMAP_H
83
84  /* Don't use compat names in internal code. */
85 #define _XKBCOMMON_COMPAT_H
86 #include "xkbcommon/xkbcommon.h"
87
88 #include "utils.h"
89 #include "context.h"
90
91 /* This limit is artificially enforced, we do not depend on it any where.
92  * The reason it's still here is that the rules file format does not
93  * support multiple groups very well, and the rules shipped with
94  * xkeyboard-config (see rules/evdev) depend on this limit extensively.
95  * So just lifting this limit would cause problems for people who will use
96  * more than 4 layouts.
97  * TODO: Fix the group index syntax in the rules format, preferably in a
98  *       backwards compatible way.
99  *       See e.g. https://bugs.freedesktop.org/show_bug.cgi?id=14372
100  * Note: A limit on the number of groups we *do* depend on is imposed by
101  * the size of the xkb_layout_mask_t type (32). This is more than enough
102  * though.
103  */
104 #define XKB_MAX_GROUPS 4
105
106 /* Don't allow more modifiers than we can hold in xkb_mod_mask_t. */
107 #define XKB_MAX_MODS ((xkb_mod_index_t) (sizeof(xkb_mod_mask_t) * 8))
108
109 /* Don't allow more leds than we can hold in xkb_led_mask_t. */
110 #define XKB_MAX_LEDS ((xkb_led_index_t) (sizeof(xkb_led_mask_t) * 8))
111
112 /* Special value to handle modMap None {…} */
113 #define XKB_MOD_NONE 0xffffffffU
114
115 /* These should all go away. */
116 enum mod_type {
117     MOD_REAL = (1 << 0),
118     MOD_VIRT = (1 << 1),
119     MOD_BOTH = (MOD_REAL | MOD_VIRT),
120 };
121 #define MOD_REAL_MASK_ALL ((xkb_mod_mask_t) 0x000000ff)
122
123 enum xkb_action_type {
124     ACTION_TYPE_NONE = 0,
125     ACTION_TYPE_MOD_SET,
126     ACTION_TYPE_MOD_LATCH,
127     ACTION_TYPE_MOD_LOCK,
128     ACTION_TYPE_GROUP_SET,
129     ACTION_TYPE_GROUP_LATCH,
130     ACTION_TYPE_GROUP_LOCK,
131     ACTION_TYPE_PTR_MOVE,
132     ACTION_TYPE_PTR_BUTTON,
133     ACTION_TYPE_PTR_LOCK,
134     ACTION_TYPE_PTR_DEFAULT,
135     ACTION_TYPE_TERMINATE,
136     ACTION_TYPE_SWITCH_VT,
137     ACTION_TYPE_CTRL_SET,
138     ACTION_TYPE_CTRL_LOCK,
139     ACTION_TYPE_PRIVATE,
140     _ACTION_TYPE_NUM_ENTRIES
141 };
142
143 enum xkb_action_flags {
144     ACTION_LOCK_CLEAR = (1 << 0),
145     ACTION_LATCH_TO_LOCK = (1 << 1),
146     ACTION_LOCK_NO_LOCK = (1 << 2),
147     ACTION_LOCK_NO_UNLOCK = (1 << 3),
148     ACTION_MODS_LOOKUP_MODMAP = (1 << 4),
149     ACTION_ABSOLUTE_SWITCH = (1 << 5),
150     ACTION_ABSOLUTE_X = (1 << 6),
151     ACTION_ABSOLUTE_Y = (1 << 7),
152     ACTION_ACCEL = (1 << 8),
153     ACTION_SAME_SCREEN = (1 << 9),
154 };
155
156 enum xkb_action_controls {
157     CONTROL_REPEAT = (1 << 0),
158     CONTROL_SLOW = (1 << 1),
159     CONTROL_DEBOUNCE = (1 << 2),
160     CONTROL_STICKY = (1 << 3),
161     CONTROL_MOUSEKEYS = (1 << 4),
162     CONTROL_MOUSEKEYS_ACCEL = (1 << 5),
163     CONTROL_AX = (1 << 6),
164     CONTROL_AX_TIMEOUT = (1 << 7),
165     CONTROL_AX_FEEDBACK = (1 << 8),
166     CONTROL_BELL = (1 << 9),
167     CONTROL_IGNORE_GROUP_LOCK = (1 << 10),
168     CONTROL_ALL = \
169         (CONTROL_REPEAT | CONTROL_SLOW | CONTROL_DEBOUNCE | CONTROL_STICKY | \
170          CONTROL_MOUSEKEYS | CONTROL_MOUSEKEYS_ACCEL | CONTROL_AX | \
171          CONTROL_AX_TIMEOUT | CONTROL_AX_FEEDBACK | CONTROL_BELL | \
172          CONTROL_IGNORE_GROUP_LOCK)
173 };
174
175 enum xkb_match_operation {
176     MATCH_NONE,
177     MATCH_ANY_OR_NONE,
178     MATCH_ANY,
179     MATCH_ALL,
180     MATCH_EXACTLY,
181 };
182
183 struct xkb_mods {
184     xkb_mod_mask_t mods;       /* original real+virtual mods in definition */
185     xkb_mod_mask_t mask;       /* computed effective mask */
186 };
187
188 struct xkb_mod_action {
189     enum xkb_action_type type;
190     enum xkb_action_flags flags;
191     struct xkb_mods mods;
192 };
193
194 struct xkb_group_action {
195     enum xkb_action_type type;
196     enum xkb_action_flags flags;
197     int32_t group;
198 };
199
200 struct xkb_controls_action {
201     enum xkb_action_type type;
202     enum xkb_action_flags flags;
203     enum xkb_action_controls ctrls;
204 };
205
206 struct xkb_pointer_default_action {
207     enum xkb_action_type type;
208     enum xkb_action_flags flags;
209     int8_t value;
210 };
211
212 struct xkb_switch_screen_action {
213     enum xkb_action_type type;
214     enum xkb_action_flags flags;
215     int8_t screen;
216 };
217
218 struct xkb_pointer_action {
219     enum xkb_action_type type;
220     enum xkb_action_flags flags;
221     int16_t x;
222     int16_t y;
223 };
224
225 struct xkb_pointer_button_action {
226     enum xkb_action_type type;
227     enum xkb_action_flags flags;
228     uint8_t count;
229     uint8_t button;
230 };
231
232 struct xkb_private_action {
233     enum xkb_action_type type;
234     uint8_t data[7];
235 };
236
237 union xkb_action {
238     enum xkb_action_type type;
239     struct xkb_mod_action mods;
240     struct xkb_group_action group;
241     struct xkb_controls_action ctrls;
242     struct xkb_pointer_default_action dflt;
243     struct xkb_switch_screen_action screen;
244     struct xkb_pointer_action ptr;
245     struct xkb_pointer_button_action btn;
246     struct xkb_private_action priv;
247 };
248
249 struct xkb_key_type_entry {
250     xkb_level_index_t level;
251     struct xkb_mods mods;
252     struct xkb_mods preserve;
253 };
254
255 struct xkb_key_type {
256     xkb_atom_t name;
257     struct xkb_mods mods;
258     xkb_level_index_t num_levels;
259     unsigned int num_level_names;
260     xkb_atom_t *level_names;
261     unsigned int num_entries;
262     struct xkb_key_type_entry *entries;
263 };
264
265 struct xkb_sym_interpret {
266     xkb_keysym_t sym;
267     enum xkb_match_operation match;
268     xkb_mod_mask_t mods;
269     xkb_mod_index_t virtual_mod;
270     union xkb_action action;
271     bool level_one_only;
272     bool repeat;
273 };
274
275 struct xkb_led {
276     xkb_atom_t name;
277     enum xkb_state_component which_groups;
278     xkb_layout_mask_t groups;
279     enum xkb_state_component which_mods;
280     struct xkb_mods mods;
281     enum xkb_action_controls ctrls;
282 };
283
284 struct xkb_key_alias {
285     xkb_atom_t real;
286     xkb_atom_t alias;
287 };
288
289 struct xkb_controls {
290     unsigned char groups_wrap;
291     struct xkb_mods internal;
292     struct xkb_mods ignore_lock;
293     unsigned short repeat_delay;
294     unsigned short repeat_interval;
295     unsigned short slow_keys_delay;
296     unsigned short debounce_delay;
297     unsigned short ax_options;
298     unsigned short ax_timeout;
299     unsigned short axt_opts_mask;
300     unsigned short axt_opts_values;
301     unsigned int axt_ctrls_mask;
302     unsigned int axt_ctrls_values;
303 };
304
305 /* Such an awkward name.  Oh well. */
306 enum xkb_range_exceed_type {
307     RANGE_WRAP = 0,
308     RANGE_SATURATE,
309     RANGE_REDIRECT,
310 };
311
312 enum xkb_explicit_components {
313     EXPLICIT_INTERP = (1 << 0),
314     EXPLICIT_VMODMAP = (1 << 1),
315     EXPLICIT_REPEAT = (1 << 2),
316 };
317
318 struct xkb_level {
319     union xkb_action action;
320     unsigned int num_syms;
321     union {
322         xkb_keysym_t sym;       /* num_syms == 1 */
323         xkb_keysym_t *syms;     /* num_syms > 1  */
324     } u;
325 };
326
327 struct xkb_group {
328     bool explicit_type;
329     /* Points to a type in keymap->types. */
330     const struct xkb_key_type *type;
331     /* Use XkbKeyNumLevels for the number of levels. */
332     struct xkb_level *levels;
333 };
334
335 struct xkb_key {
336     xkb_keycode_t keycode;
337     xkb_atom_t name;
338
339     enum xkb_explicit_components explicit;
340
341     xkb_mod_mask_t modmap;
342     xkb_mod_mask_t vmodmap;
343
344     bool repeats;
345
346     enum xkb_range_exceed_type out_of_range_group_action;
347     xkb_layout_index_t out_of_range_group_number;
348
349     xkb_layout_index_t num_groups;
350     struct xkb_group *groups;
351 };
352
353 struct xkb_mod {
354     xkb_atom_t name;
355     enum mod_type type;
356     xkb_mod_mask_t mapping; /* vmod -> real mod mapping */
357 };
358
359 struct xkb_mod_set {
360     struct xkb_mod mods[XKB_MAX_MODS];
361     unsigned int num_mods;
362 };
363
364 /* Common keyboard description structure */
365 struct xkb_keymap {
366     struct xkb_context *ctx;
367
368     int refcnt;
369     enum xkb_keymap_compile_flags flags;
370     enum xkb_keymap_format format;
371
372     enum xkb_action_controls enabled_ctrls;
373
374     xkb_keycode_t min_key_code;
375     xkb_keycode_t max_key_code;
376     struct xkb_key *keys;
377
378     /* aliases in no particular order */
379     unsigned int num_key_aliases;
380     struct xkb_key_alias *key_aliases;
381
382     struct xkb_key_type *types;
383     unsigned int num_types;
384
385     unsigned int num_sym_interprets;
386     struct xkb_sym_interpret *sym_interprets;
387
388     struct xkb_mod_set mods;
389
390     /* Number of groups in the key with the most groups. */
391     xkb_layout_index_t num_groups;
392     /* Not all groups must have names. */
393     xkb_layout_index_t num_group_names;
394     xkb_atom_t *group_names;
395
396     struct xkb_led leds[XKB_MAX_LEDS];
397     unsigned int num_leds;
398
399     char *keycodes_section_name;
400     char *symbols_section_name;
401     char *types_section_name;
402     char *compat_section_name;
403 };
404
405 #define xkb_keys_foreach(iter, keymap) \
406     for ((iter) = (keymap)->keys + (keymap)->min_key_code; \
407          (iter) <= (keymap)->keys + (keymap)->max_key_code; \
408          (iter)++)
409
410 #define xkb_mods_foreach(iter, mods_) \
411     for ((iter) = (mods_)->mods; \
412          (iter) < (mods_)->mods + (mods_)->num_mods; \
413          (iter)++)
414
415 #define xkb_mods_enumerate(idx, iter, mods_) \
416     for ((idx) = 0, (iter) = (mods_)->mods; \
417          (idx) < (mods_)->num_mods; \
418          (idx)++, (iter)++)
419
420 #define xkb_leds_foreach(iter, keymap) \
421     for ((iter) = (keymap)->leds; \
422          (iter) < (keymap)->leds + (keymap)->num_leds; \
423          (iter)++)
424
425 #define xkb_leds_enumerate(idx, iter, keymap) \
426     for ((idx) = 0, (iter) = (keymap)->leds; \
427          (idx) < (keymap)->num_leds; \
428          (idx)++, (iter)++)
429
430 static inline const struct xkb_key *
431 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
432 {
433     if (kc < keymap->min_key_code || kc > keymap->max_key_code)
434         return NULL;
435     return &keymap->keys[kc];
436 }
437
438 static inline xkb_level_index_t
439 XkbKeyNumLevels(const struct xkb_key *key, xkb_layout_index_t layout)
440 {
441     return key->groups[layout].type->num_levels;
442 }
443
444 /*
445  * If the virtual modifiers are not bound to anything, the entry
446  * is not active and should be skipped. xserver does this with
447  * cached entry->active field.
448  */
449 static inline bool
450 entry_is_active(const struct xkb_key_type_entry *entry)
451 {
452     return entry->mods.mods == 0 || entry->mods.mask != 0;
453 }
454
455 struct xkb_keymap *
456 xkb_keymap_new(struct xkb_context *ctx,
457                enum xkb_keymap_format format,
458                enum xkb_keymap_compile_flags flags);
459
460 struct xkb_key *
461 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases);
462
463 xkb_atom_t
464 XkbResolveKeyAlias(const struct xkb_keymap *keymap, xkb_atom_t name);
465
466 void
467 XkbEscapeMapName(char *name);
468
469 xkb_mod_index_t
470 XkbModNameToIndex(const struct xkb_mod_set *mods, xkb_atom_t name,
471                   enum mod_type type);
472
473 bool
474 XkbLevelsSameSyms(const struct xkb_level *a, const struct xkb_level *b);
475
476 xkb_layout_index_t
477 XkbWrapGroupIntoRange(int32_t group,
478                       xkb_layout_index_t num_groups,
479                       enum xkb_range_exceed_type out_of_range_group_action,
480                       xkb_layout_index_t out_of_range_group_number);
481
482 xkb_mod_mask_t
483 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods);
484
485 struct xkb_keymap_format_ops {
486     bool (*keymap_new_from_names)(struct xkb_keymap *keymap,
487                                   const struct xkb_rule_names *names);
488     bool (*keymap_new_from_string)(struct xkb_keymap *keymap,
489                                    const char *string, size_t length);
490     bool (*keymap_new_from_file)(struct xkb_keymap *keymap, FILE *file);
491     char *(*keymap_get_as_string)(struct xkb_keymap *keymap);
492 };
493
494 extern const struct xkb_keymap_format_ops text_v1_keymap_format_ops;
495
496 #endif