all repos — mgba @ 973abb7b94019b6e154063e1242afa9a93250760

mGBA Game Boy Advance Emulator

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

  1#include "sdl-events.h"
  2
  3#include "debugger.h"
  4#include "gba-io.h"
  5#include "gba-video.h"
  6
  7int GBASDLInitEvents(struct GBASDLEvents* context) {
  8	if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
  9		return 0;
 10	}
 11	SDL_JoystickEventState(SDL_ENABLE);
 12	context->joystick = SDL_JoystickOpen(0);
 13	return 1;
 14}
 15
 16void GBASDLDeinitEvents(struct GBASDLEvents* context) {
 17	SDL_JoystickClose(context->joystick);
 18	SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
 19}
 20
 21static void _GBASDLHandleKeypress(struct GBAThread* context, const struct SDL_KeyboardEvent* event) {
 22	enum GBAKey key = 0;
 23	switch (event->keysym.sym) {
 24	case SDLK_z:
 25		key = GBA_KEY_A;
 26		break;
 27	case SDLK_x:
 28		key = GBA_KEY_B;
 29		break;
 30	case SDLK_a:
 31		key = GBA_KEY_L;
 32		break;
 33	case SDLK_s:
 34		key = GBA_KEY_R;
 35		break;
 36	case SDLK_RETURN:
 37		key = GBA_KEY_START;
 38		break;
 39	case SDLK_BACKSPACE:
 40		key = GBA_KEY_SELECT;
 41		break;
 42	case SDLK_UP:
 43		key = GBA_KEY_UP;
 44		break;
 45	case SDLK_DOWN:
 46		key = GBA_KEY_DOWN;
 47		break;
 48	case SDLK_LEFT:
 49		key = GBA_KEY_LEFT;
 50		break;
 51	case SDLK_RIGHT:
 52		key = GBA_KEY_RIGHT;
 53		break;
 54	case SDLK_TAB:
 55		context->sync.audioWait = !context->sync.audioWait;
 56		return;
 57	default:
 58		return;
 59	}
 60
 61	if (event->type == SDL_KEYDOWN) {
 62		context->activeKeys |= 1 << key;
 63	} else {
 64		context->activeKeys &= ~(1 << key);
 65	}
 66}
 67
 68static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
 69	enum GBAKey key = 0;
 70	// Sorry, hardcoded to my gamepad for now
 71	switch (event->button) {
 72	case 2:
 73		key = GBA_KEY_A;
 74		break;
 75	case 1:
 76		key = GBA_KEY_B;
 77		break;
 78	case 6:
 79		key = GBA_KEY_L;
 80		break;
 81	case 7:
 82		key = GBA_KEY_R;
 83		break;
 84	case 8:
 85		key = GBA_KEY_START;
 86		break;
 87	case 9:
 88		key = GBA_KEY_SELECT;
 89		break;
 90	default:
 91		return;
 92	}
 93
 94	if (event->type == SDL_JOYBUTTONDOWN) {
 95		context->activeKeys |= 1 << key;
 96	} else {
 97		context->activeKeys &= ~(1 << key);
 98	}
 99}
100
101static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
102	enum GBAKey key = 0;
103
104	if (event->value & SDL_HAT_UP) {
105		key |= 1 << GBA_KEY_UP;
106	}
107	if (event->value & SDL_HAT_LEFT) {
108		key |= 1 << GBA_KEY_LEFT;
109	}
110	if (event->value & SDL_HAT_DOWN) {
111		key |= 1 << GBA_KEY_DOWN;
112	}
113	if (event->value & SDL_HAT_RIGHT) {
114		key |= 1 << GBA_KEY_RIGHT;
115	}
116
117	context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
118	context->activeKeys |= key;
119}
120
121void GBASDLHandleEvent(struct GBAThread* context, const union SDL_Event* event) {
122	switch (event->type) {
123	case SDL_QUIT:
124		// FIXME: this isn't thread-safe
125		if (context->debugger) {
126			context->debugger->state = DEBUGGER_EXITING;
127		} else {
128			context->started = 0;
129		}
130		break;
131	case SDL_KEYDOWN:
132	case SDL_KEYUP:
133		_GBASDLHandleKeypress(context, &event->key);
134		break;
135	case SDL_JOYBUTTONDOWN:
136	case SDL_JOYBUTTONUP:
137		_GBASDLHandleJoyButton(context, &event->jbutton);
138		break;
139	case SDL_JOYHATMOTION:
140		_GBASDLHandleJoyHat(context, &event->jhat);
141	}
142}