summaryrefslogtreecommitdiff
path: root/xc/lib/Xext/XTestExt1.c
blob: d1c274a473944d1f62f1985691b159dc01dc994b (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
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
/* $TOG: XTestExt1.c /main/10 1998/02/15 16:19:21 kaleb $ */
/*
 *	File:  xtestext1lib.c
 *
 *	This file contains the Xlib parts of the input synthesis extension
 */

/*


Copyright 1986, 1987, 1988, 1998   The Open Group

All Rights Reserved.

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
OPEN GROUP 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 Open Group 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 Open Group.


Copyright 1986, 1987, 1988 by Hewlett-Packard Corporation

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 Hewlett-Packard not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

Hewlett-Packard makes no representations about the 
suitability of this software for any purpose.  It is provided 
"as is" without express or implied warranty.

This software is not subject to any license of the American
Telephone and Telegraph Company or of the Regents of the
University of California.

*/
/* $XFree86: xc/lib/Xext/XTestExt1.c,v 1.2 1999/07/12 05:57:24 dawes Exp $ */

/******************************************************************************
 * include files
 *****************************************************************************/

#define NEED_REPLIES
#define NEED_EVENTS

#include <stdio.h>
#include <X11/Xproto.h>
#include <X11/Xlibint.h>
#include <X11/extensions/xtestext1.h>

/******************************************************************************
 * variables
 *****************************************************************************/

/*
 * Holds the request type code for this extension.  The request type code
 * for this extension may vary depending on how many extensions are installed
 * already, so the initial value given below will be added to the base request
 * code that is aquired when this extension is installed.
 */
static int		XTestReqCode = 0;
/*
 * Holds the two event type codes for this extension.  The event type codes
 * for this extension may vary depending on how many extensions are installed
 * already, so the initial values given below will be added to the base event
 * code that is aquired when this extension is installed.
 *
 * These two variables must be available to programs that use this extension.
 */
int			XTestInputActionType = 0;
int			XTestFakeAckType   = 1;
/*
 * holds the current x and y coordinates for XTestMovePointer
 */
static int	current_x = 0;
static int	current_y = 0;
/*
 * Holds input actions being accumulated until the input action buffer is
 * full or until XTestFlush is called.
 */
static CARD8		action_buf[XTestMAX_ACTION_LIST_SIZE];
/*
 * the index into the input action buffer
 */
static int		action_index = 0;
/*
 * the number of input actions that the server can handle at one time
 */
static unsigned long	action_array_size = 0;
/*
 * the current number of input actions
 */
static unsigned long	action_count = 0;

/******************************************************************************
 * function declarations
 *****************************************************************************/

static int	XTestWireToEvent();
static int	XTestCheckExtInit();
static Bool	XTestIdentifyMyEvent();
static int	XTestInitExtension();
static int	XTestKeyOrButton();
static int	XTestCheckDelay();
static int	XTestPackInputAction();
static int	XTestWriteInputActions();

/******************************************************************************
 *
 *	XTestFakeInput
 *
 *	Send a a request containing one or more input actions to be sent
 *	to the server by this extension.
 */
int
XTestFakeInput(dpy, action_list_addr, action_list_size, ack_flag)
/*
 * the connection to the X server
 */
register Display	*dpy;
/*
 * the address of a list of input actions to be sent to the server
 */
char			*action_list_addr;
/*
 * the size (in bytes) of the list of input actions
 */
int			action_list_size;
/*
 * specifies whether the server needs to send an event to indicate that its
 * input action buffer is empty
 */
int			ack_flag;
{	
	/*
	 * pointer to xTestFakeInputReq structure
	 */
	xTestFakeInputReq	*req;
	/*
	 * loop index
	 */
	int			i;

	LockDisplay(dpy);
	if ((XTestCheckExtInit(dpy) == -1) ||
	    (action_list_size > XTestMAX_ACTION_LIST_SIZE))
	{
		/*
		 * if the extension is not installed in the server or the 
		 * action list will not fit in the request, then unlock
		 * the display and return -1.
		 */
		UnlockDisplay(dpy);
		return(-1);
	}
	else
	{
		/*
		 * Get the next available X request packet in the buffer.
		 * It sets the `length' field to the size (in 32-bit words)
		 * of the request.  It also sets the `reqType' field in the
		 * request to X_TestFakeInput, which is not what is needed.
		 *
		 * GetReq is a macro defined in Xlibint.h.
		 */
		GetReq(TestFakeInput, req);		
		/*
		 * fix up the request type code to what is needed
		 */
		req->reqType = XTestReqCode;
		/*
		 * set the minor request type code to X_TestFakeInput
		 */
		req->XTestReqType = X_TestFakeInput;
		/*
		 * set the ack code
		 */
		req->ack = ack_flag;
		/*
		 * Set the action_list area to all 0's. An input action header
		 * value of 0 is interpreted as a flag to the input action
		 * list handling code in the server part of this extension
		 * that there are no more input actions in this request.
		 */
		for (i = 0; i < XTestMAX_ACTION_LIST_SIZE; i++)
		{
			req->action_list[i] = 0;
		}
		/*
		 * copy the input actions into the request
		 */
		for (i = 0; i < action_list_size; i++)
		{
			req->action_list[i] = *(action_list_addr++);
		}
		UnlockDisplay(dpy);
		SyncHandle();
		return(0);
	}
}

/******************************************************************************
 *
 *	XTestGetInput
 *
 *	Request the server to begin putting user input actions into events
 *	to be sent to the client that called this function.
 */
int
XTestGetInput(dpy, action_handling)
/*
 * the connection to the X server
 */
register Display	*dpy;
/*
 * tells the server what to do with the user input actions
 */
int			action_handling;
{	
	/*
	 * pointer to xTestGetInputReq structure
	 */
	xTestGetInputReq 	*req;

	LockDisplay(dpy);
	if (XTestCheckExtInit(dpy) == -1)
	{
		/*
		 * if the extension is not installed in the server
		 * then unlock the display and return -1.
		 */
		UnlockDisplay(dpy);
		return(-1);
	}
	else
	{
		/*
		 * Get the next available X request packet in the buffer.
		 * It sets the `length' field to the size (in 32-bit words)
		 * of the request.  It also sets the `reqType' field in the
		 * request to X_TestGetInput, which is not what is needed.
		 *
		 * GetReq is a macro defined in Xlibint.h.
		 */
		GetReq(TestGetInput, req);		
		/*
		 * fix up the request type code to what is needed
		 */
		req->reqType = XTestReqCode;
		/*
		 * set the minor request type code to X_TestGetInput
		 */
		req->XTestReqType = X_TestGetInput;
		/*
		 * set the action handling mode
		 */
		req->mode = action_handling;
		UnlockDisplay(dpy);
		SyncHandle();
		return(0);
	}
}

/******************************************************************************
 *
 *	XTestStopInput
 *
 *	Tell the server to stop putting information about user input actions
 *	into events.
 */
int
XTestStopInput(dpy)
/*
 * the connection to the X server
 */
register Display	*dpy;
{	
	/*
	 * pointer to xTestStopInputReq structure
	 */
	xTestStopInputReq 	*req;

	LockDisplay(dpy);
	if (XTestCheckExtInit(dpy) == -1)
	{
		/*
		 * if the extension is not installed in the server
		 * then unlock the display and return -1.
		 */
		UnlockDisplay(dpy);
		return(-1);
	}
	else
	{
		/*
		 * Get the next available X request packet in the buffer.
		 * It sets the `length' field to the size (in 32-bit words)
		 * of the request.  It also sets the `reqType' field in the
		 * request to X_TestStopInput, which is not what is needed.
		 *
		 * GetReq is a macro defined in Xlibint.h.
		 */
		GetReq(TestStopInput, req);		
		/*
		 * fix up the request type code to what is needed
		 */
		req->reqType = XTestReqCode;
		/*
		 * set the minor request type code to X_TestStopInput
		 */
		req->XTestReqType = X_TestStopInput;
		UnlockDisplay(dpy);
		SyncHandle();
		return(0);
	}
}

/******************************************************************************
 *
 *	XTestReset
 *
 *	Tell the server to set everything having to do with this extension
 *	back to its initial state.
 */
int
XTestReset(dpy)
/*
 * the connection to the X server
 */
register Display	*dpy;
{	
	/*
	 * pointer to xTestReset structure
	 */
	xTestResetReq 	*req;

	LockDisplay(dpy);
	if (XTestCheckExtInit(dpy) == -1)
	{
		/*
		 * if the extension is not installed in the server
		 * then unlock the display and return -1.
		 */
		UnlockDisplay(dpy);
		return(-1);
	}
	else
	{
		/*
		 * Get the next available X request packet in the buffer.
		 * It sets the `length' field to the size (in 32-bit words)
		 * of the request.  It also sets the `reqType' field in the
		 * request to X_TestReset, which is not what is needed.
		 *
		 * GetReq is a macro defined in Xlibint.h.
		 */
		GetReq(TestReset, req);		
		/*
		 * fix up the request type code to what is needed
		 */
		req->reqType = XTestReqCode;
		/*
		 * set the minor request type code to X_TestReset
		 */
		req->XTestReqType = X_TestReset;
		UnlockDisplay(dpy);
		SyncHandle();
		return(0);
	}
}

/******************************************************************************
 *
 *	XTestQueryInputSize
 *
 *	Returns the number of input actions in the server's input action buffer.
 */
int
XTestQueryInputSize(dpy, size_return)
/*
 * the connection to the X server
 */
register Display	*dpy;
/*
 * the address of the place to put the number of input actions in the
 * server's input action buffer
 */
unsigned long		*size_return;
{	
	/*
	 * pointer to xTestQueryInputSize structure
	 */
	xTestQueryInputSizeReq 		*req;
	/*
	 * pointer to xTestQueryInputSize structure
	 */
	xTestQueryInputSizeReply 	rep;

	LockDisplay(dpy);
	if (XTestCheckExtInit(dpy) == -1)
	{
		/*
		 * if the extension is not installed in the server
		 * then unlock the display and return -1.
		 */
		UnlockDisplay(dpy);
		return(-1);
	}
	else
	{
		/*
		 * Get the next available X request packet in the buffer.
		 * It sets the `length' field to the size (in 32-bit words)
		 * of the request.  It also sets the `reqType' field in the
		 * request to X_TestQueryInputSize, which is not what is needed.
		 *
		 * GetReq is a macro defined in Xlibint.h.
		 */
		GetReq(TestQueryInputSize, req);		
		/*
		 * fix up the request type code to what is needed
		 */
		req->reqType = XTestReqCode;
		/*
		 * set the minor request type code to X_TestQueryInputSize
		 */
		req->XTestReqType = X_TestQueryInputSize;
		/*
		 * get a reply from the server
		 */
		(void) _XReply (dpy, (xReply *) &rep, 0, xTrue);
		/*
		 * put the size in the caller's variable
		 */
		*size_return = (unsigned long) rep.size_return;
		UnlockDisplay(dpy);
		SyncHandle();
		return(0);
	}
}

/******************************************************************************
 *
 *	XTestCheckExtInit
 *
 *	Check to see if the XTest extension is installed in the server.
 */
static int
XTestCheckExtInit(dpy)
/*
 * the connection to the X server
 */
register Display	*dpy;
{
	/*
	 * if the extension has not been initialized, then do so
	 */
	if (!XTestReqCode) 
	{
		return(XTestInitExtension(dpy));
	}
	return(0);
}

/******************************************************************************
 *
 *	XTestInitExtension
 *
 *	Attempt to initialize this extension in the server.  Return 0 if it
 *	succeeds, -1 if it does not succeed.
 */
static int
XTestInitExtension(dpy)
/*
 * the connection to the X server
 */
register Display	*dpy;
{
	/*
	 * loop index
	 */
	int			i;
	/*
	 * return value from XInitExtension
	 */
	XExtCodes		*ret;

	/*
	 * attempt to initialize the extension
	 */
	ret = XInitExtension(dpy, XTestEXTENSION_NAME);
	/*
	 * if the initialize failed, return -1
	 */
	if (ret == NULL)
	{
		return (-1);
	}
	/*
	 * the initialize succeeded, remember the major opcode
	 * for this extension
	 */
	XTestReqCode = ret->major_opcode;
	/*
	 * set up the event handler for any events from 
	 * this extension
	 */
	for (i = 0; i < XTestEVENT_COUNT; i++)
	{
		XESetWireToEvent(dpy,
				 ret->first_event+i,
				 XTestWireToEvent);
	}
	/*
	 * compute the event type codes for the events
	 * in this extension
	 */
	XTestInputActionType += ret->first_event;
	XTestFakeAckType += ret->first_event;
	/*
	 * everything worked ok
	 */
	return(0);
}

/******************************************************************************
 *
 *	XTestWireToEvent
 *
 *	Handle XTest extension events.
 *	Reformat a wire event into an XEvent structure of the right type.
 */
static Bool
XTestWireToEvent(dpy, reTemp, eventTemp)
/*
 * the connection to the X server
 */
Display	*dpy;
/*
 * a pointer to where a host formatted event should be stored
 * with the information copied to it
 */
XEvent	*reTemp;
/*
 * a pointer to the wire event
 */
xEvent	*eventTemp;
{
	XTestInputActionEvent *re    = (XTestInputActionEvent *) reTemp;
	xTestInputActionEvent *event = (xTestInputActionEvent *) eventTemp;

	/*
	 * loop index
	 */
	int	i;
	/*
	 * pointer to where the input actions go in the host format event
	 */
	CARD8	*to;
	/*
	 * pointer to the input actions in the wire event
	 */
	CARD8	*from;

	/*
	 * Copy the type of the wire event to the new event.
	 * This will work for either event type because the type,
	 * display, and window fields in the events have to be the same.
	 */
	re->type = event->type;
	/*
	 * set the display parameter in case it is needed (by who?)
	 */
	re->display = dpy;
	if (re->type == XTestInputActionType)
	{
		/*
		 * point at the first byte of input actions in the wire event
		 */
		from = &(event->actions[0]);
		/*
		 * point at where the input action bytes go in the new event
		 */
		to = &(re->actions[0]);
		/*
		 * copy the input action bytes from the wire event to
		 * the new event
		 */
		for (i = 0; i < XTestACTIONS_SIZE; i++)
		{
			*(to++) = *(from++);
		}
	}
	else if (re->type == XTestFakeAckType)
	{
		/*
		 * nothing else needs to be done
		 */
	}
	else
	{
		printf("XTestWireToEvent: UNKNOWN WIRE EVENT! type=%d\n",
			(int) event->type);
		printf("%s is giving up.\n", XTestEXTENSION_NAME);
		exit (1);
	}
	return 1;
}

/******************************************************************************
 *
 *	XTestPressKey
 *
 *	Send input actions to the server to cause the server to think
 *	that the specified key on the keyboard was moved as specified.
 */
int
XTestPressKey(display, device_id, delay, keycode, key_action)
Display		*display;
int		device_id;
unsigned long	delay;
unsigned int	keycode;
unsigned int	key_action;
{
	/*
	 * bounds check the key code
	 */
	if (keycode < 8 || keycode > 255)
	{
		return(-1);
	}
	/*
	 * use the commmon key/button handling routine
	 */
	return(XTestKeyOrButton(display,
				device_id,
				delay,
				keycode,
				key_action));
}

/******************************************************************************
 *
 *	XTestPressButton
 *
 *	Send input actions to the server to cause the server to think
 *	that the specified button on the mouse was moved as specified.
 */
int
XTestPressButton(display, device_id, delay, button_number, button_action)
Display		*display;
int		device_id;
unsigned long	delay;
unsigned int	button_number;
unsigned int	button_action;
{
	/*
	 * bounds check the button number
	 */
	if (button_number > 7)
	{
		return(-1);
	}
	/*
	 * use the commmon key/button handling routine
	 */
	return(XTestKeyOrButton(display,
				device_id,
				delay,
				button_number,
				button_action));
}

/******************************************************************************
 *
 *	XTestKeyOrButton
 *
 *	Send input actions to the server to cause the server to think
 *	that the specified key/button was moved as specified.
 */
static int
XTestKeyOrButton(display, device_id, delay, code, action)
Display		*display;
int		device_id;
unsigned long	delay;
unsigned int	code;
unsigned int	action;
{
	/*
	 * holds a key input action to be filled out and sent to the server
	 */
	XTestKeyInfo	keyinfo;

	/*
	 * bounds check the device id
	 */
	if (device_id < 0 || device_id > XTestMAX_DEVICE_ID)
	{
		return(-1);
	}
	/*
	 * fill out the key input action(s) as appropriate
	 */
	switch(action)
	{
	case XTestPRESS:
		/*
		 * Check the delay.  If it is larger than will fit in the
		 * key input action, send a delay input action.
		 */
		if(XTestCheckDelay(display, &delay) == -1)
		{
			/*
			 * an error occurred, return -1
			 */
			return(-1);
		}
		/*
		 * create the header 
		 */
		keyinfo.header = XTestPackDeviceID(device_id) |
				 XTestKEY_ACTION |
				 XTestKEY_DOWN;
		/*
		 * set the key/button code
		 */
		keyinfo.keycode = code;
		/*
		 * set the delay time
		 */
		keyinfo.delay_time = delay;
		/*
		 * pack the input action into a request to be sent to the
		 * server when the request is full or XTestFlush is called
		 */
		return(XTestPackInputAction(display,
					    (CARD8 *) &keyinfo,
					    sizeof(XTestKeyInfo)));
	case XTestRELEASE:
		/*
		 * Check the delay.  If it is larger than will fit in the
		 * key input action, send a delay input action.
		 */
		if(XTestCheckDelay(display, &delay) == -1)
		{
			/*
			 * an error occurred, return -1
			 */
			return(-1);
		}
		/*
		 * create the header 
		 */
		keyinfo.header = XTestPackDeviceID(device_id) |
				 XTestKEY_ACTION |
				 XTestKEY_UP;
		/*
		 * set the key/button code
		 */
		keyinfo.keycode = code;
		/*
		 * set the delay time
		 */
		keyinfo.delay_time = delay;
		/*
		 * pack the input action into a request to be sent to the
		 * server when the request is full or XTestFlush is called
		 */
		return(XTestPackInputAction(display,
					    (CARD8 *) &keyinfo,
					    sizeof(XTestKeyInfo)));
	case XTestSTROKE:
		/*
		 * Check the delay.  If it is larger than will fit in the
		 * key input action, send a delay input action.
		 */
		if(XTestCheckDelay(display, &delay) == -1)
		{
			/*
			 * an error occurred, return -1
			 */
			return(-1);
		}
		/*
		 * create a key/button-down input action header
		 */
		keyinfo.header = XTestPackDeviceID(device_id) |
				 XTestKEY_ACTION |
				 XTestKEY_DOWN;
		/*
		 * set the key/button code
		 */
		keyinfo.keycode = code;
		/*
		 * set the delay time
		 */
		keyinfo.delay_time = delay;
		/*
		 * pack the input action into a request to be sent to the
		 * server when the request is full or XTestFlush is called
		 */
		if (XTestPackInputAction(display,
					 (CARD8 *) &keyinfo,
					 sizeof(XTestKeyInfo)) == -1)
		{
			/*
			 * an error occurred, return -1
			 */
			return(-1);
		}
		/*
		 * set the delay to XTestSTROKE_DELAY_TIME
		 */
		delay = XTestSTROKE_DELAY_TIME;
		/*
		 * Check the delay.  If it is larger than will fit in the
		 * key input action, send a delay input action.
		 */
		if(XTestCheckDelay(display, &delay) == -1)
		{
			/*
			 * an error occurred, return -1
			 */
			return(-1);
		}
		/*
		 * create a key/button-up input action header
		 */
		keyinfo.header = XTestPackDeviceID(device_id) |
				 XTestKEY_ACTION |
				 XTestKEY_UP;
		/*
		 * set the key/button code
		 */
		keyinfo.keycode = code;
		/*
		 * set the delay time
		 */
		keyinfo.delay_time = delay;
		/*
		 * pack the input action into a request to be sent to the
		 * server when the request is full or XTestFlush is called
		 */
		return(XTestPackInputAction(display,
					    (CARD8 *) &keyinfo,
					    sizeof(XTestKeyInfo)));
	default:
		/*
		 * invalid action value, return -1
		 */
		 return(-1);
	}
}

/******************************************************************************
 *
 *	XTestMovePointer
 *
 *	Send input actions to the server to cause the server to think
 *	that the mouse was moved as specified.
 */
int
XTestMovePointer(display, device_id, delay, x, y, count)
Display		*display;
int		device_id;
unsigned long	delay[];
int		x[];
int		y[];
unsigned int	count;
{
	/*
	 * holds a motion input action to be filled out and sent to the server
	 */
	XTestMotionInfo	motioninfo;
	/*
	 * holds a jump input action to be filled out and sent to the server
	 */
	XTestJumpInfo	jumpinfo;
	/*
	 * loop index
	 */
	unsigned int	i;
	/*
	 * holds the change in x and y directions from the current x and y
	 * coordinates
	 */
	int	dx;
	int	dy;

	/*
	 * bounds check the device id
	 */
	if (device_id < 0 || device_id > XTestMAX_DEVICE_ID)
	{
		return(-1);
	}
	/*
	 * if the count is 0, there is nothing to do.  return 0
	 */
	if (count == 0)
	{
		return(0);
	}
	/*
	 * loop through the pointer motions, creating the appropriate
	 * input actions for each motion
	 */
	for (i = 0; i < count; i++)
	{
		/*
		 * Check the delay.  If it is larger than will fit in the
		 * input action, send a delay input action.
		 */
		if(XTestCheckDelay(display, &(delay[i])) == -1)
		{
			/*
			 * an error occurred, return -1
			 */
			return(-1);
		}
		/*
		 * compute the change from the current x and y coordinates
		 * to the new x and y coordinates
		 */
		dx = x[i] - current_x;
		dy = y[i] - current_y;
		/*
		 * update the current x and y coordinates
		 */
		current_x = x[i];
		current_y = y[i];
		/*
		 * If the pointer motion range is too large to fit into
		 * a motion input action, then use a jump input action.
		 * Otherwise, use a motion input action.
		 */
		 if ((dx > XTestMOTION_MAX) || (dx < XTestMOTION_MIN) ||
		     (dy > XTestMOTION_MAX) || (dy < XTestMOTION_MIN))
		{
			/*
			 * create a jump input action header
			 */
			jumpinfo.header = XTestPackDeviceID(device_id) |
					  XTestJUMP_ACTION;
			/*
			 * set the x and y coordinates to jump to
			 */
			jumpinfo.jumpx = x[i];
			jumpinfo.jumpy = y[i];
			/*
			 * set the delay time
			 */
			jumpinfo.delay_time = delay[i];
			/*
			 * pack the jump input action into a request to be
			 * sent to the server when the request is full
			 * or XTestFlush is called
			 */
			if (XTestPackInputAction(display,
						 (CARD8 *) &jumpinfo,
						 sizeof(XTestJumpInfo)) == -1)
			{
				/*
				 * an error occurred, return -1
				 */
				return(-1);
			}
		}
		else
		{
			/*
			 * create a motion input action header
			 */
			motioninfo.header = XTestPackDeviceID(device_id) |
					    XTestMOTION_ACTION;
			/*
			 * compute the motion data byte
			 */
			if (dx < 0)
			{
				motioninfo.header |= XTestX_NEGATIVE;
				dx = abs(dx);
			}
			if (dy < 0)
			{
				motioninfo.header |= XTestY_NEGATIVE;
				dy = abs(dy);
			}
			motioninfo.motion_data = XTestPackXMotionValue(dx);
			motioninfo.motion_data |= XTestPackYMotionValue(dy);
			/*
			 * set the delay time
			 */
			motioninfo.delay_time = delay[i];
			/*
			 * pack the motion input action into a request to be
			 * sent to the server when the request is full
			 * or XTestFlush is called
			 */
			if (XTestPackInputAction(display,
						 (CARD8 *) &motioninfo,
						 sizeof(XTestMotionInfo)) == -1)
			{
				/*
				 * an error occurred, return -1
				 */
				return(-1);
			}
		}
	}
	/*
	 * if you get here, everything went ok
	 */
	return(0);
}

/******************************************************************************
 *
 *	XTestCheckDelay
 *
 *	Check the delay value at the passed-in address.  If it is larger than
 *	will fit in a normal input action, then send a delay input action.
 */
static int
XTestCheckDelay(display, delay_addr)
Display		*display;
unsigned long	*delay_addr;
{
	/*
	 * holds a delay input action to be filled out and sent to the server
	 */
	XTestDelayInfo	delayinfo;

	/*
	 * if the delay value will fit in the input action,
	 * then there is no need for a delay input action
	 */
	if (*delay_addr <= XTestSHORT_DELAY_TIME)
	{
		return(0);
	}
	/*
	 * fill out a delay input action
	 */
	delayinfo.header = XTestPackDeviceID(XTestDELAY_DEVICE_ID);
	delayinfo.delay_time = *delay_addr;
	/*
	 * all of the delay time will be accounted for in the
	 * delay input action, so set the original delay value to 0
	 */
	*delay_addr = 0;
	/*
	 * pack the delay input action into a request to be sent to the
	 * server when the request is full or XTestFlush is called
	 */
	return(XTestPackInputAction(display,
				    (CARD8 *) &delayinfo,
				    sizeof(XTestDelayInfo)));
}

/******************************************************************************
 *
 *	XTestPackInputAction
 *
 *	If the input action buffer is full or the number of input actions
 *	has reached the maximum that the server can handle at one time,
 *	then send the input actions to the server using XTestFakeInput.
 */
static int
XTestPackInputAction(display, action_addr, action_size)
Display	*display;
CARD8	*action_addr;
int	action_size;
{
	/*
	 * loop index
	 */
	int	i;
	/*
	 * acknowledge flag
	 */
	int	ack_flag;

	/*
	 * if we don't already know it, find out how many input actions
	 * the server can handle at one time
	 */
	if (action_array_size == 0)
	{
		if(XTestQueryInputSize(display, &action_array_size) == -1)
		{
			/*
			 * if an error, return -1
			 */
			return(-1);
		}
	}
	/*
	 * if the specified input action will fit in the the input
	 * action buffer and won't exceed the server's capacity, then
	 * put the input action into the input buffer
	 */
	if(((action_index + action_size) <= XTestMAX_ACTION_LIST_SIZE) &&
	   ((action_count + 1) < action_array_size))
	{
		/*
		 * copy the input action into the buffer
		 */
		for (i = 0; i < action_size; i++)
		{
			action_buf[action_index++] = *(action_addr++);
		}
		/*
		 * increment the action count
		 */
		action_count++;
		/*
		 * everything went ok, return 0
		 */
		return(0);
	}
	/*
	 * We have to write input actions to the server.  If the server's
	 * input action capacity will be reached, then ask for an
	 * acknowledge event when the server has processed all of the 
	 * input actions.  Otherwise, an acknowledge event is not needed.
	 */
	if (action_count >= action_array_size)
	{
		ack_flag = XTestFAKE_ACK_REQUEST;
	}
	else
	{
		ack_flag = XTestFAKE_ACK_NOT_NEEDED;
	}
	/*
	 * write the input actions to the server
	 */
	if (XTestWriteInputActions(display,
				   (char *) &(action_buf[0]),
				   action_index,
				   ack_flag) == -1)
	{
		/*
		 * error, return -1
		 */
		return(-1);
	}
	/*
	 * copy the input action into the buffer
	 */
	for (i = 0; i < action_size; i++)
	{
		action_buf[action_index++] = *(action_addr++);
	}
	/*
	 * increment the action count
	 */
	action_count++;
	return(0);
}

/******************************************************************************
 *
 *	XTestWriteInputActions
 *
 *	Send input actions to the server.
 */
static int
XTestWriteInputActions(display, action_list_addr, action_list_size, ack_flag)
Display	*display;
char	*action_list_addr;
int	action_list_size;
int	ack_flag;
{
	/*
	 * Holds an event.  Used while waiting for an acknowledge event
	 */
	XEvent	event;
	/*
	 * points to XTestIdentifyMyEvent
	 */
	Bool	(*func_ptr)();

	/*
	 * write the input actions to the server
	 */
	if (XTestFakeInput(display,
			   action_list_addr,
			   action_list_size,
			   ack_flag) == -1)
	{
		/*
		 * if an error, return -1
		 */
		return(-1);
	}
	/*
	 * flush X's buffers to make sure that the server really gets
	 * the input actions
	 */
	XFlush(display);
	/*
	 * mark the input action buffer as empty
	 */
	action_index = 0;
	/*
	 * if we asked for an acknowledge event, then wait for it
	 */
	if (ack_flag == XTestFAKE_ACK_REQUEST)
	{
		/*
		 * point func_ptr at XTestIdentifyMyEvent
		 */
		func_ptr = XTestIdentifyMyEvent;
		/*
		 * Wait until the acknowledge event comes.  When the
		 * acknowledge event comes, it is removed from the event
		 * queue without disturbing any other events that might
		 * be in the queue.
		 */
		XIfEvent(display, &event, func_ptr, NULL);
		/*
		 * set the input action count back to 0
		 */
		action_count = 0;
	}
	/*
	 * if we got here, then everything is ok, return 0
	 */
	return(0);
}

/******************************************************************************
 *
 *	XTestIdentifyMyEvent
 *
 *	This function is called by XIfEvent to look at an event and see if
 *	it is of XTestFakeAckType.
 */
static	Bool
XTestIdentifyMyEvent(display, event_ptr, args)
Display	*display;
/*
 * Holds the event that this routiine is supposed to look at.
 */
XEvent	*event_ptr;
/*
 * this points to any user-specified arguments (ignored)
 */
char	*args;
{
	/*
	 * if the event if of the correct type, return the Bool True,
	 * otherwise return the Bool False.
	 */
	if (event_ptr->type == XTestFakeAckType)
	{
		return(True);
	}
	else
	{
		return(False);
	}
}

/******************************************************************************
 *
 *	XTestFlush
 *
 *	Send any input actions in the input action buffer to the server.
 */
int
XTestFlush(display)
Display	*display;
{
	/*
	 * acknowledge flag
	 */
	int	ack_flag;

	/*
	 * if there are no input actions in the input action buffer,
	 * then return 0
	 */
	if (action_index == 0)
	{
		return(0);
	}
	/*
	 * We have input actions to write to the server.  We will
	 * wait until the server has finished processing the input actions.
	 */
	ack_flag = XTestFAKE_ACK_REQUEST;
	/*
	 * write the input actions to the server
	 */
	return(XTestWriteInputActions(display,
				      (char *) &(action_buf[0]),
				      action_index,
				      ack_flag));
}