keymap: rename XkbKeyGroupWidth to XkbKeyNumLevels
[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 /* These should all go away. */
113 enum mod_type {
114     MOD_REAL = (1 << 0),
115     MOD_VIRT = (1 << 1),
116     MOD_BOTH = (MOD_REAL | MOD_VIRT),
117 };
118 #define MOD_REAL_MASK_ALL ((xkb_mod_mask_t) 0x000000ff)
119
120 enum xkb_action_type {
121     ACTION_TYPE_NONE = 0,
122     ACTION_TYPE_MOD_SET,
123     ACTION_TYPE_MOD_LATCH,
124     ACTION_TYPE_MOD_LOCK,
125     ACTION_TYPE_GROUP_SET,
126     ACTION_TYPE_GROUP_LATCH,
127     ACTION_TYPE_GROUP_LOCK,
128     ACTION_TYPE_PTR_MOVE,
129     ACTION_TYPE_PTR_BUTTON,
130     ACTION_TYPE_PTR_LOCK,
131     ACTION_TYPE_PTR_DEFAULT,
132     ACTION_TYPE_TERMINATE,
133     ACTION_TYPE_SWITCH_VT,
134     ACTION_TYPE_CTRL_SET,
135     ACTION_TYPE_CTRL_LOCK,
136     ACTION_TYPE_PRIVATE,
137     _ACTION_TYPE_NUM_ENTRIES
138 };
139
140 enum xkb_action_flags {
141     ACTION_LOCK_CLEAR = (1 << 0),
142     ACTION_LATCH_TO_LOCK = (1 << 1),
143     ACTION_LOCK_NO_LOCK = (1 << 2),
144     ACTION_LOCK_NO_UNLOCK = (1 << 3),
145     ACTION_MODS_LOOKUP_MODMAP = (1 << 4),
146     ACTION_ABSOLUTE_SWITCH = (1 << 5),
147     ACTION_ABSOLUTE_X = (1 << 6),
148     ACTION_ABSOLUTE_Y = (1 << 7),
149     ACTION_ACCEL = (1 << 8),
150     ACTION_SAME_SCREEN = (1 << 9),
151 };
152
153 enum xkb_action_controls {
154     CONTROL_REPEAT = (1 << 0),
155     CONTROL_SLOW = (1 << 1),
156     CONTROL_DEBOUNCE = (1 << 2),
157     CONTROL_STICKY = (1 << 3),
158     CONTROL_MOUSEKEYS = (1 << 4),
159     CONTROL_MOUSEKEYS_ACCEL = (1 << 5),
160     CONTROL_AX = (1 << 6),
161     CONTROL_AX_TIMEOUT = (1 << 7),
162     CONTROL_AX_FEEDBACK = (1 << 8),
163     CONTROL_BELL = (1 << 9),
164     CONTROL_IGNORE_GROUP_LOCK = (1 << 10),
165     CONTROL_ALL = \
166         (CONTROL_REPEAT | CONTROL_SLOW | CONTROL_DEBOUNCE | CONTROL_STICKY | \
167          CONTROL_MOUSEKEYS | CONTROL_MOUSEKEYS_ACCEL | CONTROL_AX | \
168          CONTROL_AX_TIMEOUT | CONTROL_AX_FEEDBACK | CONTROL_BELL | \
169          CONTROL_IGNORE_GROUP_LOCK)
170 };
171
172 enum xkb_match_operation {
173     MATCH_NONE,
174     MATCH_ANY_OR_NONE,
175     MATCH_ANY,
176     MATCH_ALL,
177     MATCH_EXACTLY,
178 };
179
180 struct xkb_mods {
181     xkb_mod_mask_t mods;       /* original real+virtual mods in definition */
182     xkb_mod_mask_t mask;       /* computed effective mask */
183 };
184
185 struct xkb_mod_action {
186     enum xkb_action_type type;
187     enum xkb_action_flags flags;
188     struct xkb_mods mods;
189 };
190
191 struct xkb_group_action {
192     enum xkb_action_type type;
193     enum xkb_action_flags flags;
194     int32_t group;
195 };
196
197 struct xkb_controls_action {
198     enum xkb_action_type type;
199     enum xkb_action_flags flags;
200     enum xkb_action_controls ctrls;
201 };
202
203 struct xkb_pointer_default_action {
204     enum xkb_action_type type;
205     enum xkb_action_flags flags;
206     int8_t value;
207 };
208
209 struct xkb_switch_screen_action {
210     enum xkb_action_type type;
211     enum xkb_action_flags flags;
212     int8_t screen;
213 };
214
215 struct xkb_pointer_action {
216     enum xkb_action_type type;
217     enum xkb_action_flags flags;
218     int16_t x;
219     int16_t y;
220 };
221
222 struct xkb_pointer_button_action {
223     enum xkb_action_type type;
224     enum xkb_action_flags flags;
225     uint8_t count;
226     uint8_t button;
227 };
228
229 struct xkb_private_action {
230     enum xkb_action_type type;
231     uint8_t data[7];
232 };
233
234 union xkb_action {
235     enum xkb_action_type type;
236     struct xkb_mod_action mods;
237     struct xkb_group_action group;
238     struct xkb_controls_action ctrls;
239     struct xkb_pointer_default_action dflt;
240     struct xkb_switch_screen_action screen;
241     struct xkb_pointer_action ptr;
242     struct xkb_pointer_button_action btn;
243     struct xkb_private_action priv;
244 };
245
246 struct xkb_key_type_entry {
247     xkb_level_index_t level;
248     struct xkb_mods mods;
249     struct xkb_mods preserve;
250 };
251
252 struct xkb_key_type {
253     xkb_atom_t name;
254     struct xkb_mods mods;
255     xkb_level_index_t num_levels;
256     xkb_atom_t *level_names;
257     unsigned int num_entries;
258     struct xkb_key_type_entry *entries;
259 };
260
261 struct xkb_sym_interpret {
262     xkb_keysym_t sym;
263     enum xkb_match_operation match;
264     xkb_mod_mask_t mods;
265     xkb_mod_index_t virtual_mod;
266     union xkb_action action;
267     bool level_one_only;
268     bool repeat;
269 };
270
271 struct xkb_led {
272     xkb_atom_t name;
273     enum xkb_state_component which_groups;
274     xkb_layout_mask_t groups;
275     enum xkb_state_component which_mods;
276     struct xkb_mods mods;
277     enum xkb_action_controls ctrls;
278 };
279
280 struct xkb_key_alias {
281     xkb_atom_t real;
282     xkb_atom_t alias;
283 };
284
285 struct xkb_controls {
286     unsigned char groups_wrap;
287     struct xkb_mods internal;
288     struct xkb_mods ignore_lock;
289     unsigned short repeat_delay;
290     unsigned short repeat_interval;
291     unsigned short slow_keys_delay;
292     unsigned short debounce_delay;
293     unsigned short ax_options;
294     unsigned short ax_timeout;
295     unsigned short axt_opts_mask;
296     unsigned short axt_opts_values;
297     unsigned int axt_ctrls_mask;
298     unsigned int axt_ctrls_values;
299 };
300
301 /* Such an awkward name.  Oh well. */
302 enum xkb_range_exceed_type {
303     RANGE_WRAP = 0,
304     RANGE_SATURATE,
305     RANGE_REDIRECT,
306 };
307
308 enum xkb_explicit_components {
309     EXPLICIT_INTERP = (1 << 0),
310     EXPLICIT_VMODMAP = (1 << 1),
311     EXPLICIT_REPEAT = (1 << 2),
312 };
313
314 struct xkb_level {
315     union xkb_action action;
316     unsigned int num_syms;
317     union {
318         xkb_keysym_t sym;       /* num_syms == 1 */
319         xkb_keysym_t *syms;     /* num_syms > 1  */
320     } u;
321 };
322
323 struct xkb_group {
324     bool explicit_type;
325     /* Points to a type in keymap->types. */
326     const struct xkb_key_type *type;
327     /* Use XkbKeyGroupWidth for the number of levels. */
328     struct xkb_level *levels;
329 };
330
331 struct xkb_key {
332     xkb_keycode_t keycode;
333     xkb_atom_t name;
334
335     enum xkb_explicit_components explicit;
336
337     xkb_mod_mask_t modmap;
338     xkb_mod_mask_t vmodmap;
339
340     bool repeats;
341
342     enum xkb_range_exceed_type out_of_range_group_action;
343     xkb_layout_index_t out_of_range_group_number;
344
345     xkb_layout_index_t num_groups;
346     struct xkb_group *groups;
347 };
348
349 struct xkb_mod {
350     xkb_atom_t name;
351     enum mod_type type;
352     xkb_mod_mask_t mapping; /* vmod -> real mod mapping */
353 };
354
355 struct xkb_mod_set {
356     struct xkb_mod mods[XKB_MAX_MODS];
357     unsigned int num_mods;
358 };
359
360 /* Common keyboard description structure */
361 struct xkb_keymap {
362     struct xkb_context *ctx;
363
364     int refcnt;
365     enum xkb_keymap_compile_flags flags;
366     enum xkb_keymap_format format;
367
368     enum xkb_action_controls enabled_ctrls;
369
370     xkb_keycode_t min_key_code;
371     xkb_keycode_t max_key_code;
372     struct xkb_key *keys;
373
374     /* aliases in no particular order */
375     unsigned int num_key_aliases;
376     struct xkb_key_alias *key_aliases;
377
378     struct xkb_key_type *types;
379     unsigned int num_types;
380
381     unsigned int num_sym_interprets;
382     struct xkb_sym_interpret *sym_interprets;
383
384     struct xkb_mod_set mods;
385
386     /* Number of groups in the key with the most groups. */
387     xkb_layout_index_t num_groups;
388     /* Not all groups must have names. */
389     xkb_layout_index_t num_group_names;
390     xkb_atom_t *group_names;
391
392     struct xkb_led leds[XKB_MAX_LEDS];
393     unsigned int num_leds;
394
395     char *keycodes_section_name;
396     char *symbols_section_name;
397     char *types_section_name;
398     char *compat_section_name;
399 };
400
401 #define xkb_keys_foreach(iter, keymap) \
402     for ((iter) = (keymap)->keys + (keymap)->min_key_code; \
403          (iter) <= (keymap)->keys + (keymap)->max_key_code; \
404          (iter)++)
405
406 #define xkb_mods_foreach(iter, mods_) \
407     for ((iter) = (mods_)->mods; \
408          (iter) < (mods_)->mods + (mods_)->num_mods; \
409          (iter)++)
410
411 #define xkb_mods_enumerate(idx, iter, mods_) \
412     for ((idx) = 0, (iter) = (mods_)->mods; \
413          (idx) < (mods_)->num_mods; \
414          (idx)++, (iter)++)
415
416 #define xkb_leds_foreach(iter, keymap) \
417     for ((iter) = (keymap)->leds; \
418          (iter) < (keymap)->leds + (keymap)->num_leds; \
419          (iter)++)
420
421 #define xkb_leds_enumerate(idx, iter, keymap) \
422     for ((idx) = 0, (iter) = (keymap)->leds; \
423          (idx) < (keymap)->num_leds; \
424          (idx)++, (iter)++)
425
426 static inline const struct xkb_key *
427 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
428 {
429     if (kc < keymap->min_key_code || kc > keymap->max_key_code)
430         return NULL;
431     return &keymap->keys[kc];
432 }
433
434 static inline xkb_level_index_t
435 XkbKeyNumLevels(const struct xkb_key *key, xkb_layout_index_t layout)
436 {
437     return key->groups[layout].type->num_levels;
438 }
439
440 struct xkb_keymap *
441 xkb_keymap_new(struct xkb_context *ctx,
442                enum xkb_keymap_format format,
443                enum xkb_keymap_compile_flags flags);
444
445 struct xkb_key *
446 XkbKeyByName(struct xkb_keymap *keymap, xkb_atom_t name, bool use_aliases);
447
448 xkb_atom_t
449 XkbResolveKeyAlias(const struct xkb_keymap *keymap, xkb_atom_t name);
450
451 void
452 XkbEscapeMapName(char *name);
453
454 xkb_mod_index_t
455 XkbModNameToIndex(const struct xkb_mod_set *mods, xkb_atom_t name,
456                   enum mod_type type);
457
458 xkb_layout_index_t
459 XkbWrapGroupIntoRange(int32_t group,
460                       xkb_layout_index_t num_groups,
461                       enum xkb_range_exceed_type out_of_range_group_action,
462                       xkb_layout_index_t out_of_range_group_number);
463
464 xkb_mod_mask_t
465 mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods);
466
467 struct xkb_keymap_format_ops {
468     bool (*keymap_new_from_names)(struct xkb_keymap *keymap,
469                                   const struct xkb_rule_names *names);
470     bool (*keymap_new_from_string)(struct xkb_keymap *keymap,
471                                    const char *string, size_t length);
472     bool (*keymap_new_from_file)(struct xkb_keymap *keymap, FILE *file);
473     char *(*keymap_get_as_string)(struct xkb_keymap *keymap);
474 };
475
476 extern const struct xkb_keymap_format_ops text_v1_keymap_format_ops;
477
478 #endif