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