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->rr);
106 GBARRStopRecording(context->gba->rr);
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 GBARRContextCreate(context->gba);
135 GBARRSetStream(context->gba->rr, context->stateDir);
136 GBARRStopPlaying(context->gba->rr);
137 GBARRStartRecording(context->gba->rr);
138 GBAThreadContinue(context);
139 break;
140 case SDLK_y:
141 GBAThreadReset(context);
142 GBAThreadInterrupt(context);
143 GBARRContextCreate(context->gba);
144 GBARRSetStream(context->gba->rr, context->stateDir);
145 GBARRStopRecording(context->gba->rr);
146 GBARRStartPlaying(context->gba->rr, event->keysym.mod & KMOD_SHIFT);
147 GBAThreadContinue(context);
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, event->keysym.sym - SDLK_F1);
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, event->keysym.sym - SDLK_F1);
186 GBAThreadContinue(context);
187 break;
188 default:
189 break;
190 }
191 }
192 }
193 return;
194 }
195
196 if (event->type == SDL_KEYDOWN) {
197 context->activeKeys |= 1 << key;
198 } else {
199 context->activeKeys &= ~(1 << key);
200 }
201}
202
203static void _GBASDLHandleJoyButton(struct GBAThread* context, const struct SDL_JoyButtonEvent* event) {
204 enum GBAKey key = 0;
205 key = GBASDLMapButtonToKey(event->button);
206 if (key == GBA_KEY_NONE) {
207 return;
208 }
209
210 if (event->type == SDL_JOYBUTTONDOWN) {
211 context->activeKeys |= 1 << key;
212 } else {
213 context->activeKeys &= ~(1 << key);
214 }
215}
216
217static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
218 enum GBAKey key = 0;
219
220 if (event->value & SDL_HAT_UP) {
221 key |= 1 << GBA_KEY_UP;
222 }
223 if (event->value & SDL_HAT_LEFT) {
224 key |= 1 << GBA_KEY_LEFT;
225 }
226 if (event->value & SDL_HAT_DOWN) {
227 key |= 1 << GBA_KEY_DOWN;
228 }
229 if (event->value & SDL_HAT_RIGHT) {
230 key |= 1 << GBA_KEY_RIGHT;
231 }
232
233 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
234 context->activeKeys |= key;
235}
236
237#if SDL_VERSION_ATLEAST(2, 0, 0)
238static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_WindowEvent* event) {
239 UNUSED(context);
240 switch (event->event) {
241 case SDL_WINDOWEVENT_SIZE_CHANGED:
242 sdlContext->windowUpdated = 1;
243 break;
244 }
245}
246#endif
247
248void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLEvents* sdlContext, const union SDL_Event* event) {
249 switch (event->type) {
250 case SDL_QUIT:
251 // FIXME: this isn't thread-safe
252 if (context->debugger) {
253 context->debugger->state = DEBUGGER_EXITING;
254 }
255 MutexLock(&context->stateMutex);
256 context->state = THREAD_EXITING;
257 ConditionWake(&context->stateCond);
258 MutexUnlock(&context->stateMutex);
259 break;
260#if SDL_VERSION_ATLEAST(2, 0, 0)
261 case SDL_WINDOWEVENT:
262 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
263 break;
264#endif
265 case SDL_KEYDOWN:
266 case SDL_KEYUP:
267 _GBASDLHandleKeypress(context, sdlContext, &event->key);
268 break;
269 case SDL_JOYBUTTONDOWN:
270 case SDL_JOYBUTTONUP:
271 _GBASDLHandleJoyButton(context, &event->jbutton);
272 break;
273 case SDL_JOYHATMOTION:
274 _GBASDLHandleJoyHat(context, &event->jhat);
275 }
276}