6ed98846f44a07dfd191bcece8a8e562b0d6e30c
[platform/upstream/libxkbcommon.git] / include / xkbcommon / xkbcommon.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 #ifndef _XKBCOMMON_H_
56 #define _XKBCOMMON_H_
57
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <X11/Xfuncproto.h>
61 #include <X11/extensions/XKB.h>
62
63 typedef uint32_t xkb_keycode_t;
64 typedef uint32_t xkb_keysym_t;
65
66 #define XKB_KEYCODE_MAX                 (0xffffffff - 1)
67 #define xkb_keycode_is_legal_ext(kc)    (kc <= XKB_KEYCODE_MAX)
68 #define xkb_keycode_is_legal_x11(kc)    (kc <= XKB_KEYCODE_MAX)
69 #define xkb_keymap_keycode_range_is_legal(xkb) \
70     (xkb->max_key_code > 0 && \
71      xkb->max_key_code > xkb->min_key_code && \
72      xkb_keycode_is_legal_ext(xkb->min_key_code) && \
73      xkb_keycode_is_legal_ext(xkb->max_key_code))
74
75 /* Duplicate the modifier mask defines so libxkcommon can be used
76  * without X.h */
77 #define XKB_COMMON_SHIFT_MASK           (1 << 0)
78 #define XKB_COMMON_LOCK_MASK            (1 << 1)
79 #define XKB_COMMON_CONTROL_MASK         (1 << 2)
80 #define XKB_COMMON_MOD1_MASK            (1 << 3)
81 #define XKB_COMMON_MOD2_MASK            (1 << 4)
82 #define XKB_COMMON_MOD3_MASK            (1 << 5)
83 #define XKB_COMMON_MOD4_MASK            (1 << 6)
84 #define XKB_COMMON_MOD5_MASK            (1 << 7)
85
86
87 struct xkb_rule_names {
88     const char *  rules;
89     const char *  model;
90     const char *  layout;
91     const char *  variant;
92     const char *  options;
93 };
94
95 struct xkb_any_action {
96     uint8_t   type;
97     uint8_t   data[7];
98 };
99
100 struct xkb_mod_action {
101     uint8_t         type;
102     uint8_t         flags;
103     uint8_t         mask;
104     uint8_t         real_mods;
105     uint16_t        vmods;
106 };
107
108 struct xkb_group_action {
109     uint8_t         type;
110     uint8_t         flags;
111     int16_t         group;
112 };
113
114 struct xkb_iso_action {
115     uint8_t         type;
116     uint8_t         flags;
117     uint8_t         mask;
118     uint8_t         real_mods;
119     uint8_t         group;
120     uint8_t         affect;
121     uint16_t        vmods;
122 };
123
124 struct xkb_controls_action {
125     uint8_t         type;
126     uint8_t         flags;
127     uint32_t        ctrls;
128 };
129
130 struct xkb_device_button_action {
131     uint8_t         type;
132     uint8_t         flags;
133     uint8_t         count;
134     uint8_t         button;
135     uint8_t         device;
136 };
137
138 struct xkb_device_valuator_action {
139     uint8_t         type;
140     uint8_t         device;
141     uint8_t         v1_what;
142     uint8_t         v1_index;
143     uint8_t         v1_value;
144     uint8_t         v2_what;
145     uint8_t         v2_index;
146     uint8_t         v2_value;
147 };
148
149 struct xkb_pointer_default_action {
150     uint8_t         type;
151     uint8_t         flags;
152     uint8_t         affect;
153     uint8_t         value;
154 };
155
156 struct xkb_switch_screen_action {
157     uint8_t         type;
158     uint8_t         flags;
159     uint8_t         screen;
160 };
161
162 struct xkb_redirect_key_action {
163     uint8_t             type;
164     xkb_keycode_t       new_key;
165     uint8_t             mods_mask;
166     uint8_t             mods;
167     uint16_t            vmods_mask;
168     uint16_t            vmods;
169 };
170
171 struct xkb_pointer_action {
172     uint8_t     type;
173     uint8_t     flags;
174     uint16_t    x;
175     uint16_t    y;
176 };
177
178 struct xkb_message_action {
179     uint8_t     type;
180     uint8_t     flags;
181     uint8_t     message[6];
182 };
183
184 struct xkb_pointer_button_action {
185     uint8_t     type;
186     uint8_t     flags;
187     uint8_t     count;
188     uint8_t     button;
189 };
190
191 union xkb_action {
192     struct xkb_any_action             any;
193     struct xkb_mod_action             mods;
194     struct xkb_group_action           group;
195     struct xkb_iso_action             iso;
196     struct xkb_controls_action        ctrls;
197     struct xkb_device_button_action   devbtn;
198     struct xkb_device_valuator_action devval;
199     struct xkb_pointer_default_action dflt;
200     struct xkb_switch_screen_action   screen;
201     struct xkb_redirect_key_action    redirect; /* XXX wholly unnecessary? */
202     struct xkb_pointer_action         ptr; /* XXX delete for DeviceValuator */
203     struct xkb_pointer_button_action  btn; /* XXX delete for DeviceBtn */
204     struct xkb_message_action         msg; /* XXX just delete */
205     unsigned char                     type;
206 };
207
208 struct xkb_mods {
209         uint32_t        mask;   /* effective mods */
210         uint32_t        vmods;
211         uint8_t         real_mods;
212 };
213
214 struct xkb_kt_map_entry {
215         int            active;
216         uint16_t        level;
217         struct xkb_mods     mods;
218 };
219
220 struct xkb_key_type {
221     struct xkb_mods             mods;
222     uint16_t                num_levels;
223     unsigned char           map_count;
224     struct xkb_kt_map_entry *       map;
225     struct xkb_mods *             preserve;
226     const char              *name;
227     const char              **level_names;
228 };
229
230 struct xkb_sym_interpret {
231     xkb_keysym_t    sym;
232     unsigned char   flags;
233     unsigned char   match;
234     uint8_t         mods; /* XXX real or virt? */
235     uint32_t        virtual_mod;
236     struct xkb_any_action   act;
237 };
238
239 struct xkb_compat_map {
240     struct xkb_sym_interpret *      sym_interpret;
241     struct xkb_mods              groups[XkbNumKbdGroups];
242     unsigned short           num_si;
243     unsigned short           size_si;
244 };
245
246 struct xkb_sym_map {
247         unsigned char    kt_index[XkbNumKbdGroups];
248         unsigned char    group_info;
249         unsigned char    width;
250         unsigned short   offset;
251 };
252
253 #define XkbNumGroups(g)                 ((g)&0x0f)
254 #define XkbOutOfRangeGroupInfo(g)       ((g)&0xf0)
255 #define XkbOutOfRangeGroupAction(g)     ((g)&0xc0)
256 #define XkbOutOfRangeGroupNumber(g)     (((g)&0x30)>>4)
257 #define XkbSetGroupInfo(g,w,n)  (((w)&0xc0)|(((n)&3)<<4)|((g)&0x0f))
258 #define XkbSetNumGroups(g,n)    (((g)&0xf0)|((n)&0x0f))
259
260 struct xkb_client_map {
261     unsigned char            size_types;
262     unsigned char            num_types;
263     struct xkb_key_type *           types;
264
265     uint32_t                 size_syms;
266     uint32_t                 num_syms;
267     xkb_keysym_t             *syms;
268     struct xkb_sym_map *             key_sym_map;
269
270     unsigned char           *modmap;
271 };
272
273 struct xkb_behavior {
274         unsigned char   type;
275         unsigned char   data;
276 };
277
278 struct xkb_server_map {
279     unsigned short      num_acts;
280     unsigned short      size_acts;
281
282 #if defined(__cplusplus) || defined(c_plusplus)
283     /* explicit is a C++ reserved word */
284     unsigned char *     c_explicit;
285 #else
286     unsigned char *     explicit;
287 #endif
288
289     union xkb_action          *acts;
290     struct xkb_behavior         *behaviors;
291     unsigned short      *key_acts;
292     unsigned char       *explicits;
293     uint32_t            vmods[XkbNumVirtualMods]; /* vmod -> mod mapping */
294     uint32_t            *vmodmap; /* key -> vmod mapping */
295 };
296
297
298 struct xkb_indicator_map {
299         unsigned char   flags;
300         unsigned char   which_groups;
301         unsigned char   groups;
302         unsigned char   which_mods;
303         struct xkb_mods mods;
304         unsigned int    ctrls;
305 };
306
307 struct xkb_indicator {
308         unsigned long           phys_indicators;
309         struct xkb_indicator_map        maps[XkbNumIndicators];
310 };
311
312 struct xkb_key_name {
313         char    name[XkbKeyNameLength];
314 };
315
316 struct xkb_key_alias {
317         char    real[XkbKeyNameLength];
318         char    alias[XkbKeyNameLength];
319 };
320
321 struct xkb_names {
322     const char            *vmods[XkbNumVirtualMods];
323     const char            *indicators[XkbNumIndicators];
324     const char            *groups[XkbNumKbdGroups];
325     struct xkb_key_name *     keys;
326     struct xkb_key_alias *    key_aliases;
327
328     xkb_keycode_t     num_keys;
329     xkb_keycode_t     num_key_aliases;
330 };
331
332 struct xkb_controls {
333         unsigned char   num_groups;
334         unsigned char   groups_wrap;
335         struct xkb_mods internal;
336         struct xkb_mods ignore_lock;
337         unsigned int    enabled_ctrls;
338         unsigned short  repeat_delay;
339         unsigned short  repeat_interval;
340         unsigned short  slow_keys_delay;
341         unsigned short  debounce_delay;
342         unsigned short  ax_options;
343         unsigned short  ax_timeout;
344         unsigned short  axt_opts_mask;
345         unsigned short  axt_opts_values;
346         unsigned int    axt_ctrls_mask;
347         unsigned int    axt_ctrls_values;
348         unsigned char   *per_key_repeat;
349 };
350
351 /* Common keyboard description structure */
352 struct xkb_desc {
353     unsigned int        defined;
354     unsigned short      flags;
355     unsigned short      device_spec;
356     xkb_keycode_t       min_key_code;
357     xkb_keycode_t       max_key_code;
358
359     struct xkb_controls *      ctrls;
360     struct xkb_server_map *    server;
361     struct xkb_client_map *    map;
362     struct xkb_indicator *     indicators;
363     struct xkb_names *        names;
364     struct xkb_compat_map *    compat;
365 };
366
367
368 #define XkbKeyGroupInfo(d,k)    ((d)->map->key_sym_map[k].group_info)
369 #define XkbKeyNumGroups(d,k)    (XkbNumGroups((d)->map->key_sym_map[k].group_info))
370 #define XkbKeyGroupWidth(d,k,g) (XkbKeyType(d,k,g)->num_levels)
371 #define XkbKeyGroupsWidth(d,k)  ((d)->map->key_sym_map[k].width)
372 #define XkbKeyTypeIndex(d,k,g)  ((d)->map->key_sym_map[k].kt_index[g&0x3])
373 #define XkbKeyType(d,k,g)       (&(d)->map->types[XkbKeyTypeIndex(d,k,g)])
374 #define XkbKeyNumSyms(d,k)      (XkbKeyGroupsWidth(d,k)*XkbKeyNumGroups(d,k))
375 #define XkbKeySymsOffset(d,k)   ((d)->map->key_sym_map[k].offset)
376 #define XkbKeySymsPtr(d,k)      (&(d)->map->syms[XkbKeySymsOffset(d,k)])
377 #define XkbKeySym(d,k,n)        (XkbKeySymsPtr(d,k)[n])
378 #define XkbKeySymEntry(d,k,sl,g) \
379         (XkbKeySym(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))))
380 #define XkbKeyHasActions(d,k)   ((d)->server->key_acts[k]!=0)
381 #define XkbKeyNumActions(d,k)   (XkbKeyHasActions(d,k)?XkbKeyNumSyms(d,k):1)
382 #define XkbKeyActionsPtr(d,k)   (&(d)->server->acts[(d)->server->key_acts[k]])
383 #define XkbKeyAction(d,k,n) \
384         (XkbKeyHasActions(d,k)?&XkbKeyActionsPtr(d,k)[n]:NULL)
385 #define XkbKeyActionEntry(d,k,sl,g) \
386         (XkbKeyHasActions(d,k)?\
387                 XkbKeyAction(d,k,((XkbKeyGroupsWidth(d,k)*(g))+(sl))):NULL)
388
389 #define XkbKeycodeInRange(d,k)  (((k)>=(d)->min_key_code)&&\
390                                  ((k)<=(d)->max_key_code))
391 #define XkbNumKeys(d)           ((d)->max_key_code-(d)->min_key_code+1)
392
393 struct xkb_map_changes {
394         unsigned short           changed;
395         xkb_keycode_t            min_key_code;
396         xkb_keycode_t            max_key_code;
397         unsigned char            first_type;
398         unsigned char            num_types;
399         xkb_keycode_t            first_key_sym;
400         xkb_keycode_t            num_key_syms;
401         xkb_keycode_t            first_key_act;
402         xkb_keycode_t            num_key_acts;
403         xkb_keycode_t            first_key_behavior;
404         xkb_keycode_t            num_key_behaviors;
405         xkb_keycode_t            first_key_explicit;
406         xkb_keycode_t            num_key_explicit;
407         xkb_keycode_t            first_modmap_key;
408         xkb_keycode_t            num_modmap_keys;
409         xkb_keycode_t            first_vmodmap_key;
410         xkb_keycode_t            num_vmodmap_keys;
411         unsigned short           vmods;
412 };
413
414 struct xkb_controls_changes {
415         unsigned int             changed_ctrls;
416         unsigned int             enabled_ctrls_changes;
417         int                      num_groups_changed;
418 };
419
420 struct xkb_indicator_changes {
421         unsigned int             state_changes;
422         unsigned int             map_changes;
423 };
424
425 struct xkb_name_changes {
426         unsigned int            changed;
427         unsigned char           first_type;
428         unsigned char           num_types;
429         unsigned char           first_lvl;
430         unsigned char           num_lvls;
431         xkb_keycode_t           num_aliases;
432         xkb_keycode_t           first_key;
433         xkb_keycode_t           num_keys;
434         unsigned short          changed_vmods;
435         unsigned long           changed_indicators;
436         unsigned char           changed_groups;
437 };
438
439 struct xkb_compat_changes {
440         unsigned char           changed_groups;
441         unsigned short          first_si;
442         unsigned short          num_si;
443 };
444
445 struct xkb_changes {
446         unsigned short           device_spec;
447         unsigned short           state_changes;
448         struct xkb_map_changes   map;
449         struct xkb_controls_changes      ctrls;
450         struct xkb_indicator_changes     indicators;
451         struct xkb_name_changes  names;
452         struct xkb_compat_changes        compat;
453 };
454
455 struct xkb_component_names {
456         char *                   keymap;
457         char *                   keycodes;
458         char *                   types;
459         char *                   compat;
460         char *                   symbols;
461 };
462
463 struct xkb_component_name {
464         unsigned short          flags;
465         char *                  name;
466 };
467
468 struct xkb_component_list {
469         int                     num_keymaps;
470         int                     num_keycodes;
471         int                     num_types;
472         int                     num_compat;
473         int                     num_symbols;
474         struct xkb_component_name *     keymaps;
475         struct xkb_component_name *     keycodes;
476         struct xkb_component_name *     types;
477         struct xkb_component_name *     compat;
478         struct xkb_component_name *     symbols;
479 };
480
481 struct xkb_state {
482         unsigned char   group; /* base + latched + locked */
483         /* FIXME: Why are base + latched short and not char?? */
484         unsigned short  base_group; /* physically ... down? */
485         unsigned short  latched_group;
486         unsigned char   locked_group;
487
488         unsigned char   mods; /* base + latched + locked */
489         unsigned char   base_mods; /* physically down */
490         unsigned char   latched_mods;
491         unsigned char   locked_mods;
492
493         unsigned char   compat_state; /* mods + group for core state */
494
495         /* grab mods = all depressed and latched mods, _not_ locked mods */
496         unsigned char   grab_mods; /* grab mods minus internal mods */
497         unsigned char   compat_grab_mods; /* grab mods + group for core state,
498                                              but not locked groups if
499                                              IgnoreGroupLocks set */
500
501         /* effective mods = all mods (depressed, latched, locked) */
502         unsigned char   lookup_mods; /* effective mods minus internal mods */
503         unsigned char   compat_lookup_mods; /* effective mods + group */
504
505         unsigned short  ptr_buttons; /* core pointer buttons */
506 };
507
508 #define XkbStateFieldFromRec(s) XkbBuildCoreState((s)->lookup_mods,(s)->group)
509 #define XkbGrabStateFromRec(s)  XkbBuildCoreState((s)->grab_mods,(s)->group)
510
511 #define XkbNumGroups(g)                 ((g)&0x0f)
512 #define XkbOutOfRangeGroupInfo(g)       ((g)&0xf0)
513 #define XkbOutOfRangeGroupAction(g)     ((g)&0xc0)
514 #define XkbOutOfRangeGroupNumber(g)     (((g)&0x30)>>4)
515 #define XkbSetNumGroups(g,n)    (((g)&0xf0)|((n)&0x0f))
516
517 _XFUNCPROTOBEGIN
518
519 _X_EXPORT extern struct xkb_desc *
520 xkb_compile_keymap_from_rules(const struct xkb_rule_names *rules);
521
522 _X_EXPORT extern struct xkb_desc *
523 xkb_compile_keymap_from_components(const struct xkb_component_names * ktcsg);
524
525 _X_EXPORT extern struct xkb_desc *
526 xkb_compile_keymap_from_file(FILE *inputFile, const char *mapName);
527
528 _X_EXPORT extern struct xkb_desc *
529 xkb_compile_keymap_from_string(const char *string, const char *mapName);
530
531 _X_EXPORT extern void
532 xkb_free_keymap(struct xkb_desc *xkb);
533
534 _X_EXPORT extern struct xkb_component_list *
535 xkb_list_components(struct xkb_component_names * ptrns, int *maxMatch);
536
537 /*
538  * Canonicalises component names by prepending the relevant component from
539  * 'old' to the one in 'names' when the latter has a leading '+' or '|', and
540  * by replacing a '%' with the relevant component, e.g.:
541  *
542  * names        old           output
543  * ------------------------------------------
544  * +bar         foo           foo+bar
545  * |quux        baz           baz|quux
546  * foo+%|baz    bar           foo+bar|baz
547  *
548  * If a component in names needs to be modified, the existing value will be
549  * free()d, and a new one allocated with malloc().
550  */
551 _X_EXPORT extern void
552 xkb_canonicalise_components(struct xkb_component_names * names,
553                            const struct xkb_component_names * old);
554
555 /*
556  * Converts a keysym to a string; will return unknown Unicode codepoints
557  * as "Ua1b2", and other unknown keysyms as "0xabcd1234".
558  */
559 _X_EXPORT extern void
560 xkb_keysym_to_string(xkb_keysym_t ks, char *buffer, size_t size);
561
562 /*
563  * See xkb_keysym_to_string comments: this function will accept any string
564  * from that function.
565  */
566 _X_EXPORT extern xkb_keysym_t
567 xkb_string_to_keysym(const char *s);
568
569 _X_EXPORT unsigned int
570 xkb_key_get_syms(struct xkb_desc *xkb, struct xkb_state *state,
571                  xkb_keycode_t key, xkb_keysym_t **syms_out);
572
573 _XFUNCPROTOEND
574
575 #endif /* _XKBCOMMON_H_ */