aboutsummaryrefslogtreecommitdiff
path: root/src/input.c
blob: db491dd8ec00babb8c0e0aba9a30b6b3867cc51e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <SDL3/SDL_log.h>

#include "input.h"

struct pw_input *pw_input_init(void) {
  struct pw_input *input = SDL_malloc(sizeof(*input));
  if (!input) {
    return NULL;
  }

  SDL_zerop(input);
  return input;
}

void pw_input_free(struct pw_input *input) {
  if (!input) {
    SDL_InvalidParamError("input");
    return;
  }

  SDL_free(input);
}

static void set_button(enum pw_button *player, enum pw_button button,
                       bool pressed) {
  if (pressed) {
    *player |= button;
  } else {
    *player &= ~button;
  }
}

static bool handle_axis(struct pw_input *input, struct pw_players *players,
                        SDL_GamepadAxisEvent *event) {
  size_t i = pw_player_find(players, event->which);
  if (i >= PW_PLAYERS_MAX) {
    return true;
  }
  enum pw_button *player = &input->players[i];

  switch (event->axis) {
  case SDL_GAMEPAD_AXIS_LEFTX:
    set_button(player, PW_BUTTON_LEFT, event->value < -PW_AXIS_THRESHOLD);
    set_button(player, PW_BUTTON_RIGHT, event->value > PW_AXIS_THRESHOLD);
    break;
  case SDL_GAMEPAD_AXIS_LEFTY:
    set_button(player, PW_BUTTON_UP, event->value < -PW_AXIS_THRESHOLD);
    set_button(player, PW_BUTTON_DOWN, event->value > PW_AXIS_THRESHOLD);
    break;
  }

  // Some special POOM handling, which uses both player's inputs for controls.
  // This allows one player's directions be used for turning, and another for
  // strafing.
  if (!i && pw_players_count(players) == 1) {
    enum pw_button *p2 = &input->players[1];
    switch (event->axis) {
    case SDL_GAMEPAD_AXIS_RIGHTX:
      set_button(p2, PW_BUTTON_LEFT, event->value < -PW_AXIS_THRESHOLD);
      set_button(p2, PW_BUTTON_RIGHT, event->value > PW_AXIS_THRESHOLD);
      break;
    case SDL_GAMEPAD_AXIS_RIGHTY:
      set_button(p2, PW_BUTTON_UP, event->value < -PW_AXIS_THRESHOLD);
      set_button(p2, PW_BUTTON_DOWN, event->value > PW_AXIS_THRESHOLD);
      break;
    case SDL_GAMEPAD_AXIS_LEFT_TRIGGER:
      set_button(p2, PW_BUTTON_O, event->value > PW_TRIGGER_THRESHOLD);
      break;
    case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER:
      set_button(p2, PW_BUTTON_X, event->value > PW_TRIGGER_THRESHOLD);
      break;
    }
  }

  return true;
}

static bool handle_button(struct pw_input *input, struct pw_players *players,
                          SDL_GamepadButtonEvent *event) {
  size_t i = pw_player_find(players, event->which);
  if (i >= PW_PLAYERS_MAX) {
    return true;
  }
  enum pw_button *player = &input->players[i];

  switch (event->button) {
  case SDL_GAMEPAD_BUTTON_SOUTH:
    set_button(player, PW_BUTTON_O, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_EAST:
    set_button(player, PW_BUTTON_X, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_WEST:
    set_button(player, PW_BUTTON_X, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_NORTH:
    set_button(player, PW_BUTTON_O, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_BACK:
    set_button(player, PW_BUTTON_MENU, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_START:
    set_button(player, PW_BUTTON_MENU, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER:
    set_button(player, PW_BUTTON_O, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER:
    set_button(player, PW_BUTTON_X, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_DPAD_UP:
    set_button(player, PW_BUTTON_UP, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
    set_button(player, PW_BUTTON_DOWN, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
    set_button(player, PW_BUTTON_LEFT, event->down);
    break;
  case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
    set_button(player, PW_BUTTON_RIGHT, event->down);
    break;
  }

  return true;
}

static bool enqueue_item(struct pw_keyboard_queue *queue, int16_t item) {
  size_t next = (queue->end + 1) % PW_KEYBOARD_BUFFER_SIZE;
  if (next == queue->start) {
    return false;
  }

  queue->items[queue->end] = item;
  queue->end = next;
  return true;
}

static int8_t scancode_to_p8scii(SDL_Scancode sc, SDL_Keymod mod) {
  switch (sc) {
  case SDL_SCANCODE_TAB:
    return 9;
  case SDL_SCANCODE_RETURN:
  case SDL_SCANCODE_KP_ENTER:
    return 13;
  case SDL_SCANCODE_BACKSPACE:
    return 8;
  case SDL_SCANCODE_ESCAPE:
    return 27;
  default:
    break;
  }

  // Clear other modifiers, PICO-8 requests things like Ctrl and Alt through a
  // separate `pw_input_keymod` call.
  mod &= SDL_KMOD_SHIFT | SDL_KMOD_CAPS;

  SDL_Keycode keycode = SDL_GetKeyFromScancode(sc, mod, false);
  if (keycode < 32 || keycode > 126) {
    // Non-ASCII characters (including the exceptions above) are mapped to
    // negative values which are ignored. Note that we _still_ need to push
    // these to the queue, as they trigger some generic "key pressed" code
    // in the PICO-8 runtime.
    return -1;
  }

  return (int8_t)keycode;
}

static bool handle_key(struct pw_input *input, SDL_KeyboardEvent *event) {
  if (event->scancode < 0 || event->scancode > INT16_MAX) {
    return false;
  }

  int16_t scancode = (int16_t)event->scancode;
  if (!event->down) {
    scancode = -scancode;
  }
  if (!enqueue_item(&input->keyboard.scancodes, scancode)) {
    SDL_LogDebug(0, "keyboard event queue full, dropping scancode");
  }

  int16_t key = (int16_t)scancode_to_p8scii(event->scancode, event->mod);
  if (!enqueue_item(&input->keyboard.text, key)) {
    SDL_LogDebug(0, "keyboard text queue full, dropping key");
  }

  return true;
}

bool pw_input_event(struct pw_input *input, struct pw_players *players,
                    SDL_Event *event) {
  if (!input) {
    SDL_InvalidParamError("input");
    return false;
  }
  if (!players) {
    SDL_InvalidParamError("players");
    return false;
  }
  if (!event) {
    SDL_InvalidParamError("event");
    return false;
  }

  switch (event->type) {
  case SDL_EVENT_GAMEPAD_AXIS_MOTION:
    return handle_axis(input, players, &event->gaxis);
  case SDL_EVENT_GAMEPAD_BUTTON_UP:
  case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
    return handle_button(input, players, &event->gbutton);
  case SDL_EVENT_KEY_DOWN:
  case SDL_EVENT_KEY_UP:
    return handle_key(input, &event->key);
  default:
    return true;
  }
}

static void update_keymod(struct pw_input *input) {
  input->keyboard.mod = PW_KEYMOD_NONE;

  SDL_Keymod state = SDL_GetModState();
  if (state & SDL_KMOD_LSHIFT) {
    input->keyboard.mod |= PW_KEYMOD_LSHIFT;
  }
  if (state & SDL_KMOD_RSHIFT) {
    input->keyboard.mod |= PW_KEYMOD_RSHIFT;
  }
  if (state & SDL_KMOD_LCTRL) {
    input->keyboard.mod |= PW_KEYMOD_LCTRL;
  }
  if (state & SDL_KMOD_RCTRL) {
    input->keyboard.mod |= PW_KEYMOD_RCTRL;
  }
  if (state & SDL_KMOD_LALT) {
    input->keyboard.mod |= PW_KEYMOD_LALT;
  }
  if (state & SDL_KMOD_RALT) {
    input->keyboard.mod |= PW_KEYMOD_RALT;
  }
}

static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) {
  float x, y;
  SDL_MouseButtonFlags flags = SDL_GetMouseState(&x, &y);

  struct pw_coordinates new;
  if (!pw_fb_coordinates(fb, x, y, &new)) {
    return false;
  }

  input->mouse.button = PW_MOUSE_BUTTON_NONE;
  if (flags & SDL_BUTTON_LMASK) {
    input->mouse.button |= PW_MOUSE_BUTTON_LEFT;
  }
  if (flags & SDL_BUTTON_MMASK) {
    input->mouse.button |= PW_MOUSE_BUTTON_MIDDLE;
  }
  if (flags & SDL_BUTTON_RMASK) {
    input->mouse.button |= PW_MOUSE_BUTTON_RIGHT;
  }

  input->mouse.xrel = (int32_t)new.x - (int32_t)input->mouse.pos.x;
  input->mouse.yrel = (int32_t)new.y - (int32_t)input->mouse.pos.y;
  input->mouse.pos.x = new.x;
  input->mouse.pos.y = new.y;

  return true;
}

bool pw_input_iterate(struct pw_input *input, struct pw_framebuffer *fb) {
  if (!input) {
    SDL_InvalidParamError("input");
    return false;
  }
  if (!fb) {
    SDL_InvalidParamError("fb");
    return false;
  }

  update_keymod(input);
  return update_mouse(input, fb);
}

enum pw_button pw_input_btn(struct pw_input *input, size_t player) {
  if (!input) {
    SDL_InvalidParamError("input");
    return PW_BUTTON_NONE;
  }
  if (player >= PW_PLAYERS_MAX) {
    SDL_InvalidParamError("player");
    return PW_BUTTON_NONE;
  }

  enum pw_button button = input->players[player];

  // PICO-8 only checks the menu state for player 1, so for that player, the
  // menu needs to be set if it is set for any player.
  if (!player) {
    for (size_t i = 1; i < PW_PLAYERS_MAX; i++) {
      button |= input->players[i] & PW_BUTTON_MENU;
    }
  }

  return button & PW_BUTTON_ALL;
}

enum pw_keymod pw_input_keymod(struct pw_input *input) {
  if (!input) {
    SDL_InvalidParamError("input");
    return PW_KEYMOD_NONE;
  }

  return input->keyboard.mod;
}

enum pw_mouse_button pw_input_mouse(struct pw_input *input) {
  if (!input) {
    SDL_InvalidParamError("input");
    return PW_MOUSE_BUTTON_NONE;
  }

  return input->mouse.button;
}