This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
id_in.c
667 lines (520 loc) · 13.8 KB
/
id_in.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//
// ID Engine
// ID_IN.c - Input Manager
// v1.0d1
// By Jason Blochowiak
//
#include "wl_def.h"
/*
=============================================================================
GLOBAL VARIABLES
=============================================================================
*/
//
// configuration variables
//
boolean MousePresent;
boolean forcegrabmouse;
// Global variables
bool Keyboard[sc_Last];
char textinput[TEXTINPUTSIZE];
boolean Paused;
ScanCode LastScan;
static SDL_Joystick *Joystick;
int JoyNumButtons;
static int JoyNumHats;
bool GrabInput = false;
/*
=============================================================================
LOCAL VARIABLES
=============================================================================
*/
static boolean IN_Started;
static byte DirTable[] = // Quick lookup for total direction
{
dir_NorthWest, dir_North, dir_NorthEast,
dir_West, dir_None, dir_East,
dir_SouthWest, dir_South, dir_SouthEast
};
///////////////////////////////////////////////////////////////////////////
//
// INL_GetMouseButtons() - Gets the status of the mouse buttons from the
// mouse driver
//
///////////////////////////////////////////////////////////////////////////
static int INL_GetMouseButtons (void)
{
int buttons = SDL_GetMouseState(NULL,NULL);
int middlePressed = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
int rightPressed = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
buttons &= ~(SDL_BUTTON(SDL_BUTTON_MIDDLE) | SDL_BUTTON(SDL_BUTTON_RIGHT));
if (middlePressed)
buttons |= 1 << 2;
if (rightPressed)
buttons |= 1 << 1;
return buttons;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_GetJoyDelta() - Returns the relative movement of the specified
// joystick (from +/-127)
//
///////////////////////////////////////////////////////////////////////////
void IN_GetJoyDelta (int *dx, int *dy)
{
if (!Joystick)
{
*dx = *dy = 0;
return;
}
SDL_JoystickUpdate();
#ifdef _arch_dreamcast
int x = 0;
int y = 0;
#else
int x = SDL_JoystickGetAxis(Joystick,0) >> 8;
int y = SDL_JoystickGetAxis(Joystick,1) >> 8;
#endif
if (param_joystickhat != -1)
{
uint8_t hatState = SDL_JoystickGetHat(Joystick,param_joystickhat);
if (hatState & SDL_HAT_RIGHT)
x += 127;
else if (hatState & SDL_HAT_LEFT)
x -= 127;
if (hatState & SDL_HAT_DOWN)
y += 127;
else if (hatState & SDL_HAT_UP)
y -= 127;
x = MAX(-128,MIN(x,127));
y = MAX(-128,MIN(y,127));
}
*dx = x;
*dy = y;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_GetJoyFineDelta() - Returns the relative movement of the specified
// joystick without dividing the results by 256 (from +/-127)
//
///////////////////////////////////////////////////////////////////////////
void IN_GetJoyFineDelta (int *dx, int *dy)
{
if (!Joystick)
{
*dx = 0;
*dy = 0;
return;
}
SDL_JoystickUpdate();
int x = SDL_JoystickGetAxis(Joystick,0);
int y = SDL_JoystickGetAxis(Joystick,1);
x = MAX(-128,MIN(x,127));
y = MAX(-128,MIN(y,127));
*dx = x;
*dy = y;
}
/*
===================
=
= IN_JoyButtons
=
===================
*/
int IN_JoyButtons (void)
{
int i;
if (!Joystick)
return 0;
SDL_JoystickUpdate();
int res = 0;
for (i = 0; i < JoyNumButtons && i < 32; i++)
res |= SDL_JoystickGetButton(Joystick,i) << i;
return res;
}
boolean IN_JoyPresent (void)
{
return Joystick != NULL;
}
/*
===================
=
= Clear accumulated mouse movement
=
===================
*/
void IN_CenterMouse (void)
{
if (MousePresent && GrabInput)
SDL_WarpMouseInWindow (window,screenWidth / 2,screenHeight / 2);
}
/*
===================
=
= Map certain keys to their defaults
=
===================
*/
ScanCode IN_MapKey (int key)
{
ScanCode scan = key;
switch (key)
{
case sc_KeyPadEnter: scan = sc_Enter; break;
case sc_RShift: scan = sc_LShift; break;
case sc_RAlt: scan = sc_LAlt; break;
case sc_RControl: scan = sc_LControl; break;
case sc_KeyPad2:
case sc_KeyPad4:
case sc_KeyPad6:
case sc_KeyPad8:
if (!(SDL_GetModState() & KMOD_NUM))
{
switch (key)
{
case sc_KeyPad2: scan = sc_DownArrow; break;
case sc_KeyPad4: scan = sc_LeftArrow; break;
case sc_KeyPad6: scan = sc_RightArrow; break;
case sc_KeyPad8: scan = sc_UpArrow; break;
}
}
break;
}
return scan;
}
/*
===================
=
= IN_SetWindowGrab
=
===================
*/
void IN_SetWindowGrab (SDL_Window *window)
{
const char *which[] = {"hide","show"};
if (SDL_ShowCursor(!GrabInput) < 0)
Quit ("Unable to %s cursor: %s\n",which[!GrabInput],SDL_GetError());
SDL_SetWindowGrab (window,GrabInput);
if (SDL_SetRelativeMouseMode(GrabInput))
Quit ("Unable to set relative mode for mouse: %s\n",SDL_GetError());
}
/*
=============================================================================
INPUT PROCESSING
=============================================================================
=
= Input processing is not done via interrupts anymore! Instead you have to call IN_ProcessEvents
= in order to process any events like key presses or mouse movements. Alternatively you can call
= IN_WaitAndProcessEvents, which waits for an event before processing if none were available.
=
= If you have a loop with a blinking cursor where you are waiting for some key to be pressed,
= this loop MUST contain an IN_ProcessEvents/IN_WaitAndProcessEvents call, otherwise the program
= will never notice any keypress.
=
= Don't write something like, for example, "while (!keyboard[sc_X]) IN_ProcessEvents();" to
= wait for the player to press the X key, as this would result in 100% CPU usage. Either
= use IN_WaitAndProcessEvents, or add SDL_Delay(5) to the loop, which waits for 5 ms before
= it continues.
=
===========================================
*/
static void IN_HandleEvent (SDL_Event *event)
{
int key;
key = event->key.keysym.scancode;
switch (event->type)
{
case SDL_QUIT:
Quit (NULL);
break;
case SDL_KEYDOWN:
if (key == sc_ScrollLock || key == sc_F12)
{
GrabInput = !GrabInput;
IN_SetWindowGrab (window);
return;
}
LastScan = IN_MapKey(key);
if (Keyboard[sc_Alt])
{
if (LastScan == sc_F4)
Quit (NULL);
}
if (LastScan < sc_Last)
Keyboard[LastScan] = true;
if (LastScan == sc_Pause)
Paused = true;
break;
case SDL_KEYUP:
key = IN_MapKey(key);
if (key < sc_Last)
Keyboard[key] = false;
break;
#if defined(GP2X)
case SDL_JOYBUTTONDOWN:
GP2X_ButtonDown (event->jbutton.button);
break;
case SDL_JOYBUTTONUP:
GP2X_ButtonUp (event->jbutton.button);
break;
#endif
case SDL_TEXTINPUT:
snprintf (textinput,sizeof(textinput),"%s",event->text.text);
break;
}
}
void IN_ProcessEvents (void)
{
SDL_Event event;
while (SDL_PollEvent(&event))
IN_HandleEvent (&event);
}
void IN_WaitEvent (void)
{
if (!SDL_WaitEvent(NULL))
Quit ("Error waiting for event: %s\n",SDL_GetError());
}
void IN_WaitAndProcessEvents (void)
{
IN_WaitEvent ();
IN_ProcessEvents ();
}
///////////////////////////////////////////////////////////////////////////
//
// IN_Startup() - Starts up the Input Mgr
//
///////////////////////////////////////////////////////////////////////////
void IN_Startup(void)
{
if (IN_Started)
return;
IN_ClearKeysDown();
if (param_joystickindex >= 0 && param_joystickindex < SDL_NumJoysticks())
{
Joystick = SDL_JoystickOpen(param_joystickindex);
if (Joystick)
{
JoyNumButtons = SDL_JoystickNumButtons(Joystick);
if (JoyNumButtons > 32)
JoyNumButtons = 32; // only up to 32 buttons are supported
JoyNumHats = SDL_JoystickNumHats(Joystick);
if (param_joystickhat < -1 || param_joystickhat >= JoyNumHats)
Quit ("The joystickhat param must be between 0 and %i!",JoyNumHats - 1);
}
}
SDL_EventState (SDL_MOUSEMOTION,SDL_IGNORE);
if (fullscreen || forcegrabmouse)
{
GrabInput = true;
IN_SetWindowGrab (window);
}
// I didn't find a way to ask libSDL whether a mouse is present, yet...
#if defined(GP2X)
MousePresent = false;
#elif defined(_arch_dreamcast)
MousePresent = DC_MousePresent();
#else
MousePresent = true;
#endif
IN_Started = true;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_Shutdown() - Shuts down the Input Mgr
//
///////////////////////////////////////////////////////////////////////////
void IN_Shutdown(void)
{
if (!IN_Started)
return;
if (Joystick)
SDL_JoystickClose(Joystick);
IN_Started = false;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_ClearKeysDown() - Clears the keyboard array
//
///////////////////////////////////////////////////////////////////////////
void IN_ClearKeysDown(void)
{
LastScan = sc_None;
memset (Keyboard,0,sizeof(Keyboard));
}
void IN_ClearTextInput (void)
{
memset (textinput,0,sizeof(textinput));
}
///////////////////////////////////////////////////////////////////////////
//
// IN_ReadControl() - Reads the device associated with the specified
// player and fills in the control info struct
//
///////////////////////////////////////////////////////////////////////////
void IN_ReadControl (ControlInfo *info)
{
word buttons;
int dx,dy;
int mx,my;
dx = dy = 0;
mx = my = 0;
buttons = 0;
IN_ProcessEvents();
if (Keyboard[sc_Home])
{
mx = -1;
my = -1;
}
else if (Keyboard[sc_PgUp])
{
mx = 1;
my = -1;
}
else if (Keyboard[sc_End])
{
mx = -1;
my = 1;
}
else if (Keyboard[sc_PgDn])
{
mx = 1;
my = 1;
}
if (Keyboard[sc_UpArrow])
my = -1;
else if (Keyboard[sc_DownArrow])
my = 1;
if (Keyboard[sc_LeftArrow])
mx = -1;
else if (Keyboard[sc_RightArrow])
mx = 1;
dx = mx * 127;
dy = my * 127;
info->x = dx;
info->xaxis = mx;
info->y = dy;
info->yaxis = my;
info->button0 = (buttons & 1) != 0;
info->button1 = (buttons & (1 << 1)) != 0;
info->button2 = (buttons & (1 << 2)) != 0;
info->button3 = (buttons & (1 << 3)) != 0;
info->dir = DirTable[((my + 1) * 3) + (mx + 1)];
}
///////////////////////////////////////////////////////////////////////////
//
// IN_WaitForKey() - Waits for a scan code, then clears LastScan and
// returns the scan code
//
///////////////////////////////////////////////////////////////////////////
ScanCode IN_WaitForKey (void)
{
ScanCode result;
for (result = LastScan; !result; result = LastScan)
IN_WaitAndProcessEvents();
LastScan = 0;
return result;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_Ack() - waits for a button or key press. If a button is down, upon
// calling, it must be released for it to be recognized
//
///////////////////////////////////////////////////////////////////////////
boolean btnstate[NUMBUTTONS];
void IN_StartAck (void)
{
int i;
IN_ProcessEvents();
//
// get initial state of everything
//
IN_ClearKeysDown();
memset (btnstate,0,sizeof(btnstate));
int buttons = IN_JoyButtons() << 4;
if (MousePresent)
buttons |= IN_MouseButtons();
for (i = 0; i < NUMBUTTONS; i++, buttons >>= 1)
{
if (buttons & 1)
btnstate[i] = true;
}
}
boolean IN_CheckAck (void)
{
int i;
IN_ProcessEvents();
//
// see if something has been pressed
//
if (LastScan)
return true;
int buttons = IN_JoyButtons() << 4;
if (MousePresent)
buttons |= IN_MouseButtons();
for (i = 0; i < NUMBUTTONS; i++, buttons >>= 1)
{
if (buttons & 1)
{
if (!btnstate[i])
{
// Wait until button has been released
do
{
IN_WaitAndProcessEvents();
buttons = IN_JoyButtons() << 4;
if (MousePresent)
buttons |= IN_MouseButtons();
} while (buttons & (1 << i));
return true;
}
}
else
btnstate[i] = false;
}
return false;
}
void IN_Ack (void)
{
IN_StartAck ();
do
{
IN_WaitAndProcessEvents ();
} while (!IN_CheckAck());
}
///////////////////////////////////////////////////////////////////////////
//
// IN_UserInput() - Waits for the specified delay time (in ticks) or the
// user pressing a key or a mouse button. If the clear flag is set, it
// then either clears the key or waits for the user to let the mouse
// button up.
//
///////////////////////////////////////////////////////////////////////////
boolean IN_UserInput (longword delay)
{
longword lasttime;
lasttime = GetTimeCount();
IN_StartAck ();
do
{
IN_ProcessEvents();
if (IN_CheckAck())
return true;
SDL_Delay(5);
} while (GetTimeCount() - lasttime < delay);
return false;
}
//===========================================================================
/*
===================
=
= IN_MouseButtons
=
===================
*/
int IN_MouseButtons (void)
{
if (MousePresent)
return INL_GetMouseButtons();
else
return 0;
}