0829c398f5591058309ed9d9463b1572d7be506a
[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 MAP_H
82 #define MAP_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 #define XKB_KEY_NAME_LENGTH 4
92
93 /* This limit is artificially enforced, we do not depend on it any where.
94  * The reason it's still here is that the rules file format does not
95  * support multiple groups very well, and the rules shipped with
96  * xkeyboard-config (see rules/evdev) depend on this limit extensively.
97  * So just lifting this limit would cause problems for people who will use
98  * more than 4 layouts.
99  * TODO: Fix the group index syntax in the rules format, preferably in a
100  *       backwards compatible way.
101  *       See e.g. https://bugs.freedesktop.org/show_bug.cgi?id=14372
102  * Note: A limit on the number of groups we *do* depend on is imposed by
103  * the size of the xkb_layout_mask_t type (32). This is more than enough
104  * though.
105  */
106 #define XKB_NUM_GROUPS 4
107
108 /* These should all be dynamic. */
109 #define XKB_NUM_INDICATORS 32
110 #define XKB_NUM_VIRTUAL_MODS 16
111 #define XKB_NUM_CORE_MODS 8
112
113 enum xkb_action_type {
114     ACTION_TYPE_NONE = 0,
115     ACTION_TYPE_MOD_SET,
116     ACTION_TYPE_MOD_LATCH,
117     ACTION_TYPE_MOD_LOCK,
118     ACTION_TYPE_GROUP_SET,
119     ACTION_TYPE_GROUP_LATCH,
120     ACTION_TYPE_GROUP_LOCK,
121     ACTION_TYPE_PTR_MOVE,
122     ACTION_TYPE_PTR_BUTTON,
123     ACTION_TYPE_PTR_LOCK,
124     ACTION_TYPE_PTR_DEFAULT,
125     ACTION_TYPE_TERMINATE,
126     ACTION_TYPE_SWITCH_VT,
127     ACTION_TYPE_CTRL_SET,
128     ACTION_TYPE_CTRL_LOCK,
129     ACTION_TYPE_KEY_REDIRECT,
130     ACTION_TYPE_PRIVATE,
131     _ACTION_TYPE_NUM_ENTRIES
132 };
133
134 enum xkb_action_flags {
135     ACTION_LOCK_CLEAR = (1 << 0),
136     ACTION_LATCH_TO_LOCK = (1 << 1),
137     ACTION_LOCK_NO_LOCK = (1 << 2),
138     ACTION_LOCK_NO_UNLOCK = (1 << 3),
139     ACTION_MODS_LOOKUP_MODMAP = (1 << 4),
140     ACTION_ABSOLUTE_SWITCH = (1 << 5),
141     ACTION_ABSOLUTE_X = (1 << 6),
142     ACTION_ABSOLUTE_Y = (1 << 7),
143     ACTION_NO_ACCEL = (1 << 8),
144     ACTION_SAME_SCREEN = (1 << 9),
145 };
146
147 enum xkb_action_controls {
148     CONTROL_REPEAT = (1 << 0),
149     CONTROL_SLOW = (1 << 1),
150     CONTROL_DEBOUNCE = (1 << 2),
151     CONTROL_STICKY = (1 << 3),
152     CONTROL_MOUSEKEYS = (1 << 4),
153     CONTROL_MOUSEKEYS_ACCEL = (1 << 5),
154     CONTROL_AX = (1 << 6),
155     CONTROL_AX_TIMEOUT = (1 << 7),
156     CONTROL_AX_FEEDBACK = (1 << 8),
157     CONTROL_BELL = (1 << 9),
158     CONTROL_IGNORE_GROUP_LOCK = (1 << 10),
159     CONTROL_ALL = \
160         (CONTROL_REPEAT | CONTROL_SLOW | CONTROL_DEBOUNCE | CONTROL_STICKY | \
161          CONTROL_MOUSEKEYS | CONTROL_MOUSEKEYS_ACCEL | CONTROL_AX | \
162          CONTROL_AX_TIMEOUT | CONTROL_AX_FEEDBACK | CONTROL_BELL | \
163          CONTROL_IGNORE_GROUP_LOCK)
164 };
165
166 enum xkb_match_operation {
167     MATCH_NONE = 0,
168     MATCH_ANY_OR_NONE = 1,
169     MATCH_ANY = 2,
170     MATCH_ALL = 3,
171     MATCH_EXACTLY = 4,
172     MATCH_OP_MASK = \
173         (MATCH_NONE | MATCH_ANY_OR_NONE | MATCH_ANY | MATCH_ALL | \
174          MATCH_EXACTLY),
175     MATCH_LEVEL_ONE_ONLY = (1 << 7),
176 };
177
178 struct xkb_mods {
179     xkb_mod_mask_t mods;       /* original real+virtual mods in definition */
180     xkb_mod_mask_t mask;       /* computed effective mask */
181 };
182
183 struct xkb_mod_action {
184     enum xkb_action_type type;
185     enum xkb_action_flags flags;
186     struct xkb_mods mods;
187 };
188
189 struct xkb_group_action {
190     enum xkb_action_type type;
191     enum xkb_action_flags flags;
192     int32_t group;
193 };
194
195 struct xkb_controls_action {
196     enum xkb_action_type type;
197     enum xkb_action_flags flags;
198     enum xkb_action_controls ctrls;
199 };
200
201 struct xkb_pointer_default_action {
202     enum xkb_action_type type;
203     enum xkb_action_flags flags;
204     int8_t value;
205 };
206
207 struct xkb_switch_screen_action {
208     enum xkb_action_type type;
209     enum xkb_action_flags flags;
210     int8_t screen;
211 };
212
213 struct xkb_redirect_key_action {
214     enum xkb_action_type type;
215     enum xkb_action_flags flags;
216     xkb_keycode_t new_kc;
217     uint8_t mods_mask;
218     uint8_t mods;
219     uint16_t vmods_mask;
220     uint16_t vmods;
221 };
222
223 struct xkb_pointer_action {
224     enum xkb_action_type type;
225     enum xkb_action_flags flags;
226     int16_t x;
227     int16_t y;
228 };
229
230 struct xkb_pointer_button_action {
231     enum xkb_action_type type;
232     enum xkb_action_flags flags;
233     uint8_t count;
234     int8_t button;
235 };
236
237 struct xkb_private_action {
238     enum xkb_action_type type;
239     enum xkb_action_flags flags;
240     uint8_t data[7];
241 };
242
243 union xkb_action {
244     enum xkb_action_type type;
245     struct xkb_mod_action mods;
246     struct xkb_group_action group;
247     struct xkb_controls_action ctrls;
248     struct xkb_pointer_default_action dflt;
249     struct xkb_switch_screen_action screen;
250     struct xkb_redirect_key_action redirect;    /* XXX wholly unnecessary? */
251     struct xkb_pointer_action ptr;
252     struct xkb_pointer_button_action btn;
253     struct xkb_private_action priv;
254 };
255
256 struct xkb_kt_map_entry {
257     xkb_level_index_t level;
258     struct xkb_mods mods;
259     struct xkb_mods preserve;
260 };
261
262 struct xkb_key_type {
263     struct xkb_mods mods;
264     xkb_level_index_t num_levels;
265     struct xkb_kt_map_entry *map;
266     unsigned int num_entries;
267     xkb_atom_t name;
268     xkb_atom_t *level_names;
269 };
270
271 struct xkb_sym_interpret {
272     xkb_keysym_t sym;
273     bool repeat;
274     enum xkb_match_operation match;
275     uint8_t mods;
276     xkb_mod_index_t virtual_mod;
277     union xkb_action act;
278 };
279
280 struct xkb_indicator_map {
281     xkb_atom_t name;
282     enum xkb_state_component which_groups;
283     uint32_t groups;
284     enum xkb_state_component which_mods;
285     struct xkb_mods mods;
286     enum xkb_action_controls ctrls;
287 };
288
289 struct xkb_key_alias {
290     char real[XKB_KEY_NAME_LENGTH];
291     char alias[XKB_KEY_NAME_LENGTH];
292 };
293
294 struct xkb_controls {
295     unsigned char groups_wrap;
296     struct xkb_mods internal;
297     struct xkb_mods ignore_lock;
298     unsigned short repeat_delay;
299     unsigned short repeat_interval;
300     unsigned short slow_keys_delay;
301     unsigned short debounce_delay;
302     unsigned short ax_options;
303     unsigned short ax_timeout;
304     unsigned short axt_opts_mask;
305     unsigned short axt_opts_values;
306     unsigned int axt_ctrls_mask;
307     unsigned int axt_ctrls_values;
308 };
309
310 /* Such an awkward name.  Oh well. */
311 enum xkb_range_exceed_type {
312     RANGE_SATURATE,
313     RANGE_WRAP,
314     RANGE_REDIRECT,
315 };
316
317 enum xkb_explicit_components {
318     EXPLICIT_INTERP = (1 << 0),
319     EXPLICIT_VMODMAP = (1 << 1),
320     EXPLICIT_REPEAT = (1 << 2),
321 };
322
323 struct xkb_level {
324     union xkb_action action;
325     unsigned int num_syms;
326     union {
327         xkb_keysym_t sym;       /* num_syms == 1 */
328         xkb_keysym_t *syms;     /* num_syms > 1  */
329     } u;
330 };
331
332 struct xkb_group {
333     bool explicit_type;
334     /* Points to a type in keymap->types. */
335     const struct xkb_key_type *type;
336     /* Use XkbKeyGroupWidth for the number of levels. */
337     struct xkb_level *levels;
338 };
339
340 struct xkb_key {
341     xkb_keycode_t keycode;
342     char name[XKB_KEY_NAME_LENGTH];
343
344     enum xkb_explicit_components explicit;
345
346     unsigned char modmap;
347     xkb_mod_mask_t vmodmap;
348
349     bool repeats;
350
351     enum xkb_range_exceed_type out_of_range_group_action;
352     xkb_layout_index_t out_of_range_group_number;
353
354     xkb_layout_index_t num_groups;
355     struct xkb_group *groups;
356 };
357
358 typedef darray(xkb_atom_t) darray_xkb_atom_t;
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
373     darray(struct xkb_key) keys;
374
375     /* aliases in no particular order */
376     darray(struct xkb_key_alias) key_aliases;
377
378     struct xkb_key_type *types;
379     unsigned int num_types;
380
381     darray(struct xkb_sym_interpret) sym_interpret;
382
383     /* vmod -> mod mapping */
384     xkb_mod_mask_t vmods[XKB_NUM_VIRTUAL_MODS];
385     xkb_atom_t vmod_names[XKB_NUM_VIRTUAL_MODS];
386
387     /* Number of groups in the key with the most groups. */
388     xkb_layout_index_t num_groups;
389     darray_xkb_atom_t group_names;
390
391     struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
392
393     char *keycodes_section_name;
394     char *symbols_section_name;
395     char *types_section_name;
396     char *compat_section_name;
397 };
398
399 static inline const struct xkb_key *
400 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
401 {
402     if (kc < keymap->min_key_code || kc > keymap->max_key_code)
403         return NULL;
404     return &darray_item(keymap->keys, kc);
405 }
406
407 #define xkb_foreach_key(iter, keymap) \
408     darray_foreach(iter, keymap->keys)
409
410 static inline xkb_level_index_t
411 XkbKeyGroupWidth(const struct xkb_key *key, xkb_layout_index_t layout)
412 {
413     return key->groups[layout].type->num_levels;
414 }
415
416 static inline unsigned int
417 XkbKeyNumSyms(const struct xkb_key *key, xkb_layout_index_t layout,
418               xkb_level_index_t level)
419 {
420     return key->groups[layout].levels[level].num_syms;
421 }
422
423 static inline const xkb_keysym_t *
424 XkbKeySymEntry(const struct xkb_key *key, xkb_layout_index_t layout,
425                xkb_level_index_t level)
426 {
427     if (XkbKeyNumSyms(key, layout, level) <= 1)
428         return &key->groups[layout].levels[level].u.sym;
429     else
430         return key->groups[layout].levels[level].u.syms;
431 }
432
433 static inline const union xkb_action *
434 XkbKeyActionEntry(const struct xkb_key *key, xkb_layout_index_t layout,
435                   xkb_level_index_t level)
436 {
437     return &key->groups[layout].levels[level].action;
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);
444
445 #endif