summaryrefslogtreecommitdiff
path: root/xc/unsupported/test/Xkb/xkbctrl.c
blob: c27a0e7e496e6a0b5fbe3dca27b9a5f7edfea528 (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
/* $XConsortium: xkbctrl.c,v 1.2 93/09/28 23:51:33 rws Exp $ */
/************************************************************
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.

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

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <X11/Xproto.h>
#include <X11/Xlib.h>
#include <X11/X.h>
#include <X11/XKBlib.h>

#define	ON	 1
#define	OFF	 0
#define	IGNORE	-1

static	char		*dpyName = NULL;
static	unsigned	 which;
static	XkbControlsRec	 newControls;
static	unsigned	 changeInternal;
static	unsigned	 internal;
static	unsigned	 changeVInternal;
static	unsigned	 vInternal;
static	unsigned	 changeIgnoreLocks;
static	unsigned	 ignoreLocks;
static	unsigned	 changeVIgnoreLocks;
static	unsigned	 vIgnoreLocks;
static	unsigned	 changeEnabled;
static	unsigned	 enabled;
static	int		 synch = 0;
static	int		 device = XkbUseCoreKbd;

int
parseMods(modText,onOff,changes,mods)
    char *modText;
    int   onOff;
    unsigned *changes;
    unsigned *mods;
{
register char *tmp;

    for (tmp=modText;*tmp;tmp++) {
	if ((*tmp=='s')||(*tmp=='S')) {
	    *changes|= ShiftMask;
	    if (onOff)	*mods|= ShiftMask;
	    else	*mods&= ~ShiftMask;
	}
	if ((*tmp=='l')||(*tmp=='L')) {
	    *changes|= LockMask;
	    if (onOff)	*mods|= LockMask;
	    else	*mods&= ~LockMask;
	}
	if ((*tmp=='c')||(*tmp=='C')) {
	    *changes|= ControlMask;
	    if (onOff)	*mods|= ControlMask;
	    else	*mods&= ~ControlMask;
	}
	if ((*tmp=='a')||(*tmp=='A')||(*tmp=='m')||(*tmp=='M')||(*tmp=='1')) {
	    *changes|= Mod1Mask;
	    if (onOff)	*mods|= Mod1Mask;
	    else	*mods&= ~Mod1Mask;
	}
	if (*tmp=='2') {
	    *changes|= Mod2Mask;
	    if (onOff)	*mods|= Mod2Mask;
	    else	*mods&= ~Mod2Mask;
	}
	if (*tmp=='3') {
	    *changes|= Mod3Mask;
	    if (onOff)	*mods|= Mod3Mask;
	    else	*mods&= ~Mod3Mask;
	}
	if (*tmp=='4') {
	    *changes|= Mod4Mask;
	    if (onOff)	*mods|= Mod4Mask;
	    else	*mods&= ~Mod2Mask;
	}
	if (*tmp=='5') {
	    *changes|= Mod5Mask;
	    if (onOff)	*mods|= Mod5Mask;
	    else	*mods&= ~Mod5Mask;
	}
    }
    return 1;
}

int
parseVMods(modText,onOff,changes,mods)
    char *modText;
    int   onOff;
    unsigned *changes;
    unsigned *mods;
{
register char *tmp;

    for (tmp=modText;*tmp;tmp++) {
	if (isdigit(*tmp)) {
	    int ndx= *tmp-'0';
	    *changes|= (1<<ndx);
	    if (onOff)	*mods|= (1<<ndx);
	    else	*mods&= ~(1<<ndx);
	}
	else if ( (*tmp>='a') && (*tmp<='f') ) {
	    int ndx= 10+(*tmp-'a');
	    *changes|= (1<<ndx);
	    if (onOff)	*mods|= (1<<ndx);
	    else	*mods&= ~(1<<ndx);
	}
	else if ( (*tmp>='A') && (*tmp<='F') ) {
	    int ndx= 10+(*tmp-'F');
	    *changes|= (1<<ndx);
	    if (onOff)	*mods|= (1<<ndx);
	    else	*mods&= ~(1<<ndx);
	}
    }
    return 1;
}

#define	E(m)	fprintf(stderr,m)
void
usage(argc,argv)
    int argc;
    char *argv[];
{
    fprintf(stderr,"Usage: %s <options>\n",argv[0]);
    E("Where legal options are:\n");
    E("-display <dpy>                 specifies display to use\n");
    E("[+-]synch                      synchonize on/off\n");
    E("-device  <id>                  specifies device to use\n");
    E("[+-]wrap                       group wrap on/off\n");
    E("[+-]bell                       audible bell on/off\n");
    E("[+-]autoautorepeat             automatic determination of repeating keys on/off\n");
    E("[+-]internal <mods>            set/clear internal modifiers\n");
    E("[+-]ignorelock <mods>          set/clear ignore locks modifiers\n");
    E("                               <mods> can contain one or more of:\n");
    E("                               s:         Shift\n");
    E("                               l:         Lock\n");
    E("                               c:         Control\n");
    E("                               [am]:      Alt/Meta (mod1)\n");
    E("                               [1-5]:     Mod1-Mod5\n");
    E("[+-]vinternal <vmods>          set/clear internal virtual modifiers\n");
    E("[+-]vignorelock <vmods>        set/clear ignore locks virtual mods\n");
    E("                               virtual modifiers are specified by\n");
    E("                               using one hexadecimal digit per vmod\n");
    E("-repeat                        disable RepeatKeys\n");
    E("+repeat [ delay [ ival ] ]     enable RepeatKeys with specified delay\n");
    E("                               and interval\n");
    E("-slow                          disable SlowKeys\n");
    E("+slow [ delay ]                enable SlowKeys with the specified delay\n");
    E("-bounce                        disable BounceKeys\n");
    E("+bounce [timeout]              enable BounceKeys with the specified timeout\n");
    E("[+-]sticky                     enable/disable sticky keys\n");
    E("-mouse                         disable MouseKeys\n");
    E("+mouse [ delay [ interval [ time-to-max [ dflt-button ] ] ] ]\n");
    E("                               enable mouse keys with the specified behavior\n");
    E("-accessx                       disable AccessX hotkeys and timeout\n");
    E("+accessx [timeout]             enable AccessX hotkeys with the specified\n");
    E("                               timeout (0 indicates no timeout)\n");
}

int
parseArgs(argc,argv)
    int argc;
    char *argv[];
{
int i;
int onoff;

    for (i=1;i<argc;i++) {
	 if (argv[i][0]=='-')		onoff= 0;
	 else if (argv[i][0]=='+')	onoff= 1;
	 else {
	     fprintf(stderr,"Options must start with '+' or '-'\n");
	     return 0;
	 }
	 if ( strcmp(argv[i],"-display")==0 ) {
	    if ( ++i<argc )	dpyName= argv[i];
	    else {
		fprintf(stderr,"Must specify a display with -display option\n");
		return 0;
	    }
	}
	else if ( strcmp(argv[i],"-device")==0 ) {
	    if (( (i+1)<argc ) && isdigit(argv[i+1][0]))
		device= atoi(argv[++i]);
	    else {
		fprintf(stderr,"Must specify an id with -device option\n");
		return 0;
	    }
	}
	else if ( strcmp(&argv[i][1],"synch")==0 ) {
	    synch= onoff;
	}
	else if ( strcmp(&argv[i][1],"wrap")==0 ) {
	    which|= XkbControlsEnabledMask;
	    if (onoff)	enabled|= XkbGroupsWrapMask;
	    else	enabled&= ~XkbGroupsWrapMask;
	}
	else if ( strcmp(&argv[i][1],"bell")==0 ) {
	    which|= XkbControlsEnabledMask;
	    if (onoff)	enabled|= XkbAudibleBellMask;
	    else	enabled&= ~XkbAudibleBellMask;
	}
	else if ( strcmp(&argv[i][1],"autoautorepeat")==0 ) {
	    which|= XkbAutoAutorepeatMask;
	    if (onoff)	enabled|= XkbAutoAutorepeatMask;
	    else	enabled&= ~XkbAutoAutorepeatMask;
	}
	else if ( strcmp(&argv[i][1],"internal")==0 ) {
	    which|= XkbInternalModsMask;
	    if (((i+1)>=argc) ||
		(!parseMods(argv[i+1],onoff,&changeInternal,&internal))) {
		fprintf(stderr,"Must specify the internal modifiers\n");
		return 0;
	    }
	    i++;
	}
	else if ( strcmp(&argv[i][1],"ignorelock")==0 ) {
	    which|= XkbIgnoreLockModsMask;
	    if (((i+1)>=argc) || (!parseMods(argv[i+1],onoff,&changeIgnoreLocks,
							&ignoreLocks))) {
		fprintf(stderr,"Must specify the ignore lock modifiers\n");
		return 0;
	    }
	    i++;
	}
	else if ( strcmp(&argv[i][1],"vinternal")==0 ) {
	    which|= XkbInternalModsMask;
	    if (((i+1)>=argc) ||
		(!parseVMods(argv[i+1],onoff,&changeVInternal,&vInternal))) {
		fprintf(stderr,"Must specify the internal virtual modifiers\n");
		return 0;
	    }
	    i++;
	}
	else if ( strcmp(&argv[i][1],"vignorelock")==0 ) {
	    which|= XkbIgnoreLockModsMask;
	    if (((i+1)>=argc) || (!parseVMods(argv[i+1],onoff,
					&changeVIgnoreLocks,&vIgnoreLocks))) {
		fprintf(stderr,"Must specify the ignore lock virtual mods\n");
		return 0;
	    }
	    i++;
	}
	else if ( strcmp(&argv[i][1],"repeat")==0 ) {
	    which|= XkbControlsEnabledMask;
	    changeEnabled|= XkbRepeatKeysMask;
	    enabled|= (onoff?XkbRepeatKeysMask:0);
	    if (onoff) {
		int ok= 0;
		if ( ((i+1)<argc) && (isdigit(argv[i+1][0])) ) {
		    which|= XkbRepeatKeysMask;
		    newControls.repeat_delay= atoi(argv[++i]);
		    ok= 1;
		}
		if (ok && ((i+1)<argc) && (isdigit(argv[i+1][0]))) {
		    newControls.repeat_interval= atoi(argv[++i]);
		}
	    }
	}
	else if ( strcmp(&argv[i][1],"slow")==0 ) {
	    which|= XkbControlsEnabledMask;
	    changeEnabled|= XkbSlowKeysMask;
	    enabled|= (onoff?XkbSlowKeysMask:0);
	    if (onoff) {
		if ( ((i+1)<argc) && (isdigit(argv[i+1][0])) ) {
		    which|= XkbSlowKeysMask;
		    newControls.slow_keys_delay= atoi(argv[++i]);
		}
	    }
	}
	else if ( strcmp(&argv[i][1],"bounce")==0 ) {
	    which|= XkbControlsEnabledMask;
	    changeEnabled|= XkbBounceKeysMask;
	    enabled|= (onoff?XkbBounceKeysMask:0);
	    if (onoff) {
		if ( ((i+1)<argc) && (isdigit(argv[i+1][0])) ) {
		    which|= XkbBounceKeysMask;
		    newControls.debounce_delay= atoi(argv[++i]);
		}
	    }
	}
	else if ( strcmp(&argv[i][1],"sticky")==0 ) {
	    which|= XkbControlsEnabledMask;
	    changeEnabled|= XkbStickyKeysMask;
	    enabled|= (onoff?XkbStickyKeysMask:0);
	}
	else if ( strcmp(&argv[i][1],"mouse")==0 ) {
	    which|= XkbControlsEnabledMask;
	    changeEnabled|= XkbMouseKeysMask;
	    enabled|= (onoff?XkbMouseKeysMask:0);
	    if (onoff) {
		int ok= 0;
		if ( ((i+1)<argc) && (isdigit(argv[i+1][0])) ) {
		    which|= XkbMouseKeysMask;
		    newControls.mouse_keys_delay= atoi(argv[++i]);
		    ok= 1;
		}
		if (ok && ((i+1)<argc) && (isdigit(argv[i+1][0]))) {
		    newControls.mouse_keys_interval= atoi(argv[++i]);
		}
		if (ok && ((i+1)<argc) && (isdigit(argv[i+1][0]))) {
		    newControls.mouse_keys_time_to_max= atoi(argv[++i]);
		}
		if (ok && ((i+1)<argc) && (isdigit(argv[i+1][0]))) {
		    newControls.mouse_keys_curve= atoi(argv[++i]);
		}
		if (ok && ((i+1)<argc) && (isdigit(argv[i+1][0]))) {
		    newControls.mouse_keys_dflt_btn= atoi(argv[++i]);
		}
	    }
	}
	else if ( strcmp(&argv[i][1],"accessx")==0 ) {
	    which|= XkbControlsEnabledMask;
	    changeEnabled|= XkbAccessXKeysMask;
	    enabled|= (onoff?XkbAccessXKeysMask:0);
	    if (onoff) {
		if ( ((i+1)<argc) && (isdigit(argv[i+1][0])) ) {
		    which|= XkbAccessXKeysMask;
		    newControls.accessx_timeout= atoi(argv[++i]);
		}
	    }
	}
	else if ( strcmp(argv[i],"-help") ) {
	    return 0;
	}
	else {
	    fprintf(stderr,"Unknown option %s\n",argv[i]);
	    return 0;
	}
    }
    return 1;
}

int
main(argc,argv)
    int argc;
    char *argv[];
{
Display	*dpy;
int	i1,i2,i3,i4,i5;
XkbDescRec	*desc;
XkbControlsRec	*ctrls;
unsigned	 query;

  
    if (!parseArgs(argc,argv)) {
	usage(argc,argv);
	return 1;
    }
    dpy = XOpenDisplay(dpyName);
    if ( !dpy )
	return 1;
    if (synch)
	XSynchronize(dpy,1);
    if ( !XkbQueryExtension(dpy,&i1,&i2,&i3,&i4,&i5)>0 ) {
	fprintf(stderr,"query failed\n");
	goto BAIL;
    }
    if ( !XkbUseExtension(dpy) ) {
	fprintf(stderr,"use extension failed (%d,%d)\n",i4,i5);
	goto BAIL;
    }
    desc = XkbGetMap(dpy,0,device);
    if (desc) {
	if (!XkbGetControls(dpy,XkbAllControlsMask,desc)) {
	    fprintf(stderr,"XkbGetControls failed\n");
	    goto BAIL;
	}
	if (which) {
	    if (which&XkbRepeatKeysMask) {
		fprintf(stderr,"Changing RepeatKeys delay%s",
			newControls.repeat_interval>0?" and interval\n":"\n");
		desc->ctrls->repeat_delay= newControls.repeat_delay;
		if (newControls.repeat_interval>0)
		    desc->ctrls->repeat_interval= newControls.repeat_interval;
	    }
	    if (which&XkbSlowKeysMask) {
		fprintf(stderr,"Changing SlowKeys delay\n");
		desc->ctrls->slow_keys_delay= newControls.slow_keys_delay;
	    }
	    if (which&XkbBounceKeysMask) {
		fprintf(stderr,"Changing BounceKeys delay\n");
		desc->ctrls->debounce_delay= newControls.debounce_delay;
	    }
	    if (which&XkbMouseKeysMask) {
		fprintf(stderr,"Changing MouseKeys delay");
		desc->ctrls->mouse_keys_delay= newControls.mouse_keys_delay;
		if (newControls.mouse_keys_interval>0) {
		    fprintf(stderr,", interval");
		    desc->ctrls->mouse_keys_interval=
					newControls.mouse_keys_interval;
		}
		if (newControls.mouse_keys_time_to_max) {
		    fprintf(stderr,", time-to-max");
		    desc->ctrls->mouse_keys_time_to_max=
					newControls.mouse_keys_time_to_max;
		}
		if (newControls.mouse_keys_time_to_max) {
		    fprintf(stderr,", curve");
		    desc->ctrls->mouse_keys_curve= newControls.mouse_keys_curve;
		}
		if (newControls.mouse_keys_dflt_btn) {
		    fprintf(stderr,", default button");
		    desc->ctrls->mouse_keys_dflt_btn=
					newControls.mouse_keys_dflt_btn;
		}
		fprintf(stderr,"\n");
	    }
	    if (which&XkbAccessXKeysMask) {
		fprintf(stderr,"Changing AccessX timeout\n");
		desc->ctrls->accessx_timeout= 
					newControls.accessx_timeout;
	    }
	    if (which&XkbControlsEnabledMask) {
		fprintf(stderr,"Changing enabled ctrls\n");
		desc->ctrls->enabled_ctrls&= ~changeEnabled;
		desc->ctrls->enabled_ctrls|= (changeEnabled&enabled);
	    }
	    if (which&XkbInternalModsMask) {
		fprintf(stderr,"Changing internal modifiers\n");
		desc->ctrls->internal_real_mods&= ~changeInternal;
		desc->ctrls->internal_real_mods|= (changeInternal|internal);
		desc->ctrls->internal_vmods&= ~changeVInternal;
		desc->ctrls->internal_vmods|= 
					(changeVInternal&vInternal);
	    }
	    if (which&XkbIgnoreLockModsMask) {
		fprintf(stderr,"Changing ignore locks modifiers\n");
		desc->ctrls->ignore_lock_real_mods&= ~changeIgnoreLocks;
		desc->ctrls->ignore_lock_real_mods|=
					(changeIgnoreLocks|ignoreLocks);
		desc->ctrls->ignore_lock_vmods&= ~changeVIgnoreLocks;
		desc->ctrls->ignore_lock_vmods|=
					(changeVIgnoreLocks&vIgnoreLocks);
	    }
	    XkbSetControls(dpy,which,desc);
	}
    }
    else {
	fprintf(stderr,"Get keyboard description request failed\n");
	return 1;
    }
    ctrls= desc->ctrls;
    printf("Device ID:         %d\n",desc->device_spec);
    printf("Groups:            %swrap\n",
		(ctrls->enabled_ctrls&XkbGroupsWrapMask)?"":"don't ");
    printf("audible bell:      %s\n",
		(ctrls->enabled_ctrls&XkbAudibleBellMask)?"on ":"off");
    printf("auto autorepeat:   %s\n",
		(ctrls->enabled_ctrls&XkbAutoAutorepeatMask)?"on ":"off");
    printf("internal mask:     0x%x\n",ctrls->internal_mask);
    printf("ignore lock mask:  0x%x\n",ctrls->ignore_lock_mask);
    printf("internal mods:     0x%x\n",ctrls->internal_real_mods);
    printf("ignore lock mods:  0x%x\n",ctrls->ignore_lock_real_mods);
    printf("internal vmods:    0x%x\n",ctrls->internal_vmods);
    printf("ignore lock vmods: 0x%x\n",ctrls->ignore_lock_vmods);
    printf("repeat keys:       %s (%d/%d)\n",
		(ctrls->enabled_ctrls&XkbRepeatKeysMask?"on ":"off"),
		ctrls->repeat_delay,ctrls->repeat_interval);
    printf("slow keys:         %s (%d)\n",
		(ctrls->enabled_ctrls&XkbSlowKeysMask?"on ":"off"),
		ctrls->slow_keys_delay);
    printf("bounce keys:       %s (%d)\n",
		(ctrls->enabled_ctrls&XkbBounceKeysMask?"on ":"off"),
		ctrls->debounce_delay);
    printf("sticky keys:       %s\n",
		(ctrls->enabled_ctrls&XkbStickyKeysMask?"on ":"off"));
    printf("mouse keys:        %s (btn=%d,accel=%d/%d/%d/%d)\n",
		(ctrls->enabled_ctrls&XkbMouseKeysMask?"on ":"off"),
		ctrls->mouse_keys_dflt_btn,
		ctrls->mouse_keys_delay,
		ctrls->mouse_keys_interval,
		ctrls->mouse_keys_time_to_max,
		ctrls->mouse_keys_curve);
    printf("access X keys:     %s (timeout=%d, mask= 0x%x)\n",
		(ctrls->enabled_ctrls&XkbAccessXKeysMask?"on ":"off"),
		ctrls->accessx_timeout,ctrls->accessx_timeout_mask);
    XCloseDisplay(dpy);
    return 0;
BAIL:
    XCloseDisplay(dpy);
    return 0;
}