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