kbproto unentanglement: XkbNumIndicators
[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
104 struct xkb_context {
105     int refcnt;
106
107     ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx,
108                                      enum xkb_log_level level,
109                                      const char *fmt, va_list args);
110     enum xkb_log_level log_level;
111     int log_verbosity;
112     void *user_data;
113
114     darray(char *) includes;
115     darray(char *) failed_includes;
116
117     /* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
118     unsigned file_id;
119
120     struct atom_table *atom_table;
121 };
122
123 /**
124  * Legacy names for the components of an XKB keymap, also known as KcCGST.
125  */
126 struct xkb_component_names {
127     char *keycodes;
128     char *types;
129     char *compat;
130     char *symbols;
131 };
132
133 struct xkb_mods {
134     xkb_mod_mask_t mods;       /* original real+virtual mods in definition */
135     xkb_mod_mask_t mask;       /* computed effective mask */
136 };
137
138 struct xkb_mod_action {
139     uint8_t type;
140     uint8_t flags;
141     struct xkb_mods mods;
142 };
143
144 struct xkb_group_action {
145     uint8_t type;
146     uint8_t flags;
147     int32_t group;
148 };
149
150 struct xkb_iso_action {
151     uint8_t type;
152     uint8_t flags;
153     struct xkb_mods mods;
154     int32_t group;
155     uint8_t affect;
156 };
157
158 struct xkb_controls_action {
159     uint8_t type;
160     uint8_t flags;
161     uint32_t ctrls;
162 };
163
164 struct xkb_device_button_action {
165     uint8_t type;
166     uint8_t flags;
167     uint8_t count;
168     uint8_t button;
169     uint8_t device;
170 };
171
172 struct xkb_device_valuator_action {
173     uint8_t type;
174     uint8_t device;
175     uint8_t v1_what;
176     uint8_t v1_index;
177     int8_t v1_value;
178     uint8_t v2_what;
179     uint8_t v2_index;
180     int8_t v2_value;
181 };
182
183 struct xkb_pointer_default_action {
184     uint8_t type;
185     uint8_t flags;
186     uint8_t affect;
187     int8_t value;
188 };
189
190 struct xkb_switch_screen_action {
191     uint8_t type;
192     uint8_t flags;
193     int8_t screen;
194 };
195
196 struct xkb_redirect_key_action {
197     uint8_t type;
198     xkb_keycode_t new_kc;
199     uint8_t mods_mask;
200     uint8_t mods;
201     uint16_t vmods_mask;
202     uint16_t vmods;
203 };
204
205 struct xkb_pointer_action {
206     uint8_t type;
207     uint8_t flags;
208     int16_t x;
209     int16_t y;
210 };
211
212 struct xkb_message_action {
213     uint8_t type;
214     uint8_t flags;
215     uint8_t message[6];
216 };
217
218 struct xkb_pointer_button_action {
219     uint8_t type;
220     uint8_t flags;
221     uint8_t count;
222     int8_t button;
223 };
224
225 /* Instead of non-sequential XkbSA_XFree86Private. */
226 #define PrivateAction (XkbSA_LastAction + 1)
227
228 struct xkb_private_action {
229     uint8_t type;
230     uint8_t data[7];
231 };
232
233 union xkb_action {
234     struct xkb_mod_action mods;
235     struct xkb_group_action group;
236     struct xkb_iso_action iso;
237     struct xkb_controls_action ctrls;
238     struct xkb_device_button_action devbtn;
239     struct xkb_device_valuator_action devval;
240     struct xkb_pointer_default_action dflt;
241     struct xkb_switch_screen_action screen;
242     struct xkb_redirect_key_action redirect;    /* XXX wholly unnecessary? */
243     struct xkb_pointer_action ptr;         /* XXX delete for DeviceValuator */
244     struct xkb_pointer_button_action btn;  /* XXX delete for DeviceBtn */
245     struct xkb_message_action msg;         /* XXX just delete */
246     struct xkb_private_action priv;
247     unsigned char type;
248 };
249
250 struct xkb_kt_map_entry {
251     xkb_level_index_t level;
252     struct xkb_mods mods;
253     struct xkb_mods preserve;
254 };
255
256 struct xkb_key_type {
257     struct xkb_mods mods;
258     xkb_level_index_t num_levels;
259     struct xkb_kt_map_entry *map;
260     unsigned int num_entries;
261     xkb_atom_t name;
262     xkb_atom_t *level_names;
263 };
264
265 struct xkb_sym_interpret {
266     xkb_keysym_t sym;
267     unsigned char flags;
268     unsigned char match;
269     uint8_t mods;
270     xkb_mod_index_t virtual_mod;
271     union xkb_action act;
272 };
273
274 struct xkb_indicator_map {
275     xkb_atom_t name;
276     unsigned char which_groups;
277     uint32_t groups;
278     unsigned char which_mods;
279     struct xkb_mods mods;
280     unsigned int ctrls;
281 };
282
283 struct xkb_key_alias {
284     char real[XKB_KEY_NAME_LENGTH];
285     char alias[XKB_KEY_NAME_LENGTH];
286 };
287
288 struct xkb_controls {
289     unsigned char groups_wrap;
290     struct xkb_mods internal;
291     struct xkb_mods ignore_lock;
292     unsigned short repeat_delay;
293     unsigned short repeat_interval;
294     unsigned short slow_keys_delay;
295     unsigned short debounce_delay;
296     unsigned short ax_options;
297     unsigned short ax_timeout;
298     unsigned short axt_opts_mask;
299     unsigned short axt_opts_values;
300     unsigned int axt_ctrls_mask;
301     unsigned int axt_ctrls_values;
302 };
303
304 struct xkb_key {
305     char name[XKB_KEY_NAME_LENGTH];
306
307     unsigned char explicit;
308
309     unsigned char modmap;
310     xkb_mod_mask_t vmodmap;
311
312     bool repeats;
313
314     union xkb_action *actions;
315
316     unsigned kt_index[XKB_NUM_GROUPS];
317
318     xkb_group_index_t num_groups;
319     /* How many levels the largest group has. */
320     xkb_level_index_t 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     struct xkb_key_type *types;
350     unsigned int num_types;
351
352     darray(struct xkb_sym_interpret) sym_interpret;
353
354     /* vmod -> mod mapping */
355     xkb_mod_mask_t vmods[XkbNumVirtualMods];
356     xkb_atom_t vmod_names[XkbNumVirtualMods];
357
358     /* Number of groups in the key with the most groups. */
359     xkb_group_index_t num_groups;
360     xkb_atom_t group_names[XKB_NUM_GROUPS];
361
362     struct xkb_indicator_map indicators[XKB_NUM_INDICATORS];
363
364     char *keycodes_section_name;
365     char *symbols_section_name;
366     char *types_section_name;
367     char *compat_section_name;
368 };
369
370 static inline struct xkb_key *
371 XkbKey(struct xkb_keymap *keymap, xkb_keycode_t kc)
372 {
373     return &darray_item(keymap->keys, kc);
374 }
375
376 static inline xkb_keycode_t
377 XkbKeyGetKeycode(struct xkb_keymap *keymap, struct xkb_key *key)
378 {
379     /* Hack to avoid having to keep the keycode inside the xkb_key. */
380     return (xkb_keycode_t)(key - keymap->keys.item);
381 }
382
383 #define xkb_foreach_key_from(iter, keymap, from) \
384     darray_foreach_from(iter, keymap->keys, from)
385
386 #define xkb_foreach_key(iter, keymap) \
387     xkb_foreach_key_from(iter, keymap, keymap->min_key_code)
388
389 static inline struct xkb_key_type *
390 XkbKeyType(struct xkb_keymap *keymap, struct xkb_key *key,
391            xkb_group_index_t group)
392 {
393     return &keymap->types[key->kt_index[group]];
394 }
395
396 static inline xkb_level_index_t
397 XkbKeyGroupWidth(struct xkb_keymap *keymap, struct xkb_key *key,
398                  xkb_group_index_t group)
399 {
400     return XkbKeyType(keymap, key, group)->num_levels;
401 }
402
403 static inline unsigned int
404 XkbKeyNumSyms(struct xkb_key *key, xkb_group_index_t group,
405               xkb_level_index_t level)
406 {
407     return key->num_syms[group * key->width + level];
408 }
409
410 static inline xkb_keysym_t *
411 XkbKeySymEntry(struct xkb_key *key, xkb_group_index_t group,
412                xkb_level_index_t level)
413 {
414     return &darray_item(key->syms,
415                         key->sym_index[group * key->width + level]);
416 }
417
418 static inline union xkb_action *
419 XkbKeyActionEntry(struct xkb_key *key, xkb_group_index_t group,
420                   xkb_level_index_t level)
421 {
422     return &key->actions[key->width * group + level];
423 }
424
425 static inline bool
426 XkbKeycodeInRange(struct xkb_keymap *keymap, xkb_keycode_t kc)
427 {
428     return kc >= keymap->min_key_code && kc <= keymap->max_key_code;
429 }
430
431 struct xkb_keymap *
432 xkb_map_new(struct xkb_context *ctx);
433
434 /*
435  * Returns XKB_ATOM_NONE if @string was not previously interned,
436  * otherwise returns the atom.
437  */
438 xkb_atom_t
439 xkb_atom_lookup(struct xkb_context *ctx, const char *string);
440
441 xkb_atom_t
442 xkb_atom_intern(struct xkb_context *ctx, const char *string);
443
444 /**
445  * If @string is dynamically allocated, free'd immediately after
446  * being interned, and not used afterwards, use this function
447  * instead of xkb_atom_intern to avoid some unnecessary allocations.
448  * The caller should not use or free the passed in string afterwards.
449  */
450 xkb_atom_t
451 xkb_atom_steal(struct xkb_context *ctx, char *string);
452
453 char *
454 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
455
456 const char *
457 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
458
459 xkb_group_index_t
460 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t kc);
461
462 xkb_level_index_t
463 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
464                   xkb_group_index_t group);
465
466 extern int
467 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, struct xkb_key *key,
468                           xkb_group_index_t group, xkb_level_index_t level,
469                           const xkb_keysym_t **syms_out);
470
471 extern unsigned
472 xkb_context_take_file_id(struct xkb_context *ctx);
473
474 bool
475 xkb_keysym_is_lower(xkb_keysym_t keysym);
476
477 bool
478 xkb_keysym_is_upper(xkb_keysym_t keysym);
479
480 bool
481 xkb_keysym_is_keypad(xkb_keysym_t keysym);
482
483 ATTR_PRINTF(3, 4) void
484 xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
485         const char *fmt, ...);
486
487 #define xkb_log_cond_level(ctx, level, ...) do { \
488     if (xkb_get_log_level(ctx) >= (level)) \
489     xkb_log((ctx), (level), __VA_ARGS__); \
490 } while (0)
491
492 #define xkb_log_cond_verbosity(ctx, level, vrb, ...) do { \
493     if (xkb_get_log_verbosity(ctx) >= (vrb)) \
494     xkb_log_cond_level((ctx), (level), __VA_ARGS__); \
495 } while (0)
496
497 /*
498  * The format is not part of the argument list in order to avoid the
499  * "ISO C99 requires rest arguments to be used" warning when only the
500  * format is supplied without arguments. Not supplying it would still
501  * result in an error, though.
502  */
503 #define log_dbg(ctx, ...) \
504     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_DEBUG, __VA_ARGS__)
505 #define log_info(ctx, ...) \
506     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_INFO, __VA_ARGS__)
507 #define log_warn(ctx, ...) \
508     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_WARNING, __VA_ARGS__)
509 #define log_err(ctx, ...) \
510     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_ERROR, __VA_ARGS__)
511 #define log_wsgo(ctx, ...) \
512     xkb_log_cond_level((ctx), XKB_LOG_LEVEL_CRITICAL, __VA_ARGS__)
513 #define log_vrb(ctx, vrb, ...) \
514     xkb_log_cond_verbosity((ctx), XKB_LOG_LEVEL_WARNING, (vrb), __VA_ARGS__)
515
516 #endif /* XKB_PRIV_H */