summaryrefslogtreecommitdiff
path: root/xc/test/xsuite/xtest/tset/CH07/ungrbky/ungrbky.m
blob: a74ec01ffde25e9e9e7ea5b18566af611587ee02 (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
/*
 
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.
 *
 * $XConsortium: ungrbky.m,v 1.11 94/04/17 21:06:56 rws Exp $
 */
>>TITLE XUngrabKey CH07
void

Display	*display = Dsp;
int 	keycode = grab_key_code(display);
unsigned int 	modifiers = 0;
Window	grab_window = defwin(display);
>>SET startup focusstartup
>>SET cleanup focuscleanup
>>EXTERN

static int minkc = -1,maxkc = -1;

static void set_kcs(dpy)
	Display *dpy;
{
	int kmin, kmax;

	if (minkc >= 8)
		return;
	XDisplayKeycodes(dpy, &kmin, &kmax);
	if (kmin < 8)
		kmin = 8;	/* For buggy servers */
	if (kmin >= kmax) {
		delete("Could not find two distinct key codes\n");
		delete("\t(min=%d, max=%d)\n", kmin, kmax);
		return;
	}
	minkc = kmin;
	maxkc = kmax;
}

#define	NMODS	8	/* Number of modifiers */

static int grab_key_code(dpy)
	Display *dpy;
{
	XModifierKeymap	*curmap;
	int i,key;

	set_kcs(dpy);
	curmap = XGetModifierMapping(dpy);
	for (key=minkc;key<=maxkc;key++) {
	    for (i = NMODS*curmap->max_keypermod; --i >= 0; )
		if (curmap->modifiermap[i] == key)
		    break;
	    if (i < 0) /* not a modifier, return it*/
		return key;
	}
	return minkc;
}

static int nongrab_key_code(dpy)
	Display *dpy;
{
	set_kcs(dpy);
	return (keycode == maxkc) ? minkc : maxkc;
}

#define	ACTPOSX	3
#define	ACTPOSY	6
/*
 * Warp to the grab_window and press the keys in modifiers and then press
 * the keycode in the 'keycode' arg.
 * (This activates the previously set up grab if the arg variables
 * have not been changed.)
 */
static void
activate_press()
{
	XSetInputFocus(display, grab_window, RevertToPointerRoot, CurrentTime);
	if (modifiers)
		modpress(display, modifiers);
	keypress(display, keycode);
}

/*
 * As above, but release (all) keys.
 */
static void
activate_release()
{
	XSetInputFocus(display, grab_window, RevertToPointerRoot, CurrentTime);
	keyrel(display, keycode);
	if (modifiers)
		modrel(display, modifiers);
}

/*
 * Returns True if the keyboard is grabbed.  This is not a general purpose
 * routine since it knows about the Grab Key args.
 * When the keyboard is grabbed then keyboard events are not reported to
 * non-grabbing clients. The grab client gets all keyboard events with
 * event window depending on owner_events and clients event mask.
 */
static
kgrabbed_check(client2)
Display	*client2;
{
XEvent	ev;
int	saved_keyc = keycode;

	XSelectInput(client2, grab_window, KeyPressMask|KeyReleaseMask);
	/* Flush events for client2 */
	XSync(client2, True);

	/*
	 * Ensure another-key press release pair in grab_window.
	 */
	keycode = nongrab_key_code(display);
	activate_press();
	activate_release();
	keycode = saved_keyc;

	XSync(client2, False);
	if (getevent(client2, &ev)) {
		/*
		 * An event was reported - keyboard isn't grabbed, do a sanity
		 * check on the type of event.
		 */
		if (ev.type != KeyPress && ev.type != KeyRelease) {
			delete("Unexpected event received in kgrabbed()");
			delete("  event type %s", eventname(ev.type));
		}
		return(False);
	} else {
		return(True);
	}
}

/*
 * as for kgrabbed_check() but used where number of fd's consumed by
 * multiple calls doesn't outweigh convenience of avoiding extra arg.
 */
static
kgrabbed()
{
Display	*client2;

	client2 = opendisplay();
	return kgrabbed_check(client2);
}

>>ASSERTION Good B 3
When the specified key/modifier combination has been grabbed by this
client, then a call to xname releases the grab.
>>STRATEGY
If extension available:
  Setup up grab on specified key/modifier combination using XGrabKey.
  Activate grab.
  Check keyboard grabbed.
  Deactivate grab.
  Check keyboard not grabbed.
  Call xname to release grab.
  Attempt to reactivate Grab.
  Check keyboard still not grabbed.
  Release keys.
else
  Grab key.
  Touch test.
  Report untested.
>>CODE

	XGrabKey(display, keycode, modifiers, grab_window, False,
		GrabModeAsync, GrabModeAsync);

	if (noext(0)) {
		XCALL;
		report("There is no reliable test method, but a touch test was performed");
		return;
	} else
		CHECK;

	activate_press();
	if (!kgrabbed()) {
		delete("Failed to activate grab on keycode %d.", keycode);
		relalldev();
		return;
	} else
		CHECK;

	activate_release();
	if (kgrabbed()) {
		delete("Failed to deactivate grab on keycode %d.", keycode);
		relalldev();
		return;
	} else
		CHECK;

	XCALL;

	activate_press();
	if (kgrabbed()) {
		report("Failed to release initial grab on keycode %d.", keycode);
		FAIL;
	} else
		CHECK;
	relalldev();

	CHECKPASS(4);

>>ASSERTION Good B 3
A
.A modifiers
argument of
.S AnyModifier
releases all grabs by this client for the specified key and all possible
modifier combinations.
>>STRATEGY
If extensions are available:
  Place passive grab with a bunch of modifier keys.
  Activate grab.
  Check keyboard grabbed.
  Deactivate grab.
  Check keyboard not grabbed.
  Call xname with a modifiers of AnyModifier.
  Attempt to reactivate Grab.
  Check keyboard still not grabbed.
  Release keys.

  Place passive grab with just keycode (no modifiers).
  Activate grab.
  Check keyboard grabbed.
  Deactivate grab.
  Check keyboard not grabbed.
  Call xname with a modifiers of AnyModifier.
  Attempt to reactivate Grab.
  Check keyboard still not grabbed.
  Release keys.
else
  Report untested.
>>CODE
unsigned int 	mods;

	if (noext(0)) {
		return;
	} else
		CHECK;

	modifiers = mods = wantmods(display, 4);

	XGrabKey(display, keycode, mods, grab_window, False,
		GrabModeAsync, GrabModeAsync);

	activate_press();
	if (!kgrabbed()) {
		delete("Failed to activate grab on keycode %d.", keycode);
		delete("  Modifiers used %s", keymaskname((unsigned long)mods));
		relalldev();
		return;
	} else
		CHECK;

	activate_release();
	if (kgrabbed()) {
		delete("Failed to deactivate grab on keycode %d.", keycode);
		delete("  Modifiers used %s", keymaskname((unsigned long)mods));
		relalldev();
		return;
	} else
		CHECK;

	modifiers = AnyModifier;
	XCALL;

	modifiers = mods;
	activate_press();
	if (!kgrabbed())
		CHECK;
	else {
		report("Grab not released for AnyModifier on keycode %d", keycode);
		report("  Modifier keys used %s", keymaskname((unsigned long)mods));
		FAIL;
	}
	relalldev();

	if (kgrabbed()) { /* releasing keys may have done the job. */
		delete("Could not release grab for second part of test");
		return;
	} else
		CHECK;

	XGrabKey(display, keycode, 0, grab_window, False,
		GrabModeAsync, GrabModeAsync);

	modifiers = 0;
	activate_press();
	if (!kgrabbed()) {
		delete("Failed to activate grab on keycode %d.", keycode);
		relalldev();
		return;
	} else
		CHECK;

	activate_release();
	if (kgrabbed()) {
		delete("Failed to deactivate grab on keycode %d.", keycode);
		relalldev();
		return;
	} else
		CHECK;

	modifiers = AnyModifier;
	XCALL;

	modifiers = 0;
	activate_press();
	if (!kgrabbed())
		CHECK;
	else {
		report("Grab with no modifier and keycode %d was not released.", keycode);
		FAIL;
	}

	relalldev();

	CHECKPASS(8);
>>ASSERTION Good B 3
A
.A keycode
argument of
.S AnyKey
releases all grabs by this client for the specified modifiers and all keys.
>>STRATEGY
If extensions are available:
  Place passive grab with a bunch of modifier keys and given keycode.
  Activate grab.
  Check keyboard grabbed.
  Deactivate grab.
  Check keyboard not grabbed.
  Call xname with a keycode of AnyKey.
  Attempt to reactivate Grab.
  Check keyboard still not grabbed.
  Release keys.
  Repeat for all keycodes in the range min_keycode to max_keycode
	except for those in the modifier bunch.

else
  Report untested.
>>CODE
unsigned int 	mods;
int	i;
int 	minkc, maxkc;
Display	*client2;
int	keycount = 0;

	client2 = opendisplay();

	XDisplayKeycodes(display, &minkc, &maxkc);

	if (noext(0)) {
		return;
	} else
		CHECK;

	modifiers = mods = wantmods(display, 4);

	for(keycount=0, i = minkc; i <= maxkc; i++) {
		if (ismodkey(mods, i))
			continue;
		keycount++;
		XGrabKey(display, i, mods, grab_window, False,
			GrabModeAsync, GrabModeAsync);

		keycode = i;
		activate_press();
		if (!kgrabbed_check(client2)) {
			delete("Failed to activate grab on keycode %d.", keycode);
			delete("  Modifiers used %s", keymaskname((unsigned long)mods));
			relalldev();
			return;
		} else
			CHECK;

		activate_release();
		if (kgrabbed_check(client2)) {
			delete("Failed to deactivate grab on keycode %d.", keycode);
			delete("  Modifiers used %s", keymaskname((unsigned long)mods));
			relalldev();
			return;
		} else
			CHECK;

		keycode = AnyKey;
		XCALL;

		keycode = i;
		activate_press();
		if (!kgrabbed_check(client2))
			CHECK;
		else {
			report("Grab not released for AnyKey on keycode %d", i);
			report("  Modifier keys used %s", keymaskname((unsigned long)mods));
			FAIL;
		}
		relalldev();

		if (kgrabbed_check(client2)) { /* releasing keys may have done the job. */
			delete("Could not release grab for second part of test");
			return;
		} else
			CHECK;

		relalldev();
	}
	trace("Tested on %d non-modifier keys, with %d modifiers - all from %d keys (%d to %d).",
		keycount, bitcount((unsigned long)mods), 
		1+maxkc-minkc, minkc, maxkc);

	relalldev();

	CHECKPASS(1+4*keycount);
>>ASSERTION Good B 3
A call to xname has no effect on an active grab.
>>STRATEGY
If extension available:
  Set passive grab.
  Activate it.
  Check keyboard is grabbed.
  Call xname to attempt to release grab.
  Check keyboard still grabbed.
  Release keys to release grab.
  If still grabbed:
    Delete with message.
  else
    Call xname to attempt to release grab.
    Attempt to activate grab.
    Check keyboard still not grabbed.
  Release keys.
else
  Report untested.
>>CODE

	if (noext(0)) {
		return;
	} else
		CHECK;
	XGrabKey(display, keycode, modifiers, grab_window, False, GrabModeAsync, GrabModeAsync);

	activate_press();

	if (!kgrabbed()) {
		delete("Failed to set initial grab.");
		return;
	} else
		CHECK;

	XCALL;

	if (!kgrabbed()) {
		report("Released active grab.");
		FAIL;
	} else
		CHECK;

	relalldev(); /* should release grab, allowing a final test */

	if (kgrabbed()) {
		delete("grab still active after key release.");
		return;
	} else
		CHECK;

	XCALL;

	activate_press();
	if (kgrabbed()) {
		report("Couldn't release grab when inactive.");
		FAIL;
	} else
		CHECK;

	CHECKPASS(5);

>>ASSERTION Bad A
When the specified keycode is not in the range
specified by min_keycode and max_keycode in the connection setup or
.S AnyKey ,
then a
.S BadValue
error occurs.
>>STRATEGY
Get min and max keycodes.
Attempt to grab key less than the minimum.
Verify that a BadValue error occurs.
If the maximum is less than 255
  Attempt to grab key greater than the maximum
  Verify a BadValue error occurs.
>>CODE BadValue
int 	minkc, maxkc;

	XDisplayKeycodes(display, &minkc, &maxkc);

	keycode = minkc - 1;
	XCALL;

	if (geterr() == BadValue)
		CHECK;

	if (maxkc < 255) {
		keycode = maxkc + 1;

		XCALL;

		if (geterr() == BadValue)
			CHECK;
	} else
		CHECK;

	CHECKPASS(2);
>>ASSERTION Bad A
.ER BadValue modifiers mask ShiftMask LockMask ControlMask Mod1Mask Mod2Mask Mod3Mask Mod4Mask Mod5Mask AnyModifier
>>ASSERTION Bad A
.ER BadWindow