all repos — mgba @ 90760a30a3681c99db8b5964e671e4d1ac6d581b

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		break;
 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	default:
107		if (event->type == SDL_KEYDOWN) {
108			if (event->keysym.mod & GUI_MOD) {
109				switch (event->keysym.sym) {
110#if SDL_VERSION_ATLEAST(2, 0, 0)
111				case SDLK_f:
112					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
113					sdlContext->fullscreen = !sdlContext->fullscreen;
114					break;
115#endif
116				case SDLK_p:
117					GBAThreadTogglePause(context);
118					break;
119				case SDLK_n:
120					GBAThreadPause(context);
121					context->frameCallback = _pauseAfterFrame;
122					GBAThreadUnpause(context);
123					break;
124				default:
125					break;
126				}
127			}
128			if (event->keysym.mod & KMOD_SHIFT) {
129				switch (event->keysym.sym) {
130				case SDLK_F1:
131				case SDLK_F2:
132				case SDLK_F3:
133				case SDLK_F4:
134				case SDLK_F5:
135				case SDLK_F6:
136				case SDLK_F7:
137				case SDLK_F8:
138				case SDLK_F9:
139				case SDLK_F10:
140					if (!isPaused) {
141						GBAThreadPause(context);
142					}
143					GBASaveState(context->gba, event->keysym.sym - SDLK_F1);
144					if (!isPaused) {
145						GBAThreadUnpause(context);
146					}
147					break;
148				default:
149					break;
150				}
151			} else {
152				switch (event->keysym.sym) {
153				case SDLK_F1:
154				case SDLK_F2:
155				case SDLK_F3:
156				case SDLK_F4:
157				case SDLK_F5:
158				case SDLK_F6:
159				case SDLK_F7:
160				case SDLK_F8:
161				case SDLK_F9:
162				case SDLK_F10:
163					if (!isPaused) {
164						GBAThreadPause(context);
165					}
166					GBALoadState(context->gba, event->keysym.sym - SDLK_F1);
167					if (!isPaused) {
168						GBAThreadUnpause(context);
169					}
170					break;
171				default:
172					break;
173				}
174			}
175		}
176		return;
177	}
178
179	if (event->type == SDL_KEYDOWN) {
180		context->activeKeys |= 1 << key;
181	} else {
182		context->activeKeys &= ~(1 << key);
183	}
184}
185
186static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
187	enum GBAKey key = 0;
188	key = GBASDLMapButtonToKey(event->button);
189	if (key == GBA_KEY_NONE) {
190		return;
191	}
192
193	if (event->type == SDL_JOYBUTTONDOWN) {
194		context->activeKeys |= 1 << key;
195	} else {
196		context->activeKeys &= ~(1 << key);
197	}
198}
199
200static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
201	enum GBAKey key = 0;
202
203	if (event->value & SDL_HAT_UP) {
204		key |= 1 << GBA_KEY_UP;
205	}
206	if (event->value & SDL_HAT_LEFT) {
207		key |= 1 << GBA_KEY_LEFT;
208	}
209	if (event->value & SDL_HAT_DOWN) {
210		key |= 1 << GBA_KEY_DOWN;
211	}
212	if (event->value & SDL_HAT_RIGHT) {
213		key |= 1 << GBA_KEY_RIGHT;
214	}
215
216	context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
217	context->activeKeys |= key;
218}
219
220void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
221	switch (event->type) {
222	case SDL_QUIT:
223		// FIXME: this isn't thread-safe
224		if (context->debugger) {
225			context->debugger->state = DEBUGGER_EXITING;
226		}
227		MutexLock(&context->stateMutex);
228		context->state = THREAD_EXITING;
229		ConditionWake(&context->stateCond);
230		MutexUnlock(&context->stateMutex);
231		break;
232	case SDL_KEYDOWN:
233	case SDL_KEYUP:
234		_GBASDLHandleKeypress(context, sdlContext, &event->key);
235		break;
236	case SDL_JOYBUTTONDOWN:
237	case SDL_JOYBUTTONUP:
238		_GBASDLHandleJoyButton(context, &event->jbutton);
239		break;
240	case SDL_JOYHATMOTION:
241		_GBASDLHandleJoyHat(context, &event->jhat);
242	}
243}