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