/
dd_input.c
1437 lines (1229 loc) · 35.8 KB
/
dd_input.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2011 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* dd_input.c: System Independent Input
*/
// HEADER FILES ------------------------------------------------------------
#include <ctype.h>
#include <math.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_misc.h"
#include "de_ui.h"
#include "gl_main.h"
// MACROS ------------------------------------------------------------------
#define DEFAULT_JOYSTICK_DEADZONE .05f // 5%
#define MAX_AXIS_FILTER 40
#define KBDQUESIZE 32
#define MAX_DOWNKEYS 16 // Most keyboards support 6 or 7.
// TYPES -------------------------------------------------------------------
typedef struct repeater_s {
int key; // The DDKEY code (0 if not in use).
timespan_t timer; // How's the time?
int count; // How many times has been repeated?
} repeater_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(AxisPrintConfig);
D_CMD(AxisChangeOption);
D_CMD(AxisChangeValue);
D_CMD(DumpKeyMap);
D_CMD(KeyMap);
D_CMD(ListInputDevices);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
boolean ignoreInput = false;
int mouseFilter = 1; // Filtering on by default.
// The initial and secondary repeater delays (tics).
int repWait1 = 15, repWait2 = 3;
int keyRepeatDelay1 = 430, keyRepeatDelay2 = 85; // milliseconds
unsigned int mouseFreq = 0;
boolean shiftDown = false, altDown = false;
static byte shiftKeyMappings[NUMKKEYS], altKeyMappings[NUMKKEYS];
// PRIVATE DATA DEFINITIONS ------------------------------------------------
/*static*/ inputdev_t inputDevices[NUM_INPUT_DEVICES];
static ddevent_t events[MAXEVENTS];
static int eventhead;
static int eventtail;
static char defaultShiftTable[96] = // Contains characters 32 to 127.
{
/* 32 */ ' ', 0, 0, 0, 0, 0, 0, '"',
/* 40 */ 0, 0, 0, 0, '<', '_', '>', '?', ')', '!',
/* 50 */ '@', '#', '$', '%', '^', '&', '*', '(', 0, ':',
/* 60 */ 0, '+', 0, 0, 0, 'a', 'b', 'c', 'd', 'e',
/* 70 */ 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
/* 80 */ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
/* 90 */ 'z', '{', '|', '}', 0, 0, 0, 'A', 'B', 'C',
/* 100 */ 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
/* 110 */ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
/* 120 */ 'X', 'Y', 'Z', 0, 0, 0, 0, 0
};
/* *INDENT-ON* */
static repeater_t keyReps[MAX_DOWNKEYS];
//static int oldJoyBState = 0;
static float oldPOV = IJOY_POV_CENTER;
static char* eventStrings[MAXEVENTS];
static boolean uiMouseMode = false; // Can mouse data be modified?
// CODE --------------------------------------------------------------------
void DD_RegisterInput(void)
{
// Cvars
C_VAR_INT("input-key-delay1", &keyRepeatDelay1, CVF_NO_MAX, 50, 0);
C_VAR_INT("input-key-delay2", &keyRepeatDelay2, CVF_NO_MAX, 20, 0);
C_VAR_INT("input-mouse-filter", &mouseFilter, 0, 0, MAX_AXIS_FILTER - 1);
C_VAR_INT("input-mouse-frequency", &mouseFreq, CVF_NO_MAX, 0, 0);
// Ccmds
#if 0
C_CMD("dumpkeymap", "s", DumpKeyMap);
C_CMD("keymap", "s", KeyMap);
#endif
C_CMD("listinputdevices", "", ListInputDevices);
//C_CMD_FLAGS("setaxis", "s", AxisPrintConfig, CMDF_NO_DEDICATED);
//C_CMD_FLAGS("setaxis", "ss", AxisChangeOption, CMDF_NO_DEDICATED);
//C_CMD_FLAGS("setaxis", "sss", AxisChangeValue, CMDF_NO_DEDICATED);
}
/**
* Allocate an array of bytes for the input devices keys.
* The allocated memory is cleared to zero.
*/
static void I_DeviceAllocKeys(inputdev_t *dev, uint count)
{
dev->numKeys = count;
dev->keys = M_Calloc(count * sizeof(inputdevkey_t));
}
static void I_DeviceAllocHats(inputdev_t *dev, uint count)
{
dev->numHats = count;
dev->hats = M_Calloc(count * sizeof(inputdevhat_t));
}
/**
* Add a new axis to the input device.
*/
static inputdevaxis_t *I_DeviceNewAxis(inputdev_t *dev, const char *name, uint type)
{
inputdevaxis_t *axis;
dev->axes = M_Realloc(dev->axes, sizeof(inputdevaxis_t) * ++dev->numAxes);
axis = &dev->axes[dev->numAxes - 1];
memset(axis, 0, sizeof(*axis));
strcpy(axis->name, name);
axis->type = type;
// Set reasonable defaults. The user's settings will be restored
// later.
axis->scale = 1;
axis->deadZone = 0;
return axis;
}
/**
* Initialize the input device state table.
*
* \note There need not be actual physical devices available in order to
* use these state tables.
*/
void I_InitVirtualInputDevices(void)
{
int i;
inputdev_t *dev;
inputdevaxis_t *axis;
memset(inputDevices, 0, sizeof(inputDevices));
// The keyboard is always assumed to be present.
// DDKEYs are used as key indices.
dev = &inputDevices[IDEV_KEYBOARD];
dev->flags = ID_ACTIVE;
strcpy(dev->name, "key");
I_DeviceAllocKeys(dev, 256);
// The mouse may not be active.
dev = &inputDevices[IDEV_MOUSE];
strcpy(dev->name, "mouse");
I_DeviceAllocKeys(dev, IMB_MAXBUTTONS);
// The first five mouse buttons have symbolic names.
dev->keys[0].name = "left";
dev->keys[1].name = "middle";
dev->keys[2].name = "right";
dev->keys[3].name = "wheelup";
dev->keys[4].name = "wheeldown";
// The mouse wheel is translated to keys, so there is no need to
// create an axis for it.
axis = I_DeviceNewAxis(dev, "x", IDAT_POINTER);
axis->filter = 1; // On by default.
axis->scale = 1.f/1000;
axis = I_DeviceNewAxis(dev, "y", IDAT_POINTER);
axis->filter = 1; // On by default.
axis->scale = 1.f/1000;
// Register console variables for the axis settings.
// CAUTION: Allocating new axes may invalidate the pointers here.
C_VAR_FLOAT("input-mouse-x-scale", &dev->axes[0].scale, CVF_NO_MAX, 0, 0);
C_VAR_INT("input-mouse-x-flags", &dev->axes[0].flags, 0, 0, 3);
C_VAR_FLOAT("input-mouse-y-scale", &dev->axes[1].scale, CVF_NO_MAX, 0, 0);
C_VAR_INT("input-mouse-y-flags", &dev->axes[1].flags, 0, 0, 3);
if(I_MousePresent())
dev->flags = ID_ACTIVE;
// TODO: Add support for several joysticks.
dev = &inputDevices[IDEV_JOY1];
strcpy(dev->name, "joy");
I_DeviceAllocKeys(dev, IJOY_MAXBUTTONS);
for(i = 0; i < IJOY_MAXAXES; ++i)
{
char name[32];
if(i < 4)
{
strcpy(name, i == 0? "x" : i == 1? "y" : i == 2? "z" : "w");
}
else
{
sprintf(name, "axis%02i", i + 1);
}
axis = I_DeviceNewAxis(dev, name, IDAT_STICK);
axis->scale = 1.0f / IJOY_AXISMAX;
axis->deadZone = DEFAULT_JOYSTICK_DEADZONE;
}
// Register console variables for the axis settings.
for(i = 0; i < IJOY_MAXAXES; ++i)
{
inputdevaxis_t* axis = &dev->axes[i];
char varName[80];
sprintf(varName, "input-joy-%s-scale", axis->name);
C_VAR_FLOAT(varName, &axis->scale, CVF_NO_MAX, 0, 0);
sprintf(varName, "input-joy-%s-flags", axis->name);
C_VAR_INT(varName, &axis->flags, 0, 0, 3);
sprintf(varName, "input-joy-%s-deadzone", axis->name);
C_VAR_FLOAT(varName, &axis->deadZone, 0, 0, 1);
}
I_DeviceAllocHats(dev, IJOY_MAXHATS);
for(i = 0; i < IJOY_MAXHATS; ++i)
{
dev->hats[i].pos = -1; // centered
}
// The joystick may not be active.
if(I_JoystickPresent())
dev->flags = ID_ACTIVE;
}
/**
* Free the memory allocated for the input devices.
*/
void I_ShutdownInputDevices(void)
{
uint i;
inputdev_t* dev;
for(i = 0; i < NUM_INPUT_DEVICES; ++i)
{
dev = &inputDevices[i];
if(dev->keys)
M_Free(dev->keys);
dev->keys = 0;
if(dev->axes)
M_Free(dev->axes);
dev->axes = 0;
if(dev->hats)
M_Free(dev->hats);
dev->hats = 0;
}
}
void I_DeviceReset(uint ident)
{
inputdev_t* dev = &inputDevices[ident];
int k;
for(k = 0; k < (int)dev->numAxes; ++k)
{
if(dev->axes[k].type == IDAT_POINTER)
{
// Clear the accumulation.
dev->axes[k].position = 0;
}
}
}
/**
* Retrieve a pointer to the input device state by identifier.
*
* @param ident Intput device identifier (index).
* @param ifactive Only return if the device is active.
*
* @return Ptr to the input device state OR @c NULL,.
*/
inputdev_t *I_GetDevice(uint ident, boolean ifactive)
{
inputdev_t* dev = &inputDevices[ident];
if(ifactive)
{
if(dev->flags & ID_ACTIVE)
return dev;
else
return NULL;
}
return dev;
}
/**
* Retrieve a pointer to the input device state by name.
*
* @param name Input device name.
* @param ifactive Only return if the device is active.
*
* @return Ptr to the input device state OR @c NULL,.
*/
inputdev_t *I_GetDeviceByName(const char *name, boolean ifactive)
{
uint i;
inputdev_t *dev = NULL;
boolean found;
i = 0;
found = false;
while(i < NUM_INPUT_DEVICES && !found)
{
if(!stricmp(inputDevices[i].name, name))
{
dev = &inputDevices[i];
found = true;
}
else
i++;
}
if(dev)
{
if(ifactive)
{
if(dev->flags & ID_ACTIVE)
return dev;
else
return NULL;
}
}
return dev;
}
/**
* Retrieve a ptr to the device axis specified by id.
*
* @param device Ptr to input device info, to get the axis ptr from.
* @param id Axis index, to search for.
*
* @return Ptr to the device axis OR @c NULL, if not
* found.
*/
inputdevaxis_t *I_GetAxisByID(inputdev_t *device, uint id)
{
if(!device || id > device->numAxes - 1)
return NULL;
return &device->axes[id-1];
}
/**
* Retrieve the index of a device's axis by name.
*
* @param device Ptr to input device info, to get the axis index from.
* @param name Ptr to string containing the name to be searched for.
*
* @return Index of the device axis named; or -1, if not found.
*/
int I_GetAxisByName(inputdev_t *device, const char *name)
{
uint i;
for(i = 0; i < device->numAxes; ++i)
{
if(!stricmp(device->axes[i].name, name))
return i;
}
return -1;
}
int I_GetKeyByName(inputdev_t* device, const char* name)
{
int i;
for(i = 0; i < (int)device->numKeys; ++i)
{
if(device->keys[i].name && !stricmp(device->keys[i].name, name))
return i;
}
return -1;
}
/**
* Check through the axes registered for the given device, see if there is
* one identified by the given name.
*
* @return @c false, if the string is invalid.
*/
boolean I_ParseDeviceAxis(const char* str, uint* deviceID, int* axis)
{
char name[30], *ptr;
inputdev_t* device;
ptr = strchr(str, '-');
if(!ptr)
return false;
// The name of the device.
memset(name, 0, sizeof(name));
strncpy(name, str, ptr - str);
device = I_GetDeviceByName(name, false);
if(device == NULL)
return false;
if(*deviceID)
*deviceID = device - inputDevices;
// The axis name.
if(*axis)
{
int a = I_GetAxisByName(device, ptr + 1);
if((*axis = a) < 0)
return false;
*axis = a + 1; // Axis indices are base 1.
}
return true;
}
float I_TransformAxis(inputdev_t* dev, uint axis, float rawPos)
{
float pos = rawPos;
inputdevaxis_t* a = &dev->axes[axis];
// Disabled axes are always zero.
if(a->flags & IDA_DISABLED)
{
return 0;
}
// Apply scaling, deadzone and clamping.
pos *= a->scale;
if(a->type == IDAT_STICK) // Pointer axes are not dead-zoned or clamped.
{
if(fabs(pos) <= a->deadZone)
{
pos = 0;
}
else
{
pos -= a->deadZone * SIGN_OF(pos); // Remove the dead zone.
pos *= 1.0f/(1.0f - a->deadZone); // Normalize.
pos = MINMAX_OF(-1.0f, pos, 1.0f);
}
}
if(a->flags & IDA_INVERT)
{
// Invert the axis position.
pos = -pos;
}
return pos;
}
/**
* Update an input device axis. Transformation is applied.
*/
static void I_UpdateAxis(inputdev_t *dev, uint axis, float pos, timespan_t ticLength)
{
inputdevaxis_t *a = &dev->axes[axis];
float oldRealPos = a->realPosition;
float transformed = I_TransformAxis(dev, axis, pos);
// The unfiltered position.
a->realPosition = transformed;
if(oldRealPos != a->realPosition)
{
// Mark down the time of the change.
a->time = Sys_GetRealTime();
}
if(a->filter > 0)
{
pos = a->realPosition;
}
else
{
// This is the new axis position.
pos = a->realPosition;
}
if(a->type == IDAT_STICK)
a->position = pos; //a->realPosition;
else // Cumulative.
a->position += pos; //a->realPosition;
// We can clear the expiration when it returns to default state.
if(!a->position || a->type == IDAT_POINTER)
{
a->assoc.flags &= ~IDAF_EXPIRED;
}
/* if(verbose > 3)
{
Con_Message("I_UpdateAxis: device=%s axis=%i pos=%f\n",
dev->name, axis, pos);
}*/
}
/**
* Update the input device state table.
*/
void I_TrackInput(ddevent_t *ev, timespan_t ticLength)
{
inputdev_t *dev;
if((dev = I_GetDevice(ev->device, true)) == NULL)
return;
// Track the state of Shift and Alt.
if(IS_KEY_TOGGLE(ev))
{
if(ev->toggle.id == DDKEY_RSHIFT)
{
if(ev->toggle.state == ETOG_DOWN)
shiftDown = true;
else if(ev->toggle.state == ETOG_UP)
shiftDown = false;
}
else if(ev->toggle.id == DDKEY_RALT)
{
if(ev->toggle.state == ETOG_DOWN)
altDown = true;
else if(ev->toggle.state == ETOG_UP)
altDown = false;
}
}
// Update the state table.
if(ev->type == E_AXIS)
{
I_UpdateAxis(dev, ev->axis.id, ev->axis.pos, ticLength);
}
else if(ev->type == E_TOGGLE)
{
inputdevkey_t* key = &dev->keys[ev->toggle.id];
key->isDown = (ev->toggle.state == ETOG_DOWN || ev->toggle.state == ETOG_REPEAT);
// Mark down the time when the change occurs.
if(ev->toggle.state == ETOG_DOWN || ev->toggle.state == ETOG_UP)
{
key->time = Sys_GetRealTime();
}
// We can clear the expiration when the key is released.
if(!key->isDown)
{
key->assoc.flags &= ~IDAF_EXPIRED;
}
}
else if(ev->type == E_ANGLE)
{
inputdevhat_t* hat = &dev->hats[ev->angle.id];
hat->pos = ev->angle.pos;
// Mark down the time when the change occurs.
hat->time = Sys_GetRealTime();
// We can clear the expiration when the hat is centered.
if(hat->pos < 0)
{
hat->assoc.flags &= ~IDAF_EXPIRED;
}
}
}
void I_ClearDeviceContextAssociations(void)
{
uint i, j;
inputdev_t *dev;
for(i = 0; i < NUM_INPUT_DEVICES; ++i)
{
dev = &inputDevices[i];
// Keys.
for(j = 0; j < dev->numKeys; ++j)
{
dev->keys[j].assoc.prevBContext = dev->keys[j].assoc.bContext;
dev->keys[j].assoc.bContext = NULL;
}
// Axes.
for(j = 0; j < dev->numAxes; ++j)
{
dev->axes[j].assoc.prevBContext = dev->axes[j].assoc.bContext;
dev->axes[j].assoc.bContext = NULL;
}
// Hats.
for(j = 0; j < dev->numHats; ++j)
{
dev->hats[j].assoc.prevBContext = dev->hats[j].assoc.bContext;
dev->hats[j].assoc.bContext = NULL;
}
}
}
/**
* @return The key state from the downKeys array.
*/
boolean I_IsDeviceKeyDown(uint ident, uint code)
{
inputdev_t *dev;
if((dev = I_GetDevice(ident, true)) != NULL)
{
if(code >= dev->numKeys)
return false;
return dev->keys[code].isDown;
}
return false;
}
/**
* @return Either key number or the scan code for the given token.
*/
int DD_KeyOrCode(char *token)
{
char *end = M_FindWhite(token);
if(end - token > 1)
{
// Longer than one character, it must be a number.
return strtol(token, 0, !strnicmp(token, "0x", 2) ? 16 : 10);
}
// Direct mapping.
return (unsigned char) *token;
}
/**
* Initializes the key mappings to the default values.
*/
void DD_InitInput(void)
{
int i;
for(i = 0; i < 256; ++i)
{
shiftKeyMappings[i] = i >= 32 && i <= 127 &&
defaultShiftTable[i - 32] ? defaultShiftTable[i - 32] : i;
altKeyMappings[i] = i;
}
}
const char* DD_AllocEventString(const char* str)
{
static int eventStringRover = 0;
const char* returnValue = 0;
free(eventStrings[eventStringRover]);
returnValue = eventStrings[eventStringRover] = strdup(str);
if(++eventStringRover >= MAXEVENTS)
{
eventStringRover = 0;
}
return returnValue;
}
void DD_ClearEventStrings(void)
{
int i;
for(i = 0; i < MAXEVENTS; ++i)
{
free(eventStrings[i]);
eventStrings[i] = 0;
}
}
/**
* Clear the input event queue.
*/
void DD_ClearEvents(void)
{
eventhead = eventtail;
DD_ClearEventStrings();
}
/**
* Called by the I/O functions when input is detected.
*/
void DD_PostEvent(ddevent_t *ev)
{
events[eventhead] = *ev;
if(ev->type == E_SYMBOLIC)
{
// Allocate a throw-away string from our buffer.
events[eventhead].symbolic.name = DD_AllocEventString(ev->symbolic.name);
}
eventhead++;
eventhead &= MAXEVENTS - 1;
}
/**
* Get the next event from the input event queue. Returns NULL if no
* more events are available.
*/
static ddevent_t *DD_GetEvent(void)
{
ddevent_t *ev;
if(eventhead == eventtail)
return NULL;
ev = &events[eventtail];
eventtail = (eventtail + 1) & (MAXEVENTS - 1);
return ev;
}
void DD_ConvertEvent(const ddevent_t* ddEvent, event_t* ev)
{
// Copy the essentials into a cutdown version for the game.
// Ensure the format stays the same for future compatibility!
//
// FIXME: This is probably broken! (DD_MICKEY_ACCURACY=1000 no longer used...)
//
memset(ev, 0, sizeof(ev));
if(ddEvent->type == E_SYMBOLIC)
{
ev->type = EV_SYMBOLIC;
#ifdef __64BIT__
ASSERT_64BIT(int64_t);
ASSERT_64BIT(ddEvent->symbolic.name);
ev->data1 = (int)(((int64_t) ddEvent->symbolic.name) & 0xffffffff); // low dword
ev->data2 = (int)(((int64_t) ddEvent->symbolic.name) >> 32); // high dword
#else
ASSERT_NOT_64BIT(ddEvent->symbolic.name);
ev->data1 = (int) ddEvent->symbolic.name;
ev->data2 = 0;
#endif
}
else
{
switch(ddEvent->device)
{
case IDEV_KEYBOARD:
ev->type = EV_KEY;
if(ddEvent->type == E_TOGGLE)
{
ev->state = ( ddEvent->toggle.state == ETOG_UP? EVS_UP :
ddEvent->toggle.state == ETOG_DOWN? EVS_DOWN :
EVS_REPEAT );
ev->data1 = ddEvent->toggle.id;
}
break;
case IDEV_MOUSE:
if(ddEvent->type == E_AXIS)
{
ev->type = EV_MOUSE_AXIS;
}
else if(ddEvent->type == E_TOGGLE)
{
ev->type = EV_MOUSE_BUTTON;
ev->data1 = ddEvent->toggle.id;
ev->state = ( ddEvent->toggle.state == ETOG_UP? EVS_UP :
ddEvent->toggle.state == ETOG_DOWN? EVS_DOWN :
EVS_REPEAT );
}
break;
case IDEV_JOY1:
case IDEV_JOY2:
case IDEV_JOY3:
case IDEV_JOY4:
if(ddEvent->type == E_AXIS)
{
int* data = &ev->data1;
ev->type = EV_JOY_AXIS;
ev->state = 0;
if(ddEvent->axis.id >= 0 && ddEvent->axis.id < 6)
{
data[ddEvent->axis.id] = ddEvent->axis.pos;
}
/// @todo The other dataN's must contain up-to-date information
/// as well. Read them from the current joystick status.
}
else if(ddEvent->type == E_TOGGLE)
{
ev->type = EV_JOY_BUTTON;
ev->state = ( ddEvent->toggle.state == ETOG_UP? EVS_UP :
ddEvent->toggle.state == ETOG_DOWN? EVS_DOWN :
EVS_REPEAT );
ev->data1 = ddEvent->toggle.id;
}
else if(ddEvent->type == E_ANGLE)
ev->type = EV_POV;
break;
default:
#if _DEBUG
Con_Error("DD_ProcessEvents: Unknown deviceID in ddevent_t");
#endif
break;
}
}
}
/**
* Send all the events of the given timestamp down the responder chain.
*/
static void dispatchEvents(timespan_t ticLength)
{
ddevent_t *ddev;
event_t ev;
while((ddev = DD_GetEvent()) != NULL)
{
if(ignoreInput)
continue;
// Update the state of the input device tracking table.
I_TrackInput(ddev, ticLength);
DD_ConvertEvent(ddev, &ev);
// Does the special responder use this event?
if(gx.PrivilegedResponder)
if(gx.PrivilegedResponder(&ev))
continue;
if(UI_Responder(ddev))
continue;
// The console.
if(Con_Responder(ddev))
continue;
// The game responder only returns true if the bindings
// can't be used (like when chatting).
if(gx.G_Responder(&ev))
continue;
// The bindings responder.
if(B_Responder(ddev))
continue;
// The "fallback" responder. Gets the event if no one else is
// interested.
if(gx.FallbackResponder)
gx.FallbackResponder(&ev);
}
}
/**
* Poll all event sources (i.e., input devices) and post events.
*/
static void postEvents(timespan_t ticLength)
{
DD_ReadKeyboard();
if(!isDedicated)
{ // In dedicated mode, we don't do mice or joysticks.
DD_ReadMouse(ticLength);
DD_ReadJoystick();
}
}
/**
* Process all incoming input for the given timestamp.
*
* This gets called at least 35 times per second. Usually more frequently
* than that.
*/
void DD_ProcessEvents(timespan_t ticLength)
{
// Poll all event sources (i.e., input devices) and post events.
postEvents(ticLength);
// Despatch all accumulated events down the responder chain.
dispatchEvents(ticLength);
}
/**
* Apply all active modifiers to the key.
*/
byte DD_ModKey(byte key)
{
if(shiftDown)
key = shiftKeyMappings[key];
if(altDown)
key = altKeyMappings[key];
if(key >= DDKEY_NUMPAD7 && key <= DDKEY_NUMPAD0)
{
byte numPadKeys[10] = {
'7', '8', '9', '4', '5', '6', '1', '2', '3', '0'
};
return numPadKeys[key - DDKEY_NUMPAD7];
}
return key;
}
/**
* Clears the repeaters array.
*/
void DD_ClearKeyRepeaters(void)
{
memset(keyReps, 0, sizeof(keyReps));
}
/**
* Checks the current keyboard state, generates input events
* based on pressed/held keys and posts them.
*/
void DD_ReadKeyboard(void)
{
uint i, k;
ddevent_t ev;
size_t n, numkeyevs;
keyevent_t keyevs[KBDQUESIZE];
// Check the repeaters.
ev.device = IDEV_KEYBOARD;
ev.type = E_TOGGLE;
ev.toggle.state = ETOG_REPEAT;
for(i = 0; i < MAX_DOWNKEYS; ++i)
{
repeater_t *rep = keyReps + i;
if(!rep->key)
continue;
ev.toggle.id = rep->key;
if(!rep->count && sysTime - rep->timer >= keyRepeatDelay1 / 1000.0)
{
// The first time.
rep->count++;
rep->timer += keyRepeatDelay1 / 1000.0;
DD_PostEvent(&ev);
}
if(rep->count)
{
while(sysTime - rep->timer >= keyRepeatDelay2 / 1000.0)
{
rep->count++;
rep->timer += keyRepeatDelay2 / 1000.0;