-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputdevice.c
executable file
·2357 lines (2153 loc) · 62.8 KB
/
inputdevice.c
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, 2002 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 "keybuf.h"
#include "custom.h"
#include "xwin.h"
#include "drawing.h"
#include "memory.h"
#include "events.h"
#include "newcpu.h"
#include "traps.h"
#include "inputdevice.h"
#include "uae.h"
#include "debug.h"
#include "ar.h"
#include "gui.h"
#include "disk.h"
#include "gensound.h"
#include "savestate.h"
#include <ctype.h>
//ric
#include "profiling.h"
#define PROFILINGINDEX 600
//#undef PROFILING
//end ric
#define DIR_LEFT 1
#define DIR_RIGHT 2
#define DIR_UP 4
#define DIR_DOWN 8
struct inputevent {
const char *confname;
const char *name;
int allow_mask;
int type;
int unit;
int data;
};
#define JOYBUTTON_1 0 /* fire/left mousebutton */
#define JOYBUTTON_2 1 /* 2nd/right mousebutton */
#define JOYBUTTON_3 2 /* 3rd/middle mousebutton */
#define JOYBUTTON_CD32_PLAY 3
#define JOYBUTTON_CD32_RWD 4
#define JOYBUTTON_CD32_FFW 5
#define JOYBUTTON_CD32_GREEN 6
#define JOYBUTTON_CD32_YELLOW 7
#define JOYBUTTON_CD32_RED 8
#define JOYBUTTON_CD32_BLUE 9
#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 (note - IS NOT the # of buttons!!!) */
#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_K (AM_KEY|AM_JOY_BUT|AM_MOUSE_BUT|AM_AF) /* generic button/switch */
/* event flags */
#define ID_FLAG_AUTOFIRE 1
#define DEFEVENT(A, B, C, D, E, F) {#A, B, C, D, E, F },
const struct inputevent events[] = {
{0, 0, AM_K, 0, 0, 0},
#include "inputevents.def"
{0, 0, 0, 0, 0, 0}
};
#undef DEFEVENT
static struct inputdevice_functions idev[3];
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 mouse_settings_reset[MAX_INPUT_SETTINGS][MAX_INPUT_DEVICES];
static uae_u8 joystick_settings_reset[MAX_INPUT_SETTINGS][MAX_INPUT_DEVICES];
static int isdevice (const 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 char *s, const char *parm)
{
int i;
for (i = 1; events[i].name; i++) {
if (!strcmp (s, events[i].confname)) {
handle_input_event (i, atol (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;
static double mouse_axis[MAX_INPUT_DEVICES][MAX_INPUT_DEVICE_EVENTS];
static double 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];
static int potgo_hsync;
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, framecnt, nextframecnt;
};
static struct input_queue_struct input_queue[INPUT_QUEUE_SIZE];
static void out_config (FILE *f, int id, int num, const char *s1, const char *s2)
{
cfgfile_write (f, "input.%d.%s%d=%s\n", id, s1, num, s2);
}
static void write_config2 (FILE *f, int idnum, int i, int offset, const char *tmp1, const struct uae_input_device *id)
{
char tmp2[200], *p;
int event, got, j, k;
const char *custom;
p = tmp2;
got = 0;
for (j = 0; j < MAX_INPUT_SUB_EVENT; j++) {
event = id->eventid[i + offset][j];
custom = id->custom[i + offset][j];
if (custom == NULL && event <= 0) {
for (k = j + 1; k < MAX_INPUT_SUB_EVENT; k++) {
if (id->eventid[i + offset][k] > 0) break;
}
if (k == MAX_INPUT_SUB_EVENT)
break;
}
if (p > tmp2) {
*p++ = ',';
*p = 0;
}
if (custom)
sprintf (p, "'%s'.%d", custom, id->flags[i + offset][j]);
else if (event <= 0)
sprintf (p, "NULL");
else
sprintf (p, "%s.%d", events[event].confname, id->flags[i + offset][j]);
p += strlen (p);
}
if (p > tmp2)
out_config (f, idnum, i, tmp1, tmp2);
}
static void write_config (FILE *f, int idnum, int devnum, const char *name, const struct uae_input_device *id, const struct uae_input_device2 *id2)
{
char tmp1[100];
int i;
if (!isdevice (id))
return;
cfgfile_write (f, "input.%d.%s.%d.disabled=%d\n", idnum, name, devnum, id->enabled ? 0 : 1);
sprintf (tmp1, "%s.%d.axis.", name, devnum);
for (i = 0; i < ID_AXIS_TOTAL; i++)
write_config2 (f, idnum, i, ID_AXIS_OFFSET, tmp1, id);
sprintf (tmp1, "%s.%d.button." ,name, devnum);
for (i = 0; i < ID_BUTTON_TOTAL; i++)
write_config2 (f, idnum, i, ID_BUTTON_OFFSET, tmp1, id);
}
static void kbrlabel (char *s)
{
while (*s) {
*s = toupper(*s);
if (*s == ' ') *s = '_';
s++;
}
}
static void write_kbr_config (FILE *f, int idnum, int devnum, const struct uae_input_device *kbr)
{
char tmp1[200], tmp2[200], tmp3[200], *p;
int i, j, k, event, skip;
if (!keyboard_default)
return;
i = 0;
while (i < MAX_INPUT_DEVICE_EVENTS && kbr->extra[i][0] >= 0) {
skip = 0;
k = 0;
while (keyboard_default[k].scancode >= 0) {
if (keyboard_default[k].scancode == kbr->extra[i][0]) {
skip = 1;
for (j = 1; j < MAX_INPUT_SUB_EVENT; j++) {
if (kbr->flags[i][j] || kbr->eventid[i][j] > 0)
skip = 0;
}
if (keyboard_default[k].event != kbr->eventid[i][0] || kbr->flags[i][0] != 0)
skip = 0;
break;
}
k++;
}
if (kbr->eventid[i][0] == 0 && kbr->flags[i][0] == 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++) {
const char *custom = kbr->custom[i][j];
event = kbr->eventid[i][j];
if (custom == NULL && event <= 0) {
for (k = j + 1; k < MAX_INPUT_SUB_EVENT; k++) {
if (kbr->eventid[i][k] > 0) break;
}
if (k == MAX_INPUT_SUB_EVENT)
break;
}
if (p > tmp2) {
*p++ = ',';
*p = 0;
}
if (custom)
sprintf (p, "'%s'.%d", custom, kbr->flags[i][j]);
else if (event > 0)
sprintf (p, "%s.%d", events[event].confname, kbr->flags[i][j]);
else
strcat (p, "NULL");
p += strlen(p);
}
sprintf (tmp3, "%d", kbr->extra[i][0]);
kbrlabel (tmp3);
sprintf (tmp1, "keyboard.%d.button.%s", devnum, tmp3);
cfgfile_write (f, "input.%d.%s=%s\n", idnum, tmp1, tmp2);
i++;
}
}
void write_inputdevice_config (const struct uae_prefs *p, FILE *f)
{
int i, id;
cfgfile_write (f, "input.config=%d\n", p->input_selected_setting);
cfgfile_write (f, "input.joymouse_speed_analog=%d\n", p->input_joymouse_multiplier);
cfgfile_write (f, "input.joymouse_speed_digital=%d\n", p->input_joymouse_speed);
cfgfile_write (f, "input.joymouse_deadzone=%d\n", p->input_joymouse_deadzone);
cfgfile_write (f, "input.joystick_deadzone=%d\n", p->input_joystick_deadzone);
cfgfile_write (f, "input.mouse_speed=%d\n", p->input_mouse_speed);
cfgfile_write (f, "input.autofire=%d\n", p->input_autofire_framecnt);
for (id = 1; id <= MAX_INPUT_SETTINGS; id++) {
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_config (f, id, i, "joystick", &p->joystick_settings[id][i], &joysticks2[i]);
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_config (f, id, i, "mouse", &p->mouse_settings[id][i], &mice2[i]);
for (i = 0; i < MAX_INPUT_DEVICES; i++)
write_kbr_config (f, id, i, &p->keyboard_settings[id][i]);
}
}
static int getnum (const char **pp)
{
const char *p = *pp;
int v = atol (p);
while (*p != 0 && *p !='.' && *p != ',') p++;
if (*p == '.' || *p == ',') p++;
*pp = p;
return v;
}
static char *getstring (const char **pp)
{
int i;
static char str[1000];
const char *p = *pp;
if (*p == 0)
return 0;
i = 0;
while (*p != 0 && *p !='.' && *p != ',')
str[i++] = *p++;
if (*p == '.' || *p == ',')
p++;
str[i] = 0;
*pp = p;
return str;
}
void reset_inputdevice_config (struct uae_prefs *pr)
{
memset (joystick_settings_reset, 0, sizeof (joystick_settings_reset));
memset (mouse_settings_reset, 0, sizeof (mouse_settings_reset));
}
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++)
free (id->custom[i][j]);
}
#endif
memset (id, 0, sizeof (struct uae_input_device));
id->enabled = 1;
}
void read_inputdevice_config (struct uae_prefs *pr, const char *option, const char *value)
{
struct uae_input_device *id = 0;
const struct inputevent *ie;
int devnum, num, button = 0, joystick, flags, i, subnum, idnum, keynum = 0;
int mask;
const char *p, *p2;
char *custom;
option += 6; /* "input." */
p = getstring (&option);
if (!strcasecmp (p, "config"))
pr->input_selected_setting = atol (value);
if (!strcasecmp (p, "joymouse_speed_analog"))
pr->input_joymouse_multiplier = atol (value);
if (!strcasecmp (p, "joymouse_speed_digital"))
pr->input_joymouse_speed = atol (value);
if (!strcasecmp (p, "joystick_deadzone"))
pr->input_joystick_deadzone = atol (value);
if (!strcasecmp (p, "joymouse_deadzone"))
pr->input_joymouse_deadzone = atol (value);
if (!strcasecmp (p, "mouse_speed"))
pr->input_mouse_speed = atol (value);
if (!strcasecmp (p, "autofire"))
pr->input_autofire_framecnt = atol (value);
idnum = atol (p);
if (idnum <= 0 || idnum > MAX_INPUT_SETTINGS)
return;
if (memcmp (option, "mouse.", 6) == 0) {
p = option + 6;
devnum = getnum (&p);
if (devnum < 0 || devnum >= MAX_INPUT_DEVICES)
return;
id = &pr->mouse_settings[idnum][devnum];
if (!mouse_settings_reset[idnum][devnum])
clear_id (id);
mouse_settings_reset[idnum][devnum] = 1;
joystick = 0;
} else if (memcmp (option, "joystick.", 9) == 0) {
p = option + 9;
devnum = getnum (&p);
if (devnum < 0 || devnum >= MAX_INPUT_DEVICES)
return;
id = &pr->joystick_settings[idnum][devnum];
if (!joystick_settings_reset[idnum][devnum])
clear_id (id);
joystick_settings_reset[idnum][devnum] = 1;
joystick = 1;
} else if (memcmp (option, "keyboard.", 9) == 0) {
joystick = -1;
p = option + 9;
devnum = getnum (&p);
if (devnum < 0 || devnum >= MAX_INPUT_DEVICES)
return;
id = &pr->keyboard_settings[idnum][devnum];
}
if (!id)
return;
p2 = getstring (&p);
if (!p2)
return;
if (!strcmp (p2, "disabled")) {
int disabled;
p = value;
disabled = getnum (&p);
id->enabled = disabled == 0 ? 1 : 0;
return;
}
if (joystick < 0) {
num = getnum (&p);
keynum = 0;
while (id->extra[keynum][0] >= 0) {
if (id->extra[keynum][0] == num)
break;
keynum++;
}
if (id->extra[keynum][0] < 0)
return;
} else {
button = -1;
if (!strcmp (p2, "axis"))
button = 0;
else if(!strcmp (p2, "button"))
button = 1;
if (button < 0)
return;
num = getnum (&p);
}
p = value;
custom = NULL;
for (subnum = 0; subnum < MAX_INPUT_SUB_EVENT; subnum++) {
free (custom);
custom = NULL;
p2 = getstring (&p);
if (!p2)
break;
i = 1;
while (events[i].name) {
if (!strcmp (events[i].confname, p2))
break;
i++;
}
ie = &events[i];
if (!ie->name) {
ie = &events[0];
if (strlen (p2) > 2 && p2[0] == '\'' && p2[strlen (p2) - 1] == '\'') {
custom = my_strdup (p2 + 1);
custom[strlen (custom) - 1] = 0;
}
}
flags = getnum (&p);
if (custom == NULL && ie->name == NULL)
continue;
if (joystick < 0) {
if (!(ie->allow_mask & AM_K))
continue;
id->eventid[keynum][subnum] = ie - events;
id->flags[keynum][subnum] = flags;
free (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))
continue;
id->eventid[num + ID_BUTTON_OFFSET][subnum] = ie - events;
id->flags[num + ID_BUTTON_OFFSET][subnum] = flags;
free (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))
continue;
id->eventid[num + ID_AXIS_OFFSET][subnum] = ie - events;
id->flags[num + ID_AXIS_OFFSET][subnum] = flags;
free (id->custom[num + ID_AXIS_OFFSET][subnum]);
id->custom[num + ID_AXIS_OFFSET][subnum] = custom;
}
custom = NULL;
}
free (custom);
}
/* Mousehack stuff */
static int ievent_alive = 0;
static int lastmx, lastmy;
int mousehack_alive (void)
{
return ievent_alive > 0;
}
/* FIXME */
extern uae_u32 REGPARAM2 mousehack_helper (struct TrapContext *);
uae_u32 REGPARAM2 mousehack_helper (TrapContext *context)
{
int mousexpos, mouseypos;
#ifdef PICASSO96
if (picasso_on) {
mousexpos = lastmx - picasso96_state.XOffset;
mouseypos = lastmy - picasso96_state.YOffset;
} else
#endif
{
if (mouse_y[0] >= gfxvidinfo.height)
mouse_y[0] = gfxvidinfo.height - 1;
mouseypos = coord_native_to_amiga_y (lastmy) << 1;
mousexpos = coord_native_to_amiga_x (lastmx);
}
switch (m68k_dreg (&context->regs, 0)) {
case 0:
return ievent_alive ? -1 : needmousehack ();
case 1:
ievent_alive = 10;
if (!mousehack_allowed ())
return 0;
return mousexpos;
case 2:
if (!mousehack_allowed ())
return 0;
return mouseypos;
}
return 0;
}
STATIC_INLINE int adjust (int val)
{
if (val > 127)
return 127;
else if (val < -127)
return -127;
return val;
}
int getbuttonstate (int joy, int button)
{
return joybutton[joy] & (1 << button);
}
static void mouseupdate (int pct)
{
int v, i;
for (i = 0; i < 2; i++) {
v = mouse_delta[i][0] * pct / 100;
mouse_x[i] += v;
if (!mouse_deltanoreset[i][0])
mouse_delta[i][0] -= v;
v = mouse_delta[i][1] * pct / 100;
mouse_y[i] += v;
if (!mouse_deltanoreset[i][1])
mouse_delta[i][1] -= v;
v = mouse_delta[i][2] * pct / 100;
if (v > 0)
record_key (0x7a << 1);
else if (v < 0)
record_key (0x7b << 1);
if (!mouse_deltanoreset[i][2])
mouse_delta[i][2] = 0;
if (mouse_frame_x[i] - mouse_x[i] > 127)
mouse_x[i] = mouse_frame_x[i] - 127;
if (mouse_frame_x[i] - mouse_x[i] < -127)
mouse_x[i] = mouse_frame_x[i] + 127;
if (mouse_frame_y[i] - mouse_y[i] > 127)
mouse_y[i] = mouse_frame_y[i] - 127;
if (mouse_frame_y[i] - mouse_y[i] < -127)
mouse_y[i] = mouse_frame_y[i] + 127;
if (pct == 100) {
if (!mouse_deltanoreset[i][0])
mouse_delta[i][0] = 0;
if (!mouse_deltanoreset[i][1])
mouse_delta[i][1] = 0;
if (!mouse_deltanoreset[i][2])
mouse_delta[i][2] = 0;
mouse_frame_x[i] = mouse_x[i];
mouse_frame_y[i] = mouse_y[i];
}
}
}
static unsigned int input_read, input_vpos;
static void readinput (void)
{
if (!input_read && (vpos & ~31) != (input_vpos & ~31)) {
idev[IDTYPE_JOYSTICK].read ();
idev[IDTYPE_MOUSE].read ();
mouseupdate ((vpos - input_vpos) * 100 / maxvpos);
input_vpos = vpos;
}
if (input_read) {
input_vpos = vpos;
input_read = 0;
}
}
int getjoystate (int joy)
{
int left = 0, right = 0, top = 0, bot = 0;
uae_u16 v = 0;
readinput ();
if (joydir[joy] & DIR_LEFT)
left = 1;
if (joydir[joy] & DIR_RIGHT)
right = 1;
if (joydir[joy] & DIR_UP)
top = 1;
if (joydir[joy] & DIR_DOWN)
bot = 1;
v = (uae_u8)mouse_x[joy] | (mouse_y[joy] << 8);
if (left || right || top || bot || !mouse_port[joy]) {
if (left)
top = !top;
if (right)
bot = !bot;
v &= ~0x0303;
v |= bot | (right << 1) | (top << 8) | (left << 9);
}
#ifdef DONGLE_DEBUG
if (notinrom ())
write_log ("JOY%dDAT %04.4X %s\n", joy, v, debuginfo (0));
#endif
return v;
}
uae_u16 JOY0DAT (void)
{
return getjoystate (0);
}
uae_u16 JOY1DAT (void)
{
return getjoystate (1);
}
void JOYTEST (uae_u16 v)
{
mouse_x[0] &= 3;
mouse_y[0] &= 3;
mouse_x[1] &= 3;
mouse_y[1] &= 3;
mouse_x[0] |= v & 0xFC;
mouse_x[1] |= v & 0xFC;
mouse_y[0] |= (v >> 8) & 0xFC;
mouse_y[1] |= (v >> 8) & 0xFC;
mouse_frame_x[0] = mouse_x[0];
mouse_frame_y[0] = mouse_y[0];
mouse_frame_x[1] = mouse_x[1];
mouse_frame_y[1] = mouse_y[1];
// write_log ("%d:%04.4X %p\n",vpos,v,m68k_getpc());
}
static uae_u8 parconvert (uae_u8 v, int jd, int shift)
{
if (jd & DIR_UP)
v &= ~(1 << shift);
if (jd & DIR_DOWN)
v &= ~(2 << shift);
if (jd & DIR_LEFT)
v &= ~(4 << shift);
if (jd & DIR_RIGHT)
v &= ~(8 << shift);
return v;
}
/* io-pins floating: dir=1 -> return data, dir=0 -> always return 1 */
uae_u8 handle_parport_joystick (int port, uae_u8 pra, uae_u8 dra)
{
uae_u8 v;
switch (port) {
case 0:
v = (pra & dra) | (dra ^ 0xff);
if (parport_joystick_enabled) {
v = parconvert (v, joydir[2], 0);
v = parconvert (v, joydir[3], 4);
}
return v;
case 1:
v = ((pra & dra) | (dra ^ 0xff)) & 0x7;
if (parport_joystick_enabled) {
if (getbuttonstate (2, 0)) v &= ~1;
if (getbuttonstate (3, 0)) v &= ~4;
}
return v;
default:
abort ();
return 0;
}
}
uae_u8 handle_joystick_buttons (uae_u8 dra)
{
uae_u8 but = 0;
int i;
for (i = 0; i < 2; i++) {
if (cd32_pad_enabled[i]) {
uae_u16 p5dir = 0x0200 << (i * 4);
uae_u16 p5dat = 0x0100 << (i * 4);
but |= 0x40 << i;
/* Red button is connected to fire when p5 is 1 or floating */
if (!(potgo_value & p5dir) || ((potgo_value & p5dat) && (potgo_value & p5dir))) {
if (getbuttonstate (i, JOYBUTTON_CD32_RED))
but &= ~(0x40 << i);
}
} else if (!getbuttonstate (i, JOYBUTTON_1)) {
but |= 0x40 << i;
}
}
return but;
}
/* joystick 1 button 1 is used as a output for incrementing shift register */
void handle_cd32_joystick_cia (uae_u8 pra, uae_u8 dra)
{
static int oldstate[2];
int i;
for (i = 0; i < 2; i++) {
uae_u8 but = 0x40 << i;
uae_u16 p5dir = 0x0200 << (i * 4); /* output enable P5 */
uae_u16 p5dat = 0x0100 << (i * 4); /* data P5 */
if (!(potgo_value & p5dir) || !(potgo_value & p5dat)) {
if ((dra & but) && (pra & but) != oldstate[i]) {
if (!(pra & but)) {
cd32_shifter[i]--;
if (cd32_shifter[i] < 0)
cd32_shifter[i] = 0;
}
}
}
oldstate[i] = pra & but;
}
}
/* joystick port 1 button 2 is input for button state */
static uae_u16 handle_joystick_potgor (uae_u16 potgor)
{
int i;
for (i = 0; i < 2; i++) {
uae_u16 p9dir = 0x0800 << (i * 4); /* output enable P9 */
uae_u16 p9dat = 0x0400 << (i * 4); /* data P9 */
uae_u16 p5dir = 0x0200 << (i * 4); /* output enable P5 */
uae_u16 p5dat = 0x0100 << (i * 4); /* data P5 */
if (mouse_port[i]) {
/* mouse has pull-up resistors in button lines */
if (!(potgo_value & p5dir))
potgor |= p5dat;
if (!(potgo_value & p9dir))
potgor |= p9dat;
}
if (potgo_hsync < 0) {
/* first 10 or so lines after potgo has started
* forces input-lines to zero
*/
if (!(potgo_value & p5dir))
potgor &= ~p5dat;
if (!(potgo_value & p9dir))
potgor &= ~p9dat;
}
if (cd32_pad_enabled[i]) {
/* p5 is floating in input-mode */
potgor &= ~p5dat;
potgor |= potgo_value & p5dat;
if (!(potgo_value & p9dir))
potgor |= p9dat;
/* P5 output and 1 -> shift register is kept reset (Blue button) */
if ((potgo_value & p5dir) && (potgo_value & p5dat))
cd32_shifter[i] = 8;
/* shift at 1 == return one, >1 = return button states */
if (cd32_shifter[i] == 0)
potgor &= ~p9dat; /* shift at zero == return zero */
if (cd32_shifter[i] >= 2 && (joybutton[i] & ((1 << JOYBUTTON_CD32_PLAY) << (cd32_shifter[i] - 2))))
potgor &= ~p9dat;
//write_log ("%d:%04.4X %08.8X\n", cd32_shifter[i], potgor, m68k_getpc());
} else {
if (getbuttonstate (i, JOYBUTTON_3))
potgor &= ~p5dat;
if (getbuttonstate (i, JOYBUTTON_2))
potgor &= ~p9dat;
}
}
return potgor;
}
uae_u16 potgo_value;
static uae_u16 potdats[2];
static int inputdelay;
void inputdevice_hsync (void)
{
#ifdef PROFILING
PROFILINGBEGIN
#endif
int joy;
for (joy = 0; joy < 2; joy++) {
if (potgo_hsync >= 0) {
int active;
active = 0;
if ((potgo_value >> 9) & 1) /* output? */
active = ((potgo_value >> 8) & 1) ? 0 : 1;
if (potgo_hsync < joydirpot[joy][0])
active = 1;
if (getbuttonstate (joy, JOYBUTTON_3))
active = 1;
if (active)
potdats[joy] = ((potdats[joy] + 1) & 0xFF) | (potdats[joy] & 0xFF00);
active = 0;
if ((potgo_value >> 11) & 1) /* output? */
active = ((potgo_value >> 10) & 1) ? 0 : 1;
if (potgo_hsync < joydirpot[joy][1])
active = 1;
if (getbuttonstate (joy, JOYBUTTON_2))
active = 1;
if (active)
potdats[joy] += 0x100;
}
}
potgo_hsync++;
if (potgo_hsync > 255)
potgo_hsync = 255;
if (inputdelay > 0) {
inputdelay--;
if (inputdelay == 0) {
idev[IDTYPE_JOYSTICK].read ();
idev[IDTYPE_KEYBOARD].read ();
}
}
#ifdef PROFILING
PROFILINGEND(PROFILINGINDEX)
#endif
}
uae_u16 POT0DAT (void)
{
return potdats[0];
}
uae_u16 POT1DAT (void)
{
return potdats[1];
}
/* direction=input, data pin floating, last connected logic level or previous status
written when direction was ouput
* otherwise it is currently connected logic level.
* direction=output, data pin is current value, forced to zero if joystick button is pressed
* it takes some tens of microseconds before data pin changes state
*/
void POTGO (uae_u16 v)
{
int i;
//write_log ("W:%d: %04.4X %p\n", vpos, v, m68k_getpc());
#ifdef DONGLE_DEBUG
if (notinrom ())
write_log ("POTGO %04.4X %s\n", v, debuginfo(0));
#endif
potgo_value = potgo_value & 0x5500; /* keep state of data bits */
potgo_value |= v & 0xaa00; /* get new direction bits */
for (i = 0; i < 8; i += 2) {
uae_u16 dir = 0x0200 << i;
if (v & dir) {
uae_u16 data = 0x0100 << i;
potgo_value &= ~data;
potgo_value |= v & data;
}
}
for (i = 0; i < 2; i++) {
if (cd32_pad_enabled[i]) {
uae_u16 p5dir = 0x0200 << (i * 4); /* output enable P5 */
uae_u16 p5dat = 0x0100 << (i * 4); /* data P5 */
if ((potgo_value & p5dir) && (potgo_value & p5dat))
cd32_shifter[i] = 8;
}
}
if (v & 1) {
potdats[0] = potdats[1] = 0;
potgo_hsync = -15;
}
}
uae_u16 POTGOR (void)
{
uae_u16 v = handle_joystick_potgor (potgo_value) & 0x5500;
#ifdef DONGLE_DEBUG
if (notinrom ())
write_log ("POTGOR %04.4X %s\n", v, debuginfo(0));
#endif
//write_log("R:%d:%04.4X %d %p\n", vpos, v, cd32_shifter[1], m68k_getpc());
return v;
}
static int check_input_queue (int event)
{
const struct input_queue_struct *iq;
int i;
for (i = 0; i < INPUT_QUEUE_SIZE; i++) {
iq = &input_queue[i];
if (iq->event == event)
return i;
}
return -1;
}
static void queue_input_event (int event, int state, int max, int framecnt, int autofire)
{
struct input_queue_struct *iq;
int i = check_input_queue (event);
if (event <= 0)
return;
if (state < 0 && i >= 0) {
iq = &input_queue[i];
iq->nextframecnt = -1;
iq->framecnt = -1;
iq->event = 0;
if (iq->state == 0)
handle_input_event (event, 0, 1, 0);
} else if (i < 0) {
for (i = 0; i < INPUT_QUEUE_SIZE; i++) {
iq = &input_queue[i];
if (iq->framecnt < 0) break;
}
if (i == INPUT_QUEUE_SIZE) {
write_log ("input queue overflow\n");
return;
}
iq->event = event;
iq->state = iq->storedstate = state;
iq->max = max;
iq->framecnt = framecnt;
iq->nextframecnt = autofire > 0 ? framecnt : -1;
}
}
static uae_u8 keybuf[256];
static int inputcode_pending, inputcode_pending_state;
void inputdevice_add_inputcode (int code, int state)