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/vfs.h"
10
11#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
12#define GUI_MOD KMOD_GUI
13#else
14#define GUI_MOD KMOD_CTRL
15#endif
16
17#define SDL_BINDING_KEY 0x53444C4B
18#define SDL_BINDING_BUTTON 0x53444C42
19
20bool GBASDLInitEvents(struct GBASDLEvents* context) {
21 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
22 return false;
23 }
24 SDL_JoystickEventState(SDL_ENABLE);
25 context->joystick = SDL_JoystickOpen(0);
26#if !SDL_VERSION_ATLEAST(2, 0, 0)
27 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
28#endif
29
30 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_z, GBA_KEY_A);
31 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_x, GBA_KEY_B);
32 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
33 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
34 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
35 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
36 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
37 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
38 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
39 GBAInputBindKey(context->bindings, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
40
41 GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 2, GBA_KEY_A);
42 GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 1, GBA_KEY_B);
43 GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 6, GBA_KEY_L);
44 GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 7, GBA_KEY_R);
45 GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 8, GBA_KEY_START);
46 GBAInputBindKey(context->bindings, SDL_BINDING_BUTTON, 9, GBA_KEY_SELECT);
47 return true;
48}
49
50void GBASDLDeinitEvents(struct GBASDLEvents* context) {
51 SDL_JoystickClose(context->joystick);
52 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
53}
54
55static void _pauseAfterFrame(struct GBAThread* context) {
56 context->frameCallback = 0;
57 GBAThreadPauseFromThread(context);
58}
59
60static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLEvents* sdlContext, const struct SDL_KeyboardEvent* event) {
61 enum GBAKey key = GBA_KEY_NONE;
62 if (!event->keysym.mod) {
63 key = GBAInputMapKey(&context->inputMap, SDL_BINDING_KEY, event->keysym.sym);
64 }
65 if (key != GBA_KEY_NONE) {
66 if (event->type == SDL_KEYDOWN) {
67 context->activeKeys |= 1 << key;
68 } else {
69 context->activeKeys &= ~(1 << key);
70 }
71 return;
72 }
73 switch (event->keysym.sym) {
74 case SDLK_F11:
75 if (event->type == SDL_KEYDOWN && context->debugger) {
76 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL);
77 }
78 return;
79 case SDLK_F12:
80 if (event->type == SDL_KEYDOWN) {
81 GBAThreadInterrupt(context);
82 GBAThreadTakeScreenshot(context);
83 GBAThreadContinue(context);
84 }
85 return;
86 case SDLK_TAB:
87 context->sync.audioWait = event->type != SDL_KEYDOWN;
88 return;
89 case SDLK_LEFTBRACKET:
90 GBAThreadInterrupt(context);
91 GBARewind(context, 10);
92 GBAThreadContinue(context);
93 return;
94 case SDLK_ESCAPE:
95 GBAThreadInterrupt(context);
96 if (context->gba->rr) {
97 GBARRStopPlaying(context->gba->rr);
98 GBARRStopRecording(context->gba->rr);
99 }
100 GBAThreadContinue(context);
101 return;
102 default:
103 if (event->type == SDL_KEYDOWN) {
104 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.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 case SDLK_r:
122 GBAThreadReset(context);
123 break;
124 case SDLK_t:
125 if (context->stateDir) {
126 GBAThreadReset(context);
127 GBAThreadInterrupt(context);
128 GBARRContextCreate(context->gba);
129 if (!GBARRIsRecording(context->gba->rr)) {
130 GBARRInitStream(context->gba->rr, context->stateDir);
131 GBARRReinitStream(context->gba->rr, INIT_FROM_SAVEGAME);
132 GBARRStopPlaying(context->gba->rr);
133 GBARRStartRecording(context->gba->rr);
134 }
135 GBAThreadContinue(context);
136 }
137 break;
138 case SDLK_y:
139 if (context->stateDir) {
140 GBAThreadReset(context);
141 GBAThreadInterrupt(context);
142 GBARRContextCreate(context->gba);
143 GBARRInitStream(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}