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