include: improve file-not-found error reporting
[platform/upstream/libxkbcommon.git] / src / xkb-priv.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 XKB_PRIV_H
82 #define XKB_PRIV_H
83
84 #include <stdbool.h>
85 #include <string.h>
86 #include <strings.h>
87
88 #include "xkbcommon/xkbcommon.h"
89 #include "utils.h"
90 #include "darray.h"
91
92 typedef uint32_t xkb_level_index_t;
93 typedef uint32_t xkb_atom_t;
94
95 #define XKB_ATOM_NONE 0
96 #define XKB_LEVEL_INVALID 0xffffffff
97
98 #define XKB_KEY_NAME_LENGTH 4
99
100 /* These should all be dynamic. */
101 #define XKB_NUM_GROUPS 4
102 #define XKB_NUM_INDICATORS 32
103 #define XKB_NUM_VIRTUAL_MODS 16
104 #define XKB_NUM_CORE_MODS 8
105
106 struct xkb_context {
107     int refcnt;
108
109     ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx,
110                                      enum xkb_log_level level,
111                                      const char *fmt, va_list args);
112     enum xkb_log_level log_level;
113     int log_verbosity;
114     void *user_data;
115
116     darray(char *) includes;
117     darray(char *) failed_includes;
118
119     /* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
120     unsigned file_id;
121
122     struct atom_table *atom_table;
123 };
124
125 /**
126  * Legacy names for the components of an XKB keymap, also known as KcCGST.
127  */
128 struct xkb_component_names {
129     char *keycodes;
130     char *types;
131     char *compat;
132     char *symbols;
133 };
134
135 enum xkb_action_type {
136     ACTION_TYPE_NONE = 0,
137     ACTION_TYPE_MOD_SET,
138     ACTION_TYPE_MOD_LATCH,
139     ACTION_TYPE_MOD_LOCK,
140     ACTION_TYPE_GROUP_SET,
141     ACTION_TYPE_GROUP_LATCH,
142     ACTION_TYPE_GROUP_LOCK,
143     ACTION_TYPE_PTR_MOVE,
144     ACTION_TYPE_PTR_BUTTON,
145     ACTION_TYPE_PTR_LOCK,
146     ACTION_TYPE_PTR_DEFAULT,
147     ACTION_TYPE_TERMINATE,
148     ACTION_TYPE_SWITCH_VT,
149     ACTION_TYPE_CTRL_SET,
150     ACTION_TYPE_CTRL_LOCK,
151     ACTION_TYPE_KEY_REDIRECT,
152     ACTION_TYPE_PRIVATE,
153     _ACTION_TYPE_NUM_ENTRIES
154 };
155
156 enum xkb_action_flags {
157     ACTION_LOCK_CLEAR = (1 << 0),
158     ACTION_LATCH_TO_LOCK = (1 << 1),
159     ACTION_LOCK_NO_LOCK = (1 << 2),
160     ACTION_LOCK_NO_UNLOCK = (1 << 3),
161     ACTION_MODS_LOOKUP_MODMAP = (1 << 4),
162     ACTION_ABSOLUTE_SWITCH = (1 << 5),
163     ACTION_ABSOLUTE_X = (1 << 6),
164     ACTION_ABSOLUTE_Y = (1 << 7),
165     ACTION_NO_ACCEL = (1 << 8),
166     ACTION_SAME_SCREEN = (1 << 9),
167 };
168
169 enum xkb_action_controls {
170     CONTROL_REPEAT = (1 << 0),
171     CONTROL_SLOW = (1 << 1),
172     CONTROL_DEBOUNCE = (1 << 2),
173     CONTROL_STICKY = (1 << 3),
174     CONTROL_MOUSEKEYS = (1 << 4),
175     CONTROL_MOUSEKEYS_ACCEL = (1 << 5),
176     CONTROL_AX = (1 << 6),
177     CONTROL_AX_TIMEOUT = (1 << 7),
178     CONTROL_AX_FEEDBACK = (1 << 8),
179     CONTROL_BELL = (1 << 9),
180     CONTROL_IGNORE_GROUP_LOCK = (1 << 10),
181     CONTROL_ALL = \
182         (CONTROL_REPEAT | CONTROL_SLOW | CONTROL_DEBOUNCE | CONTROL_STICKY | \
183          CONTROL_MOUSEKEYS | CONTROL_MOUSEKEYS_ACCEL | CONTROL_AX | \
184          CONTROL_AX_TIMEOUT | CONTROL_AX_FEEDBACK | CONTROL_BELL | \
185          CONTROL_IGNORE_GROUP_LOCK)
186 };
187
188 enum xkb_match_operation {
189     MATCH_NONE = 0,
190     MATCH_ANY_OR_NONE = 1,
191     MATCH_ANY = 2,
192     MATCH_ALL = 3,
193     MATCH_EXACTLY = 4,
194     MATCH_OP_MASK = \
195         (MATCH_NONE | MATCH_ANY_OR_NONE | MATCH_ANY | MATCH_ALL | \
196          MATCH_EXACTLY),
197     MATCH_LEVEL_ONE_ONLY = (1 << 7),
198 };
199
200 struct xkb_mods {
201     xkb_mod_mask_t mods;       /* original real+virtual mods in definition */
202     xkb_mod_mask_t mask;       /* computed effective mask */
203 };
204
205 struct xkb_mod_action {
206     enum xkb_action_type type;
207     enum xkb_action_flags flags;
208     struct xkb_mods mods;
209 };
210
211 struct xkb_group_action {
212     enum xkb_action_type type;
213     enum xkb_action_flags flags;
214     int32_t group;
215 };
216
217 struct xkb_controls_action {
218     enum xkb_action_type type;
219     enum xkb_action_flags flags;
220     enum xkb_action_controls ctrls;
221 };
222
223 struct xkb_pointer_default_action {
224     enum xkb_action_type type;
225     enum xkb_action_flags flags;
226     int8_t value;
227 };
228
229 struct xkb_switch_screen_action {
230     enum xkb_action_type type;
231     enum xkb_action_flags flags;
232     int8_t screen;
233 };
234
235 struct xkb_redirect_key_action {
236     enum xkb_action_type type;
237     enum xkb_action_flags flags;
238     xkb_keycode_t new_kc;
239     uint8_t mods_mask;
240     uint8_t mods;
241     uint16_t vmods_mask;
242     uint16_t vmods;
243 };
244
245 struct xkb_pointer_action {
246     enum xkb_action_type type;
247     enum xkb_action_flags flags;
248     int16_t x;
249     int16_t y;
250 };
251
252 struct xkb_pointer_button_action {
253     enum xkb_action_type type;
254     enum xkb_action_flags flags;
255     uint8_t count;
256     int8_t button;
257 };
258
259 struct xkb_private_action {
260     enum xkb_action_type type;
261     enum xkb_action_flags flags;
262     uint8_t data[7];
263 };
264
265 union xkb_action {
266     enum xkb_action_type type;
267     struct xkb_mod_action mods;
268     struct xkb_group_action group;
269     struct xkb_controls_action ctrls;
270     struct xkb_pointer_default_action dflt;
271     struct xkb_switch_screen_action screen;
272     struct xkb_redirect_key_action redirect;    /* XXX wholly unnecessary? */
273     struct xkb_pointer_action ptr;
274     struct xkb_pointer_button_action btn;
275     struct xkb_private_action priv;
276 };
277
278 struct xkb_kt_map_entry {
279     xkb_level_index_t level;
280     struct xkb_mods mods;
281     struct xkb_mods preserve;
282 };
283
284 struct xkb_key_type {
285     struct xkb_mods mods;
286     xkb_level_index_t num_levels;
287     struct xkb_kt_map_entry *map;
288     unsigned int num_entries;
289     xkb_atom_t name;
290     xkb_atom_t *level_names;
291 };
292
293 struct xkb_sym_interpret {
294     xkb_keysym_t sym;
295     bool repeat;
296     enum xkb_match_operation match;
297     uint8_t mods;
298     xkb_mod_index_t virtual_mod;
299     union xkb_action act;
300 };
301
302 struct xkb_indicator_map {
303     xkb_atom_t name;
304     enum xkb_state_component which_groups;
305     uint32_t groups;
306     enum xkb_state_component which_mods;
307     struct xkb_mods mods;
308     enum xkb_action_controls ctrls;
309 };
310
311 struct xkb_key_alias {
312     char real[XKB_KEY_NAME_LENGTH];
313     char alias[XKB_KEY_NAME_LENGTH];
314 };
315
316 struct xkb_controls {
317     unsigned char groups_wrap;
318     struct xkb_mods internal;
319     struct xkb_mods ignore_lock;
320     unsigned short repeat_delay;
321     unsigned short repeat_interval;
322     unsigned short slow_keys_delay;
323     unsigned short debounce_delay;
324     unsigned short ax_options;
325     unsigned short ax_timeout;
326     unsigned short axt_opts_mask;
327     unsigned short axt_opts_values;
328     unsigned int axt_ctrls_mask;
329     unsigned int axt_ctrls_values;
330 };
331
332 /* Such an awkward name.  Oh well. */
333 enum xkb_range_exceed_type {
334     RANGE_SATURATE,
335     RANGE_WRAP,
336     RANGE_REDIRECT,
337 };
338
339 enum xkb_explicit_components {
340     EXPLICIT_INTERP = (1 << 0),
341     EXPLICIT_VMODMAP = (1 << 1),
342     EXPLICIT_REPEAT = (1 << 2),
343 };
344
345 struct xkb_key {
346     char name[XKB_KEY_NAME_LENGTH];
347
348     enum xkb_explicit_components explicit;
349     xkb_group_mask_t explicit_groups;
350
351     unsigned char modmap;
352     xkb_mod_mask_t vmodmap;
353
354     bool repeats;
355
356     union xkb_action *actions;
357
358     unsigned kt_index[XKB_NUM_GROUPS];
359
360     xkb_group_index_t num_groups;
361     /* How many levels the largest group has. */
362     xkb_level_index_t width;
363
364     enum xkb_range_exceed_type out_of_range_group_action;
365     xkb_group_index_t out_of_range_group_number;
366
367     /* per level/group index into 'syms' */
368     int *sym_index;
369     /* per level/group */
370     unsigned int *num_syms;
371     xkb_keysym_t *syms;
372 };
373
374 /* Common keyboard description structure */
375 struct xkb_keymap {
376     struct xkb_context *ctx;
377
378     int refcnt;
379     enum xkb_map_compile_flags flags;
380
381     unsigned int enabled_ctrls;
382
383     xkb_keycode_t min_key_code;
384     xkb_keycode_t max_key_code;
385
386     darray(struct xkb_key) keys;
387
388     /* aliases in no particular order */
389     darray(struct xkb_key_alias) key_aliases;
390
391     struct xkb_key_type *types;
392     unsigned int num_types;
393
394     darray(struct xkb_sym_interpret) sym_interpret;
395
396     /* vmod -> mod mapping */
397     xkb_mod_mask_t vmods[XKB_NUM_VIRTUAL_MODS];
398     xkb_atom_t vmod_names[XKB_NUM_VIRTUAL_MODS];
399
400     /* Number of groups in the key with the most groups. */
401     xkb_group_index_t num_groups;
402     xkb_atom_t group_names[XKB_NUM_GROUPS];
403
404     struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
405
406     char *keycodes_section_name;
407     char *symbols_section_name;
408     char *types_section_name;
409     char *compat_section_name;
410 };
411
412 static inline const struct xkb_key *
413 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
414 {
415     if (kc < keymap->min_key_code || kc > keymap->max_key_code)
416         return NULL;
417     return &darray_item(keymap->keys, kc);
418 }
419
420 static inline xkb_keycode_t
421 XkbKeyGetKeycode(struct xkb_keymap *keymap, const struct xkb_key *key)
422 {
423     /* Hack to avoid having to keep the keycode inside the xkb_key. */
424     return (xkb_keycode_t)(key - keymap->keys.item);
425 }
426
427 #define xkb_foreach_key(iter, keymap) \
428     darray_foreach(iter, keymap->keys)
429
430 static inline struct xkb_key_type *
431 XkbKeyType(struct xkb_keymap *keymap, const struct xkb_key *key,
432            xkb_group_index_t group)
433 {
434     return &keymap->types[key->kt_index[group]];
435 }
436
437 static inline xkb_level_index_t
438 XkbKeyGroupWidth(struct xkb_keymap *keymap, const struct xkb_key *key,
439                  xkb_group_index_t group)
440 {
441     return XkbKeyType(keymap, key, group)->num_levels;
442 }
443
444 static inline unsigned int
445 XkbKeyNumSyms(const struct xkb_key *key, xkb_group_index_t group,
446               xkb_level_index_t level)
447 {
448     return key->num_syms[group * key->width + level];
449 }
450
451 static inline const xkb_keysym_t *
452 XkbKeySymEntry(const struct xkb_key *key, xkb_group_index_t group,
453                xkb_level_index_t level)
454 {
455     return &key->syms[key->sym_index[group * key->width + level]];
456 }
457
458 static inline const union xkb_action *
459 XkbKeyActionEntry(const struct xkb_key *key, xkb_group_index_t group,
460                   xkb_level_index_t level)
461 {
462     return &key->actions[key->width * group + level];
463 }
464
465 struct xkb_keymap *
466 xkb_map_new(struct xkb_context *ctx);
467
468 /*
469  * Returns XKB_ATOM_NONE if @string was not previously interned,
470  * otherwise returns the atom.
471  */
472 xkb_atom_t
473 xkb_atom_lookup(struct xkb_context *ctx, const char *string);
474
475 xkb_atom_t
476 xkb_atom_intern(struct xkb_context *ctx, const char *string);
477
478 /**
479  * If @string is dynamically allocated, free'd immediately after
480  * being interned, and not used afterwards, use this function
481  * instead of xkb_atom_intern to avoid some unnecessary allocations.
482  * The caller should not use or free the passed in string afterwards.
483  */
484 xkb_atom_t
485 xkb_atom_steal(struct xkb_context *ctx, char *string);
486
487 char *
488 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
489
490 const char *
491 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
492
493 xkb_group_index_t
494 xkb_key_get_group(struct xkb_state *state, const struct xkb_key *key);
495
496 xkb_level_index_t
497 xkb_key_get_level(struct xkb_state *state, const struct xkb_key *key,
498                   xkb_group_index_t group);
499
500 extern int
501 xkb_key_get_syms_by_level(struct xkb_keymap *keymap,
502                           const struct xkb_key *key,
503                           xkb_group_index_t group, xkb_level_index_t level,
504                           const xkb_keysym_t **syms_out);
505
506 extern unsigned
507 xkb_context_take_file_id(struct xkb_context *ctx);
508
509 unsigned int
510 xkb_context_num_failed_include_paths(struct xkb_context *ctx);
511
512 const char *
513 xkb_context_failed_include_path_get(struct xkb_context *ctx,
514                                     unsigned int idx);
515
516 bool
517 xkb_keysym_is_lower(xkb_keysym_t keysym);
518
519 bool
520 xkb_keysym_is_upper(xkb_keysym_t keysym);
521
522 bool
523 xkb_keysym_is_keypad(xkb_keysym_t keysym);
524
525 ATTR_PRINTF(3, 4) void
526 xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
527         const char *fmt, ...);
528
529 #define xkb_log_cond_level(ctx, level, ...) do { \
530     if (xkb_get_log_level(ctx) >= (level)) \
531     xkb_log((ctx), (level), __VA_ARGS__); \
532 } while (0)
533
534 #define xkb_log_cond_verbosity(ctx, level, vrb, ...) do { \
535     if (xkb_get_log_verbosity(ctx) >= (vrb)) \
536     xkb_log_cond_level((ctx), (level), __VA_ARGS__); \
537 } while (0)
538
539 /*
540  * The format is not part of the argument list in order to avoid the
541  * "ISO C99 requires rest arguments to be used" warning when only the
542  * format is supplied without arguments. Not supplying it would still
543  * result in an error, though.
544  */
545 #define log_dbg(ctx, ...) \
546     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_DEBUG, __VA_ARGS__)
547 #define log_info(ctx, ...) \
548     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_INFO, __VA_ARGS__)
549 #define log_warn(ctx, ...) \
550     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_WARNING, __VA_ARGS__)
551 #define log_err(ctx, ...) \
552     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_ERROR, __VA_ARGS__)
553 #define log_wsgo(ctx, ...) \
554     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_CRITICAL, __VA_ARGS__)
555 #define log_vrb(ctx, vrb, ...) \
556     xkb_log_cond_verbosity((ctx), XKB_LOG_LEVEL_WARNING, (vrb), __VA_ARGS__)
557
558 #endif /* XKB_PRIV_H */