]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
Fixed fullscreen<-->windowed mode swith in OpenGL mode, phase one of
[virtualjaguar] / src / joystick.cpp
1 //
2 // Joystick handler
3 //
4 // by cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes by James L. Hammons
7 //
8
9 #include "joystick.h"
10
11 #include <time.h>
12 #include <SDL.h>
13 #include "jaguar.h"
14 #include "log.h"
15 #include "gpu.h"
16 #include "gui.h"
17 #include "settings.h"
18 #include "video.h"
19
20 #define BUTTON_U                0
21 #define BUTTON_D                1
22 #define BUTTON_L                2
23 #define BUTTON_R                3
24 #define BUTTON_s                4
25 #define BUTTON_7                5
26 #define BUTTON_4                6
27 #define BUTTON_1                7
28 #define BUTTON_0                8
29 #define BUTTON_8                9
30 #define BUTTON_5                10
31 #define BUTTON_2                11
32 #define BUTTON_d                12
33 #define BUTTON_9                13
34 #define BUTTON_6                14
35 #define BUTTON_3                15
36
37 #define BUTTON_A                16
38 #define BUTTON_B                17
39 #define BUTTON_C                18
40 #define BUTTON_OPTION   19
41 #define BUTTON_PAUSE    20
42
43 // Global vars
44
45 static uint8 joystick_ram[4];
46 static uint8 joypad_0_buttons[21];
47 static uint8 joypad_1_buttons[21];
48 //extern bool finished;
49 ////extern bool showGUI;
50 bool GUIKeyHeld = false;
51 extern int start_logging;
52 int gpu_start_log = 0;
53 int op_start_log = 0;
54 int blit_start_log = 0;
55 int effect_start = 0;
56 int effect_start2 = 0, effect_start3 = 0, effect_start4 = 0, effect_start5 = 0, effect_start6 = 0;
57 bool interactiveMode = false;
58 bool iLeft, iRight, iToggle = false;
59 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
60 int objectPtr = 0;
61 bool startMemLog = false;
62 extern bool doDSPDis, doGPUDis;
63
64 bool blitterSingleStep = false;
65 bool bssGo = false;
66 bool bssHeld = false;
67
68 void JoystickInit(void)
69 {
70         JoystickReset();
71 }
72
73 void JoystickExec(void)
74 {
75 //      extern bool useJoystick;
76         uint8 * keystate = SDL_GetKeyState(NULL);
77
78         memset(joypad_0_buttons, 0, 21);
79         memset(joypad_1_buttons, 0, 21);
80         gpu_start_log = 0;                                                      // Only log while key down!
81         effect_start = 0;
82         effect_start2 = effect_start3 = effect_start4 = effect_start5 = effect_start6 = 0;
83         blit_start_log = 0;
84         iLeft = iRight = false;
85
86         if ((keystate[SDLK_LALT] || keystate[SDLK_RALT]) & keystate[SDLK_RETURN])
87                 ToggleFullscreen();
88
89         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
90 //      vjs.p1KeyBindings[0] = sdlemu_getval_int("p1k_up", SDLK_UP);
91
92         if (keystate[vjs.p1KeyBindings[0]])
93                 joypad_0_buttons[BUTTON_U] = 0x01;
94         if (keystate[vjs.p1KeyBindings[1]])
95                 joypad_0_buttons[BUTTON_D] = 0x01;
96         if (keystate[vjs.p1KeyBindings[2]])
97                 joypad_0_buttons[BUTTON_L] = 0x01;
98         if (keystate[vjs.p1KeyBindings[3]])
99                 joypad_0_buttons[BUTTON_R] = 0x01;
100         // The buttons are labelled C,B,A on the controller (going from left to right)
101         if (keystate[vjs.p1KeyBindings[4]])
102                 joypad_0_buttons[BUTTON_C] = 0x01;
103         if (keystate[vjs.p1KeyBindings[5]])
104                 joypad_0_buttons[BUTTON_B] = 0x01;
105         if (keystate[vjs.p1KeyBindings[6]])
106                 joypad_0_buttons[BUTTON_A] = 0x01;
107 //I may yet move these to O and P...
108         if (keystate[vjs.p1KeyBindings[7]])
109                 joypad_0_buttons[BUTTON_OPTION] = 0x01;
110         if (keystate[vjs.p1KeyBindings[8]])
111                 joypad_0_buttons[BUTTON_PAUSE] = 0x01;
112
113         if (keystate[vjs.p1KeyBindings[9]])
114                 joypad_0_buttons[BUTTON_0] = 0x01;
115         if (keystate[vjs.p1KeyBindings[10]])
116                 joypad_0_buttons[BUTTON_1] = 0x01;
117         if (keystate[vjs.p1KeyBindings[11]])
118                 joypad_0_buttons[BUTTON_2] = 0x01;
119         if (keystate[vjs.p1KeyBindings[12]])
120                 joypad_0_buttons[BUTTON_3] = 0x01;
121         if (keystate[vjs.p1KeyBindings[13]])
122                 joypad_0_buttons[BUTTON_4] = 0x01;
123         if (keystate[vjs.p1KeyBindings[14]])
124                 joypad_0_buttons[BUTTON_5] = 0x01;
125         if (keystate[vjs.p1KeyBindings[15]])
126                 joypad_0_buttons[BUTTON_6] = 0x01;
127         if (keystate[vjs.p1KeyBindings[16]])
128                 joypad_0_buttons[BUTTON_7] = 0x01;
129         if (keystate[vjs.p1KeyBindings[17]])
130                 joypad_0_buttons[BUTTON_8] = 0x01;
131         if (keystate[vjs.p1KeyBindings[18]])
132                 joypad_0_buttons[BUTTON_9] = 0x01;
133         if (keystate[vjs.p1KeyBindings[19]])
134                 joypad_0_buttons[BUTTON_s] = 0x01;
135         if (keystate[vjs.p1KeyBindings[20]])
136                 joypad_0_buttons[BUTTON_d] = 0x01;
137
138         extern bool debounceRunKey;
139     if (keystate[SDLK_ESCAPE])
140     {
141                 if (!debounceRunKey)
142                 finished = true;
143     }
144     else
145                 debounceRunKey = false;
146
147         if (keystate[SDLK_TAB])
148         {
149                 if (!GUIKeyHeld)
150                         showGUI = !showGUI, GUIKeyHeld = true;
151         }
152         else
153                 GUIKeyHeld = false;
154
155         if (keystate[SDLK_q])
156                 start_logging = 1;
157         if (keystate[SDLK_w])
158                 gpu_reset_stats();
159 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
160         if (keystate[SDLK_d])
161                 DumpMainMemory();
162         if (keystate[SDLK_l])
163                 gpu_start_log = 1;
164         if (keystate[SDLK_o])
165                 op_start_log = 1;
166         if (keystate[SDLK_b])
167                 blit_start_log = 1;
168
169         if (keystate[SDLK_1])
170                 effect_start = 1;
171         if (keystate[SDLK_2])
172                 effect_start2 = 1;
173         if (keystate[SDLK_3])
174                 effect_start3 = 1;
175         if (keystate[SDLK_4])
176                 effect_start4 = 1;
177         if (keystate[SDLK_5])
178                 effect_start5 = 1;
179         if (keystate[SDLK_6])
180                 effect_start6 = 1;
181
182         if (keystate[SDLK_i])
183                 interactiveMode = true;
184
185         if (keystate[SDLK_8] && interactiveMode)
186         {
187                 if (!keyHeld1)
188                         objectPtr--, keyHeld1 = true;
189         }
190         else
191                 keyHeld1 = false;
192
193         if (keystate[SDLK_0] && interactiveMode)
194         {
195                 if (!keyHeld2)
196                         objectPtr++, keyHeld2 = true;
197         }
198         else
199                 keyHeld2 = false;
200
201         if (keystate[SDLK_9] && interactiveMode)
202         {
203                 if (!keyHeld3)
204                         iToggle = !iToggle, keyHeld3 = true;
205         }
206         else
207                 keyHeld3 = false;
208
209         if (keystate[SDLK_e])
210                 startMemLog = true;
211         if (keystate[SDLK_r])
212                 WriteLog("\n--------> MARK!\n\n");
213         if (keystate[SDLK_t])
214                 doDSPDis = true;
215         if (keystate[SDLK_y])
216                 doGPUDis = true;
217
218         // BLITTER single step
219         if (keystate[SDLK_F5])
220                 blitterSingleStep = true;
221
222         if (keystate[SDLK_F6])
223         {
224                 if (!bssHeld)
225                 {
226                         bssHeld = true;
227                         bssGo = true;
228                 }
229         }
230         else
231                 bssHeld = false;
232
233         // Joystick support [nwagenaar]
234
235     if (vjs.useJoystick)
236     {
237                 extern SDL_Joystick * joystick;
238                 int16 x = SDL_JoystickGetAxis(joystick, 0),
239                         y = SDL_JoystickGetAxis(joystick, 1);
240
241                 if (x > 16384)
242                         joypad_0_buttons[BUTTON_R] = 0x01;
243                 if (x < -16384)
244                         joypad_0_buttons[BUTTON_L] = 0x01;
245                 if (y > 16384)
246                         joypad_0_buttons[BUTTON_D] = 0x01;
247                 if (y < -16384)
248                         joypad_0_buttons[BUTTON_U] = 0x01;
249
250                 if (SDL_JoystickGetButton(joystick, 0) == SDL_PRESSED)
251                         joypad_0_buttons[BUTTON_A] = 0x01;
252                 if (SDL_JoystickGetButton(joystick, 1) == SDL_PRESSED)
253                         joypad_0_buttons[BUTTON_B] = 0x01;
254                 if (SDL_JoystickGetButton(joystick, 2) == SDL_PRESSED)
255                         joypad_0_buttons[BUTTON_C] = 0x01;
256         }
257
258         // Needed to ensure that the events queue is empty [nwagenaar]
259     SDL_PumpEvents();
260 }
261
262 void JoystickReset(void)
263 {
264         memset(joystick_ram, 0x00, 4);
265         memset(joypad_0_buttons, 0, 21);
266         memset(joypad_1_buttons, 0, 21);
267 }
268
269 void JoystickDone(void)
270 {
271 }
272
273 uint8 JoystickReadByte(uint32 offset)
274 {
275 #warning No bounds checking done in JoystickReadByte!
276 //      extern bool hardwareTypeNTSC;
277         offset &= 0x03;
278
279         if (offset == 0)
280         {
281                 uint8 data = 0x00;
282                 int pad0Index = joystick_ram[1] & 0x0F;
283                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
284
285 // This is bad--we're assuming that a bit is set in the last case. Might not be so!
286                 if (!(pad0Index & 0x01))
287                         pad0Index = 0;
288                 else if (!(pad0Index & 0x02))
289                         pad0Index = 1;
290                 else if (!(pad0Index & 0x04))
291                         pad0Index = 2;
292                 else
293                         pad0Index = 3;
294
295                 if (!(pad1Index & 0x01))
296                         pad1Index = 0;
297                 else if (!(pad1Index & 0x02))
298                         pad1Index = 1;
299                 else if (!(pad1Index & 0x04))
300                         pad1Index = 2;
301                 else
302                         pad1Index = 3;
303
304                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
305                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
306                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
307                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
308                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
309                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
310                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
311                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
312
313                 return ~data;
314         }
315         else if (offset == 3)
316         {
317                 // Hardware ID returns NTSC/PAL identification bit here
318                 uint8 data = 0x2F | (vjs.hardwareTypeNTSC ? 0x10 : 0x00);
319                 int pad0Index = joystick_ram[1] & 0x0F;
320 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
321
322                 if (!(pad0Index & 0x01))
323                 {
324                         if (joypad_0_buttons[BUTTON_PAUSE])
325                                 data ^= 0x01;
326                         if (joypad_0_buttons[BUTTON_A])
327                                 data ^= 0x02;
328                 }
329                 else if (!(pad0Index & 0x02))
330                 {
331                         if (joypad_0_buttons[BUTTON_B])
332                                 data ^= 0x02;
333                 }
334                 else if (!(pad0Index & 0x04))
335                 {
336                         if (joypad_0_buttons[BUTTON_C])
337                                 data ^= 0x02;
338                 }
339                 else
340                 {
341                         if (joypad_0_buttons[BUTTON_OPTION])
342                                 data ^= 0x02;
343                 }
344                 return data;
345         }
346
347         return joystick_ram[offset];
348 }
349
350 uint16 JoystickReadWord(uint32 offset)
351 {
352         return ((uint16)JoystickReadByte((offset + 0) & 0x03) << 8) | JoystickReadByte((offset + 1) & 0x03);
353 }
354
355 void JoystickWriteByte(uint32 offset, uint8 data)
356 {
357         joystick_ram[offset & 0x03] = data;
358 }
359
360 void JoystickWriteWord(uint32 offset, uint16 data)
361 {
362 #warning No bounds checking done for JoystickWriteWord!
363         offset &= 0x03;
364         joystick_ram[offset + 0] = (data >> 8) & 0xFF;
365         joystick_ram[offset + 1] = data & 0xFF;
366 }