summaryrefslogtreecommitdiff
path: root/xc/test/xsuite/xtest/tset/CH07/grbky/grbky.m
blob: 583d48b31047d76596ff2643a54af7312051ea57 (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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
/*
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 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: grbky.m,v 1.10 93/07/18 13:22:05 rws Exp $
 */
>>TITLE XGrabKey CH07
void

Display	*display = Dsp;
int 	keycode = grab_key_code(display); /* always min. k.c. */
unsigned int 	modifiers = 0;
Window	grab_window = defwin(display);
Bool	owner_events = False;
int 	pointer_mode = GrabModeAsync;
int 	keyboard_mode = GrabModeAsync;
>>SET startup focusstartup
>>SET cleanup focuscleanup
>>EXTERN

/*
 * as unwarppointer but don't free ptr as this done at tpcleanup time.
 */
static void
my_unwarppointer(display, ptr)
Display *display;
PointerPlace *ptr;
{
        XWarpPointer(display, None, ptr->oroot, 0, 0, 0, 0, ptr->ox, ptr->oy);
}


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);
}

static char	*WindowTree[]= {
	".",
		"child1 . (10,10) 30x30",
		"child2 . (50,50) 30x30",
			"grandchild child2 (2,2) 20x20",
};

static int NWindowTree = NELEM(WindowTree);

>>ASSERTION Good B 3
A call to xname establishes a passive grab on the keyboard that is activated
in the future by
the specified key being logically pressed,
the specified modifier keys being logically down,
no other modifier keys being logically down,
the
.A grab_window
being the focus window or an ancestor of the focus window
or being a descendant of the focus window that contains the pointer
and
a passive grab on the same key combination not existing on any
ancestor of
.A grab_window .
>>STRATEGY
Call xname as touch test.
If extensions available:
  Set focus to grab window.
  Simulate a key press of key.
  Verify that keyboard is now grabbed.
  Release grab & key.

  Set up a grab with xname for a key.
  Set focus to grab window.
  Simulate press of another key.
  Simulate the key press.
  Verify that the keyboard is not grabbed.
  Release grab & key.

  Set up a grab with xname for a key and modifier keys.
  Set focus to grab window.
  Simulate modifier key presses.
  Simulate the key press.
  Verify that the keyboard is grabbed.
  Release grab & key.

  Set up a grab with xname for a key and modifier keys.
  Set focus to grab window.
  Simulate modifier key presses.
  Simulate extra modifier key presses.
  Simulate the key press.
  Verify that the keyboard is not grabbed.
  Release grab & key.

  Make a child of current grab_window become focus window.
  Call xname for a passive grab with no modifiers on parent.
  Set focus window to child of grab window.
  Activate the grab.
  Check if grabbed.
  Release key & grab.

  Have grab_window a child of focus window and containing the pointer.
  Call xname with no modifiers.
  Activate the grab.
  Check if grabbed.
  Release key & grab.

  Have grab_window a child of focus window and not containing the pointer.
  Call xname with no modifiers.
  Activate the grab.
  Check not grabbed.
  Release key & grab.

  Set grab with no modifiers and grab_window a top-level window.
  Set another grab on child of top-level window.
  Discard event queue.
  Attempt to activate second grab (in child).
  Check event reported w.r.t. parent.
  Check that no event reported for child.
  Check no further events outstanding.
  Check that a grab is active.
  Release parent grab and check grab not active on child.
  Activate child and check key grabbed.
  Release any grabs and keys outstanding.

>>CODE
unsigned int    mask;
int     onemod;
int	keysave,otherkc;
Window	parent,child1,child2,gchild;
struct buildtree *tree;
PointerPlace *p;
XEvent	ev;
int	cantdoit;

	if (!(cantdoit=noext(0)) && kgrabbed()) { /* Sanity check */
		delete("Keyboard seemed to be grabbed before doing test");
		return;
	}

	XCALL;

	if (cantdoit) {
		untested("There is no reliable test method, but a touch test was performed");
		XUngrabKey(display, AnyKey, AnyModifier, grab_window);
		return;
	}

	/*
	 * --- Simple case no modifiers.
	 */
	activate_press();

	if (kgrabbed()) {
		CHECK;
	} else {
		report("Keyboard was not grabbed after key press");
		FAIL;
	}
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	/*
	 * --- Press another key instead of the grabbed key.
	 * The keyboard should not be grabbed.
	 */
	if ((otherkc=nongrab_key_code(display)) != keycode) {
		XCALL;

		keysave = keycode;
		keycode = otherkc;
		activate_press();
		keycode = keysave; /* restore to normality */
		if (kgrabbed()) {
			report("Keyboard was grabbed although another key was pressed");
			FAIL;
		} else
			CHECK;
		relalldev();
	} else {
		trace("Only one key supported");
		CHECK;
	}
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	/*
	 * --- Set up a grab with modifiers.
	 */
	modifiers = wantmods(display, 2);
	grab_window = defwin(display);
	trace("Grabbing keycode %d with mods %s", keycode,
		keymaskname((unsigned long)modifiers));
	XCALL;

	activate_press();
	if (kgrabbed()) {
		CHECK;
	} else {
		report("Keyboard was not grabbed for keycode %d and %s", keycode,
			keymaskname((unsigned long)modifiers));
		FAIL;
	}
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	/*
	 * --- Set up a grab with modifiers, try to activate the grab with
	 * too many modifiers held down.  Grab should not become active.
	 */
	mask = wantmods(display, 2);
	for (onemod = 1; onemod; onemod <<= 1) {
		if (mask & onemod)
			break;
	}
	/* Only assumes one modifier */
	modifiers = mask & ~onemod;
	grab_window = defwin(display);
	XCALL;

	XSetInputFocus(display, grab_window, RevertToPointerRoot, CurrentTime);
	/* Pressing an extra modifier */
	modpress(display, mask);
	keypress(display, keycode);

	if (mask && kgrabbed()) {
		report("Keyboard was grabbed when there were extra modifier keys down");
		FAIL;
	} else {
		/*
		 * If mask was zero this means that there are no modifiers KeyCodes
		 * available.  This is unlikely and realy means that we cannot test
		 * this part of the assertion.  However in this case this part of
		 * the assertion has no meaning so say it passes.
		 */
		CHECK;
	}
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

/* Make a child of current grab_window become focus window. */
	parent = grab_window;
	tree = buildtree(display, parent, WindowTree, NWindowTree);
	child1 = btntow(tree, "child1");
/* Call xname for a passive grab with no modifiers on parent. */
	modifiers = 0;
	XCALL;
/* Set focus window to child of grab window. */
	XSetInputFocus(display, parent, RevertToPointerRoot, CurrentTime);
/* Activate the grab. */
	keypress(display, keycode);
/* Check if grabbed. */
	if (kgrabbed()) {
		CHECK;
	} else {
		report("Key not grabbed when focus is child of grab_window.");
		FAIL;
	}
/* Release key & grab. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);


/* Have grab_window a child of focus window and containing the pointer. */
	child2 = btntow(tree, "child2");
	gchild = btntow(tree, "grandchild");
	grab_window = gchild;
	XSetInputFocus(display, child2, RevertToPointerRoot, CurrentTime);
	p = warppointer(display, grab_window, 2, 2);
/* Call xname with no modifiers. */
	modifiers = 0;
	XCALL;
/* Activate the grab. */
	keypress(display, keycode);
/* Check if grabbed. */
	if (kgrabbed()) {
		CHECK;
	} else {
		report("Key not grabbed with grab_window a descendent of focus");
		report(" and containing the pointer.");
		FAIL;
	}
	my_unwarppointer(display, p);
/* Release key & grab. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

/* Have grab_window a child of focus window and not containing the pointer. */
	grab_window = gchild;
	XSetInputFocus(display, child2, RevertToPointerRoot, CurrentTime);
	p = warppointer(display, child1, 2, 2);
/* Call xname with no modifiers. */
	modifiers = 0;
	XCALL;
/* Activate the grab. */
	keypress(display, keycode);
/* Check not grabbed. */
	if (!kgrabbed()) {
		CHECK;
	} else {
		report("Key grabbed with grab_window a descendent of focus");
		report(" and not containing the pointer.");
		FAIL;
	}
	my_unwarppointer(display, p);
/* Release key & grab. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

/* Set grab with no modifiers and grab_window a top-level window. */
	modifiers = 0;
	grab_window = parent;
	XCALL;
/* Set another grab on child of top-level window. */
	grab_window = child1;
	XCALL;
/* Discard event queue. */
	XSync(display, True);
/* Attempt to activate second grab (in child). */
	grab_window = child1;
	activate_press();
/* Check event reported w.r.t. parent. */
	if (XCheckWindowEvent(display, parent, KeyPressMask, &ev))
		CHECK;
	else {
		report("No KeyPress event for parent window.");
		FAIL;
	}
/* Check that no event reported for child. */
	if (!XCheckWindowEvent(display, child1, KeyPressMask, &ev))
		CHECK;
	else {
		report("Unexpected KeyPress event for child window.");
		FAIL;
	}
/* Check no further events outstanding. */
	if (getevent(display, &ev) != 0) {
		report("Unexpected %s event on %s window.",
			eventname(ev.type),
			ev.xany.window == parent ? "parent" : (
				ev.xany.window == child1 ? "child" : "unexpected"
			));
		FAIL;
	} else
		CHECK;
/* Check that a grab is active. */
	if (kgrabbed())
		CHECK;
	else {
		report("Key not grabbed after set in parent and child and");
		report(" activation attempt in child.");
		FAIL;
	}
/* Release parent grab and check grab not active on child. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, parent);
	/* grab_window is child1, still, so kgrabbed() checking OK. */
	if (kgrabbed()) {
		report("Child grab became active after interfering parent released.");
		FAIL;
	} else
		CHECK;
/* Activate child and check key grabbed. */
	/* grab_window still child so activate_press etc. OK */
	activate_press();
	if (!kgrabbed()) {
		report("Child grab not active after interfering parent released.");
		FAIL;
	} else
		CHECK;
/* Release any grabs and keys outstanding. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);
	
	CHECKPASS(13);
>>ASSERTION Good B 3
When the conditions for activating the grab are otherwise satisfied
and the keyboard is already grabbed,
then no active grab is established.
>>STRATEGY
If extensions are available:
  Create two windows that do not overlap.
  Check that at least two keycodes are available.
  Set and activate grab on first window.
  Check grab activated.
  Set grab on second window on another keycode.
  Attempt to activate grab on second window, using other keycode.
  Check that only one KeyPress event received.
  Check it was reported from first grab window.
  Check grab still outstanding.
  Release keys & grabs.
else
  Report untested.
>>CODE
Window	w1,w2;
XEvent	ev;
int	n;
int	grabbed;
int	keysave,otherkc;

	if (noext(0))
		return;
	else
		CHECK;
	w1 = defwin(display);
	w2 = defwin(display);
	keysave = keycode;
	otherkc = nongrab_key_code(display);

	if (otherkc == keycode) {
		delete("Need at least two distinct keycodes.");
		return;
	} else
		CHECK;

	grab_window = w1;
	XCALL;
	activate_press();
	if (!kgrabbed()) {
		delete("Could not activate first grab.");
		relalldev();
		XUngrabKey(display, AnyKey, AnyModifier, grab_window);
		return;
	} else
		CHECK;

	grab_window = w2;
	keycode = otherkc;
	XCALL;
	XSync(display, True);
	activate_press();
	keycode = keysave;
	if ((n=getevent(display, &ev)) != 1) {
		report("Received %d events on key press when grab active, expected just %d.", n, 1);
		FAIL;
	} else
		CHECK;
	if (n > 0 && ev.type != KeyPress) {
		report("First event was type %s instead of KeyPress.",eventname(ev.type));
		FAIL;
	} else
		CHECK;
	grabbed = kgrabbed();
	if (n > 0 && ev.xany.window != w1) {
		report("Event appeared on %s window instead of first grab window.",
			ev.xany.window == w2 ? "second grab" : "unexpected");
		if (grabbed)
			report("Grab appears to have moved.");
		else
			report("Grab has been prematurely released.");
		FAIL;
	} else if (!grabbed) {
		report("Key grab erroneously released on trying to activate second.");
		FAIL;
	} else
		CHECK;
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, w1);
	XUngrabKey(display, AnyKey, AnyModifier, w2);

	CHECKPASS(6);
>>ASSERTION Good B 3
When the conditions for activating the grab are satisfied
and the grab subsequently becomes active, then
the last-keyboard-grab time is set to the time at which the key was pressed.
>>STRATEGY
If extensions are available:
  Set and activate grab.
  Check activated.
  Check activating event received.
  Check event type and event window are KeyPress and grab_window.
  Attempt XUngrabKeyboard at time just before event time.
  Check still grabbed.
  Attempt XUngrabKeyboard at time equal to event time.
  Check no longer grabbed.
  Release grab & key.
else
  Report untested.
>>CODE
XEvent	ev;

	if (noext(0))
		return;
	else
		CHECK;
	XSync(display, True);
	XCALL;
	activate_press();
	if (!kgrabbed()) {
		delete("Failed to activate grab.");
		return;
	} else
		CHECK;
	if (!getevent(display, &ev)) {
		delete("No event reported for activating grab.");
		return;
	} else
		CHECK;
	/* sanity check on the event. */
	if (ev.type != KeyPress || ev.xany.window != grab_window) {
		delete("Reported event has type %s and event window 0x%x", eventname(ev.type), ev.xany.window);
		delete(" expected KeyPress and 0x%x.", grab_window);
		return;
	} else
		CHECK;
	/* now set up OK */
	trace("Grabbed at time 0x%lx.",(unsigned long)ev.xkey.time);
	XUngrabKeyboard(display, ev.xkey.time - 1);
	if (!kgrabbed()) {
		report("Last keyboard grab time set earlier than reported event time.");
		FAIL;
	} else
		CHECK;
	XUngrabKeyboard(display, ev.xkey.time);
	if (kgrabbed()) {
		report("Last keyboard grab time set later than reported event time.");
		FAIL;
	} else
		CHECK;
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	CHECKPASS(6);
	
>>ASSERTION Good B 3
When the grab subsequently becomes active and later
the logical state of the
keyboard has the specified key released,
then the active grab is terminated automatically.
>>#(independent of the logical state of the modifier keys).
>>STRATEGY
If extension available:
    Set grab with modifiers.
    Activate grab.
    Check grabbed.
    Simulate keycode only key release with testing extension.
    Check for grab release.
    Simulate modifiers only key release with testing extension.
    Check for grab release.
    Release grab & keys.
  Repeat but in opposite order with grab release expected on keycode up only.
  Do the same for keycode = AnyKey, no modifiers.
    (releasing the grab with XUngrabKey before expected !kgrabbed() tests.)
else
  Report untested.
>>CODE

	if (noext(0))
		return;
	else
		CHECK;
	modifiers = wantmods(display, 2);
	trace("Grabbing keycode %d with mods %s", keycode,
		keymaskname((unsigned long)modifiers));
	XCALL;

	activate_press();
	if (kgrabbed()) {
		CHECK;
	} else {
		delete("Keyboard was not grabbed for keycode %d and %s", keycode,
			keymaskname((unsigned long)modifiers));
		return;
	}
	trace("releasing keycode %d only.", keycode);
	keyrel(display, keycode); /* leaving modifiers down. */
	if (!kgrabbed()) {
		CHECK;
	} else {
		report("Keyboard still grabbed for keycode %d and %s after keycode release.", keycode,
			keymaskname((unsigned long)modifiers));
		FAIL;
	}
	modrel(display, modifiers); /* there go the modifiers. */
	if (!kgrabbed()) {
		CHECK;
	} else {
		report("Keyboard still grabbed for keycode %d and %s even after all keys released.", keycode,
			keymaskname((unsigned long)modifiers));
		FAIL;
	}
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);
	/* repeat but now release modifiers first and keycode last */
	trace("Grabbing keycode %d with mods %s", keycode,
		keymaskname((unsigned long)modifiers));
	XCALL;

	activate_press();
	if (kgrabbed()) {
		CHECK;
	} else {
		delete("Keyboard was not grabbed for keycode %d and %s", keycode,
			keymaskname((unsigned long)modifiers));
		return;
	}
	trace("releasing modifiers %s only.", keymaskname((unsigned long)modifiers));
	modrel(display, modifiers); /* leaving keycode down. */
	if (kgrabbed()) {
		CHECK;
	} else {
		report("Keyboard no longer grabbed for keycode %d and %s after %s release.", keycode,
			keymaskname((unsigned long)modifiers),
			keymaskname((unsigned long)modifiers));
		FAIL;
	}
	keyrel(display, keycode); /* there goes the keycode key. */
	if (!kgrabbed()) {
		CHECK;
	} else {
		report("Keyboard still grabbed for keycode %d and %s even after all keys released.", keycode,
			keymaskname((unsigned long)modifiers));
		FAIL;
	}
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	/*
	 * Finally test AnyKey grabs only release when activating key released.
	 */
	modifiers = 0;
	keycode = AnyKey;
	trace("Grabbing keycode AnyKey with mods %s",
		keymaskname((unsigned long)modifiers));
	XCALL;

	keycode = grab_key_code(display);
	activate_press();
	if (kgrabbed()) {
		CHECK;
	} else {
		delete("Keyboard was not grabbed for keycode AnyKey when keycode %d only pressed.", keycode);
		return;
	}
	trace("releasing non activating keycode only.");
	/* already released by previous kgrabbed test but can't check
	 * if grabbed with kgrabbed as that would just make it active if
	 * (erroneously) passive. So, try and release grab with XUngrabKey
	 * which will do nothing if its active, as it should be, but will
	 * allow subsequent kgrabbed to return False if it wasn't active.
	 */
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);
	if (kgrabbed()) {
		CHECK;
	} else {
		delete("Keyboard was not grabbed for keycode AnyKey after press/release of non-activating key.");
		return;
	}
	trace("releasing activating keycode %d only.", keycode);
	keyrel(display, keycode);
	/*
	 * now the XUngrabKey should find it non-active so kgrabbed => False
	 */
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);
	if (!kgrabbed()) {
		CHECK;
	} else {
		report("Keyboard still grabbed for keycode AnyKey after keycode %d release.", keycode);
		FAIL;
	}
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	CHECKPASS(10);
>>ASSERTION Good B 3
A call to xname overrides all previous passive grabs by the same client on the
same key combinations on the same window.
>>STRATEGY
If extensions available:
  Create a window for event reporting and set event mask to KeyPressMask.
  Set a grab with owner_events False on another window.
  Set a grab with owner_events True on this other window.
  Activate the grab.
  Check it activated.
  Simulate a KeyPress in the reporting window.
  Check that the reported event has event window equal to reporting
    window rather than grab_window showing that second overrode first.
  Release grabs & keys.
else
  Report untested.
>>CODE
Window	reportwin;
XEvent	ev;

	if (noext(0))
		return;
	else
		CHECK;
	reportwin = defwin(display);
	XCALL;
	owner_events = True;
	XCALL;
	activate_press();
	if (!kgrabbed()) {
		delete("Failed to activate grab.");
		return;
	} else
		CHECK;
	XSync(display, True); /* empty event-Q */
	XSelectInput(display, reportwin, KeyPressMask);
	/* now cause key-press in reportwin */
	XSetInputFocus(display, reportwin, RevertToPointerRoot, CurrentTime);
	keypress(display, nongrab_key_code(display));
	/* check it was reported w.r.t. reportwin, not grab_window */
	if (!getevent(display, &ev)) {
		delete("Event not reported to grabbing client.");
		return;
	} else
		CHECK;
	if (ev.type != KeyPress) {
		delete("Reported event of type %s rather than KeyPress.", eventname(ev.type));
		return;
	} else
		CHECK;
	if (ev.xany.window != reportwin) {
		if (ev.xany.window == grab_window) {
			report("First grab not overridden by second.");
			FAIL;
		} else {
			delete("Unexpected window 0x%lx in reported event (not grab_window or reportwin).",
				(unsigned long)ev.xany.window);
			return;
		}
	} else
		CHECK;
	
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	CHECKPASS(5);
>>ASSERTION Good B 3
When the
.A modifiers
argument is
.S AnyModifier ,
then this is equivalent to separate calls to xname for all
possible modifier combinations including no modifiers.
>>STRATEGY
If extensions are available:
  Place passive grab with a modifiers of AnyModifier.
  Press a bunch of modifier keys.
  Press keycode to activate grab.
  Verify that grab is activated.
  Release keys & grab.

  Press keycode (no modifiers).
  Verify that grab is active.
else
  Perform touch test.
  Report untested.
>>CODE
unsigned int 	mods;

	modifiers = AnyModifier;
	XCALL;

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

	modifiers = mods = wantmods(display, 4);

	/*
	 * modifiers was AnyModifier, several modifier keys are held down.
	 */
	activate_press();
	if (kgrabbed())
		CHECK;
	else {
		report("Grab not activated for AnyModifier on keycode %d", keycode);
		report("  Modifiers used %s", keymaskname((unsigned long)mods));
		FAIL;
	}

	/* Release all grabs, keys and modifiers */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	if (kgrabbed()) {
		delete("Could not release grab for second part of test");
		return;
	} else
		CHECK;

	modifiers = AnyModifier;
	XCALL;
	modifiers = 0; /* cause keycode only to be pressed */
	activate_press();
	if (kgrabbed())
		CHECK;
	else {
		report("Grab with AnyModifier was not activated by pressing keycode %d with", keycode);
		report("  no modifiers");
		FAIL;
	}

	/* Release all grabs, keys etc. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	CHECKPASS(4);
>>ASSERTION Good B 3
It is not required that all modifiers specified have
currently assigned KeyCodes.
>>STRATEGY
If extensions are available:
  Get a modifier mask.
  Remove the keycode for the modifier from the map.
  Call xname to set up a passive grab with that modifier.
  Reset the keycode in the modifier map.
  Verify that the grab can be activated with the newly set modifier.
else
  Report untested.
>>CODE
XModifierKeymap	*mmap;
XModifierKeymap	*newmap;
int 	i;

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

	modifiers = wantmods(display, 1);
	if (modifiers == 0) {
		untested("No available modifier keys");
		return;
	} else
		CHECK;

	mmap = XGetModifierMapping(display);
	if (mmap == NULL) {
		delete("Could not get modifier map");
		return;
	} else
		CHECK;

	/*
	 * Remove all the modifiers mappings.
	 */
	newmap = XNewModifiermap(mmap->max_keypermod);
	for (i = 0; i < newmap->max_keypermod*8; i++)
		newmap->modifiermap[i] = NoSymbol;

	if (XSetModifierMapping(display, newmap) == MappingSuccess)
		CHECK;
	else {
		delete("Could not remove modifier mapping");
		return;
	}

	/*
	 * Now we have a modifier that has no keycode - set up a passive grab.
	 */
	XCALL;

	/*
	 * Reset the modifier map, and try to activate the grab.
	 */
	if (XSetModifierMapping(display, mmap) == MappingSuccess)
		CHECK;
	else {
		delete("Could not reset modifier mapping");
		return;
	}

	activate_press();

	if (kgrabbed())
		CHECK;
	else {
		report("Passive grab not set when the modifier did not have a current keycode");
		FAIL;
	}
	/* Release all grabs, keys etc. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	CHECKPASS(6);
>>ASSERTION Good B 3
When the
.A keycode
argument is
.S AnyKey ,
then this is equivalent to separate calls to xname for
all possible KeyCodes.
>>STRATEGY
If extensions are available:
  Set up a passive grab using AnyKey.
    Activate grab with simulated key press.
    Verify that keyboard is grabbed.
    Release key & grab.
    Repeat for other keycodes in range min_keycode to max_keycode.
  Release all grabs & keys.
else
  Touch test using AnyKey.
  Report untested.
>>CODE
int 	i;
int 	minkc, maxkc;
Display	*client2;

	client2 = opendisplay();

	XDisplayKeycodes(display, &minkc, &maxkc);

	keycode = AnyKey;
	XCALL;

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

	XSetInputFocus(display, grab_window, RevertToPointerRoot, CurrentTime);

	for (i = minkc; i <= maxkc; i++) {
		keycode = i; /* let nongrab_key_code know */
		keypress(display, i);
		if (kgrabbed_check(client2))
			CHECK;
		else {
			report("Passive grab of AnyKey, not grabbed for keycode %d", (int)i);
			FAIL;
		}

		/*
		 * Release this grab and try next key.
		 */
		relkeys();
		/*
		 * This XUngrabKey must be in or else the key release is not
		 * enough because the press/release in kgrabbed will
		 * trigger/release the grab again. To test properly we
		 * must try and remove the grab, which only works if not
		 * active.
		 */
		XUngrabKey(display, AnyKey, AnyModifier, grab_window);
		XSync(display, True);
		if (kgrabbed_check(client2)) {
			delete("Could not release grab for next part of test");
			return;
		} else
			CHECK;
		/*
		 * The XUngrabKey requires us to have another passive
		 * grab installed for next iteration.
		 */
		keycode = AnyKey;
		XCALL;
	}
	/* Release all grabs, keys etc. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	CHECKPASS(1+2*(1 + maxkc - minkc));
>>ASSERTION Good B 3
When the event window for an active grab becomes not viewable, then the
grab is released automatically.
>>STRATEGY
If extension is available:
  Set up grab on a child window (to avoid window manager interference).
  Activate grab.
  Check grabbed.
  Unmap grab_window.
  Map grab_window back again (to perform grab check).
  Check no longer grabbed.
  Release key & grab.
else
  Report untested.
>>CODE
Window	win;
struct area	a;

	if (noext(0))
		return;
	else
		CHECK;
	setarea(&a, 2, 2, 0, 0);
	win = crechild(display, grab_window, &a);
	grab_window = win;
	XCALL;
	activate_press();
	if (!kgrabbed()) {
		delete("Failed to activate grab.");
		return;
	} else
		CHECK;
	XUnmapWindow(display, grab_window);
	XSync(display, True);
	XMapWindow(display, grab_window);
	XSync(display, True);
	if (kgrabbed()) {
		report("Unmapping grab_window did not inactivate grab.");
		FAIL;
	} else
		CHECK;
	/* Release all grabs, keys etc. */
	relalldev();
	XUngrabKey(display, AnyKey, AnyModifier, grab_window);

	CHECKPASS(3);
>>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
Call xname with keycode less than min_keycode.
Verify that a BadValue error occurs.
Call xname with keycode greater than max_keycode if it is less than 255.
Verify that a BadValue error occurs.
>>CODE BadValue
int 	minkc, maxkc;

	XDisplayKeycodes(display, &minkc, &maxkc);

	keycode = minkc - 2;
	XCALL;

	if (geterr() == BadValue)
		CHECK;

	/*
	 * Since the protocol only has one byte for the key then this
	 * assertion cannot be tested when max_keycode is 255.
	 */
	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 BadValue owner_events True False
>>ASSERTION Bad A
.ER BadValue pointer_mode GrabModeSync GrabModeAsync
>>ASSERTION Bad A
.ER BadValue keyboard_mode GrabModeSync GrabModeAsync
>>ASSERTION Bad A
.ER Access grab
>>STRATEGY
Grab key/modifier.
Create client2.
Attempt to grab same key modifier for client2.
Verify BadAccess error.
>>CODE BadAccess
Display	*client2;

	XGrabKey(Dsp, keycode, modifiers, grab_window, owner_events, pointer_mode,
		keyboard_mode);
	if (isdeleted()) {
		delete("Could not set up initial grab");
		return;
	}

	if ((client2 = opendisplay()) == NULL)
		return;

	display = client2;
	XCALL;

	if (geterr() == BadAccess)
		CHECK;
	else
		FAIL;

	CHECKPASS(1);
>>ASSERTION Bad A
.ER BadWindow