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 | #define DEBUG_VARxkbevdDebug xkbevdDebug |
28 | #include <X11/Xosdefs.h> |
29 | #include <stdlib.h> |
30 | #include "xkbevd.h" |
31 | |
32 | |
33 | #define lowbit(x)((x) & (-(x))) ((x) & (-(x))) |
34 | |
35 | |
36 | |
37 | #ifndef DFLT_XKBEVD_CONFIG"%s/.xkb/xkbevd.cf" |
38 | #define DFLT_XKBEVD_CONFIG"%s/.xkb/xkbevd.cf" "%s/.xkb/xkbevd.cf" |
39 | #endif /* DFLT_XKBEVD_CONFIG */ |
40 | |
41 | #ifndef DFLT_XKB_CONFIG_ROOT"/var/tmp/jhbuild/share/X11/xkb" |
42 | #define DFLT_XKB_CONFIG_ROOT"/var/tmp/jhbuild/share/X11/xkb" "/usr/X11R6/lib/xkb" |
43 | #endif |
44 | |
45 | #ifndef DFLT_SYS_XKBEVD_CONFIG"%s/xkbevd.cf" |
46 | #define DFLT_SYS_XKBEVD_CONFIG"%s/xkbevd.cf" "%s/xkbevd.cf" |
47 | #endif /* DFLT_SYS_XKBEVD_CONFIG */ |
48 | |
49 | #ifndef DFLT_SOUND_CMD"/usr/sbin/sfplay -q" |
50 | #define DFLT_SOUND_CMD"/usr/sbin/sfplay -q" "/usr/sbin/sfplay -q" |
51 | #endif /* DFLT_SOUND_CMD */ |
52 | |
53 | #ifndef DFLT_SOUND_DIR"/usr/share/data/sounds/prosonus/" |
54 | #define DFLT_SOUND_DIR"/usr/share/data/sounds/prosonus/" "/usr/share/data/sounds/prosonus/" |
55 | #endif /* DFLT_SOUND_DIR */ |
56 | |
57 | |
58 | |
59 | static char * dpyName= NULL((void*)0); |
60 | Display * dpy= NULL((void*)0); |
61 | static const char * cfgFileName= NULL((void*)0); |
62 | int xkbOpcode= 0; |
63 | int xkbEventCode= 0; |
64 | Boolint detectableRepeat= False0; |
65 | |
66 | static |
67 | CfgEntryPtr config= NULL((void*)0); |
68 | static |
69 | unsigned long eventMask= 0; |
70 | |
71 | static Boolint synch= False0; |
72 | static int verbose= 0; |
73 | static Boolint background= False0; |
74 | |
75 | static const char * soundCmd= NULL((void*)0); |
76 | static const char * soundDir= NULL((void*)0); |
77 | |
78 | XkbDescPtr xkb= NULL((void*)0); |
79 | |
80 | |
81 | |
82 | static void |
83 | Usage(int argc, char *argv[]) |
84 | { |
85 | fprintf(stderrstderr, "Usage: %s [options]...\n%s", argv[0], |
86 | "Legal options:\n" |
87 | "-?, -help Print this message\n" |
88 | "-cfg <file> Specify a config file\n" |
89 | "-sc <cmd> Specify the command to play sounds\n" |
90 | "-sd <dir> Specify the root directory for sound files\n" |
91 | "-d[isplay] <dpy> Specify the display to watch\n" |
92 | "-bg Run in background\n" |
93 | "-synch Force synchronization\n" |
94 | "-v Print verbose messages\n"); |
95 | return; |
96 | } |
97 | |
98 | |
99 | |
100 | static Boolint |
101 | parseArgs(int argc, char *argv[]) |
102 | { |
103 | register int i; |
104 | |
105 | for (i=1;i<argc;i++) { |
106 | if (strcmp(argv[i],"-bg")==0) { |
107 | background= True1; |
108 | } |
109 | else if (strcmp(argv[i],"-cfg")==0) { |
110 | if (i>=(argc-1)) { |
111 | uError("No configuration file specified on command line\n"); |
112 | uAction("Trailing %s argument ignored\n",argv[i]); |
113 | } |
114 | else { |
115 | char *name= argv[++i]; |
116 | if (cfgFileName!=NULL((void*)0)) { |
117 | if (uStringEqual(cfgFileName,name)((strcmp(cfgFileName,name))==((Comparison)0))) |
118 | uWarning("Config file \"%s\" specified twice!\n", |
119 | name); |
120 | else { |
121 | uWarning("Multiple config files on command line\n"); |
122 | uAction("Using \"%s\", ignoring \"%s\"\n",name, |
123 | cfgFileName); |
124 | } |
125 | } |
126 | cfgFileName= name; |
127 | } |
128 | } |
129 | else if ((strcmp(argv[i],"-d")==0)||(strcmp(argv[i],"-display")==0)) { |
130 | if (i>=(argc-1)) { |
131 | uError("No display specified on command line\n"); |
132 | uAction("Trailing %s argument ignored\n",argv[i]); |
133 | } |
134 | else { |
135 | char *name= argv[++i]; |
136 | if (dpyName!=NULL((void*)0)) { |
137 | if (uStringEqual(dpyName,name)((strcmp(dpyName,name))==((Comparison)0))) |
138 | uWarning("Display \"%s\" specified twice!\n", |
139 | name); |
140 | else { |
141 | uWarning("Multiple displays on command line\n"); |
142 | uAction("Using \"%s\", ignoring \"%s\"\n",name, |
143 | dpyName); |
144 | } |
145 | } |
146 | dpyName= name; |
147 | } |
148 | } |
149 | else if (strcmp(argv[i],"-sc")==0) { |
150 | if (i>=(argc-1)) { |
151 | uError("No sound command specified on command line\n"); |
152 | uAction("Trailing %s argument ignored\n",argv[i]); |
153 | } |
154 | else { |
155 | char *name= argv[++i]; |
156 | if (soundCmd!=NULL((void*)0)) { |
157 | if (uStringEqual(soundCmd,name)((strcmp(soundCmd,name))==((Comparison)0))) |
158 | uWarning("Sound command \"%s\" specified twice!\n", |
159 | name); |
160 | else { |
161 | uWarning("Multiple sound commands on command line\n"); |
162 | uAction("Using \"%s\", ignoring \"%s\"\n",name, |
163 | soundCmd); |
164 | } |
165 | } |
166 | soundCmd= name; |
167 | } |
168 | } |
169 | else if (strcmp(argv[i],"-sd")==0) { |
170 | if (i>=(argc-1)) { |
171 | uError("No sound directory specified on command line\n"); |
172 | uAction("Trailing %s argument ignored\n",argv[i]); |
173 | } |
174 | else { |
175 | char *name= argv[++i]; |
176 | if (soundDir!=NULL((void*)0)) { |
177 | if (uStringEqual(soundDir,name)((strcmp(soundDir,name))==((Comparison)0))) |
178 | uWarning("Sound directory \"%s\" specified twice!\n", |
179 | name); |
180 | else { |
181 | uWarning("Multiple sound dirs on command line\n"); |
182 | uAction("Using \"%s\", ignoring \"%s\"\n",name, |
183 | soundDir); |
184 | } |
185 | } |
186 | soundDir= name; |
187 | } |
188 | } |
189 | else if ((strcmp(argv[i],"-synch")==0)||(strcmp(argv[i],"-s")==0)) { |
190 | synch= True1; |
191 | } |
192 | else if (strcmp(argv[i],"-v")==0) { |
193 | verbose++; |
194 | } |
195 | else if ((strcmp(argv[i],"-?")==0)||(strcmp(argv[i],"-help")==0)) { |
196 | Usage(argc,argv); |
197 | exit(0); |
198 | } |
199 | else { |
200 | uError("Unknown flag \"%s\" on command line\n",argv[i]); |
201 | Usage(argc,argv); |
202 | return False0; |
203 | } |
204 | } |
205 | if (background == False0) { |
206 | eventMask = XkbAllEventsMask(0xFFF); |
207 | verbose++; |
208 | } |
209 | |
210 | return True1; |
211 | } |
212 | |
213 | static Display * |
214 | GetDisplay(char *program, char *dpyName, int *opcodeRtrn, int *evBaseRtrn) |
215 | { |
216 | int mjr,mnr,error; |
217 | Display *dpy; |
218 | |
219 | mjr= XkbMajorVersion1; |
220 | mnr= XkbMinorVersion0; |
221 | dpy= XkbOpenDisplay(dpyName,evBaseRtrn,NULL((void*)0),&mjr,&mnr,&error); |
222 | if (dpy==NULL((void*)0)) { |
223 | switch (error) { |
224 | case XkbOD_BadLibraryVersion1: |
225 | uInformation("%s was compiled with XKB version %d.%02d\n", |
226 | program,XkbMajorVersion1,XkbMinorVersion0); |
227 | uError("X library supports incompatible version %d.%02d\n", |
228 | mjr,mnr); |
229 | break; |
230 | case XkbOD_ConnectionRefused2: |
231 | uError("Cannot open display \"%s\"\n",dpyName); |
232 | break; |
233 | case XkbOD_NonXkbServer3: |
234 | uError("XKB extension not present on %s\n",dpyName); |
235 | break; |
236 | case XkbOD_BadServerVersion4: |
237 | uInformation("%s was compiled with XKB version %d.%02d\n", |
238 | program,XkbMajorVersion1,XkbMinorVersion0); |
239 | uError("Server %s uses incompatible version %d.%02d\n", |
240 | dpyName,mjr,mnr); |
241 | break; |
242 | default: |
243 | uInternalError("Unknown error %d from XkbOpenDisplay\n",error); |
244 | } |
245 | } |
246 | else if (synch) |
247 | XSynchronize(dpy,True1); |
248 | if (opcodeRtrn) |
249 | XkbQueryExtension(dpy,opcodeRtrn,evBaseRtrn,NULL((void*)0),&mjr,&mnr); |
250 | return dpy; |
251 | } |
252 | |
253 | |
254 | |
255 | void |
256 | InterpretConfigs(CfgEntryPtr cfg) |
257 | { |
258 | char * name; |
259 | unsigned priv= 0; |
260 | |
261 | config= cfg; |
262 | while (cfg!=NULL((void*)0)) { |
263 | name= cfg->name.str; |
264 | if (cfg->entry_type==VariableDef1) { |
265 | if (uStrCaseEqual(name,"sounddirectory")((strcasecmp(name,"sounddirectory"))==0)|| |
266 | uStrCaseEqual(name,"sounddir")((strcasecmp(name,"sounddir"))==0)) { |
267 | if (soundDir==NULL((void*)0)) { |
268 | soundDir= cfg->action.text; |
269 | cfg->name.str= NULL((void*)0); |
270 | cfg->action.text= NULL((void*)0); |
271 | } |
272 | } |
273 | else if (uStrCaseEqual(name,"soundcommand")((strcasecmp(name,"soundcommand"))==0)|| |
274 | uStrCaseEqual(name,"soundcmd")((strcasecmp(name,"soundcmd"))==0)) { |
275 | if (soundCmd==NULL((void*)0)) { |
276 | soundCmd= cfg->action.text; |
277 | cfg->name.str= NULL((void*)0); |
278 | cfg->action.text= NULL((void*)0); |
279 | } |
280 | } |
281 | else { |
282 | uWarning("Assignment to unknown variable \"%s\"\n", name); |
283 | uAction("Ignored\n"); |
284 | } |
285 | } |
286 | else if (cfg->entry_type==EventDef0) switch (cfg->event_type) { |
287 | case XkbBellNotify8: |
288 | if (name!=NULL((void*)0)) cfg->name.atom= XInternAtom(dpy,name,False0); |
289 | else cfg->name.atom= None0L; |
290 | if (name) free(name); |
291 | break; |
292 | case XkbAccessXNotify10: |
293 | priv= 0; |
294 | if (name==NULL((void*)0)) |
295 | priv= XkbAllNewKeyboardEventsMask(0x7); |
296 | else if (uStrCaseEqual(name,"skpress")((strcasecmp(name,"skpress"))==0)) |
297 | priv= XkbAXN_SKPressMask(1L << 0); |
298 | else if (uStrCaseEqual(name,"skaccept")((strcasecmp(name,"skaccept"))==0)) |
299 | priv= XkbAXN_SKAcceptMask(1L << 1); |
300 | else if (uStrCaseEqual(name,"skreject")((strcasecmp(name,"skreject"))==0)) |
301 | priv= XkbAXN_SKRejectMask(1L << 2); |
302 | else if (uStrCaseEqual(name,"skrelease")((strcasecmp(name,"skrelease"))==0)) |
303 | priv= XkbAXN_SKReleaseMask(1L << 3); |
304 | else if (uStrCaseEqual(name,"bkaccept")((strcasecmp(name,"bkaccept"))==0)) |
305 | priv= XkbAXN_BKAcceptMask(1L << 4); |
306 | else if (uStrCaseEqual(name,"bkreject")((strcasecmp(name,"bkreject"))==0)) |
307 | priv= XkbAXN_BKRejectMask(1L << 5); |
308 | else if (uStrCaseEqual(name,"warning")((strcasecmp(name,"warning"))==0)) |
309 | priv= XkbAXN_AXKWarningMask(1L << 6); |
310 | if (name) free(name); |
311 | cfg->name.priv= priv; |
312 | break; |
313 | case XkbActionMessage9: |
314 | |
315 | break; |
316 | } |
317 | eventMask|= (1L<<cfg->event_type); |
318 | cfg= cfg->next; |
319 | } |
320 | while ((config)&&(config->entry_type!=EventDef0)) { |
321 | CfgEntryPtr next; |
322 | if (config->name.str) free(config->name.str); |
323 | if (config->action.text) free(config->action.text); |
324 | config->name.str= NULL((void*)0); |
325 | config->action.text= NULL((void*)0); |
326 | next= config->next; |
327 | free(config); |
328 | config= next; |
329 | } |
330 | cfg= config; |
331 | while ((cfg!=NULL((void*)0))&&(cfg->next!=NULL((void*)0))) { |
332 | CfgEntryPtr next; |
333 | next= cfg->next; |
334 | if (next->entry_type!=EventDef0) { |
335 | if (next->name.str) free(config->name.str); |
336 | if (next->action.text) free(config->action.text); |
337 | next->name.str= NULL((void*)0); |
338 | next->action.text= NULL((void*)0); |
339 | cfg->next= next->next; |
340 | next->next= NULL((void*)0); |
341 | free(next); |
342 | } |
343 | else cfg= next; |
344 | } |
345 | return; |
346 | } |
347 | |
348 | static CfgEntryPtr |
349 | FindMatchingConfig(XkbEvent *ev) |
350 | { |
351 | CfgEntryPtr cfg,dflt; |
352 | |
353 | dflt= NULL((void*)0); |
354 | for (cfg= config;(cfg!=NULL((void*)0));cfg=cfg->next) { |
355 | if ((ev->type!=xkbEventCode)||(cfg->event_type!=ev->any.xkb_type)) |
356 | continue; |
357 | switch (ev->any.xkb_type) { |
358 | case XkbBellNotify8: |
359 | if (ev->bell.name==cfg->name.atom) |
360 | return cfg; |
361 | else if ((cfg->name.atom==None0L)&&(dflt==NULL((void*)0))) |
362 | dflt= cfg; |
363 | break; |
364 | case XkbAccessXNotify10: |
365 | if (cfg->name.priv&(1L<<ev->accessx.detail)) |
366 | return cfg; |
367 | break; |
368 | case XkbActionMessage9: |
369 | if (cfg->name.str==NULL((void*)0)) |
370 | dflt= cfg; |
371 | else if (strncmp(cfg->name.str,ev->message.message, |
372 | XkbActionMessageLength6)==0) |
373 | return cfg; |
374 | break; |
375 | default: |
376 | uInternalError("Can't handle type %d XKB events yet, Sorry.\n", |
377 | ev->any.xkb_type); |
378 | break; |
379 | } |
380 | } |
381 | return dflt; |
382 | } |
383 | |
384 | static Boolint |
385 | ProcessMatchingConfig(XkbEvent *ev) |
386 | { |
387 | CfgEntryPtr cfg; |
388 | char buf[1024],*cmd; |
389 | int ok; |
390 | |
391 | cfg= FindMatchingConfig(ev); |
392 | if (!cfg) { |
393 | if (verbose) |
394 | PrintXkbEvent(stdoutstdout,ev); |
395 | return False0; |
396 | } |
397 | if (cfg->action.type==UnknownAction0) { |
398 | if (cfg->action.text==NULL((void*)0)) |
399 | cfg->action.type= NoAction1; |
400 | else if (cfg->action.text[0]=='!') { |
401 | char *tmp; |
402 | cfg->action.type= ShellAction4; |
403 | tmp= uStringDup(&cfg->action.text[1])(strdup(&cfg->action.text[1])); |
404 | free(cfg->action.text); |
405 | cfg->action.text= tmp; |
406 | } |
407 | else cfg->action.type= SoundAction5; |
408 | } |
409 | switch (cfg->action.type) { |
410 | case NoAction1: |
411 | return True1; |
412 | case EchoAction2: |
413 | if (cfg->action.text!=NULL((void*)0)) { |
414 | sprintf(buf,"%s",cfg->action.text); |
415 | cmd= SubstituteEventArgs(buf,ev); |
416 | printf("%s",cmd); |
417 | } |
418 | return True1; |
419 | case PrintEvAction3: |
420 | PrintXkbEvent(stdoutstdout,ev); |
421 | return True1; |
422 | case ShellAction4: |
423 | if (cfg->action.text==NULL((void*)0)) { |
424 | uWarning("Empty shell command!\n"); |
425 | uAction("Ignored\n"); |
426 | return True1; |
427 | } |
428 | cmd= cfg->action.text; |
429 | break; |
430 | case SoundAction5: |
431 | if (cfg->action.text==NULL((void*)0)) { |
432 | uWarning("Empty sound command!\n"); |
433 | uAction("Ignored\n"); |
434 | return True1; |
435 | } |
436 | sprintf(buf,"%s %s%s",soundCmd,soundDir,cfg->action.text); |
437 | cmd= buf; |
438 | break; |
439 | default: |
440 | uInternalError("Unknown error action type %d\n",cfg->action.type); |
441 | return False0; |
442 | } |
443 | cmd= SubstituteEventArgs(cmd,ev); |
444 | if (verbose) |
445 | uInformation("Executing shell command \"%s\"\n",cmd); |
446 | ok= (system(cmd)==0); |
447 | return ok; |
448 | } |
449 | |
450 | |
451 | |
452 | int |
453 | main(int argc, char *argv[]) |
454 | { |
455 | FILE * file; |
456 | static char buf[1024]; |
457 | XkbEvent ev; |
458 | Boolint ok; |
459 | |
460 | |
461 | yyin = stdinstdin; |
462 | uSetEntryFile(NullString((char *)((void*)0))); |
463 | uSetDebugFile(NullString((char *)((void*)0))); |
464 | uSetErrorFile(NullString((char *)((void*)0))); |
465 | |
466 | if (!parseArgs(argc,argv)) |
467 | exit(1); |
468 | file= NULL((void*)0); |
469 | XkbInitAtoms(NULL((void*)0)); |
470 | if (cfgFileName==NULL((void*)0)) { |
471 | char *home; |
472 | home= (char *)getenv("HOME"); |
473 | sprintf(buf,DFLT_XKBEVD_CONFIG"%s/.xkb/xkbevd.cf",(home?home:"")); |
474 | cfgFileName= buf; |
475 | } |
476 | if (uStringEqual(cfgFileName,"-")((strcmp(cfgFileName,"-"))==((Comparison)0))) { |
477 | static const char *in= "stdin"; |
478 | file= stdinstdin; |
479 | cfgFileName= in; |
480 | } |
481 | else { |
482 | file= fopen(cfgFileName,"r"); |
483 | if (file==NULL((void*)0)) { |
484 | if (cfgFileName!=buf) { |
485 | uError("Can't open config file \"%s\n",cfgFileName); |
486 | uAction("Exiting\n"); |
487 | exit(1); |
488 | } |
489 | sprintf(buf,DFLT_SYS_XKBEVD_CONFIG"%s/xkbevd.cf",DFLT_XKB_CONFIG_ROOT"/var/tmp/jhbuild/share/X11/xkb"); |
490 | file= fopen(cfgFileName,"r"); |
491 | if (file==NULL((void*)0) && !eventMask) { |
492 | if (verbose) { |
493 | uError("Couldn't find a config file anywhere\n"); |
494 | uAction("Exiting\n"); |
495 | exit(1); |
496 | } |
497 | exit(0); |
498 | } |
499 | } |
500 | } |
501 | |
502 | if (background) { |
503 | if (fork()!=0) { |
504 | if (verbose) |
505 | uInformation("Running in the background\n"); |
506 | exit(0); |
507 | } |
508 | } |
509 | dpy= GetDisplay(argv[0],dpyName,&xkbOpcode,&xkbEventCode); |
510 | if (!dpy) |
511 | goto BAILOUT; |
512 | ok= True1; |
| Value stored to 'ok' is never read |
513 | setScanState(cfgFileName,1); |
514 | CFGParseFile(file); |
515 | if (!config && !eventMask) { |
516 | uError("No configuration specified in \"%s\"\n",cfgFileName); |
517 | goto BAILOUT; |
518 | } |
519 | if (eventMask==0) { |
520 | uError("No events to watch in \"%s\"\n",cfgFileName); |
521 | goto BAILOUT; |
522 | } |
523 | if (!XkbSelectEvents(dpy,XkbUseCoreKbd0x0100,eventMask,eventMask)) { |
524 | uError("Couldn't select desired XKB events\n"); |
525 | goto BAILOUT; |
526 | } |
527 | xkb= XkbGetKeyboard(dpy,XkbGBN_AllComponentsMask(0xff),XkbUseCoreKbd0x0100); |
528 | if (eventMask&XkbBellNotifyMask(1L << 8)) { |
529 | unsigned ctrls,vals; |
530 | if (verbose) |
531 | uInformation("Temporarily disabling the audible bell\n"); |
532 | if (!XkbChangeEnabledControls(dpy,XkbUseCoreKbd0x0100,XkbAudibleBellMask(1L << 9),0)) { |
533 | uError("Couldn't disable audible bell\n"); |
534 | goto BAILOUT; |
535 | } |
536 | ctrls= vals= XkbAudibleBellMask(1L << 9); |
537 | if (!XkbSetAutoResetControls(dpy,XkbAudibleBellMask(1L << 9),&ctrls,&vals)) { |
538 | uWarning("Couldn't configure audible bell to reset on exit\n"); |
539 | uAction("Audible bell might remain off\n"); |
540 | } |
541 | } |
542 | if (soundCmd==NULL((void*)0)) soundCmd= DFLT_SOUND_CMD"/usr/sbin/sfplay -q"; |
543 | if (soundDir==NULL((void*)0)) soundDir= DFLT_SOUND_DIR"/usr/share/data/sounds/prosonus/"; |
544 | XkbStdBellEvent(dpy,None0L,0,XkbBI_ImAlive24); |
545 | while (1) { |
546 | XNextEvent(dpy,&ev.core); |
547 | if ((!ProcessMatchingConfig(&ev))&&(ev.type==xkbEventCode)&& |
548 | (ev.any.xkb_type==XkbBellNotify8)) { |
549 | XkbForceDeviceBell(dpy,ev.bell.device, |
550 | ev.bell.bell_class,ev.bell.bell_id, |
551 | ev.bell.percent); |
552 | } |
553 | } |
554 | |
555 | XCloseDisplay(dpy); |
556 | return (ok==0); |
557 | BAILOUT: |
558 | uAction("Exiting\n"); |
559 | if (dpy!=NULL((void*)0)) |
560 | XCloseDisplay(dpy); |
561 | exit(1); |
562 | } |