| 1 |
// |
| 2 |
// Copyright(C) 2020 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Input |
| 16 |
// |
| 17 |
|
| 18 |
#include <string.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#include "input.h" |
| 22 |
|
| 23 |
int dsda_input_profile; |
| 24 |
static dsda_input_t dsda_input[DSDA_INPUT_PROFILE_COUNT][DSDA_INPUT_IDENTIFIER_COUNT]; |
| 25 |
|
| 26 |
typedef struct |
| 27 |
{ |
| 28 |
dboolean on; |
| 29 |
int activated_at; |
| 30 |
int deactivated_at; |
| 31 |
dboolean game_on; |
| 32 |
int game_activated_at; |
| 33 |
int game_deactivated_at; |
| 34 |
} dsda_input_state_t; |
| 35 |
|
| 36 |
static int dsda_input_counter; // +1 for each event |
| 37 |
static int dsda_input_tick_counter; // +1 for each game tick |
| 38 |
static dsda_input_state_t gamekeys[NUMKEYS]; |
| 39 |
static dsda_input_state_t mousearray[MAX_MOUSE_BUTTONS + 1]; |
| 40 |
static dsda_input_state_t *mousebuttons = &mousearray[1]; // allow [-1] |
| 41 |
static dsda_input_state_t joyarray[MAX_JOY_BUTTONS + 1]; |
| 42 |
static dsda_input_state_t *joybuttons = &joyarray[1]; // allow [-1] |
| 43 |
|
| 44 |
static void dsda_InputTrackButtons(dsda_input_state_t* buttons, int max, event_t* ev) { |
| 45 |
int i; |
| 46 |
|
| 47 |
for (i = 0; i < max; ++i) { |
| 48 |
unsigned int button_on = (ev->data1.i & (1 << i)) != 0; |
| 49 |
|
| 50 |
if (!buttons[i].on && button_on) |
| 51 |
buttons[i].activated_at = dsda_input_counter; |
| 52 |
|
| 53 |
if (buttons[i].on && !button_on) |
| 54 |
buttons[i].deactivated_at = dsda_input_counter; |
| 55 |
|
| 56 |
buttons[i].on = button_on; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
static void dsda_InputTrackKeyDown(event_t* ev) { |
| 61 |
int key = ev->data1.i; |
| 62 |
|
| 63 |
if (key >= NUMKEYS) return; |
| 64 |
|
| 65 |
gamekeys[key].activated_at = dsda_input_counter; |
| 66 |
gamekeys[key].on = true; |
| 67 |
} |
| 68 |
|
| 69 |
static void dsda_InputTrackKeyUp(event_t* ev) { |
| 70 |
int key = ev->data1.i; |
| 71 |
|
| 72 |
if (key >= NUMKEYS) return; |
| 73 |
|
| 74 |
gamekeys[key].deactivated_at = dsda_input_counter; |
| 75 |
gamekeys[key].on = false; |
| 76 |
} |
| 77 |
|
| 78 |
void dsda_InputFlushTick(void) { |
| 79 |
dsda_input_tick_counter = dsda_input_counter; |
| 80 |
} |
| 81 |
|
| 82 |
void dsda_InputTrackEvent(event_t* ev) { |
| 83 |
++dsda_input_counter; |
| 84 |
|
| 85 |
switch (ev->type) |
| 86 |
{ |
| 87 |
case ev_keydown: |
| 88 |
dsda_InputTrackKeyDown(ev); |
| 89 |
break; |
| 90 |
case ev_keyup: |
| 91 |
dsda_InputTrackKeyUp(ev); |
| 92 |
break; |
| 93 |
case ev_mouse: |
| 94 |
dsda_InputTrackButtons(mousebuttons, MAX_MOUSE_BUTTONS, ev); |
| 95 |
break; |
| 96 |
case ev_joystick: |
| 97 |
dsda_InputTrackButtons(joybuttons, MAX_JOY_BUTTONS, ev); |
| 98 |
break; |
| 99 |
default: |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static void dsda_InputTrackGameButtons(dsda_input_state_t* buttons, int max, event_t* ev) { |
| 105 |
int i; |
| 106 |
|
| 107 |
for (i = 0; i < max; ++i) { |
| 108 |
unsigned int button_on = (ev->data1.i & (1 << i)) != 0; |
| 109 |
|
| 110 |
if (!buttons[i].game_on && button_on) |
| 111 |
buttons[i].game_activated_at = dsda_input_counter; |
| 112 |
|
| 113 |
if (buttons[i].game_on && !button_on) |
| 114 |
buttons[i].game_deactivated_at = dsda_input_counter; |
| 115 |
|
| 116 |
buttons[i].game_on = button_on; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static void dsda_InputTrackGameKeyDown(event_t* ev) { |
| 121 |
int key = ev->data1.i; |
| 122 |
|
| 123 |
if (key >= NUMKEYS) return; |
| 124 |
|
| 125 |
gamekeys[key].game_activated_at = dsda_input_counter; |
| 126 |
gamekeys[key].game_on = true; |
| 127 |
} |
| 128 |
|
| 129 |
static void dsda_InputTrackGameKeyUp(event_t* ev) { |
| 130 |
int key = ev->data1.i; |
| 131 |
|
| 132 |
if (key >= NUMKEYS) return; |
| 133 |
|
| 134 |
gamekeys[key].game_deactivated_at = dsda_input_counter; |
| 135 |
gamekeys[key].game_on = false; |
| 136 |
} |
| 137 |
|
| 138 |
void dsda_InputTrackGameEvent(event_t* ev) { |
| 139 |
switch (ev->type) |
| 140 |
{ |
| 141 |
case ev_keydown: |
| 142 |
dsda_InputTrackGameKeyDown(ev); |
| 143 |
break; |
| 144 |
case ev_keyup: |
| 145 |
dsda_InputTrackGameKeyUp(ev); |
| 146 |
break; |
| 147 |
case ev_mouse: |
| 148 |
dsda_InputTrackGameButtons(mousebuttons, MAX_MOUSE_BUTTONS, ev); |
| 149 |
break; |
| 150 |
case ev_joystick: |
| 151 |
dsda_InputTrackGameButtons(joybuttons, MAX_JOY_BUTTONS, ev); |
| 152 |
break; |
| 153 |
default: |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
dboolean dsda_InputActivated(int identifier) { |
| 159 |
int i; |
| 160 |
dsda_input_t* input; |
| 161 |
input = &dsda_input[dsda_input_profile][identifier]; |
| 162 |
|
| 163 |
for (i = 0; i < input->num_keys; ++i) |
| 164 |
if (gamekeys[input->key[i]].activated_at == dsda_input_counter) |
| 165 |
return true; |
| 166 |
|
| 167 |
return |
| 168 |
mousebuttons[input->mouseb].activated_at == dsda_input_counter || |
| 169 |
joybuttons[input->joyb].activated_at == dsda_input_counter; |
| 170 |
} |
| 171 |
|
| 172 |
dboolean dsda_InputTickActivated(int identifier) { |
| 173 |
int i; |
| 174 |
dsda_input_t* input; |
| 175 |
input = &dsda_input[dsda_input_profile][identifier]; |
| 176 |
|
| 177 |
for (i = 0; i < input->num_keys; ++i) |
| 178 |
if (gamekeys[input->key[i]].game_activated_at > dsda_input_tick_counter) |
| 179 |
return true; |
| 180 |
|
| 181 |
return |
| 182 |
mousebuttons[input->mouseb].game_activated_at > dsda_input_tick_counter || |
| 183 |
joybuttons[input->joyb].game_activated_at > dsda_input_tick_counter; |
| 184 |
} |
| 185 |
|
| 186 |
dboolean dsda_InputDeactivated(int identifier) { |
| 187 |
int i; |
| 188 |
dboolean deactivated = false; |
| 189 |
dsda_input_t* input; |
| 190 |
input = &dsda_input[dsda_input_profile][identifier]; |
| 191 |
|
| 192 |
for (i = 0; i < input->num_keys; ++i) |
| 193 |
if (gamekeys[input->key[i]].on) |
| 194 |
return false; |
| 195 |
else if (gamekeys[input->key[i]].deactivated_at == dsda_input_counter) |
| 196 |
deactivated = true; |
| 197 |
|
| 198 |
return |
| 199 |
!mousebuttons[input->mouseb].on && |
| 200 |
!joybuttons[input->joyb].on && |
| 201 |
( |
| 202 |
deactivated || |
| 203 |
mousebuttons[input->mouseb].deactivated_at == dsda_input_counter || |
| 204 |
joybuttons[input->joyb].deactivated_at == dsda_input_counter |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
void dsda_InputFlush(void) { |
| 209 |
memset(gamekeys, 0, sizeof(gamekeys)); |
| 210 |
memset(mousearray, 0, sizeof(mousearray)); |
| 211 |
memset(joyarray, 0, sizeof(joyarray)); |
| 212 |
dsda_input_tick_counter = 0; |
| 213 |
dsda_input_counter = 0; |
| 214 |
} |
| 215 |
|
| 216 |
dsda_input_t* dsda_Input(int identifier) { |
| 217 |
return &dsda_input[dsda_input_profile][identifier]; |
| 218 |
} |
| 219 |
|
| 220 |
void dsda_InputCopy(int identifier, dsda_input_t* input[DSDA_INPUT_PROFILE_COUNT]) { |
| 221 |
int i; |
| 222 |
|
| 223 |
for (i = 0; i < DSDA_INPUT_PROFILE_COUNT; ++i) { |
| 224 |
input[i] = &dsda_input[i][identifier]; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
int dsda_InputMatchKey(int identifier, int value) { |
| 229 |
int i; |
| 230 |
dsda_input_t* p = &dsda_input[dsda_input_profile][identifier]; |
| 231 |
|
| 232 |
for (i = 0; i < p->num_keys; ++i) |
| 233 |
if (p->key[i] == value) |
| 234 |
return true; |
| 235 |
|
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
int dsda_InputMatchMouseB(int identifier, int value) { |
| 240 |
return dsda_input[dsda_input_profile][identifier].mouseb == value; |
| 241 |
} |
| 242 |
|
| 243 |
int dsda_InputMatchJoyB(int identifier, int value) { |
| 244 |
return dsda_input[dsda_input_profile][identifier].joyb == value; |
| 245 |
} |
| 246 |
|
| 247 |
void dsda_InputResetSpecific(int config_index, int identifier) { |
| 248 |
dsda_input_t* p = &dsda_input[config_index][identifier]; |
| 249 |
|
| 250 |
p->num_keys = 0; |
| 251 |
p->mouseb = -1; |
| 252 |
p->joyb = -1; |
| 253 |
} |
| 254 |
|
| 255 |
void dsda_InputReset(int identifier) { |
| 256 |
dsda_InputResetSpecific(dsda_input_profile, identifier); |
| 257 |
} |
| 258 |
|
| 259 |
void dsda_InputSet(int identifier, dsda_input_default_t input) { |
| 260 |
dsda_InputSetSpecific(dsda_input_profile, identifier, input); |
| 261 |
} |
| 262 |
|
| 263 |
void dsda_InputSetSpecific(int config_index, int identifier, dsda_input_default_t input) { |
| 264 |
dsda_input_t* p = &dsda_input[config_index][identifier]; |
| 265 |
|
| 266 |
if (p->num_keys == 0) |
| 267 |
p->key = realloc(p->key, sizeof(*p->key)); |
| 268 |
if (input.key > 0) { |
| 269 |
p->key[0] = input.key; |
| 270 |
p->num_keys = 1; |
| 271 |
} |
| 272 |
else p->num_keys = 0; |
| 273 |
|
| 274 |
p->mouseb = input.mouseb; |
| 275 |
p->joyb = input.joyb; |
| 276 |
} |
| 277 |
|
| 278 |
static void dsda_InputAddThing(int** list, int* count, int value) { |
| 279 |
int i; |
| 280 |
|
| 281 |
for (i = 0; i < (*count); ++i) |
| 282 |
if ((*list)[i] == value) return; |
| 283 |
|
| 284 |
(*count)++; |
| 285 |
(*list) = realloc((*list), (*count) * sizeof(**list)); |
| 286 |
(*list)[(*count) - 1] = value; |
| 287 |
} |
| 288 |
|
| 289 |
void dsda_InputAddSpecificKey(int config_index, int identifier, int value) { |
| 290 |
dsda_input_t* p = &dsda_input[config_index][identifier]; |
| 291 |
|
| 292 |
if (value < 1 || value >= NUMKEYS) return; |
| 293 |
|
| 294 |
dsda_InputAddThing(&p->key, &p->num_keys, value); |
| 295 |
} |
| 296 |
|
| 297 |
void dsda_InputAddKey(int identifier, int value) { |
| 298 |
dsda_InputAddSpecificKey(dsda_input_profile, identifier, value); |
| 299 |
} |
| 300 |
|
| 301 |
void dsda_InputAddSpecificMouseB(int config_index, int identifier, int value) { |
| 302 |
if (value < -1 || value >= MAX_MOUSE_BUTTONS) return; |
| 303 |
|
| 304 |
dsda_input[config_index][identifier].mouseb = value; |
| 305 |
} |
| 306 |
|
| 307 |
void dsda_InputAddMouseB(int identifier, int value) { |
| 308 |
dsda_InputAddSpecificMouseB(dsda_input_profile, identifier, value); |
| 309 |
} |
| 310 |
|
| 311 |
void dsda_InputAddSpecificJoyB(int config_index, int identifier, int value) { |
| 312 |
if (value < -1 || value >= MAX_JOY_BUTTONS) return; |
| 313 |
|
| 314 |
dsda_input[config_index][identifier].joyb = value; |
| 315 |
} |
| 316 |
|
| 317 |
void dsda_InputAddJoyB(int identifier, int value) { |
| 318 |
dsda_InputAddSpecificJoyB(dsda_input_profile, identifier, value); |
| 319 |
} |
| 320 |
|
| 321 |
static void dsda_InputRemoveThing(int* list, int* count, int value) { |
| 322 |
int i; |
| 323 |
dboolean found = false; |
| 324 |
|
| 325 |
for (i = 0; i < (*count); ++i) |
| 326 |
if (list[i] == value) { |
| 327 |
found = true; |
| 328 |
|
| 329 |
for (; i < (*count) - 1; ++i) |
| 330 |
list[i] = list[i + 1]; |
| 331 |
|
| 332 |
break; |
| 333 |
} |
| 334 |
|
| 335 |
if (!found) return; |
| 336 |
|
| 337 |
(*count)--; |
| 338 |
} |
| 339 |
|
| 340 |
void dsda_InputRemoveKey(int identifier, int value) { |
| 341 |
dsda_input_t* p = &dsda_input[dsda_input_profile][identifier]; |
| 342 |
|
| 343 |
dsda_InputRemoveThing(p->key, &p->num_keys, value); |
| 344 |
} |
| 345 |
|
| 346 |
void dsda_InputRemoveMouseB(int identifier, int value) { |
| 347 |
dsda_input[dsda_input_profile][identifier].mouseb = -1; |
| 348 |
} |
| 349 |
|
| 350 |
void dsda_InputRemoveJoyB(int identifier, int value) { |
| 351 |
dsda_input[dsda_input_profile][identifier].joyb = -1; |
| 352 |
} |
| 353 |
|
| 354 |
dboolean dsda_InputActive(int identifier) { |
| 355 |
int i; |
| 356 |
dsda_input_t* input; |
| 357 |
input = &dsda_input[dsda_input_profile][identifier]; |
| 358 |
|
| 359 |
for (i = 0; i < input->num_keys; ++i) |
| 360 |
if (gamekeys[input->key[i]].game_on) |
| 361 |
return true; |
| 362 |
|
| 363 |
return (input->mouseb >= 0 && mousebuttons[input->mouseb].game_on) || |
| 364 |
(input->joyb >= 0 && joybuttons[input->joyb].game_on); |
| 365 |
} |
| 366 |
|
| 367 |
dboolean dsda_InputKeyActive(int identifier) { |
| 368 |
int i; |
| 369 |
dsda_input_t* input; |
| 370 |
input = &dsda_input[dsda_input_profile][identifier]; |
| 371 |
|
| 372 |
for (i = 0; i < input->num_keys; ++i) |
| 373 |
if (gamekeys[input->key[i]].game_on) |
| 374 |
return true; |
| 375 |
|
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
dboolean dsda_InputMouseBActive(int identifier) { |
| 380 |
dsda_input_t* input; |
| 381 |
input = &dsda_input[dsda_input_profile][identifier]; |
| 382 |
|
| 383 |
return input->mouseb >= 0 && mousebuttons[input->mouseb].game_on; |
| 384 |
} |
| 385 |
|
| 386 |
dboolean dsda_InputJoyBActive(int identifier) { |
| 387 |
dsda_input_t* input; |
| 388 |
input = &dsda_input[dsda_input_profile][identifier]; |
| 389 |
|
| 390 |
return input->joyb >= 0 && joybuttons[input->joyb].game_on; |
| 391 |
} |