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 *
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. *
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. *
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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25 #if ! SDL_VERSION_ATLEAST(1,3,0)
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_1 SDLK_1
45 #define SDL_SCANCODE_2 SDLK_2
46 #define SDL_SCANCODE_3 SDLK_3
47 #define SDL_SCANCODE_4 SDLK_4
48 #define SDL_SCANCODE_5 SDLK_5
49 #define SDL_SCANCODE_6 SDLK_6
50 #define SDL_SCANCODE_7 SDLK_7
51 #define SDL_SCANCODE_8 SDLK_8
52 #define SDL_SCANCODE_9 SDLK_9
54 #define SDL_SetEventFilter(func, data) SDL_SetEventFilter(func)
55 #define event_sdl_filter(userdata, event) event_sdl_filter(const event)
59 #define M64P_CORE_PROTOTYPES 1
61 #include "eventloop.h"
63 #include "api/callbacks.h"
64 #include "api/config.h"
65 #include "api/m64p_config.h"
66 #include "plugin/plugin.h"
67 #include "r4300/interupt.h"
68 #include "r4300/reset.h"
70 /* version number for CoreEvents config section */
71 #define CONFIG_PARAM_VERSION 1.00
73 static m64p_handle l_CoreEventsConfig = NULL;
75 /*********************************************************************************************************
76 * static variables and definitions for eventloop.c
79 #define kbdFullscreen "Kbd Mapping Fullscreen"
80 #define kbdStop "Kbd Mapping Stop"
81 #define kbdPause "Kbd Mapping Pause"
82 #define kbdSave "Kbd Mapping Save State"
83 #define kbdLoad "Kbd Mapping Load State"
84 #define kbdIncrement "Kbd Mapping Increment Slot"
85 #define kbdReset "Kbd Mapping Reset"
86 #define kbdSpeeddown "Kbd Mapping Speed Down"
87 #define kbdSpeedup "Kbd Mapping Speed Up"
88 #define kbdScreenshot "Kbd Mapping Screenshot"
89 #define kbdMute "Kbd Mapping Mute"
90 #define kbdIncrease "Kbd Mapping Increase Volume"
91 #define kbdDecrease "Kbd Mapping Decrease Volume"
92 #define kbdForward "Kbd Mapping Fast Forward"
93 #define kbdAdvance "Kbd Mapping Frame Advance"
94 #define kbdGameshark "Kbd Mapping Gameshark"
96 typedef enum {joyFullscreen,
110 static const char *JoyCmdName[] = { "Joy Mapping Fullscreen",
113 "Joy Mapping Save State",
114 "Joy Mapping Load State",
115 "Joy Mapping Increment Slot",
116 "Joy Mapping Screenshot",
118 "Joy Mapping Increase Volume",
119 "Joy Mapping Decrease Volume",
120 "Joy Mapping Fast Forward",
121 "Joy Mapping Gameshark"};
123 static const int NumJoyCommands = sizeof(JoyCmdName) / sizeof(const char *);
125 static int JoyCmdActive[16]; /* if extra joystick commands are added above, make sure there is enough room in this array */
127 static int GamesharkActive = 0;
129 /*********************************************************************************************************
130 * static functions for eventloop.c
134 * This function processes an SDL event and updates the JoyCmdActive array if the
135 * event matches with the given command.
137 * If the event activates a joystick command which was not previously active, then
138 * a 1 is returned. If the event de-activates an command, a -1 is returned. Otherwise
139 * (if the event does not match the command or active status didn't change), a 0 is returned.
141 static int MatchJoyCommand(const SDL_Event *event, eJoyCommand cmd)
143 const char *event_str = ConfigGetParamString(l_CoreEventsConfig, JoyCmdName[cmd]);
144 int dev_number, input_number, input_value;
147 /* Empty string or non-joystick command */
148 if (event_str == NULL || strlen(event_str) < 4 || event_str[0] != 'J')
151 /* Evaluate event based on type of joystick input expected by the given command */
152 switch (event_str[2])
156 if (event->type != SDL_JOYAXISMOTION)
158 if (sscanf(event_str, "J%dA%d%c", &dev_number, &input_number, &axis_direction) != 3)
160 if (dev_number != event->jaxis.which || input_number != event->jaxis.axis)
162 if (axis_direction == '+')
164 if (event->jaxis.value >= 15000 && JoyCmdActive[cmd] == 0)
166 JoyCmdActive[cmd] = 1;
169 else if (event->jaxis.value <= 8000 && JoyCmdActive[cmd] == 1)
171 JoyCmdActive[cmd] = 0;
176 else if (axis_direction == '-')
178 if (event->jaxis.value <= -15000 && JoyCmdActive[cmd] == 0)
180 JoyCmdActive[cmd] = 1;
183 else if (event->jaxis.value >= -8000 && JoyCmdActive[cmd] == 1)
185 JoyCmdActive[cmd] = 0;
190 else return 0; /* invalid axis direction in configuration parameter */
194 if (event->type != SDL_JOYHATMOTION)
196 if (sscanf(event_str, "J%dH%dV%d", &dev_number, &input_number, &input_value) != 3)
198 if (dev_number != event->jhat.which || input_number != event->jhat.hat)
200 if ((event->jhat.value & input_value) == input_value && JoyCmdActive[cmd] == 0)
202 JoyCmdActive[cmd] = 1;
205 else if ((event->jhat.value & input_value) != input_value && JoyCmdActive[cmd] == 1)
207 JoyCmdActive[cmd] = 0;
214 if (event->type != SDL_JOYBUTTONDOWN && event->type != SDL_JOYBUTTONUP)
216 if (sscanf(event_str, "J%dB%d", &dev_number, &input_number) != 2)
218 if (dev_number != event->jbutton.which || input_number != event->jbutton.button)
220 if (event->type == SDL_JOYBUTTONDOWN && JoyCmdActive[cmd] == 0)
222 JoyCmdActive[cmd] = 1;
225 else if (event->type == SDL_JOYBUTTONUP && JoyCmdActive[cmd] == 1)
227 JoyCmdActive[cmd] = 0;
233 /* bad configuration parameter */
237 /* impossible to reach this point */
241 /*********************************************************************************************************
244 static int SDLCALL event_sdl_filter(void *userdata, SDL_Event *event)
250 // user clicked on window close button
256 #if SDL_VERSION_ATLEAST(1,3,0)
257 if (event->key.repeat)
260 event_sdl_keydown(event->key.keysym.scancode, event->key.keysym.mod);
262 event_sdl_keydown(event->key.keysym.sym, event->key.keysym.mod);
266 #if SDL_VERSION_ATLEAST(1,3,0)
267 event_sdl_keyup(event->key.keysym.scancode, event->key.keysym.mod);
269 event_sdl_keyup(event->key.keysym.sym, event->key.keysym.mod);
273 #if SDL_VERSION_ATLEAST(2,0,0)
274 case SDL_WINDOWEVENT:
275 switch (event->window.event) {
276 case SDL_WINDOWEVENT_RESIZED:
277 // call the video plugin. if the video plugin supports resizing, it will resize its viewport and call
278 // VidExt_ResizeWindow to update the window manager handling our opengl output window
279 gfx.resizeVideoOutput(event->window.data1, event->window.data2);
280 return 0; // consumed the event
285 case SDL_VIDEORESIZE:
286 // call the video plugin. if the video plugin supports resizing, it will resize its viewport and call
287 // VidExt_ResizeWindow to update the window manager handling our opengl output window
288 gfx.resizeVideoOutput(event->resize.w, event->resize.h);
289 return 0; // consumed the event
293 // if joystick action is detected, check if it's mapped to a special function
294 case SDL_JOYAXISMOTION:
295 case SDL_JOYBUTTONDOWN:
296 case SDL_JOYBUTTONUP:
297 case SDL_JOYHATMOTION:
298 for (cmd = 0; cmd < NumJoyCommands; cmd++)
300 action = MatchJoyCommand(event, (eJoyCommand) cmd);
301 if (action == 1) /* command was just activated (button down, etc) */
303 if (cmd == joyFullscreen)
305 else if (cmd == joyStop)
307 else if (cmd == joyPause)
309 else if (cmd == joySave)
310 main_state_save(1, NULL); /* save in mupen64plus format using current slot */
311 else if (cmd == joyLoad)
312 main_state_load(NULL); /* load using current slot */
313 else if (cmd == joyIncrement)
314 main_state_inc_slot();
315 else if (cmd == joyScreenshot)
316 main_take_next_screenshot();
317 else if (cmd == joyMute)
319 else if (cmd == joyDecrease)
321 else if (cmd == joyIncrease)
323 else if (cmd == joyForward)
324 main_set_fastforward(1);
325 else if (cmd == joyGameshark)
326 event_set_gameshark(1);
328 else if (action == -1) /* command was just de-activated (button up, etc) */
330 if (cmd == joyForward)
331 main_set_fastforward(0);
332 else if (cmd == joyGameshark)
333 event_set_gameshark(0);
344 /*********************************************************************************************************
348 void event_initialize(void)
350 const char *event_str = NULL;
353 /* set initial state of all joystick commands to 'off' */
354 for (i = 0; i < NumJoyCommands; i++)
357 /* activate any joysticks which are referenced in the joystick event command strings */
358 for (i = 0; i < NumJoyCommands; i++)
360 event_str = ConfigGetParamString(l_CoreEventsConfig, JoyCmdName[i]);
361 if (event_str != NULL && strlen(event_str) >= 4 && event_str[0] == 'J' && event_str[1] >= '0' && event_str[1] <= '9')
363 int device = event_str[1] - '0';
364 if (!SDL_WasInit(SDL_INIT_JOYSTICK))
365 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
366 #if SDL_VERSION_ATLEAST(2,0,0)
367 SDL_JoystickOpen(device);
369 if (!SDL_JoystickOpened(device))
370 SDL_JoystickOpen(device);
375 /* set up SDL event filter and disable key repeat */
376 #if !SDL_VERSION_ATLEAST(2,0,0)
377 SDL_EnableKeyRepeat(0, 0);
379 SDL_SetEventFilter(event_sdl_filter, NULL);
382 int event_set_core_defaults(void)
384 float fConfigParamsVersion;
387 if (ConfigOpenSection("CoreEvents", &l_CoreEventsConfig) != M64ERR_SUCCESS || l_CoreEventsConfig == NULL)
389 DebugMessage(M64MSG_ERROR, "Failed to open CoreEvents config section.");
393 if (ConfigGetParameter(l_CoreEventsConfig, "Version", M64TYPE_FLOAT, &fConfigParamsVersion, sizeof(float)) != M64ERR_SUCCESS)
395 DebugMessage(M64MSG_WARNING, "No version number in 'CoreEvents' config section. Setting defaults.");
396 ConfigDeleteSection("CoreEvents");
397 ConfigOpenSection("CoreEvents", &l_CoreEventsConfig);
400 else if (((int) fConfigParamsVersion) != ((int) CONFIG_PARAM_VERSION))
402 DebugMessage(M64MSG_WARNING, "Incompatible version %.2f in 'CoreEvents' config section: current is %.2f. Setting defaults.", fConfigParamsVersion, (float) CONFIG_PARAM_VERSION);
403 ConfigDeleteSection("CoreEvents");
404 ConfigOpenSection("CoreEvents", &l_CoreEventsConfig);
407 else if ((CONFIG_PARAM_VERSION - fConfigParamsVersion) >= 0.0001f)
409 /* handle upgrades */
410 float fVersion = CONFIG_PARAM_VERSION;
411 ConfigSetParameter(l_CoreEventsConfig, "Version", M64TYPE_FLOAT, &fVersion);
412 DebugMessage(M64MSG_INFO, "Updating parameter set version in 'CoreEvents' config section to %.2f", fVersion);
416 ConfigSetDefaultFloat(l_CoreEventsConfig, "Version", CONFIG_PARAM_VERSION, "Mupen64Plus CoreEvents config parameter set version number. Please don't change this version number.");
417 /* Keyboard presses mapped to core functions */
418 ConfigSetDefaultInt(l_CoreEventsConfig, kbdStop, SDL_SCANCODE_ESCAPE, "SDL keysym for stopping the emulator");
419 ConfigSetDefaultInt(l_CoreEventsConfig, kbdFullscreen, SDL_NUM_SCANCODES, "SDL keysym for switching between fullscreen/windowed modes");
420 ConfigSetDefaultInt(l_CoreEventsConfig, kbdSave, SDL_SCANCODE_F5, "SDL keysym for saving the emulator state");
421 ConfigSetDefaultInt(l_CoreEventsConfig, kbdLoad, SDL_SCANCODE_F7, "SDL keysym for loading the emulator state");
422 ConfigSetDefaultInt(l_CoreEventsConfig, kbdIncrement, 0, "SDL keysym for advancing the save state slot");
423 ConfigSetDefaultInt(l_CoreEventsConfig, kbdReset, SDL_SCANCODE_F9, "SDL keysym for resetting the emulator");
424 ConfigSetDefaultInt(l_CoreEventsConfig, kbdSpeeddown, SDL_SCANCODE_F10, "SDL keysym for slowing down the emulator");
425 ConfigSetDefaultInt(l_CoreEventsConfig, kbdSpeedup, SDL_SCANCODE_F11, "SDL keysym for speeding up the emulator");
426 ConfigSetDefaultInt(l_CoreEventsConfig, kbdScreenshot, SDL_SCANCODE_F12, "SDL keysym for taking a screenshot");
427 ConfigSetDefaultInt(l_CoreEventsConfig, kbdPause, SDL_SCANCODE_P, "SDL keysym for pausing the emulator");
428 ConfigSetDefaultInt(l_CoreEventsConfig, kbdMute, SDL_SCANCODE_M, "SDL keysym for muting/unmuting the sound");
429 ConfigSetDefaultInt(l_CoreEventsConfig, kbdIncrease, SDL_SCANCODE_RIGHTBRACKET,"SDL keysym for increasing the volume");
430 ConfigSetDefaultInt(l_CoreEventsConfig, kbdDecrease, SDL_SCANCODE_LEFTBRACKET, "SDL keysym for decreasing the volume");
431 ConfigSetDefaultInt(l_CoreEventsConfig, kbdForward, SDL_SCANCODE_F, "SDL keysym for temporarily going really fast");
432 ConfigSetDefaultInt(l_CoreEventsConfig, kbdAdvance, SDL_SCANCODE_SLASH, "SDL keysym for advancing by one frame when paused");
433 ConfigSetDefaultInt(l_CoreEventsConfig, kbdGameshark, SDL_SCANCODE_G, "SDL keysym for pressing the game shark button");
434 /* Joystick events mapped to core functions */
435 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyStop], "", "Joystick event string for stopping the emulator");
436 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyFullscreen], "", "Joystick event string for switching between fullscreen/windowed modes");
437 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joySave], "", "Joystick event string for saving the emulator state");
438 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyLoad], "", "Joystick event string for loading the emulator state");
439 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyIncrement], "", "Joystick event string for advancing the save state slot");
440 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyScreenshot], "", "Joystick event string for taking a screenshot");
441 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyPause], "", "Joystick event string for pausing the emulator");
442 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyMute], "", "Joystick event string for muting/unmuting the sound");
443 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyIncrease], "", "Joystick event string for increasing the volume");
444 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyDecrease], "", "Joystick event string for decreasing the volume");
445 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyForward], "", "Joystick event string for fast-forward");
446 ConfigSetDefaultString(l_CoreEventsConfig, JoyCmdName[joyGameshark], "", "Joystick event string for pressing the game shark button");
449 ConfigSaveSection("CoreEvents");
454 static int get_saveslot_from_keysym(int keysym)
482 /*********************************************************************************************************
483 * sdl keyup/keydown handlers
486 void event_sdl_keydown(int keysym, int keymod)
490 /* check for the only 2 hard-coded key commands: Alt-enter for fullscreen and 0-9 for save state slot */
491 if (keysym == SDL_SCANCODE_RETURN && keymod & (KMOD_LALT | KMOD_RALT))
493 else if ((slot = get_saveslot_from_keysym(keysym)) >= 0)
494 main_state_set_slot(slot);
495 /* check all of the configurable commands */
496 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdStop))
498 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdFullscreen))
500 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSave))
501 main_state_save(0, NULL); /* save in mupen64plus format using current slot */
502 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdLoad))
503 main_state_load(NULL); /* load using current slot */
504 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdIncrement))
505 main_state_inc_slot();
506 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdReset))
508 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSpeeddown))
510 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSpeedup))
512 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdScreenshot))
513 main_take_next_screenshot(); /* screenshot will be taken at the end of frame rendering */
514 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdPause))
516 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdMute))
518 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdIncrease))
520 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdDecrease))
522 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdForward))
523 main_set_fastforward(1);
524 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdAdvance))
526 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdGameshark))
527 event_set_gameshark(1);
530 /* pass all other keypresses to the input plugin */
531 input.keyDown(keymod, keysym);
536 void event_sdl_keyup(int keysym, int keymod)
538 if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdStop))
542 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdForward))
544 main_set_fastforward(0);
546 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdGameshark))
548 event_set_gameshark(0);
550 else input.keyUp(keymod, keysym);
554 int event_gameshark_active(void)
556 return GamesharkActive;
559 void event_set_gameshark(int active)
561 // if boolean value doesn't change then just return
562 if (!active == !GamesharkActive)
565 // set the button state
566 GamesharkActive = (active ? 1 : 0);
568 // notify front-end application that gameshark button state has changed
569 StateChanged(M64CORE_INPUT_GAMESHARK, GamesharkActive);