kbproto unentanglement: action 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 <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 #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_LAST
154 };
155
156 struct xkb_mods {
157     xkb_mod_mask_t mods;       /* original real+virtual mods in definition */
158     xkb_mod_mask_t mask;       /* computed effective mask */
159 };
160
161 struct xkb_mod_action {
162     enum xkb_action_type type;
163     uint8_t flags;
164     struct xkb_mods mods;
165 };
166
167 struct xkb_group_action {
168     enum xkb_action_type type;
169     uint8_t flags;
170     int32_t group;
171 };
172
173 struct xkb_controls_action {
174     enum xkb_action_type type;
175     uint8_t flags;
176     uint32_t ctrls;
177 };
178
179 struct xkb_pointer_default_action {
180     enum xkb_action_type type;
181     uint8_t flags;
182     uint8_t affect;
183     int8_t value;
184 };
185
186 struct xkb_switch_screen_action {
187     enum xkb_action_type type;
188     uint8_t flags;
189     int8_t screen;
190 };
191
192 struct xkb_redirect_key_action {
193     enum xkb_action_type 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     enum xkb_action_type type;
203     uint8_t flags;
204     int16_t x;
205     int16_t y;
206 };
207
208 struct xkb_pointer_button_action {
209     enum xkb_action_type type;
210     uint8_t flags;
211     uint8_t count;
212     int8_t button;
213 };
214
215 struct xkb_private_action {
216     enum xkb_action_type type;
217     uint8_t data[7];
218 };
219
220 union xkb_action {
221     enum xkb_action_type type;
222     struct xkb_mod_action mods;
223     struct xkb_group_action group;
224     struct xkb_controls_action ctrls;
225     struct xkb_pointer_default_action dflt;
226     struct xkb_switch_screen_action screen;
227     struct xkb_redirect_key_action redirect;    /* XXX wholly unnecessary? */
228     struct xkb_pointer_action ptr;
229     struct xkb_pointer_button_action btn;
230     struct xkb_private_action priv;
231 };
232
233 struct xkb_kt_map_entry {
234     xkb_level_index_t level;
235     struct xkb_mods mods;
236     struct xkb_mods preserve;
237 };
238
239 struct xkb_key_type {
240     struct xkb_mods mods;
241     xkb_level_index_t num_levels;
242     struct xkb_kt_map_entry *map;
243     unsigned int num_entries;
244     xkb_atom_t name;
245     xkb_atom_t *level_names;
246 };
247
248 struct xkb_sym_interpret {
249     xkb_keysym_t sym;
250     unsigned char flags;
251     unsigned char match;
252     uint8_t mods;
253     xkb_mod_index_t virtual_mod;
254     union xkb_action act;
255 };
256
257 struct xkb_indicator_map {
258     xkb_atom_t name;
259     unsigned char which_groups;
260     uint32_t groups;
261     unsigned char which_mods;
262     struct xkb_mods mods;
263     unsigned int ctrls;
264 };
265
266 struct xkb_key_alias {
267     char real[XKB_KEY_NAME_LENGTH];
268     char alias[XKB_KEY_NAME_LENGTH];
269 };
270
271 struct xkb_controls {
272     unsigned char groups_wrap;
273     struct xkb_mods internal;
274     struct xkb_mods ignore_lock;
275     unsigned short repeat_delay;
276     unsigned short repeat_interval;
277     unsigned short slow_keys_delay;
278     unsigned short debounce_delay;
279     unsigned short ax_options;
280     unsigned short ax_timeout;
281     unsigned short axt_opts_mask;
282     unsigned short axt_opts_values;
283     unsigned int axt_ctrls_mask;
284     unsigned int axt_ctrls_values;
285 };
286
287 struct xkb_key {
288     char name[XKB_KEY_NAME_LENGTH];
289
290     unsigned char explicit;
291
292     unsigned char modmap;
293     xkb_mod_mask_t vmodmap;
294
295     bool repeats;
296
297     union xkb_action *actions;
298
299     unsigned kt_index[XKB_NUM_GROUPS];
300
301     xkb_group_index_t num_groups;
302     /* How many levels the largest group has. */
303     xkb_level_index_t width;
304
305     uint8_t out_of_range_group_action;
306     xkb_group_index_t out_of_range_group_number;
307
308     /* per level/group index into 'syms' */
309     int *sym_index;
310     /* per level/group */
311     unsigned int *num_syms;
312     darray(xkb_keysym_t) syms;
313 };
314
315 /* Common keyboard description structure */
316 struct xkb_keymap {
317     struct xkb_context *ctx;
318
319     int refcnt;
320     enum xkb_map_compile_flags flags;
321
322     unsigned int enabled_ctrls;
323
324     xkb_keycode_t min_key_code;
325     xkb_keycode_t max_key_code;
326
327     darray(struct xkb_key) keys;
328
329     /* aliases in no particular order */
330     darray(struct xkb_key_alias) key_aliases;
331
332     struct xkb_key_type *types;
333     unsigned int num_types;
334
335     darray(struct xkb_sym_interpret) sym_interpret;
336
337     /* vmod -> mod mapping */
338     xkb_mod_mask_t vmods[XKB_NUM_VIRTUAL_MODS];
339     xkb_atom_t vmod_names[XKB_NUM_VIRTUAL_MODS];
340
341     /* Number of groups in the key with the most groups. */
342     xkb_group_index_t num_groups;
343     xkb_atom_t group_names[XKB_NUM_GROUPS];
344
345     struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
346
347     char *keycodes_section_name;
348     char *symbols_section_name;
349     char *types_section_name;
350     char *compat_section_name;
351 };
352
353 static inline struct xkb_key *
354 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
355 {
356     return &darray_item(keymap->keys, kc);
357 }
358
359 static inline xkb_keycode_t
360 XkbKeyGetKeycode(struct xkb_keymap *keymap, struct xkb_key *key)
361 {
362     /* Hack to avoid having to keep the keycode inside the xkb_key. */
363     return (xkb_keycode_t)(key - keymap->keys.item);
364 }
365
366 #define xkb_foreach_key_from(iter, keymap, from) \
367     darray_foreach_from(iter, keymap->keys, from)
368
369 #define xkb_foreach_key(iter, keymap) \
370     xkb_foreach_key_from(iter, keymap, keymap->min_key_code)
371
372 static inline struct xkb_key_type *
373 XkbKeyType(struct xkb_keymap *keymap, struct xkb_key *key,
374            xkb_group_index_t group)
375 {
376     return &keymap->types[key->kt_index[group]];
377 }
378
379 static inline xkb_level_index_t
380 XkbKeyGroupWidth(struct xkb_keymap *keymap, struct xkb_key *key,
381                  xkb_group_index_t group)
382 {
383     return XkbKeyType(keymap, key, group)->num_levels;
384 }
385
386 static inline unsigned int
387 XkbKeyNumSyms(struct xkb_key *key, xkb_group_index_t group,
388               xkb_level_index_t level)
389 {
390     return key->num_syms[group * key->width + level];
391 }
392
393 static inline xkb_keysym_t *
394 XkbKeySymEntry(struct xkb_key *key, xkb_group_index_t group,
395                xkb_level_index_t level)
396 {
397     return &darray_item(key->syms,
398                         key->sym_index[group * key->width + level]);
399 }
400
401 static inline union xkb_action *
402 XkbKeyActionEntry(struct xkb_key *key, xkb_group_index_t group,
403                   xkb_level_index_t level)
404 {
405     return &key->actions[key->width * group + level];
406 }
407
408 static inline bool
409 XkbKeycodeInRange(struct xkb_keymap *keymap, xkb_keycode_t kc)
410 {
411     return kc >= keymap->min_key_code && kc <= keymap->max_key_code;
412 }
413
414 struct xkb_keymap *
415 xkb_map_new(struct xkb_context *ctx);
416
417 /*
418  * Returns XKB_ATOM_NONE if @string was not previously interned,
419  * otherwise returns the atom.
420  */
421 xkb_atom_t
422 xkb_atom_lookup(struct xkb_context *ctx, const char *string);
423
424 xkb_atom_t
425 xkb_atom_intern(struct xkb_context *ctx, const char *string);
426
427 /**
428  * If @string is dynamically allocated, free'd immediately after
429  * being interned, and not used afterwards, use this function
430  * instead of xkb_atom_intern to avoid some unnecessary allocations.
431  * The caller should not use or free the passed in string afterwards.
432  */
433 xkb_atom_t
434 xkb_atom_steal(struct xkb_context *ctx, char *string);
435
436 char *
437 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
438
439 const char *
440 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
441
442 xkb_group_index_t
443 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc);
444
445 xkb_level_index_t
446 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
447                   xkb_group_index_t group);
448
449 extern int
450 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
451                           xkb_group_index_t group, xkb_level_index_t level,
452                           const xkb_keysym_t **syms_out);
453
454 extern unsigned
455 xkb_context_take_file_id(struct xkb_context *ctx);
456
457 bool
458 xkb_keysym_is_lower(xkb_keysym_t keysym);
459
460 bool
461 xkb_keysym_is_upper(xkb_keysym_t keysym);
462
463 bool
464 xkb_keysym_is_keypad(xkb_keysym_t keysym);
465
466 ATTR_PRINTF(3, 4) void
467 xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
468         const char *fmt, ...);
469
470 #define xkb_log_cond_level(ctx, level, ...) do { \
471     if (xkb_get_log_level(ctx) >= (level)) \
472     xkb_log((ctx), (level), __VA_ARGS__); \
473 } while (0)
474
475 #define xkb_log_cond_verbosity(ctx, level, vrb, ...) do { \
476     if (xkb_get_log_verbosity(ctx) >= (vrb)) \
477     xkb_log_cond_level((ctx), (level), __VA_ARGS__); \
478 } while (0)
479
480 /*
481  * The format is not part of the argument list in order to avoid the
482  * "ISO C99 requires rest arguments to be used" warning when only the
483  * format is supplied without arguments. Not supplying it would still
484  * result in an error, though.
485  */
486 #define log_dbg(ctx, ...) \
487     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_DEBUG, __VA_ARGS__)
488 #define log_info(ctx, ...) \
489     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_INFO, __VA_ARGS__)
490 #define log_warn(ctx, ...) \
491     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_WARNING, __VA_ARGS__)
492 #define log_err(ctx, ...) \
493     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_ERROR, __VA_ARGS__)
494 #define log_wsgo(ctx, ...) \
495     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_CRITICAL, __VA_ARGS__)
496 #define log_vrb(ctx, vrb, ...) \
497     xkb_log_cond_verbosity((ctx), XKB_LOG_LEVEL_WARNING, (vrb), __VA_ARGS__)
498
499 #endif /* XKB_PRIV_H */