remove unused function xkb_canonicalise_components
[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 | FILE_TYPE_SYMBOLS | FILE_TYPE_KEYCODES)
109 #define LEGAL_FILE_TYPES    (FILE_TYPE_GEOMETRY | REQUIRED_FILE_TYPES)
110
111 /**
112  * Legacy names for the components of an XKB keymap, also known as KcCGST.
113  */
114 struct xkb_component_names {
115     char *keycodes;
116     char *types;
117     char *compat;
118     char *symbols;
119 };
120
121 struct xkb_any_action {
122     uint8_t   type;
123     uint8_t   data[7];
124 };
125
126 struct xkb_mod_action {
127     uint8_t         type;
128     uint8_t         flags;
129     uint8_t         mask;
130     uint8_t         real_mods;
131     uint16_t        vmods;
132 };
133
134 struct xkb_group_action {
135     uint8_t         type;
136     uint8_t         flags;
137     int16_t         group;
138 };
139
140 struct xkb_iso_action {
141     uint8_t         type;
142     uint8_t         flags;
143     uint8_t         mask;
144     uint8_t         real_mods;
145     uint8_t         group;
146     uint8_t         affect;
147     uint16_t        vmods;
148 };
149
150 struct xkb_controls_action {
151     uint8_t         type;
152     uint8_t         flags;
153     uint32_t        ctrls;
154 };
155
156 struct xkb_device_button_action {
157     uint8_t         type;
158     uint8_t         flags;
159     uint8_t         count;
160     uint8_t         button;
161     uint8_t         device;
162 };
163
164 struct xkb_device_valuator_action {
165     uint8_t         type;
166     uint8_t         device;
167     uint8_t         v1_what;
168     uint8_t         v1_index;
169     int8_t          v1_value;
170     uint8_t         v2_what;
171     uint8_t         v2_index;
172     int8_t          v2_value;
173 };
174
175 struct xkb_pointer_default_action {
176     uint8_t         type;
177     uint8_t         flags;
178     uint8_t         affect;
179     int8_t          value;
180 };
181
182 struct xkb_switch_screen_action {
183     uint8_t         type;
184     uint8_t         flags;
185     int8_t          screen;
186 };
187
188 struct xkb_redirect_key_action {
189     uint8_t             type;
190     xkb_keycode_t       new_key;
191     uint8_t             mods_mask;
192     uint8_t             mods;
193     uint16_t            vmods_mask;
194     uint16_t            vmods;
195 };
196
197 struct xkb_pointer_action {
198     uint8_t     type;
199     uint8_t     flags;
200     int16_t     x;
201     int16_t     y;
202 };
203
204 struct xkb_message_action {
205     uint8_t     type;
206     uint8_t     flags;
207     uint8_t     message[6];
208 };
209
210 struct xkb_pointer_button_action {
211     uint8_t     type;
212     uint8_t     flags;
213     uint8_t     count;
214     int8_t      button;
215 };
216
217 union xkb_action {
218     struct xkb_any_action             any;
219     struct xkb_mod_action             mods;
220     struct xkb_group_action           group;
221     struct xkb_iso_action             iso;
222     struct xkb_controls_action        ctrls;
223     struct xkb_device_button_action   devbtn;
224     struct xkb_device_valuator_action devval;
225     struct xkb_pointer_default_action dflt;
226     struct xkb_switch_screen_action   screen;
227     struct xkb_redirect_key_action    redirect; /* XXX wholly unnecessary? */
228     struct xkb_pointer_action         ptr; /* XXX delete for DeviceValuator */
229     struct xkb_pointer_button_action  btn; /* XXX delete for DeviceBtn */
230     struct xkb_message_action         msg; /* XXX just delete */
231     unsigned char                     type;
232 };
233
234 struct xkb_mods {
235         uint32_t        mask;   /* effective mods */
236         uint32_t        vmods;
237         uint8_t         real_mods;
238 };
239
240 struct xkb_kt_map_entry {
241         int            active;
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_compat_map {
265     darray(struct xkb_sym_interpret) sym_interpret;
266     struct xkb_mods              groups[XkbNumKbdGroups];
267 };
268
269 struct xkb_sym_map {
270         unsigned char    kt_index[XkbNumKbdGroups];
271         unsigned char    group_info;
272         unsigned char    width;
273         int              *sym_index; /* per level/group index into 'syms' */
274         unsigned int     *num_syms; /* per level/group */
275         darray(xkb_keysym_t) syms;
276 };
277
278 struct xkb_client_map {
279     darray(struct xkb_key_type) types;
280     darray(struct xkb_sym_map) key_sym_map;
281     unsigned char *modmap;
282 };
283
284 struct xkb_behavior {
285         unsigned char   type;
286         unsigned char   data;
287 };
288
289 struct xkb_server_map {
290     unsigned char *     explicit;
291
292     darray(union xkb_action) acts;
293     darray(size_t ) key_acts;           /* acts[key_acts[keycode]] */
294     struct xkb_behavior         *behaviors;
295     uint32_t            vmods[XkbNumVirtualMods]; /* vmod -> mod mapping */
296     uint32_t            *vmodmap; /* key -> vmod mapping */
297 };
298
299
300 struct xkb_indicator_map {
301         unsigned char   flags;
302         unsigned char   which_groups;
303         unsigned char   groups;
304         unsigned char   which_mods;
305         struct xkb_mods mods;
306         unsigned int    ctrls;
307 };
308
309 struct xkb_indicator {
310         struct xkb_indicator_map        maps[XkbNumIndicators];
311 };
312
313 struct xkb_key_name {
314         char    name[XkbKeyNameLength];
315 };
316
317 struct xkb_key_alias {
318         char    real[XkbKeyNameLength];
319         char    alias[XkbKeyNameLength];
320 };
321
322 struct xkb_names {
323     char *keycodes;
324     char *symbols;
325     char *keytypes;
326     char *compat;
327
328     char *vmods[XkbNumVirtualMods];
329     char *indicators[XkbNumIndicators];
330     char *groups[XkbNumKbdGroups];
331
332     darray(struct xkb_key_name) keys;
333     darray(struct xkb_key_alias) key_aliases;
334 };
335
336 struct xkb_controls {
337     unsigned char num_groups;
338     /* unsigned char groups_wrap; */
339     /* struct xkb_mods internal; */
340     /* struct xkb_mods ignore_lock; */
341     unsigned int enabled_ctrls;
342     /* unsigned short repeat_delay; */
343     /* unsigned short repeat_interval; */
344     /* unsigned short slow_keys_delay; */
345     /* unsigned short debounce_delay; */
346     /* unsigned short ax_options; */
347     /* unsigned short ax_timeout; */
348     /* unsigned short axt_opts_mask; */
349     /* unsigned short axt_opts_values; */
350     /* unsigned int axt_ctrls_mask; */
351     /* unsigned int axt_ctrls_values; */
352     unsigned char *per_key_repeat;
353 };
354
355 /* Common keyboard description structure */
356 struct xkb_keymap {
357     struct xkb_context  *ctx;
358
359     unsigned int        refcnt;
360     unsigned short      flags;
361     xkb_keycode_t       min_key_code;
362     xkb_keycode_t       max_key_code;
363
364     struct xkb_controls *      ctrls;
365     struct xkb_server_map *    server;
366     struct xkb_client_map *    map;
367     struct xkb_indicator *     indicators;
368     struct xkb_names *        names;
369     struct xkb_compat_map *    compat;
370 };
371
372 #define XkbNumGroups(g)             ((g) & 0x0f)
373 #define XkbOutOfRangeGroupInfo(g)   ((g) & 0xf0)
374 #define XkbOutOfRangeGroupAction(g) ((g) & 0xc0)
375 #define XkbOutOfRangeGroupNumber(g) (((g) & 0x30) >> 4)
376 #define XkbSetGroupInfo(g, w, n)    (((w) & 0xc0) | (((n) & 3) << 4) | ((g) & 0x0f))
377 #define XkbSetNumGroups(g, n)       (((g) & 0xf0) | ((n) & 0x0f))
378
379 #define XkbKeyGroupInfo(d, k) \
380     (darray_item((d)->map->key_sym_map, k).group_info)
381 #define XkbKeyNumGroups(d, k) \
382     (XkbNumGroups(darray_item((d)->map->key_sym_map, k).group_info))
383 #define XkbKeyGroupWidth(d, k, g) \
384     (XkbKeyType(d, k, g)->num_levels)
385 #define XkbKeyGroupsWidth(d, k) \
386     (darray_item((d)->map->key_sym_map, k).width)
387 #define XkbKeyTypeIndex(d, k, g) \
388     (darray_item((d)->map->key_sym_map, k).kt_index[g & 0x3])
389 #define XkbKeyType(d, k, g) \
390     (&darray_item((d)->map->types, XkbKeyTypeIndex(d, k, g)))
391 #define XkbKeyNumSyms(d, k, g, sl) \
392     (darray_item((d)->map->key_sym_map, k).num_syms[(g * XkbKeyGroupsWidth(d, k)) + sl])
393 #define XkbKeySym(d, k, n) \
394     (&darray_item(darray_item((d)->map->key_sym_map, k).syms, n))
395 #define XkbKeySymOffset(d, k, g, sl) \
396     (darray_item((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) \
400     (darray_item((d)->server->key_acts, k) != 0)
401 #define XkbKeyNumActions(d, k) \
402     (XkbKeyHasActions(d, k) ? \
403      (XkbKeyGroupsWidth(d, k) * XkbKeyNumGroups(d, k)) : \
404      1)
405 #define XkbKeyActionsPtr(d, k) \
406     (darray_mem((d)->server->acts, darray_item((d)->server->key_acts, k)))
407 #define XkbKeyAction(d, k, n) \
408     (XkbKeyHasActions(d, k) ? &XkbKeyActionsPtr(d, k)[n] : NULL)
409 #define XkbKeyActionEntry(d, k, sl, g) \
410     (XkbKeyHasActions(d, k) ? \
411      XkbKeyAction(d, k, ((XkbKeyGroupsWidth(d, k) * (g)) + (sl))) : \
412      NULL)
413
414 #define XkbKeycodeInRange(d, k) \
415     (((k) >= (d)->min_key_code) && ((k) <= (d)->max_key_code))
416
417 typedef uint32_t xkb_atom_t;
418
419 #define XKB_ATOM_NONE 0
420
421 xkb_atom_t
422 xkb_atom_intern(struct xkb_context *ctx, const char *string);
423
424 char *
425 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
426
427 const char *
428 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
429
430 extern unsigned int
431 xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key);
432
433 extern unsigned int
434 xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
435                   unsigned int group);
436
437 extern int
438 xkb_key_get_syms_by_level(struct xkb_keymap *keymap, xkb_keycode_t key,
439                           unsigned int group, unsigned int level,
440                           const xkb_keysym_t **syms_out);
441
442 /**
443  * Deprecated entrypoint for legacy users who need to be able to compile
444  * XKB keymaps by KcCGST (Keycodes + Compat + Geometry + Symbols + Types)
445  * names.
446  *
447  * You should not use this unless you are the X server.  This entrypoint
448  * may well disappear in future releases.  Please, please, don't use it.
449  *
450  * Geometry will be ignored since xkbcommon does not support it in any way.
451  */
452 struct xkb_keymap *
453 xkb_map_new_from_kccgst(struct xkb_context *ctx,
454                         const struct xkb_component_names *kccgst,
455                         enum xkb_map_compile_flags flags);
456
457 extern int
458 xkb_context_take_file_id(struct xkb_context *ctx);
459
460 extern bool
461 XkbcComputeEffectiveMap(struct xkb_keymap *keymap, struct xkb_key_type *type,
462                         unsigned char *map_rtrn);
463
464 extern int
465 XkbcInitCanonicalKeyTypes(struct xkb_keymap *keymap, unsigned which,
466                           int keypadVMod);
467
468 extern unsigned
469 _XkbcKSCheckCase(xkb_keysym_t sym);
470
471 extern xkb_keycode_t
472 XkbcFindKeycodeByName(struct xkb_keymap *keymap, const char *name,
473                       bool use_aliases);
474
475 #define _XkbKSLower (1 << 0)
476 #define _XkbKSUpper (1 << 1)
477
478 #define XkbcKSIsLower(k) (_XkbcKSCheckCase(k) & _XkbKSLower)
479 #define XkbcKSIsUpper(k) (_XkbcKSCheckCase(k) & _XkbKSUpper)
480
481 #define XkbKSIsKeypad(k) (((k) >= XKB_KEY_KP_Space) && ((k) <= XKB_KEY_KP_Equal))
482
483 #endif /* XKB_PRIV_H */