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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/XKBlib.h>
#include <Xm/MainW.h>
#include <Xm/RowColumn.h>
#include <Xm/ToggleB.h>
Display *theDisplay;
XtAppContext appContext;
int xkbEventBase;
Widget topLevel;
Widget leds[XkbNumIndicators];
Atom ledAtoms[XkbNumIndicators];
XmString ledNames[XkbNumIndicators];
XkbDescPtr xkb_desc;
void valueChangedProc(Widget,XtPointer,XmToggleButtonCallbackStruct *);
XtCallbackRec valueChangedCB[2]={(XtCallbackProc)valueChangedProc,NULL};
/************************************************************************/
/* */
/* Application Resources */
/* */
/************************************************************************/
#define YES 1
#define NO 0
#define DONT_CARE -1
typedef struct
{
int wanted;
int wantAutomatic;
int wantExplicit;
int wantNamed;
int wantReal;
int wantVirtual;
int useUnion;
} OptionsRec;
OptionsRec options;
#define Offset(field) XtOffsetOf(OptionsRec,field)
XtResource resources[] =
{
{"wanted", "Wanted", XtRInt, sizeof(int),
Offset(wanted), XtRImmediate, (XtPointer) DONT_CARE },
{"wantAutomatic", "WantAutomatic", XtRInt, sizeof(int),
Offset(wantAutomatic), XtRImmediate, (XtPointer) DONT_CARE},
{"wantExplicit", "WantExplicit", XtRInt, sizeof(int),
Offset(wantExplicit), XtRImmediate, (XtPointer) DONT_CARE},
{"wantNamed", "WantNamed", XtRInt, sizeof(int),
Offset(wantNamed), XtRImmediate, (XtPointer) DONT_CARE},
{"wantReal", "WantReal", XtRInt, sizeof(int),
Offset(wantReal), XtRImmediate, (XtPointer) DONT_CARE},
{"wantVirtual", "WantVirtual", XtRInt, sizeof(int),
Offset(wantVirtual), XtRImmediate, (XtPointer) DONT_CARE},
{"useUnion", "UseUnion", XtRInt, sizeof(int),
Offset(useUnion), XtRImmediate, (XtPointer) YES},
NULL
};
#undef Offset
String fallbackResources[] =
{
"*mainWindow.width: 100",
"*mainWindow.height: 50",
NULL
};
XrmOptionDescRec optionDesc[] =
{
{"-watch", "*wanted", XrmoptionSepArg, (XtPointer) "0"},
{"-automatic", "*wantAutomatic", XrmoptionNoArg, (XtPointer) "0"},
{"+automatic", "*wantAutomatic", XrmoptionNoArg, (XtPointer) "1"},
{"-explicit", "*wantExplicit", XrmoptionNoArg, (XtPointer) "0"},
{"+explicit", "*wantExplicit", XrmoptionNoArg, (XtPointer) "1"},
{"-named", "*wantNamed", XrmoptionNoArg, (XtPointer) "0"},
{"+named", "*wantNamed", XrmoptionNoArg, (XtPointer) "1"},
{"-real", "*wantReal", XrmoptionNoArg, (XtPointer) "0"},
{"+real", "*wantReal", XrmoptionNoArg, (XtPointer) "1"},
{"-virtual", "*wantVirtual", XrmoptionNoArg, (XtPointer) "0"},
{"+virtual", "*wantVirtual", XrmoptionNoArg, (XtPointer) "1"},
{"-intersection", "*useUnion", XrmoptionNoArg, (XtPointer) "0"},
{"-union", "*useUnion", XrmoptionNoArg, (XtPointer) "1"}
};
/************************************************************************/
/* */
/* usage */
/* */
/************************************************************************/
void usage(char *program)
{
printf("Usage: %s <options>\n",program);
printf("Legal options include the usual X toolkit options plus:\n");
printf(" -help Print this message\n");
printf(" -indpy <name> Name of display to watch\n");
printf(" -watch <leds> Mask of LEDs to watch\n");
printf(" [-+]automatic (Don't) watch automatic LEDs\n");
printf(" [-+]explicit (Don't) watch explicit LEDs\n");
printf(" [-+]named (Don't) watch named LEDs\n");
printf(" [-+]real (Don't) watch real LEDs\n");
printf(" [-+]virtual (Don't) watch virtual LEDs\n");
printf(" -intersection Watch only LEDs in all desired sets\n");
printf(" -union Watch LEDs in any desired sets\n");
printf("The default set of LEDs is -intersection +named +virtual\n");
return;
}
/************************************************************************/
/* */
/* XkbEventHandler */
/* */
/* DESCRIPTION: */
/* */
/* Handles events generated by the Xkb server extension. */
/* */
/************************************************************************/
Boolean XkbEventHandler(XEvent *event)
{
XkbEvent *xkbEv = (XkbEvent *) event;
if (xkbEv->any.xkb_type==XkbIndicatorStateNotify) {
register int i;
register unsigned bit;
for (i=0,bit=1;i<XkbNumIndicators;i++,bit<<=1)
if ((xkbEv->indicators.changed&bit)&&(leds[i]))
{
if (xkbEv->indicators.state&bit)
XmToggleButtonSetState(leds[i],True,False);
else
XmToggleButtonSetState(leds[i],False,False);
}
}
else if (xkbEv->any.xkb_type==XkbIndicatorMapNotify) {
unsigned change= xkbEv->indicators.changed;
if (XkbGetIndicatorMap(theDisplay,change,xkb_desc)!=Success)
fprintf(stderr,"Couldn't get changed indicator maps\n");
}
return True;
} /* XkbEventHandler */
/************************************************************************/
/* */
/* InitXkb */
/* */
/************************************************************************/
Boolean InitXkb(Display *theDisplay)
{
int i,opcode,errorBase,major,minor;
XkbDescPtr xkb;
unsigned int bit;
unsigned int real,virtual,named,explicit,automatic;
char *name;
if (!XkbQueryExtension(theDisplay,
&opcode,
&xkbEventBase,
&errorBase,
&major,
&minor))
return False;
if (!XkbUseExtension(theDisplay,&major,&minor))
return False;
XkbSelectEvents(theDisplay,
XkbUseCoreKbd,
XkbIndicatorStateNotifyMask|XkbIndicatorMapNotifyMask,
XkbIndicatorStateNotifyMask|XkbIndicatorMapNotifyMask);
XtSetEventDispatcher(theDisplay,
xkbEventBase+XkbEventCode,
XkbEventHandler);
xkb=XkbGetMap(theDisplay,0,XkbUseCoreKbd);
real=virtual=named=explicit=automatic=0;
if (!xkb)
{
fprintf(stderr,"Couldn't get keymap\n");
return False;
}
if (XkbGetIndicatorMap(theDisplay,XkbAllIndicatorsMask,xkb)!=Success)
{
fprintf(stderr,"Couldn't read indicator map\n");
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return False;
}
real=virtual=named=explicit=automatic=0;
if (XkbGetNames(theDisplay,XkbIndicatorNamesMask,xkb)!=Success)
{
fprintf(stderr,"Couldn't read indicator names\n");
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return False;
}
real=virtual=named=explicit=automatic=0;
for (i=0,bit=1;i<XkbNumIndicators;i++,bit<<=1)
{
XkbIndicatorMapPtr map= &xkb->indicators->maps[i];
name = NULL;
if (xkb->names->indicators[i]!=None)
{
named|= bit;
name = XGetAtomName(theDisplay,xkb->names->indicators[i]);
}
if (name != NULL)
{
ledAtoms[i] = xkb->names->indicators[i];
ledNames[i] = XmStringCreate(name,XmSTRING_DEFAULT_CHARSET);
}
else
{
char temp[12];
sprintf(temp,"led%d\0",i+1);
ledAtoms[i] = None;
ledNames[i] = XmStringCreate(temp,XmSTRING_DEFAULT_CHARSET);
}
if (xkb->indicators->phys_indicators&bit)
real|= bit;
if ((((map->which_groups!=0)&&(map->groups!=0))||
((map->which_mods!=0)&&
((map->mods.real_mods!=0)||(map->mods.vmods!=0)))||
(map->ctrls!=0))&&
((map->flags&XkbIM_NoAutomatic)==0)) {
automatic|= bit;
}
else explicit|= bit;
}
virtual = ~real;
if (options.useUnion)
{
if ((options.wantReal==NO) || (options.wantReal==DONT_CARE))
real = 0;
if ((options.wantVirtual==NO) || (options.wantVirtual==DONT_CARE))
virtual = 0;
if ((options.wantNamed==NO) || (options.wantNamed==DONT_CARE))
named = 0;
if ((options.wantAutomatic==NO) || (options.wantAutomatic==DONT_CARE))
automatic = 0;
if ((options.wantExplicit==NO) || (options.wantExplicit==DONT_CARE))
explicit = 0;
options.wanted |= real|virtual|named|automatic|explicit;
}
else
{
if (options.wanted == DONT_CARE)
options.wanted = ~0;
if (options.wantReal==NO)
real = ~real;
else if (options.wantReal==DONT_CARE)
real = ~0;
if (options.wantVirtual==NO)
virtual = ~virtual;
else if (options.wantVirtual==DONT_CARE)
virtual = ~0;
if (options.wantNamed==NO)
named = ~named;
else if (options.wantNamed==DONT_CARE)
named = ~0;
if (options.wantAutomatic==NO)
automatic = ~automatic;
else if (options.wantAutomatic==DONT_CARE)
automatic = ~0;
if (options.wantExplicit==NO)
explicit = ~explicit;
else if (options.wantExplicit==DONT_CARE)
explicit = ~0;
options.wanted &= real&virtual&named&automatic&explicit;
}
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return True;
} /* InitXkb */
/************************************************************************/
/* */
/* valueChangedProc - called when a toggle button is pressed. */
/* */
/************************************************************************/
void valueChangedProc(Widget w,
XtPointer clientData,
XmToggleButtonCallbackStruct *callbackData)
{
int led = (int) clientData;
XkbDescPtr xkb;
xkb = XkbGetMap(theDisplay,0,XkbUseCoreKbd);
if (!xkb)
{
fprintf(stderr,"XkbGetMap failed\n");
return;
}
if (XkbGetIndicatorMap(theDisplay,XkbAllIndicatorsMask,xkb)!=Success)
{
fprintf(stderr,"GetIndicatorMap failed\n");
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return;
}
/* The 'flags' field tells whether this indicator is automatic
* (XkbIM_NoExplicit - 0x80), explicit (XkbIM_NoAutomatic - 0x40),
* or neither (both - 0xC0).
*
* If NoAutomatic is set, the server ignores the rest of the
* fields in the indicator map (i.e. it disables automatic control
* of the LED). If NoExplicit is set, the server prevents clients
* from explicitly changing the value of the LED (using the core
* protocol *or* XKB). If NoAutomatic *and* NoExplicit are set,
* the LED cannot be changed (unless you change the map first).
* If neither NoAutomatic nor NoExplicit are set, the server will
* change the LED according to the indicator map, but clients can
* override that (until the next automatic change) using the core
* protocol or XKB.
*/
switch (xkb->indicators->maps[led].flags &
(XkbIM_NoExplicit|XkbIM_NoAutomatic))
{
case XkbIM_NoExplicit|XkbIM_NoAutomatic:
{
XmToggleButtonSetState(w,!callbackData->set,FALSE);
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return;
}
case XkbIM_NoAutomatic:
{
if (ledAtoms[led] != None)
XkbSetNamedIndicator(theDisplay,XkbUseCoreKbd,
ledAtoms[led],callbackData->set,
FALSE,NULL);
else
{
XKeyboardControl xkc;
xkc.led= led;
if (callbackData->set)
xkc.led_mode= LedModeOn;
else xkc.led_mode= LedModeOff;
XChangeKeyboardControl(theDisplay,KBLed|KBLedMode,&xkc);
XSync(theDisplay,0);
}
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return;
}
case XkbIM_NoExplicit:
break;
}
/* The 'ctrls' field tells what controls tell this indicator to
* to turn on: RepeatKeys (0x1), SlowKeys (0x2), BounceKeys (0x4),
* StickyKeys (0x8), MouseKeys (0x10), AccessXKeys (0x20),
* TimeOut (0x40), Feedback (0x80), ToggleKeys (0x100),
* Overlay1 (0x200), Overlay2 (0x400), GroupsWrap (0x800),
* InternalMods (0x1000), IgnoreLockMods (0x2000),
* PerKeyRepeat (0x3000), or ControlsEnabled (0x4000)
*/
if (xkb->indicators->maps[led].ctrls)
{
unsigned long which = xkb->indicators->maps[led].ctrls;
XkbGetControls(theDisplay,XkbAllControlsMask,xkb);
if (callbackData->set)
xkb->ctrls->enabled_ctrls |= which;
else
xkb->ctrls->enabled_ctrls &= ~which;
XkbSetControls(theDisplay,which|XkbControlsEnabledMask,xkb);
}
/* The 'which_groups' field tells when this indicator turns on
* for the 'groups' field: base (0x1), latched (0x2), locked (0x4),
* or effective (0x8).
*/
if (xkb->indicators->maps[led].groups)
{
int i;
unsigned int group = 1;
/* Turning on a group indicator is kind of tricky. For
* now, we will just Latch or Lock the first group we find
* if that is what this indicator does. Otherwise, we're
* just going to punt and get out of here.
*/
if (callbackData->set)
{
for (i = XkbNumKbdGroups-1; i >= 0; i--)
if ((1 << i) &
xkb->indicators->maps[led].groups)
group = i;
if (xkb->indicators->maps[led].which_groups &
(XkbIM_UseLocked | XkbIM_UseEffective))
XkbLockGroup(theDisplay,XkbUseCoreKbd,group);
else if (xkb->indicators->maps[led].which_groups&XkbIM_UseLatched)
XkbLatchGroup(theDisplay,XkbUseCoreKbd,group);
else
{
XmToggleButtonSetState(w,!callbackData->set,FALSE);
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return;
}
}
/* Turning off a group indicator will mean that we just
* Lock the first group that this indicator doesn't watch.
*/
else
{
for (i = XkbNumKbdGroups-1; i >= 0; i--)
if (!((1 << i) &
xkb->indicators->maps[led].groups))
group = i;
XkbLockGroup(theDisplay,XkbUseCoreKbd,group);
}
}
/* The 'which_mods' field tells when this indicator turns on
* for the modifiers: base (0x1), latched (0x2), locked (0x4),
* or effective (0x8).
*
* The 'real_mods' field tells whether this turns on when one of
* the real X modifiers is set: Shift (0x1), Lock (0x2), Control (0x4),
* Mod1 (0x8), Mod2 (0x10), Mod3 (0x20), Mod4 (0x40), or Mod5 (0x80).
*
* The 'virtual_mods' field tells whether this turns on when one of
* the virtual modifiers is set.
*
* The 'mask' field tells what real X modifiers the virtual_modifiers
* map to?
*/
if (xkb->indicators->maps[led].mods.real_mods ||
xkb->indicators->maps[led].mods.mask)
{
XkbStateRec state;
unsigned int affect,mods;
affect = (xkb->indicators->maps[led].mods.real_mods |
xkb->indicators->maps[led].mods.mask);
if (callbackData->set)
mods = affect;
else
mods = 0;
if (xkb->indicators->maps[led].which_mods &
(XkbIM_UseLocked | XkbIM_UseEffective))
XkbLockModifiers(theDisplay,XkbUseCoreKbd,affect,mods);
else if (xkb->indicators->maps[led].which_mods &
XkbIM_UseLatched)
XkbLatchModifiers(theDisplay,XkbUseCoreKbd,affect,mods);
else
{
XmToggleButtonSetState(w,!callbackData->set,FALSE);
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
return;
}
}
XkbFreeKeyboard(xkb,XkbAllComponentsMask,True);
} /* valueChangedProc */
/************************************************************************/
/* */
/* InitializeUI */
/* */
/************************************************************************/
void InitializeUI(Widget topLevel)
{
Arg argList[3];
char buf[256];
int i;
unsigned int bit,n;
Widget mainWindow,rowColumn;
XmString tempString;
mainWindow = (Widget) XmCreateMainWindow(topLevel,"mainWindow",NULL,0);
XtManageChild(mainWindow);
rowColumn = (Widget) XmCreateRowColumn(mainWindow,"rowColumn",NULL,0);
XtManageChild(rowColumn);
XkbGetIndicatorState(theDisplay,XkbUseCoreKbd,&n);
for (i=0,bit=1;i<XkbNumIndicators;i++,bit<<=1)
{
if (options.wanted&bit)
{
/* [[[ WDW - If we wanted to be really fancy, we
* would look for a "*ledxx.labelString" value
* in the resource database so the I18N dudes
* can see localized strings. ]]]
*/
XtSetArg(argList[0], XmNlabelString,ledNames[i]);
if (n&bit) XtSetArg(argList[1], XmNset, True);
else XtSetArg(argList[1], XmNset, False);
sprintf(buf,"led%d\0",i);
valueChangedCB[0].closure = (XtPointer) i;
XtSetArg(argList[2], XmNvalueChangedCallback, valueChangedCB);
leds[i]= XmCreateToggleButton(rowColumn,buf,argList,3);
XtManageChild(leds[i]);
}
else
leds[i]=0;
}
} /* InitializeUI */
/************************************************************************/
/* */
/* main */
/* */
/************************************************************************/
#if NeedFunctionPrototypes
int main(int argc,
char *argv[])
#else
int main(argc, argv)
int argc;
char *argv[];
#endif
{
/********************************************************************/
/* */
/* Initialize the toolkit */
/* */
/********************************************************************/
Arg argList[2];
topLevel = XtAppInitialize(&appContext, "xkbleds",
optionDesc, XtNumber(optionDesc),
&argc, argv,
fallbackResources,
NULL, 0);
XtSetArg(argList[0], XtNallowShellResize, TRUE);
XtSetValues(topLevel,argList,1);
XtGetApplicationResources(topLevel, (XtPointer)&options, resources,
XtNumber(resources), NULL, 0);
if (argc > 1)
{
usage(argv[0]);
exit(0);
}
/* Defaults
*/
if ((options.wanted == DONT_CARE) &&
(options.wantReal == DONT_CARE) &&
(options.wantVirtual == DONT_CARE) &&
(options.wantNamed == DONT_CARE) &&
(options.wantAutomatic == DONT_CARE) &&
(options.wantExplicit == DONT_CARE) &&
(options.useUnion == YES))
{
options.wanted = 0;
options.wantReal = YES;
options.wantNamed = YES;
options.wantAutomatic = YES;
}
/********************************************************************/
/* */
/* See if the server has XKB. */
/* */
/********************************************************************/
theDisplay = XtDisplay(topLevel);
if (!InitXkb(theDisplay))
{
fprintf(stderr,"Could not initialize XKB extension.\n");
exit(0);
}
if (options.wanted == 0)
{
fprintf(stderr,"No LED's were selected.\n\n");
usage(argv[0]);
exit(0);
}
/********************************************************************/
/* */
/* Set up the UI and go. */
/* */
/********************************************************************/
XtRealizeWidget(topLevel);
InitializeUI(topLevel);
XtAppMainLoop(appContext);
/* NOT REACHED */
exit(0L);
}
|