all repos — mgba @ e9c441281988d973b8e9fc7956e983249f7c8acd

mGBA Game Boy Advance Emulator

src/platform/sdl/sdl-events.c (view raw)

  1#include "sdl-events.h"
  2
  3#include "debugger.h"
  4#include "gba-io.h"
  5#include "gba-serialize.h"
  6#include "gba-video.h"
  7
  8#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
  9#define GUI_MOD KMOD_GUI
 10#else
 11#define GUI_MOD KMOD_CTRL
 12#endif
 13
 14int GBASDLInitEvents(struct GBASDLEvents* context) {
 15	if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
 16		return 0;
 17	}
 18	SDL_JoystickEventState(SDL_ENABLE);
 19	context->joystick = SDL_JoystickOpen(0);
 20#if !SDL_VERSION_ATLEAST(2, 0, 0)
 21	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 22#endif
 23	return 1;
 24}
 25
 26void GBASDLDeinitEvents(struct GBASDLEvents* context) {
 27	SDL_JoystickClose(context->joystick);
 28	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 29}
 30
 31enum GBAKey GBASDLMapButtonToKey(int button) {
 32	// Sorry, hardcoded to my gamepad for now
 33	switch (button) {
 34	case 2:
 35		return GBA_KEY_A;
 36	case 1:
 37		return GBA_KEY_B;
 38	case 6:
 39		return GBA_KEY_L;
 40	case 7:
 41		return GBA_KEY_R;
 42	case 8:
 43		return GBA_KEY_START;
 44	case 9:
 45		return GBA_KEY_SELECT;
 46	default:
 47		return GBA_KEY_NONE;
 48	}
 49}
 50
 51static void _pauseAfterFrame(struct GBAThread* context) {
 52	context->frameCallback = 0;
 53	GBAThreadPause(context);
 54}
 55
 56static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
 57	enum GBAKey key = 0;
 58	int isPaused = GBAThreadIsPaused(context);
 59	switch (event->keysym.sym) {
 60	case SDLK_z:
 61		key = GBA_KEY_A;
 62		break;
 63	case SDLK_x:
 64		key = GBA_KEY_B;
 65		break;
 66	case SDLK_a:
 67		key = GBA_KEY_L;
 68		break;
 69	case SDLK_s:
 70		key = GBA_KEY_R;
 71		break;
 72	case SDLK_RETURN:
 73		key = GBA_KEY_START;
 74		break;
 75	case SDLK_BACKSPACE:
 76		key = GBA_KEY_SELECT;
 77		break;
 78	case SDLK_UP:
 79		key = GBA_KEY_UP;
 80		break;
 81	case SDLK_DOWN:
 82		key = GBA_KEY_DOWN;
 83		break;
 84	case SDLK_LEFT:
 85		key = GBA_KEY_LEFT;
 86		break;
 87	case SDLK_RIGHT:
 88		key = GBA_KEY_RIGHT;
 89		break;
 90	case SDLK_F11:
 91		if (event->type == SDL_KEYDOWN && context->debugger) {
 92			ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL);
 93		}
 94		return;
 95	case SDLK_TAB:
 96		context->sync.audioWait = event->type != SDL_KEYDOWN;
 97		return;
 98	case SDLK_LEFTBRACKET:
 99		if (!isPaused) {
100			GBAThreadPause(context);
101		}
102		GBARewind(context, 10);
103		if (!isPaused) {
104			GBAThreadUnpause(context);
105		}
106		return;
107	default:
108		if (event->type == SDL_KEYDOWN) {
109			if (event->keysym.mod & GUI_MOD) {
110				switch (event->keysym.sym) {
111#if SDL_VERSION_ATLEAST(2, 0, 0)
112				case SDLK_f:
113					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
114					sdlContext->fullscreen = !sdlContext->fullscreen;
115					break;
116#endif
117				case SDLK_p:
118					GBAThreadTogglePause(context);
119					break;
120				case SDLK_n:
121					GBAThreadPause(context);
122					context->frameCallback = _pauseAfterFrame;
123					GBAThreadUnpause(context);
124					break;
125				default:
126					break;
127				}
128			}
129			if (event->keysym.mod & KMOD_SHIFT) {
130				switch (event->keysym.sym) {
131				case SDLK_F1:
132				case SDLK_F2:
133				case SDLK_F3:
134				case SDLK_F4:
135				case SDLK_F5:
136				case SDLK_F6:
137				case SDLK_F7:
138				case SDLK_F8:
139				case SDLK_F9:
140				case SDLK_F10:
141					if (!isPaused) {
142						GBAThreadPause(context);
143					}
144					GBASaveState(context->gba, event->keysym.sym - SDLK_F1);
145					if (!isPaused) {
146						GBAThreadUnpause(context);
147					}
148					break;
149				default:
150					break;
151				}
152			} else {
153				switch (event->keysym.sym) {
154				case SDLK_F1:
155				case SDLK_F2:
156				case SDLK_F3:
157				case SDLK_F4:
158				case SDLK_F5:
159				case SDLK_F6:
160				case SDLK_F7:
161				case SDLK_F8:
162				case SDLK_F9:
163				case SDLK_F10:
164					if (!isPaused) {
165						GBAThreadPause(context);
166					}
167					GBALoadState(context->gba, event->keysym.sym - SDLK_F1);
168					if (!isPaused) {
169						GBAThreadUnpause(context);
170					}
171					break;
172				default:
173					break;
174				}
175			}
176		}
177		return;
178	}
179
180	if (event->type == SDL_KEYDOWN) {
181		context->activeKeys |= 1 << key;
182	} else {
183		context->activeKeys &= ~(1 << key);
184	}
185}
186
187static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
188	enum GBAKey key = 0;
189	key = GBASDLMapButtonToKey(event->button);
190	if (key == GBA_KEY_NONE) {
191		return;
192	}
193
194	if (event->type == SDL_JOYBUTTONDOWN) {
195		context->activeKeys |= 1 << key;
196	} else {
197		context->activeKeys &= ~(1 << key);
198	}
199}
200
201static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
202	enum GBAKey key = 0;
203
204	if (event->value & SDL_HAT_UP) {
205		key |= 1 << GBA_KEY_UP;
206	}
207	if (event->value & SDL_HAT_LEFT) {
208		key |= 1 << GBA_KEY_LEFT;
209	}
210	if (event->value & SDL_HAT_DOWN) {
211		key |= 1 << GBA_KEY_DOWN;
212	}
213	if (event->value & SDL_HAT_RIGHT) {
214		key |= 1 << GBA_KEY_RIGHT;
215	}
216
217	context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
218	context->activeKeys |= key;
219}
220
221void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
222	switch (event->type) {
223	case SDL_QUIT:
224		// FIXME: this isn't thread-safe
225		if (context->debugger) {
226			context->debugger->state = DEBUGGER_EXITING;
227		}
228		MutexLock(&context->stateMutex);
229		context->state = THREAD_EXITING;
230		ConditionWake(&context->stateCond);
231		MutexUnlock(&context->stateMutex);
232		break;
233	case SDL_KEYDOWN:
234	case SDL_KEYUP:
235		_GBASDLHandleKeypress(context, sdlContext, &event->key);
236		break;
237	case SDL_JOYBUTTONDOWN:
238	case SDL_JOYBUTTONUP:
239		_GBASDLHandleJoyButton(context, &event->jbutton);
240		break;
241	case SDL_JOYHATMOTION:
242		_GBASDLHandleJoyHat(context, &event->jhat);
243	}
244}