xtest1dd.c 38.5 KB
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 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
/*
 *	File: xtest1dd.c
 *
 *	This file contains the device dependent parts of the input
 *	synthesis extension.
 */

/*


Copyright 1986, 1987, 1988, 1998  The Open Group

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.

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.

*/

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

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <stdio.h>
#include <nx-X11/Xos.h>
#include <nx-X11/X.h>
#include <nx-X11/Xmd.h>
#include <nx-X11/Xproto.h>
#include "misc.h"
#include "dixstruct.h"
#define  XTestSERVER_SIDE
#include <nx-X11/extensions/xtestext1.h>	

#include "xtest1dd.h"

/***************************************************************
 * defines
 ***************************************************************/

/*
 * the size of the fake input action array
 */
#define ACTION_ARRAY_SIZE	100

/***************************************************************
 * externals
 ***************************************************************/

/*
 * Holds the xTestInputAction event type code.
 * This is defined in xtestext1di.c.
 */
extern int			XTestInputActionType;
/*
 * Holds the xTestFakeAck event type code.
 * This is defined in xtestext1di.c.
 */
extern int			XTestFakeAckType;
/*
 * used in the WriteReplyToClient macro
 */
extern int			exclusive_steal;

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

/*
 * array to hold fake input actions
 */
struct {
	/*
	 * holds the action type, one of: XTestDELAY_ACTION,
	 * XTestKEY_ACTION, XTestMOTION_ACTION, XTestJUMP_ACTION
	 */
	CARD8	type;	
	/*
	 * holds the device type, in the range 0 to 15
	 */
	CARD8	device;
	/*
	 * for XTestKEY_ACTION type, holds the keycode
	 */
	CARD8	keycode;
	/*
	 * for XTestKEY_ACTION type, holds the key up/down state
	 */
	CARD8	keystate;
	/*
	 * for XTestMOTION_ACTION and XTestJUMP_ACTION types,
	 * holds the x and y coordinates to move the mouse to
	 */
	int	x;
	int	y;
	/*
	 * holds the time to delay (in milliseconds) before performing
	 * the action
	 */
	CARD32	delay_time;
}action_array[ACTION_ARRAY_SIZE];

/*
 * write index for input action array
 */
static int			write_index = 0;
/*
 * read index for input action array
 */
static int			read_index = 0;
/*
 * this is where the input actions are accumulated until they are sent
 * to a client (in a wire event)
 */
static xTestInputActionEvent	input_action_packet;
/*
 * holds the index (in bytes) into the input actions buffer in the
 * current input action event
 */
static int 			packet_index;
/*
 * logical x position of the mouse during input action gathering
 */
short				xtest_mousex;
/*
 * logical y position of the mouse during input action gathering
 */
short				xtest_mousey;
/*
 * logical x position of the mouse while we are reading fake input actions
 * from the client and putting them into the fake input action array
 */
static short			pmousex;
/*
 * logical y position of the mouse while we are reading fake input actions
 * from the client and putting them into the fake input action array
 */
static short			pmousey;
/*
 * The playback_on flag is set to 1 while there are input actions in the 
 * input action array.  It is set to 0 when the server has received all of
 * the user actions.
 */
int			playback_on = 0;
/*
 * identity of the client using XTestGetInput to get user input actions
 */
ClientPtr 		current_xtest_client;
/*
 * if 1 send multiple input actions per XTestInputAction event;
 * if 0 send one input action per XTestInputAction event
 */
static char			packed_mode;
/*
 * identity of the client using the XTestFakeInput function to send some
 * fake input actions to the server
 */
ClientPtr		playback_client = NULL;
/*
 * Set to 1 when the XTestFAKE_ACK_REQUEST flag is set in a XTestFakeInput
 * request.  Set back to 0 when all of the input actions have been sent
 * to the server.
 */
static int			acknowledge = 0;
/*
 * The server's idea of the current time is saved in these variables when
 * a XTestFakeInput request is received.  It is restored when all fake input
 * actions are sent to the server or when the playback client disconnects.
 */
static int			saved_sec;
static int			saved_usec;
/*
 * Set to 1 when there is a valid time in saved_sec and saved_usec.
 */
static int			time_saved = 0;
/*
 * holds the extension's notion of what the current time is while it is 
 * sending input actions to a client
 */
static struct timeval		current_time;
/*
 * holds the time when the extension should place the next fake input action
 * into the server's normal events queue
 */
static struct timeval		play_time;
/*
 * set to 1 when play_time is first set, cleared to 0 when the
 * client using the extension disconnects, or when XTestReset is called
 */
static char			play_clock = 0;
/*
 * holds the amount of time left until the next input action from the
 * input action array can be sent to the server
 */
static struct timeval		rtime;
/*
 * Set to 1 after the extension is done waiting for the correct time delay
 * for an input action to be sent to the server.  Remains a 1 until the time
 * delay for the next input action is computed.  Then set to 0 if the
 * extension has to wait for the correct time delay.
 */
static int			go_for_next = 1;
/*
 * needed to restore waitime if playback is to be aborted
 */
static struct timeval		*restorewait;
/*
 * tmon special command key
 *
 * To use the test monitor program (called tmon) efficiently, it is
 * desirable to have the extension be able to recognize a special "trigger"
 * key.  If the extension did not do this, tmon would have to have the
 * extension send all keyboard user input actions exclusively to tmon,
 * only to have tmon send them right back if they were not the command key.
 *
 * If the extension can recognize the command key, then tmon can let the
 * extension handle keyboard user input actions normally until the command
 * key is pressed (and released), and only then have the extension start
 * sending keyboard user input actions exclusively to tmon.
 *
 * Any key on the keyboard can be used for this command key.  It is most
 * convenient if it is a low-frequency key.  If you want to generate a
 * normal occurrance of this key to a client, just hit it twice.  Tmon
 * will recognize the first occurrance of the key, take control of the input
 * actions, and wait for certain keys.  If it sees another occurrance of the
 * command key, it will send one occurrance of the command key to the
 * extension, and go back to waiting.
 *
 * set and also referenced in device layer
 * XXX there should be a way to set this through the protocol
 */
KeyCode			xtest_command_key = 0;

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

static void	parse_key_fake(
			XTestKeyInfo	* /* fkey */
			);
static void	parse_motion_fake(
			XTestMotionInfo	* /* fmotion */
			);
static void	parse_jump_fake(
			XTestJumpInfo	* /* fjump */
			);
static void	parse_delay_fake(
			XTestDelayInfo	* /* tevent */
			);
static void	send_ack(
			ClientPtr	 /* client */
			);
static void	start_play_clock(
			void
			);
static void	compute_action_time(
			struct timeval	* /* rtime */
			);
static int	find_residual_time(
			struct timeval	* /* rtime */
			);

static CARD16	check_time_event(
			void
			);
static CARD32	current_ms(
			struct timeval	* /* otime */
			);
static int	there_is_room(
			int	/* actsize */
			);

/******************************************************************************
 *
 * 	stop_stealing_input
 *
 *	Stop stealing input actions.
 */
void
stop_stealing_input()
{
/*
 * put any code that you might need to stop stealing input actions here
 */
	if (packet_index != 0) 
	{
		/*
		 * if there is a partially full input action event waiting
		 * when this function is called, send it to the client
		 */
		flush_input_actions();
	}
}

/******************************************************************************
 *
 * 	steal_input
 *
 *	Start stealing input actions and sending them to the passed-in client.
 */
void
steal_input(client, mode)
/*
 * which client is to receive the input action events
 */
ClientPtr	client;
/*
 * what input action packing mode to use.  one of 0, XTestPACKED_MOTION,
 * or XTestPACKED_ACTIONS; optionally 'or'ed with XTestEXCLUSIVE,
 */
CARD32		mode;
{
	if (packet_index != 0) 
	{
		/*
		 * if there is a partially full input action event waiting
		 * when this function is called, send it to the client
		 */
		flush_input_actions();
	}
	else
	{	
		/*
		 * otherwise, set up a new input action event
		 */
		input_action_packet.type = XTestInputActionType;
		packet_index = 0;
	}
	/*
	 * set up the new input action packing mode
	 */
	packed_mode = mode & ~(XTestEXCLUSIVE);
	/*
	 * keep track of where the mouse is
	 */
	XTestGetPointerPos(&xtest_mousex, &xtest_mousey);
	/*
	 * keep track of which client is getting input actions
	 */
	current_xtest_client = client;
	/*
	 * find out what time it is
	 */
	X_GETTIMEOFDAY(&current_time);
	/*
	 * jump to the initial position of the mouse, using a device type of 0.
	 */
	XTestStealJumpData(xtest_mousex, xtest_mousey, 0);
}
	
/******************************************************************************
 *
 *	flush_input_actions
 *
 *	Write the input actions event to the current requesting client
 *	and re-initialize the input action event.
 */
void
flush_input_actions()
{
	/*
	 * pointer to the input action event
	 */
	char			*rep;
	/*
	 * loop index
	 */
	int			i;

	if (packet_index == 0)
	{
		/*
		 * empty input actions event 
		 */
		return;
	}
	else if (packet_index < XTestACTIONS_SIZE)
	{
		/*
		 * fill to the end of the input actions event with 0's
		 */
		for (i = packet_index; i <XTestACTIONS_SIZE; i++)
		{
			input_action_packet.actions[i] = 0;
		}
	}
	rep = (char *) (&input_action_packet);

	/*
	 * set the serial number of the input action event
	 */
	input_action_packet.sequenceNumber = current_xtest_client->sequence;
	/*
	 * send the input action event to the client
	 */
	WriteEventsToClient(current_xtest_client, 1, (xEvent *) rep);
	/*
	 * re-initialize the input action event
	 */
	input_action_packet.type = XTestInputActionType;
 	packet_index = 0;
}	

/******************************************************************************
 *
 *	XTestStealJumpData
 *
 *	Create one or more input actions and put them in the input action
 *	event.  The input actions will be an (maybe) XTestDELAY_ACTION
 *	and an XTestJUMP_ACTION.
 */
void
XTestStealJumpData(jx, jy, dev_type)
/*
 * the x and y coordinates to jump to
 */
int	jx;
int	jy;
/*
 * which device caused the jump
 */
int	dev_type;
{	
	XTestJumpInfo 	*jmp_ptr;
	/*
	 * time delta (in ms) from previous event
	 */
	CARD16			tchar;

	/*
	 * Get the time delta from the previous event.  If needed,
	 * the check_time_event routine will put an XTestDELAY_ACTION
	 * type action in the input action event.
	 */
	tchar = check_time_event();
	if (!there_is_room(sizeof(XTestJumpInfo)))
	{
		/*
		 * If there isn't room in the input action event for
		 * an XTestJUMP_ACTION, then send that event to the
		 * client and start filling an empty one.
		 */
		flush_input_actions();
	}
	/*
	 * update the logical mouse position
	 */
	xtest_mousex = jx;
	xtest_mousey = jy;
	/*
	 * point jmp_ptr to the correct place in the input action event
	 */
	jmp_ptr = (XTestJumpInfo *)
		  &(input_action_packet.actions[packet_index]);
	/*
	 * compute the input action header
	 */
	jmp_ptr->header = (XTestPackDeviceID(dev_type) | XTestJUMP_ACTION);	
	/*
	 * set the x and y coordinates to jump to in the input action
	 */
	jmp_ptr->jumpx = jx;
	jmp_ptr->jumpy = jy;
	/*
	 * set the delay time in the input action
	 */
	jmp_ptr->delay_time = tchar;
	/*
	 * increment the packet index by the size of the input action
	 */
	packet_index = packet_index + sizeof(XTestJumpInfo);
	if (packed_mode == 0)
	{
		/*
		 * if input actions are not packed, send the input
		 * action event to the client
		 */
		flush_input_actions();
	}
}	

/******************************************************************************
 *
 *	current_ms
 *
 *	Returns the number of milliseconds from the passed-in time to the
 *	current time, and then updates the passed-in time to the current time.
 */
static CARD32
current_ms(otime)
struct timeval	*otime;
{	
	struct timeval	tval;
	unsigned long	the_ms;
	unsigned long	sec;
	unsigned long	usec;

	/*
	 * get the current time
	 */
	X_GETTIMEOFDAY(&tval);
	if (tval.tv_usec < otime->tv_usec)
	{
		/*
		 * borrow a second's worth of microseconds if needed
		 */
		usec = tval.tv_usec - otime->tv_usec + 1000000;
		sec = tval.tv_sec - 1 - otime->tv_sec;
	}
	else
	{
		usec = tval.tv_usec - otime->tv_usec;
		sec = tval.tv_sec - otime->tv_sec;
	}
	/*
	 * update the passed-in time to the new time
	 */
	*otime = tval;
	/*
	 * compute the number of milliseconds contained in
	 * 'sec' seconds and 'usec' microseconds
	 */
	the_ms = (sec * 1000000L + usec) / 1000L;
	return (the_ms);
}

/******************************************************************************
 *
 *	check_time_event
 *
 *	If time delta is > XTestSHORT_DELAY_TIME then insert a time event
 *	and return 0; else return the delay time.
 */
static CARD16
check_time_event()
{
	CARD32		tstamp;
	CARD16		tchar;
	XTestDelayInfo	*tptr;

	/*
	 * get the number of milliseconds between input actions
	 */
	tstamp = current_ms(&current_time);
	/*
	 * if the number of milliseconds is too large to fit in a CARD16,
	 * then add a XTestDELAY_ACTION to the input action event.
	 */
	if (tstamp > XTestSHORT_DELAY_TIME)
	{
		/*
		 * If there isn't room in the input action event for
		 * an XTestDELAY_ACTION, then send that event to the
		 * client and start filling an empty one.
		 */
		if (!there_is_room(sizeof(XTestDelayInfo)))
		{
			flush_input_actions();
		}
		/*
		 * point tptr to the correct place in the input action event
		 */
		tptr = (XTestDelayInfo *)
		       (&(input_action_packet.actions[packet_index]));
		/*
		 * compute the input action header
		 */
		tptr->header = XTestPackDeviceID(XTestDELAY_DEVICE_ID) |
			       XTestDELAY_ACTION;
		/*
		 * set the delay time in the input action
		 */
		tptr->delay_time = tstamp;
		/*
		 * increment the packet index by the size of the input action
		 */
		packet_index = packet_index + (sizeof(XTestDelayInfo));
		if (packed_mode != XTestPACKED_ACTIONS) 
		{
			/*
			 * if input actions are not packed, send the input
			 * action event to the client
			 */
			flush_input_actions();
		}
		/*
		 * set the returned delay time to 0
		 */
		tchar = 0;
	}
	else
	{
		/*
		 * set the returned delay time to the computed delay time
		 */
		tchar = tstamp;
	}
	return(tchar);
}

/******************************************************************************
 *
 *	there_is_room
 *
 *	Checks if there is room in the input_action_packet for an input action
 *	of the size actsize bytes.  Returns 1 if there is space, 0 otherwise.
 *
 */
static int
there_is_room(actsize)
/*
 * the number of bytes of space needed
 */
int	actsize;
{
	if ((packet_index + actsize) > XTestACTIONS_SIZE)
	{ 
		return(0);
	}
	else
	{
		return(1);
	}
}

/******************************************************************************
 *
 *	XTestStealMotionData
 *
 *	Put motion information from the locator into an input action.
 *
 *	called from x_hil.c
 */
void
XTestStealMotionData(dx, dy, dev_type, mx, my)
/*
 * the x and y delta motion of the locator
 */
int	dx;
int	dy;
/*
 * which locator did the moving
 */
int	dev_type;
/*
 * the x and y position of the locator before the delta motion
 */
int	mx;
int	my;
{
	/*
	 * pointer to a XTestMOTION_ACTION input action
	 */
	XTestMotionInfo	*fm;
	/*
	 * time delta from previous event
	 */
	CARD16			tchar;

	/*
	 * if the current position of the locator is not the same as
	 * the logical position, then update the logical position
	 */
	if ((mx != xtest_mousex) || (my != xtest_mousey))
	{
		XTestStealJumpData(mx, my, dev_type);
	}
	/*
	 * if the delta motion is outside the range that can
	 * be held in a motion input action, use a jump input action
	 */
	if ((dx > XTestMOTION_MAX) || (dx < XTestMOTION_MIN) ||
	    (dy > XTestMOTION_MAX) || (dy < XTestMOTION_MIN))
	{
		XTestStealJumpData((xtest_mousex + dx),
				   (xtest_mousey + dy), dev_type);
	}
	else
	{ 
		/*
		 * compute the new logical position of the mouse
		 */
		xtest_mousex += dx;
		xtest_mousey += dy;
		/*
		 * Get the time delta from the previous event.  If needed,
		 * the check_time_event routine will put an XTestDELAY_ACTION
		 * type action in the input action event.
		 */
		tchar = check_time_event();
		/*
		 * If there isn't room in the input action event for
		 * an XTestDELAY_ACTION, then send that event to the
		 * client and start filling an empty one.
		 */
		if (!there_is_room(sizeof(XTestMotionInfo)))
		{
			flush_input_actions();
		/*
		 * point fm to the correct place in the input action event
		 */
		}
		fm = (XTestMotionInfo *)
		     &(input_action_packet.actions[packet_index]);
		/*
		 * compute the input action header
		 */
		fm->header = XTestMOTION_ACTION;
		if (dx < 0)	
		{  
			fm->header |= XTestX_NEGATIVE;
			dx = abs(dx);
		}
		if (dy < 0)   
		{  
			fm->header |= XTestY_NEGATIVE;
			dy = abs(dy);
		}
		fm->header |= XTestPackDeviceID(dev_type);
		/*
		 * compute the motion data byte
		 */
		fm->motion_data = XTestPackYMotionValue(dy);
		fm->motion_data |= XTestPackXMotionValue(dx);
		/*
		 * set the delay time in the input action
		 */
		fm->delay_time = tchar;
		/*
		 * increment the packet index by the size of the input action
		 */
		packet_index = packet_index + sizeof(XTestMotionInfo);
		if (packed_mode == 0)
		{
			/*
			 * if input actions are not packed, send the input
			 * action event to the client
			 */
			flush_input_actions();
		}

	}   
}

/******************************************************************************
 *
 *	XTestStealKeyData
 *
 * 	Place this key data in the input_action_packet.
 *
 */
Bool
XTestStealKeyData(keycode, keystate, dev_type, locx, locy)
/*
 * which key/button moved
 */
unsigned	keycode;
/*
 * whether the key/button was pressed or released
 */
int		keystate;
/*
 * which device caused the input action
 */
int		dev_type;
/*
 * the x and y coordinates of the locator when the action happenned
 */
int		locx;
int		locy;
{
	/*
	 * pointer to key/button motion input action
	 */
	XTestKeyInfo	*kp;
	/*
	 * time delta from previous event
	 */
	CARD16			tchar;
	char		keytrans = 0;

	/*
	 * update the logical position of the locator if the physical position
	 * of the locator is not the same as the logical position.
	 */
	if ((locx != xtest_mousex) || (locy != xtest_mousey))
	{
		XTestStealJumpData(locx, locy, dev_type);
	}
	/*
	 * Get the time delta from the previous event.  If needed,
	 * the check_time_event routine will put an XTestDELAY_ACTION
	 * type action in the input action event.
	 */
	tchar = check_time_event();
	if (!there_is_room(sizeof(XTestKeyInfo)))
	{
		/*
		 * If there isn't room in the input action event for
		 * an XTestDELAY_ACTION, then send that event to the
		 * client and start filling an empty one.
		 */
		flush_input_actions();
	}
	/*
	 * point kp to the correct place in the input action event
	 */
	kp = (XTestKeyInfo *)
	     (&(input_action_packet.actions[packet_index]));
	/*
	 * compute the input action header
	 */
	kp->header = XTestPackDeviceID(dev_type);
	if ((keystate == KeyRelease) || (keystate == ButtonRelease))
	{
		keytrans = XTestKEY_UP;
	}
	else if ((keystate == KeyPress) || (keystate == ButtonPress))
	{
		keytrans = XTestKEY_DOWN;
	}
	else
	{
		printf("%s: invalid key/button state %d.\n",
		       XTestEXTENSION_NAME,
		       keystate);
	}
	kp->header = kp->header | keytrans | XTestKEY_ACTION;
	/*
	 * set the keycode in the input action
	 */
	kp->keycode = keycode;
	/*
	 * set the delay time in the input action
	 */
	kp->delay_time = tchar;
	/*
	 * increment the packet index by the size of the input action
	 */
	packet_index = packet_index + sizeof(XTestKeyInfo);
	/*
	 * if the command key has been released or input actions are not
	 * packed, send the input action event to the client
	 */
 	if(((keycode == xtest_command_key) && (keystate == KeyRelease)) ||
	   (packed_mode != XTestPACKED_ACTIONS))
	{	
		flush_input_actions();
	}
	/* return TRUE if the event should be passed on to DIX */
	if (exclusive_steal)
		return ((keystate == KeyRelease) &&
			(keycode == xtest_command_key));
	else
		return ((keystate != KeyRelease) ||
			(keycode != xtest_command_key));
}

/******************************************************************************
 *
 *	parse_fake_input
 *
 *	Parsing routine for a XTestFakeInput request.  It will take a request
 *	and parse its contents into the input action array.  Eventually the
 *	XTestProcessInputAction routine will be called to take input actions
 *	from the input action array and send them to the server to be handled.
 */
void
parse_fake_input(client, req)
/*
 * which client did the XTestFakeInput request
 */
ClientPtr	client;
/*
 * a pointer to the xTestFakeInputReq structure sent by the client
 */
char		*req;
{	
	/*
	 * if set to 1, done processing input actions from the request
	 */
	int	        	done = 0;
	/*
	 * type of input action
	 */
	CARD8			action_type;
	/*
	 * device type
	 */
	CARD8			dev_type;
	/*
	 * pointer to an xTestFakeInputReq structure
	 */
	xTestFakeInputReq	*request;
	/*
	 * holds the index into the action list in the request
	 */
	int			parse_index;	

	/*
	 * get a correct-type pointer to the client-supplied request data
	 */
	request = (xTestFakeInputReq *) req;
	/*
	 * save the acknowledge requested state for use in
	 * XTestProcessInputAction
	 */
	acknowledge = request->ack;
	/*
	 * set up an index into the action list in the request
	 */
	parse_index = 0;
	if (write_index >= ACTION_ARRAY_SIZE)
	{
		/*
		 * if the input action array is full, don't add any more
		 */
		done = 1;
	}
	while (!done)
	{ 
		/*
		 * get the type of input action in the list
		 */
		action_type = (request->action_list[parse_index])
			      & XTestACTION_TYPE_MASK;
		/*
		 * get the type of device in the list
		 */
		dev_type = XTestUnpackDeviceID(request->action_list[parse_index]);
		/*
		 * process the input action appropriately
		 */
		switch (action_type)
		{ 
		case XTestKEY_ACTION:
			parse_key_fake((XTestKeyInfo *)
				       &(request->action_list[parse_index]));
			parse_index = parse_index + sizeof(XTestKeyInfo);
			break;
		case XTestMOTION_ACTION:
			parse_motion_fake((XTestMotionInfo *)
					  &(request->action_list[parse_index]));
			parse_index = parse_index + sizeof(XTestMotionInfo);
			break;
		case XTestJUMP_ACTION:
			parse_jump_fake((XTestJumpInfo *)
					&(request->action_list[parse_index]));
			parse_index = parse_index + sizeof(XTestJumpInfo);
			break;
		case XTestDELAY_ACTION:
			if (dev_type == XTestDELAY_DEVICE_ID)
			{ 
				parse_delay_fake((XTestDelayInfo *)
						 &(request->action_list[parse_index]));
				parse_index = parse_index +
					      sizeof(XTestDelayInfo);
			}
			else
			{ 
				/*
				 * An invalid input action header byte has
				 * been detected, so there are no more
				 * input actions in this request.
				 * The intended invalid action header byte
				 * for this case should have a value of 0.
				 */
				done = 1;
			}
			break;
		}
		if (parse_index >= XTestMAX_ACTION_LIST_SIZE)
		{
			/*
			 * entire XTestFakeInput request has been processed
			 */
			done = 1;
		}
		if (write_index >= ACTION_ARRAY_SIZE) 
		{
			/*
			 * no room in the input actions array
			 */
			done = 1;
		}
	}
	if (write_index > read_index)
	{ 
		/*
		 * there are fake input actions in the input action array
		 * to be given to the server
		 */
		playback_on = 1;
		playback_client = client;
	} 
}

/******************************************************************************
 *
 *	parse_key_fake
 *
 *	Called from parse_fake_input.
 *
 *	Copy the fake key input action from its packed form into the array of
 *	pending input events.
 */
static void
parse_key_fake(fkey)
XTestKeyInfo	*fkey;
{	
	action_array[write_index].type = XTestKEY_ACTION;
	action_array[write_index].device = XTestUnpackDeviceID(fkey->header);
	action_array[write_index].keycode = fkey->keycode;
	action_array[write_index].keystate = fkey->header & XTestKEY_STATE_MASK;
	action_array[write_index].delay_time = fkey->delay_time;
	write_index++;
}

/******************************************************************************
 *
 *	parse_motion_fake
 *
 *	Called from parse_fake_input.
 *
 *	Copy the fake motion input action from its packed form into the array of
 *	pending input events.
 */
static void
parse_motion_fake(fmotion)
XTestMotionInfo	*fmotion;
{	
	int	dx;
	int	dy;

	dx = (XTestUnpackXMotionValue(fmotion->motion_data));
	dy = (XTestUnpackYMotionValue(fmotion->motion_data));
	if (((fmotion->header) & XTestX_SIGN_BIT_MASK) == XTestX_NEGATIVE)
	{
		pmousex -= dx;
	}
	else
	{
		pmousex += dx;
	}
	if (((fmotion->header) & XTestY_SIGN_BIT_MASK) == XTestY_NEGATIVE)
	{
		pmousey -= dy;
	}
	else 
	{
		pmousey += dy;
	}
	action_array[write_index].type = XTestJUMP_ACTION;
	action_array[write_index].device = XTestUnpackDeviceID(fmotion->header);
	action_array[write_index].x = pmousex;
	action_array[write_index].y = pmousey;
	action_array[write_index].delay_time = fmotion->delay_time;
	write_index++;
}

/******************************************************************************
 *
 *	parse_jump_fake
 *
 *	Called from parse_fake_input.
 *
 *	Copy the fake jump input action from its packed form into the array of
 *	pending input events.
 */
static void
parse_jump_fake(fjump)
XTestJumpInfo	*fjump;
{
	pmousex = fjump->jumpx;
	pmousey = fjump->jumpy;
	action_array[write_index].type = XTestJUMP_ACTION;
	action_array[write_index].device = XTestUnpackDeviceID(fjump->header);
	action_array[write_index].x = pmousex;
	action_array[write_index].y = pmousey;
	action_array[write_index].delay_time = fjump->delay_time;
	write_index++;
}

/******************************************************************************
 *
 *	parse_delay_fake
 *
 *	Called from parse_fake_input.
 *
 *	Copy the fake delay input action from its packed form into the array of
 *	pending input events.
 */
static void
parse_delay_fake(tevent)
XTestDelayInfo	*tevent;
{
	action_array[write_index].type = XTestDELAY_ACTION;
	action_array[write_index].delay_time = tevent->delay_time;
	write_index++;
}

/******************************************************************************
 *
 *	XTestComputeWaitTime
 *
 *	Compute the amount of time the server should wait before sending the
 *	next monitor event in playback mode.
 */
void
XTestComputeWaitTime(waittime)
struct timeval	*waittime;
{	
	/*
	 * The playback_on flag is set to 1 in parse_fake_input.  It is set to
	 * 0 in XTestProcessInputAction if the server has replayed all input
	 * actions.
	 */
	if (playback_on)
	{  
		if (!play_clock)
		{
			/*
			 * if the playback clock has never been set,
			 * then do it now
			 */
			start_play_clock();
		}
		/*
		 * We need to save the waittime the first time through.  This
		 * is a value the server uses, and we have to restore it when
		 * all of the input actions are processed by the server.
		 */
		if (!time_saved)
		{
			saved_sec = waittime->tv_sec;
			saved_usec = waittime->tv_usec; 
			time_saved = 1;
		}	
		if (go_for_next) 
		{
			/*
			 * if we just processed an input action, figure out
			 * how long to wait for the next input action
			 */
			compute_action_time(&rtime);
		}
		else  
		{
			/*
			 * else just find out how much more time to wait
			 * on the current input action
			 */
			(void)find_residual_time(&rtime);
		}
		waittime->tv_sec = rtime.tv_sec;
		waittime->tv_usec = rtime.tv_usec;
	}
}

/******************************************************************************
 *
 *	XTestProcessInputAction
 *
 *	If there are any input actions in the input action array,
 *	then take one out and process it.
 *
 */
int
XTestProcessInputAction(readable, waittime)
/*
 * This is the value that a 'select' function returned just before this
 * routine was called.  If the select timed out, this value will be 0.
 *
 * This extension modifies the select call's timeout value to cause the
 * select to time out when the next input action is ready to given to
 * the server.  This routine is called immediately after the select, to 
 * give it a chance to process an input action.  If we have an input action
 * to process and the only reason that the select returned was because it
 * timed out, then we change the select value to 1 and return 1 instead of 0.
 */
int		readable;
/*
 * this is the timeout value that the select was called with
 */
struct timeval	*waittime;
{	
int mousex, mousey;
	/*
	 * if playback_on is 0, then the input action array is empty
	 */
	if (playback_on)
	{ 
		restorewait = waittime;
		/*
		 * figure out if we need to wait for the next input action
		 */
		if (find_residual_time(&rtime) > 0) 
		{
			/*
			 * still have to wait before processing the current
			 * input action
			 */
			go_for_next = 0;
		}
		else 
		{
			/*
			 * don't have to wait any longer before processing
			 * the current input action
			 */
			go_for_next = 1;
		}
		/*
		 * if we have an input action to process and the only reason
		 * that the select returned was because it timed out, then we
		 * change the select value to 1 and return 1 instead of 0
		 */
		if (readable == 0) 
		{
			readable++;			
		}
		/*
		 * if we don't need to wait, then get an input action from
		 * the input action array and process it
		 */
		if (go_for_next)
		{  
			/*
			 * There are three possible types of input actions in
			 * the input action array (motion input actions are
			 * converted to jump input actions before being put
			 * into the input action array).  Delay input actions 
			 * are processed by the compute_action_time function
			 * which is called from XTestComputeWaitTime.  The
			 * other two types of input actions are processed here.
			 */
			if (action_array[read_index].type == XTestJUMP_ACTION)
			{	
				XTestJumpPointer(
					action_array[read_index].x, 
					action_array[read_index].y, 
					action_array[read_index].device);
			}
			if (action_array[read_index].type == XTestKEY_ACTION)
			    {
			    GetSpritePosition(&mousex, &mousey);
			    XTestGenerateEvent(
				     action_array[read_index].device, 
				     action_array[read_index].keycode, 
				     action_array[read_index].keystate,
				     mousex,
				     mousey);
			    }
			read_index++;
			/*
			 * if all input actions are processed, then restore 
			 * the server state 
			 */
			if (read_index >= write_index)
			{ 
				waittime->tv_sec = saved_sec;
				waittime->tv_usec = saved_usec;
				time_saved = 0;
				playback_on = 0;
				if (acknowledge) 
				{ 
					/*
					 * if the playback client is waiting
					 * for an xTestFakeAck event, send
					 * it to him
					 */
					send_ack(playback_client);		
					acknowledge = 0;
				}
				write_index = 0;
				read_index = 0;
				playback_client = (ClientPtr) NULL;
				play_clock = 0;
			}
		}
	}
	return(readable);
}

/******************************************************************************
 *
 *	send_ack
 *
 *	send an xTestFakeAck event to the client
 */
static void
send_ack(client)
ClientPtr	client;
{
	xTestFakeAckEvent  rep;

	/*
	 * set the serial number of the xTestFakeAck event
	 */
	rep.sequenceNumber = client->sequence;
	rep.type = XTestFakeAckType;
	WriteEventsToClient(client, 1, (xEvent *) &rep);		
}		

/******************************************************************************
 *
 *	start_play_clock
 *
 *	start the clock for play back.
 */
static void
start_play_clock()
{
	X_GETTIMEOFDAY(&play_time);
	/*
	 * flag that play_time is valid
	 */
	play_clock = 1;
}

/******************************************************************************
 *
 *	compute_action_time
 *
 *	Set the play clock to the time when the next input action should be put
 *	into the server's input queue.  Fill the rtime structure with values
 *	for the delta until the time for the next input action.
 */
static void
compute_action_time(rtime)
struct timeval	*rtime;
{
	/*
	 * holds the delay time in milliseconds
	 */
	unsigned long	dtime;
	/*
	 * holds the number of microseconds in the sum of the dtime value
	 * and the play_time value
	 */
	unsigned long	tot_usec;
	/*
	 * holds the number of seconds and microseconds in the
	 * dtime value
	 */
	unsigned long 	sec;
	unsigned long 	usec;
	/*
	 * holds the current time
	 */
	struct timeval	btime;

	/*
	 * Put the time from the current input action in dtime
	 */
	dtime = action_array[read_index].delay_time;
	/*
	 * If the current input action is a delay input action,
	 * add in the time from the following input action.
	 */
	if ((action_array[read_index].type == XTestDELAY_ACTION) &&
	    ((read_index + 1) < write_index))
	{  
		read_index++;
		dtime = dtime + action_array[read_index].delay_time;
	}
	/*
	 * compute the number of seconds and microseconds in the
	 * dtime value
	 */
  	sec = dtime / 1000;
  	usec = (dtime % 1000) * 1000;
	/*
	 * get the current time in btime
	 */
	X_GETTIMEOFDAY(&btime);
	/*
	 * compute the number of microseconds in the sum of the dtime value
	 * and the current usec value
	 */
	tot_usec = btime.tv_usec + usec;
	/*
	 * if it is greater than one second's worth, adjust the seconds
	 */
	if (tot_usec >= 1000000)
	{ 
		tot_usec -= 1000000;
		sec++;
	}
	play_time.tv_usec = tot_usec;
	play_time.tv_sec = btime.tv_sec + sec;
	/*
	 * put the time until the next input action in rtime
	 */
	rtime->tv_sec = sec;
	rtime->tv_usec = usec;
}

/******************************************************************************
 *
 *	find_residual_time
 *
 *	Find the time interval from the current time to the value in play_time.
 *	This is the time to wait till putting the next input action into the
 *	server's input queue.  If the time is already up, reset play_time to
 *	the current time.
 */
static int
find_residual_time(the_residual)
struct timeval	*the_residual;
{
	/*
	 * if > 0, there is time to wait.  If < 0, then don't wait
	 */
	int		wait = 1;
	/*
	 * holds the current time
	 */
	struct timeval	btime;
	/*
	 * holds the current time in seconds and microseconds
	 */
	unsigned long	bsec;
	unsigned long	busec;
	/*
	 * holds the playback time in seconds and microseconds
	 */
	unsigned long	psec;
	unsigned long	pusec;

	/*
	 * get the current time in btime
	 */
	X_GETTIMEOFDAY(&btime);
	/*
	 * get the current time in seconds and microseconds
	 */
	bsec = btime.tv_sec;
	busec = btime.tv_usec;
	/*
	 * get the playback time in seconds and microseconds
	 */
	psec = play_time.tv_sec;
	pusec = play_time.tv_usec;
	/*
	 * if the current time is already later than the playback time,
	 * we don't need to wait
	 */
	if (bsec > psec)	
	{
	    wait = -1;
	}
	else
	{ 
		if (bsec == psec)
		{ 
			/*
			 * if the current and playback times have the same
			 * second value, then compare the microsecond values
			 */
			if ( busec >= pusec) 
			{ 
				/*
				 * if the current time is already later than
				 * the playback time, we don't need to wait
				 */
				wait = -1;
			}
			else
			{ 
				the_residual->tv_usec = pusec - busec;
				the_residual->tv_sec = 0;
			}
		}
		else	
		{ 
			if (busec > pusec)
			{ 
				/*
				 * 'borrow' a second's worth of microseconds
				 * from the seconds left to wait
				 */
				the_residual->tv_usec = 1000000 - busec + pusec;
				psec--;
				the_residual->tv_sec = psec - bsec;
			}
			else
			{ 
				the_residual->tv_sec = psec - bsec;
				the_residual->tv_usec = pusec - busec;
			}
		}
	}
	if (wait < 0)
	{ 
		/*
		 * if don't need to wait, set the playback time
		 * to the current time
		 */
		X_GETTIMEOFDAY(&play_time);
		/*
		 * set the time to wait to 0
		 */
		the_residual->tv_sec = 0;
		the_residual->tv_usec = 0;
	}
	return(wait);
}
	
/******************************************************************************
 *
 *	abort_play_back
 */
void
abort_play_back()
{
	/*
	 * If we were playing back input actions at the time of the abort,
	 * restore the original wait time for the select in the main wait
	 * loop of the server
	 */
	if (playback_on)
	{
		restorewait->tv_sec = saved_sec;
		restorewait->tv_usec = saved_usec;
	}
	/*
	 * make the input action array empty
	 */
	read_index = 0;
	write_index = 0;
	/*
	 * we are no longer playing back anything
	 */
	playback_on = 0;
	play_clock = 0;
	go_for_next = 1;
	/*
	 * there is no valid wait time saved any more
	 */
	time_saved = 0;
	/*
	 * there are no valid clients using this extension
	 */
	playback_client = (ClientPtr) NULL;
	current_xtest_client = (ClientPtr) NULL;
}

/******************************************************************************
 *
 *	return_input_array_size
 *
 *	Return the number of input actions in the input action array.
 */
void
return_input_array_size(client)
/*
 * which client to send the reply to
 */
ClientPtr	client;
{
	xTestQueryInputSizeReply  rep;

	rep.type = X_Reply;
	/*
	 * set the serial number of the reply
	 */
	rep.sequenceNumber = client->sequence;
	rep.length = 0;
	rep.size_return = ACTION_ARRAY_SIZE;
	WriteReplyToClient(client,
			   sizeof(xTestQueryInputSizeReply),
			   (void *) &rep);
}