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