| 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 |
#include "keyboard.h" |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "log.h" |
| 34 |
#include "utils.h" |
| 35 |
|
| 36 |
|
| 37 |
const char *KeycodeStr(int k) |
| 38 |
{ |
| 39 |
switch (k) |
| 40 |
{ |
| 41 |
T2S(KEY_CODE_LEFT, "left"); |
| 42 |
T2S(KEY_CODE_RIGHT, "right"); |
| 43 |
T2S(KEY_CODE_UP, "up"); |
| 44 |
T2S(KEY_CODE_DOWN, "down"); |
| 45 |
T2S(KEY_CODE_BUTTON1, "button1"); |
| 46 |
T2S(KEY_CODE_BUTTON2, "button2"); |
| 47 |
T2S(KEY_CODE_GRENADE, "grenade"); |
| 48 |
T2S(KEY_CODE_MAP, "map"); |
| 49 |
default: |
| 50 |
return ""; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
#define KEYBOARD_REPEAT_DELAY 1500 |
| 55 |
#define KEYBOARD_REPEAT_TICKS 300 |
| 56 |
#define DIAGONAL_RELEASE_DELAY 50 |
| 57 |
|
| 58 |
void KeyInit(keyboard_t *keyboard) |
| 59 |
{ |
| 60 |
memset(keyboard, 0, sizeof *keyboard); |
| 61 |
keyboard->modState = KMOD_NONE; |
| 62 |
keyboard->repeatedTicks = 0; |
| 63 |
keyboard->isFirstRepeat = 1; |
| 64 |
for (int i = 0; i < MAX_KEYBOARD_CONFIGS; i++) |
| 65 |
{ |
| 66 |
char buf[256]; |
| 67 |
sprintf(buf, "Input.PlayerCodes%d", i); |
| 68 |
keyboard->PlayerKeys[i] = KeyLoadPlayerKeys(ConfigGet(&gConfig, buf)); |
| 69 |
} |
| 70 |
} |
| 71 |
InputKeys KeyLoadPlayerKeys(Config *c) |
| 72 |
{ |
| 73 |
InputKeys k; |
| 74 |
k.left = (SDL_Scancode)ConfigGetInt(c, "left"); |
| 75 |
k.right = (SDL_Scancode)ConfigGetInt(c, "right"); |
| 76 |
k.up = (SDL_Scancode)ConfigGetInt(c, "up"); |
| 77 |
k.down = (SDL_Scancode)ConfigGetInt(c, "down"); |
| 78 |
k.button1 = (SDL_Scancode)ConfigGetInt(c, "button1"); |
| 79 |
k.button2 = (SDL_Scancode)ConfigGetInt(c, "button2"); |
| 80 |
k.grenade = (SDL_Scancode)ConfigGetInt(c, "grenade"); |
| 81 |
k.map = (SDL_Scancode)ConfigGetInt(c, "map"); |
| 82 |
return k; |
| 83 |
} |
| 84 |
|
| 85 |
void KeyLockKeys(keyboard_t *k) |
| 86 |
{ |
| 87 |
memcpy( |
| 88 |
k->lockedKeys, |
| 89 |
k->currentKeys, |
| 90 |
sizeof k->lockedKeys); |
| 91 |
} |
| 92 |
|
| 93 |
void KeyPrePoll(keyboard_t *keyboard) |
| 94 |
{ |
| 95 |
memcpy( |
| 96 |
keyboard->previousKeys, |
| 97 |
keyboard->currentKeys, |
| 98 |
sizeof keyboard->previousKeys); |
| 99 |
keyboard->modState = SDL_GetModState(); |
| 100 |
keyboard->Typed[0] = '\0'; |
| 101 |
} |
| 102 |
|
| 103 |
void KeyOnKeyDown(keyboard_t *keyboard, const SDL_Keysym s) |
| 104 |
{ |
| 105 |
keyboard->currentKeys[s.scancode].isPressed = true; |
| 106 |
if (s.sym >= (SDL_Keycode)' ' && s.sym <= (SDL_Keycode)'z') |
| 107 |
{ |
| 108 |
keyboard->currentKeys[s.scancode].keycode = s.sym; |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
keyboard->currentKeys[s.scancode].keycode = 0; |
| 113 |
} |
| 114 |
} |
| 115 |
void KeyOnKeyUp(keyboard_t *keyboard, const SDL_Keysym s) |
| 116 |
{ |
| 117 |
keyboard->currentKeys[s.scancode].isPressed = false; |
| 118 |
keyboard->lockedKeys[s.scancode].isPressed = false; |
| 119 |
} |
| 120 |
|
| 121 |
void DiagonalHold(keyboard_t *keyboard, int currentPlayer) |
| 122 |
{ |
| 123 |
|
| 124 |
|
| 125 |
int currentTicks = (int)SDL_GetTicks(); |
| 126 |
|
| 127 |
|
| 128 |
const Uint8* realKeyboardState = SDL_GetKeyboardState(NULL); |
| 129 |
|
| 130 |
int cd = keyboard->diagonalState[currentPlayer].currentDiagonal; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
KeyPress currentPlayerKeys[4] = {keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].up], keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].right], |
| 135 |
keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].down], keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].left]}; |
| 136 |
|
| 137 |
|
| 138 |
int realCurrentPlayerKeys[4] = {realKeyboardState[keyboard->PlayerKeys[currentPlayer].up], realKeyboardState[keyboard->PlayerKeys[currentPlayer].right], |
| 139 |
realKeyboardState[keyboard->PlayerKeys[currentPlayer].down], realKeyboardState[keyboard->PlayerKeys[currentPlayer].left]}; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if ((keyboard->diagonalState[currentPlayer].diagonalStatus == DIAGONAL_STATUS_SUSTAIN) && (currentPlayerKeys[(cd + 2) % 4].isPressed || (currentPlayerKeys[(cd + 3) % 4].isPressed))) |
| 145 |
{ |
| 146 |
keyboard->diagonalState[currentPlayer].diagonalStatus = DIAGONAL_STATUS_UNPRESSED; |
| 147 |
keyboard->diagonalState[currentPlayer].diagonalTicks = (-1); |
| 148 |
currentPlayerKeys[cd].isPressed = realCurrentPlayerKeys[cd]; |
| 149 |
currentPlayerKeys[(cd + 1) % 4].isPressed = realCurrentPlayerKeys[(cd + 1) % 4]; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if ((keyboard->diagonalState[currentPlayer].diagonalStatus == DIAGONAL_STATUS_PRESSED) && ((currentPlayerKeys[cd].isPressed || currentPlayerKeys[(cd + 1) % 4].isPressed))) |
| 156 |
{ |
| 157 |
keyboard->diagonalState[currentPlayer].diagonalStatus = DIAGONAL_STATUS_SUSTAIN; |
| 158 |
keyboard->diagonalState[currentPlayer].diagonalTicks = currentTicks + DIAGONAL_RELEASE_DELAY; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
for (int i = 0; i < 4; ++i) |
| 164 |
{ |
| 165 |
if ((keyboard->diagonalState[currentPlayer].diagonalStatus == DIAGONAL_STATUS_UNPRESSED) && (currentPlayerKeys[i].isPressed && currentPlayerKeys[(i + 1) % 4].isPressed)) |
| 166 |
{ |
| 167 |
keyboard->diagonalState[currentPlayer].currentDiagonal = i; |
| 168 |
keyboard->diagonalState[currentPlayer].diagonalStatus = DIAGONAL_STATUS_PRESSED; |
| 169 |
keyboard->diagonalState[currentPlayer].diagonalTicks = (-1); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if ((keyboard->diagonalState[currentPlayer].diagonalStatus == DIAGONAL_STATUS_SUSTAIN) && (keyboard->diagonalState[currentPlayer].diagonalTicks > currentTicks)) |
| 176 |
{ |
| 177 |
currentPlayerKeys[cd].isPressed = true; |
| 178 |
currentPlayerKeys[(cd + 1) % 4].isPressed = true; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if (((keyboard->diagonalState[currentPlayer].diagonalStatus == DIAGONAL_STATUS_SUSTAIN) && ((keyboard->diagonalState[currentPlayer].diagonalTicks <= currentTicks) |
| 185 |
|| ((!realCurrentPlayerKeys[cd]) && (!realCurrentPlayerKeys[(cd + 1) % 4]))))) |
| 186 |
{ |
| 187 |
keyboard->diagonalState[currentPlayer].diagonalStatus = DIAGONAL_STATUS_UNPRESSED; |
| 188 |
currentPlayerKeys[cd].isPressed = realCurrentPlayerKeys[cd]; |
| 189 |
currentPlayerKeys[(cd + 1) % 4].isPressed = realCurrentPlayerKeys[(cd + 1) % 4]; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].up].isPressed = currentPlayerKeys[0].isPressed; |
| 194 |
keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].right].isPressed = currentPlayerKeys[1].isPressed; |
| 195 |
keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].down].isPressed = currentPlayerKeys[2].isPressed; |
| 196 |
keyboard->currentKeys[keyboard->PlayerKeys[currentPlayer].left].isPressed = currentPlayerKeys[3].isPressed; |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
void KeyPostPoll(keyboard_t *keyboard, const Uint32 ticks) |
| 201 |
{ |
| 202 |
int isRepeating = 0; |
| 203 |
int areSameKeysPressed = 1; |
| 204 |
for (int i = 0; i < SDL_NUM_SCANCODES; i++) |
| 205 |
{ |
| 206 |
if (keyboard->previousKeys[i].isPressed ^ |
| 207 |
keyboard->currentKeys[i].isPressed) |
| 208 |
{ |
| 209 |
areSameKeysPressed = 0; |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if (areSameKeysPressed) |
| 215 |
{ |
| 216 |
keyboard->repeatedTicks += ticks; |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
keyboard->repeatedTicks = 0; |
| 221 |
keyboard->isFirstRepeat = 1; |
| 222 |
} |
| 223 |
|
| 224 |
if (keyboard->repeatedTicks > KEYBOARD_REPEAT_DELAY && |
| 225 |
keyboard->isFirstRepeat) |
| 226 |
{ |
| 227 |
keyboard->repeatedTicks -= KEYBOARD_REPEAT_DELAY; |
| 228 |
isRepeating = 1; |
| 229 |
} |
| 230 |
else if (keyboard->repeatedTicks > KEYBOARD_REPEAT_TICKS && |
| 231 |
!keyboard->isFirstRepeat) |
| 232 |
{ |
| 233 |
keyboard->repeatedTicks -= KEYBOARD_REPEAT_TICKS; |
| 234 |
isRepeating = 1; |
| 235 |
} |
| 236 |
if (isRepeating) |
| 237 |
{ |
| 238 |
memcpy( |
| 239 |
keyboard->pressedKeys, |
| 240 |
keyboard->currentKeys, |
| 241 |
sizeof keyboard->pressedKeys); |
| 242 |
keyboard->isFirstRepeat = false; |
| 243 |
|
| 244 |
|
| 245 |
keyboard->pressedKeys[SDL_SCANCODE_NUMLOCKCLEAR].isPressed = false; |
| 246 |
keyboard->pressedKeys[SDL_SCANCODE_CAPSLOCK].isPressed = false; |
| 247 |
keyboard->pressedKeys[SDL_SCANCODE_SCROLLLOCK].isPressed = false; |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
for (int i = 0; i < SDL_NUM_SCANCODES; i++) |
| 252 |
{ |
| 253 |
keyboard->pressedKeys[i].isPressed = |
| 254 |
keyboard->currentKeys[i].isPressed && |
| 255 |
!keyboard->previousKeys[i].isPressed; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
for (int currentPlayer = 0; currentPlayer < MAX_KEYBOARD_CONFIGS; ++currentPlayer) |
| 260 |
{ |
| 261 |
|
| 262 |
DiagonalHold(keyboard, currentPlayer); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
bool KeyIsDown(const keyboard_t *k, const int key) |
| 267 |
{ |
| 268 |
return !k->lockedKeys[key].isPressed && k->currentKeys[key].isPressed; |
| 269 |
} |
| 270 |
|
| 271 |
bool KeyIsPressed(const keyboard_t *k, const int key) |
| 272 |
{ |
| 273 |
return k->pressedKeys[key].isPressed; |
| 274 |
} |
| 275 |
|
| 276 |
bool KeyIsReleased(const keyboard_t *k, const int key) |
| 277 |
{ |
| 278 |
return !KeyIsDown(k, key) && k->previousKeys[key].isPressed; |
| 279 |
} |
| 280 |
|
| 281 |
SDL_Scancode KeyGetPressed(const keyboard_t *k) |
| 282 |
{ |
| 283 |
for (SDL_Scancode i = 0; i < SDL_NUM_SCANCODES; i++) |
| 284 |
{ |
| 285 |
if (KeyIsPressed(k, i)) |
| 286 |
{ |
| 287 |
return i; |
| 288 |
} |
| 289 |
} |
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
SDL_Scancode KeyGet(const InputKeys *keys, const key_code_e keyCode) |
| 294 |
{ |
| 295 |
switch (keyCode) |
| 296 |
{ |
| 297 |
case KEY_CODE_LEFT: |
| 298 |
return keys->left; |
| 299 |
case KEY_CODE_RIGHT: |
| 300 |
return keys->right; |
| 301 |
case KEY_CODE_UP: |
| 302 |
return keys->up; |
| 303 |
case KEY_CODE_DOWN: |
| 304 |
return keys->down; |
| 305 |
case KEY_CODE_BUTTON1: |
| 306 |
return keys->button1; |
| 307 |
case KEY_CODE_BUTTON2: |
| 308 |
return keys->button2; |
| 309 |
case KEY_CODE_GRENADE: |
| 310 |
return keys->grenade; |
| 311 |
case KEY_CODE_MAP: |
| 312 |
return keys->map; |
| 313 |
default: |
| 314 |
LOG(LM_MAIN, LL_ERROR, "Unhandled key code %d\n", (int)keyCode); |
| 315 |
return SDL_SCANCODE_UNKNOWN; |
| 316 |
} |
| 317 |
} |