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