types: use regular array for types
[platform/upstream/libxkbcommon.git] / src / xkb-priv.h
1 /*
2  * Copyright 1985, 1987, 1990, 1998  The Open Group
3  * Copyright 2008  Dan Nicholson
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Except as contained in this notice, the names of the authors or their
23  * institutions shall not be used in advertising or otherwise to promote the
24  * sale, use or other dealings in this Software without prior written
25  * authorization from the authors.
26  */
27
28 /************************************************************
29  * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
30  *
31  * Permission to use, copy, modify, and distribute this
32  * software and its documentation for any purpose and without
33  * fee is hereby granted, provided that the above copyright
34  * notice appear in all copies and that both that copyright
35  * notice and this permission notice appear in supporting
36  * documentation, and that the name of Silicon Graphics not be
37  * used in advertising or publicity pertaining to distribution
38  * of the software without specific prior written permission.
39  * Silicon Graphics makes no representation about the suitability
40  * of this software for any purpose. It is provided "as is"
41  * without any express or implied warranty.
42  *
43  * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
44  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
45  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
46  * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
47  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
48  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
49  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
50  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
51  *
52  ********************************************************/
53
54 /*
55  * Copyright © 2012 Intel Corporation
56  *
57  * Permission is hereby granted, free of charge, to any person obtaining a
58  * copy of this software and associated documentation files (the "Software"),
59  * to deal in the Software without restriction, including without limitation
60  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
61  * and/or sell copies of the Software, and to permit persons to whom the
62  * Software is furnished to do so, subject to the following conditions:
63  *
64  * The above copyright notice and this permission notice (including the next
65  * paragraph) shall be included in all copies or substantial portions of the
66  * Software.
67  *
68  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
69  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
70  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
71  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
72  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
73  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
74  * DEALINGS IN THE SOFTWARE.
75  *
76  * Author: Daniel Stone <daniel@fooishbar.org>
77  */
78
79 #ifndef XKB_PRIV_H
80 #define XKB_PRIV_H
81
82 #include <stdbool.h>
83 #include <string.h>
84 #include <strings.h>
85 #include <syslog.h>
86 #include <X11/extensions/XKB.h>
87
88 #include "xkbcommon/xkbcommon.h"
89 #include "utils.h"
90 #include "darray.h"
91 #include "list.h"
92
93 typedef uint16_t xkb_level_index_t;
94
95 enum xkb_file_type {
96     /* The top level file which includes the other component files. */
97     FILE_TYPE_KEYMAP = (1 << 0),
98
99     /* Component files. */
100     FILE_TYPE_TYPES = (1 << 1),
101     FILE_TYPE_COMPAT = (1 << 2),
102     FILE_TYPE_SYMBOLS = (1 << 3),
103     FILE_TYPE_KEYCODES = (1 << 4),
104     FILE_TYPE_GEOMETRY = (1 << 5),
105
106     /* This one doesn't mix with the others, but useful here as well. */
107     FILE_TYPE_RULES = (1 << 6),
108 };
109
110 /* Files needed for a complete keymap. */
111 #define REQUIRED_FILE_TYPES (FILE_TYPE_TYPES | FILE_TYPE_COMPAT | \
112                              FILE_TYPE_SYMBOLS | FILE_TYPE_KEYCODES)
113 #define LEGAL_FILE_TYPES    REQUIRED_FILE_TYPES
114
115 /**
116  * Legacy names for the components of an XKB keymap, also known as KcCGST.
117  */
118 struct xkb_component_names {
119     char *keycodes;
120     char *types;
121     char *compat;
122     char *symbols;
123 };
124
125 struct xkb_any_action {
126     uint8_t type;
127     uint8_t data[7];
128 };
129
130 struct xkb_mod_action {
131     uint8_t type;
132     uint8_t flags;
133     uint8_t mask;
134     uint8_t real_mods;
135     uint16_t vmods;
136 };
137
138 struct xkb_group_action {
139     uint8_t type;
140     uint8_t flags;
141     int32_t group;
142 };
143
144 struct xkb_iso_action {
145     uint8_t type;
146     uint8_t flags;
147     uint8_t mask;
148     uint8_t real_mods;
149     int32_t group;
150     uint8_t affect;
151     uint16_t vmods;
152 };
153
154 struct xkb_controls_action {
155     uint8_t type;
156     uint8_t flags;
157     uint32_t ctrls;
158 };
159
160 struct xkb_device_button_action {
161     uint8_t type;
162     uint8_t flags;
163     uint8_t count;
164     uint8_t button;
165     uint8_t device;
166 };
167
168 struct xkb_device_valuator_action {
169     uint8_t type;
170     uint8_t device;
171     uint8_t v1_what;
172     uint8_t v1_index;
173     int8_t v1_value;
174     uint8_t v2_what;
175     uint8_t v2_index;
176     int8_t v2_value;
177 };
178
179 struct xkb_pointer_default_action {
180     uint8_t type;
181     uint8_t flags;
182     uint8_t affect;
183     int8_t value;
184 };
185
186 struct xkb_switch_screen_action {
187     uint8_t type;
188     uint8_t flags;
189     int8_t screen;
190 };
191
192 struct xkb_redirect_key_action {
193     uint8_t type;
194     xkb_keycode_t new_kc;
195     uint8_t mods_mask;
196     uint8_t mods;
197     uint16_t vmods_mask;
198     uint16_t vmods;
199 };
200
201 struct xkb_pointer_action {
202     uint8_t type;
203     uint8_t flags;
204     int16_t x;
205     int16_t y;
206 };
207
208 struct xkb_message_action {
209     uint8_t type;
210     uint8_t flags;
211     uint8_t message[6];
212 };
213
214 struct xkb_pointer_button_action {
215     uint8_t type;
216     uint8_t flags;
217     uint8_t count;
218     int8_t button;
219 };
220
221 union xkb_action {
222     struct xkb_any_action any;
223     struct xkb_mod_action mods;
224     struct xkb_group_action group;
225     struct xkb_iso_action iso;
226     struct xkb_controls_action ctrls;
227     struct xkb_device_button_action devbtn;
228     struct xkb_device_valuator_action devval;
229     struct xkb_pointer_default_action dflt;
230     struct xkb_switch_screen_action screen;
231     struct xkb_redirect_key_action redirect;    /* XXX wholly unnecessary? */
232     struct xkb_pointer_action ptr;         /* XXX delete for DeviceValuator */
233     struct xkb_pointer_button_action btn;  /* XXX delete for DeviceBtn */
234     struct xkb_message_action msg;         /* XXX just delete */
235     unsigned char type;
236 };
237
238 struct xkb_mods {
239     xkb_mod_mask_t mask;              /* effective mods */
240     xkb_mod_mask_t vmods;
241     uint8_t real_mods;
242 };
243
244 struct xkb_kt_map_entry {
245     xkb_level_index_t level;
246     struct xkb_mods mods;
247     struct xkb_mods preserve;
248 };
249
250 struct xkb_key_type {
251     struct xkb_mods mods;
252     xkb_level_index_t num_levels;
253     darray(struct xkb_kt_map_entry) map;
254     const char *name;
255     const char **level_names;
256 };
257
258 struct xkb_sym_interpret {
259     xkb_keysym_t sym;
260     unsigned char flags;
261     unsigned char 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     unsigned char flags;
269     unsigned char which_groups;
270     uint32_t groups;
271     unsigned char which_mods;
272     struct xkb_mods mods;
273     unsigned int ctrls;
274 };
275
276 struct xkb_key_alias {
277     char real[XkbKeyNameLength];
278     char alias[XkbKeyNameLength];
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 struct xkb_key {
298     char name[XkbKeyNameLength];
299
300     unsigned char explicit;
301
302     unsigned char modmap;
303     xkb_mod_mask_t vmodmap;
304
305     bool repeats;
306
307     /* Index into keymap->acts */
308     size_t acts_index;
309
310     unsigned char kt_index[XkbNumKbdGroups];
311
312     xkb_group_index_t num_groups;
313     /* How many levels the largest group has. */
314     xkb_level_index_t width;
315
316     uint8_t out_of_range_group_action;
317     xkb_group_index_t out_of_range_group_number;
318
319     /* per level/group index into 'syms' */
320     int *sym_index;
321     /* per level/group */
322     unsigned int *num_syms;
323     darray(xkb_keysym_t) syms;
324 };
325
326 /* Common keyboard description structure */
327 struct xkb_keymap {
328     struct xkb_context *ctx;
329
330     int refcnt;
331     enum xkb_map_compile_flags flags;
332
333     unsigned int enabled_ctrls;
334
335     xkb_keycode_t min_key_code;
336     xkb_keycode_t max_key_code;
337
338     darray(struct xkb_key) keys;
339
340     /* aliases in no particular order */
341     darray(struct xkb_key_alias) key_aliases;
342
343     struct xkb_key_type *types;
344     unsigned int num_types;
345
346     darray(struct xkb_sym_interpret) sym_interpret;
347
348     /* vmod -> mod mapping */
349     xkb_mod_index_t vmods[XkbNumVirtualMods];
350     const char *vmod_names[XkbNumVirtualMods];
351
352     struct xkb_mods groups[XkbNumKbdGroups];
353     const char *group_names[XkbNumKbdGroups];
354
355     darray(union xkb_action) acts;
356
357     struct xkb_indicator_map indicators[XkbNumIndicators];
358     const char *indicator_names[XkbNumIndicators];
359
360     char *keycodes_section_name;
361     char *symbols_section_name;
362     char *types_section_name;
363     char *compat_section_name;
364 };
365
366 static inline struct xkb_key *
367 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
368 {
369     return &darray_item(keymap->keys, kc);
370 }
371
372 static inline xkb_keycode_t
373 XkbKeyGetKeycode(struct xkb_keymap *keymap, struct xkb_key *key)
374 {
375     /* Hack to avoid having to keep the keycode inside the xkb_key. */
376     return (xkb_keycode_t)(key - keymap->keys.item);
377 }
378
379 #define xkb_foreach_key_from(iter, keymap, from) \
380     darray_foreach_from(iter, keymap->keys, from)
381
382 #define xkb_foreach_key(iter, keymap) \
383     xkb_foreach_key_from(iter, keymap, keymap->min_key_code)
384
385 static inline unsigned char
386 XkbKeyTypeIndex(struct xkb_key *key, xkb_group_index_t group)
387 {
388     return key->kt_index[group & 0x3];
389 }
390
391 static inline struct xkb_key_type *
392 XkbKeyType(struct xkb_keymap *keymap, struct xkb_key *key,
393            xkb_group_index_t group)
394 {
395     return &keymap->types[XkbKeyTypeIndex(key, group)];
396 }
397
398 static inline xkb_level_index_t
399 XkbKeyGroupWidth(struct xkb_keymap *keymap, struct xkb_key *key,
400                  xkb_group_index_t group)
401 {
402     return XkbKeyType(keymap, key, group)->num_levels;
403 }
404
405 static inline unsigned int
406 XkbKeyNumSyms(struct xkb_key *key, xkb_group_index_t group,
407               xkb_level_index_t level)
408 {
409     return key->num_syms[group * key->width + level];
410 }
411
412 static inline xkb_keysym_t *
413 XkbKeySym(struct xkb_key *key, int ndx)
414 {
415     return &darray_item(key->syms, ndx);
416 }
417
418 static inline int
419 XkbKeySymOffset(struct xkb_key *key, xkb_group_index_t group,
420                 xkb_level_index_t level)
421 {
422     return key->sym_index[group * key->width + level];
423 }
424
425 static inline xkb_keysym_t *
426 XkbKeySymEntry(struct xkb_key *key, xkb_group_index_t group,
427                xkb_level_index_t level)
428 {
429     return XkbKeySym(key, XkbKeySymOffset(key, group, level));
430 }
431
432 static inline bool
433 XkbKeyHasActions(struct xkb_key *key)
434 {
435     return key->acts_index != 0;
436 }
437
438 static inline unsigned int
439 XkbKeyNumActions(struct xkb_key *key)
440 {
441     if (XkbKeyHasActions(key))
442         return key->width * key->num_groups;
443     return 1;
444 }
445
446 static inline union xkb_action *
447 XkbKeyActionsPtr(struct xkb_keymap *keymap, struct xkb_key *key)
448 {
449     return darray_mem(keymap->acts, key->acts_index);
450 }
451
452 static inline union xkb_action *
453 XkbKeyActionEntry(struct xkb_keymap *keymap, struct xkb_key *key,
454                   xkb_group_index_t group, xkb_level_index_t level)
455 {
456     if (XkbKeyHasActions(key))
457         return &XkbKeyActionsPtr(keymap, key)[key->width * group + level];
458     return NULL;
459 }
460
461 static inline bool
462 XkbKeycodeInRange(struct xkb_keymap *keymap, xkb_keycode_t kc)
463 {
464     return kc >= keymap->min_key_code && kc <= keymap->max_key_code;
465 }
466
467 typedef uint32_t xkb_atom_t;
468
469 #define XKB_ATOM_NONE 0
470
471 xkb_atom_t
472 xkb_atom_intern(struct xkb_context *ctx, const char *string);
473
474 /**
475  * If @string is dynamically allocated, free'd immediately after
476  * being interned, and not used afterwards, use this function
477  * instead of xkb_atom_intern to avoid some unnecessary allocations.
478  * The caller should not use or free the passed in string afterwards.
479  */
480 xkb_atom_t
481 xkb_atom_steal(struct xkb_context *ctx, char *string);
482
483 char *
484 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
485
486 const char *
487 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
488
489 xkb_group_index_t
490 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc);
491
492 xkb_level_index_t
493 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
494                   xkb_group_index_t group);
495
496 extern int
497 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
498                           xkb_group_index_t group, xkb_level_index_t level,
499                           const xkb_keysym_t **syms_out);
500
501 /**
502  * Deprecated entrypoint for legacy users who need to be able to compile
503  * XKB keymaps by KcCGST (Keycodes + Compat + Geometry + Symbols + Types)
504  * names.
505  *
506  * You should not use this unless you are the X server.  This entrypoint
507  * may well disappear in future releases.  Please, please, don't use it.
508  *
509  * Geometry will be ignored since xkbcommon does not support it in any way.
510  */
511 struct xkb_keymap *
512 xkb_map_new_from_kccgst(struct xkb_context *ctx,
513                         const struct xkb_component_names *kccgst,
514                         enum xkb_map_compile_flags flags);
515
516 extern unsigned
517 xkb_context_take_file_id(struct xkb_context *ctx);
518
519 bool
520 xkb_keysym_is_lower(xkb_keysym_t keysym);
521
522 bool
523 xkb_keysym_is_upper(xkb_keysym_t keysym);
524
525 bool
526 xkb_keysym_is_keypad(xkb_keysym_t keysym);
527
528 ATTR_PRINTF(3, 4) void
529 xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...);
530
531 #define xkb_log_cond(ctx, prio, ...) \
532     do { \
533         if (xkb_get_log_priority(ctx) >= (prio)) \
534             xkb_log((ctx), (prio), __VA_ARGS__); \
535     } while (0)
536
537 #define xkb_log_cond_lvl(ctx, prio, lvl, ...) \
538     do { \
539         if (xkb_get_log_verbosity(ctx) >= (lvl)) \
540             xkb_log_cond((ctx), (prio), __VA_ARGS__); \
541     } while (0)
542
543 /*
544  * The format is not part of the argument list in order to avoid the
545  * "ISO C99 requires rest arguments to be used" warning when only the
546  * format is supplied without arguments. Not supplying it would still
547  * result in an error, though.
548  */
549 #define log_dbg(ctx, ...) xkb_log_cond((ctx), LOG_DEBUG, __VA_ARGS__)
550 #define log_info(ctx, ...) xkb_log_cond((ctx), LOG_INFO, __VA_ARGS__)
551 #define log_warn(ctx, ...) xkb_log_cond((ctx), LOG_WARNING, __VA_ARGS__)
552 #define log_err(ctx, ...) xkb_log_cond((ctx), LOG_ERR, __VA_ARGS__)
553 #define log_wsgo(ctx, ...) xkb_log_cond((ctx), LOG_CRIT, __VA_ARGS__)
554 #define log_lvl(ctx, lvl, ...) \
555     xkb_log_cond_lvl((ctx), LOG_WARNING, (lvl), __VA_ARGS__)
556
557 #endif /* XKB_PRIV_H */