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