forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputdevice.cpp
7563 lines (6978 loc) · 201 KB
/
inputdevice.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* joystick/mouse emulation
*
* Copyright 2001-2012 Toni Wilen
*
* new fetures:
* - very configurable (and very complex to configure :)
* - supports multiple native input devices (joysticks and mice)
* - supports mapping joystick/mouse buttons to keys and vice versa
* - joystick mouse emulation (supports both ports)
* - supports parallel port joystick adapter
* - full cd32 pad support (supports both ports)
* - fully backward compatible with old joystick/mouse configuration
*
*/
//#define DONGLE_DEBUG
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "keyboard.h"
#include "inputdevice.h"
#include "inputrecord.h"
#include "keybuf.h"
#include "custom.h"
#include "xwin.h"
#include "drawing.h"
#include "memory.h"
#include "events.h"
#include "newcpu.h"
#include "uae.h"
#include "picasso96.h"
#include "catweasel.h"
#include "debug.h"
#include "ar.h"
#include "gui.h"
#include "disk.h"
#include "audio.h"
#include "sound.h"
#include "savestate.h"
#include "arcadia.h"
#include "zfile.h"
#include "cia.h"
#include "autoconf.h"
#ifdef RETROPLATFORM
#include "rp.h"
#endif
#include "dongle.h"
#include "cdtv.h"
#ifdef AVIOUTPUT
#include "avioutput.h"
#endif
#include "tabletlibrary.h"
#include "statusline.h"
// 01 = host events
// 02 = joystick
// 04 = cia buttons
// 16 = potgo
// 32 = vsync
int inputdevice_logging = 0;
extern int tablet_log;
#define COMPA_RESERVED_FLAGS ID_FLAG_INVERT
#define ID_FLAG_CANRELEASE 0x1000
#define ID_FLAG_TOGGLED 0x2000
#define ID_FLAG_CUSTOMEVENT_TOGGLED1 0x4000
#define ID_FLAG_CUSTOMEVENT_TOGGLED2 0x8000
#define IE_INVERT 0x80
#define IE_CDTV 0x100
#define INPUTEVENT_JOY1_CD32_FIRST INPUTEVENT_JOY1_CD32_PLAY
#define INPUTEVENT_JOY2_CD32_FIRST INPUTEVENT_JOY2_CD32_PLAY
#define INPUTEVENT_JOY1_CD32_LAST INPUTEVENT_JOY1_CD32_BLUE
#define INPUTEVENT_JOY2_CD32_LAST INPUTEVENT_JOY2_CD32_BLUE
#define JOYMOUSE_CDTV 8
#define DEFEVENT(A, B, C, D, E, F) {_T(#A), B, C, D, E, F },
static struct inputevent events[] = {
{0, 0, AM_K,0,0,0},
#include "inputevents.def"
{0, 0, 0, 0, 0, 0}
};
#undef DEFEVENT
static int sublevdir[2][MAX_INPUT_SUB_EVENT];
static const int slotorder1[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
static const int slotorder2[] = { 8, 1, 2, 3, 4, 5, 6, 7 };
struct uae_input_device2 {
uae_u32 buttonmask;
int states[MAX_INPUT_DEVICE_EVENTS / 2][MAX_INPUT_SUB_EVENT + 1];
};
static struct uae_input_device2 joysticks2[MAX_INPUT_DEVICES];
static struct uae_input_device2 mice2[MAX_INPUT_DEVICES];
static uae_u8 scancodeused[MAX_INPUT_DEVICES][256];
static uae_u64 qualifiers, qualifiers_r;
static uae_s16 *qualifiers_evt[MAX_INPUT_QUALIFIERS];
// fire/left mouse button pullup resistors enabled?
static bool mouse_pullup = true;
static int joymodes[MAX_JPORTS];
static int *joyinputs[MAX_JPORTS];
static int input_acquired;
static int testmode, testmode_read, testmode_toggle;
struct teststore
{
int testmode_type;
int testmode_num;
int testmode_wtype;
int testmode_wnum;
int testmode_state;
int testmode_max;
};
#define TESTMODE_MAX 2
static int testmode_count;
static struct teststore testmode_data[TESTMODE_MAX];
static struct teststore testmode_wait[TESTMODE_MAX];
static int bouncy;
static signed long bouncy_cycles;
static int handle_input_event (int nr, int state, int max, int autofire, bool canstoprecord, bool playbackevent);
static struct inputdevice_functions idev[IDTYPE_MAX];
static int isdevice (struct uae_input_device *id)
{
int i, j;
for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++) {
for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
if (id->eventid[i][j] > 0)
return 1;
}
}
return 0;
}
int inputdevice_uaelib (const TCHAR *s, const TCHAR *parm)
{
int i;
for (i = 1; events[i].name; i++) {
if (!_tcscmp (s, events[i].confname)) {
handle_input_event (i, parm ? _tstol (parm) : 0, 1, 0, false, false);
return 1;
}
}
return 0;
}
static struct uae_input_device *joysticks;
static struct uae_input_device *mice;
static struct uae_input_device *keyboards;
static struct uae_input_device *internalevents;
static struct uae_input_device_kbr_default *keyboard_default, **keyboard_default_table;
static int default_keyboard_layout[MAX_JPORTS];
#define KBR_DEFAULT_MAP_FIRST 0
#define KBR_DEFAULT_MAP_LAST 5
#define KBR_DEFAULT_MAP_CD32_FIRST 6
#define KBR_DEFAULT_MAP_CD32_LAST 8
#define KBR_DEFAULT_MAP_NP 0
#define KBR_DEFAULT_MAP_CK 1
#define KBR_DEFAULT_MAP_SE 2
#define KBR_DEFAULT_MAP_NP3 3
#define KBR_DEFAULT_MAP_CK3 4
#define KBR_DEFAULT_MAP_SE3 5
#define KBR_DEFAULT_MAP_CD32_NP 6
#define KBR_DEFAULT_MAP_CD32_CK 7
#define KBR_DEFAULT_MAP_CD32_SE 8
#define KBR_DEFAULT_MAP_XA1 9
#define KBR_DEFAULT_MAP_XA2 10
#define KBR_DEFAULT_MAP_ARCADIA 11
#define KBR_DEFAULT_MAP_ARCADIA_XA 12
#define KBR_DEFAULT_MAP_CDTV 13
static int **keyboard_default_kbmaps;
static int mouse_axis[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
static int oldm_axis[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
#define MOUSE_AXIS_TOTAL 4
static uae_s16 mouse_x[MAX_JPORTS], mouse_y[MAX_JPORTS];
static uae_s16 mouse_delta[MAX_JPORTS][MOUSE_AXIS_TOTAL];
static uae_s16 mouse_deltanoreset[MAX_JPORTS][MOUSE_AXIS_TOTAL];
static uae_s16 lightpen_delta[2];
static uae_s16 lightpen_deltanoreset[2];
static int joybutton[MAX_JPORTS];
static int joydir[MAX_JPORTS];
static int joydirpot[MAX_JPORTS][2];
static uae_s16 mouse_frame_x[MAX_JPORTS], mouse_frame_y[MAX_JPORTS];
static int mouse_port[NORMAL_JPORTS];
static int cd32_shifter[NORMAL_JPORTS];
static int cd32_pad_enabled[NORMAL_JPORTS];
static int parport_joystick_enabled;
static int oleft[MAX_JPORTS], oright[MAX_JPORTS], otop[MAX_JPORTS], obot[MAX_JPORTS];
static int horizclear[MAX_JPORTS], vertclear[MAX_JPORTS];
static int relativecount[MAX_JPORTS][2];
uae_u16 potgo_value;
static int pot_cap[NORMAL_JPORTS][2];
static uae_u8 pot_dat[NORMAL_JPORTS][2];
static int pot_dat_act[NORMAL_JPORTS][2];
static int analog_port[NORMAL_JPORTS][2];
static int digital_port[NORMAL_JPORTS][2];
#define POTDAT_DELAY_PAL 8
#define POTDAT_DELAY_NTSC 7
static int use_joysticks[MAX_INPUT_DEVICES];
static int use_mice[MAX_INPUT_DEVICES];
static int use_keyboards[MAX_INPUT_DEVICES];
#define INPUT_QUEUE_SIZE 16
struct input_queue_struct {
int evt, storedstate, state, max, linecnt, nextlinecnt;
TCHAR *custom;
};
static struct input_queue_struct input_queue[INPUT_QUEUE_SIZE];
uae_u8 *restore_input (uae_u8 *src)
{
restore_u32 ();
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
pot_cap[i][j] = restore_u16 ();
}
}
return src;
}
uae_u8 *save_input (int *len, uae_u8 *dstptr)
{
uae_u8 *dstbak, *dst;
if (dstptr)
dstbak = dst = dstptr;
else
dstbak = dst = xmalloc (uae_u8, 1000);
save_u32 (0);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
save_u16 (pot_cap[i][j]);
}
}
*len = dst - dstbak;
return dstbak;
}
static void freejport (struct uae_prefs *dst, int num)
{
bool override = dst->jports[num].nokeyboardoverride;
memset (&dst->jports[num], 0, sizeof (struct jport));
dst->jports[num].id = -1;
dst->jports[num].nokeyboardoverride = override;
}
static void copyjport (const struct uae_prefs *src, struct uae_prefs *dst, int num)
{
if (!src)
return;
freejport (dst, num);
_tcscpy (dst->jports[num].configname, src->jports[num].configname);
_tcscpy (dst->jports[num].name, src->jports[num].name);
dst->jports[num].id = src->jports[num].id;
dst->jports[num].mode = src->jports[num].mode;
dst->jports[num].autofire = src->jports[num].autofire;
dst->jports[num].nokeyboardoverride = src->jports[num].nokeyboardoverride;
}
static void out_config (struct zfile *f, int id, int num, const TCHAR *s1, const TCHAR *s2)
{
TCHAR tmp[MAX_DPATH];
_stprintf (tmp, _T("input.%d.%s%d"), id, s1, num);
cfgfile_write_str (f, tmp, s2);
}
static bool write_config_head (struct zfile *f, int idnum, int devnum, const TCHAR *name, struct uae_input_device *id, struct inputdevice_functions *idf)
{
TCHAR *s = NULL;
TCHAR tmp2[CONFIG_BLEN];
if (idnum == GAMEPORT_INPUT_SETTINGS) {
if (!isdevice (id))
return false;
if (!id->enabled)
return false;
}
if (id->name)
s = id->name;
else if (devnum < idf->get_num ())
s = idf->get_friendlyname (devnum);
if (s) {
_stprintf (tmp2, _T("input.%d.%s.%d.friendlyname"), idnum + 1, name, devnum);
cfgfile_write_str (f, tmp2, s);
}
s = NULL;
if (id->configname)
s = id->configname;
else if (devnum < idf->get_num ())
s = idf->get_uniquename (devnum);
if (s) {
_stprintf (tmp2, _T("input.%d.%s.%d.name"), idnum + 1, name, devnum);
cfgfile_write_str (f, tmp2, s);
}
if (!isdevice (id)) {
_stprintf (tmp2, _T("input.%d.%s.%d.empty"), idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, true);
if (id->enabled) {
_stprintf (tmp2, _T("input.%d.%s.%d.disabled"), idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, id->enabled ? false : true);
}
return false;
}
if (idnum == GAMEPORT_INPUT_SETTINGS) {
_stprintf (tmp2, _T("input.%d.%s.%d.custom"), idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, true);
} else {
_stprintf (tmp2, _T("input.%d.%s.%d.empty"), idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, false);
_stprintf (tmp2, _T("input.%d.%s.%d.disabled"), idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, id->enabled ? false : true);
}
return true;
}
static bool write_slot (TCHAR *p, struct uae_input_device *uid, int i, int j)
{
bool ok = false;
if (i < 0 || j < 0) {
_tcscpy (p, _T("NULL"));
return false;
}
uae_u64 flags = uid->flags[i][j];
if (uid->custom[i][j] && _tcslen (uid->custom[i][j]) > 0) {
_stprintf (p, _T("'%s'.%d"), uid->custom[i][j], flags & ID_FLAG_SAVE_MASK_CONFIG);
ok = true;
} else if (uid->eventid[i][j] > 0) {
_stprintf (p, _T("%s.%d"), events[uid->eventid[i][j]].confname, flags & ID_FLAG_SAVE_MASK_CONFIG);
ok = true;
} else {
_tcscpy (p, _T("NULL"));
}
if (ok && (flags & ID_FLAG_SAVE_MASK_QUALIFIERS)) {
TCHAR *p2 = p + _tcslen (p);
*p2++ = '.';
for (int i = 0; i < MAX_INPUT_QUALIFIERS * 2; i++) {
if ((ID_FLAG_QUALIFIER1 << i) & flags) {
if (i & 1)
_stprintf (p2, _T("%c"), 'a' + i / 2);
else
_stprintf (p2, _T("%c"), 'A' + i / 2);
p2++;
}
}
}
return ok;
}
static struct inputdevice_functions *getidf (int devnum);
static void kbrlabel (TCHAR *s)
{
while (*s) {
*s = _totupper (*s);
if (*s == ' ')
*s = '_';
s++;
}
}
static void write_config2 (struct zfile *f, int idnum, int i, int offset, const TCHAR *extra, struct uae_input_device *id)
{
TCHAR tmp2[CONFIG_BLEN], tmp3[CONFIG_BLEN], *p;
int evt, got, j, k;
TCHAR *custom;
const int *slotorder;
int io = i + offset;
tmp2[0] = 0;
p = tmp2;
got = 0;
slotorder = slotorder1;
// if gameports non-custom mapping in slot0 -> save slot8 as slot0
if (id->port[io][0] && !(id->flags[io][0] & ID_FLAG_GAMEPORTSCUSTOM_MASK))
slotorder = slotorder2;
for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
evt = id->eventid[io][slotorder[j]];
custom = id->custom[io][slotorder[j]];
if (custom == NULL && evt <= 0) {
for (k = j + 1; k < MAX_INPUT_SUB_EVENT; k++) {
if ((id->port[io][k] == 0 || id->port[io][k] == MAX_JPORTS + 1) && (id->eventid[io][slotorder[k]] > 0 || id->custom[io][slotorder[k]] != NULL))
break;
}
if (k == MAX_INPUT_SUB_EVENT)
break;
}
if (p > tmp2) {
*p++ = ',';
*p = 0;
}
bool ok = write_slot (p, id, io, slotorder[j]);
p += _tcslen (p);
if (ok) {
if (id->port[io][slotorder[j]] > 0 && id->port[io][slotorder[j]] < MAX_JPORTS + 1) {
int pnum = id->port[io][slotorder[j]] - 1;
_stprintf (p, _T(".%d"), pnum);
p += _tcslen (p);
if (idnum != GAMEPORT_INPUT_SETTINGS && j == 0 && id->port[io][SPARE_SUB_EVENT] && slotorder == slotorder1) {
*p++ = '.';
write_slot (p, id, io, SPARE_SUB_EVENT);
p += _tcslen (p);
}
}
}
}
if (p > tmp2) {
_stprintf (tmp3, _T("input.%d.%s%d"), idnum + 1, extra, i);
cfgfile_write_str (f, tmp3, tmp2);
}
}
static void write_kbr_config (struct zfile *f, int idnum, int devnum, struct uae_input_device *kbr, struct inputdevice_functions *idf)
{
TCHAR tmp1[CONFIG_BLEN], tmp2[CONFIG_BLEN], tmp3[CONFIG_BLEN], tmp4[CONFIG_BLEN], tmp5[CONFIG_BLEN], *p;
int i, j, k, evt, skip;
const int *slotorder;
if (!keyboard_default)
return;
if (!write_config_head (f, idnum, devnum, _T("keyboard"), kbr, idf))
return;
i = 0;
while (i < MAX_INPUT_DEVICE_EVENTS && kbr->extra[i] >= 0) {
slotorder = slotorder1;
// if gameports non-custom mapping in slot0 -> save slot4 as slot0
if (kbr->port[i][0] && !(kbr->flags[i][0] & ID_FLAG_GAMEPORTSCUSTOM_MASK))
slotorder = slotorder2;
skip = 0;
k = 0;
while (keyboard_default[k].scancode >= 0) {
if (keyboard_default[k].scancode == kbr->extra[i]) {
skip = 1;
for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
if (keyboard_default[k].node[j].evt != 0) {
if (keyboard_default[k].node[j].evt != kbr->eventid[i][slotorder[j]] || keyboard_default[k].node[j].flags != (kbr->flags[i][slotorder[j]] & ID_FLAG_SAVE_MASK_FULL))
skip = 0;
} else if ((kbr->flags[i][slotorder[j]] & ID_FLAG_SAVE_MASK_FULL) != 0 || kbr->eventid[i][slotorder[j]] > 0) {
skip = 0;
}
}
break;
}
k++;
}
bool isdefaultspare =
kbr->port[i][SPARE_SUB_EVENT] &&
keyboard_default[k].node[0].evt == kbr->eventid[i][SPARE_SUB_EVENT] && keyboard_default[k].node[0].flags == (kbr->flags[i][SPARE_SUB_EVENT] & ID_FLAG_SAVE_MASK_FULL);
if (kbr->port[i][0] > 0 && !(kbr->flags[i][0] & ID_FLAG_GAMEPORTSCUSTOM_MASK) &&
(kbr->eventid[i][1] <= 0 && kbr->eventid[i][2] <= 0 && kbr->eventid[i][3] <= 0) &&
(kbr->port[i][SPARE_SUB_EVENT] == 0 || isdefaultspare))
skip = 1;
if (kbr->eventid[i][0] == 0 && (kbr->flags[i][0] & ID_FLAG_SAVE_MASK_FULL) == 0 && keyboard_default[k].scancode < 0)
skip = 1;
if (skip) {
i++;
continue;
}
tmp2[0] = 0;
p = tmp2;
for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
TCHAR *custom = kbr->custom[i][slotorder[j]];
evt = kbr->eventid[i][slotorder[j]];
if (custom == NULL && evt <= 0) {
for (k = j + 1; k < MAX_INPUT_SUB_EVENT; k++) {
if (kbr->eventid[i][slotorder[k]] > 0 || kbr->custom[i][slotorder[k]] != NULL)
break;
}
if (k == MAX_INPUT_SUB_EVENT)
break;
}
if (p > tmp2) {
*p++ = ',';
*p = 0;
}
bool ok = write_slot (p, kbr, i, slotorder[j]);
p += _tcslen (p);
if (ok) {
// save port number + SPARE SLOT if needed
if (kbr->port[i][slotorder[j]] > 0 && (kbr->flags[i][slotorder[j]] & ID_FLAG_GAMEPORTSCUSTOM_MASK)) {
_stprintf (p, _T(".%d"), kbr->port[i][slotorder[j]] - 1);
p += _tcslen (p);
if (idnum != GAMEPORT_INPUT_SETTINGS && j == 0 && kbr->port[i][SPARE_SUB_EVENT] && !isdefaultspare && slotorder == slotorder1) {
*p++ = '.';
write_slot (p, kbr, i, SPARE_SUB_EVENT);
p += _tcslen (p);
}
}
}
}
idf->get_widget_type (devnum, i, tmp5, NULL);
_stprintf (tmp3, _T("%d%s%s"), kbr->extra[i], tmp5[0] ? _T(".") : _T(""), tmp5[0] ? tmp5 : _T(""));
kbrlabel (tmp3);
_stprintf (tmp1, _T("keyboard.%d.button.%s"), devnum, tmp3);
_stprintf (tmp4, _T("input.%d.%s"), idnum + 1, tmp1);
cfgfile_write_str (f, tmp4, tmp2[0] ? tmp2 : _T("NULL"));
i++;
}
}
static void write_config (struct zfile *f, int idnum, int devnum, const TCHAR *name, struct uae_input_device *id, struct inputdevice_functions *idf)
{
TCHAR tmp1[MAX_DPATH];
int i;
if (!write_config_head (f, idnum, devnum, name, id, idf))
return;
_stprintf (tmp1, _T("%s.%d.axis."), name, devnum);
for (i = 0; i < ID_AXIS_TOTAL; i++)
write_config2 (f, idnum, i, ID_AXIS_OFFSET, tmp1, id);
_stprintf (tmp1, _T("%s.%d.button.") ,name, devnum);
for (i = 0; i < ID_BUTTON_TOTAL; i++)
write_config2 (f, idnum, i, ID_BUTTON_OFFSET, tmp1, id);
}
static const TCHAR *kbtypes[] = { _T("amiga"), _T("pc"), NULL };
void write_inputdevice_config (struct uae_prefs *p, struct zfile *f)
{
int i, id;
cfgfile_write (f, _T("input.config"), _T("%d"), p->input_selected_setting == GAMEPORT_INPUT_SETTINGS ? 0 : p->input_selected_setting + 1);
cfgfile_write (f, _T("input.joymouse_speed_analog"), _T("%d"), p->input_joymouse_multiplier);
cfgfile_write (f, _T("input.joymouse_speed_digital"), _T("%d"), p->input_joymouse_speed);
cfgfile_write (f, _T("input.joymouse_deadzone"), _T("%d"), p->input_joymouse_deadzone);
cfgfile_write (f, _T("input.joystick_deadzone"), _T("%d"), p->input_joystick_deadzone);
cfgfile_write (f, _T("input.analog_joystick_multiplier"), _T("%d"), p->input_analog_joystick_mult);
cfgfile_write (f, _T("input.analog_joystick_offset"), _T("%d"), p->input_analog_joystick_offset);
cfgfile_write (f, _T("input.mouse_speed"), _T("%d"), p->input_mouse_speed);
cfgfile_write (f, _T("input.autofire_speed"), _T("%d"), p->input_autofire_linecnt);
cfgfile_dwrite_str (f, _T("input.keyboard_type"), kbtypes[p->input_keyboard_type]);
cfgfile_dwrite (f, _T("input.contact_bounce"), _T("%d"), p->input_contact_bounce);
for (id = 0; id < MAX_INPUT_SETTINGS; id++) {
TCHAR tmp[MAX_DPATH];
if (id < GAMEPORT_INPUT_SETTINGS) {
_stprintf (tmp, _T("input.%d.name"), id + 1);
cfgfile_dwrite_str (f, tmp, p->input_config_name[id]);
}
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_config (f, id, i, _T("joystick"), &p->joystick_settings[id][i], &idev[IDTYPE_JOYSTICK]);
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_config (f, id, i, _T("mouse"), &p->mouse_settings[id][i], &idev[IDTYPE_MOUSE]);
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_kbr_config (f, id, i, &p->keyboard_settings[id][i], &idev[IDTYPE_KEYBOARD]);
write_config (f, id, 0, _T("internal"), &p->internalevent_settings[id][0], &idev[IDTYPE_INTERNALEVENT]);
}
}
static uae_u64 getqual (const TCHAR **pp)
{
const TCHAR *p = *pp;
uae_u64 mask = 0;
while ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')) {
bool press = (*p >= 'A' && *p <= 'Z');
int shift, inc;
if (press) {
shift = *p - 'A';
inc = 0;
} else {
shift = *p - 'a';
inc = 1;
}
mask |= ID_FLAG_QUALIFIER1 << (shift * 2 + inc);
p++;
}
while (*p != 0 && *p !='.' && *p != ',')
p++;
if (*p == '.' || *p == ',')
p++;
*pp = p;
return mask;
}
static int getnum (const TCHAR **pp)
{
const TCHAR *p = *pp;
int v;
if (!_tcsnicmp (p, _T("false"), 5))
v = 0;
else if (!_tcsnicmp (p, _T("true"), 4))
v = 1;
else
v = _tstol (p);
while (*p != 0 && *p !='.' && *p != ',')
p++;
if (*p == '.' || *p == ',')
p++;
*pp = p;
return v;
}
static TCHAR *getstring (const TCHAR **pp)
{
int i;
static TCHAR str[CONFIG_BLEN];
const TCHAR *p = *pp;
bool quoteds = false;
bool quotedd = false;
if (*p == 0)
return 0;
i = 0;
while (*p != 0 && i < 1000 - 1) {
if (*p == '\"')
quotedd = quotedd ? false : true;
if (*p == '\'')
quoteds = quoteds ? false : true;
if (!quotedd && !quoteds) {
if (*p == '.' || *p == ',')
break;
}
str[i++] = *p++;
}
if (*p == '.' || *p == ',')
p++;
str[i] = 0;
*pp = p;
return str;
}
static void reset_inputdevice_settings (struct uae_input_device *uid)
{
for (int l = 0; l < MAX_INPUT_DEVICE_EVENTS; l++) {
for (int i = 0; i < MAX_INPUT_SUB_EVENT_ALL; i++) {
uid->eventid[l][i] = 0;
uid->flags[l][i] = 0;
if (uid->custom[l][i]) {
xfree (uid->custom[l][i]);
uid->custom[l][i] = NULL;
}
}
}
}
static void reset_inputdevice_slot (struct uae_prefs *prefs, int slot)
{
for (int m = 0; m < MAX_INPUT_DEVICES; m++) {
reset_inputdevice_settings (&prefs->joystick_settings[slot][m]);
reset_inputdevice_settings (&prefs->mouse_settings[slot][m]);
reset_inputdevice_settings (&prefs->keyboard_settings[slot][m]);
}
}
void reset_inputdevice_config (struct uae_prefs *prefs)
{
for (int i = 0; i < MAX_INPUT_SETTINGS; i++)
reset_inputdevice_slot (prefs, i);
}
static void set_kbr_default_event (struct uae_input_device *kbr, struct uae_input_device_kbr_default *trans, int num)
{
for (int i = 0; trans[i].scancode >= 0; i++) {
if (kbr->extra[num] == trans[i].scancode) {
int k;
for (k = 0; k < MAX_INPUT_SUB_EVENT; k++) {
if (kbr->eventid[num][k] == 0)
break;
}
if (k == MAX_INPUT_SUB_EVENT) {
write_log (_T("corrupt default keyboard mappings\n"));
return;
}
int l = 0;
while (k < MAX_INPUT_SUB_EVENT && trans[i].node[l].evt) {
int evt = trans[i].node[l].evt;
if (evt < 0 || evt >= INPUTEVENT_SPC_LAST)
gui_message(_T("invalid event in default keyboard table!"));
kbr->eventid[num][k] = evt;
kbr->flags[num][k] = trans[i].node[l].flags;
l++;
k++;
}
break;
}
}
}
static void clear_id (struct uae_input_device *id)
{
#ifndef _DEBUG
int i, j;
for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++) {
for (j = 0; j < MAX_INPUT_SUB_EVENT_ALL; j++)
xfree (id->custom[i][j]);
}
#endif
TCHAR *cn = id->configname;
TCHAR *n = id->name;
memset (id, 0, sizeof (struct uae_input_device));
id->configname = cn;
id->name = n;
}
static void set_kbr_default (struct uae_prefs *p, int index, int devnum, struct uae_input_device_kbr_default *trans)
{
int i, j;
struct uae_input_device *kbr;
struct inputdevice_functions *id = &idev[IDTYPE_KEYBOARD];
uae_u32 scancode;
if (!trans)
return;
for (j = 0; j < MAX_INPUT_DEVICES; j++) {
if (devnum >= 0 && devnum != j)
continue;
kbr = &p->keyboard_settings[index][j];
clear_id (kbr);
for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++)
kbr->extra[i] = -1;
if (j < id->get_num ()) {
if (input_get_default_keyboard (j))
kbr->enabled = 1;
for (i = 0; i < id->get_widget_num (j); i++) {
id->get_widget_type (j, i, 0, &scancode);
kbr->extra[i] = scancode;
set_kbr_default_event (kbr, trans, i);
}
}
}
}
static void inputdevice_default_kb (struct uae_prefs *p, int num)
{
if (num == GAMEPORT_INPUT_SETTINGS) {
if (p->jports[0].id != JPORT_CUSTOM || p->jports[1].id != JPORT_CUSTOM)
reset_inputdevice_slot (p, num);
}
set_kbr_default (p, num, -1, keyboard_default);
}
static void inputdevice_default_kb_all (struct uae_prefs *p)
{
for (int i = 0; i < MAX_INPUT_SETTINGS; i++)
inputdevice_default_kb (p, i);
}
static bool read_slot (const TCHAR *parm, int num, int joystick, int button, struct uae_input_device *id, int keynum, int subnum, struct inputevent *ie, uae_u64 flags, int port, TCHAR *custom)
{
int mask;
if (custom == NULL && ie->name == NULL) {
if (!_tcscmp (parm, _T("NULL"))) {
if (joystick < 0) {
id->eventid[keynum][subnum] = 0;
id->flags[keynum][subnum] = 0;
} else if (button) {
id->eventid[num + ID_BUTTON_OFFSET][subnum] = 0;
id->flags[num + ID_BUTTON_OFFSET][subnum] = 0;
} else {
id->eventid[num + ID_AXIS_OFFSET][subnum] = 0;
id->flags[num + ID_AXIS_OFFSET][subnum] = 0;
}
}
return false;
}
if (custom)
ie = &events[INPUTEVENT_SPC_CUSTOM_EVENT];
if (joystick < 0) {
if (!(ie->allow_mask & AM_K))
return false;
id->eventid[keynum][subnum] = ie - events;
id->flags[keynum][subnum] = flags;
id->port[keynum][subnum] = port;
xfree (id->custom[keynum][subnum]);
id->custom[keynum][subnum] = custom;
} else if (button) {
if (joystick)
mask = AM_JOY_BUT;
else
mask = AM_MOUSE_BUT;
if (!(ie->allow_mask & mask))
return false;
id->eventid[num + ID_BUTTON_OFFSET][subnum] = ie - events;
id->flags[num + ID_BUTTON_OFFSET][subnum] = flags;
id->port[num + ID_BUTTON_OFFSET][subnum] = port;
xfree (id->custom[num + ID_BUTTON_OFFSET][subnum]);
id->custom[num + ID_BUTTON_OFFSET][subnum] = custom;
} else {
if (joystick)
mask = AM_JOY_AXIS;
else
mask = AM_MOUSE_AXIS;
if (!(ie->allow_mask & mask))
return false;
id->eventid[num + ID_AXIS_OFFSET][subnum] = ie - events;
id->flags[num + ID_AXIS_OFFSET][subnum] = flags;
id->port[num + ID_AXIS_OFFSET][subnum] = port;
xfree (id->custom[num + ID_AXIS_OFFSET][subnum]);
id->custom[num + ID_AXIS_OFFSET][subnum] = custom;
}
return true;
}
static struct inputevent *readevent (const TCHAR *name, TCHAR **customp)
{
int i = 1;
while (events[i].name) {
if (!_tcscmp (events[i].confname, name))
return &events[i];
i++;
}
if (_tcslen (name) > 2 && name[0] == '\'' && name[_tcslen (name) - 1] == '\'') {
TCHAR *custom = my_strdup (name + 1);
custom[_tcslen (custom) - 1] = 0;
*customp = custom;
}
return &events[0];
}
void read_inputdevice_config (struct uae_prefs *pr, const TCHAR *option, TCHAR *value)
{
struct uae_input_device *id = 0;
struct inputevent *ie;
int devnum, num, button, joystick, subnum, idnum, keynum;
const TCHAR *p;
TCHAR *p2, *custom;
option += 6; /* "input." */
p = getstring (&option);
if (!strcasecmp (p, _T("config"))) {
pr->input_selected_setting = _tstol (value) - 1;
if (pr->input_selected_setting == -1)
pr->input_selected_setting = GAMEPORT_INPUT_SETTINGS;
if (pr->input_selected_setting < 0 || pr->input_selected_setting > MAX_INPUT_SETTINGS)
pr->input_selected_setting = 0;
}
if (!strcasecmp (p, _T("joymouse_speed_analog")))
pr->input_joymouse_multiplier = _tstol (value);
if (!strcasecmp (p, _T("joymouse_speed_digital")))
pr->input_joymouse_speed = _tstol (value);
if (!strcasecmp (p, _T("joystick_deadzone")))
pr->input_joystick_deadzone = _tstol (value);
if (!strcasecmp (p, _T("joymouse_deadzone")))
pr->input_joymouse_deadzone = _tstol (value);
if (!strcasecmp (p, _T("mouse_speed")))
pr->input_mouse_speed = _tstol (value);
if (!strcasecmp (p, _T("autofire_speed")))
pr->input_autofire_linecnt = _tstol (value);
if (!strcasecmp (p, _T("analog_joystick_multiplier")))
pr->input_analog_joystick_mult = _tstol (value);
if (!strcasecmp (p, _T("analog_joystick_offset")))
pr->input_analog_joystick_offset = _tstol (value);
if (!strcasecmp (p, _T("keyboard_type"))) {
cfgfile_strval (option, value, NULL, &pr->input_analog_joystick_offset, kbtypes, 0);
keyboard_default = keyboard_default_table[pr->input_keyboard_type];
inputdevice_default_kb_all (pr);
}
if (!strcasecmp (p, _T("contact_bounce")))
pr->input_contact_bounce = _tstol (value);
idnum = _tstol (p);
if (idnum <= 0 || idnum > MAX_INPUT_SETTINGS)
return;
idnum--;
if (!_tcscmp (option, _T("name"))) {
if (idnum < GAMEPORT_INPUT_SETTINGS)
_tcscpy (pr->input_config_name[idnum], value);
return;
}
if (_tcsncmp (option, _T("mouse."), 6) == 0) {
p = option + 6;
} else if (_tcsncmp (option, _T("joystick."), 9) == 0) {
p = option + 9;
} else if (_tcsncmp (option, _T("keyboard."), 9) == 0) {
p = option + 9;
} else if (_tcsncmp (option, _T("internal."), 9) == 0) {
p = option + 9;
} else
return;
devnum = getnum (&p);
if (devnum < 0 || devnum >= MAX_INPUT_DEVICES)
return;
p2 = getstring (&p);
if (!p2)
return;
if (_tcsncmp (option, _T("mouse."), 6) == 0) {
id = &pr->mouse_settings[idnum][devnum];
joystick = 0;
} else if (_tcsncmp (option, _T("joystick."), 9) == 0) {
id = &pr->joystick_settings[idnum][devnum];
joystick = 1;
} else if (_tcsncmp (option, _T("keyboard."), 9) == 0) {
id = &pr->keyboard_settings[idnum][devnum];
joystick = -1;
} else if (_tcsncmp (option, _T("internal."), 9) == 0) {
if (devnum > 0)
return;
id = &pr->internalevent_settings[idnum][devnum];
joystick = 1;
}
if (!id)
return;
if (!_tcscmp (p2, _T("name"))) {
xfree (id->configname);
id->configname = my_strdup (value);
return;
}
if (!_tcscmp (p2, _T("friendlyname"))) {
xfree (id->name);
id->name = my_strdup (value);
return;
}
if (!_tcscmp (p2, _T("custom"))) {
int iscustom;
p = value;
iscustom = getnum (&p);
if (idnum == GAMEPORT_INPUT_SETTINGS) {
clear_id (id);
if (joystick < 0)
set_kbr_default (pr, idnum, devnum, keyboard_default);
id->enabled = iscustom;
} else {
id->enabled = false;
}
return;
}
if (!_tcscmp (p2, _T("empty"))) {
int empty;
p = value;
empty = getnum (&p);
clear_id (id);
if (!empty) {
if (joystick < 0)
set_kbr_default (pr, idnum, devnum, keyboard_default);
}
id->enabled = 1;
if (idnum == GAMEPORT_INPUT_SETTINGS)
id->enabled = 0;
return;
}
if (!_tcscmp (p2, _T("disabled"))) {
int disabled;
p = value;
disabled = getnum (&p);
id->enabled = disabled == 0 ? 1 : 0;
if (idnum == GAMEPORT_INPUT_SETTINGS)
id->enabled = 0;
return;
}
if (idnum == GAMEPORT_INPUT_SETTINGS && id->enabled == 0)
return;
button = 0;
keynum = 0;
if (joystick < 0) {
num = getnum (&p);
for (keynum = 0; keynum < MAX_INPUT_DEVICE_EVENTS; keynum++) {
if (id->extra[keynum] == num)
break;
}
if (keynum >= MAX_INPUT_DEVICE_EVENTS)