summaryrefslogtreecommitdiff
path: root/indicators.c
blob: 470091edd2072bdd66cf321065073c948904d68c (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/************************************************************
 Copyright (c) 1994 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.

 ********************************************************/

#include "xkbcomp.h"
#include "misc.h"
#include "tokens.h"
#include "expr.h"
#include "vmod.h"
#include "indicators.h"
#include "action.h"
#include "compat.h"

/***====================================================================***/

#define	ReportIndicatorBadType(d,l,f,w)	\
		ReportBadType("indicator map",(f),\
		      	XkbAtomText((d),(l)->name,XkbMessage),(w))
#define	ReportIndicatorNotArray(d,l,f)	\
		ReportNotArray("indicator map",(f),\
			XkbAtomText((d),(l)->name,XkbMessage))

/***====================================================================***/

void
ClearIndicatorMapInfo(Display * dpy, LEDInfo * info)
{
    info->name = XkbInternAtom(dpy, "default", False);
    info->indicator = _LED_NotBound;
    info->flags = info->which_mods = info->real_mods = 0;
    info->vmods = 0;
    info->which_groups = info->groups = 0;
    info->ctrls = 0;
    return;
}

LEDInfo *
AddIndicatorMap(LEDInfo *oldLEDs, const LEDInfo *new)
{
    LEDInfo *old, *last;

    last = NULL;
    for (old = oldLEDs; old != NULL; old = (LEDInfo *) old->defs.next)
    {
        if (old->name == new->name)
        {
            unsigned collide;

            if ((old->real_mods == new->real_mods) &&
                (old->vmods == new->vmods) &&
                (old->groups == new->groups) &&
                (old->ctrls == new->ctrls) &&
                (old->which_mods == new->which_mods) &&
                (old->which_groups == new->which_groups))
            {
                old->defs.defined |= new->defs.defined;
                return oldLEDs;
            }
            if (new->defs.merge == MergeReplace)
            {
                CommonInfo *next = old->defs.next;
                if (((old->defs.fileID == new->defs.fileID)
                     && (warningLevel > 0)) || (warningLevel > 9))
                {
                    WARN("Map for indicator %s redefined\n",
                          XkbAtomText(NULL, old->name, XkbMessage));
                    ACTION("Earlier definition ignored\n");
                }
                *old = *new;
                old->defs.next = next;
                return oldLEDs;
            }
            collide = 0;
            if (UseNewField(_LED_Index, &old->defs, &new->defs, &collide))
            {
                old->indicator = new->indicator;
                old->defs.defined |= _LED_Index;
            }
            if (UseNewField(_LED_Mods, &old->defs, &new->defs, &collide))
            {
                old->which_mods = new->which_mods;
                old->real_mods = new->real_mods;
                old->vmods = new->vmods;
                old->defs.defined |= _LED_Mods;
            }
            if (UseNewField(_LED_Groups, &old->defs, &new->defs, &collide))
            {
                old->which_groups = new->which_groups;
                old->groups = new->groups;
                old->defs.defined |= _LED_Groups;
            }
            if (UseNewField(_LED_Ctrls, &old->defs, &new->defs, &collide))
            {
                old->ctrls = new->ctrls;
                old->defs.defined |= _LED_Ctrls;
            }
            if (UseNewField(_LED_Explicit, &old->defs, &new->defs, &collide))
            {
                old->flags &= ~XkbIM_NoExplicit;
                old->flags |= (new->flags & XkbIM_NoExplicit);
                old->defs.defined |= _LED_Explicit;
            }
            if (UseNewField(_LED_Automatic, &old->defs, &new->defs, &collide))
            {
                old->flags &= ~XkbIM_NoAutomatic;
                old->flags |= (new->flags & XkbIM_NoAutomatic);
                old->defs.defined |= _LED_Automatic;
            }
            if (UseNewField(_LED_DrivesKbd, &old->defs, &new->defs, &collide))
            {
                old->flags &= ~XkbIM_LEDDrivesKB;
                old->flags |= (new->flags & XkbIM_LEDDrivesKB);
                old->defs.defined |= _LED_DrivesKbd;
            }
            if (collide && (warningLevel > 0))
            {
                WARN("Map for indicator %s redefined\n",
                      XkbAtomText(NULL, old->name, XkbMessage));
                ACTION("Using %s definition for duplicate fields\n",
                        (new->defs.merge == MergeAugment ? "first" : "last"));
            }
            return oldLEDs;
        }
        if (old->defs.next == NULL)
            last = old;
    }
    /* new definition */
    old = malloc(sizeof(LEDInfo));
    if (!old)
    {
        WSGO("Couldn't allocate indicator map\n");
        ACTION("Map for indicator %s not compiled\n",
                XkbAtomText(NULL, new->name, XkbMessage));
        return NULL;
    }
    *old = *new;
    old->defs.next = NULL;
    if (last)
    {
        last->defs.next = &old->defs;
        return oldLEDs;
    }
    return old;
}

static LookupEntry modComponentNames[] = {
    {"base", XkbIM_UseBase}
    ,
    {"latched", XkbIM_UseLatched}
    ,
    {"locked", XkbIM_UseLocked}
    ,
    {"effective", XkbIM_UseEffective}
    ,
    {"compat", XkbIM_UseCompat}
    ,
    {"any", XkbIM_UseAnyMods}
    ,
    {"none", 0}
    ,
    {NULL, 0}
};
static LookupEntry groupComponentNames[] = {
    {"base", XkbIM_UseBase}
    ,
    {"latched", XkbIM_UseLatched}
    ,
    {"locked", XkbIM_UseLocked}
    ,
    {"effective", XkbIM_UseEffective}
    ,
    {"any", XkbIM_UseAnyGroup}
    ,
    {"none", 0}
    ,
    {NULL, 0}
};

int
SetIndicatorMapField(LEDInfo *led, XkbDescPtr xkb, const char *field,
                     const ExprDef *arrayNdx, const ExprDef *value)
{
    ExprResult rtrn;
    Bool ok;

    ok = True;
    if ((uStrCaseCmp(field, "modifiers") == 0)
        || (uStrCaseCmp(field, "mods") == 0))
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveModMask(value, &rtrn, LookupVModMask, (XPointer) xkb))
            return ReportIndicatorBadType(xkb->dpy, led, field,
                                          "modifier mask");
        led->real_mods = rtrn.uval & 0xff;
        led->vmods = (rtrn.uval >> 8) & 0xff;
        led->defs.defined |= _LED_Mods;
    }
    else if (uStrCaseCmp(field, "groups") == 0)
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveMask
            (value, &rtrn, SimpleLookup, (XPointer) groupNames))
            return ReportIndicatorBadType(xkb->dpy, led, field, "group mask");
        led->groups = rtrn.uval;
        led->defs.defined |= _LED_Groups;
    }
    else if ((uStrCaseCmp(field, "controls") == 0) ||
             (uStrCaseCmp(field, "ctrls") == 0))
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveMask
            (value, &rtrn, SimpleLookup, (XPointer) ctrlNames))
            return ReportIndicatorBadType(xkb->dpy, led, field,
                                          "controls mask");
        led->ctrls = rtrn.uval;
        led->defs.defined |= _LED_Ctrls;
    }
    else if (uStrCaseCmp(field, "allowexplicit") == 0)
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveBoolean(value, &rtrn, NULL, NULL))
            return ReportIndicatorBadType(xkb->dpy, led, field, "boolean");
        if (rtrn.uval)
            led->flags &= ~XkbIM_NoExplicit;
        else
            led->flags |= XkbIM_NoExplicit;
        led->defs.defined |= _LED_Explicit;
    }
    else if ((uStrCaseCmp(field, "whichmodstate") == 0) ||
             (uStrCaseCmp(field, "whichmodifierstate") == 0))
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveMask(value, &rtrn, SimpleLookup,
                             (XPointer) modComponentNames))
        {
            return ReportIndicatorBadType(xkb->dpy, led, field,
                                          "mask of modifier state components");
        }
        led->which_mods = rtrn.uval;
    }
    else if (uStrCaseCmp(field, "whichgroupstate") == 0)
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveMask(value, &rtrn, SimpleLookup,
                             (XPointer) groupComponentNames))
        {
            return ReportIndicatorBadType(xkb->dpy, led, field,
                                          "mask of group state components");
        }
        led->which_groups = rtrn.uval;
    }
    else if ((uStrCaseCmp(field, "driveskbd") == 0) ||
             (uStrCaseCmp(field, "driveskeyboard") == 0) ||
             (uStrCaseCmp(field, "leddriveskbd") == 0) ||
             (uStrCaseCmp(field, "leddriveskeyboard") == 0) ||
             (uStrCaseCmp(field, "indicatordriveskbd") == 0) ||
             (uStrCaseCmp(field, "indicatordriveskeyboard") == 0))
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveBoolean(value, &rtrn, NULL, NULL))
            return ReportIndicatorBadType(xkb->dpy, led, field, "boolean");
        if (rtrn.uval)
            led->flags |= XkbIM_LEDDrivesKB;
        else
            led->flags &= ~XkbIM_LEDDrivesKB;
        led->defs.defined |= _LED_DrivesKbd;
    }
    else if (uStrCaseCmp(field, "index") == 0)
    {
        if (arrayNdx != NULL)
            return ReportIndicatorNotArray(xkb->dpy, led, field);
        if (!ExprResolveInteger(value, &rtrn, NULL, NULL))
            return ReportIndicatorBadType(xkb->dpy, led, field,
                                          "indicator index");
        if ((rtrn.uval < 1) || (rtrn.uval > 32))
        {
            ERROR("Illegal indicator index %d (range 1..%d)\n",
                   rtrn.uval, XkbNumIndicators);
            ACTION("Index definition for %s indicator ignored\n",
                    XkbAtomText(NULL, led->name, XkbMessage));
            return False;
        }
        led->indicator = rtrn.uval;
        led->defs.defined |= _LED_Index;
    }
    else
    {
        ERROR("Unknown field %s in map for %s indicator\n", field,
               XkbAtomText(NULL, led->name, XkbMessage));
        ACTION("Definition ignored\n");
        ok = False;
    }
    return ok;
}

LEDInfo *
HandleIndicatorMapDef(IndicatorMapDef *def, XkbDescPtr xkb,
                      const LEDInfo *dflt, LEDInfo *oldLEDs, unsigned merge)
{
    LEDInfo led;
    Bool ok;

    if (def->merge != MergeDefault)
        merge = def->merge;

    led = *dflt;
    led.defs.merge = merge;
    led.name = def->name;

    ok = True;
    for (VarDef *var = def->body; var != NULL;
         var = (VarDef *) var->common.next)
    {
        ExprResult elem, field;
        ExprDef *arrayNdx;
        if (!ExprResolveLhs(var->name, &elem, &field, &arrayNdx))
        {
            ok = False;
            continue;
        }
        if (elem.str != NULL)
        {
            ERROR
                ("Cannot set defaults for \"%s\" element in indicator map\n",
                 elem.str);
            ACTION("Assignment to %s.%s ignored\n", elem.str, field.str);
            ok = False;
        }
        else
        {
            ok = SetIndicatorMapField(&led, xkb, field.str, arrayNdx,
                                      var->value) && ok;
        }
    }
    if (ok)
    {
        LEDInfo *rtrn = AddIndicatorMap(oldLEDs, &led);
        return rtrn;
    }
    return NULL;
}

Bool
CopyIndicatorMapDefs(XkbFileInfo * result, LEDInfo * leds,
                     LEDInfo ** unboundRtrn)
{
    LEDInfo *led, *next;
    LEDInfo *unbound, *last;
    XkbDescPtr xkb;

    xkb = result->xkb;
    if (XkbAllocNames(xkb, XkbIndicatorNamesMask, 0, 0) != Success)
    {
        WSGO("Couldn't allocate names\n");
        ACTION("Indicator names may be incorrect\n");
    }
    if (XkbAllocIndicatorMaps(xkb) != Success)
    {
        WSGO("Can't allocate indicator maps\n");
        ACTION("Indicator map definitions may be lost\n");
        return False;
    }
    last = unbound = (unboundRtrn ? *unboundRtrn : NULL);
    while ((last != NULL) && (last->defs.next != NULL))
    {
        last = (LEDInfo *) last->defs.next;
    }
    for (led = leds; led != NULL; led = next)
    {
        next = (LEDInfo *) led->defs.next;
        if ((led->groups != 0) && (led->which_groups == 0))
            led->which_groups = XkbIM_UseEffective;
        if ((led->which_mods == 0) && ((led->real_mods) || (led->vmods)))
            led->which_mods = XkbIM_UseEffective;
        if ((led->indicator == _LED_NotBound) || (!xkb->indicators))
        {
            if (unboundRtrn != NULL)
            {
                led->defs.next = NULL;
                if (last != NULL)
                    last->defs.next = (CommonInfo *) led;
                else
                    unbound = led;
                last = led;
            }
            else
                free(led);
        }
        else
        {
            XkbIndicatorMapPtr im;
            im = &xkb->indicators->maps[led->indicator - 1];
            im->flags = led->flags;
            im->which_groups = led->which_groups;
            im->groups = led->groups;
            im->which_mods = led->which_mods;
            im->mods.mask = led->real_mods;
            im->mods.real_mods = led->real_mods;
            im->mods.vmods = led->vmods;
            im->ctrls = led->ctrls;
            if (xkb->names != NULL)
                xkb->names->indicators[led->indicator - 1] = led->name;
            free(led);
        }
    }
    if (unboundRtrn != NULL)
    {
        *unboundRtrn = unbound;
    }
    return True;
}

Bool
BindIndicators(XkbFileInfo * result,
               Bool force, LEDInfo * unbound, LEDInfo ** unboundRtrn)
{
    XkbDescPtr xkb;
    LEDInfo *led, *next, *last;

    xkb = result->xkb;
    if (xkb->names != NULL)
    {
        for (led = unbound; led != NULL; led = (LEDInfo *) led->defs.next)
        {
            if (led->indicator == _LED_NotBound)
            {
                for (int i = 0; i < XkbNumIndicators; i++)
                {
                    if (xkb->names->indicators[i] == led->name)
                    {
                        led->indicator = i + 1;
                        break;
                    }
                }
            }
        }
        if (force)
        {
            for (led = unbound; led != NULL; led = (LEDInfo *) led->defs.next)
            {
                if (led->indicator == _LED_NotBound)
                {
                    for (int i = 0; i < XkbNumIndicators; i++)
                    {
                        if (xkb->names->indicators[i] == None)
                        {
                            xkb->names->indicators[i] = led->name;
                            led->indicator = i + 1;
                            xkb->indicators->phys_indicators &= ~(1 << i);
                            break;
                        }
                    }
                    if (led->indicator == _LED_NotBound)
                    {
                        ERROR("No unnamed indicators found\n");
                        ACTION
                            ("Virtual indicator map \"%s\" not bound\n",
                             XkbAtomGetString(xkb->dpy, led->name));
                        continue;
                    }
                }
            }
        }
    }
    for (last = NULL, led = unbound; led != NULL; led = next)
    {
        next = (LEDInfo *) led->defs.next;
        if (led->indicator == _LED_NotBound)
        {
            if (force)
            {
                unbound = next;
                free(led);
            }
            else
            {
                if (last)
                    last->defs.next = &led->defs;
                else
                    unbound = led;
                last = led;
            }
        }
        else
        {
            if ((xkb->names != NULL) &&
                (xkb->names->indicators[led->indicator - 1] != led->name))
            {
                Atom old = xkb->names->indicators[led->indicator - 1];
                ERROR("Multiple names bound to indicator %d\n",
                       (unsigned int) led->indicator);
                ACTION("Using %s, ignoring %s\n",
                        XkbAtomGetString(xkb->dpy, old),
                        XkbAtomGetString(xkb->dpy, led->name));
                led->indicator = _LED_NotBound;
                if (force)
                {
                    free(led);
                    unbound = next;
                }
                else
                {
                    if (last)
                        last->defs.next = &led->defs;
                    else
                        unbound = led;
                    last = led;
                }
            }
            else
            {
                XkbIndicatorMapPtr map;
                map = &xkb->indicators->maps[led->indicator - 1];
                map->flags = led->flags;
                map->which_groups = led->which_groups;
                map->groups = led->groups;
                map->which_mods = led->which_mods;
                map->mods.mask = led->real_mods;
                map->mods.real_mods = led->real_mods;
                map->mods.vmods = led->vmods;
                map->ctrls = led->ctrls;
                if (last)
                    last->defs.next = &next->defs;
                else
                    unbound = next;
                led->defs.next = NULL;
                free(led);
            }
        }
    }
    if (unboundRtrn)
    {
        *unboundRtrn = unbound;
    }
    else if (unbound)
    {
        for (led = unbound; led != NULL; led = next)
        {
            next = (LEDInfo *) led->defs.next;
            free(led);
        }
    }
    return True;
}