Store actions inside struct xkb_key
[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 <X11/extensions/XKB.h>
86
87 #include "xkbcommon/xkbcommon.h"
88 #include "utils.h"
89 #include "darray.h"
90 #include "list.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 enum xkb_file_type {
99     /* The top level file which includes the other component files. */
100     FILE_TYPE_KEYMAP = (1 << 0),
101
102     /* Component files. */
103     FILE_TYPE_TYPES = (1 << 1),
104     FILE_TYPE_COMPAT = (1 << 2),
105     FILE_TYPE_SYMBOLS = (1 << 3),
106     FILE_TYPE_KEYCODES = (1 << 4),
107     FILE_TYPE_GEOMETRY = (1 << 5),
108
109     /* This one doesn't mix with the others, but useful here as well. */
110     FILE_TYPE_RULES = (1 << 6),
111 };
112
113 struct xkb_context {
114     int refcnt;
115
116     ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx, int priority,
117                                      const char *fmt, va_list args);
118     enum xkb_log_level log_priority;
119     int log_verbosity;
120     void *user_data;
121
122     darray(char *) includes;
123     darray(char *) failed_includes;
124
125     /* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
126     unsigned file_id;
127
128     struct atom_table *atom_table;
129 };
130
131 /* Files needed for a complete keymap. */
132 #define REQUIRED_FILE_TYPES (FILE_TYPE_TYPES | FILE_TYPE_COMPAT | \
133                              FILE_TYPE_SYMBOLS | FILE_TYPE_KEYCODES)
134 #define LEGAL_FILE_TYPES    REQUIRED_FILE_TYPES
135
136 /**
137  * Legacy names for the components of an XKB keymap, also known as KcCGST.
138  */
139 struct xkb_component_names {
140     char *keycodes;
141     char *types;
142     char *compat;
143     char *symbols;
144 };
145
146 struct xkb_mods {
147     xkb_mod_mask_t mods;       /* original real+virtual mods in definition */
148     xkb_mod_mask_t mask;       /* computed effective mask */
149 };
150
151 struct xkb_any_action {
152     uint8_t type;
153     uint8_t data[7];
154 };
155
156 struct xkb_mod_action {
157     uint8_t type;
158     uint8_t flags;
159     struct xkb_mods mods;
160 };
161
162 struct xkb_group_action {
163     uint8_t type;
164     uint8_t flags;
165     int32_t group;
166 };
167
168 struct xkb_iso_action {
169     uint8_t type;
170     uint8_t flags;
171     struct xkb_mods mods;
172     int32_t group;
173     uint8_t affect;
174 };
175
176 struct xkb_controls_action {
177     uint8_t type;
178     uint8_t flags;
179     uint32_t ctrls;
180 };
181
182 struct xkb_device_button_action {
183     uint8_t type;
184     uint8_t flags;
185     uint8_t count;
186     uint8_t button;
187     uint8_t device;
188 };
189
190 struct xkb_device_valuator_action {
191     uint8_t type;
192     uint8_t device;
193     uint8_t v1_what;
194     uint8_t v1_index;
195     int8_t v1_value;
196     uint8_t v2_what;
197     uint8_t v2_index;
198     int8_t v2_value;
199 };
200
201 struct xkb_pointer_default_action {
202     uint8_t type;
203     uint8_t flags;
204     uint8_t affect;
205     int8_t value;
206 };
207
208 struct xkb_switch_screen_action {
209     uint8_t type;
210     uint8_t flags;
211     int8_t screen;
212 };
213
214 struct xkb_redirect_key_action {
215     uint8_t type;
216     xkb_keycode_t new_kc;
217     uint8_t mods_mask;
218     uint8_t mods;
219     uint16_t vmods_mask;
220     uint16_t vmods;
221 };
222
223 struct xkb_pointer_action {
224     uint8_t type;
225     uint8_t flags;
226     int16_t x;
227     int16_t y;
228 };
229
230 struct xkb_message_action {
231     uint8_t type;
232     uint8_t flags;
233     uint8_t message[6];
234 };
235
236 struct xkb_pointer_button_action {
237     uint8_t type;
238     uint8_t flags;
239     uint8_t count;
240     int8_t button;
241 };
242
243 union xkb_action {
244     struct xkb_any_action any;
245     struct xkb_mod_action mods;
246     struct xkb_group_action group;
247     struct xkb_iso_action iso;
248     struct xkb_controls_action ctrls;
249     struct xkb_device_button_action devbtn;
250     struct xkb_device_valuator_action devval;
251     struct xkb_pointer_default_action dflt;
252     struct xkb_switch_screen_action screen;
253     struct xkb_redirect_key_action redirect;    /* XXX wholly unnecessary? */
254     struct xkb_pointer_action ptr;         /* XXX delete for DeviceValuator */
255     struct xkb_pointer_button_action btn;  /* XXX delete for DeviceBtn */
256     struct xkb_message_action msg;         /* XXX just delete */
257     unsigned char type;
258 };
259
260 struct xkb_kt_map_entry {
261     xkb_level_index_t level;
262     struct xkb_mods mods;
263     struct xkb_mods preserve;
264 };
265
266 struct xkb_key_type {
267     struct xkb_mods mods;
268     xkb_level_index_t num_levels;
269     struct xkb_kt_map_entry *map;
270     unsigned int num_entries;
271     xkb_atom_t name;
272     xkb_atom_t *level_names;
273 };
274
275 struct xkb_sym_interpret {
276     xkb_keysym_t sym;
277     unsigned char flags;
278     unsigned char match;
279     uint8_t mods;
280     xkb_mod_index_t virtual_mod;
281     union xkb_action act;
282 };
283
284 struct xkb_indicator_map {
285     unsigned char flags;
286     unsigned char which_groups;
287     uint32_t groups;
288     unsigned char which_mods;
289     struct xkb_mods mods;
290     unsigned int ctrls;
291 };
292
293 struct xkb_key_alias {
294     char real[XkbKeyNameLength];
295     char alias[XkbKeyNameLength];
296 };
297
298 struct xkb_controls {
299     unsigned char groups_wrap;
300     struct xkb_mods internal;
301     struct xkb_mods ignore_lock;
302     unsigned short repeat_delay;
303     unsigned short repeat_interval;
304     unsigned short slow_keys_delay;
305     unsigned short debounce_delay;
306     unsigned short ax_options;
307     unsigned short ax_timeout;
308     unsigned short axt_opts_mask;
309     unsigned short axt_opts_values;
310     unsigned int axt_ctrls_mask;
311     unsigned int axt_ctrls_values;
312 };
313
314 struct xkb_key {
315     char name[XkbKeyNameLength];
316
317     unsigned char explicit;
318
319     unsigned char modmap;
320     xkb_mod_mask_t vmodmap;
321
322     bool repeats;
323
324     union xkb_action *actions;
325
326     unsigned char kt_index[XkbNumKbdGroups];
327
328     xkb_group_index_t num_groups;
329     /* How many levels the largest group has. */
330     xkb_level_index_t width;
331
332     uint8_t out_of_range_group_action;
333     xkb_group_index_t out_of_range_group_number;
334
335     /* per level/group index into 'syms' */
336     int *sym_index;
337     /* per level/group */
338     unsigned int *num_syms;
339     darray(xkb_keysym_t) syms;
340 };
341
342 /* Common keyboard description structure */
343 struct xkb_keymap {
344     struct xkb_context *ctx;
345
346     int refcnt;
347     enum xkb_map_compile_flags flags;
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[XkbNumVirtualMods];
366     const char *vmod_names[XkbNumVirtualMods];
367
368     struct xkb_mods groups[XkbNumKbdGroups];
369     const char *group_names[XkbNumKbdGroups];
370
371     struct xkb_indicator_map indicators[XkbNumIndicators];
372     const char *indicator_names[XkbNumIndicators];
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 struct xkb_key *
381 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
382 {
383     return &darray_item(keymap->keys, kc);
384 }
385
386 static inline xkb_keycode_t
387 XkbKeyGetKeycode(struct xkb_keymap *keymap, struct xkb_key *key)
388 {
389     /* Hack to avoid having to keep the keycode inside the xkb_key. */
390     return (xkb_keycode_t)(key - keymap->keys.item);
391 }
392
393 #define xkb_foreach_key_from(iter, keymap, from) \
394     darray_foreach_from(iter, keymap->keys, from)
395
396 #define xkb_foreach_key(iter, keymap) \
397     xkb_foreach_key_from(iter, keymap, keymap->min_key_code)
398
399 static inline unsigned char
400 XkbKeyTypeIndex(struct xkb_key *key, xkb_group_index_t group)
401 {
402     return key->kt_index[group & 0x3];
403 }
404
405 static inline struct xkb_key_type *
406 XkbKeyType(struct xkb_keymap *keymap, struct xkb_key *key,
407            xkb_group_index_t group)
408 {
409     return &keymap->types[XkbKeyTypeIndex(key, group)];
410 }
411
412 static inline xkb_level_index_t
413 XkbKeyGroupWidth(struct xkb_keymap *keymap, struct xkb_key *key,
414                  xkb_group_index_t group)
415 {
416     return XkbKeyType(keymap, key, group)->num_levels;
417 }
418
419 static inline unsigned int
420 XkbKeyNumSyms(struct xkb_key *key, xkb_group_index_t group,
421               xkb_level_index_t level)
422 {
423     return key->num_syms[group * key->width + level];
424 }
425
426 static inline xkb_keysym_t *
427 XkbKeySym(struct xkb_key *key, int ndx)
428 {
429     return &darray_item(key->syms, ndx);
430 }
431
432 static inline int
433 XkbKeySymOffset(struct xkb_key *key, xkb_group_index_t group,
434                 xkb_level_index_t level)
435 {
436     return key->sym_index[group * key->width + level];
437 }
438
439 static inline xkb_keysym_t *
440 XkbKeySymEntry(struct xkb_key *key, xkb_group_index_t group,
441                xkb_level_index_t level)
442 {
443     return XkbKeySym(key, XkbKeySymOffset(key, group, level));
444 }
445
446 static inline union xkb_action *
447 XkbKeyActionEntry(struct xkb_key *key, xkb_group_index_t group,
448                   xkb_level_index_t level)
449 {
450     return &key->actions[key->width * group + level];
451 }
452
453 static inline bool
454 XkbKeycodeInRange(struct xkb_keymap *keymap, xkb_keycode_t kc)
455 {
456     return kc >= keymap->min_key_code && kc <= keymap->max_key_code;
457 }
458
459 xkb_atom_t
460 xkb_atom_intern(struct xkb_context *ctx, const char *string);
461
462 /**
463  * If @string is dynamically allocated, free'd immediately after
464  * being interned, and not used afterwards, use this function
465  * instead of xkb_atom_intern to avoid some unnecessary allocations.
466  * The caller should not use or free the passed in string afterwards.
467  */
468 xkb_atom_t
469 xkb_atom_steal(struct xkb_context *ctx, char *string);
470
471 char *
472 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
473
474 const char *
475 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
476
477 xkb_group_index_t
478 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc);
479
480 xkb_level_index_t
481 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
482                   xkb_group_index_t group);
483
484 extern int
485 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
486                           xkb_group_index_t group, xkb_level_index_t level,
487                           const xkb_keysym_t **syms_out);
488
489 extern unsigned
490 xkb_context_take_file_id(struct xkb_context *ctx);
491
492 bool
493 xkb_keysym_is_lower(xkb_keysym_t keysym);
494
495 bool
496 xkb_keysym_is_upper(xkb_keysym_t keysym);
497
498 bool
499 xkb_keysym_is_keypad(xkb_keysym_t keysym);
500
501 ATTR_PRINTF(3, 4) void
502 xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...);
503
504 #define xkb_log_cond(ctx, prio, ...) \
505     do { \
506         if (xkb_get_log_priority(ctx) >= (prio)) \
507             xkb_log((ctx), (prio), __VA_ARGS__); \
508     } while (0)
509
510 #define xkb_log_cond_lvl(ctx, prio, lvl, ...) \
511     do { \
512         if (xkb_get_log_verbosity(ctx) >= (lvl)) \
513             xkb_log_cond((ctx), (prio), __VA_ARGS__); \
514     } while (0)
515
516 /*
517  * The format is not part of the argument list in order to avoid the
518  * "ISO C99 requires rest arguments to be used" warning when only the
519  * format is supplied without arguments. Not supplying it would still
520  * result in an error, though.
521  */
522 #define log_dbg(ctx, ...) \
523     xkb_log_cond((ctx), XKB_LOG_LEVEL_DEBUG, __VA_ARGS__)
524 #define log_info(ctx, ...) \
525     xkb_log_cond((ctx), XKB_LOG_LEVEL_INFO, __VA_ARGS__)
526 #define log_warn(ctx, ...) \
527     xkb_log_cond((ctx), XKB_LOG_LEVEL_WARNING, __VA_ARGS__)
528 #define log_err(ctx, ...) \
529     xkb_log_cond((ctx), XKB_LOG_LEVEL_ERROR, __VA_ARGS__)
530 #define log_wsgo(ctx, ...) \
531     xkb_log_cond((ctx), XKB_LOG_LEVEL_CRITICAL, __VA_ARGS__)
532 #define log_lvl(ctx, lvl, ...) \
533     xkb_log_cond_lvl((ctx), XKB_LOG_LEVEL_WARNING, (lvl), __VA_ARGS__)
534
535 #endif /* XKB_PRIV_H */