RICE: OOT Fix from mupen64plus-ae team
[mupen64plus-pandora.git] / source / mupen64plus-core / src / main / eventloop.c
CommitLineData
451ab91e 1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - eventloop.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2008-2009 Richard Goedeken *
5 * Copyright (C) 2008 Ebenblues Nmn Okaygo Tillin9 *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22
23#include <stdlib.h>
24#include <SDL.h>
25#if ! SDL_VERSION_ATLEAST(1,3,0)
26
27#define SDL_SCANCODE_ESCAPE SDLK_ESCAPE
28#define SDL_NUM_SCANCODES SDLK_LAST
29#define SDL_SCANCODE_F5 SDLK_F5
30#define SDL_SCANCODE_F7 SDLK_F7
31#define SDL_SCANCODE_F9 SDLK_F9
32#define SDL_SCANCODE_F10 SDLK_F10
33#define SDL_SCANCODE_F11 SDLK_F11
34#define SDL_SCANCODE_F12 SDLK_F12
35#define SDL_SCANCODE_P SDLK_p
36#define SDL_SCANCODE_M SDLK_m
37#define SDL_SCANCODE_RIGHTBRACKET SDLK_RIGHTBRACKET
38#define SDL_SCANCODE_LEFTBRACKET SDLK_LEFTBRACKET
39#define SDL_SCANCODE_F SDLK_f
40#define SDL_SCANCODE_SLASH SDLK_SLASH
41#define SDL_SCANCODE_G SDLK_g
42#define SDL_SCANCODE_RETURN SDLK_RETURN
43#define SDL_SCANCODE_0 SDLK_0
44#define SDL_SCANCODE_9 SDLK_9
45
46#define SDL_SetEventFilter(func, data) SDL_SetEventFilter(func)
47#define event_sdl_filter(userdata, event) event_sdl_filter(const event)
48
49#endif
50
51#define M64P_CORE_PROTOTYPES 1
52#include "main.h"
53#include "eventloop.h"
54#include "util.h"
55#include "api/callbacks.h"
56#include "api/config.h"
57#include "api/m64p_config.h"
58#include "plugin/plugin.h"
59#include "r4300/interupt.h"
60#include "r4300/reset.h"
61
62/* version number for CoreEvents config section */
63#define CONFIG_PARAM_VERSION 1.00
64
65static m64p_handle l_CoreEventsConfig = NULL;
66
67/*********************************************************************************************************
68* static variables and definitions for eventloop.c
69*/
70
71#define kbdFullscreen "Kbd Mapping Fullscreen"
72#define kbdStop "Kbd Mapping Stop"
73#define kbdPause "Kbd Mapping Pause"
74#define kbdSave "Kbd Mapping Save State"
75#define kbdLoad "Kbd Mapping Load State"
76#define kbdIncrement "Kbd Mapping Increment Slot"
77#define kbdReset "Kbd Mapping Reset"
78#define kbdSpeeddown "Kbd Mapping Speed Down"
79#define kbdSpeedup "Kbd Mapping Speed Up"
80#define kbdScreenshot "Kbd Mapping Screenshot"
81#define kbdMute "Kbd Mapping Mute"
82#define kbdIncrease "Kbd Mapping Increase Volume"
83#define kbdDecrease "Kbd Mapping Decrease Volume"
84#define kbdForward "Kbd Mapping Fast Forward"
85#define kbdAdvance "Kbd Mapping Frame Advance"
86#define kbdGameshark "Kbd Mapping Gameshark"
87
88typedef enum {joyFullscreen,
89 joyStop,
90 joyPause,
91 joySave,
92 joyLoad,
93 joyIncrement,
94 joyScreenshot,
95 joyMute,
96 joyIncrease,
97 joyDecrease,
98 joyForward,
99 joyGameshark
100} eJoyCommand;
101
102static const char *JoyCmdName[] = { "Joy Mapping Fullscreen",
103 "Joy Mapping Stop",
104 "Joy Mapping Pause",
105 "Joy Mapping Save State",
106 "Joy Mapping Load State",
107 "Joy Mapping Increment Slot",
108 "Joy Mapping Screenshot",
109 "Joy Mapping Mute",
110 "Joy Mapping Increase Volume",
111 "Joy Mapping Decrease Volume",
112 "Joy Mapping Fast Forward",
113 "Joy Mapping Gameshark"};
114
115static const int NumJoyCommands = sizeof(JoyCmdName) / sizeof(const char *);
116
117static int JoyCmdActive[16]; /* if extra joystick commands are added above, make sure there is enough room in this array */
118
119static int GamesharkActive = 0;
120
121/*********************************************************************************************************
122* static functions for eventloop.c
123*/
124
125/** MatchJoyCommand
126 * This function processes an SDL event and updates the JoyCmdActive array if the
127 * event matches with the given command.
128 *
129 * If the event activates a joystick command which was not previously active, then
130 * a 1 is returned. If the event de-activates an command, a -1 is returned. Otherwise
131 * (if the event does not match the command or active status didn't change), a 0 is returned.
132 */
133static int MatchJoyCommand(const SDL_Event *event, eJoyCommand cmd)
134{
135 const char *event_str = ConfigGetParamString(l_CoreEventsConfig, JoyCmdName[cmd]);
136 int dev_number, input_number, input_value;
137 char axis_direction;
138
139 /* Empty string or non-joystick command */
140 if (event_str == NULL || strlen(event_str) < 4 || event_str[0] != 'J')
141 return 0;
142
143 /* Evaluate event based on type of joystick input expected by the given command */
144 switch (event_str[2])
145 {
146 /* Axis */
147 case 'A':
148 if (event->type != SDL_JOYAXISMOTION)
149 return 0;
150 if (sscanf(event_str, "J%dA%d%c", &dev_number, &input_number, &axis_direction) != 3)
151 return 0;
152 if (dev_number != event->jaxis.which || input_number != event->jaxis.axis)
153 return 0;
154 if (axis_direction == '+')
155 {
156 if (event->jaxis.value >= 15000 && JoyCmdActive[cmd] == 0)
157 {
158 JoyCmdActive[cmd] = 1;
159 return 1;
160 }
161 else if (event->jaxis.value <= 8000 && JoyCmdActive[cmd] == 1)
162 {
163 JoyCmdActive[cmd] = 0;
164 return -1;
165 }
166 return 0;
167 }
168 else if (axis_direction == '-')
169 {
170 if (event->jaxis.value <= -15000 && JoyCmdActive[cmd] == 0)
171 {
172 JoyCmdActive[cmd] = 1;
173 return 1;
174 }
175 else if (event->jaxis.value >= -8000 && JoyCmdActive[cmd] == 1)
176 {
177 JoyCmdActive[cmd] = 0;
178 return -1;
179 }
180 return 0;
181 }
182 else return 0; /* invalid axis direction in configuration parameter */
183 break;
184 /* Hat */
185 case 'H':
186 if (event->type != SDL_JOYHATMOTION)
187 return 0;
188 if (sscanf(event_str, "J%dH%dV%d", &dev_number, &input_number, &input_value) != 3)
189 return 0;
190 if (dev_number != event->jhat.which || input_number != event->jhat.hat)
191 return 0;
192 if ((event->jhat.value & input_value) == input_value && JoyCmdActive[cmd] == 0)
193 {
194 JoyCmdActive[cmd] = 1;
195 return 1;
196 }
197 else if ((event->jhat.value & input_value) != input_value && JoyCmdActive[cmd] == 1)
198 {
199 JoyCmdActive[cmd] = 0;
200 return -1;
201 }
202 return 0;
203 break;
204 /* Button. */
205 case 'B':
206 if (event->type != SDL_JOYBUTTONDOWN && event->type != SDL_JOYBUTTONUP)
207 return 0;
208 if (sscanf(event_str, "J%dB%d", &dev_number, &input_number) != 2)
209 return 0;
210 if (dev_number != event->jbutton.which || input_number != event->jbutton.button)
211 return 0;
212 if (event->type == SDL_JOYBUTTONDOWN && JoyCmdActive[cmd] == 0)
213 {
214 JoyCmdActive[cmd] = 1;
215 return 1;
216 }
217 else if (event->type == SDL_JOYBUTTONUP && JoyCmdActive[cmd] == 1)
218 {
219 JoyCmdActive[cmd] = 0;
220 return -1;
221 }
222 return 0;
223 break;
224 default:
225 /* bad configuration parameter */
226 return 0;
227 }
228
229 /* impossible to reach this point */
230 return 0;
231}
232
233/*********************************************************************************************************
234* sdl event filter
235*/
236static int SDLCALL event_sdl_filter(void *userdata, SDL_Event *event)
237{
238 int cmd, action;
239
240 switch(event->type)
241 {
242 // user clicked on window close button
243 case SDL_QUIT:
244 main_stop();
245 break;
246
247 case SDL_KEYDOWN:
248#if SDL_VERSION_ATLEAST(1,3,0)
249 if (event->key.repeat)
250 return 0;
251
252 event_sdl_keydown(event->key.keysym.scancode, event->key.keysym.mod);
253#else
254 event_sdl_keydown(event->key.keysym.sym, event->key.keysym.mod);
255#endif
256 return 0;
257 case SDL_KEYUP:
258#if SDL_VERSION_ATLEAST(1,3,0)
259 event_sdl_keyup(event->key.keysym.scancode, event->key.keysym.mod);
260#else
261 event_sdl_keyup(event->key.keysym.sym, event->key.keysym.mod);
262#endif
263 return 0;
264
265#if SDL_VERSION_ATLEAST(2,0,0)
266 case SDL_WINDOWEVENT:
267 switch (event->window.event) {
268 case SDL_WINDOWEVENT_RESIZED:
269 // call the video plugin. if the video plugin supports resizing, it will resize its viewport and call
270 // VidExt_ResizeWindow to update the window manager handling our opengl output window
271 gfx.resizeVideoOutput(event->window.data1, event->window.data2);
272 return 0; // consumed the event
273 break;
274 }
275 break;
276#else
277 case SDL_VIDEORESIZE:
278 // call the video plugin. if the video plugin supports resizing, it will resize its viewport and call
279 // VidExt_ResizeWindow to update the window manager handling our opengl output window
280 gfx.resizeVideoOutput(event->resize.w, event->resize.h);
281 return 0; // consumed the event
282 break;
283#endif
284
285 // if joystick action is detected, check if it's mapped to a special function
286 case SDL_JOYAXISMOTION:
287 case SDL_JOYBUTTONDOWN:
288 case SDL_JOYBUTTONUP:
289 case SDL_JOYHATMOTION:
290 for (cmd = 0; cmd < NumJoyCommands; cmd++)
291 {
292 action = MatchJoyCommand(event, (eJoyCommand) cmd);
293 if (action == 1) /* command was just activated (button down, etc) */
294 {
295 if (cmd == joyFullscreen)
296 gfx.changeWindow();
297 else if (cmd == joyStop)
298 main_stop();
299 else if (cmd == joyPause)
300 main_toggle_pause();
301 else if (cmd == joySave)
302 main_state_save(1, NULL); /* save in mupen64plus format using current slot */
303 else if (cmd == joyLoad)
304 main_state_load(NULL); /* load using current slot */
305 else if (cmd == joyIncrement)
306 main_state_inc_slot();
307 else if (cmd == joyScreenshot)
308 main_take_next_screenshot();
309 else if (cmd == joyMute)
310 main_volume_mute();
311 else if (cmd == joyDecrease)
312 main_volume_down();
313 else if (cmd == joyIncrease)
314 main_volume_up();
315 else if (cmd == joyForward)
316 main_set_fastforward(1);
317 else if (cmd == joyGameshark)
318 event_set_gameshark(1);
319 }
320 else if (action == -1) /* command was just de-activated (button up, etc) */
321 {
322 if (cmd == joyForward)
323 main_set_fastforward(0);
324 else if (cmd == joyGameshark)
325 event_set_gameshark(0);
326 }
327 }
328
329 return 0;
330 break;
331 }
332
333 return 1;
334}
335
336/*********************************************************************************************************
337* global functions
338*/
339
340void event_initialize(void)
341{
342 const char *event_str = NULL;
343 int i;
344
345 /* set initial state of all joystick commands to 'off' */
346 for (i = 0; i < NumJoyCommands; i++)
347 JoyCmdActive[i] = 0;
348
349 /* activate any joysticks which are referenced in the joystick event command strings */
350 for (i = 0; i < NumJoyCommands; i++)
351 {
352 event_str = ConfigGetParamString(l_CoreEventsConfig, JoyCmdName[i]);
353 if (event_str != NULL && strlen(event_str) >= 4 && event_str[0] == 'J' && event_str[1] >= '0' && event_str[1] <= '9')
354 {
355 int device = event_str[1] - '0';
356 if (!SDL_WasInit(SDL_INIT_JOYSTICK))
357 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
358#if SDL_VERSION_ATLEAST(2,0,0)
359 SDL_JoystickOpen(device);
360#else
361 if (!SDL_JoystickOpened(device))
362 SDL_JoystickOpen(device);
363#endif
364 }
365 }
366
367 /* set up SDL event filter and disable key repeat */
368#if !SDL_VERSION_ATLEAST(2,0,0)
369 SDL_EnableKeyRepeat(0, 0);
370#endif
371 SDL_SetEventFilter(event_sdl_filter, NULL);
372}
373
374int event_set_core_defaults(void)
375{
376 float fConfigParamsVersion;
377 int bSaveConfig = 0;
378
379 if (ConfigOpenSection("CoreEvents", &l_CoreEventsConfig) != M64ERR_SUCCESS || l_CoreEventsConfig == NULL)
380 {
381 DebugMessage(M64MSG_ERROR, "Failed to open CoreEvents config section.");
382 return 0; /* fail */
383 }
384
385 if (ConfigGetParameter(l_CoreEventsConfig, "Version", M64TYPE_FLOAT, &fConfigParamsVersion, sizeof(float)) != M64ERR_SUCCESS)
386 {
387 DebugMessage(M64MSG_WARNING, "No version number in 'CoreEvents' config section. Setting defaults.");
388 ConfigDeleteSection("CoreEvents");
389 ConfigOpenSection("CoreEvents", &l_CoreEventsConfig);
390 bSaveConfig = 1;
391 }
392 else if (((int) fConfigParamsVersion) != ((int) CONFIG_PARAM_VERSION))
393 {
394 DebugMessage(M64MSG_WARNING, "Incompatible version %.2f in 'CoreEvents' config section: current is %.2f. Setting defaults.", fConfigParamsVersion, (float) CONFIG_PARAM_VERSION);
395 ConfigDeleteSection("CoreEvents");
396 ConfigOpenSection("CoreEvents", &l_CoreEventsConfig);
397 bSaveConfig = 1;
398 }
399 else if ((CONFIG_PARAM_VERSION - fConfigParamsVersion) >= 0.0001f)
400 {
401 /* handle upgrades */
402 float fVersion = CONFIG_PARAM_VERSION;
403 ConfigSetParameter(l_CoreEventsConfig, "Version", M64TYPE_FLOAT, &fVersion);
404 DebugMessage(M64MSG_INFO, "Updating parameter set version in 'CoreEvents' config section to %.2f", fVersion);
405 bSaveConfig = 1;
406 }
407
408 ConfigSetDefaultFloat(l_CoreEventsConfig, "Version", CONFIG_PARAM_VERSION, "Mupen64Plus CoreEvents config parameter set version number. Please don't change this version number.");
409 /* Keyboard presses mapped to core functions */
410 ConfigSetDefaultInt(l_CoreEventsConfig, kbdStop, SDL_SCANCODE_ESCAPE, "SDL keysym for stopping the emulator");
411 ConfigSetDefaultInt(l_CoreEventsConfig, kbdFullscreen, SDL_NUM_SCANCODES, "SDL keysym for switching between fullscreen/windowed modes");
412 ConfigSetDefaultInt(l_CoreEventsConfig, kbdSave, SDL_SCANCODE_F5, "SDL keysym for saving the emulator state");
413 ConfigSetDefaultInt(l_CoreEventsConfig, kbdLoad, SDL_SCANCODE_F7, "SDL keysym for loading the emulator state");
414 ConfigSetDefaultInt(l_CoreEventsConfig, kbdIncrement, 0, "SDL keysym for advancing the save state slot");
415 ConfigSetDefaultInt(l_CoreEventsConfig, kbdReset, SDL_SCANCODE_F9, "SDL keysym for resetting the emulator");
416 ConfigSetDefaultInt(l_CoreEventsConfig, kbdSpeeddown, SDL_SCANCODE_F10, "SDL keysym for slowing down the emulator");
417 ConfigSetDefaultInt(l_CoreEventsConfig, kbdSpeedup, SDL_SCANCODE_F11, "SDL keysym for speeding up the emulator");
418 ConfigSetDefaultInt(l_CoreEventsConfig, kbdScreenshot, SDL_SCANCODE_F12, "SDL keysym for taking a screenshot");
419 ConfigSetDefaultInt(l_CoreEventsConfig, kbdPause, SDL_SCANCODE_P, "SDL keysym for pausing the emulator");
420 ConfigSetDefaultInt(l_CoreEventsConfig, kbdMute, SDL_SCANCODE_M, "SDL keysym for muting/unmuting the sound");
421 ConfigSetDefaultInt(l_CoreEventsConfig, kbdIncrease, SDL_SCANCODE_RIGHTBRACKET,"SDL keysym for increasing the volume");
422 ConfigSetDefaultInt(l_CoreEventsConfig, kbdDecrease, SDL_SCANCODE_LEFTBRACKET, "SDL keysym for decreasing the volume");
423 ConfigSetDefaultInt(l_CoreEventsConfig, kbdForward, SDL_SCANCODE_F, "SDL keysym for temporarily going really fast");
424 ConfigSetDefaultInt(l_CoreEventsConfig, kbdAdvance, SDL_SCANCODE_SLASH, "SDL keysym for advancing by one frame when paused");
425 ConfigSetDefaultInt(l_CoreEventsConfig, kbdGameshark, SDL_SCANCODE_G, "SDL keysym for pressing the game shark button");
426 /* Joystick events mapped to core functions */
427 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyStop], "", "Joystick event string for stopping the emulator");
428 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyFullscreen], "", "Joystick event string for switching between fullscreen/windowed modes");
429 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joySave], "", "Joystick event string for saving the emulator state");
430 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyLoad], "", "Joystick event string for loading the emulator state");
431 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyIncrement], "", "Joystick event string for advancing the save state slot");
432 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyScreenshot], "", "Joystick event string for taking a screenshot");
433 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyPause], "", "Joystick event string for pausing the emulator");
434 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyMute], "", "Joystick event string for muting/unmuting the sound");
435 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyIncrease], "", "Joystick event string for increasing the volume");
436 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyDecrease], "", "Joystick event string for decreasing the volume");
437 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyForward], "", "Joystick event string for fast-forward");
438 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyGameshark], "", "Joystick event string for pressing the game shark button");
439
440 if (bSaveConfig)
441 ConfigSaveSection("CoreEvents");
442
443 return 1;
444}
445
446/*********************************************************************************************************
447* sdl keyup/keydown handlers
448*/
449
450void event_sdl_keydown(int keysym, int keymod)
451{
452 /* check for the only 2 hard-coded key commands: Alt-enter for fullscreen and 0-9 for save state slot */
453 if (keysym == SDL_SCANCODE_RETURN && keymod & (KMOD_LALT | KMOD_RALT))
454 gfx.changeWindow();
455 else if (keysym >= SDL_SCANCODE_0 && keysym <= SDL_SCANCODE_9)
456 main_state_set_slot(keysym - SDL_SCANCODE_0);
457 /* check all of the configurable commands */
458 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdStop))
459 main_stop();
460 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdFullscreen))
461 gfx.changeWindow();
462 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSave))
463 main_state_save(0, NULL); /* save in mupen64plus format using current slot */
464 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdLoad))
465 main_state_load(NULL); /* load using current slot */
466 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdIncrement))
467 main_state_inc_slot();
468 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdReset))
469 reset_soft();
470 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSpeeddown))
471 main_speeddown(5);
472 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSpeedup))
473 main_speedup(5);
474 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdScreenshot))
475 main_take_next_screenshot(); /* screenshot will be taken at the end of frame rendering */
476 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdPause))
477 main_toggle_pause();
478 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdMute))
479 main_volume_mute();
480 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdIncrease))
481 main_volume_up();
482 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdDecrease))
483 main_volume_down();
484 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdForward))
485 main_set_fastforward(1);
486 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdAdvance))
487 main_advance_one();
488 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdGameshark))
489 event_set_gameshark(1);
490 else
491 {
492 /* pass all other keypresses to the input plugin */
493 input.keyDown(keymod, keysym);
494 }
495
496}
497
498void event_sdl_keyup(int keysym, int keymod)
499{
500 if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdStop))
501 {
502 return;
503 }
504 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdForward))
505 {
506 main_set_fastforward(0);
507 }
508 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdGameshark))
509 {
510 event_set_gameshark(0);
511 }
512 else input.keyUp(keymod, keysym);
513
514}
515
516int event_gameshark_active(void)
517{
518 return GamesharkActive;
519}
520
521void event_set_gameshark(int active)
522{
523 // if boolean value doesn't change then just return
524 if (!active == !GamesharkActive)
525 return;
526
527 // set the button state
528 GamesharkActive = (active ? 1 : 0);
529
530 // notify front-end application that gameshark button state has changed
531 StateChanged(M64CORE_INPUT_GAMESHARK, GamesharkActive);
532}
533