]> Shamusworld >> Repos - virtualjaguar/blob - src/joystick.cpp
Fixed a bad variable definition
[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 //#ifndef __PORT__
10 //#include "include/stdafx.h"
11 //#include <mmsystem.h>
12 //#endif
13 #include <time.h>
14 #include <SDL.h>
15 //#include "SDLptc.h"
16 #include "jaguar.h"
17
18 void main_screen_switch(void);
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 static uint8 joystick_ram[4];
44 static uint8 joypad_0_buttons[21];
45 static uint8 joypad_1_buttons[21];
46 extern bool finished;
47 extern bool showGUI;
48 bool GUIKeyHeld = false;
49 extern int start_logging;
50 int gpu_start_log = 0;
51 int op_start_log = 0;
52 int blit_start_log = 0;
53 int effect_start = 0;
54 bool interactiveMode = false;
55 bool iLeft, iRight, iToggle = false;
56 bool keyHeld1 = false, keyHeld2 = false, keyHeld3 = false;
57 int objectPtr = 0;
58
59
60 void main_screen_switch(void)
61 {
62         extern SDL_Surface * mainSurface;
63         extern Uint32 mainSurfaceFlags;
64         extern bool fullscreen;
65
66         fullscreen = !fullscreen;
67         mainSurfaceFlags &= ~SDL_FULLSCREEN;
68         if (fullscreen)
69                 mainSurfaceFlags |= SDL_FULLSCREEN;
70
71 //???Should we do this???
72 //      SDL_FreeSurface(mainSurface);
73         mainSurface = SDL_SetVideoMode(tom_width, tom_height, 16, mainSurfaceFlags);
74
75         if (mainSurface == NULL)
76         {
77                 WriteLog("Joystick: SDL is unable to set the video mode: %s\n", SDL_GetError());
78                 exit(1);
79         }
80
81         SDL_WM_SetCaption("Virtual Jaguar", "Virtual Jaguar");
82 /*      if (fullscreen)
83                 console.option("fullscreen output");
84         else
85                 console.option("windowed output");*/
86
87 //      console.close();
88 //      console.open("Virtual Jaguar", tom_width, tom_height, format);
89 }
90
91 void joystick_init(void)
92 {
93         joystick_reset();
94 }
95
96 void joystick_exec(void)
97 {
98         extern SDL_Joystick * joystick;
99         extern bool useJoystick;
100         uint8 * keystate = SDL_GetKeyState(NULL);
101 //      extern Console console;
102         
103         memset(joypad_0_buttons, 0, 21);
104         memset(joypad_1_buttons, 0, 21);
105         gpu_start_log = 0;                                                      // Only log while key down!
106         effect_start = 0;
107         blit_start_log = 0;
108         iLeft = iRight = false;
109
110         if ((keystate[SDLK_LALT] || keystate[SDLK_RALT]) & keystate[SDLK_RETURN])
111                 main_screen_switch();
112
113         /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com) */
114
115         if (keystate[SDLK_UP])          joypad_0_buttons[BUTTON_U] = 0x01;
116         if (keystate[SDLK_DOWN])        joypad_0_buttons[BUTTON_D] = 0x01;
117         if (keystate[SDLK_LEFT])        joypad_0_buttons[BUTTON_L] = 0x01;
118         if (keystate[SDLK_RIGHT])       joypad_0_buttons[BUTTON_R] = 0x01;
119         // The buttons are labelled C,B,A on the controller (going from left to right)
120         if (keystate[SDLK_z])           joypad_0_buttons[BUTTON_C] = 0x01;
121         if (keystate[SDLK_x])           joypad_0_buttons[BUTTON_B] = 0x01;
122         if (keystate[SDLK_c])           joypad_0_buttons[BUTTON_A] = 0x01;
123 //I may yet move these to O and P...
124         if (keystate[SDLK_QUOTE])       joypad_0_buttons[BUTTON_OPTION] = 0x01;
125         if (keystate[SDLK_RETURN])      joypad_0_buttons[BUTTON_PAUSE] = 0x01;
126
127         if (keystate[SDLK_TAB])
128         {
129                 if (!GUIKeyHeld)
130                         showGUI = !showGUI, GUIKeyHeld = true;
131         }
132         else
133                 GUIKeyHeld = false;
134
135         if (keystate[SDLK_q])
136                 start_logging = 1;
137         if (keystate[SDLK_w])
138                 gpu_reset_stats();
139 //      if (keystate[SDLK_u])           jaguar_long_write(0xf1c384,jaguar_long_read(0xf1c384)+1);
140         if (keystate[SDLK_d])
141                 DumpMainMemory();
142         if (keystate[SDLK_l])
143                 gpu_start_log = 1;
144         if (keystate[SDLK_o])
145                 op_start_log = 1;
146         if (keystate[SDLK_b])
147                 blit_start_log = 1;
148         if (keystate[SDLK_1])
149                 effect_start = 1;
150
151         if (keystate[SDLK_i])
152                 interactiveMode = true;
153
154         if (keystate[SDLK_8] && interactiveMode)
155         {
156                 if (!keyHeld1)
157                         objectPtr--, keyHeld1 = true;
158         }
159         else
160                 keyHeld1 = false;
161
162         if (keystate[SDLK_0] && interactiveMode)
163         {
164                 if (!keyHeld2)
165                         objectPtr++, keyHeld2 = true;
166         }
167         else
168                 keyHeld2 = false;
169
170         if (keystate[SDLK_9] && interactiveMode)
171         {
172                 if (!keyHeld3)
173                         iToggle = !iToggle, keyHeld3 = true;
174         }
175         else
176                 keyHeld3 = false;
177
178         if (keystate[SDLK_KP0])         joypad_0_buttons[BUTTON_0] = 0x01;
179         if (keystate[SDLK_KP1])         joypad_0_buttons[BUTTON_1] = 0x01;
180         if (keystate[SDLK_KP2])         joypad_0_buttons[BUTTON_2] = 0x01;
181         if (keystate[SDLK_KP3])         joypad_0_buttons[BUTTON_3] = 0x01;
182         if (keystate[SDLK_KP4])         joypad_0_buttons[BUTTON_4] = 0x01;
183         if (keystate[SDLK_KP5])         joypad_0_buttons[BUTTON_5] = 0x01;
184         if (keystate[SDLK_KP6])         joypad_0_buttons[BUTTON_6] = 0x01;
185         if (keystate[SDLK_KP7])         joypad_0_buttons[BUTTON_7] = 0x01;
186         if (keystate[SDLK_KP8])         joypad_0_buttons[BUTTON_8] = 0x01;
187         if (keystate[SDLK_KP9])         joypad_0_buttons[BUTTON_9] = 0x01;
188
189     if (keystate[SDLK_ESCAPE])
190         finished = true;
191
192     /* Added/Changed by SDLEMU (http://sdlemu.ngemu.com */
193     /* Joystick support                                 */
194     
195 //    if (console.JoyEnabled() == 1)
196     if (useJoystick)
197     {
198 //              int16 x = SDL_JoystickGetAxis(console.joystick, 0),
199 //                      y = SDL_JoystickGetAxis(console.joystick, 1);
200                 int16 x = SDL_JoystickGetAxis(joystick, 0),
201                         y = SDL_JoystickGetAxis(joystick, 1);
202         
203                 if (x > 16384)
204                         joypad_0_buttons[BUTTON_R] = 0x01;
205                 if (x < -16384)
206                         joypad_0_buttons[BUTTON_L] = 0x01;
207                 if (y > 16384)
208                         joypad_0_buttons[BUTTON_D] = 0x01;
209                 if (y < -16384)
210                         joypad_0_buttons[BUTTON_U] = 0x01;
211         
212 //              if (SDL_JoystickGetButton(console.joystick, 0) == SDL_PRESSED)
213                 if (SDL_JoystickGetButton(joystick, 0) == SDL_PRESSED)
214                         joypad_0_buttons[BUTTON_A] = 0x01;
215 //              if (SDL_JoystickGetButton(console.joystick, 1) == SDL_PRESSED)
216                 if (SDL_JoystickGetButton(joystick, 1) == SDL_PRESSED)
217                         joypad_0_buttons[BUTTON_B] = 0x01;
218 //              if (SDL_JoystickGetButton(console.joystick, 2) == SDL_PRESSED)
219                 if (SDL_JoystickGetButton(joystick, 2) == SDL_PRESSED)
220                         joypad_0_buttons[BUTTON_C] = 0x01;
221         }
222         
223         /* ADDED by SDLEMU (http://sdlemu.ngemu.com */
224         /* Needed to make sure that the events queue is empty */
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 void joystick_byte_write(uint32 offset, uint8 data)
240 {
241         joystick_ram[offset&0x03] = data;
242 }
243
244 void joystick_word_write(uint32 offset, uint16 data)
245 {
246         offset &= 0x03;
247         joystick_ram[offset+0] = (data >> 8) & 0xFF;
248         joystick_ram[offset+1] = data & 0xFF;
249 }
250
251 uint8 joystick_byte_read(uint32 offset)
252 {
253         extern bool hardwareTypeNTSC;
254         offset &= 0x03;
255
256         if (offset == 0)
257         {
258                 uint8 data = 0x00;
259                 int pad0Index = joystick_ram[1] & 0x0F;
260                 int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
261                 
262 // This is bad--we're assuming that a bit is set in the last case
263                 if (!(pad0Index & 0x01)) 
264                         pad0Index = 0;
265                 else if (!(pad0Index & 0x02)) 
266                         pad0Index = 1;
267                 else if (!(pad0Index & 0x04)) 
268                         pad0Index = 2;
269                 else 
270                         pad0Index = 3;
271                 
272                 if (!(pad1Index & 0x01)) 
273                         pad1Index = 0;
274                 else if (!(pad1Index & 0x02)) 
275                         pad1Index = 1;
276                 else if (!(pad1Index & 0x04)) 
277                         pad1Index = 2;
278                 else
279                         pad1Index = 3;
280
281                 if (joypad_0_buttons[(pad0Index << 2) + 0])     data |= 0x01;
282                 if (joypad_0_buttons[(pad0Index << 2) + 1]) data |= 0x02;
283                 if (joypad_0_buttons[(pad0Index << 2) + 2]) data |= 0x04;
284                 if (joypad_0_buttons[(pad0Index << 2) + 3]) data |= 0x08;
285                 if (joypad_1_buttons[(pad1Index << 2) + 0]) data |= 0x10;
286                 if (joypad_1_buttons[(pad1Index << 2) + 1]) data |= 0x20;
287                 if (joypad_1_buttons[(pad1Index << 2) + 2]) data |= 0x40;
288                 if (joypad_1_buttons[(pad1Index << 2) + 3]) data |= 0x80;
289
290                 return ~data;
291         }
292         else if (offset == 3)
293         {
294 //              uint8 data = ((1 << 5) | (1 << 4) | 0x0F);
295                 uint8 data = 0x2F | (hardwareTypeNTSC ? 0x10 : 0x00);
296                 int pad0Index = joystick_ram[1] & 0x0F;
297 //unused                int pad1Index = (joystick_ram[1] >> 4) & 0x0F;
298                 
299                 if (!(pad0Index & 0x01))
300                 {
301                         if (joypad_0_buttons[BUTTON_PAUSE])
302                                 data ^= 0x01;
303                         if (joypad_0_buttons[BUTTON_A])
304                                 data ^= 0x02;
305                 }
306                 else if (!(pad0Index & 0x02))
307                 {
308                         if (joypad_0_buttons[BUTTON_B])
309                                 data ^= 0x02;
310                 }
311                 else if (!(pad0Index & 0x04))
312                 {
313                         if (joypad_0_buttons[BUTTON_C])
314                                 data ^= 0x02;
315                 }
316                 else
317                 {
318                         if (joypad_0_buttons[BUTTON_OPTION])
319                                 data ^= 0x02;
320                 }               
321                 return data;
322         }
323
324         return joystick_ram[offset];
325 }
326
327 uint16 joystick_word_read(uint32 offset)
328 {
329         return ((uint16)joystick_byte_read((offset+0)&0x03) << 8) | joystick_byte_read((offset+1)&0x03);
330 }