all repos — mgba @ 939c3495337773f6f29019256981e4aedb8ec869

mGBA Game Boy Advance Emulator

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

  1#include "sdl-events.h"
  2
  3#include "debugger/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	switch (event->keysym.sym) {
 59	case SDLK_z:
 60		key = GBA_KEY_A;
 61		break;
 62	case SDLK_x:
 63		key = GBA_KEY_B;
 64		break;
 65	case SDLK_a:
 66		key = GBA_KEY_L;
 67		break;
 68	case SDLK_s:
 69		key = GBA_KEY_R;
 70		break;
 71	case SDLK_RETURN:
 72		key = GBA_KEY_START;
 73		break;
 74	case SDLK_BACKSPACE:
 75		key = GBA_KEY_SELECT;
 76		break;
 77	case SDLK_UP:
 78		key = GBA_KEY_UP;
 79		break;
 80	case SDLK_DOWN:
 81		key = GBA_KEY_DOWN;
 82		break;
 83	case SDLK_LEFT:
 84		key = GBA_KEY_LEFT;
 85		break;
 86	case SDLK_RIGHT:
 87		key = GBA_KEY_RIGHT;
 88		break;
 89	case SDLK_F11:
 90		if (event->type == SDL_KEYDOWN && context->debugger) {
 91			ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL);
 92		}
 93		return;
 94	case SDLK_TAB:
 95		context->sync.audioWait = event->type != SDL_KEYDOWN;
 96		return;
 97	case SDLK_LEFTBRACKET:
 98		GBAThreadInterrupt(context);
 99		GBARewind(context, 10);
100		GBAThreadContinue(context);
101		return;
102	default:
103		if (event->type == SDL_KEYDOWN) {
104			if (event->keysym.mod & GUI_MOD) {
105				switch (event->keysym.sym) {
106#if SDL_VERSION_ATLEAST(2, 0, 0)
107				case SDLK_f:
108					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
109					sdlContext->fullscreen = !sdlContext->fullscreen;
110					sdlContext->windowUpdated = 1;
111					break;
112#endif
113				case SDLK_p:
114					GBAThreadTogglePause(context);
115					break;
116				case SDLK_n:
117					GBAThreadPause(context);
118					context->frameCallback = _pauseAfterFrame;
119					GBAThreadUnpause(context);
120					break;
121				default:
122					break;
123				}
124			}
125			if (event->keysym.mod & KMOD_SHIFT) {
126				switch (event->keysym.sym) {
127				case SDLK_F1:
128				case SDLK_F2:
129				case SDLK_F3:
130				case SDLK_F4:
131				case SDLK_F5:
132				case SDLK_F6:
133				case SDLK_F7:
134				case SDLK_F8:
135				case SDLK_F9:
136				case SDLK_F10:
137					GBAThreadInterrupt(context);
138					GBASaveState(context->gba, event->keysym.sym - SDLK_F1);
139					GBAThreadContinue(context);
140					break;
141				default:
142					break;
143				}
144			} else {
145				switch (event->keysym.sym) {
146				case SDLK_F1:
147				case SDLK_F2:
148				case SDLK_F3:
149				case SDLK_F4:
150				case SDLK_F5:
151				case SDLK_F6:
152				case SDLK_F7:
153				case SDLK_F8:
154				case SDLK_F9:
155				case SDLK_F10:
156					GBAThreadInterrupt(context);
157					GBALoadState(context->gba, event->keysym.sym - SDLK_F1);
158					GBAThreadContinue(context);
159					break;
160				default:
161					break;
162				}
163			}
164		}
165		return;
166	}
167
168	if (event->type == SDL_KEYDOWN) {
169		context->activeKeys |= 1 << key;
170	} else {
171		context->activeKeys &= ~(1 << key);
172	}
173}
174
175static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
176	enum GBAKey key = 0;
177	key = GBASDLMapButtonToKey(event->button);
178	if (key == GBA_KEY_NONE) {
179		return;
180	}
181
182	if (event->type == SDL_JOYBUTTONDOWN) {
183		context->activeKeys |= 1 << key;
184	} else {
185		context->activeKeys &= ~(1 << key);
186	}
187}
188
189static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
190	enum GBAKey key = 0;
191
192	if (event->value & SDL_HAT_UP) {
193		key |= 1 << GBA_KEY_UP;
194	}
195	if (event->value & SDL_HAT_LEFT) {
196		key |= 1 << GBA_KEY_LEFT;
197	}
198	if (event->value & SDL_HAT_DOWN) {
199		key |= 1 << GBA_KEY_DOWN;
200	}
201	if (event->value & SDL_HAT_RIGHT) {
202		key |= 1 << GBA_KEY_RIGHT;
203	}
204
205	context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
206	context->activeKeys |= key;
207}
208
209#if SDL_VERSION_ATLEAST(2, 0, 0)
210static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
211	UNUSED(context);
212	switch (event->event) {
213	case SDL_WINDOWEVENT_SIZE_CHANGED:
214		sdlContext->windowUpdated = 1;
215		break;
216	}
217}
218#endif
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#if SDL_VERSION_ATLEAST(2, 0, 0)
233	case SDL_WINDOWEVENT:
234		_GBASDLHandleWindowEvent(context, sdlContext, &event->window);
235		break;
236#endif
237	case SDL_KEYDOWN:
238	case SDL_KEYUP:
239		_GBASDLHandleKeypress(context, sdlContext, &event->key);
240		break;
241	case SDL_JOYBUTTONDOWN:
242	case SDL_JOYBUTTONUP:
243		_GBASDLHandleJoyButton(context, &event->jbutton);
244		break;
245	case SDL_JOYHATMOTION:
246		_GBASDLHandleJoyHat(context, &event->jhat);
247	}
248}