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