summaryrefslogtreecommitdiff
path: root/app/xkbevd/xkbevd.c
blob: 47765703ae4e0baae10d29dce94b28c110885505 (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
/************************************************************
 Copyright (c) 1995 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.

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

#define	DEBUG_VAR xkbevdDebug
#include <X11/Xosdefs.h>
#include <stdlib.h>
#include "xkbevd.h"


#define	lowbit(x)	((x) & (-(x)))

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

#ifndef DFLT_XKBEVD_CONFIG
#define DFLT_XKBEVD_CONFIG "%s/.xkb/xkbevd.cf"
#endif /* DFLT_XKBEVD_CONFIG */

#ifndef DFLT_XKB_CONFIG_ROOT
#define	DFLT_XKB_CONFIG_ROOT "/usr/X11R6/lib/xkb"
#endif

#ifndef DFLT_SYS_XKBEVD_CONFIG
#define DFLT_SYS_XKBEVD_CONFIG "%s/xkbevd.cf"
#endif /* DFLT_SYS_XKBEVD_CONFIG */

#ifndef DFLT_SOUND_CMD
#define	DFLT_SOUND_CMD "/usr/sbin/sfplay -q"
#endif /* DFLT_SOUND_CMD */

#ifndef DFLT_SOUND_DIR
#define	DFLT_SOUND_DIR "/usr/share/data/sounds/prosonus/"
#endif /* DFLT_SOUND_DIR */

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

static char *	dpyName=	NULL;
Display *	dpy=		NULL;
static const char *	cfgFileName=	NULL;
int		xkbOpcode=	0;
int		xkbEventCode=	0;
Bool		detectableRepeat= False;

static
CfgEntryPtr	config=		NULL;
static
unsigned long	eventMask=	0;

static Bool	synch=		False;
static int	verbose=	0;
static Bool	background=	False;

static const char *	soundCmd=	NULL;
static const char *	soundDir=	NULL;

XkbDescPtr	xkb=		NULL;

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

static void
Usage(int argc, char *argv[])
{
    fprintf(stderr, "Usage: %s [options]...\n%s", argv[0],
	    "Legal options:\n"
	    "-?, -help            Print this message\n"
	    "-cfg <file>          Specify a config file\n"
	    "-sc <cmd>            Specify the command to play sounds\n"
	    "-sd <dir>            Specify the root directory for sound files\n"
	    "-d[isplay] <dpy>     Specify the display to watch\n"
	    "-bg                  Run in background\n"
	    "-synch               Force synchronization\n"
	    "-v                   Print verbose messages\n");
    return;
}

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

static Bool
parseArgs(int argc, char *argv[])
{
register int i;

    for (i=1;i<argc;i++) {
	if (strcmp(argv[i],"-bg")==0) {
	    background= True;
	}
	else if (strcmp(argv[i],"-cfg")==0) {
	    if (i>=(argc-1)) {
		uError("No configuration file specified on command line\n");
		uAction("Trailing %s argument ignored\n",argv[i]);
	    }
	    else {
		char *name= argv[++i];
		if (cfgFileName!=NULL) {
		    if (uStringEqual(cfgFileName,name))
			uWarning("Config file \"%s\" specified twice!\n",
				 name);
		    else {
			uWarning("Multiple config files on command line\n");
			uAction("Using \"%s\", ignoring \"%s\"\n",name,
								cfgFileName);
		    }
		}
		cfgFileName= name;
	    }
	}
	else if ((strcmp(argv[i],"-d")==0)||(strcmp(argv[i],"-display")==0)) {
	    if (i>=(argc-1)) {
		uError("No display specified on command line\n");
		uAction("Trailing %s argument ignored\n",argv[i]);
	    }
	    else {
		char *name= argv[++i];
		if (dpyName!=NULL) {
		    if (uStringEqual(dpyName,name))
			uWarning("Display \"%s\" specified twice!\n",
				 name);
		    else {
			uWarning("Multiple displays on command line\n");
			uAction("Using \"%s\", ignoring \"%s\"\n",name,
								dpyName);
		    }
		}
		dpyName= name;
	    }
	}
	else if (strcmp(argv[i],"-sc")==0) {
	    if (i>=(argc-1)) {
		uError("No sound command specified on command line\n");
		uAction("Trailing %s argument ignored\n",argv[i]);
	    }
	    else {
		char *name= argv[++i];
		if (soundCmd!=NULL) {
		    if (uStringEqual(soundCmd,name))
			uWarning("Sound command \"%s\" specified twice!\n",
				 name);
		    else {
			uWarning("Multiple sound commands on command line\n");
			uAction("Using \"%s\", ignoring \"%s\"\n",name,
								soundCmd);
		    }
		}
		soundCmd= name;
	    }
	}
	else if (strcmp(argv[i],"-sd")==0) {
	    if (i>=(argc-1)) {
		uError("No sound directory specified on command line\n");
		uAction("Trailing %s argument ignored\n",argv[i]);
	    }
	    else {
		char *name= argv[++i];
		if (soundDir!=NULL) {
		    if (uStringEqual(soundDir,name))
			uWarning("Sound directory \"%s\" specified twice!\n",
				 name);
		    else {
			uWarning("Multiple sound dirs on command line\n");
			uAction("Using \"%s\", ignoring \"%s\"\n",name,
								soundDir);
		    }
		}
		soundDir= name;
	    }
	}
	else if ((strcmp(argv[i],"-synch")==0)||(strcmp(argv[i],"-s")==0)) {
	    synch= True;
	}
	else if (strcmp(argv[i],"-v")==0) {
	    verbose++;
	}
	else if ((strcmp(argv[i],"-?")==0)||(strcmp(argv[i],"-help")==0)) {
	    Usage(argc,argv);
	    exit(0);
	}
	else {
	    uError("Unknown flag \"%s\" on command line\n",argv[i]);
	    Usage(argc,argv);
	    return False;
	}
    }
    if (background == False) {
	eventMask = XkbAllEventsMask;
	verbose++;
    }

    return True;
}

static Display *
GetDisplay(char *program, char *dpyName, int *opcodeRtrn, int *evBaseRtrn)
{
int	mjr,mnr,error;
Display	*dpy;

    mjr= XkbMajorVersion;
    mnr= XkbMinorVersion;
    dpy= XkbOpenDisplay(dpyName,evBaseRtrn,NULL,&mjr,&mnr,&error);
    if (dpy==NULL) {
	switch (error) {
	    case XkbOD_BadLibraryVersion:
		uInformation("%s was compiled with XKB version %d.%02d\n",
				program,XkbMajorVersion,XkbMinorVersion);
		uError("X library supports incompatible version %d.%02d\n",
				mjr,mnr);
		break;
	    case XkbOD_ConnectionRefused:
		uError("Cannot open display \"%s\"\n",dpyName);
		break;
	    case XkbOD_NonXkbServer:
		uError("XKB extension not present on %s\n",dpyName);
		break;
	    case XkbOD_BadServerVersion:
		uInformation("%s was compiled with XKB version %d.%02d\n",
				program,XkbMajorVersion,XkbMinorVersion);
		uError("Server %s uses incompatible version %d.%02d\n",
				dpyName,mjr,mnr);
		break;
	    default:
		uInternalError("Unknown error %d from XkbOpenDisplay\n",error);
	}
    }
    else if (synch)
	XSynchronize(dpy,True);
    if (opcodeRtrn)
	XkbQueryExtension(dpy,opcodeRtrn,evBaseRtrn,NULL,&mjr,&mnr);
    return dpy;
}

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

void
InterpretConfigs(CfgEntryPtr cfg)
{
char *		name;
unsigned	priv= 0;

    config= cfg;
    while (cfg!=NULL) {
	name= cfg->name.str;
	if (cfg->entry_type==VariableDef) {
	    if (uStrCaseEqual(name,"sounddirectory")||
					uStrCaseEqual(name,"sounddir")) {
		if (soundDir==NULL) {
		    soundDir= cfg->action.text;
		    cfg->name.str= NULL;
		    cfg->action.text= NULL;
		}
	    }
	    else if (uStrCaseEqual(name,"soundcommand")||
				uStrCaseEqual(name,"soundcmd")) {
		if (soundCmd==NULL) {
		    soundCmd= cfg->action.text;
		    cfg->name.str= NULL;
		    cfg->action.text= NULL;
		}
	    }
	    else {
		uWarning("Assignment to unknown variable \"%s\"\n", name);
		uAction("Ignored\n");
	    }
	}
	else if (cfg->entry_type==EventDef) switch (cfg->event_type) {
	    case XkbBellNotify:
		if (name!=NULL)	cfg->name.atom= XInternAtom(dpy,name,False);
		else 		cfg->name.atom= None;
		if (name) free(name);
		break;
	    case XkbAccessXNotify:
		priv= 0;
		if (name==NULL)
		     priv= XkbAllNewKeyboardEventsMask;
		else if (uStrCaseEqual(name,"skpress"))
		     priv= XkbAXN_SKPressMask;
		else if (uStrCaseEqual(name,"skaccept"))
		     priv= XkbAXN_SKAcceptMask;
		else if (uStrCaseEqual(name,"skreject"))
		     priv= XkbAXN_SKRejectMask;
		else if (uStrCaseEqual(name,"skrelease"))
		     priv= XkbAXN_SKReleaseMask;
		else if (uStrCaseEqual(name,"bkaccept"))
		     priv= XkbAXN_BKAcceptMask;
		else if (uStrCaseEqual(name,"bkreject"))
		     priv= XkbAXN_BKRejectMask;
		else if (uStrCaseEqual(name,"warning"))
		     priv= XkbAXN_AXKWarningMask;
		if (name)	free(name);
		cfg->name.priv= priv;
		break;
	    case XkbActionMessage:
		/* nothing to do */
		break;
	}
	eventMask|= (1L<<cfg->event_type);
	cfg= cfg->next;
    }
    while ((config)&&(config->entry_type!=EventDef)) {
	CfgEntryPtr next;
	if (config->name.str)		free(config->name.str);
	if (config->action.text)	free(config->action.text);
	config->name.str= 	NULL;
	config->action.text=	NULL;
	next= 			config->next;
	free(config);
	config= next;
    }
    cfg= config;
    while ((cfg!=NULL)&&(cfg->next!=NULL)) {
	CfgEntryPtr next;
	next= cfg->next;
	if (next->entry_type!=EventDef) {
	    if (next->name.str)		free(config->name.str);
	    if (next->action.text)	free(config->action.text);
	    next->name.str=		NULL;
	    next->action.text=		NULL;
	    cfg->next= 			next->next;
	    next->next=			NULL;
	    free(next);
	}
	else cfg= next;
    }
    return;
}

static CfgEntryPtr
FindMatchingConfig(XkbEvent *ev)
{
CfgEntryPtr	cfg,dflt;

    dflt= NULL;
    for (cfg= config;(cfg!=NULL);cfg=cfg->next) {
	if ((ev->type!=xkbEventCode)||(cfg->event_type!=ev->any.xkb_type))
	    continue;
	switch (ev->any.xkb_type) {
	   case XkbBellNotify:
		if (ev->bell.name==cfg->name.atom)
		    return cfg;
		else if ((cfg->name.atom==None)&&(dflt==NULL))
		    dflt= cfg;
		break;
	    case XkbAccessXNotify:
		if (cfg->name.priv&(1L<<ev->accessx.detail))
		    return cfg;
		break;
	    case XkbActionMessage:
		if (cfg->name.str==NULL)
		    dflt= cfg;
		else if (strncmp(cfg->name.str,ev->message.message,
						XkbActionMessageLength)==0)
		    return cfg;
		break;
	   default:
	        uInternalError("Can't handle type %d XKB events yet, Sorry.\n",
			       ev->any.xkb_type);
		break;
	}
    }
    return dflt;
}

static Bool
ProcessMatchingConfig(XkbEvent *ev)
{
CfgEntryPtr	cfg;
char		buf[1024],*cmd;
int		ok;

    cfg= FindMatchingConfig(ev);
    if (!cfg) {
	if (verbose)
	    PrintXkbEvent(stdout,ev);
	return False;
    }
    if (cfg->action.type==UnknownAction) {
	if (cfg->action.text==NULL)
	    cfg->action.type= NoAction;
	else if (cfg->action.text[0]=='!') {
	    char *tmp;
	    cfg->action.type= ShellAction;
	    tmp= uStringDup(&cfg->action.text[1]);
	    free(cfg->action.text);
	    cfg->action.text= tmp;
	}
	else cfg->action.type= SoundAction;
    }
    switch (cfg->action.type) {
	case NoAction:
	    return True;
	case EchoAction:
	    if (cfg->action.text!=NULL) {
		sprintf(buf,"%s",cfg->action.text);
		cmd= SubstituteEventArgs(buf,ev);
		printf("%s",cmd);
	    }
	    return True;
	case PrintEvAction:
	    PrintXkbEvent(stdout,ev);
	    return True;
	case ShellAction:
	    if (cfg->action.text==NULL) {
		uWarning("Empty shell command!\n");
		uAction("Ignored\n");
		return True;
	    }
	    cmd= cfg->action.text;
	    break;
	case SoundAction:
	    if (cfg->action.text==NULL) {
		uWarning("Empty sound command!\n");
		uAction("Ignored\n");
		return True;
	    }
	    sprintf(buf,"%s %s%s",soundCmd,soundDir,cfg->action.text);
	    cmd= buf;
	    break;
	default:
	    uInternalError("Unknown error action type %d\n",cfg->action.type);
	    return False;
    }
    cmd= SubstituteEventArgs(cmd,ev);
    if (verbose)
	uInformation("Executing shell command \"%s\"\n",cmd);
    ok= (system(cmd)==0);
    return ok;
}

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

int
main(int argc, char *argv[])
{
FILE 	*	file;
static char 	buf[1024];
XkbEvent	ev;
Bool		ok;


    yyin = stdin;
    uSetEntryFile(NullString);
    uSetDebugFile(NullString);
    uSetErrorFile(NullString);

    if (!parseArgs(argc,argv))
	exit(1);
    file= NULL;
    XkbInitAtoms(NULL);
    if (cfgFileName==NULL) {
	char *home;
	home= (char *)getenv("HOME");
	sprintf(buf,DFLT_XKBEVD_CONFIG,(home?home:""));
	cfgFileName= buf;
    }
    if (uStringEqual(cfgFileName,"-")) {
	static const char *in= "stdin";
	file= stdin;
	cfgFileName= in;
    }
    else {
	file= fopen(cfgFileName,"r");
	if (file==NULL) { /* no personal config, try for a system one */
	    if (cfgFileName!=buf) { /* user specified a file.  bail */
		uError("Can't open config file \"%s\n",cfgFileName);
		uAction("Exiting\n");
		exit(1);
	    }
	    sprintf(buf,DFLT_SYS_XKBEVD_CONFIG,DFLT_XKB_CONFIG_ROOT);
	    file= fopen(cfgFileName,"r");
	    if (file==NULL && !eventMask) {
		if (verbose) {
		    uError("Couldn't find a config file anywhere\n");
		    uAction("Exiting\n");
		    exit(1);
		}
		exit(0);
	    }
	}
    }

    if (background) {
	if (fork()!=0) {
	    if (verbose)
		uInformation("Running in the background\n");
	    exit(0);
	}
    }
    dpy= GetDisplay(argv[0],dpyName,&xkbOpcode,&xkbEventCode);
    if (!dpy)
	goto BAILOUT;
    ok= True;
    setScanState(cfgFileName,1);
    CFGParseFile(file);
    if (!config && !eventMask) {
	uError("No configuration specified in \"%s\"\n",cfgFileName);
	goto BAILOUT;
    }
    if (eventMask==0) {
	uError("No events to watch in \"%s\"\n",cfgFileName);
	goto BAILOUT;
    }
    if (!XkbSelectEvents(dpy,XkbUseCoreKbd,eventMask,eventMask)) {
	uError("Couldn't select desired XKB events\n");
	goto BAILOUT;
    }
    xkb= XkbGetKeyboard(dpy,XkbGBN_AllComponentsMask,XkbUseCoreKbd);
    if (eventMask&XkbBellNotifyMask) {
	unsigned ctrls,vals;
	if (verbose)
	   uInformation("Temporarily disabling the audible bell\n");
	if (!XkbChangeEnabledControls(dpy,XkbUseCoreKbd,XkbAudibleBellMask,0)) {
	    uError("Couldn't disable audible bell\n");
	    goto BAILOUT;
	}
	ctrls= vals= XkbAudibleBellMask;
	if (!XkbSetAutoResetControls(dpy,XkbAudibleBellMask,&ctrls,&vals)) {
	    uWarning("Couldn't configure audible bell to reset on exit\n");
	    uAction("Audible bell might remain off\n");
	}
    }
    if (soundCmd==NULL)	soundCmd= DFLT_SOUND_CMD;
    if (soundDir==NULL)	soundDir= DFLT_SOUND_DIR;
    XkbStdBellEvent(dpy,None,0,XkbBI_ImAlive);
    while (1) {
	XNextEvent(dpy,&ev.core);
	if ((!ProcessMatchingConfig(&ev))&&(ev.type==xkbEventCode)&&
					(ev.any.xkb_type==XkbBellNotify)) {
	    XkbForceDeviceBell(dpy,ev.bell.device,
				ev.bell.bell_class,ev.bell.bell_id,
				ev.bell.percent);
	}
    }

    XCloseDisplay(dpy);
    return (ok==0);
BAILOUT:
    uAction("Exiting\n");
    if (dpy!=NULL)
	XCloseDisplay(dpy);
    exit(1);
}