summaryrefslogtreecommitdiff
path: root/xts5/src/lib/devcntl.c
blob: 24d7815d76b8e70f7e96f2f368cacb4b86137e92 (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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*
Copyright (c) 2005 X.Org Foundation L.L.C.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
*
* Copyright Applied Testing and Technology Inc. 1995
* All rights reserved
*
* Project: VSW5
*
* File:	xts5/src/lib/devcntl.c
*
* Description:
*	Inout device extension test support routines
*
* Modifications:
* $Log: devcntl.c,v $
* Revision 1.2  2005-11-03 08:42:01  jmichael
* clean up all vsw5 paths to use xts5 instead.
*
* Revision 1.1.1.2  2005/04/15 14:05:09  anderson
* Reimport of the base with the legal name in the copyright fixed.
*
* Revision 8.0  1998/12/23 23:24:31  mar
* Branch point for Release 5.0.2
*
* Revision 7.0  1998/10/30 22:42:42  mar
* Branch point for Release 5.0.2b1
*
* Revision 6.0  1998/03/02 05:16:56  tbr
* Branch point for Release 5.0.1
*
* Revision 5.0  1998/01/26 03:13:29  tbr
* Branch point for Release 5.0.1b1
*
* Revision 4.1  1996/01/25 01:57:14  andy
* Portability improvements from DEPLOY tools
*
* Revision 4.0  1995/12/15  08:42:09  tbr
* Branch point for Release 5.0.0
*
* Revision 3.2  1995/12/15  00:39:20  andy
* Prepare for GA Release
*
*/

/*
Portions of this software are based on Xlib and X Protocol Test Suite.
We have used this material under the terms of its copyright, which grants
free use, subject to the conditions below.  Note however that those
portions of this software that are based on the original Test Suite have
been significantly revised and that all such revisions are copyright (c)
1995 Applied Testing and Technology, Inc.  Insomuch as the proprietary
revisions cannot be separated from the freely copyable material, the net
result is that use of this software is governed by the ApTest copyright.

Copyright (c) 1990, 1991  X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.

Copyright 1990, 1991 by UniSoft Group Limited.

Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee,
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 UniSoft not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  UniSoft
makes no representations about the suitability of this software for any
purpose.  It is provided "as is" without express or implied warranty.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include	"xtest.h"
#include	<X11/Xlib.h>
#include	<X11/Xutil.h>
#ifdef INPUTEXTENSION
#include	<X11/extensions/XInput.h>
#endif
#include	"xtestlib.h"

#define	MAX_DEVICES	9
#define	MAXBUT	256
#define	MAXKEY	256

static	struct	butsave {
	unsigned int 	button;
	Display	*disp;
} buttons[MAXBUT];
static	int 	butind;

static	struct	keysave {
	int 	key;
	Display	*disp;
} keys[MAXKEY];
static	int 	keyind;

static struct dkeysave {
	int	key;
	Display	*disp;
} dkeys[MAX_DEVICES][MAXKEY];
static 	int	dkeyind[MAX_DEVICES];

/*
 * Simulate a button press, saving the press so it can be automatically
 * released later.
 */
void
buttonpress(disp, button)
Display *disp;
unsigned int     button;
{
	if (!SimulateButtonPressEvent(disp, button)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Button press %d", button);

	buttons[butind].disp = disp;
	buttons[butind++].button = button;
}

#ifdef INPUTEXTENSION
void
devicebuttonpress(disp, dev, button)
Display *disp;
XDevice *dev;
unsigned int     button;
{
	if (!SimulateDeviceButtonPressEvent(disp, dev, button)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Button press %d", button);

	buttons[butind].disp = disp;
	buttons[butind++].button = button;
}
#endif

/*
 * Simulate a button release, the corresponding saved event is removed.
 */
void
buttonrel(disp, button)
Display *disp;
unsigned int     button;
{
int 	i;

	if (!SimulateButtonReleaseEvent(disp, button)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Button release %d", button);

	/*
	 * Remove the corresponding button down.
	 */
	for (i = butind-1; i >= 0; i--) {
		if (buttons[i].button == button) {
			buttons[i].disp = 0;
			break;
		}
	}
}

#ifdef INPUTEXTENSION
void
devicebuttonrel(disp, dev, button)
Display *disp;
XDevice *dev;
unsigned int     button;
{
int 	i;

	if (!SimulateDeviceButtonReleaseEvent(disp, dev, button)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Button release %d", button);

	/*
	 * Remove the corresponding button down.
	 */
	for (i = butind-1; i >= 0; i--) {
		if (buttons[i].button == button) {
			buttons[i].disp = 0;
			break;
		}
	}
}
#endif

/*
 * Simulate a key press, saving the event so it can be automatically
 * released later.
 */
void
keypress(disp, key)
Display *disp;
int     key;
{
	if (key == NoSymbol)
		return;

	if (!SimulateKeyPressEvent(disp, key)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Key press %d", key);

	keys[keyind].disp = disp;
	keys[keyind++].key = key;
}

#ifdef INPUTEXTENSION
void
devicekeypress(disp, dev, key)
Display *disp;
XDevice *dev;
int     key;
{
int id = dev->device_id;

	if (key == NoSymbol)
		return;

	if (!SimulateDeviceKeyPressEvent(disp, dev, key)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Key press %d", key);

/*
	keys[keyind].disp = disp;
	keys[keyind++].key = key;
*/
	dkeys[id][dkeyind[id]].disp = disp;
	dkeys[id][dkeyind[id]++].key = key;
}
#endif

/*
 * Simulate a key release, the corresponding saved event is removed.
 */
void
keyrel(disp, key)
Display *disp;
int     key;
{
int 	i;

	if (key == NoSymbol)
		return;
	if (!SimulateKeyReleaseEvent(disp, key)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Key release %d", key);

	/*
	 * Remove the corresponding key down.
	 */
	for (i = keyind-1; i >= 0; i--) {
		if (keys[i].key == key) {
			keys[i].disp = 0;
			break;
		}
	}
}

#ifdef INPUTEXTENSION
void
devicekeyrel(disp, dev, key)
Display *disp;
XDevice *dev;
int     key;
{
int 	i, id=dev->device_id;

	if (key == NoSymbol)
		return;
	if (!SimulateDeviceKeyReleaseEvent(disp, dev, key)) {
		delete("XTEST extension not configured or in use");
		return;
	}
	XSync(disp, False);
	debug(1, "Key release %d", key);

	/*
	 * Remove the corresponding key down.
	 */
/*
	for (i = keyind-1; i >= 0; i--) {
		if (keys[i].key == key) {
			keys[i].disp = 0;
*/
	for (i = dkeyind[id]-1; i >= 0; i--) {
		if (dkeys[id][i].key == key) {
			dkeys[id][i].disp = 0;
			break;
		}
	}
}
#endif

/*
 * Release buttons pressed with buttonpress, in reverse order of pressing.
 */
void
relbuttons()
{
int 	i;

	for (i = butind-1; i >= 0; i--) {
		if (buttons[i].disp)
			buttonrel(buttons[i].disp, buttons[i].button);
	}
	butind = 0;
}

#ifdef INPUTEXTENSION
void
devicerelbuttons(dev)
XDevice *dev;
{
int 	i;

	for (i = butind-1; i >= 0; i--) {
		if (buttons[i].disp)
			devicebuttonrel(buttons[i].disp, dev, buttons[i].button);
	}
	butind = 0;
}
#endif

/*
 * Release keys pressed with keypress, in reverse order of pressing.
 */
void
relkeys()
{
int 	i;

	for (i = keyind-1; i >= 0; i--) {
		if (keys[i].disp)
			keyrel(keys[i].disp, keys[i].key);
	}
	keyind = 0;
}
#ifdef INPUTEXTENSION
void devicerelkeys(dev)
XDevice *dev;
{
int 	i, id=dev->device_id;

/*
	for (i = keyind-1; i >= 0; i--) {
		if (keys[i].disp)
			devicekeyrel(keys[i].disp, dev, keys[i].key);
*/
	for (i = dkeyind[id]-1; i >= 0; i--) {
		if (dkeys[id][i].disp)
			devicekeyrel(dkeys[id][i].disp, dev, dkeys[id][i].key);
	}
	dkeyind[id] = 0;
}
#endif

/*
 * Release all buttons and keys, buttons first.  It doesn't matter
 * if none are currently pressed (applies to all the release functions.)
 */
void
relalldev()
{
	relbuttons();
	relkeys();
}

#define	NMODS	8	/* Number of modifiers */

static	XModifierKeymap	*origmap;
static	XModifierKeymap	*curmap;
static	XModifierKeymap	*devcurmap;

/*
 * Given the number of modifiers that you want it returns a modifier
 * key mask with that number of bits set if possible.  Each bit is
 * a valid modifier for the server.  If necessary the modifer map is
 * set up with extra keycodes.
 * If the server is really limited in how many keycodes it allows
 * to be used as modifiers then you may not get as many modifiers as
 * you want.  You should insure that the test will still work in this
 * case.
 */
unsigned int
_wantmods(disp, dev, want)
Display	*disp;
#ifdef INPUTEXTENSION
XDevice *dev;
#else
void *dev;
#endif
int 	want;
{
unsigned int 	mask;
int 	nmods;
int 	i;

	if (curmap == NULL)
	    if (dev == NULL)
		curmap = XGetModifierMapping(disp);
#ifdef INPUTEXTENSION
	    else
		curmap = XGetDeviceModifierMapping(disp, dev);
#endif
	if (curmap == NULL)
		return(0);

	mask = 0;
	nmods = 0;

	/*
	 * Find out what is currently available.
	 */
	for (i = 0; i < NMODS*curmap->max_keypermod; i++) {
		if (curmap->modifiermap[i])
			mask |= 1 << (i/curmap->max_keypermod);
	}

	/*
	 * If we want more modifiers than are currently in the map then
	 * try to add some.  There is no guarantee that this can be done though.
	 */
	nmods = bitcount(mask);
	if (nmods < want) {
		/*
		 * Exercise for the reader...
		 */
		untested("Unimplemented modmap expansion in wantmods");
	}

	/* I want, but I can't get */
	if (nmods < want)
		want = nmods;

	/*
	 * Put the wanted number of modifiers into the return mask
	 */
	for (i = 0; i < NMODS; i++) {
		if (mask & (1 << i))
			want--;
		if (want < 0)
			mask &= ~(1 << i);
	}

	return(mask);
}

#ifdef INPUTEXTENSION
unsigned int
wantdevmods(disp, dev, want)
Display	*disp;
XDevice *dev;
int 	want;
{
	return (_wantmods(disp, dev, want));
}
#endif

unsigned int
wantmods(disp, want)
Display	*disp;
int 	want;
{
	return (_wantmods(disp, NULL, want));
}

static void modthing(/* disp, mask */);
static void devmodthing(/* disp, dev, mask */);

/*
 * Simulate pressing a bunch of modifier keys.  The mask passed to this
 * function should be made of bits obtained by a previous call to wantmods,
 * to ensure that they have assigned keycodes.
 */
void modpress(disp, mask)
Display	*disp;
unsigned int 	mask;
{
	modthing(disp, mask, True);
}

#ifdef INPUTEXTENSION
void devmodpress(disp, dev, mask)
Display	*disp;
XDevice *dev;
unsigned int 	mask;
{
	devmodthing(disp, dev, mask, True);
}
#endif

/*
 * Simulate releasing a bunch of modifier keys.  The mask passed to this
 * function should be made of bits obtained by a previous call to wantmods,
 * to ensure that they have assigned keycodes.
 */
void modrel(disp, mask)
Display	*disp;
unsigned int 	mask;
{
	modthing(disp, mask, False);
}

#ifdef INPUTEXTENSION
void devmodrel(disp, dev, mask)
Display	*disp;
XDevice *dev;
unsigned int 	mask;
{
	devmodthing(disp, dev, mask, False);
}
#endif

static void
modthing(disp, mask, pressing)
Display	*disp;
unsigned int 	mask;
int pressing;
{
int 	mod;
int 	ent;
void	(*func)();

	if (curmap == NULL) {
		delete("Programming error: wantmods() not called");
		return;
	}

	if (pressing)
		func = keypress;
	else
		func = keyrel;

	for (mod = 0; mod < NMODS; mod++) {
		if (mask & (1 << mod))
			{
			for (ent=mod*curmap->max_keypermod; 
			     ent<(mod+1) *curmap->max_keypermod; ent++)
			    if (curmap->modifiermap[ent]) {
				(*func)(disp, curmap->modifiermap[ent]);
				break;
			    }
			/*
			(*func)(disp, curmap->modifiermap[mod*curmap->max_keypermod]);
			*/
			}
	}
}

#ifdef INPUTEXTENSION
static void
devmodthing(disp, dev, mask, pressing)
Display	*disp;
unsigned int 	mask;
int pressing;
{
int 	mod;
void	(*func)();

	if (curmap == NULL) {
		delete("Programming error: wantmods() not called");
		return;
	}

	if (dev)
	    if (pressing)
		func = devicekeypress;
	    else
		func = devicekeyrel;

	for (mod = 0; mod < NMODS; mod++) {
		if (mask & (1 << mod))
			(*func)(disp, dev, devcurmap->modifiermap[mod*devcurmap->max_keypermod]);
	}
}
#endif

/*
 * Check if a keycode corresponds to any of mods in a mask, returned by
 * wantmods.
 */
int ismodkey(mask, kc)
unsigned int 	mask;
int kc;
{
int 	mod;

	if (curmap == NULL) {
		delete("Programming error: wantmods() not called");
		return False;
	}
	if (!mask || kc < 8 || kc > 255)
		return False;

	for (mod = 0; mod < NMODS; mod++) {
		if (mask & (1 << mod))
			if (curmap->modifiermap[mod*curmap->max_keypermod] == kc)
				return True;
	}
	return False;
}

/*
 * This routine should be called at the end of a test after any of the
 * device press routines have been called.
 */
void restoredevstate()
{
extern	Display	*Dsp;

	relalldev();
	if (origmap)
		XSetModifierMapping(Dsp, origmap);
}

/*
 * Returns True if we don't want to do extended testing for any reason.
 */
int noext(needbutton)
int 	needbutton;
{

	if (config.extensions == False) {
		untested("Extended testing not required");
		return True;
	} else if (IsExtTestAvailable() == False) {
		untested("Server does not support XTEST extension");
		untested("or test suite not configured to use XTEST extension");
		return True;
	} else if (needbutton && nbuttons() == 0) {
		untested("No buttons exist on the server");
		return True;
	}
	return False;
}


/*
 * Returns the number of physical buttons.
 */
int nbuttons()
{
static int 	Nbuttons = -1;
unsigned	char	pmap[5];
extern	Display	*Dsp;

	if (Nbuttons == -1)
		Nbuttons = XGetPointerMapping(Dsp, pmap, sizeof(pmap));
	return Nbuttons;
}

/*
 * Returns a valid keycode for the server.  A different one is returned
 * every time (until it wraps round).
 */
int getkeycode(display)
Display	*display;
{
static	int 	minkc, maxkc;
static	int 	curkey;

	XDisplayKeycodes(display, &minkc, &maxkc);
	if (minkc < 8)
		minkc = 8;
	if (!curkey)
		curkey = minkc;

	if (curkey > maxkc)
		curkey = minkc;

	return curkey++;
}

/*
 * Returns a valid keycode for the device.  A different one is returned
 * every time (until it wraps round).
 */
#ifdef INPUTEXTENSION
int getdevkeycode(display,dev)
Display	*display;
XDevice *dev;
{
static	int 	minkc, maxkc;
static	int 	devcurkey[MAX_DEVICES];
XDeviceInfoPtr list;
XAnyClassPtr any;
int i, j, id=dev->device_id, ndevices;

        list = XListInputDevices (display, &ndevices);
	for(i=0; i<ndevices; i++)
	    if ((list+i)->id == id)
		break;

	any = (XAnyClassPtr) ((list+i)->inputclassinfo);
	for (j=0; j<list->num_classes; j++)
	    {
	    XKeyInfo *K = (XKeyInfo *) any;
	    if (any->class == KeyClass)
		{
		minkc = K->min_keycode;
		maxkc = K->max_keycode;
		break;
		}
	    any = (XAnyClassPtr) ((char *) any + any->length);
	    }
 	XFreeDeviceList(list);

	if (minkc < 8)
		minkc = 8;
	if (!devcurkey[id])
		devcurkey[id] = minkc;

	if (devcurkey[id] > maxkc)
		devcurkey[id] = minkc;

	return devcurkey[id]++;
}
#endif