darray: some changes for convenience
[profile/ivi/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 /* From XKM.h */
93 #define XkmKeymapFile           22
94 #define XkmRulesFile            24
95
96 #define XkmTypesIndex           0
97 #define XkmCompatMapIndex       1
98 #define XkmSymbolsIndex         2
99 #define XkmKeyNamesIndex        4
100 #define XkmGeometryIndex        5
101
102 #define XkmTypesMask            (1<<0)
103 #define XkmCompatMapMask        (1<<1)
104 #define XkmSymbolsMask          (1<<2)
105 #define XkmKeyNamesMask         (1<<4)
106 #define XkmGeometryMask         (1<<5)
107
108 #define XkmKeymapRequired       (XkmCompatMapMask|XkmKeyNamesMask|XkmSymbolsMask|XkmTypesMask)
109 #define XkmKeymapOptional       ((XkmTypesMask|XkmGeometryMask)&(~XkmKeymapRequired))
110 #define XkmKeymapLegal          (XkmKeymapRequired|XkmKeymapOptional)
111
112 /**
113  * Legacy names for the components of an XKB keymap, also known as KcCGST.
114  */
115 struct xkb_component_names {
116     char *keymap;
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     int16_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     uint8_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_key;
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         int            active;
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     char *name;
254     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     uint32_t        virtual_mod;
263     union xkb_action act;
264 };
265
266 struct xkb_compat_map {
267     darray(struct xkb_sym_interpret) sym_interpret;
268     struct xkb_mods              groups[XkbNumKbdGroups];
269 };
270
271 struct xkb_sym_map {
272         unsigned char    kt_index[XkbNumKbdGroups];
273         unsigned char    group_info;
274         unsigned char    width;
275         int              *sym_index; /* per level/group index into 'syms' */
276         unsigned int     *num_syms; /* per level/group */
277         xkb_keysym_t     *syms;
278         unsigned int     size_syms; /* size of 'syms' */
279 };
280
281 struct xkb_client_map {
282     darray(struct xkb_key_type) types;
283     darray(struct xkb_sym_map) key_sym_map;
284     unsigned char *modmap;
285 };
286
287 struct xkb_behavior {
288         unsigned char   type;
289         unsigned char   data;
290 };
291
292 struct xkb_server_map {
293     unsigned char *     explicit;
294
295     darray(union xkb_action) acts;
296     darray(size_t ) key_acts;           /* acts[key_acts[keycode]] */
297     struct xkb_behavior         *behaviors;
298     uint32_t            vmods[XkbNumVirtualMods]; /* vmod -> mod mapping */
299     uint32_t            *vmodmap; /* key -> vmod mapping */
300 };
301
302
303 struct xkb_indicator_map {
304         unsigned char   flags;
305         unsigned char   which_groups;
306         unsigned char   groups;
307         unsigned char   which_mods;
308         struct xkb_mods mods;
309         unsigned int    ctrls;
310 };
311
312 struct xkb_indicator {
313         struct xkb_indicator_map        maps[XkbNumIndicators];
314 };
315
316 struct xkb_key_name {
317         char    name[XkbKeyNameLength];
318 };
319
320 struct xkb_key_alias {
321         char    real[XkbKeyNameLength];
322         char    alias[XkbKeyNameLength];
323 };
324
325 struct xkb_names {
326     char *vmods[XkbNumVirtualMods];
327     char *indicators[XkbNumIndicators];
328     char *groups[XkbNumKbdGroups];
329
330     darray(struct xkb_key_name) keys;
331     darray(struct xkb_key_alias) key_aliases;
332 };
333
334 struct xkb_controls {
335     unsigned char num_groups;
336     /* unsigned char groups_wrap; */
337     /* struct xkb_mods internal; */
338     /* struct xkb_mods ignore_lock; */
339     unsigned int enabled_ctrls;
340     /* unsigned short repeat_delay; */
341     /* unsigned short repeat_interval; */
342     /* unsigned short slow_keys_delay; */
343     /* unsigned short debounce_delay; */
344     /* unsigned short ax_options; */
345     /* unsigned short ax_timeout; */
346     /* unsigned short axt_opts_mask; */
347     /* unsigned short axt_opts_values; */
348     /* unsigned int axt_ctrls_mask; */
349     /* unsigned int axt_ctrls_values; */
350     unsigned char *per_key_repeat;
351 };
352
353 /* Common keyboard description structure */
354 struct xkb_keymap {
355     struct xkb_context  *ctx;
356
357     unsigned int        refcnt;
358     unsigned short      flags;
359     xkb_keycode_t       min_key_code;
360     xkb_keycode_t       max_key_code;
361
362     struct xkb_controls *      ctrls;
363     struct xkb_server_map *    server;
364     struct xkb_client_map *    map;
365     struct xkb_indicator *     indicators;
366     struct xkb_names *        names;
367     struct xkb_compat_map *    compat;
368 };
369
370 #define XkbNumGroups(g)             ((g) & 0x0f)
371 #define XkbOutOfRangeGroupInfo(g)   ((g) & 0xf0)
372 #define XkbOutOfRangeGroupAction(g) ((g) & 0xc0)
373 #define XkbOutOfRangeGroupNumber(g) (((g) & 0x30) >> 4)
374 #define XkbSetGroupInfo(g, w, n)    (((w) & 0xc0) | (((n) & 3) << 4) | ((g) & 0x0f))
375 #define XkbSetNumGroups(g, n)       (((g) & 0xf0) | ((n) & 0x0f))
376
377 #define XkbKeyGroupInfo(d, k) \
378     (darray_item((d)->map->key_sym_map, k).group_info)
379 #define XkbKeyNumGroups(d, k) \
380     (XkbNumGroups(darray_item((d)->map->key_sym_map, k).group_info))
381 #define XkbKeyGroupWidth(d, k, g) \
382     (XkbKeyType(d, k, g)->num_levels)
383 #define XkbKeyGroupsWidth(d, k) \
384     (darray_item((d)->map->key_sym_map, k).width)
385 #define XkbKeyTypeIndex(d, k, g) \
386     (darray_item((d)->map->key_sym_map, k).kt_index[g & 0x3])
387 #define XkbKeyType(d, k, g) \
388     (&darray_item((d)->map->types, XkbKeyTypeIndex(d, k, g)))
389 #define XkbKeyNumSyms(d, k, g, sl) \
390     (darray_item((d)->map->key_sym_map, k).num_syms[(g * XkbKeyGroupsWidth(d, k)) + sl])
391 #define XkbKeySym(d, k, n) \
392     (&darray_item((d)->map->key_sym_map, k).syms[n])
393 #define XkbKeySymOffset(d, k, g, sl) \
394     (darray_item((d)->map->key_sym_map, k).sym_index[(g * XkbKeyGroupsWidth(d, k)) + sl])
395 #define XkbKeySymEntry(d, k, g, sl) \
396     (XkbKeySym(d, k, XkbKeySymOffset(d, k, g, sl)))
397 #define XkbKeyHasActions(d, k) \
398     (darray_item((d)->server->key_acts, k) != 0)
399 #define XkbKeyNumActions(d, k) \
400     (XkbKeyHasActions(d, k) ? \
401      (XkbKeyGroupsWidth(d, k) * XkbKeyNumGroups(d, k)) : \
402      1)
403 #define XkbKeyActionsPtr(d, k) \
404     (darray_mem((d)->server->acts, darray_item((d)->server->key_acts, k)))
405 #define XkbKeyAction(d, k, n) \
406     (XkbKeyHasActions(d, k) ? &XkbKeyActionsPtr(d, k)[n] : NULL)
407 #define XkbKeyActionEntry(d, k, sl, g) \
408     (XkbKeyHasActions(d, k) ? \
409      XkbKeyAction(d, k, ((XkbKeyGroupsWidth(d, k) * (g)) + (sl))) : \
410      NULL)
411
412 #define XkbKeycodeInRange(d, k) \
413     (((k) >= (d)->min_key_code) && ((k) <= (d)->max_key_code))
414
415 struct xkb_state {
416         xkb_group_index_t base_group; /**< depressed */
417         xkb_group_index_t latched_group;
418         xkb_group_index_t locked_group;
419         xkb_group_index_t group; /**< effective */
420
421         xkb_mod_mask_t base_mods; /**< depressed */
422         xkb_mod_mask_t latched_mods;
423         xkb_mod_mask_t locked_mods;
424         xkb_mod_mask_t mods; /**< effective */
425
426         uint32_t        leds;
427
428         int refcnt;
429         void *filters;
430         int num_filters;
431         struct xkb_keymap *keymap;
432 };
433
434 typedef uint32_t xkb_atom_t;
435
436 #define XKB_ATOM_NONE 0
437
438 xkb_atom_t
439 xkb_atom_intern(struct xkb_context *ctx, const char *string);
440
441 char *
442 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
443
444 const char *
445 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
446
447 extern unsigned int
448 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key);
449
450 extern unsigned int
451 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
452                   unsigned int group);
453
454 extern int
455 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, xkb_keycode_t key,
456                           unsigned int group, unsigned int level,
457                           const xkb_keysym_t **syms_out);
458
459 /*
460  * Canonicalises component names by prepending the relevant component from
461  * 'old' to the one in 'names' when the latter has a leading '+' or '|', and
462  * by replacing a '%' with the relevant component, e.g.:
463  *
464  * names        old           output
465  * ------------------------------------------
466  * +bar         foo           foo+bar
467  * |quux        baz           baz|quux
468  * foo+%|baz    bar           foo+bar|baz
469  *
470  * If a component in names needs to be modified, the existing value will be
471  * free()d, and a new one allocated with malloc().
472  */
473 void
474 xkb_canonicalise_components(struct xkb_component_names *names,
475                             const struct xkb_component_names *old);
476
477 /**
478  * Deprecated entrypoint for legacy users who need to be able to compile
479  * XKB keymaps by KcCGST (Keycodes + Compat + Geometry + Symbols + Types)
480  * names.
481  *
482  * You should not use this unless you are the X server.  This entrypoint
483  * may well disappear in future releases.  Please, please, don't use it.
484  *
485  * Geometry will be ignored since xkbcommon does not support it in any way.
486  */
487 struct xkb_keymap *
488 xkb_map_new_from_kccgst(struct xkb_context *ctx,
489                         const struct xkb_component_names *kccgst,
490                         enum xkb_map_compile_flags flags);
491
492 extern int
493 xkb_context_take_file_id(struct xkb_context *ctx);
494
495 extern bool
496 XkbcComputeEffectiveMap(struct xkb_keymap *keymap, struct xkb_key_type *type,
497                         unsigned char *map_rtrn);
498
499 extern int
500 XkbcInitCanonicalKeyTypes(struct xkb_keymap *keymap, unsigned which,
501                           int keypadVMod);
502
503 extern unsigned
504 _XkbcKSCheckCase(xkb_keysym_t sym);
505
506 extern xkb_keycode_t
507 XkbcFindKeycodeByName(struct xkb_keymap *keymap, const char *name,
508                       bool use_aliases);
509
510 #define _XkbKSLower (1 << 0)
511 #define _XkbKSUpper (1 << 1)
512
513 #define XkbcKSIsLower(k) (_XkbcKSCheckCase(k) & _XkbKSLower)
514 #define XkbcKSIsUpper(k) (_XkbcKSCheckCase(k) & _XkbKSUpper)
515
516 #define XkbKSIsKeypad(k) (((k) >= XKB_KEY_KP_Space) && ((k) <= XKB_KEY_KP_Equal))
517
518 #endif /* XKB_PRIV_H */