File: controls.c

package info (click to toggle)
gnurobbo 0.68%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,212 kB
  • sloc: ansic: 21,128; sh: 301; makefile: 185; awk: 98
file content (1712 lines) | stat: -rw-r--r-- 51,518 bytes parent folder | download | duplicates (4)
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
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
/*  GNU Robbo
 *  Copyright (C) 2002-2010 The GNU Robbo Team (see AUTHORS).
 *
 *  GNU Robbo is free software - you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  GNU Robbo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the impled warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU CC; see the file COPYING. If not, write to the
 *  Free Software Foundation, 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *
 */

#include "game.h"

/* Defines */
/*
#define DEBUG_CONTROLS
#define DEBUG_SDL_JOYAXISMOTION
#define DEBUG_SDL_ACTIVEEVENT
*/

#define JOYDEADZONE (32767 * joystick_dead_zone / 100)
#define MAX_JOY_AXES 20

/* GP2X button IDs */
#define GP2X_JOY_N 0x00
#define GP2X_JOY_NW 0x01
#define GP2X_JOY_W 0x02
#define GP2X_JOY_SW 0x03
#define GP2X_JOY_S 0x04
#define GP2X_JOY_SE 0x05
#define GP2X_JOY_E 0x06
#define GP2X_JOY_NE 0x07
#define GP2X_START 0x08
#define GP2X_SELECT 0x09
#define GP2X_LTRIG 0x0A
#define GP2X_RTRIG 0x0B
#define GP2X_BTN_A 0x0C
#define GP2X_BTN_B 0x0D
#define GP2X_BTN_X 0x0E
#define GP2X_BTN_Y 0x0F
#define GP2X_VOL_UP 0x10
#define GP2X_VOL_DN 0x11
#define GP2X_BTN_JOY 0x12

/* Zaurus button IDs */
#define ZAURUS_UP SDLK_UP
#define ZAURUS_DOWN SDLK_DOWN
#define ZAURUS_LEFT SDLK_LEFT
#define ZAURUS_RIGHT SDLK_RIGHT
#define ZAURUS_SPACE SDLK_SPACE
#define ZAURUS_CANCEL SDLK_ESCAPE
#define ZAURUS_OK 0
#define ZAURUS_CALENDAR SDLK_F9
#define ZAURUS_ADDRESS SDLK_F10
#define ZAURUS_HOME SDLK_F12
#define ZAURUS_MENU SDLK_F11
#define ZAURUS_EMAIL SDLK_F13

/* PSP button IDs */
#define PSP_TRIANGLE 0
#define PSP_CIRCLE 1
#define PSP_CROSS 2
#define PSP_SQUARE 3
#define PSP_LEFT_TRIGGER 4
#define PSP_RIGHT_TRIGGER 5
#define PSP_DOWN 6
#define PSP_LEFT 7
#define PSP_UP 8
#define PSP_RIGHT 9
#define PSP_SELECT 10
#define PSP_START 11
#define PSP_HOME 12
#define PSP_HOLD 13

/* Variables */
SDL_Event event;

/* Function prototypes */


/***************************************************************************
 * Get User Action v5.4                                                    *
 ***************************************************************************/
/* This reports whether an action has been triggered via user input.
 * Controls with modifiers take precedence over single key controls.
 * Controls must be unique although you can combine actions in your code.
 * Mixing of input devices and control reconfiguration is trivial.
 * user_controls holds all the controls. Each control has its pressed state
 * recorded so you won't need to do it yourself. It's also possible to
 * gain access to the device, id and state of individual events but in normal
 * operation you should only need to know the action triggered.
 * This function manages key/button repeat which can be different for each
 * control. If you don't want key/button repeat then set delay to -1.
 * It's designed to be called every game cycle because it counts in
 * game cycle units for key/button repeat calculation.
 * The event loop code in END SCREEN in main is almost empty and makes a good
 * template.
 * 
 * To debug this function, define one or all of the following :-
 * DEBUG_CONTROLS, DEBUG_SDL_ACTIVEEVENT, DEBUG_SDL_JOYAXISMOTION
 * 
 * UPDATE: Joystick axis support added. Axes are converted into additional
 * virtual buttons (two per axis) and require a dead zone to be set up which
 * should be user configurable. For example, if a joystick has 10 physical
 * buttons then axis0 will be virtual buttons 10 and 11, axis1 12 and 13 etc.
 * 
 * v5.1 UPDATE: pollall now breaks when an action is triggered to prevent the
 * possibility that an action is pressed and released before exiting (as
 * experienced on my Zaurus).
 * 
 * v5.2 UPDATE: Added mouse support. Initially I was intending to substitute
 * this version with another from an event/object based SDL engine I've been
 * working on but it won't work. In the end adding mouse/stylus support was
 * easier than I was expecting. I've added a ACTION_PRIMARY_CLICK action that
 * can be set-up for use with a mouse or stylus.
 * 
 * v5.3 UPDATE: Added hooks to the GNU Robbo Rectangular Object Engine (ROB).
 * ROB likes to be informed of released and triggered actions so that it can
 * generate events and manage the simulated pointer via the cursor events.
 * 
 * v5.4 UPDATE: Modified the code that deals with analogue axis events so
 * that they become remapped as SDL_JOYBUTTONUP/DOWN virtual button events.
 * 
 * struct control {
 *     int device;          Keyboard, joystick or mouse
 *     int id;              Key or button id
 *     int mod;             Modifier control
 *     int state;           Pressed or released
 *     int cyclesactive;    How many cycles this has been pressed for
 *     int delay;           The initial delay in cycles before repeating -
 *                              0 disables the delay
 *     int interval;        The repeat interval in cycles - 0 disables the interval
 * };
 * struct control user_controls[USER_CONTROLS];    Holds the assignment of user controls 
 * 
 * On entry: actionid will hold any actionid found
 *           pollall = TRUE to poll all events until an action is triggered
 *           pollall = FALSE to poll one event (used to retrieve the device,
 *               id and state for each polled event)
 *           device will hold the device of the last event or UNDEFINED (-1)
 *           id will hold the id of the last event or UNDEFINED
 *           state will hold the state of the last event or UNDEFINED
 *  On exit: actionid will contain an actionid or UNDEFINED
 *           returns 1 on SDL_QUIT else 0 */

int get_user_action (int *actionid, int pollall, int *device, int *id, int *state)
{
	static int axisstates[MAX_JOY_AXES * 2], init = FALSE;
	int found = FALSE, quitfound = FALSE;
	SDL_Event virtualevent;
	int axis_end = 0;
	int rob_actionid;
	int count;

	/* Do some first time variable initialisation */
	if (!init)
	{
		init = TRUE;
		for (count = 0; count < MAX_JOY_AXES; count++)
		{
			axisstates[count * 2] = axisstates[count * 2 + 1] = SDL_RELEASED;
		}
	}
	
	*actionid = UNDEFINED;

	/* Get and process some (not necessarily all) SDL events */
	while (SDL_PollEvent (&event))
	{
		*device = *id = *state = UNDEFINED;
		switch (event.type)
		{
			case SDL_KEYUP:
			case SDL_KEYDOWN:
				*device = DEVICE_KEYBOARD;
				*id = event.key.keysym.sym;
				*state = event.key.state;
				break;
			case SDL_JOYBUTTONUP:
			case SDL_JOYBUTTONDOWN:
				*device = DEVICE_JOYSTICK;
				*id = event.jbutton.button;
				*state = event.jbutton.state;
				break;
			case SDL_MOUSEBUTTONUP:
				lastclick=0;	/* Added by neurocyp for the designer */
			case SDL_MOUSEBUTTONDOWN:
				*device = DEVICE_MOUSE;
				*id = event.button.button;
				*state = event.button.state;
				break;
			case SDL_MOUSEMOTION:
				rob_lyr_pointer->x = event.motion.x;
				rob_lyr_pointer->y = event.motion.y;
				kmx = event.motion.x;	/* Added by neurocyp for the designer */
				kmy = event.motion.y;	/* Added by neurocyp for the designer */
				break;
			case SDL_QUIT:
				quitfound = TRUE;
				break;
			#ifdef DEBUG_SDL_ACTIVEEVENT
				case SDL_ACTIVEEVENT:
					printf ("%i: states: ", cycle_count);
					if (event.active.state & SDL_APPMOUSEFOCUS)
						printf ("SDL_APPMOUSEFOCUS ");
					if (event.active.state & SDL_APPINPUTFOCUS)
						printf ("SDL_APPINPUTFOCUS ");
					if (event.active.state & SDL_APPACTIVE)
						printf ("SDL_APPACTIVE ");
					printf (": gain=%i\n", event.active.gain);
					break;
			#endif
			case SDL_JOYAXISMOTION:
				/* The axis motion will be reported as a value from -32768 to +32767
				 * with 0 being the theoretical middle point. Some analogue joysticks
				 * are actually digital and offer limited analogue functionality by
				 * firing off a few values throughout this range (these joysticks will
				 * tend to rest at 0) whilst a truly analogue joystick will fire lots
				 * of different values with the middle firing anything but 0. The
				 * JOYDEADZONE offers us a reasonable user configurable middle region.
				 * 
				 * To simulate digital on/off button functionality from an analogue
				 * axis we need to record previous axis motion activity so that we can
				 * see if we've already generated an SDL_JOYBUTTONDOWN/UP event. This
				 * activity is stored within the axisstates array as pairs of values
				 * representing both ends of an axis, with each value belonging to a
				 * virtual button. The first integer records the up/down state of the
				 * negative end of an axis with the positive end stored in the other.
				 * 
				 * Following are the situations that this code will account for :-
				 *   SDL_JOYBUTTONDOWN - axis middle to any axis end
				 *   SDL_JOYBUTTONDOWN - one axis end to another axis end with the
				 *   opposite end generating SDL_JOYBUTTONUP
				 *   SDL_JOYBUTTONUP - any axis end to the middle */

				#ifdef DEBUG_SDL_JOYAXISMOTION
					printf ("%i: axis=%i value=%i\n", cycle_count,
						event.jaxis.axis, event.jaxis.value);
				#endif

				if (abs (event.jaxis.value) >= JOYDEADZONE)
				{
					/* Identify which end of the axis we are operating upon:
					 * 0 for -32768 to JOYDEADZONE, 1 for JOYDEADZONE to +32767 */
					axis_end = 0;
					if (event.jaxis.value >= JOYDEADZONE) axis_end = 1;

					if (axisstates[event.jaxis.axis * 2 + axis_end] == SDL_RELEASED)
					{
						/* Generate a virtual joystick button event */
						virtualevent.jbutton.button = event.jaxis.axis * 2 +
							axis_end + SDL_JoystickNumButtons (joystick);
						virtualevent.type = SDL_JOYBUTTONDOWN;
						virtualevent.jbutton.state = SDL_PRESSED;
						SDL_PushEvent(&virtualevent);

						/* Record its SDL_PRESSED state within axisstates */
						axisstates[event.jaxis.axis * 2 + axis_end] = SDL_PRESSED;
					}

					/* Do we need to release the opposite end of the axis? */
					if (axisstates[event.jaxis.axis * 2 + (1 - axis_end)] == SDL_PRESSED)
					{
						/* Generate a virtual joystick button event */
						virtualevent.jbutton.button = event.jaxis.axis * 2 +
							(1 - axis_end) + SDL_JoystickNumButtons (joystick);
						virtualevent.type = SDL_JOYBUTTONUP;
						virtualevent.jbutton.state = SDL_RELEASED;
						SDL_PushEvent(&virtualevent);

						/* Record its SDL_RELEASED state within axisstates */
						axisstates[event.jaxis.axis * 2 + (1 - axis_end)] = SDL_RELEASED;
					}
				}
				else
				{
					/* Do we need to release either end of the axis? */
					for (count = 0; count < 2; count++)
					{
						if (axisstates[event.jaxis.axis * 2 + count] == SDL_PRESSED)
						{
							/* Generate a virtual joystick button event */
							virtualevent.jbutton.button = event.jaxis.axis * 2 +
								count + SDL_JoystickNumButtons (joystick);
							virtualevent.type = SDL_JOYBUTTONUP;
							virtualevent.jbutton.state = SDL_RELEASED;
							SDL_PushEvent(&virtualevent);

							/* Record its SDL_RELEASED state within axisstates */
							axisstates[event.jaxis.axis * 2 + count] = SDL_RELEASED;
						}
					}
				}
				break;
			default:
				break;
		}

		found = FALSE;
		if (*device != UNDEFINED)
		{
			/* Iterate through user_controls and record the state for all matching controls */
			#ifdef DEBUG_CONTROLS
				printf ("device=%i; id=%03i; state=%i; all control states:", *device, *id, *state);
			#endif
			for (count = 0; count < USER_CONTROLS; count++)
			{
				if (user_controls[count].device == *device && user_controls[count].id == *id)
				{
					user_controls[count].state = *state;
					if (*state == SDL_RELEASED)
					{
						user_controls[count].cyclesactive = 0;
						/* ROB will want to know what action was released. Actually this
						 * isn't reporting action releases but control releases which is
						 * not the same thing. The reporting of action releases was not
						 * part of the requirements when designing this version of
						 * get_user_action() for GNU Robbo, but what is done here more
						 * than suffices */
						rob_actionid = count | 0x80;
						ROB_GenerateEvents (&rob_actionid);
					}
					found = TRUE;
				}
				#ifdef DEBUG_CONTROLS
					printf (" %i", user_controls[count].state);
				#endif
			}
			#ifdef DEBUG_CONTROLS
				printf ("\n");
			#endif

			if (found || !pollall) break;	/* Stop polling events now as we've got something to work with */
		}
	}

	/* Now we know what's pressed we can see if an action has been triggered.
	 * Controls with modifiers take precedence so search for these first */
	found = FALSE;
	for (count = 0; count < USER_CONTROLS; count++)
	{
		if (user_controls[count].state == SDL_PRESSED &&
			user_controls[count].mod != UNDEFINED &&
			user_controls[user_controls[count].mod].state == SDL_PRESSED)
		{
			found = TRUE;
			*actionid = count;
			#ifdef DEBUG_CONTROLS
				show_user_action (actionid);
			#endif
			break;
		}
	}
	if (!found)
	{
		/* Now search for controls without modifiers */
		for (count = 0; count < USER_CONTROLS; count++)
		{
			if (user_controls[count].state == SDL_PRESSED &&
				user_controls[count].mod == UNDEFINED)
			{
				found = TRUE;
				*actionid = count;
				#ifdef DEBUG_CONTROLS
					show_user_action (actionid);
				#endif
				break;
			}
		}
	}

	/* Because this function is called every game cycle, if the user
	 * is pressing a control then it would trigger an action every game
	 * cycle. cyclesactive, delay and interval are used to decide the
	 * frequency of individual key/button repeats */
	if (found)
	{
		if (user_controls[*actionid].cyclesactive == 0)
		{
			/* Allow this action */
		}
		else if (user_controls[*actionid].cyclesactive == user_controls[*actionid].delay)
		{
			/* Allow this action */
		}
		else if (user_controls[*actionid].cyclesactive > user_controls[*actionid].delay)
		{
			if (user_controls[*actionid].interval == 0)
			{
				/* Allow this action */
			}
			else if ((user_controls[*actionid].cyclesactive -
				user_controls[*actionid].delay) % user_controls[*actionid].interval == 0)
			{
				/* Allow this action */
			}
			else
			{
				found = FALSE;
			}
		}
		else
		{
			found = FALSE;
		}
		user_controls[*actionid].cyclesactive++;
		if (!found) *actionid = UNDEFINED;
	}

	/* ROB will want to know what action was triggered */
	ROB_GenerateEvents (actionid);

	return quitfound;
}

/***************************************************************************
 * Show User Action                                                        *
 ***************************************************************************/
/* Used for debugging */

void show_user_action (int *actionid)
{
	switch (*actionid)
	{
		case ACTION_UP:
			printf ("ACTION_UP");
			break;
		case ACTION_UP_RIGHT:
			printf ("ACTION_UP_RIGHT");
			break;
		case ACTION_RIGHT:
			printf ("ACTION_RIGHT");
			break;
		case ACTION_DOWN_RIGHT:
			printf ("ACTION_DOWN_RIGHT");
			break;
		case ACTION_DOWN:
			printf ("ACTION_DOWN");
			break;
		case ACTION_DOWN_LEFT:
			printf ("ACTION_DOWN_LEFT");
			break;
		case ACTION_LEFT:
			printf ("ACTION_LEFT");
			break;
		case ACTION_UP_LEFT:
			printf ("ACTION_UP_LEFT");
			break;
		case ACTION_SHOOT_UP:
			printf ("ACTION_SHOOT_UP");
			break;
		case ACTION_SHOOT_DOWN:
			printf ("ACTION_SHOOT_DOWN");
			break;
		case ACTION_SHOOT_LEFT:
			printf ("ACTION_SHOOT_LEFT");
			break;
		case ACTION_SHOOT_RIGHT:
			printf ("ACTION_SHOOT_RIGHT");
			break;
		case ACTION_SELECT:
			printf ("ACTION_SELECT");
			break;
		case ACTION_EXIT:
			printf ("ACTION_EXIT");
			break;
		case ACTION_HELP:
			printf ("ACTION_HELP");
			break;
		case ACTION_OPTIONS:
			printf ("ACTION_OPTIONS");
			break;
		case ACTION_RESTART:
			printf ("ACTION_RESTART");
			break;
		case ACTION_PREVIOUS_LEVEL:
			printf ("ACTION_PREVIOUS_LEVEL");
			break;
		case ACTION_NEXT_LEVEL:
			printf ("ACTION_NEXT_LEVEL");
			break;
		case ACTION_PREVIOUS_PACK:
			printf ("ACTION_PREVIOUS_PACK");
			break;
		case ACTION_NEXT_PACK:
			printf ("ACTION_NEXT_PACK");
			break;
		case ACTION_TOGGLE_FULLSCREEN:
			printf ("ACTION_TOGGLE_FULLSCREEN");
			break;
		case ACTION_HOME:
			printf ("ACTION_HOME");
			break;
		case ACTION_END:
			printf ("ACTION_END");
			break;
		case ACTION_PAGEUP:
			printf ("ACTION_PAGEUP");
			break;
		case ACTION_PAGEDOWN:
			printf ("ACTION_PAGEDOWN");
			break;
		case ACTION_TOGGLE_DESIGNER:
			printf ("ACTION_TOGGLE_DESIGNER");
			break;
		case ACTION_VOLUP:
			printf ("ACTION_VOLUP");
			break;
		case ACTION_VOLDOWN:
			printf ("ACTION_VOLDOWN");
			break;
		case ACTION_MODIFIER1:
			printf ("ACTION_MODIFIER1");
			break;
		case ACTION_MODIFIER2:
			printf ("ACTION_MODIFIER2");
			break;
		case ACTION_MODIFIER3:
			printf ("ACTION_MODIFIER3");
			break;
		case ACTION_MODIFIER4:
			printf ("ACTION_MODIFIER4");
			break;
		case ACTION_SCROLL_UP:
			printf ("ACTION_SCROLL_UP");
			break;
		case ACTION_SCROLL_DOWN:
			printf ("ACTION_SCROLL_DOWN");
			break;
		case ACTION_PRIMARY_CLICK:
			printf ("ACTION_PRIMARY_CLICK");
			break;
		case ACTION_NOT_USED1:
			printf ("ACTION_NOT_USED1");
			break;
		case UNDEFINED:
			printf ("UNDEFINED");
			break;
		default:
			printf ("Unknown action");
			break;
	}
	printf (": cyclesactive=%i\n", user_controls[*actionid].cyclesactive);
}

/***************************************************************************
 * Set Default User Controls                                               *
 ***************************************************************************/

void set_default_user_controls (struct control user_controls[USER_CONTROLS])
{
	int count;

	/* Set some defaults */
	for (count = 0; count < USER_CONTROLS; count++)
	{
		#if defined(PLATFORM_WIN32) || defined(PLATFORM_PC)
			user_controls[count].device = DEVICE_KEYBOARD;
		#elif defined(PLATFORM_GP2X)  || defined(PLATFORM_CAANOO)
			user_controls[count].device = DEVICE_JOYSTICK;
		#elif defined(PLATFORM_ZAURUS)
			user_controls[count].device = DEVICE_KEYBOARD;
		#elif defined(PLATFORM_FREMANTLE)
			user_controls[count].device = DEVICE_KEYBOARD;
		#elif defined(PLATFORM_PSP)
			user_controls[count].device = DEVICE_JOYSTICK;
		#endif
		user_controls[count].id = UNDEFINED;
		user_controls[count].mod = UNDEFINED;
		user_controls[count].state = SDL_RELEASED;
		user_controls[count].cyclesactive = 0;
		user_controls[count].delay = KEY_REPEAT_DELAY;
		user_controls[count].interval = KEY_REPEAT_INTERVAL;
	}

	/* Now tune to a platform. NOTE that so far device is the only property
	 * that has been initialised to something (keyboard or joystick) so if
	 * you don't want to use a control then just set its device to UNDEFINED
	 * (id and mod are already set to UNDEFINED from the above code) otherwise
	 * within Options you'll see the device shown on its own which looks weird */
	#if defined(PLATFORM_WIN32) || defined(PLATFORM_PC)
		user_controls[ACTION_UP].id = SDLK_UP;
		user_controls[ACTION_UP_RIGHT].device = UNDEFINED;
		user_controls[ACTION_RIGHT].id = SDLK_RIGHT;
		user_controls[ACTION_DOWN_RIGHT].device = UNDEFINED;
		user_controls[ACTION_DOWN].id = SDLK_DOWN;
		user_controls[ACTION_DOWN_LEFT].device = UNDEFINED;
		user_controls[ACTION_LEFT].id = SDLK_LEFT;
		user_controls[ACTION_UP_LEFT].device = UNDEFINED;
		user_controls[ACTION_RESTART].id = SDLK_r;
		user_controls[ACTION_SHOOT_UP].id = SDLK_UP;
		user_controls[ACTION_SHOOT_UP].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SHOOT_DOWN].id = SDLK_DOWN;
		user_controls[ACTION_SHOOT_DOWN].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SHOOT_LEFT].id = SDLK_LEFT;
		user_controls[ACTION_SHOOT_LEFT].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SHOOT_RIGHT].id = SDLK_RIGHT;
		user_controls[ACTION_SHOOT_RIGHT].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SELECT].id = SDLK_RETURN;
		user_controls[ACTION_EXIT].id = SDLK_ESCAPE;
		user_controls[ACTION_HELP].id = SDLK_F1;
		user_controls[ACTION_OPTIONS].id = SDLK_F4;
		user_controls[ACTION_TOGGLE_FULLSCREEN].id = SDLK_RETURN;
		user_controls[ACTION_TOGGLE_FULLSCREEN].mod = ACTION_MODIFIER1;
		user_controls[ACTION_PREVIOUS_LEVEL].id = SDLK_F5;
		user_controls[ACTION_NEXT_LEVEL].id = SDLK_F6;
		user_controls[ACTION_PREVIOUS_PACK].id = SDLK_F7;
		user_controls[ACTION_NEXT_PACK].id = SDLK_F8;
		user_controls[ACTION_HOME].id = SDLK_HOME;
		user_controls[ACTION_END].id = SDLK_END;
		user_controls[ACTION_PAGEUP].id = SDLK_PAGEUP;
		user_controls[ACTION_PAGEDOWN].id = SDLK_PAGEDOWN;
		user_controls[ACTION_TOGGLE_DESIGNER].id = SDLK_F9;
		user_controls[ACTION_VOLUP].id = SDLK_EQUALS;
		user_controls[ACTION_VOLDOWN].id = SDLK_MINUS;
		user_controls[ACTION_MODIFIER1].id = SDLK_LALT;
		user_controls[ACTION_MODIFIER2].id = SDLK_RSHIFT;
		user_controls[ACTION_MODIFIER3].device = UNDEFINED;
		user_controls[ACTION_MODIFIER4].device = UNDEFINED;
		user_controls[ACTION_SCROLL_UP].device = DEVICE_MOUSE;
		user_controls[ACTION_SCROLL_UP].id = SDL_BUTTON_WHEELUP;
		user_controls[ACTION_SCROLL_DOWN].device = DEVICE_MOUSE;
		user_controls[ACTION_SCROLL_DOWN].id = SDL_BUTTON_WHEELDOWN;
		user_controls[ACTION_PRIMARY_CLICK].device = DEVICE_MOUSE;
		user_controls[ACTION_PRIMARY_CLICK].id = SDL_BUTTON_LEFT;
		user_controls[ACTION_NOT_USED1].device = UNDEFINED;
	#elif defined(PLATFORM_GP2X)  || defined(PLATFORM_CAANOO)
		user_controls[ACTION_UP].id = GP2X_JOY_N;
		user_controls[ACTION_UP_RIGHT].device = UNDEFINED;
		user_controls[ACTION_RIGHT].id = GP2X_JOY_E;
		user_controls[ACTION_DOWN_RIGHT].device = UNDEFINED;
		user_controls[ACTION_DOWN].id = GP2X_JOY_S;
		user_controls[ACTION_DOWN_LEFT].device = UNDEFINED;
		user_controls[ACTION_LEFT].id = GP2X_JOY_W;
		user_controls[ACTION_UP_LEFT].device = UNDEFINED;
		user_controls[ACTION_RESTART].id = GP2X_SELECT;
		user_controls[ACTION_SHOOT_UP].id = GP2X_JOY_N;
		user_controls[ACTION_SHOOT_UP].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_DOWN].id = GP2X_JOY_S;
		user_controls[ACTION_SHOOT_DOWN].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_LEFT].id = GP2X_JOY_W;
		user_controls[ACTION_SHOOT_LEFT].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_RIGHT].id = GP2X_JOY_E;
		user_controls[ACTION_SHOOT_RIGHT].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SELECT].id = GP2X_BTN_B;
		user_controls[ACTION_EXIT].id = GP2X_BTN_X;
		user_controls[ACTION_HELP].id = GP2X_BTN_Y;
		user_controls[ACTION_HELP].mod = ACTION_MODIFIER1;
		user_controls[ACTION_OPTIONS].id = GP2X_START;
		user_controls[ACTION_TOGGLE_FULLSCREEN].device = UNDEFINED;
		user_controls[ACTION_PREVIOUS_LEVEL].id = GP2X_LTRIG;
		user_controls[ACTION_PREVIOUS_LEVEL].mod = ACTION_MODIFIER1;
		user_controls[ACTION_NEXT_LEVEL].id = GP2X_RTRIG;
		user_controls[ACTION_NEXT_LEVEL].mod = ACTION_MODIFIER1;
		user_controls[ACTION_PREVIOUS_PACK].id = GP2X_LTRIG;
		user_controls[ACTION_PREVIOUS_PACK].mod = ACTION_MODIFIER2;
		user_controls[ACTION_NEXT_PACK].id = GP2X_RTRIG;
		user_controls[ACTION_NEXT_PACK].mod = ACTION_MODIFIER2;
		user_controls[ACTION_HOME].id = GP2X_VOL_DN;
		user_controls[ACTION_HOME].mod = ACTION_MODIFIER1;
		user_controls[ACTION_END].id = GP2X_VOL_UP;
		user_controls[ACTION_END].mod = ACTION_MODIFIER1;
		user_controls[ACTION_PAGEUP].id = GP2X_LTRIG;
		user_controls[ACTION_PAGEDOWN].id = GP2X_RTRIG;
		user_controls[ACTION_TOGGLE_DESIGNER].device = UNDEFINED;
		user_controls[ACTION_VOLUP].id = GP2X_VOL_UP;
		user_controls[ACTION_VOLDOWN].id = GP2X_VOL_DN;
		user_controls[ACTION_MODIFIER1].id = GP2X_BTN_A;
		user_controls[ACTION_MODIFIER2].id = GP2X_BTN_Y;
		user_controls[ACTION_MODIFIER3].device = UNDEFINED;
		user_controls[ACTION_MODIFIER4].device = UNDEFINED;
		user_controls[ACTION_SCROLL_UP].device = UNDEFINED;
		user_controls[ACTION_SCROLL_DOWN].device = UNDEFINED;
		#ifdef HAVE_DESIGNER
			/* F100 doesn't support the mouse, F200 does (or can) so
			 * what happens here depends on HAVE_DESIGNER in the Makefile */
			user_controls[ACTION_PRIMARY_CLICK].device = DEVICE_MOUSE;
			user_controls[ACTION_PRIMARY_CLICK].id = SDL_BUTTON_LEFT;
		#else
			user_controls[ACTION_PRIMARY_CLICK].device = UNDEFINED;
		#endif
		user_controls[ACTION_NOT_USED1].device = UNDEFINED;
	#elif defined(PLATFORM_ZAURUS)
		user_controls[ACTION_UP].id = ZAURUS_UP;
		user_controls[ACTION_UP_RIGHT].device = UNDEFINED;
		user_controls[ACTION_RIGHT].id = ZAURUS_RIGHT;
		user_controls[ACTION_DOWN_RIGHT].device = UNDEFINED;
		user_controls[ACTION_DOWN].id = ZAURUS_DOWN;
		user_controls[ACTION_DOWN_LEFT].device = UNDEFINED;
		user_controls[ACTION_LEFT].id = ZAURUS_LEFT;
		user_controls[ACTION_UP_LEFT].device = UNDEFINED;
		user_controls[ACTION_RESTART].id = ZAURUS_HOME;
		user_controls[ACTION_SHOOT_UP].id = ZAURUS_UP;
		user_controls[ACTION_SHOOT_UP].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_DOWN].id = ZAURUS_DOWN;
		user_controls[ACTION_SHOOT_DOWN].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_LEFT].id = ZAURUS_LEFT;
		user_controls[ACTION_SHOOT_LEFT].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_RIGHT].id = ZAURUS_RIGHT;
		user_controls[ACTION_SHOOT_RIGHT].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SELECT].id = ZAURUS_SPACE;
		user_controls[ACTION_EXIT].id = ZAURUS_CANCEL;
		user_controls[ACTION_HELP].id = ZAURUS_MENU;
		user_controls[ACTION_OPTIONS].id = ZAURUS_EMAIL;
		user_controls[ACTION_TOGGLE_FULLSCREEN].device = UNDEFINED;
		user_controls[ACTION_PREVIOUS_LEVEL].id = ZAURUS_CALENDAR;
		user_controls[ACTION_PREVIOUS_LEVEL].mod = ACTION_MODIFIER1;
		user_controls[ACTION_NEXT_LEVEL].id = ZAURUS_ADDRESS;
		user_controls[ACTION_NEXT_LEVEL].mod = ACTION_MODIFIER1;
		user_controls[ACTION_PREVIOUS_PACK].device = UNDEFINED;
		user_controls[ACTION_NEXT_PACK].device = UNDEFINED;
		user_controls[ACTION_HOME].device = UNDEFINED;
		user_controls[ACTION_END].device = UNDEFINED;
		user_controls[ACTION_PAGEUP].id = ZAURUS_CALENDAR;
		user_controls[ACTION_PAGEDOWN].id = ZAURUS_ADDRESS;
		user_controls[ACTION_TOGGLE_DESIGNER].device = UNDEFINED;
		user_controls[ACTION_VOLUP].device = UNDEFINED;
		user_controls[ACTION_VOLDOWN].device = UNDEFINED;
		user_controls[ACTION_MODIFIER1].id = ZAURUS_SPACE;
		user_controls[ACTION_MODIFIER2].device = UNDEFINED;
		user_controls[ACTION_MODIFIER3].device = UNDEFINED;
		user_controls[ACTION_MODIFIER4].device = UNDEFINED;
		user_controls[ACTION_SCROLL_UP].device = UNDEFINED;
		user_controls[ACTION_SCROLL_DOWN].device = UNDEFINED;
		user_controls[ACTION_PRIMARY_CLICK].device = DEVICE_MOUSE;
		user_controls[ACTION_PRIMARY_CLICK].id = SDL_BUTTON_LEFT;
		user_controls[ACTION_NOT_USED1].device = UNDEFINED;
	#elif defined(PLATFORM_FREMANTLE)
		user_controls[ACTION_UP].id = SDLK_UP;
		user_controls[ACTION_UP_RIGHT].device = UNDEFINED;
		user_controls[ACTION_RIGHT].id = SDLK_RIGHT;
		user_controls[ACTION_DOWN_RIGHT].device = UNDEFINED;
		user_controls[ACTION_DOWN].id = SDLK_DOWN;
		user_controls[ACTION_DOWN_LEFT].device = UNDEFINED;
		user_controls[ACTION_LEFT].id = SDLK_LEFT;
		user_controls[ACTION_UP_LEFT].device = UNDEFINED;
		user_controls[ACTION_RESTART].id = SDLK_r;
		user_controls[ACTION_SHOOT_UP].id = SDLK_UP;
		user_controls[ACTION_SHOOT_UP].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SHOOT_DOWN].id = SDLK_DOWN;
		user_controls[ACTION_SHOOT_DOWN].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SHOOT_LEFT].id = SDLK_LEFT;
		user_controls[ACTION_SHOOT_LEFT].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SHOOT_RIGHT].id = SDLK_RIGHT;
		user_controls[ACTION_SHOOT_RIGHT].mod = ACTION_MODIFIER2;
		user_controls[ACTION_SELECT].id = SDLK_KP_ENTER;
		user_controls[ACTION_EXIT].id = SDLK_BACKSPACE;
		user_controls[ACTION_HELP].id = SDLK_h;
		user_controls[ACTION_OPTIONS].id = SDLK_o;
		user_controls[ACTION_TOGGLE_FULLSCREEN].id = SDLK_RETURN;
		user_controls[ACTION_TOGGLE_FULLSCREEN].mod = ACTION_MODIFIER1;
		user_controls[ACTION_PREVIOUS_LEVEL].id = SDLK_F5;
		user_controls[ACTION_NEXT_LEVEL].id = SDLK_F6;
		user_controls[ACTION_PREVIOUS_PACK].id = SDLK_F7;
		user_controls[ACTION_NEXT_PACK].id = SDLK_F8;
		user_controls[ACTION_HOME].id = SDLK_HOME;
		user_controls[ACTION_END].id = SDLK_END;
		user_controls[ACTION_PAGEUP].id = SDLK_PAGEUP;
		user_controls[ACTION_PAGEDOWN].id = SDLK_PAGEDOWN;
		user_controls[ACTION_TOGGLE_DESIGNER].id = SDLK_F9;
		user_controls[ACTION_VOLUP].id = SDLK_EQUALS;
		user_controls[ACTION_VOLDOWN].id = SDLK_MINUS;
		user_controls[ACTION_MODIFIER1].id = SDLK_MODE;
		user_controls[ACTION_MODIFIER2].id = SDLK_LSHIFT;
		user_controls[ACTION_MODIFIER3].device = UNDEFINED;
		user_controls[ACTION_MODIFIER4].device = UNDEFINED;
		user_controls[ACTION_SCROLL_UP].device = DEVICE_MOUSE;
		user_controls[ACTION_SCROLL_UP].id = SDL_BUTTON_WHEELUP;
		user_controls[ACTION_SCROLL_DOWN].device = DEVICE_MOUSE;
		user_controls[ACTION_SCROLL_DOWN].id = SDL_BUTTON_WHEELDOWN;
		user_controls[ACTION_PRIMARY_CLICK].device = DEVICE_MOUSE;
		user_controls[ACTION_PRIMARY_CLICK].id = SDL_BUTTON_LEFT;
		user_controls[ACTION_NOT_USED1].device = UNDEFINED;
	#elif defined(PLATFORM_PSP)
		user_controls[ACTION_UP].id = PSP_UP;
		user_controls[ACTION_UP_RIGHT].device = UNDEFINED;
		user_controls[ACTION_RIGHT].id = PSP_RIGHT;
		user_controls[ACTION_DOWN_RIGHT].device = UNDEFINED;
		user_controls[ACTION_DOWN].id = PSP_DOWN;
		user_controls[ACTION_DOWN_LEFT].device = UNDEFINED;
		user_controls[ACTION_LEFT].id = PSP_LEFT;
		user_controls[ACTION_UP_LEFT].device = UNDEFINED;
		user_controls[ACTION_RESTART].id = PSP_START;
		user_controls[ACTION_SHOOT_UP].id = PSP_UP;
		user_controls[ACTION_SHOOT_UP].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_DOWN].id = PSP_DOWN;
		user_controls[ACTION_SHOOT_DOWN].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_LEFT].id = PSP_LEFT;
		user_controls[ACTION_SHOOT_LEFT].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SHOOT_RIGHT].id = PSP_RIGHT;
		user_controls[ACTION_SHOOT_RIGHT].mod = ACTION_MODIFIER1;
		user_controls[ACTION_SELECT].id = PSP_CROSS;
		user_controls[ACTION_EXIT].id = PSP_SELECT;
		user_controls[ACTION_HELP].device = UNDEFINED;
		user_controls[ACTION_OPTIONS].device = UNDEFINED;
		user_controls[ACTION_TOGGLE_FULLSCREEN].device = UNDEFINED;
		user_controls[ACTION_PREVIOUS_LEVEL].id = PSP_LEFT_TRIGGER;
		user_controls[ACTION_NEXT_LEVEL].id = PSP_RIGHT_TRIGGER;
		user_controls[ACTION_PREVIOUS_PACK].device = UNDEFINED;
		user_controls[ACTION_NEXT_PACK].device = UNDEFINED;
		user_controls[ACTION_HOME].device = UNDEFINED;
		user_controls[ACTION_END].device = UNDEFINED;
		user_controls[ACTION_PAGEUP].device = UNDEFINED;
		user_controls[ACTION_PAGEDOWN].device = UNDEFINED;
		user_controls[ACTION_TOGGLE_DESIGNER].device = UNDEFINED;
		user_controls[ACTION_VOLUP].device = UNDEFINED;
		user_controls[ACTION_VOLDOWN].device = UNDEFINED;
		user_controls[ACTION_MODIFIER1].id = PSP_CROSS;
		user_controls[ACTION_MODIFIER2].device = UNDEFINED;
		user_controls[ACTION_MODIFIER3].device = UNDEFINED;
		user_controls[ACTION_MODIFIER4].device = UNDEFINED;
		user_controls[ACTION_SCROLL_UP].device = UNDEFINED;
		user_controls[ACTION_SCROLL_DOWN].device = UNDEFINED;
		user_controls[ACTION_PRIMARY_CLICK].device = UNDEFINED;
		user_controls[ACTION_NOT_USED1].device = UNDEFINED;
	#endif
}

/***************************************************************************
 * Set Key Repeat                                                          *
 ***************************************************************************/
/* Enable or disable keyboard key repeat.
 * 
 * On entry: delay = the delay before repeating or 0 to disable
 *           interval = the repeat interval
 *  On exit: returns 1 on error else 0 */

int set_key_repeat (int delay, int interval)
{
	if (SDL_EnableKeyRepeat (delay, interval) != 0)
	{
		fprintf (stdout, "Error setting key repeat: %s\n", SDL_GetError ());
		return 1;
	}

	return 0;
}

/***************************************************************************
 * Initialise Joystick                                                     *
 ***************************************************************************/
/* Enable a joystick for input. It's designed so that it's possible to restore
 * a saved joyid and joyname from an rc file and attempt to match them up
 * taking into account somebody might have two joysticks that may have been
 * switched.
 * 
 * On entry: joyid = UNDEFINED (-1) for any joystick with joyname irrelevant
 *           joyid = 0 to n for a specific joystick by joyid only (joyname = "-1")
 *           joyid = 0 to n for a specific joystick by joyid and/or joyname with
 *               joyname taking priority (joyname != "-1")
 *           show = TRUE to dump output to the terminal
 *  On exit: returns the joyid if successful (zero based) with joyname = joystick name
 *           returns UNDEFINED (-1) if unsuccessful with joyname = "-1" */

int initialise_joystick (int joyid, char *joyname, int show)
{
	int count, number_found, found = FALSE;

	number_found = SDL_NumJoysticks ();	/* Get number of joysticks attached */

	/* Are any joysticks available? */
	if (number_found > 0)
	{
		/* WARNING: attempting to close joystick 0 on the GP2X
		 * using the GPH SDK causes a seg fault */
		#if defined(PLATFORM_WIN32) || defined(PLATFORM_PC)
			if (joystick)
			{
		#elif defined(PLATFORM_GP2X)  || defined(PLATFORM_CAANOO)
			if ((joystick) && ((SDL_JoystickIndex (joystick)) > 0))
			{
		#elif defined(PLATFORM_ZAURUS)
			if (joystick)
			{
		#elif defined(PLATFORM_FREMANTLE)
			if (joystick)
			{
		#elif defined(PLATFORM_PSP)
			if (joystick)
			{
		#endif
			if (show) fprintf (stdout, "Joystick closed: %i:%s\n",
				SDL_JoystickIndex (joystick),
				SDL_JoystickName (SDL_JoystickIndex (joystick)));
			SDL_JoystickClose (joystick);
		}

		/* Enable joystick event processing */
		SDL_JoystickEventState (SDL_ENABLE);

		/* Enable any joystick? */
		if (joyid == UNDEFINED)
		{
			for (count = 0; count < number_found; count++)
			{
				if ((joystick = SDL_JoystickOpen (count)))
				{
					if (show) fprintf (stdout, "Joystick opened: %i:%s\n",
						count, SDL_JoystickName (count));
					joyid = count;
					found = TRUE;
					break;
				}
				else
				{
				if (show) fprintf (stdout, "Couldn't open joystick %i:%s!\n",
					count, SDL_JoystickName (count));
				}
			}
			if (!found)
			{
				if (show) fprintf (stdout, "Couldn't open any of %i joystick(s)!\n",
					number_found);
				joyid = UNDEFINED;
			}

		}
		else if (!strcmp (joyname, "-1"))	/* Enable a joystick by joyid only? */
		{
			if ((joystick = SDL_JoystickOpen (joyid)))
			{
				if (show) fprintf (stdout, "Joystick opened: %i:%s\n",
					joyid, SDL_JoystickName (joyid));
			}
			else
			{
				if (show) fprintf (stdout, "Couldn't open joystick %i\n", joyid);
				joyid = UNDEFINED;
			}

		}
		else	/* Enable a joystick by joyid and/or joyname */
		{
			/* Firstly, attempt to find an exact match on joyid and joyname */
			for (count = 0; count < number_found; count++)
			{
				if ((joystick = SDL_JoystickOpen (count)))
				{
					if (joyid == count && !strcmp (joyname, SDL_JoystickName (count)))
					{
						if (show) fprintf (stdout, "Joystick opened: %i:%s\n",
							count, SDL_JoystickName (count));
						joyid = count;
						found = TRUE;
						break;
					}
				}
			}
			if (!found)
			{
				/* Exact match not found so search for joyname only */
				for (count = 0; count < number_found; count++)
				{
					if ((joystick = SDL_JoystickOpen (count)))
					{
						if (!strcmp (joyname, SDL_JoystickName (count)))
						{
							if (show) fprintf (stdout, "Joystick opened: %i:%s\n",
								count, SDL_JoystickName (count));
							joyid = count;
							found = TRUE;
							break;
						}
					}
				}
				if (!found)
				{
					if (show) fprintf (stdout, "Couldn't find joystick %i:%s\n",
						joyid, joyname);
					joyid = UNDEFINED;
				}
			}
		}
	}
	else
	{
		if (show) fprintf (stdout, "There is no joystick to initialise\n");
		joyid = UNDEFINED;
	}

	/* Flush events as opening a joystick releases all the buttons */
	if (joystick) while (SDL_PollEvent (&event));

	get_joystick_name (joyid, joyname);
	return joyid;
}

/***************************************************************************
 * Get Joystick Name                                                       *
 ***************************************************************************/
/* Gets a joystick name that isn't NULL ;) It's also truncated to
 * MAX_JOYSTICK_NAME_LENGTH which is currently 256 characters.
 * 
 * On entry: joyid = 0 to n for a specific joystick
 *  On exit: returns 0 if successful with joyname = joystick name
 *           returns 1 on error with joyname = "-1" */

int get_joystick_name (int joyid, char *joyname)
{
	if (SDL_JoystickName (joyid) != NULL)
	{
		strncpy (joyname, SDL_JoystickName (joyid), MAX_JOYSTICK_NAME_LENGTH);
		/* Force string null termination if max characters were copied */
		joyname[MAX_JOYSTICK_NAME_LENGTH - 1] = 0;
		return 0;
	}
	else
	{
		strcpy (joyname, "-1");
		return 1;
	}
}

/***************************************************************************
 * Get Joystick List                                                       *
 ***************************************************************************/
/* Get a list of attached joysticks if any. I have found that SDL does not
 * update the list if you subsequently plug or unplug a joystick.
 * 
 * On entry: joystick_list is an array of strings
 *           show = TRUE to dump output to the terminal
 *  On exit: returns number of joysticks found */

int get_joystick_list (char joystick_list[MAX_JOYSTICKS][MAX_JOYSTICK_NAME_LENGTH],
	int show)
{
	int count, number_found;

	for (count = 0; count < MAX_JOYSTICKS; count++)
		strcpy (joystick_list[count], "");

	number_found = SDL_NumJoysticks ();	/* Get number of joysticks attached */
	if (number_found > MAX_JOYSTICKS) number_found = MAX_JOYSTICKS;

	if (number_found)
	{
		SDL_JoystickEventState (SDL_ENABLE);	/* Enable joystick event processing */

		/* Initialise joystick_list */
		for (count = 0; count < number_found; count++)
		{
			get_joystick_name (count, joystick_list[count]);
			if (show) fprintf (stdout, "Joystick found: %i:%s\n",
				count, joystick_list[count]);
		}
	}
	else
	{
		if (show) fprintf (stdout, "No joystick found\n");
	}

	#ifdef DEBUG_CONTROLS
		strcpy (joystick_list[number_found++], "Micro Shaft Pretend Joystick");
		strcpy (joystick_list[number_found++], "Grevious Gamepad Pro");
		strcpy (joystick_list[number_found++], "Cheapo Joypad");
	#endif

	return number_found;
}

/***************************************************************************
 * Get Input Device Text                                                   *
 ***************************************************************************/
/* Translates an SDLKey value into a string for display in Options.
 * 
 * On entry: device = input device
 *           id = keysym.sym
 *           joyname = a string containing a possible joystick name
 *           text = the return string
 *  On exit: text = either a key/button legend or the id in text */

void get_input_device_text (int device, int id, char *joyname, char *text)
{
	if (device == DEVICE_MOUSE)
	{
		switch (id)
		{
			case SDL_BUTTON_LEFT:
				strcpy (text, txt_ptr_BtnLeft);
				break;
			case SDL_BUTTON_MIDDLE:
				strcpy (text, txt_ptr_BtnMiddle);
				break;
			case SDL_BUTTON_RIGHT:
				strcpy (text, txt_ptr_BtnRight);
				break;
			case SDL_BUTTON_WHEELUP:
				strcpy (text, txt_ptr_WheelUp);
				break;
			case SDL_BUTTON_WHEELDOWN:
				strcpy (text, txt_ptr_WheelDown);
				break;
			default:
				sprintf (text, "%i", id);
				break;
		}
	}
	else if (device == DEVICE_KEYBOARD)
	{
		switch (id)
		{
			case SDLK_UNKNOWN:
				strcpy (text, "SDLUnknown");
				break;
			case SDLK_BACKSPACE:
				strcpy (text, txt_key_Backspace);
				break;
			case SDLK_TAB:
				strcpy (text, txt_key_Tab);
				break;
			case SDLK_CLEAR:
				strcpy (text, txt_key_Clear);
				break;
			case SDLK_RETURN:
				strcpy (text, txt_key_Return);
				break;
			case SDLK_PAUSE:
				strcpy (text, txt_key_Pause);
				break;
			case SDLK_ESCAPE:
				strcpy (text, txt_key_Escape);
				break;
			case SDLK_SPACE:
				strcpy (text, txt_key_Space);
				break;
			case SDLK_EXCLAIM:
				strcpy (text, "!");
				break;
			case SDLK_QUOTEDBL:
				strcpy (text, "\"");
				break;
			case SDLK_HASH:
				strcpy (text, "#");
				break;
			case SDLK_DOLLAR:
				strcpy (text, "$");
				break;
			case SDLK_AMPERSAND:
				strcpy (text, "&");
				break;
			case SDLK_QUOTE:
				strcpy (text, "'");
				break;
			case SDLK_LEFTPAREN:
				strcpy (text, "(");
				break;
			case SDLK_RIGHTPAREN:
				strcpy (text, ")");
				break;
			case SDLK_ASTERISK:
				strcpy (text, "*");
				break;
			case SDLK_PLUS:
				strcpy (text, "+");
				break;
			case SDLK_COMMA:
				strcpy (text, ",");
				break;
			case SDLK_MINUS:
				strcpy (text, "-");
				break;
			case SDLK_PERIOD:
				strcpy (text, ".");
				break;
			case SDLK_SLASH:
				strcpy (text, "/");
				break;
			case SDLK_0:
				strcpy (text, "0");
				break;
			case SDLK_1:
				strcpy (text, "1");
				break;
			case SDLK_2:
				strcpy (text, "2");
				break;
			case SDLK_3:
				strcpy (text, "3");
				break;
			case SDLK_4:
				strcpy (text, "4");
				break;
			case SDLK_5:
				strcpy (text, "5");
				break;
			case SDLK_6:
				strcpy (text, "6");
				break;
			case SDLK_7:
				strcpy (text, "7");
				break;
			case SDLK_8:
				strcpy (text, "8");
				break;
			case SDLK_9:
				strcpy (text, "9");
				break;
			case SDLK_COLON:
				strcpy (text, ":");
				break;
			case SDLK_SEMICOLON:
				strcpy (text, ";");
				break;
			case SDLK_LESS:
				strcpy (text, "<");
				break;
			case SDLK_EQUALS:
				strcpy (text, "=");
				break;
			case SDLK_GREATER:
				strcpy (text, ">");
				break;
			case SDLK_QUESTION:
				strcpy (text, "?");
				break;
			case SDLK_AT:
				strcpy (text, "@");
				break;
			case SDLK_LEFTBRACKET:
				strcpy (text, "[");
				break;
			case SDLK_BACKSLASH:
				strcpy (text, "\\");
				break;
			case SDLK_RIGHTBRACKET:
				strcpy (text, "]");
				break;
			case SDLK_CARET:
				strcpy (text, "^");
				break;
			case SDLK_UNDERSCORE:
				strcpy (text, "_");
				break;
			case SDLK_BACKQUOTE:
				strcpy (text, "`");
				break;
			case SDLK_a:
				strcpy (text, "A");
				break;
			case SDLK_b:
				strcpy (text, "B");
				break;
			case SDLK_c:
				strcpy (text, "C");
				break;
			case SDLK_d:
				strcpy (text, "D");
				break;
			case SDLK_e:
				strcpy (text, "E");
				break;
			case SDLK_f:
				strcpy (text, "F");
				break;
			case SDLK_g:
				strcpy (text, "G");
				break;
			case SDLK_h:
				strcpy (text, "H");
				break;
			case SDLK_i:
				strcpy (text, "I");
				break;
			case SDLK_j:
				strcpy (text, "J");
				break;
			case SDLK_k:
				strcpy (text, "K");
				break;
			case SDLK_l:
				strcpy (text, "L");
				break;
			case SDLK_m:
				strcpy (text, "M");
				break;
			case SDLK_n:
				strcpy (text, "N");
				break;
			case SDLK_o:
				strcpy (text, "O");
				break;
			case SDLK_p:
				strcpy (text, "P");
				break;
			case SDLK_q:
				strcpy (text, "Q");
				break;
			case SDLK_r:
				strcpy (text, "R");
				break;
			case SDLK_s:
				strcpy (text, "S");
				break;
			case SDLK_t:
				strcpy (text, "T");
				break;
			case SDLK_u:
				strcpy (text, "U");
				break;
			case SDLK_v:
				strcpy (text, "V");
				break;
			case SDLK_w:
				strcpy (text, "W");
				break;
			case SDLK_x:
				strcpy (text, "X");
				break;
			case SDLK_y:
				strcpy (text, "Y");
				break;
			case SDLK_z:
				strcpy (text, "Z");
				break;
			case SDLK_DELETE:
				strcpy (text, txt_key_Delete);
				break;
			case SDLK_KP0:
				sprintf (text, "%s0", txt_key_KP);
				break;
			case SDLK_KP1:
				sprintf (text, "%s1", txt_key_KP);
				break;
			case SDLK_KP2:
				sprintf (text, "%s2", txt_key_KP);
				break;
			case SDLK_KP3:
				sprintf (text, "%s3", txt_key_KP);
				break;
			case SDLK_KP4:
				sprintf (text, "%s4", txt_key_KP);
				break;
			case SDLK_KP5:
				sprintf (text, "%s5", txt_key_KP);
				break;
			case SDLK_KP6:
				sprintf (text, "%s6", txt_key_KP);
				break;
			case SDLK_KP7:
				sprintf (text, "%s7", txt_key_KP);
				break;
			case SDLK_KP8:
				sprintf (text, "%s8", txt_key_KP);
				break;
			case SDLK_KP9:
				sprintf (text, "%s9", txt_key_KP);
				break;
			case SDLK_KP_PERIOD:
				sprintf (text, "%s.", txt_key_KP);
				break;
			case SDLK_KP_DIVIDE:
				sprintf (text, "%s/", txt_key_KP);
				break;
			case SDLK_KP_MULTIPLY:
				sprintf (text, "%s*", txt_key_KP);
				break;
			case SDLK_KP_MINUS:
				sprintf (text, "%s-", txt_key_KP);
				break;
			case SDLK_KP_PLUS:
				sprintf (text, "%s+", txt_key_KP);
				break;
			case SDLK_KP_ENTER:
				sprintf (text, "%sEnter", txt_key_KP);
				break;
			case SDLK_KP_EQUALS:
				sprintf (text, "%s=", txt_key_KP);
				break;
			case SDLK_UP:
				strcpy (text, txt_key_Up);
				break;
			case SDLK_DOWN:
				strcpy (text, txt_key_Down);
				break;
			case SDLK_RIGHT:
				strcpy (text, txt_key_Right);
				break;
			case SDLK_LEFT:
				strcpy (text, txt_key_Left);
				break;
			case SDLK_INSERT:
				strcpy (text, txt_key_Insert);
				break;
			case SDLK_HOME:
				strcpy (text, txt_key_Home);
				break;
			case SDLK_END:
				strcpy (text, txt_key_End);
				break;
			case SDLK_PAGEUP:
				strcpy (text, txt_key_PgUp);
				break;
			case SDLK_PAGEDOWN:
				strcpy (text, txt_key_PgDn);
				break;
			case SDLK_F1:
				strcpy (text, "F1");
				break;
			case SDLK_F2:
				strcpy (text, "F2");
				break;
			case SDLK_F3:
				strcpy (text, "F3");
				break;
			case SDLK_F4:
				strcpy (text, "F4");
				break;
			case SDLK_F5:
				strcpy (text, "F5");
				break;
			case SDLK_F6:
				strcpy (text, "F6");
				break;
			case SDLK_F7:
				strcpy (text, "F7");
				break;
			case SDLK_F8:
				strcpy (text, "F8");
				break;
			case SDLK_F9:
				strcpy (text, "F9");
				break;
			case SDLK_F10:
				strcpy (text, "F10");
				break;
			case SDLK_F11:
				strcpy (text, "F11");
				break;
			case SDLK_F12:
				strcpy (text, "F12");
				break;
			case SDLK_F13:
				strcpy (text, "F13");
				break;
			case SDLK_F14:
				strcpy (text, "F14");
				break;
			case SDLK_F15:
				strcpy (text, "F15");
				break;
			case SDLK_NUMLOCK:
				strcpy (text, txt_key_NumLk);
				break;
			case SDLK_CAPSLOCK:
				strcpy (text, txt_key_CapsLk);
				break;
			case SDLK_SCROLLOCK:
				strcpy (text, txt_key_ScrlLk);
				break;
			case SDLK_RSHIFT:
				strcpy (text, txt_key_RShift);
				break;
			case SDLK_LSHIFT:
				strcpy (text, txt_key_LShift);
				break;
			case SDLK_RCTRL:
				strcpy (text, txt_key_RCtrl);
				break;
			case SDLK_LCTRL:
				strcpy (text, txt_key_LCtrl);
				break;
			case SDLK_RALT:
				strcpy (text, txt_key_RAlt);
				break;
			case SDLK_LALT:
				strcpy (text, txt_key_LAlt);
				break;
			case SDLK_RMETA:
				strcpy (text, txt_key_RMeta);
				break;
			case SDLK_LMETA:
				strcpy (text, txt_key_LMeta);
				break;
			case SDLK_LSUPER:
				strcpy (text, txt_key_LSuper);
				break;
			case SDLK_RSUPER:
				strcpy (text, txt_key_RSuper);
				break;
			case SDLK_MODE:
				strcpy (text, txt_key_AltGr);
				break;
			case SDLK_COMPOSE:
				strcpy (text, txt_key_Compose);
				break;
			case SDLK_HELP:
				strcpy (text, txt_key_Help);
				break;
			case SDLK_PRINT:
				strcpy (text, txt_key_PrScr);
				break;
			case SDLK_SYSREQ:
				strcpy (text, txt_key_SysRq);
				break;
			case SDLK_BREAK:
				strcpy (text, txt_key_Break);
				break;
			case SDLK_MENU:
				strcpy (text, txt_key_Menu);
				break;
			case SDLK_POWER:
				strcpy (text, txt_key_Power);
				break;
			case SDLK_EURO:
				strcpy (text, txt_key_Euro);
				break;
			case SDLK_UNDO:
				strcpy (text, txt_key_Undo);
				break;
			default:
				sprintf (text, "%i", id);
				/*strcpy(text, "Unknown"); */
				break;
		}
	}
	else if (device == DEVICE_JOYSTICK)
	{
		if (strcmp (joyname, "Microsoft SideWinder Game Pad Pro USB version 1.0") == 0)
		{	/* Has a pseudo analogue (digital) D-pad with 2 axes
			* that's good for testing with but not much else ;) */
			switch (id)
			{
				case 0:
					strcpy (text, "A");
					break;
				case 1:
					strcpy (text, "B");
					break;
				case 2:
					strcpy (text, "C");
					break;
				case 3:
					strcpy (text, "X");
					break;
				case 4:
					strcpy (text, "Y");
					break;
				case 5:
					strcpy (text, "Z");
					break;
				case 6:
					strcpy (text, "LTrig");
					break;
				case 7:
					strcpy (text, "RTrig");
					break;
				case 8:
					strcpy (text, "Shift");
					break;
				case 10:
					strcpy (text, "Left");
					break;
				case 11:
					strcpy (text, "Right");
					break;
				case 12:
					strcpy (text, "Up");
					break;
				case 13:
					strcpy (text, "Down");
					break;
				default:
					sprintf (text, "%i", id);
					/*strcpy(text, "Unknown"); */
					break;
			}
		}
		else if (strcmp (joyname, "MM809 Commander Pad") == 0)
		{	/* Manta MM-809 */
			switch (id)
			{
				case 0:
					strcpy (text, "1");
					break;
				case 1:
					strcpy (text, "2");
					break;
				case 2:
					strcpy (text, "3");
					break;
				case 3:
					strcpy (text, "4");
					break;
				case 4:
					strcpy (text, "5");
					break;
				case 5:
					strcpy (text, "6");
					break;
				case 6:
					strcpy (text, "7");
					break;
				case 7:
					strcpy (text, "8");
					break;
				case 8:
					strcpy (text, "9");
					break;
				case 9:
					strcpy (text, "10");
					break;
				case 10:
					strcpy (text, "11");
					break;
				case 11:
					strcpy (text, "12");
					break;
				case 12:
					strcpy (text, "Left");
					break;
				case 13:
					strcpy (text, "Right");
					break;
				case 14:
					strcpy (text, "Up");
					break;
				case 15:
					strcpy (text, "Down");
					break;
				default:
					sprintf (text, "%i", id);
					break;
			}
		}
		else if ((strcmp (joyname, "PEP Joy") == 0) || (strcmp (joyname, "GP2X_JOYSTICK") == 0))
		{	/* GP2X F100/F200 joystick */
			switch (id)
			{
				case GP2X_JOY_N:
					strcpy (text, "Up");
					break;
				case GP2X_JOY_NW:
					strcpy (text, "UpLeft");
					break;
				case GP2X_JOY_W:
					strcpy (text, "Left");
					break;
				case GP2X_JOY_SW:
					strcpy (text, "DownLeft");
					break;
				case GP2X_JOY_S:
					strcpy (text, "Down");
					break;
				case GP2X_JOY_SE:
					strcpy (text, "DownRight");
					break;
				case GP2X_JOY_E:
					strcpy (text, "Right");
					break;
				case GP2X_JOY_NE:
					strcpy (text, "UpRight");
					break;
				case GP2X_START:
					strcpy (text, "Start");
					break;
				case GP2X_SELECT:
					strcpy (text, "Select");
					break;
				case GP2X_LTRIG:
					strcpy (text, "LTrig");
					break;
				case GP2X_RTRIG:
					strcpy (text, "RTrig");
					break;
				case GP2X_BTN_A:
					strcpy (text, "A");
					break;
				case GP2X_BTN_B:
					strcpy (text, "B");
					break;
				case GP2X_BTN_X:
					strcpy (text, "X");
					break;
				case GP2X_BTN_Y:
					strcpy (text, "Y");
					break;
				case GP2X_VOL_UP:
					strcpy (text, "VolUp");
					break;
				case GP2X_VOL_DN:
					strcpy (text, "VolDn");
					break;
				case GP2X_BTN_JOY:
					strcpy (text, "JoyPush");
					break;
				default:
					sprintf (text, "%i", id);
					/*strcpy(text, "Unknown"); */
					break;
			}
		}
		else
		{
			sprintf (text, "%i", id);
		}
	}
	else
	{
		sprintf (text, "%i", device);
	}
}