all repos — mgba @ 15ece309b7b5ddf6e113db82f1a84bcaa6fc85a9

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-rr.h"
  6#include "gba-serialize.h"
  7#include "gba-video.h"
  8#include "renderers/video-software.h"
  9#include "util/png-io.h"
 10#include "util/vfs.h"
 11
 12#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
 13#define GUI_MOD KMOD_GUI
 14#else
 15#define GUI_MOD KMOD_CTRL
 16#endif
 17
 18#define SDL_BINDING_KEY 0x53444C4B
 19#define SDL_BINDING_BUTTON 0x53444C42
 20
 21static void _takeScreenshot(struct GBAThread*);
 22
 23bool GBASDLInitEvents(struct GBASDLEvents* context) {
 24	if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
 25		return false;
 26	}
 27	SDL_JoystickEventState(SDL_ENABLE);
 28	context->joystick = SDL_JoystickOpen(0);
 29#if !SDL_VERSION_ATLEAST(2, 0, 0)
 30	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 31#endif
 32
 33	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_z, GBA_KEY_A);
 34	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_x, GBA_KEY_B);
 35	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
 36	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
 37	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
 38	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
 39	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
 40	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
 41	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
 42	GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
 43
 44	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 2, GBA_KEY_A);
 45	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 1, GBA_KEY_B);
 46	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 6, GBA_KEY_L);
 47	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 7, GBA_KEY_R);
 48	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 8, GBA_KEY_START);
 49	GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 9, GBA_KEY_SELECT);
 50	return true;
 51}
 52
 53void GBASDLDeinitEvents(struct GBASDLEvents* context) {
 54	SDL_JoystickClose(context->joystick);
 55	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 56}
 57
 58static void _pauseAfterFrame(struct GBAThread* context) {
 59	context->frameCallback = 0;
 60	GBAThreadPauseFromThread(context);
 61}
 62
 63static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
 64	enum GBAKey key = GBA_KEY_NONE;
 65	if (!event->keysym.mod) {
 66		key = GBAInputMapKey(&context->inputMap, SDL_BINDING_KEY, event->keysym.sym);
 67	}
 68	if (key != GBA_KEY_NONE) {
 69		if (event->type == SDL_KEYDOWN) {
 70			context->activeKeys |= 1 << key;
 71		} else {
 72			context->activeKeys &= ~(1 << key);
 73		}
 74		return;
 75	}
 76	switch (event->keysym.sym) {
 77	case SDLK_F11:
 78		if (event->type == SDL_KEYDOWN && context->debugger) {
 79			ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL);
 80		}
 81		return;
 82	case SDLK_F12:
 83		if (event->type == SDL_KEYDOWN) {
 84			GBAThreadInterrupt(context);
 85			_takeScreenshot(context);
 86			GBAThreadContinue(context);
 87		}
 88		return;
 89	case SDLK_TAB:
 90		context->sync.audioWait = event->type != SDL_KEYDOWN;
 91		return;
 92	case SDLK_LEFTBRACKET:
 93		GBAThreadInterrupt(context);
 94		GBARewind(context, 10);
 95		GBAThreadContinue(context);
 96		return;
 97	case SDLK_ESCAPE:
 98		GBAThreadInterrupt(context);
 99		if (context->gba->rr) {
100			GBARRStopPlaying(context->gba->rr);
101			GBARRStopRecording(context->gba->rr);
102		}
103		GBAThreadContinue(context);
104		return;
105	default:
106		if (event->type == SDL_KEYDOWN) {
107			if (event->keysym.mod & GUI_MOD) {
108				switch (event->keysym.sym) {
109#if SDL_VERSION_ATLEAST(2, 0, 0)
110				case SDLK_f:
111					SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
112					sdlContext->fullscreen = !sdlContext->fullscreen;
113					sdlContext->windowUpdated = 1;
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				case SDLK_r:
125					GBAThreadReset(context);
126					break;
127				case SDLK_t:
128					if (context->gba->rr) {
129						GBAThreadReset(context);
130						GBAThreadInterrupt(context);
131						GBARRContextCreate(context->gba);
132						GBARRSetStream(context->gba->rr, context->stateDir);
133						GBARRStopPlaying(context->gba->rr);
134						GBARRStartRecording(context->gba->rr);
135						GBAThreadContinue(context);
136					}
137					break;
138				case SDLK_y:
139					if (context->gba->rr) {
140						GBAThreadReset(context);
141						GBAThreadInterrupt(context);
142						GBARRContextCreate(context->gba);
143						GBARRSetStream(context->gba->rr, context->stateDir);
144						GBARRStopRecording(context->gba->rr);
145						GBARRStartPlaying(context->gba->rr, event->keysym.mod & KMOD_SHIFT);
146						GBAThreadContinue(context);
147					}
148					break;
149				default:
150					break;
151				}
152			}
153			if (event->keysym.mod & KMOD_SHIFT) {
154				switch (event->keysym.sym) {
155				case SDLK_F1:
156				case SDLK_F2:
157				case SDLK_F3:
158				case SDLK_F4:
159				case SDLK_F5:
160				case SDLK_F6:
161				case SDLK_F7:
162				case SDLK_F8:
163				case SDLK_F9:
164				case SDLK_F10:
165					GBAThreadInterrupt(context);
166					GBASaveState(context->gba, context->stateDir, event->keysym.sym - SDLK_F1, true);
167					GBAThreadContinue(context);
168					break;
169				default:
170					break;
171				}
172			} else {
173				switch (event->keysym.sym) {
174				case SDLK_F1:
175				case SDLK_F2:
176				case SDLK_F3:
177				case SDLK_F4:
178				case SDLK_F5:
179				case SDLK_F6:
180				case SDLK_F7:
181				case SDLK_F8:
182				case SDLK_F9:
183				case SDLK_F10:
184					GBAThreadInterrupt(context);
185					GBALoadState(context->gba, context->stateDir, event->keysym.sym - SDLK_F1);
186					GBAThreadContinue(context);
187					break;
188				default:
189					break;
190				}
191			}
192		}
193		return;
194	}
195}
196
197static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
198	enum GBAKey key = 0;
199	key = GBAInputMapKey(&context->inputMap, SDL_BINDING_BUTTON, event->button);
200	if (key == GBA_KEY_NONE) {
201		return;
202	}
203
204	if (event->type == SDL_JOYBUTTONDOWN) {
205		context->activeKeys |= 1 << key;
206	} else {
207		context->activeKeys &= ~(1 << key);
208	}
209}
210
211static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
212	enum GBAKey key = 0;
213
214	if (event->value & SDL_HAT_UP) {
215		key |= 1 << GBA_KEY_UP;
216	}
217	if (event->value & SDL_HAT_LEFT) {
218		key |= 1 << GBA_KEY_LEFT;
219	}
220	if (event->value & SDL_HAT_DOWN) {
221		key |= 1 << GBA_KEY_DOWN;
222	}
223	if (event->value & SDL_HAT_RIGHT) {
224		key |= 1 << GBA_KEY_RIGHT;
225	}
226
227	context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
228	context->activeKeys |= key;
229}
230
231#if SDL_VERSION_ATLEAST(2, 0, 0)
232static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
233	UNUSED(context);
234	switch (event->event) {
235	case SDL_WINDOWEVENT_SIZE_CHANGED:
236		sdlContext->windowUpdated = 1;
237		break;
238	}
239}
240#endif
241
242void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
243	switch (event->type) {
244	case SDL_QUIT:
245		GBAThreadEnd(context);
246		break;
247#if SDL_VERSION_ATLEAST(2, 0, 0)
248	case SDL_WINDOWEVENT:
249		_GBASDLHandleWindowEvent(context, sdlContext, &event->window);
250		break;
251#endif
252	case SDL_KEYDOWN:
253	case SDL_KEYUP:
254		_GBASDLHandleKeypress(context, sdlContext, &event->key);
255		break;
256	case SDL_JOYBUTTONDOWN:
257	case SDL_JOYBUTTONUP:
258		_GBASDLHandleJoyButton(context, &event->jbutton);
259		break;
260	case SDL_JOYHATMOTION:
261		_GBASDLHandleJoyHat(context, &event->jhat);
262	}
263}
264
265static void _takeScreenshot(struct GBAThread* context) {
266		unsigned stride;
267		void* pixels = 0;
268	struct VFile* vf = context->stateDir->openFile(context->stateDir, "screenshot.png", O_CREAT | O_WRONLY);
269	context->gba->video.renderer->getPixels(context->gba->video.renderer, &stride, &pixels);
270	png_structp png = PNGWriteOpen(vf);
271	png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
272	PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels);
273	PNGWriteClose(png, info);
274	vf->close(vf);
275}