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