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