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