summaryrefslogtreecommitdiff
path: root/src/map.c
blob: 114b0de8694643433d05e78819f59d92e8e6cc36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
 * Copyright © 2012 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Author: Daniel Stone <daniel@fooishbar.org>
 */

/************************************************************
 * Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without
 * fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation, and that the name of Silicon Graphics not be
 * used in advertising or publicity pertaining to distribution
 * of the software without specific prior written permission.
 * Silicon Graphics makes no representation about the suitability
 * of this software for any purpose. It is provided "as is"
 * without any express or implied warranty.
 *
 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * ********************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "utils.h"
#include "xkbcommon/xkbcommon.h"
#include "XKBcommonint.h"
#include "xkballoc.h"

/**
 * Returns the total number of modifiers active in the keymap.
 */
xkb_mod_index_t
xkb_map_num_mods(struct xkb_desc *xkb)
{
    xkb_mod_index_t i;

    for (i = 0; i < XkbNumVirtualMods; i++)
        if (!xkb->names->vmods[i])
            break;

    /* We always have all the core modifiers (for now), plus any virtual
     * modifiers we may have defined, and then shift to one-based indexing. */
    return i + Mod5MapIndex + 1;
}

/**
 * Return the name for a given modifier.
 */
const char *
xkb_map_mod_get_name(struct xkb_desc *xkb, xkb_mod_index_t idx)
{
    if (idx >= xkb_map_num_mods(xkb))
        return NULL;

    /* First try to find a legacy modifier name. */
    switch (idx) {
    case ShiftMapIndex:
        return "Shift";
    case ControlMapIndex:
        return "Control";
    case LockMapIndex:
        return "Caps Lock";
    case Mod1MapIndex:
        return "Mod1";
    case Mod2MapIndex:
        return "Mod2";
    case Mod3MapIndex:
        return "Mod3";
    case Mod4MapIndex:
        return "Mod4";
    case Mod5MapIndex:
        return "Mod5";
    default:
        break;
    }

    /* If that fails, try to find a virtual mod name. */
    return xkb->names->vmods[idx - Mod5MapIndex];
}

/**
 * Returns the index for a named modifier.
 */
xkb_mod_index_t
xkb_map_mod_get_index(struct xkb_desc *xkb, const char *name)
{
    xkb_mod_index_t i;

    if (strcasecmp(name, "Shift") == 0)
        return ShiftMapIndex;
    if (strcasecmp(name, "Control") == 0)
        return ControlMapIndex;
    if (strcasecmp(name, "Caps Lock") == 0)
        return LockMapIndex;
    if (strcasecmp(name, "Mod1") == 0)
        return Mod1MapIndex;
    if (strcasecmp(name, "Mod2") == 0)
        return Mod2MapIndex;
    if (strcasecmp(name, "Mod3") == 0)
        return Mod3MapIndex;
    if (strcasecmp(name, "Mod4") == 0)
        return Mod4MapIndex;
    if (strcasecmp(name, "Mod5") == 0)
        return Mod5MapIndex;

    for (i = 0; i < XkbNumVirtualMods && xkb->names->vmods[i]; i++) {
        if (strcasecmp(name, xkb->names->vmods[i]) == 0)
            return i + Mod5MapIndex;
    }

    return XKB_GROUP_INVALID;
}

/**
 * Return the total number of active groups in the keymap.
 */
xkb_group_index_t
xkb_map_num_groups(struct xkb_desc *xkb)
{
    xkb_group_index_t ret = 0;
    xkb_group_index_t i;

    for (i = 0; i < XkbNumKbdGroups; i++)
        if (xkb->compat->groups[i].mask)
            ret++;

    return ret;
}

/**
 * Returns the name for a given group.
 */
const char *
xkb_map_group_get_name(struct xkb_desc *xkb, xkb_group_index_t idx)
{
    if (idx >= xkb_map_num_groups(xkb))
        return NULL;

    return xkb->names->groups[idx];
}

/**
 * Returns the index for a named group.
 */
xkb_group_index_t
xkb_map_group_get_index(struct xkb_desc *xkb, const char *name)
{
    xkb_group_index_t num_groups = xkb_map_num_groups(xkb);
    xkb_group_index_t i;

    for (i = 0; i < num_groups; i++) {
        if (strcasecmp(xkb->names->groups[i], name) == 0)
            return i;
    }

    return XKB_GROUP_INVALID;
}

/**
 * Returns the number of groups active for a particular key.
 */
xkb_group_index_t
xkb_key_num_groups(struct xkb_desc *xkb, xkb_keycode_t key)
{
    return XkbKeyNumGroups(xkb, key);
}

/**
 * Return the total number of active LEDs in the keymap.
 */
xkb_led_index_t
xkb_map_num_leds(struct xkb_desc *xkb)
{
    xkb_led_index_t ret = 0;
    xkb_led_index_t i;

    for (i = 0; i < XkbNumIndicators; i++)
        if (xkb->indicators->maps[i].which_groups ||
            xkb->indicators->maps[i].which_mods ||
            xkb->indicators->maps[i].ctrls)
            ret++;

    return ret;
}

/**
 * Returns the name for a given group.
 */
const char *
xkb_map_led_get_name(struct xkb_desc *xkb, xkb_led_index_t idx)
{
    if (idx >= xkb_map_num_leds(xkb))
        return NULL;

    return xkb->names->indicators[idx];
}

/**
 * Returns the index for a named group.
 */
xkb_group_index_t
xkb_map_led_get_index(struct xkb_desc *xkb, const char *name)
{
    xkb_led_index_t num_leds = xkb_map_num_leds(xkb);
    xkb_led_index_t i;

    for (i = 0; i < num_leds; i++) {
        if (strcasecmp(xkb->names->indicators[i], name) == 0)
            return i;
    }

    return XKB_LED_INVALID;
}

/**
 * Returns the level to use for the given key and state, or -1 if invalid.
 */
unsigned int
xkb_key_get_level(struct xkb_state *state, xkb_keycode_t key,
                  unsigned int group)
{
    struct xkb_key_type *type = XkbKeyType(state->xkb, key, group);
    unsigned int active_mods = state->mods & type->mods.mask;
    int i;

    for (i = 0; i < type->map_count; i++) {
        if (!type->map[i].active)
            continue;
        if (type->map[i].mods.mask == active_mods)
            return type->map[i].level;
    }

    return 0;
}

/**
 * Returns the group to use for the given key and state, taking
 * wrapping/clamping/etc into account.
 */
unsigned int
xkb_key_get_group(struct xkb_state *state, xkb_keycode_t key)
{
    unsigned int info = XkbKeyGroupInfo(state->xkb, key);
    unsigned int num_groups = XkbKeyNumGroups(state->xkb, key);
    unsigned int ret = state->group;

    if (ret < XkbKeyNumGroups(state->xkb, key))
        return ret;

    switch (XkbOutOfRangeGroupAction(info)) {
    case XkbRedirectIntoRange:
        ret = XkbOutOfRangeGroupInfo(info);
        if (ret >= num_groups)
            ret = 0;
        break;
    case XkbClampIntoRange:
        ret = num_groups - 1;
        break;
    case XkbWrapIntoRange:
    default:
        ret %= num_groups;
        break;
    }

    return ret;
}

/**
 * As below, but takes an explicit group/level rather than state.
 */
unsigned int
xkb_key_get_syms_by_level(struct xkb_desc *xkb, xkb_keycode_t key, unsigned int group,
                          unsigned int level, xkb_keysym_t **syms_out)
{
    *syms_out = &(XkbKeySymEntry(xkb, key, level, group));
    if (**syms_out == XKB_KEYSYM_NO_SYMBOL)
        goto err;

    return 1;

err:
    *syms_out = NULL;
    return 0;
}

/**
 * Provides the symbols to use for the given key and state.  Returns the
 * number of symbols pointed to in syms_out.
 */
unsigned int
xkb_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
                 xkb_keysym_t **syms_out)
{
    struct xkb_desc *xkb = state->xkb;
    int group;
    int level;

    if (!state || key < xkb->min_key_code || key > xkb->max_key_code)
        return -1;

    group = xkb_key_get_group(state, key);
    if (group == -1)
        goto err;
    level = xkb_key_get_level(state, key, group);
    if (level == -1)
        goto err;

    return xkb_key_get_syms_by_level(xkb, key, group, level, syms_out);

err:
    *syms_out = NULL;
    return 0;
}