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