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_9 SDLK_9
46 #define SDL_SetEventFilter(func, data) SDL_SetEventFilter(func)
47 #define event_sdl_filter(userdata, event) event_sdl_filter(const event)
51 #define M64P_CORE_PROTOTYPES 1
53 #include "eventloop.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"
62 /* version number for CoreEvents config section */
63 #define CONFIG_PARAM_VERSION 1.00
65 static m64p_handle l_CoreEventsConfig = NULL;
67 /*********************************************************************************************************
68 * static variables and definitions for eventloop.c
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"
88 typedef enum {joyFullscreen,
102 static const char *JoyCmdName[] = { "Joy Mapping Fullscreen",
105 "Joy Mapping Save State",
106 "Joy Mapping Load State",
107 "Joy Mapping Increment Slot",
108 "Joy Mapping Screenshot",
110 "Joy Mapping Increase Volume",
111 "Joy Mapping Decrease Volume",
112 "Joy Mapping Fast Forward",
113 "Joy Mapping Gameshark"};
115 static const int NumJoyCommands = sizeof(JoyCmdName) / sizeof(const char *);
117 static int JoyCmdActive[16]; /* if extra joystick commands are added above, make sure there is enough room in this array */
119 static int GamesharkActive = 0;
121 /*********************************************************************************************************
122 * static functions for eventloop.c
126 * This function processes an SDL event and updates the JoyCmdActive array if the
127 * event matches with the given command.
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.
133 static int MatchJoyCommand(const SDL_Event *event, eJoyCommand cmd)
135 const char *event_str = ConfigGetParamString(l_CoreEventsConfig, JoyCmdName[cmd]);
136 int dev_number, input_number, input_value;
139 /* Empty string or non-joystick command */
140 if (event_str == NULL || strlen(event_str) < 4 || event_str[0] != 'J')
143 /* Evaluate event based on type of joystick input expected by the given command */
144 switch (event_str[2])
148 if (event->type != SDL_JOYAXISMOTION)
150 if (sscanf(event_str, "J%dA%d%c", &dev_number, &input_number, &axis_direction) != 3)
152 if (dev_number != event->jaxis.which || input_number != event->jaxis.axis)
154 if (axis_direction == '+')
156 if (event->jaxis.value >= 15000 && JoyCmdActive[cmd] == 0)
158 JoyCmdActive[cmd] = 1;
161 else if (event->jaxis.value <= 8000 && JoyCmdActive[cmd] == 1)
163 JoyCmdActive[cmd] = 0;
168 else if (axis_direction == '-')
170 if (event->jaxis.value <= -15000 && JoyCmdActive[cmd] == 0)
172 JoyCmdActive[cmd] = 1;
175 else if (event->jaxis.value >= -8000 && JoyCmdActive[cmd] == 1)
177 JoyCmdActive[cmd] = 0;
182 else return 0; /* invalid axis direction in configuration parameter */
186 if (event->type != SDL_JOYHATMOTION)
188 if (sscanf(event_str, "J%dH%dV%d", &dev_number, &input_number, &input_value) != 3)
190 if (dev_number != event->jhat.which || input_number != event->jhat.hat)
192 if ((event->jhat.value & input_value) == input_value && JoyCmdActive[cmd] == 0)
194 JoyCmdActive[cmd] = 1;
197 else if ((event->jhat.value & input_value) != input_value && JoyCmdActive[cmd] == 1)
199 JoyCmdActive[cmd] = 0;
206 if (event->type != SDL_JOYBUTTONDOWN && event->type != SDL_JOYBUTTONUP)
208 if (sscanf(event_str, "J%dB%d", &dev_number, &input_number) != 2)
210 if (dev_number != event->jbutton.which || input_number != event->jbutton.button)
212 if (event->type == SDL_JOYBUTTONDOWN && JoyCmdActive[cmd] == 0)
214 JoyCmdActive[cmd] = 1;
217 else if (event->type == SDL_JOYBUTTONUP && JoyCmdActive[cmd] == 1)
219 JoyCmdActive[cmd] = 0;
225 /* bad configuration parameter */
229 /* impossible to reach this point */
233 /*********************************************************************************************************
236 static int SDLCALL event_sdl_filter(void *userdata, SDL_Event *event)
242 // user clicked on window close button
248 #if SDL_VERSION_ATLEAST(1,3,0)
249 if (event->key.repeat)
252 event_sdl_keydown(event->key.keysym.scancode, event->key.keysym.mod);
254 event_sdl_keydown(event->key.keysym.sym, event->key.keysym.mod);
258 #if SDL_VERSION_ATLEAST(1,3,0)
259 event_sdl_keyup(event->key.keysym.scancode, event->key.keysym.mod);
261 event_sdl_keyup(event->key.keysym.sym, event->key.keysym.mod);
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
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
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++)
292 action = MatchJoyCommand(event, (eJoyCommand) cmd);
293 if (action == 1) /* command was just activated (button down, etc) */
295 if (cmd == joyFullscreen)
297 else if (cmd == joyStop)
299 else if (cmd == joyPause)
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)
311 else if (cmd == joyDecrease)
313 else if (cmd == joyIncrease)
315 else if (cmd == joyForward)
316 main_set_fastforward(1);
317 else if (cmd == joyGameshark)
318 event_set_gameshark(1);
320 else if (action == -1) /* command was just de-activated (button up, etc) */
322 if (cmd == joyForward)
323 main_set_fastforward(0);
324 else if (cmd == joyGameshark)
325 event_set_gameshark(0);
336 /*********************************************************************************************************
340 void event_initialize(void)
342 const char *event_str = NULL;
345 /* set initial state of all joystick commands to 'off' */
346 for (i = 0; i < NumJoyCommands; i++)
349 /* activate any joysticks which are referenced in the joystick event command strings */
350 for (i = 0; i < NumJoyCommands; i++)
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')
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);
361 if (!SDL_JoystickOpened(device))
362 SDL_JoystickOpen(device);
367 /* set up SDL event filter and disable key repeat */
368 #if !SDL_VERSION_ATLEAST(2,0,0)
369 SDL_EnableKeyRepeat(0, 0);
371 SDL_SetEventFilter(event_sdl_filter, NULL);
374 int event_set_core_defaults(void)
376 float fConfigParamsVersion;
379 if (ConfigOpenSection("CoreEvents", &l_CoreEventsConfig) != M64ERR_SUCCESS || l_CoreEventsConfig == NULL)
381 DebugMessage(M64MSG_ERROR, "Failed to open CoreEvents config section.");
385 if (ConfigGetParameter(l_CoreEventsConfig, "Version", M64TYPE_FLOAT, &fConfigParamsVersion, sizeof(float)) != M64ERR_SUCCESS)
387 DebugMessage(M64MSG_WARNING, "No version number in 'CoreEvents' config section. Setting defaults.");
388 ConfigDeleteSection("CoreEvents");
389 ConfigOpenSection("CoreEvents", &l_CoreEventsConfig);
392 else if (((int) fConfigParamsVersion) != ((int) CONFIG_PARAM_VERSION))
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);
399 else if ((CONFIG_PARAM_VERSION - fConfigParamsVersion) >= 0.0001f)
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);
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");
441 ConfigSaveSection("CoreEvents");
446 /*********************************************************************************************************
447 * sdl keyup/keydown handlers
450 void event_sdl_keydown(int keysym, int keymod)
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))
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))
460 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdFullscreen))
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))
470 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSpeeddown))
472 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdSpeedup))
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))
478 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdMute))
480 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdIncrease))
482 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdDecrease))
484 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdForward))
485 main_set_fastforward(1);
486 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdAdvance))
488 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdGameshark))
489 event_set_gameshark(1);
492 /* pass all other keypresses to the input plugin */
493 input.keyDown(keymod, keysym);
498 void event_sdl_keyup(int keysym, int keymod)
500 if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdStop))
504 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdForward))
506 main_set_fastforward(0);
508 else if (keysym == ConfigGetParamInt(l_CoreEventsConfig, kbdGameshark))
510 event_set_gameshark(0);
512 else input.keyUp(keymod, keysym);
516 int event_gameshark_active(void)
518 return GamesharkActive;
521 void event_set_gameshark(int active)
523 // if boolean value doesn't change then just return
524 if (!active == !GamesharkActive)
527 // set the button state
528 GamesharkActive = (active ? 1 : 0);
530 // notify front-end application that gameshark button state has changed
531 StateChanged(M64CORE_INPUT_GAMESHARK, GamesharkActive);