forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputdevice.cpp
5779 lines (5324 loc) · 149 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-2010 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 "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"
extern int bootrom_header, bootrom_items;
// 01 = host events
// 02 = joystick
// 04 = cia buttons
// 16 = potgo
// 32 = vsync
int inputdevice_logging = 0;
#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
/* event masks */
#define AM_KEY 1 /* keyboard allowed */
#define AM_JOY_BUT 2 /* joystick buttons allowed */
#define AM_JOY_AXIS 4 /* joystick axis allowed */
#define AM_MOUSE_BUT 8 /* mouse buttons allowed */
#define AM_MOUSE_AXIS 16 /* mouse direction allowed */
#define AM_AF 32 /* supports autofire */
#define AM_INFO 64 /* information data for gui */
#define AM_DUMMY 128 /* placeholder */
#define AM_CUSTOM 256 /* custom event */
#define AM_K (AM_KEY|AM_JOY_BUT|AM_MOUSE_BUT|AM_AF) /* generic button/switch */
#define JOYMOUSE_CDTV 8
#define DEFEVENT(A, B, C, D, E, F) {L#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];
struct uae_input_device2 {
uae_u32 buttonmask;
int states[MAX_INPUT_DEVICE_EVENTS / 2];
};
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];
// fire/left mouse button pullup resistors enabled?
static bool mouse_pullup = true;
static bool joystick_pullup = false;
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;
};
#define TESTMODE_MAX 2
static int testmode_count;
static struct teststore testmode_data[TESTMODE_MAX];
static struct teststore testmode_wait[TESTMODE_MAX];
static uae_u8 *inprec_buffer, *inprec_p;
static struct zfile *inprec_zf;
static int inprec_size;
int input_recording = 0;
static uae_u8 *inprec_plast, *inprec_plastptr;
static int inprec_div;
static uae_u32 oldbuttons[4];
static uae_u16 oldjoy[2];
static int bouncy;
static signed long bouncy_cycles;
#define BOUNCY_CYCLES 30
int inprec_open (const TCHAR *fname, int record)
{
uae_u32 t = (uae_u32)time(0);
int i;
inprec_close();
inprec_zf = zfile_fopen (fname, record > 0 ? L"wb" : L"rb", ZFD_NORMAL);
if (inprec_zf == NULL)
return 0;
inprec_size = 10000;
inprec_div = 1;
if (record < 0) {
uae_u32 id;
zfile_fseek (inprec_zf, 0, SEEK_END);
inprec_size = zfile_ftell (inprec_zf);
zfile_fseek (inprec_zf, 0, SEEK_SET);
inprec_buffer = inprec_p = xmalloc (uae_u8, inprec_size);
zfile_fread (inprec_buffer, inprec_size, 1, inprec_zf);
inprec_plastptr = inprec_buffer;
id = inprec_pu32();
if (id != 'UAE\0') {
inprec_close ();
return 0;
}
inprec_pu32();
t = inprec_pu32 ();
i = inprec_pu32 ();
while (i-- > 0)
inprec_pu8 ();
inprec_p = inprec_plastptr;
if (inprec_pstart (INPREC_STATEFILE)) {
inprec_pstr (savestate_fname);
savestate_state = STATE_RESTORE;
inprec_pend ();
}
oldbuttons[0] = oldbuttons[1] = oldbuttons[2] = oldbuttons[3] = 0;
oldjoy[0] = oldjoy[1] = 0;
if (record < -1)
inprec_div = maxvpos;
} else if (record > 0) {
inprec_buffer = inprec_p = xmalloc (uae_u8, inprec_size);
inprec_ru32 ('UAE\0');
inprec_ru8 (1);
inprec_ru8 (UAEMAJOR);
inprec_ru8 (UAEMINOR);
inprec_ru8 (UAESUBREV);
inprec_ru32 (t);
inprec_ru32 (0); // extra header size
if (savestate_state == STATE_DORESTORE) {
inprec_rstart (INPREC_STATEFILE);
inprec_rstr (savestate_fname);
inprec_rend ();
}
} else {
return 0;
}
input_recording = record;
srand (t);
CIA_inprec_prepare ();
write_log (L"inprec initialized '%s', mode=%d\n", fname, input_recording);
return 1;
}
void inprec_close (void)
{
if (!inprec_zf)
return;
if (inprec_buffer && input_recording > 0) {
hsync_counter++;
inprec_rstart(INPREC_END);
inprec_rend();
hsync_counter--;
zfile_fwrite (inprec_buffer, inprec_p - inprec_buffer, 1, inprec_zf);
inprec_p = inprec_buffer;
}
zfile_fclose (inprec_zf);
inprec_zf = NULL;
xfree (inprec_buffer);
inprec_buffer = NULL;
input_recording = 0;
write_log (L"inprec finished\n");
}
void inprec_ru8(uae_u8 v)
{
*inprec_p++= v;
}
void inprec_ru16 (uae_u16 v)
{
inprec_ru8 ((uae_u8)(v >> 8));
inprec_ru8 ((uae_u8)v);
}
void inprec_ru32 (uae_u32 v)
{
inprec_ru16 ((uae_u16)(v >> 16));
inprec_ru16 ((uae_u16)v);
}
void inprec_rstr (const TCHAR *src)
{
char *s = ua (src);
char *ss = s;
while(*s) {
inprec_ru8 (*s);
s++;
}
inprec_ru8 (0);
xfree (ss);
}
void inprec_rstart (uae_u8 type)
{
write_log (L"INPREC: %08X: %d\n", hsync_counter, type);
inprec_ru32 (hsync_counter);
inprec_ru8 (0);
inprec_plast = inprec_p;
inprec_ru8 (0xff);
inprec_ru8 (type);
}
void inprec_rend (void)
{
*inprec_plast = inprec_p - (inprec_plast + 2);
if (inprec_p >= inprec_buffer + inprec_size - 256) {
zfile_fwrite (inprec_buffer, inprec_p - inprec_buffer, 1, inprec_zf);
inprec_p = inprec_buffer;
}
}
int inprec_pstart (uae_u8 type)
{
uae_u8 *p = inprec_p;
uae_u32 hc = hsync_counter;
static uae_u8 *lastp;
uae_u32 hc_orig, hc2_orig;
if (savestate_state)
return 0;
if (p[5 + 1] == INPREC_END) {
inprec_close ();
return 0;
} else if (p[5 + 1] == INPREC_QUIT) {
inprec_close ();
uae_quit ();
return 0;
}
hc_orig = hc;
hc /= inprec_div;
hc *= inprec_div;
for (;;) {
uae_u32 hc2 = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
if (p > lastp) {
write_log (L"INPREC: Next %08x (%08x=%d): %d (%d)\n", hc2, hc, hc2 - hc, p[5 + 1], p[5]);
lastp = p;
}
hc2_orig = hc2;
hc2 /= inprec_div;
hc2 *= inprec_div;
if (hc > hc2) {
write_log (L"INPREC: %08x > %08x: %d (%d) missed!\n", hc, hc2, p[5 + 1], p[5]);
inprec_close ();
return 0;
}
if (hc2 != hc) {
lastp = p;
break;
}
if (p[5 + 1] == type) {
write_log (L"INPREC: %08x: %d (%d) (%+d)\n", hc, type, p[5], hc_orig - hc2_orig);
inprec_plast = p;
inprec_plastptr = p + 5 + 2;
return 1;
}
p += 5 + 2 + p[5];
}
inprec_plast = NULL;
return 0;
}
void inprec_pend (void)
{
uae_u8 *p = inprec_p;
uae_u32 hc = hsync_counter;
if (!inprec_plast)
return;
inprec_plast[5 + 1] = 0;
inprec_plast = NULL;
inprec_plastptr = NULL;
hc /= inprec_div;
hc *= inprec_div;
for (;;) {
uae_u32 hc2 = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
hc2 /= inprec_div;
hc2 *= inprec_div;
if (hc2 != hc)
break;
if (p[5 + 1] != 0)
return;
p += 5 + 2 + p[5];
}
inprec_p = p;
if (p[5 + 1] == INPREC_END)
inprec_close ();
}
uae_u8 inprec_pu8 (void)
{
return *inprec_plastptr++;
}
uae_u16 inprec_pu16 (void)
{
uae_u16 v = inprec_pu8 () << 8;
v |= inprec_pu8 ();
return v;
}
uae_u32 inprec_pu32 (void)
{
uae_u32 v = inprec_pu16 () << 16;
v |= inprec_pu16 ();
return v;
}
int inprec_pstr (TCHAR *dst)
{
char tmp[MAX_DPATH];
char *s;
int len = 0;
s = tmp;
for(;;) {
uae_u8 v = inprec_pu8 ();
*s++ = v;
if (!v)
break;
len++;
}
au_copy (dst, MAX_DPATH, tmp);
return len;
}
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 (TCHAR *s, TCHAR *parm)
{
int i;
for (i = 1; events[i].name; i++) {
if (!_tcscmp (s, events[i].confname)) {
handle_input_event (i, _tstol (parm), 1, 0);
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_kbr_default *keyboard_default;
#define KBR_DEFAULT_MAP_NP 0
#define KBR_DEFAULT_MAP_CK 1
#define KBR_DEFAULT_MAP_SE 2
#define KBR_DEFAULT_MAP_CD32_NP 3
#define KBR_DEFAULT_MAP_CD32_CK 4
#define KBR_DEFAULT_MAP_CD32_SE 5
#define KBR_DEFAULT_MAP_XA1 6
#define KBR_DEFAULT_MAP_XA2 7
#define KBR_DEFAULT_MAP_ARCADIA 8
#define KBR_DEFAULT_MAP_ARCADIA_XA 9
#define KBR_DEFAULT_MAP_CDTV 10
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];
static int mouse_x[MAX_INPUT_DEVICES], mouse_y[MAX_INPUT_DEVICE_EVENTS];
static int mouse_delta[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
static int mouse_deltanoreset[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
static int joybutton[MAX_INPUT_DEVICES];
static unsigned int joydir[MAX_INPUT_DEVICE_EVENTS];
static int joydirpot[MAX_INPUT_DEVICE_EVENTS][2];
static int mouse_frame_x[2], mouse_frame_y[2];
static int mouse_port[2];
static int cd32_shifter[2];
static int cd32_pad_enabled[2];
static int parport_joystick_enabled;
static int oldmx[4], oldmy[4];
static int oleft[4], oright[4], otop[4], obot[4];
uae_u16 potgo_value;
static int pot_cap[2][2];
static uae_u8 pot_dat[2][2];
static int pot_dat_act[2][2];
static int analog_port[2][2];
static int digital_port[2][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 event, storedstate, state, max, linecnt, nextlinecnt;
};
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)
{
memset (&dst->jports[num], 0, sizeof (struct jport));
dst->jports[num].id = -1;
}
static void copyjport (const struct uae_prefs *src, struct uae_prefs *dst, int num)
{
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;
}
static void out_config (struct zfile *f, int id, int num, TCHAR *s1, TCHAR *s2)
{
TCHAR tmp[MAX_DPATH];
_stprintf (tmp, L"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, TCHAR *name, struct uae_input_device *id, struct inputdevice_functions *idf)
{
TCHAR tmp2[MAX_DPATH];
if (idnum == GAMEPORT_INPUT_SETTINGS) {
if (!isdevice (id))
return false;
if (!id->enabled)
return false;
}
TCHAR *s = NULL;
if (id->name)
s = id->name;
else if (devnum < idf->get_num ())
s = idf->get_friendlyname (devnum);
if (s) {
_stprintf (tmp2, L"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, L"input.%d.%s.%d.name", idnum + 1, name, devnum);
cfgfile_write_str (f, tmp2, s);
}
if (!isdevice (id)) {
_stprintf (tmp2, L"input.%d.%s.%d.empty", idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, true);
if (id->enabled) {
_stprintf (tmp2, L"input.%d.%s.%d.disabled", idnum + 1, name, devnum);
cfgfile_write (f, tmp2, L"%d", id->enabled ? 0 : 1);
}
return false;
}
if (idnum == GAMEPORT_INPUT_SETTINGS) {
_stprintf (tmp2, L"input.%d.%s.%d.custom", idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, true);
} else {
_stprintf (tmp2, L"input.%d.%s.%d.empty", idnum + 1, name, devnum);
cfgfile_write_bool (f, tmp2, false);
_stprintf (tmp2, L"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 (uid->custom[i][j] && _tcslen (uid->custom[i][j]) > 0) {
_stprintf (p, L"'%s'.%d", uid->custom[i][j], uid->flags[i][j] & ID_FLAG_SAVE_MASK);
ok = true;
} else if (uid->eventid[i][j] > 0) {
_stprintf (p, L"%s.%d", events[uid->eventid[i][j]].confname, uid->flags[i][j] & ID_FLAG_SAVE_MASK);
ok = true;
} else {
_tcscat (p, L"NULL");
}
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, TCHAR *tmp1, struct uae_input_device *id)
{
TCHAR tmp2[200], tmp3[200], *p;
int evt, got, j, k;
TCHAR *custom;
int slotorder1[] = { 0, 1, 2, 3 };
int slotorder2[] = { 4, 1, 2, 3 };
int *slotorder;
int io = i + offset;
p = tmp2;
got = 0;
slotorder = slotorder1;
// if gameports non-custom mapping in slot0 -> save slot4 as slot0
if (id->port[io][0] && !(id->flags[io][0] & ID_FLAG_GAMEPORTSCUSTOM))
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->eventid[io][slotorder[k]] > 0 || id->custom[io][slotorder[k]] != NULL)
break;
}
if (k == MAX_INPUT_SUB_EVENT)
break;
}
if (id->port[io][0] > 0) {
if (!(id->flags[io][0] & ID_FLAG_GAMEPORTSCUSTOM) && id->port[io][SPARE_SUB_EVENT] == 0)
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) {
_stprintf (p, L".%d", id->port[io][slotorder[j]] - 1);
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, L"input.%d.%s%d", idnum + 1, tmp1, 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[200], tmp2[200], tmp3[200], tmp4[200], tmp5[200], *p;
int i, j, k, evt, skip;
int slotorder1[] = { 0, 1, 2, 3 };
int slotorder2[] = { 4, 1, 2, 3 };
int *slotorder;
if (!keyboard_default)
return;
if (!write_config_head (f, idnum, devnum, L"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))
slotorder = slotorder2;
skip = 0;
k = 0;
while (keyboard_default[k].scancode >= 0) {
if (keyboard_default[k].scancode == kbr->extra[i]) {
skip = 1;
for (j = 1; j < MAX_INPUT_SUB_EVENT; j++) {
if ((kbr->flags[i][slotorder[j]] & ID_FLAG_SAVE_MASK) != 0 || kbr->eventid[i][slotorder[j]] > 0)
skip = 0;
}
if (keyboard_default[k].evt != kbr->eventid[i][slotorder[0]] || keyboard_default[k].flags != (kbr->flags[i][slotorder[0]] & ID_FLAG_SAVE_MASK))
skip = 0;
break;
}
k++;
}
bool isdefaultspare =
kbr->port[i][SPARE_SUB_EVENT] &&
keyboard_default[k].evt == kbr->eventid[i][SPARE_SUB_EVENT] && keyboard_default[k].flags == (kbr->flags[i][SPARE_SUB_EVENT] & ID_FLAG_SAVE_MASK);
if (kbr->port[i][0] > 0 && !(kbr->flags[i][0] & ID_FLAG_GAMEPORTSCUSTOM) &&
(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) == 0 && keyboard_default[k].scancode < 0)
skip = 1;
if (skip) {
i++;
continue;
}
p = tmp2;
p[0] = 0;
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)) {
_stprintf (p, L".%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, L"%d%s%s", kbr->extra[i], tmp5[0] ? L"." : L"", tmp5[0] ? tmp5 : L"");
kbrlabel (tmp3);
_stprintf (tmp1, L"keyboard.%d.button.%s", devnum, tmp3);
_stprintf (tmp4, L"input.%d.%s", idnum + 1, tmp1);
cfgfile_write_str (f, tmp4, tmp2[0] ? tmp2 : L"NULL");
i++;
}
}
static void write_config (struct zfile *f, int idnum, int devnum, TCHAR *name, struct uae_input_device *id, struct uae_input_device2 *id2, struct inputdevice_functions *idf)
{
TCHAR tmp1[MAX_DPATH];
int i;
if (!write_config_head (f, idnum, devnum, name, id, idf))
return;
_stprintf (tmp1, L"%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, L"%s.%d.button." ,name, devnum);
for (i = 0; i < ID_BUTTON_TOTAL; i++)
write_config2 (f, idnum, i, ID_BUTTON_OFFSET, tmp1, id);
}
void write_inputdevice_config (struct uae_prefs *p, struct zfile *f)
{
int i, id;
cfgfile_write (f, L"input.config", L"%d", p->input_selected_setting == GAMEPORT_INPUT_SETTINGS ? 0 : p->input_selected_setting + 1);
cfgfile_write (f, L"input.joymouse_speed_analog", L"%d", p->input_joymouse_multiplier);
cfgfile_write (f, L"input.joymouse_speed_digital", L"%d", p->input_joymouse_speed);
cfgfile_write (f, L"input.joymouse_deadzone", L"%d", p->input_joymouse_deadzone);
cfgfile_write (f, L"input.joystick_deadzone", L"%d", p->input_joystick_deadzone);
cfgfile_write (f, L"input.analog_joystick_multiplier", L"%d", p->input_analog_joystick_mult);
cfgfile_write (f, L"input.analog_joystick_offset", L"%d", p->input_analog_joystick_offset);
cfgfile_write (f, L"input.mouse_speed", L"%d", p->input_mouse_speed);
cfgfile_write (f, L"input.autofire_speed", L"%d", p->input_autofire_linecnt);
for (id = 0; id < MAX_INPUT_SETTINGS; id++) {
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_config (f, id, i, L"joystick", &p->joystick_settings[id][i], &joysticks2[i], &idev[IDTYPE_JOYSTICK]);
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_config (f, id, i, L"mouse", &p->mouse_settings[id][i], &mice2[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]);
}
}
static int getnum (const TCHAR **pp)
{
const TCHAR *p = *pp;
int v;
if (!_tcsnicmp (p, L"false", 5))
v = 0;
if (!_tcsnicmp (p, L"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[1000];
const TCHAR *p = *pp;
if (*p == 0)
return 0;
i = 0;
while (*p != 0 && *p !='.' && *p != ',' && i < 1000 - 1)
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; i++) {
uid->eventid[l][i] = 0;
uid->flags[l][i] = 0;
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 (L"corrupt default keyboard mappings\n");
return;
}
kbr->eventid[num][k] = trans[i].evt;
kbr->flags[num][k] = trans[i].flags;
break;
}
}
}
static void set_kbr_default (struct uae_prefs *p, int index, int devnum)
{
int i, j;
struct uae_input_device_kbr_default *trans = keyboard_default;
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];
for (i = 0; i < MAX_INPUT_DEVICE_EVENTS; i++) {
memset (kbr, 0, sizeof (struct uae_input_device));
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 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; 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 bool readslot (TCHAR *parm, int num, int joystick, int button, struct uae_input_device *id, int keynum, int subnum, struct inputevent *ie, int flags, int port, TCHAR *custom)
{
int mask;
if (custom == NULL && ie->name == NULL) {
if (!_tcscmp (parm, L"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 (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, L"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, L"joymouse_speed_analog"))
pr->input_joymouse_multiplier = _tstol (value);
if (!strcasecmp (p, L"joymouse_speed_digital"))
pr->input_joymouse_speed = _tstol (value);
if (!strcasecmp (p, L"joystick_deadzone"))
pr->input_joystick_deadzone = _tstol (value);
if (!strcasecmp (p, L"joymouse_deadzone"))
pr->input_joymouse_deadzone = _tstol (value);