Debian Codegraph @

src/cdogs/events.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2016, 2018-2021 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "events.h"
29
30 #include <assert.h>
31 #include <stdlib.h>
32
33 #include <SDL_timer.h>
34
35 #include "config_io.h"
36 #include "files.h"
37 #include "gamedata.h"
38 #include "log.h"
39 #include "music.h"
40 #include "pic_manager.h"
41
42 EventHandlers gEventHandlers;
43
44 void EventInit(EventHandlers *handlers)
45 {
46 memset(handlers, 0, sizeof *handlers);
47 KeyInit(&handlers->keyboard);
48 JoyInit(&handlers->joysticks);
49 MouseInit(&handlers->mouse);
50 }
51 void EventTerminate(EventHandlers *handlers)
52 {
53 JoyTerminate(&handlers->joysticks);
54 MouseTerminate(&handlers->mouse);
55 }
56 void EventReset(EventHandlers *handlers)
57 {
58 KeyInit(&handlers->keyboard);
59 JoyReset(&handlers->joysticks);
60 MouseReset(&handlers->mouse);
61 }
62
63 void EventPoll(
64 EventHandlers *handlers, const Uint32 ticks, int (*onEvent)(SDL_Event *))
65 {
66 SDL_Event e;
67 handlers->HasResolutionChanged = false;
68 handlers->HasLostFocus = false;
69 KeyPrePoll(&handlers->keyboard);
70 MousePrePoll(&handlers->mouse);
71 JoyPrePoll(&handlers->joysticks);
72 SDL_free(handlers->DropFile);
73 handlers->DropFile = NULL;
74 // Don't process mouse events if focus just regained this cycle
75 // This is to prevent bogus click events outside the window, e.g. in the
76 // title bar
77 bool regainedFocus = false;
78 while (SDL_PollEvent(&e))
79 {
80 switch (e.type)
81 {
82 case SDL_AUDIODEVICEADDED: // fallthrough
83 case SDL_AUDIODEVICEREMOVED:
84 if (e.adevice.iscapture)
85 {
86 // Ignore capture devices
87 break;
88 }
89 SoundReopen(&gSoundDevice);
90 break;
91
92 case SDL_KEYDOWN:
93 if (e.key.repeat)
94 {
95 break;
96 }
97 KeyOnKeyDown(&handlers->keyboard, e.key.keysym);
98 break;
99 case SDL_KEYUP:
100 KeyOnKeyUp(&handlers->keyboard, e.key.keysym);
101 break;
102 case SDL_TEXTINPUT:
103 strcpy(handlers->keyboard.Typed, e.text.text);
104 break;
105
106 case SDL_CONTROLLERDEVICEADDED: {
107 const SDL_JoystickID jid = JoyAdded(e.cdevice.which);
108 if (jid == -1)
109 {
110 break;
111 }
112 // If there are players with unset devices,
113 // set this controller to them
114 CA_FOREACH(PlayerData, p, gPlayerDatas)
115 if (p->inputDevice == INPUT_DEVICE_UNSET)
116 {
117 PlayerTrySetInputDevice(p, INPUT_DEVICE_JOYSTICK, jid);
118 LOG(LM_INPUT, LL_INFO, "Joystick %d assigned to player %d",
119 jid, p->UID);
120 break;
121 }
122 CA_FOREACH_END()
123 }
124 break;
125
126 case SDL_CONTROLLERDEVICEREMOVED:
127 JoyRemoved(e.cdevice.which);
128 // If there was a player using this joystick,
129 // set their input device to nothing
130 CA_FOREACH(PlayerData, p, gPlayerDatas)
131 if (p->inputDevice == INPUT_DEVICE_JOYSTICK &&
132 p->deviceIndex == e.cdevice.which)
133 {
134 PlayerTrySetInputDevice(p, INPUT_DEVICE_UNSET, 0);
135 LOG(LM_INPUT, LL_WARN, "Joystick for player %d removed",
136 p->UID);
137 break;
138 }
139 CA_FOREACH_END()
140 break;
141
142 case SDL_CONTROLLERBUTTONDOWN:
143 JoyOnButtonDown(e.cbutton);
144 break;
145
146 case SDL_CONTROLLERBUTTONUP:
147 JoyOnButtonUp(e.cbutton);
148 break;
149
150 case SDL_CONTROLLERAXISMOTION:
151 JoyOnAxis(e.caxis);
152 break;
153
154 case SDL_MOUSEBUTTONDOWN:
155 if (regainedFocus)
156 break;
157 MouseOnButtonDown(&handlers->mouse, e.button.button);
158 break;
159 case SDL_MOUSEBUTTONUP:
160 if (regainedFocus)
161 break;
162 MouseOnButtonUp(&handlers->mouse, e.button.button);
163 break;
164 case SDL_MOUSEWHEEL:
165 if (regainedFocus)
166 break;
167 MouseOnWheel(&handlers->mouse, e.wheel.x, e.wheel.y);
168 break;
169 case SDL_WINDOWEVENT:
170 switch (e.window.event)
171 {
172 case SDL_WINDOWEVENT_FOCUS_GAINED:
173 regainedFocus = true;
174 MusicSetPlaying(&gSoundDevice.music, true);
175 break;
176 case SDL_WINDOWEVENT_FOCUS_LOST:
177 if (!gCampaign.IsClient &&
178 !ConfigGetBool(&gConfig, "StartServer"))
179 {
180 MusicSetPlaying(&gSoundDevice.music, false);
181 handlers->HasLostFocus = true;
182 }
183 // Reset input handlers
184 EventReset(handlers);
185 break;
186 case SDL_WINDOWEVENT_SIZE_CHANGED: {
187 const int w = ConfigGetInt(&gConfig, "Graphics.WindowWidth");
188 const int h = ConfigGetInt(&gConfig, "Graphics.WindowHeight");
189 if (w == e.window.data1 && h == e.window.data2)
190 {
191 // macOS sends spurious size changed events when
192 // switching between fullscreen; don't reinitialise
193 // graphics
194 break;
195 }
196 handlers->HasResolutionChanged = true;
197 const int scale =
198 ConfigGetInt(&gConfig, "Graphics.ScaleFactor");
199 if (!gGraphicsDevice.cachedConfig.IsEditor)
200 {
201 ConfigSetInt(
202 &gConfig, "Graphics.WindowWidth", e.window.data1);
203 ConfigSetInt(
204 &gConfig, "Graphics.WindowHeight", e.window.data2);
205 ConfigSave(&gConfig, GetConfigFilePath(CONFIG_FILE));
206 }
207 GraphicsConfigSet(
208 &gGraphicsDevice.cachedConfig,
209 svec2i(e.window.data1, e.window.data2), false, scale,
210 gGraphicsDevice.cachedConfig.ScaleMode,
211 gGraphicsDevice.cachedConfig.Brightness,
212 gGraphicsDevice.cachedConfig.SecondWindow);
213 GraphicsInitialize(&gGraphicsDevice);
214 }
215 break;
216 case SDL_WINDOWEVENT_CLOSE:
217 handlers->HasQuit = true;
218 break;
219 default:
220 // do nothing
221 break;
222 }
223 break;
224 case SDL_QUIT:
225 handlers->HasQuit = true;
226 break;
227 case SDL_DROPFILE:
228 handlers->DropFile = e.drop.file;
229 break;
230 default:
231 break;
232 }
233 if (onEvent)
234 {
235 onEvent(&e);
236 }
237 }
238 KeyPostPoll(&handlers->keyboard, ticks);
239 MousePostPoll(&handlers->mouse, ticks);
240
241 // Toggle fullscreen
242 if (KeyIsPressed(&handlers->keyboard, SDL_SCANCODE_RETURN) &&
243 (KeyIsDown(&handlers->keyboard, SDL_SCANCODE_LALT) ||
244 KeyIsDown(&handlers->keyboard, SDL_SCANCODE_RALT)))
245 {
246 ConfigGet(&gConfig, "Graphics.Fullscreen")->u.Bool.Value =
247 !ConfigGet(&gConfig, "Graphics.Fullscreen")->u.Bool.Value;
248 GraphicsConfigSetFromConfig(&gGraphicsDevice.cachedConfig, &gConfig);
249 GraphicsInitialize(&gGraphicsDevice);
250 }
251
252 // Auto quit on timer
253 if (handlers->DemoQuitTimer > 0)
254 {
255 handlers->DemoQuitTimer -= ticks;
256 if (handlers->DemoQuitTimer <= 0)
257 {
258 LOG(LM_MAIN, LL_INFO, "Auto-quitting");
259 handlers->HasQuit = true;
260 }
261 }
262 }
263
264 int GetKeyboardCmd(
265 keyboard_t *keyboard, const int kbIndex, const bool isPressed)
266 {
267 int cmd = 0;
268 bool (*keyFunc)(const keyboard_t *, const int) =
269 isPressed ? KeyIsPressed : KeyIsDown;
270 const InputKeys *keys = &keyboard->PlayerKeys[kbIndex];
271
272 if (keyFunc(keyboard, keys->left))
273 cmd |= CMD_LEFT;
274 else if (keyFunc(keyboard, keys->right))
275 cmd |= CMD_RIGHT;
276
277 if (keyFunc(keyboard, keys->up))
278 cmd |= CMD_UP;
279 else if (keyFunc(keyboard, keys->down))
280 cmd |= CMD_DOWN;
281
282 if (keyFunc(keyboard, keys->button1))
283 cmd |= CMD_BUTTON1;
284 if (keyFunc(keyboard, keys->button2))
285 cmd |= CMD_BUTTON2;
286 if (keyFunc(keyboard, keys->grenade))
287 cmd |= CMD_GRENADE;
288
289 return cmd;
290 }
291
292 static int GetJoystickCmd(const SDL_JoystickID id, bool isPressed)
293 {
294 int cmd = 0;
295 bool (*joyFunc)(const SDL_JoystickID, const int) =
296 isPressed ? JoyIsPressed : JoyIsDown;
297
298 if (joyFunc(id, CMD_LEFT))
299 cmd |= CMD_LEFT;
300 else if (joyFunc(id, CMD_RIGHT))
301 cmd |= CMD_RIGHT;
302
303 if (joyFunc(id, CMD_UP))
304 cmd |= CMD_UP;
305 else if (joyFunc(id, CMD_DOWN))
306 cmd |= CMD_DOWN;
307
308 if (joyFunc(id, CMD_BUTTON1))
309 cmd |= CMD_BUTTON1;
310 if (joyFunc(id, CMD_BUTTON2))
311 cmd |= CMD_BUTTON2;
312 if (joyFunc(id, CMD_GRENADE))
313 cmd |= CMD_GRENADE;
314
315 if (joyFunc(id, CMD_MAP))
316 cmd |= CMD_MAP;
317
318 if (joyFunc(id, CMD_ESC))
319 cmd |= CMD_ESC;
320
321 return cmd;
322 }
323
324 int GetGameCmd(EventHandlers *handlers, const PlayerData *playerData)
325 {
326 int cmd = 0;
327
328 switch (playerData->inputDevice)
329 {
330 case INPUT_DEVICE_KEYBOARD:
331 cmd = GetKeyboardCmd(
332 &handlers->keyboard, playerData->deviceIndex, false);
333 break;
334 case INPUT_DEVICE_JOYSTICK:
335 cmd = GetJoystickCmd(playerData->deviceIndex, false);
336 break;
337 default:
338 // do nothing
339 break;
340 }
341
342 return cmd;
343 }
344
345 int GetOnePlayerCmd(
346 EventHandlers *handlers, const bool isPressed, const input_device_e device,
347 const int deviceIndex)
348 {
349 int cmd = 0;
350 switch (device)
351 {
352 case INPUT_DEVICE_KEYBOARD:
353 cmd = GetKeyboardCmd(&handlers->keyboard, deviceIndex, isPressed);
354 break;
355 case INPUT_DEVICE_JOYSTICK:
356 cmd = GetJoystickCmd(deviceIndex, isPressed);
357 break;
358 default:
359 // Do nothing
360 break;
361 }
362 return cmd;
363 }
364
365 void GetPlayerCmds(EventHandlers *handlers, int (*cmds)[MAX_LOCAL_PLAYERS])
366 {
367 int idx = 0;
368 for (int i = 0; i < (int)gPlayerDatas.size; i++, idx++)
369 {
370 const PlayerData *p = CArrayGet(&gPlayerDatas, i);
371 if (!p->IsLocal)
372 {
373 idx--;
374 continue;
375 }
376 if (p->inputDevice != INPUT_DEVICE_UNSET)
377 {
378 (*cmds)[idx] = GetOnePlayerCmd(
379 handlers, true, p->inputDevice, p->deviceIndex);
380 }
381 }
382 }
383
384 int GetMenuCmd(EventHandlers *handlers, const bool useMouse)
385 {
386 keyboard_t *kb = &handlers->keyboard;
387 bool firstJoyPressedEsc = false;
388 SDL_JoystickID firstJoyId = 0;
389 if (handlers->joysticks.size > 0)
390 {
391 const Joystick *firstJoy = CArrayGet(&handlers->joysticks, 0);
392 firstJoyId = firstJoy->id;
393 firstJoyPressedEsc = JoyIsPressed(firstJoyId, CMD_ESC);
394 }
395 if (KeyIsPressed(kb, SDL_SCANCODE_ESCAPE) || firstJoyPressedEsc)
396 {
397 return CMD_ESC;
398 }
399
400 // Check first player keyboard
401 int cmd = GetOnePlayerCmd(handlers, true, INPUT_DEVICE_KEYBOARD, 0);
402 if (!cmd)
403 {
404 // Check keyboard
405 if (KeyIsPressed(kb, SDL_SCANCODE_LEFT))
406 cmd |= CMD_LEFT;
407 else if (KeyIsPressed(kb, SDL_SCANCODE_RIGHT))
408 cmd |= CMD_RIGHT;
409
410 if (KeyIsPressed(kb, SDL_SCANCODE_UP))
411 cmd |= CMD_UP;
412 else if (KeyIsPressed(kb, SDL_SCANCODE_DOWN))
413 cmd |= CMD_DOWN;
414
415 if (KeyIsPressed(kb, SDL_SCANCODE_RETURN))
416 cmd |= CMD_BUTTON1;
417
418 if (KeyIsPressed(kb, SDL_SCANCODE_BACKSPACE))
419 cmd |= CMD_BUTTON2;
420 }
421 if (!cmd && handlers->joysticks.size > 0)
422 {
423 // Check joystick 1
424 cmd =
425 GetOnePlayerCmd(handlers, true, INPUT_DEVICE_JOYSTICK, firstJoyId);
426 }
427 if (!cmd)
428 {
429 // Check mouse
430 if (MouseIsPressed(&handlers->mouse, SDL_BUTTON_LEFT) && useMouse)
431 cmd |= CMD_BUTTON1;
432 else if (MouseIsPressed(&handlers->mouse, SDL_BUTTON_RIGHT))
433 cmd |= CMD_BUTTON2;
434 }
435
436 return cmd;
437 }
438
439 void InputGetButtonNameColor(
440 const input_device_e d, const int dIndex, const int cmd, char *buf,
441 color_t *color)
442 {
443 switch (d)
444 {
445 case INPUT_DEVICE_KEYBOARD:
446 #ifdef __GCWZERO__
447 if (color != NULL)
448 {
449 *color = colorBlue;
450 }
451 switch (cmd)
452 {
453 case CMD_LEFT:
454 strcpy(buf, "left");
455 return;
456 case CMD_RIGHT:
457 strcpy(buf, "right");
458 return;
459 case CMD_UP:
460 strcpy(buf, "up");
461 return;
462 case CMD_DOWN:
463 strcpy(buf, "down");
464 return;
465 case CMD_BUTTON1:
466 strcpy(buf, "A");
467 return;
468 case CMD_BUTTON2:
469 strcpy(buf, "B");
470 return;
471 case CMD_GRENADE:
472 *color = colorGray;
473 strcpy(buf, "R");
474 return;
475 case CMD_MAP:
476 *color = colorGray;
477 strcpy(buf, "L");
478 return;
479 case CMD_ESC:
480 strcpy(buf, "SELECT");
481 return;
482 default:
483 CASSERT(false, "unknown button");
484 return;
485 }
486 #else
487 {
488 const InputKeys *keys = &gEventHandlers.keyboard.PlayerKeys[dIndex];
489 switch (cmd)
490 {
491 case CMD_LEFT:
492 strcpy(buf, SDL_GetScancodeName(keys->left));
493 return;
494 case CMD_RIGHT:
495 strcpy(buf, SDL_GetScancodeName(keys->right));
496 return;
497 case CMD_UP:
498 strcpy(buf, SDL_GetScancodeName(keys->up));
499 return;
500 case CMD_DOWN:
501 strcpy(buf, SDL_GetScancodeName(keys->down));
502 return;
503 case CMD_BUTTON1:
504 strcpy(buf, SDL_GetScancodeName(keys->button1));
505 return;
506 case CMD_BUTTON2:
507 strcpy(buf, SDL_GetScancodeName(keys->button2));
508 return;
509 case CMD_GRENADE:
510 strcpy(buf, SDL_GetScancodeName(keys->grenade));
511 return;
512 case CMD_MAP:
513 strcpy(buf, SDL_GetScancodeName(keys->map));
514 return;
515 case CMD_ESC:
516 strcpy(buf, SDL_GetScancodeName(SDL_SCANCODE_ESCAPE));
517 return;
518 default:
519 CASSERT(false, "unknown button");
520 return;
521 }
522 }
523 #endif
524 break;
525 case INPUT_DEVICE_JOYSTICK:
526 JoyButtonNameColor(dIndex, cmd, buf, color);
527 return;
528 case INPUT_DEVICE_AI:
529 return;
530 default:
531 CASSERT(false, "unknown input device");
532 return;
533 }
534 }
535 void InputGetDirectionNames(
536 char *buf, const input_device_e d, const int dIndex)
537 {
538 strcpy(buf, "");
539 switch (d)
540 {
541 case INPUT_DEVICE_KEYBOARD: {
542 char left[256], right[256], up[256], down[256];
543 InputGetButtonName(d, dIndex, CMD_LEFT, left);
544 InputGetButtonName(d, dIndex, CMD_RIGHT, right),
545 InputGetButtonName(d, dIndex, CMD_UP, up),
546 InputGetButtonName(d, dIndex, CMD_DOWN, down);
547 sprintf(buf, "%s, %s, %s, %s", left, right, up, down);
548 }
549 break;
550 case INPUT_DEVICE_JOYSTICK:
551 strcpy(buf, "directions");
552 break;
553 case INPUT_DEVICE_AI:
554 break;
555 case INPUT_DEVICE_UNSET:
556 break;
557 default:
558 CASSERT(false, "unknown device");
559 break;
560 }
561 }
562 bool InputHasGrenadeButton(const input_device_e d, const int dIndex)
563 {
564 switch (d)
565 {
566 case INPUT_DEVICE_UNSET:
567 return false;
568 case INPUT_DEVICE_KEYBOARD:
569 return KeyGet(
570 &gEventHandlers.keyboard.PlayerKeys[dIndex],
571 KEY_CODE_GRENADE) != SDL_SCANCODE_UNKNOWN;
572 case INPUT_DEVICE_JOYSTICK:
573 return true;
574 case INPUT_DEVICE_AI:
575 return false;
576 default:
577 CASSERT(false, "unknown input device");
578 return true;
579 }
580 }
581
582 SDL_Scancode GetKey(EventHandlers *handlers)
583 {
584 SDL_Scancode k = SDL_SCANCODE_UNKNOWN;
585 Uint32 ticksNow = SDL_GetTicks();
586 do
587 {
588 const Uint32 ticksThen = ticksNow;
589 ticksNow = SDL_GetTicks();
590 const Uint32 ticksElapsed = ticksNow - ticksThen;
591 EventPoll(handlers, ticksElapsed, NULL);
592 k = KeyGetPressed(&handlers->keyboard);
593 SDL_Delay(10);
594 } while (k == SDL_SCANCODE_UNKNOWN);
595 return k;
596 }
597
598 SDL_Scancode EventWaitKeyOrText(EventHandlers *handlers)
599 {
600 EventPoll(handlers, 0, NULL);
601 const SDL_Scancode k = KeyGetPressed(&handlers->keyboard);
602 SDL_Delay(10);
603 return k;
604 }
605
606 static EventWaitResult WaitResult(const bool result);
607 EventWaitResult EventWaitForAnyKeyOrButton(void)
608 {
609 int cmds[MAX_LOCAL_PLAYERS];
610 memset(cmds, 0, sizeof cmds);
611 GetPlayerCmds(&gEventHandlers, &cmds);
612 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
613 {
614 if (AnyButton(cmds[i]))
615 {
616 // Interpret anything other than CMD_BUTTON1 as cancel
617 return WaitResult(Button1(cmds[i]));
618 }
619 }
620
621 // Check menu commands
622 const int menuCmd = GetMenuCmd(&gEventHandlers, true);
623 if (AnyButton(menuCmd))
624 {
625 // Interpret anything other than CMD_BUTTON1 as cancel
626 return WaitResult(Button1(menuCmd));
627 }
628
629 // Check if anyone pressed escape
630 if (EventIsEscape(&gEventHandlers, cmds, menuCmd))
631 {
632 return WaitResult(false);
633 }
634
635 return EVENT_WAIT_CONTINUE;
636 }
637 static EventWaitResult WaitResult(const bool result)
638 {
639 return result ? EVENT_WAIT_OK : EVENT_WAIT_CANCEL;
640 }
641
642 bool EventIsEscape(
643 EventHandlers *handlers, const int cmds[MAX_LOCAL_PLAYERS],
644 const int menuCmd)
645 {
646 for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
647 {
648 if (cmds[i] & CMD_ESC)
649 {
650 return true;
651 }
652 }
653
654 // Check keyboard escape
655 if (KeyIsPressed(&handlers->keyboard, SDL_SCANCODE_ESCAPE) ||
656 handlers->HasQuit)
657 {
658 return true;
659 }
660
661 // Check menu commands
662 if (menuCmd & CMD_ESC)
663 {
664 return true;
665 }
666
667 return false;
668 }