forked from Cpasjuste/uae4all2
-
Notifications
You must be signed in to change notification settings - Fork 10
/
od-joy.cpp
executable file
·415 lines (385 loc) · 11.4 KB
/
od-joy.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
/*
* UAE - The Un*x Amiga Emulator
*
* Joystick emulation for Linux and BSD. They share too much code to
* split this file.
*
* Copyright 1997 Bernd Schmidt
* Copyright 1998 Krister Walfridsson
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "config.h"
#include "uae.h"
#include "options.h"
#include "memory-uae.h"
#include "custom.h"
#include "joystick.h"
#include "SDL.h"
#include "menu.h"
#include "menu_config.h"
#ifdef USE_UAE4ALL_VKBD
#include "vkbd.h"
#endif
#ifdef ANDROIDSDL
#include <android/log.h>
#endif
#ifdef GP2X
#include "gp2x.h"
#include "xwin.h"
extern int gp2xMouseEmuOn;
extern int gp2xButtonRemappingOn;
#if defined(PANDORA) || defined (ANDROIDSDL)
extern int dpadUp;
extern int dpadDown;
extern int dpadLeft;
extern int dpadRight;
extern int buttonA;
extern int buttonB;
extern int buttonX;
extern int buttonY;
extern int triggerL;
extern int triggerR;
extern int buttonSelect;
extern int buttonStart;
#endif
#ifdef __PSP2__
extern int rAnalogX;
extern int rAnalogY;
extern int lAnalogX;
extern int lAnalogY;
extern int mainMenu_leftStickMouse;
extern int mainMenu_deadZone;
#endif
extern char launchDir[300];
bool switch_autofire=false;
int delay=0;
#endif
int nr_joysticks;
SDL_Joystick *uae4all_joy0, *uae4all_joy1, *uae4all_joy2;
extern SDL_Surface *prSDLScreen;
void read_joystick(int nr, unsigned int *dir, int *button)
{
#ifndef MAX_AUTOEVENTS
int x_axis, y_axis;
int left = 0, right = 0, top = 0, bot = 0, upRight=0, downRight=0, upLeft=0, downLeft=0, x=0, y=0, a=0, b=0;
int len, i, num;
SDL_Joystick *joy = nr == 0 ? uae4all_joy0 : uae4all_joy1;
*dir = 0;
*button = 0;
nr = (~nr)&0x1;
SDL_JoystickUpdate ();
/* Temporary disabled
#ifdef ANDROIDSDL
if (nr_joysticks > 2)
{
int axis0,axis1,axis2,axis3;
axis0 = SDL_JoystickGetAxis(uae4all_joy2, 0);
axis1 = SDL_JoystickGetAxis(uae4all_joy2, 1);
axis2 = SDL_JoystickGetAxis(uae4all_joy2, 2);
axis3 = SDL_JoystickGetAxis(uae4all_joy2, 3);
if ((axis0 > 512) || (axis2 > 512)) right=1;
if ((axis0 < -512) || (axis2 < -512)) left=1;
if ((axis1 < -512) || (axis3 < -512)) top=1;
if ((axis1 > 512) || (axis3 > 512)) bot=1;
}
#endif
*/
int mouseScale = mainMenu_mouseMultiplier * 4 * 16;
if (mouseScale > (99*16))
mouseScale /= 100;
#if !defined(__PSP2__) && defined(USE_UAE4ALL_VKBD)
if (!vkbd_mode && ((mainMenu_customControls && mainMenu_custom_dpad==2) || gp2xMouseEmuOn || (triggerL && !triggerR && !gp2xButtonRemappingOn)))
#else
#if defined(__PSP2__) && defined(USE_UAE4ALL_VKBD)
//on Vita, the L trigger is by default mapped to a mousebutton
//so remove the hard coded LTrigger here that was enabling the digital mouse
if (!vkbd_mode && ((mainMenu_customControls && mainMenu_custom_dpad==2) || gp2xMouseEmuOn))
#else
#if defined(__PSP2__)
if ((mainMenu_customControls && mainMenu_custom_dpad==2) || gp2xMouseEmuOn)
#else
if (((mainMenu_customControls && mainMenu_custom_dpad==2) || gp2xMouseEmuOn || (triggerL && !triggerR && !gp2xButtonRemappingOn)))
#endif
#endif
#endif
{
if (buttonY)
mouseScale = mainMenu_mouseMultiplier * 16;
#if defined(PANDORA) || defined(ANDROIDSDL)
if (dpadLeft)
#else
if (SDL_JoystickGetButton(joy, GP2X_BUTTON_LEFT))
#endif
{
lastmx -= mouseScale;
newmousecounters=1;
}
#if !( defined(PANDORA) || defined(ANDROIDSDL) )
if (SDL_JoystickGetButton(joy, GP2X_BUTTON_UPLEFT))
{
lastmx -= mouseScale;
lastmy -= mouseScale;
newmousecounters=1;
}
if (SDL_JoystickGetButton(joy, GP2X_BUTTON_DOWNLEFT))
{
lastmx -= mouseScale;
lastmy += mouseScale;
newmousecounters=1;
}
#endif
if (dpadRight)
{
lastmx += mouseScale;
newmousecounters=1;
}
if (dpadUp)
{
lastmy -= mouseScale;
newmousecounters=1;
}
if (dpadDown)
{
lastmy += mouseScale;
newmousecounters=1;
}
}
else if (!triggerR /*R+dpad = arrow keys*/ && !(mainMenu_customControls && !mainMenu_custom_dpad))
{
#if !defined(AROS) && !defined(__PSP2__)
if (dpadRight || SDL_JoystickGetAxis(joy, 0) > 0) right=1;
if (dpadLeft || SDL_JoystickGetAxis(joy, 0) < 0) left=1;
if (dpadUp || SDL_JoystickGetAxis(joy, 1) < 0) top=1;
if (dpadDown || SDL_JoystickGetAxis(joy, 1) > 0) bot=1;
#else
if (dpadRight) right=1;
if (dpadLeft) left=1;
if (dpadUp) top=1;
if (dpadDown) bot=1;
#endif
if (mainMenu_joyConf)
{
#ifdef USE_UAE4ALL_VKBD
if (((buttonX && mainMenu_jump > -1) ) && !vkbd_mode)
#else
if (((buttonX && mainMenu_jump > -1) || SDL_JoystickGetButton(joy, mainMenu_jump)))
#endif
top = 1;
}
}
#ifdef USE_UAE4ALL_VKBD
if (!vkbd_mode) {
#endif
#ifdef __PSP2__
//VITA: always use an analog stick (default: right stick) for mouse pointer movements
//here we are using a small deadzone
int analogX=0;
int analogY=0;
if (mainMenu_leftStickMouse)
{
analogX=lAnalogX;
analogY=lAnalogY;
}
else
{
analogX=rAnalogX;
analogY=rAnalogY;
}
//Deadzone
//max movement is mouseScale.
//that way, when in one of the other mouse modes,
//the Y button to change scale still works
if (!(analogX<mainMenu_deadZone && analogX>-mainMenu_deadZone))
{
lastmx += (int) (analogX/32769.0f * mouseScale);
newmousecounters=1;
}
if (!(analogY<mainMenu_deadZone && analogY>-mainMenu_deadZone))
{
lastmy += (int) (analogY/32769.0f * mouseScale);
newmousecounters=1;
}
#endif //__PSP2__
#ifdef USE_UAE4ALL_VKBD
}
#endif
#ifdef USE_UAE4ALL_VKBD
if(mainMenu_customControls && !vkbd_mode)
#else
if(mainMenu_customControls)
#endif
{
if((mainMenu_custom_A==-5 && buttonA) || (mainMenu_custom_B==-5 && buttonB) || (mainMenu_custom_X==-5 && buttonX) || (mainMenu_custom_Y==-5 && buttonY) || (mainMenu_custom_L==-5 && triggerL) || (mainMenu_custom_R==-5 && triggerR))
top = 1;
else if(mainMenu_custom_dpad == 0)
{
if((mainMenu_custom_up==-5 && dpadUp) || (mainMenu_custom_down==-5 && dpadDown) || (mainMenu_custom_left==-5 && dpadLeft) || (mainMenu_custom_right==-5 && dpadRight))
top = 1;
}
if((mainMenu_custom_A==-6 && buttonA) || (mainMenu_custom_B==-6 && buttonB) || (mainMenu_custom_X==-6 && buttonX) || (mainMenu_custom_Y==-6 && buttonY) || (mainMenu_custom_L==-6 && triggerL) || (mainMenu_custom_R==-6 && triggerR))
bot = 1;
else if(mainMenu_custom_dpad == 0)
{
if((mainMenu_custom_up==-6 && dpadUp) || (mainMenu_custom_down==-6 && dpadDown) || (mainMenu_custom_left==-6 && dpadLeft) || (mainMenu_custom_right==-6 && dpadRight))
bot = 1;
}
if((mainMenu_custom_A==-7 && buttonA) || (mainMenu_custom_B==-7 && buttonB) || (mainMenu_custom_X==-7 && buttonX) || (mainMenu_custom_Y==-7 && buttonY) || (mainMenu_custom_L==-7 && triggerL) || (mainMenu_custom_R==-7 && triggerR))
left = 1;
else if(mainMenu_custom_dpad == 0)
{
if((mainMenu_custom_up==-7 && dpadUp) || (mainMenu_custom_down==-7 && dpadDown) || (mainMenu_custom_left==-7 && dpadLeft) || (mainMenu_custom_right==-7 && dpadRight))
left = 1;
}
if((mainMenu_custom_A==-8 && buttonA) || (mainMenu_custom_B==-8 && buttonB) || (mainMenu_custom_X==-8 && buttonX) || (mainMenu_custom_Y==-8 && buttonY) || (mainMenu_custom_L==-8 && triggerL) || (mainMenu_custom_R==-8 && triggerR))
right = 1;
else if(mainMenu_custom_dpad == 0)
{
if((mainMenu_custom_up==-8 && dpadUp) || (mainMenu_custom_down==-8 && dpadDown) || (mainMenu_custom_left==-8 && dpadLeft) || (mainMenu_custom_right==-8 && dpadRight))
right = 1;
}
}
if(!gp2xMouseEmuOn && !gp2xButtonRemappingOn)
{
if(!mainMenu_customControls && ((mainMenu_autofire & switch_autofire & delay>mainMenu_autofireRate) || (((mainMenu_autofireButton1==GP2X_BUTTON_B && buttonA) || (mainMenu_autofireButton1==GP2X_BUTTON_X && buttonX) || (mainMenu_autofireButton1==GP2X_BUTTON_Y && buttonY)) & delay>mainMenu_autofireRate)))
{
if(!buttonB)
*button=1;
delay=0;
*button |= (buttonB & 1) << 1;
}
#ifdef USE_UAE4ALL_VKBD
else if(mainMenu_customControls && !vkbd_mode)
#else
else if(mainMenu_customControls)
#endif
{
if((mainMenu_custom_A==-3 && buttonA) || (mainMenu_custom_B==-3 && buttonB) || (mainMenu_custom_X==-3 && buttonX) || (mainMenu_custom_Y==-3 && buttonY) || (mainMenu_custom_L==-3 && triggerL) || (mainMenu_custom_R==-3 && triggerR))
*button = 1;
else if(mainMenu_custom_dpad == 0)
{
if((mainMenu_custom_up==-3 && dpadUp) || (mainMenu_custom_down==-3 && dpadDown) || (mainMenu_custom_left==-3 && dpadLeft) || (mainMenu_custom_right==-3 && dpadRight))
*button = 1;
}
if((mainMenu_custom_A==-4 && buttonA) || (mainMenu_custom_B==-4 && buttonB) || (mainMenu_custom_X==-4 && buttonX) || (mainMenu_custom_Y==-4 && buttonY) || (mainMenu_custom_L==-4 && triggerL) || (mainMenu_custom_R==-4 && triggerR))
*button |= 1 << 1;
else if(mainMenu_custom_dpad == 0)
{
if((mainMenu_custom_up==-4 && dpadUp) || (mainMenu_custom_down==-4 && dpadDown) || (mainMenu_custom_left==-4 && dpadLeft) || (mainMenu_custom_right==-4 && dpadRight))
*button |= 1 << 1;
}
delay++;
}
else
{
#if !(defined(ANDROIDSDL) || defined(AROS))
*button = ((mainMenu_button1==GP2X_BUTTON_B && buttonA) || (mainMenu_button1==GP2X_BUTTON_X && buttonX) || (mainMenu_button1==GP2X_BUTTON_Y && buttonY) || SDL_JoystickGetButton(joy, mainMenu_button1)) & 1;
#else
*button = ((mainMenu_button1==GP2X_BUTTON_B && buttonA) || (mainMenu_button1==GP2X_BUTTON_X && buttonX) || (mainMenu_button1==GP2X_BUTTON_Y && buttonY)) & 1;
#endif
delay++;
#if defined(PANDORA) && !defined(AROS)
*button |= ((buttonB || SDL_JoystickGetButton(joy, mainMenu_button2)) & 1) << 1;
#else
*button |= ((buttonB) & 1) << 1;
#endif
}
}
#ifdef USE_UAE4ALL_VKBD
if (vkbd_mode && nr)
{
// move around the virtual keyboard instead
// if Start+up or Start+down is pressed: move the keyboard itself
if (buttonStart && (dpadUp || top))
{
vkbd_displace_up();
}
else if (buttonStart && (dpadDown || bot))
{
vkbd_displace_down();
}
else if (buttonStart && (dpadLeft || left))
{
vkbd_transparency_up();
}
else if (buttonStart && (dpadRight || right))
{
vkbd_transparency_down();
}
else
{
if (left || dpadLeft)
vkbd_move |= VKBD_LEFT;
else
{
vkbd_move &= ~VKBD_LEFT;
if (right || dpadRight)
vkbd_move |= VKBD_RIGHT;
else
vkbd_move &= ~VKBD_RIGHT;
}
if (top || dpadUp)
vkbd_move |= VKBD_UP;
else
{
vkbd_move &= ~VKBD_UP;
if (bot || dpadDown)
vkbd_move |= VKBD_DOWN;
else
vkbd_move &= ~VKBD_DOWN;
}
if (*button || buttonX)
{
vkbd_move=VKBD_BUTTON;
*button=0;
}
else //button release, make shift toggle possible again.
{
vkbd_can_switch_shift=1;
}
// TODO: add vkbd_button2 mapped to button2
}
}
else
#endif
{
// normal joystick movement
if (left) top = !top;
if (right) bot = !bot;
*dir = bot | (right << 1) | (top << 8) | (left << 9);
}
if(mainMenu_joyPort != 0)
{
// Only one joystick active
if((nr == 0 && mainMenu_joyPort == 2) || (nr == 1 && mainMenu_joyPort == 1))
{
*dir = 0;
*button = 0;
}
}
#endif
}
void init_joystick(void)
{
int i;
nr_joysticks = SDL_NumJoysticks ();
if (nr_joysticks > 0)
uae4all_joy0 = SDL_JoystickOpen (0);
if (nr_joysticks > 1)
uae4all_joy1 = SDL_JoystickOpen (1);
else
uae4all_joy1 = NULL;
/* if (nr_joysticks > 2)
uae4all_joy2 = SDL_JoystickOpen (2);
else
uae4all_joy2 = NULL;*/
}
void close_joystick(void)
{
if (nr_joysticks > 0)
SDL_JoystickClose (uae4all_joy0);
if (nr_joysticks > 1)
SDL_JoystickClose (uae4all_joy1);
/* if (nr_joysticks > 2)
SDL_JoystickClose (uae4all_joy2);*/
}