Debian Codegraph @

src/cdogs/keyboard.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2015, 2018, 2021, 2024 Cong Xu, davidrgmcb
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
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 //To change the level of delay, set DIAGONAL_RELEASE_DELAY up top to another number, if more than two players are ever allowed to map to a keyboard, expansion should be automatic when MAX_KEYBOARD_CONFIGS changes
124 // ------
125 int currentTicks = (int)SDL_GetTicks(); //Used to determine whether to keep holding a diagonal by comparing how long it has been since it set that diagonal's ticks vs current time
126 //diagonal is held to give some leeway between releasing one half of it and the other so that it's easier to stop facing a diagonal
127
128 const Uint8* realKeyboardState = SDL_GetKeyboardState(NULL); //This is the physical state of the keyboard untouched by any program manipulations, never manipulated, just compared against, eliminates a lot of bugs
129
130 int cd = keyboard->diagonalState[currentPlayer].currentDiagonal;
131 // cd isn't always initialized to something useful/useable but shouldn't be called before it has a useable value
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 //Needed an array of keyboard directions arranged in a circle to be able to turn this into a simple loop
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 //Needed a similar array for the actual state of the keyboard so 'i' would always match between currentKeys and realKeyboard
141
142 // ------
143 //If an opposing direction is pressed to what is currently sustained, immediately end sustain, an attempt to make sure false positives are nipped in the bud
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 //Sets sustain if both buttons in a diagonal have been pressed but one is no longer pressed
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 //Sets the stage to change to sustain, hopefully avoids false positives by creating an intermediary state between not pressed and a diagonal being sustained
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 //Keeps buttons pressed for DIAGONAL_RELEASE_DELAY (currently 20) milliseconds to help determine if a diagonal was intended
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 //Ends a SUSTAIN, returns keyboard to neutral state, assigns both to real value to avoid causing a bug that makes one of the two cardinal directions used to immediately stop functioning
183 //the first time after being pressed after a sustain
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 //Propogates all changes to the keyboard state, when these are removed the function ceases to do anything.
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 // If same keys have been pressed, remember how long they have been pressed
214 if (areSameKeysPressed)
215 {
216 keyboard->repeatedTicks += ticks;
217 }
218 else
219 {
220 keyboard->repeatedTicks = 0;
221 keyboard->isFirstRepeat = 1;
222 }
223 // If more time has elapsed, forget about previous keys for repeating
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 // Ignore the keys that tend to stay pressed/unpressed
244 // i.e. lock keys
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 //cycles through keys set to diagonals on each player to make them a bit stickier so diagonals are easier to land on
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 }