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
9#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
10#define GUI_MOD KMOD_GUI
11#else
12#define GUI_MOD KMOD_CTRL
13#endif
14
15bool GBASDLInitEvents(struct GBASDLEvents* context) {
16 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
17 return false;
18 }
19 SDL_JoystickEventState(SDL_ENABLE);
20 context->joystick = SDL_JoystickOpen(0);
21#if !SDL_VERSION_ATLEAST(2, 0, 0)
22 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
23#endif
24 return true;
25}
26
27void GBASDLDeinitEvents(struct GBASDLEvents* context) {
28 SDL_JoystickClose(context->joystick);
29 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
30}
31
32enum GBAKey GBASDLMapButtonToKey(int button) {
33 // Sorry, hardcoded to my gamepad for now
34 switch (button) {
35 case 2:
36 return GBA_KEY_A;
37 case 1:
38 return GBA_KEY_B;
39 case 6:
40 return GBA_KEY_L;
41 case 7:
42 return GBA_KEY_R;
43 case 8:
44 return GBA_KEY_START;
45 case 9:
46 return GBA_KEY_SELECT;
47 default:
48 return GBA_KEY_NONE;
49 }
50}
51
52static void _pauseAfterFrame(struct GBAThread* context) {
53 context->frameCallback = 0;
54 GBAThreadPause(context);
55}
56
57static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
58 enum GBAKey key = 0;
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 GBAThreadInterrupt(context);
100 GBARewind(context, 10);
101 GBAThreadContinue(context);
102 return;
103 case SDLK_ESCAPE:
104 GBAThreadInterrupt(context);
105 GBARRStopPlaying(context->gba);
106 GBARRStopRecording(context->gba);
107 GBAThreadContinue(context);
108 return;
109 default:
110 if (event->type == SDL_KEYDOWN) {
111 if (event->keysym.mod & GUI_MOD) {
112 switch (event->keysym.sym) {
113#if SDL_VERSION_ATLEAST(2, 0, 0)
114 case SDLK_f:
115 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
116 sdlContext->fullscreen = !sdlContext->fullscreen;
117 sdlContext->windowUpdated = 1;
118 break;
119#endif
120 case SDLK_p:
121 GBAThreadTogglePause(context);
122 break;
123 case SDLK_n:
124 GBAThreadPause(context);
125 context->frameCallback = _pauseAfterFrame;
126 GBAThreadUnpause(context);
127 break;
128 case SDLK_r:
129 GBAThreadReset(context);
130 break;
131 case SDLK_t:
132 GBAThreadReset(context);
133 GBAThreadInterrupt(context);
134 GBARRStopPlaying(context->gba);
135 GBARRStartRecording(context->gba);
136 GBAThreadContinue(context);
137 break;
138 case SDLK_y:
139 GBAThreadReset(context);
140 GBAThreadInterrupt(context);
141 GBARRStopRecording(context->gba);
142 GBARRStartPlaying(context->gba);
143 GBAThreadContinue(context);
144 break;
145 default:
146 break;
147 }
148 }
149 if (event->keysym.mod & KMOD_SHIFT) {
150 switch (event->keysym.sym) {
151 case SDLK_F1:
152 case SDLK_F2:
153 case SDLK_F3:
154 case SDLK_F4:
155 case SDLK_F5:
156 case SDLK_F6:
157 case SDLK_F7:
158 case SDLK_F8:
159 case SDLK_F9:
160 case SDLK_F10:
161 GBAThreadInterrupt(context);
162 GBASaveState(context->gba, event->keysym.sym - SDLK_F1);
163 GBAThreadContinue(context);
164 break;
165 default:
166 break;
167 }
168 } else {
169 switch (event->keysym.sym) {
170 case SDLK_F1:
171 case SDLK_F2:
172 case SDLK_F3:
173 case SDLK_F4:
174 case SDLK_F5:
175 case SDLK_F6:
176 case SDLK_F7:
177 case SDLK_F8:
178 case SDLK_F9:
179 case SDLK_F10:
180 GBAThreadInterrupt(context);
181 GBALoadState(context->gba, event->keysym.sym - SDLK_F1);
182 GBAThreadContinue(context);
183 break;
184 default:
185 break;
186 }
187 }
188 }
189 return;
190 }
191
192 if (event->type == SDL_KEYDOWN) {
193 context->activeKeys |= 1 << key;
194 } else {
195 context->activeKeys &= ~(1 << key);
196 }
197}
198
199static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
200 enum GBAKey key = 0;
201 key = GBASDLMapButtonToKey(event->button);
202 if (key == GBA_KEY_NONE) {
203 return;
204 }
205
206 if (event->type == SDL_JOYBUTTONDOWN) {
207 context->activeKeys |= 1 << key;
208 } else {
209 context->activeKeys &= ~(1 << key);
210 }
211}
212
213static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
214 enum GBAKey key = 0;
215
216 if (event->value & SDL_HAT_UP) {
217 key |= 1 << GBA_KEY_UP;
218 }
219 if (event->value & SDL_HAT_LEFT) {
220 key |= 1 << GBA_KEY_LEFT;
221 }
222 if (event->value & SDL_HAT_DOWN) {
223 key |= 1 << GBA_KEY_DOWN;
224 }
225 if (event->value & SDL_HAT_RIGHT) {
226 key |= 1 << GBA_KEY_RIGHT;
227 }
228
229 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
230 context->activeKeys |= key;
231}
232
233#if SDL_VERSION_ATLEAST(2, 0, 0)
234static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
235 UNUSED(context);
236 switch (event->event) {
237 case SDL_WINDOWEVENT_SIZE_CHANGED:
238 sdlContext->windowUpdated = 1;
239 break;
240 }
241}
242#endif
243
244void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
245 switch (event->type) {
246 case SDL_QUIT:
247 // FIXME: this isn't thread-safe
248 if (context->debugger) {
249 context->debugger->state = DEBUGGER_EXITING;
250 }
251 MutexLock(&context->stateMutex);
252 context->state = THREAD_EXITING;
253 ConditionWake(&context->stateCond);
254 MutexUnlock(&context->stateMutex);
255 break;
256#if SDL_VERSION_ATLEAST(2, 0, 0)
257 case SDL_WINDOWEVENT:
258 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
259 break;
260#endif
261 case SDL_KEYDOWN:
262 case SDL_KEYUP:
263 _GBASDLHandleKeypress(context, sdlContext, &event->key);
264 break;
265 case SDL_JOYBUTTONDOWN:
266 case SDL_JOYBUTTONUP:
267 _GBASDLHandleJoyButton(context, &event->jbutton);
268 break;
269 case SDL_JOYHATMOTION:
270 _GBASDLHandleJoyHat(context, &event->jhat);
271 }
272}