/
dd_input.cpp
2247 lines (1939 loc) · 58.5 KB
/
dd_input.cpp
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 dd_input.cpp Platform-independent input subsystem.
* @ingroup input
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/
#include <ctype.h>
#include <math.h>
#include <de/memory.h>
#include "de_platform.h"
#include "de_console.h"
#include "de_infine.h"
#include "de_system.h"
#include "de_misc.h"
#include "de_ui.h"
#include "dd_def.h"
#include "dd_main.h"
#include "dd_loop.h"
#include "render/vr.h"
#include "ui/clientwindowsystem.h"
#include <de/KeyEvent>
// For the debug visuals:
#if _DEBUG
# include "de_graphics.h"
#endif
using namespace de;
#define DEFAULT_JOYSTICK_DEADZONE .05f // 5%
#define MAX_AXIS_FILTER 40
#define KBDQUESIZE 32
#if 0
#define MAX_DOWNKEYS 16 // Most keyboards support 6 or 7.
typedef struct repeater_s {
int key; // The DDKEY code (0 if not in use).
int native; // Used to determine which key is repeating.
char text[8]; // Text to insert.
timespan_t timer; // How's the time?
int count; // How many times has been repeated?
} repeater_t;
#endif
typedef struct {
ddevent_t events[MAXEVENTS];
int head;
int tail;
} eventqueue_t;
#if 0
D_CMD(AxisPrintConfig);
D_CMD(AxisChangeOption);
D_CMD(AxisChangeValue);
#endif
D_CMD(ListInputDevices);
D_CMD(ReleaseMouse);
static void postEventsFromInputDevices(void);
// The initial and secondary repeater delays (tics).
int repWait1 = 15, repWait2 = 3;
unsigned int mouseFreq = 0;
dd_bool shiftDown = false, altDown = false;
inputdev_t inputDevices[NUM_INPUT_DEVICES];
//-------------------------------------------------------------------------
static dd_bool ignoreInput = false;
static byte shiftKeyMappings[NUMKKEYS], altKeyMappings[NUMKKEYS];
static eventqueue_t queue;
static eventqueue_t sharpQueue;
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, '~', '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
};
static float oldPOV = IJOY_POV_CENTER;
static char* eventStrings[MAXEVENTS];
static dd_bool uiMouseMode = false; // Can mouse data be modified?
static byte useSharpInputEvents = true; ///< cvar
#if _DEBUG
static byte devRendKeyState = false; ///< cvar
static byte devRendMouseState = false; ///< cvar
static byte devRendJoyState = false; ///< cvar
#endif
//-------------------------------------------------------------------------
void DD_RegisterInput(void)
{
// Cvars
C_VAR_BYTE("input-sharp", &useSharpInputEvents, 0, 0, 1);
#if _DEBUG
C_VAR_BYTE("rend-dev-input-joy-state", &devRendJoyState, CVF_NO_ARCHIVE, 0, 1);
C_VAR_BYTE("rend-dev-input-key-state", &devRendKeyState, CVF_NO_ARCHIVE, 0, 1);
C_VAR_BYTE("rend-dev-input-mouse-state", &devRendMouseState, CVF_NO_ARCHIVE, 0, 1);
#endif
// Ccmds
C_CMD("listinputdevices", "", ListInputDevices);
C_CMD("releasemouse", "", ReleaseMouse);
//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 = (inputdevkey_t *) M_Calloc(count * sizeof(inputdevkey_t));
}
static void I_DeviceAllocHats(inputdev_t *dev, uint count)
{
dev->numHats = count;
dev->hats = (inputdevhat_t *) 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 = (inputdevaxis_t *) 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;
axis->smoother = Smoother_New();
Smoother_SetMaximumPastNowDelta(axis->smoother, 2*SECONDSPERTIC);
// Set reasonable defaults. The user's settings will be restored
// later.
axis->scale = 1;
axis->deadZone = 0;
return axis;
}
static void registerAxisCvars(inputdev_t *device)
{
// Register console variables for the axis settings.
for(uint i = 0; i < device->numAxes; ++i)
{
inputdevaxis_t* axis = &device->axes[i];
char varName[80];
sprintf(varName, "input-%s-%s-scale", device->name, axis->name);
C_VAR_FLOAT(varName, &axis->scale, CVF_NO_MAX, 0, 0);
sprintf(varName, "input-%s-%s-flags", device->name, axis->name);
C_VAR_INT(varName, &axis->flags, 0, 0, 7);
sprintf(varName, "input-%s-%s-deadzone", device->name, axis->name);
C_VAR_FLOAT(varName, &axis->deadZone, 0, 0, 1);
}
}
/**
* 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)
{
inputdev_t* dev;
inputdevaxis_t* axis;
// Allow re-init.
I_ShutdownInputDevices();
de::zap(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->niceName, "Keyboard");
strcpy(dev->name, "key");
I_DeviceAllocKeys(dev, 256);
// The mouse may not be active.
dev = &inputDevices[IDEV_MOUSE];
strcpy(dev->niceName, "Mouse");
strcpy(dev->name, "mouse");
I_DeviceAllocKeys(dev, IMB_MAXBUTTONS);
// Some of the mouse buttons have symbolic names.
dev->keys[IMB_LEFT].name = "left";
dev->keys[IMB_MIDDLE].name = "middle";
dev->keys[IMB_RIGHT].name = "right";
dev->keys[IMB_MWHEELUP].name = "wheelup";
dev->keys[IMB_MWHEELDOWN].name = "wheeldown";
dev->keys[IMB_MWHEELLEFT].name = "wheelleft";
dev->keys[IMB_MWHEELRIGHT].name = "wheelright";
// 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, 7);
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, 7);
//C_VAR_INT("input-mouse-filter", &dev->axes[0].filter, 0, 0, MAX_AXIS_FILTER - 1); // note: same filter used for Y axis
if(Mouse_IsPresent())
dev->flags = ID_ACTIVE;
// TODO: Add support for several joysticks.
{
dev = &inputDevices[IDEV_JOY1];
strcpy(dev->niceName, "Joystick");
strcpy(dev->name, "joy");
I_DeviceAllocKeys(dev, IJOY_MAXBUTTONS);
for(int 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.
registerAxisCvars(dev);
I_DeviceAllocHats(dev, IJOY_MAXHATS);
for(int i = 0; i < IJOY_MAXHATS; ++i)
{
dev->hats[i].pos = -1; // centered
}
// The joystick may not be active.
if(Joystick_IsPresent())
dev->flags = ID_ACTIVE;
}
// Set up a head tracking device.
{
dev = &inputDevices[IDEV_HEAD_TRACKER];
strcpy(dev->niceName, "Head Tracker");
strcpy(dev->name, "head");
axis = I_DeviceNewAxis(dev, "yaw", IDAT_STICK);
axis->flags |= IDA_RAW;
axis = I_DeviceNewAxis(dev, "pitch", IDAT_STICK);
axis->flags |= IDA_RAW;
axis = I_DeviceNewAxis(dev, "roll", IDAT_STICK);
axis->flags |= IDA_RAW;
registerAxisCvars(dev);
}
}
/**
* Free the memory allocated for the input devices.
*/
void I_ShutdownInputDevices(void)
{
uint i, k;
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)
{
for(k = 0; k < dev->numAxes; ++k)
{
Smoother_Delete(dev->axes[k].smoother);
}
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;
LOG_INPUT_VERBOSE("Reset input device %s") << Str_Text(I_DeviceNameStr(ident));
for(k = 0; k < (int)dev->numKeys && dev->keys; ++k)
{
if(dev->keys[k].isDown)
{
dev->keys[k].assoc.flags |= IDAF_EXPIRED;
}
else
{
dev->keys[k].isDown = false;
dev->keys[k].time = 0;
dev->keys[k].assoc.flags &= ~(IDAF_TRIGGERED | IDAF_EXPIRED);
}
}
for(k = 0; k < (int)dev->numAxes && dev->axes; ++k)
{
if(dev->axes[k].type == IDAT_POINTER)
{
// Clear the accumulation.
dev->axes[k].position = 0;
dev->axes[k].sharpPosition = 0;
dev->axes[k].prevSmoothPos = 0;
}
Smoother_Clear(dev->axes[k].smoother);
}
if(ident == IDEV_KEYBOARD)
{
altDown = shiftDown = false;
}
}
void I_ResetAllDevices(void)
{
uint i;
for(i = 0; i < NUM_INPUT_DEVICES; ++i)
{
I_DeviceReset(i);
}
}
inputdev_t *I_GetDevice(uint ident, InputDeviceGetMode mode)
{
inputdev_t *dev = &inputDevices[ident];
if(mode == OnlyActiveInputDevice)
{
if(!(dev->flags & ID_ACTIVE))
return 0;
}
return dev;
}
inputdev_t *I_GetDeviceByName(const char *name, InputDeviceGetMode mode)
{
for(uint i = 0; i < NUM_INPUT_DEVICES; ++i)
{
if(!stricmp(inputDevices[i].name, name))
{
return I_GetDevice(i, mode);
}
}
return 0;
}
ddstring_t const *I_DeviceNameStr(uint ident)
{
static const de::Str names[1 + NUM_INPUT_DEVICES] = {
"(invalid-identifier)",
"keyboard",
"mouse",
"joystick",
"joystick2",
"joystick3",
"joystick4"
};
if(ident >= NUM_INPUT_DEVICES) return names[0];
return names[1 + ident];
}
inputdevaxis_t* I_GetAxisByID(inputdev_t* device, uint id)
{
if(!device || id == 0 || id > device->numAxes - 1)
return NULL;
return &device->axes[id-1];
}
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;
}
inputdevkey_t* I_GetKeyByID(inputdev_t* device, uint id)
{
if(!device || id == 0 || id > device->numKeys - 1)
return NULL;
return &device->keys[id-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;
}
inputdevhat_t* I_GetHatByID(inputdev_t* device, uint id)
{
if(!device || id == 0 || id > device->numHats - 1)
return NULL;
return &device->hats[id-1];
}
dd_bool I_ParseDeviceAxis(const char* str, uint* deviceID, uint* axis)
{
char name[30];
char const *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);
if(device == NULL)
return false;
if(deviceID)
*deviceID = device - inputDevices;
// The axis name.
if(axis)
{
int a = I_GetAxisByName(device, ptr + 1);
if(a < 0)
{
*axis = 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) // Only stick axes are dead-zoned and 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;
}
#if 0
static float filterAxis(int grade, float* accumulation, float ticLength)
{
float target;
float avail;
float used;
int dir;
dir = SIGN_OF(*accumulation);
avail = fabs(*accumulation);
// Determine the target velocity.
target = avail * (MAX_AXIS_FILTER - MINMAX_OF(1, grade, 39));
/*
// test: clamp
if(target < -.7) target = -.7;
else if(target > .7) target = .7;
else target = 0;
*/
// Determine the amount of mickeys to send. It depends on the
// current mouse velocity, and how much time has passed.
used = target * ticLength;
// Don't go past the available motion.
if(used > avail)
{
*accumulation = 0;
used = avail;
}
else
{
if(*accumulation > 0)
*accumulation -= used;
else
*accumulation += used;
}
// This is the new (filtered) axis position.
return dir * used;
}
#endif
/**
* Update an input device axis. Transformation is applied.
*/
static void I_ApplyRealPositionToAxis(inputdev_t* dev, uint axis, float pos)
{
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 = DD_LatestRunTicsStartTime();
}
if(a->type == IDAT_STICK)
{
a->sharpPosition = a->realPosition;
}
else // Cumulative.
{
// Convert the delta to an absolute position for smoothing.
a->sharpPosition += a->realPosition;
}
Smoother_AddPosXY(a->smoother, DD_LatestRunTicsStartTime(), a->sharpPosition, 0);
}
static void I_UpdateAxis(inputdev_t *dev, uint axis, timespan_t ticLength)
{
inputdevaxis_t *a = &dev->axes[axis];
Smoother_Advance(a->smoother, ticLength);
if(a->type == IDAT_STICK)
{
if(a->flags & IDA_RAW)
{
// The axis is supposed to be unfiltered.
a->position = a->realPosition;
}
else
{
// Absolute positions are straightforward to evaluate.
Smoother_EvaluateComponent(a->smoother, 0, &a->position);
}
}
else if(a->type == IDAT_POINTER)
{
if(a->flags & IDA_RAW)
{
// The axis is supposed to be unfiltered.
a->position += a->realPosition;
a->realPosition = 0;
}
else
{
// Apply smoothing by converting back into a delta.
coord_t smoothPos = a->prevSmoothPos;
Smoother_EvaluateComponent(a->smoother, 0, &smoothPos);
a->position += smoothPos - a->prevSmoothPos;
a->prevSmoothPos = smoothPos;
}
}
// We can clear the expiration now that an updated value is available.
a->assoc.flags &= ~IDAF_EXPIRED;
}
dd_bool I_ShiftDown(void)
{
return shiftDown;
}
/**
* Update the input device state table.
*/
void I_TrackInput(ddevent_t *ev)
{
if(ev->type == E_FOCUS || ev->type == E_SYMBOLIC)
return; // Not a tracked device state.
inputdev_t *dev;
if((dev = I_GetDevice(ev->device, OnlyActiveInputDevice)) == 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;
//qDebug() << "Alt down";
}
else if(ev->toggle.state == ETOG_UP)
{
altDown = false;
//qDebug() << "Alt up";
}
}
}
// Update the state table.
if(ev->type == E_AXIS)
{
I_ApplyRealPositionToAxis(dev, ev->axis.id, ev->axis.pos);
}
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 = Timer_RealMilliseconds();
}
if(key->isDown)
{
// This will get cleared after the state is checked by someone.
key->assoc.flags |= IDAF_TRIGGERED;
}
else
{
// We can clear the expiration when the key is released.
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 = Timer_RealMilliseconds();
// We can clear the expiration when the hat is centered.
if(hat->pos < 0)
{
hat->assoc.flags &= ~IDAF_EXPIRED;
}
}
}
void I_ClearDeviceContextAssociations(void)
{
inputdev_t* dev;
uint i, j;
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;
dev->keys[j].assoc.flags &= ~IDAF_TRIGGERED;
}
// 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;
}
}
}
dd_bool I_IsKeyDown(inputdev_t* dev, uint id)
{
if(dev && id < dev->numKeys)
{
return dev->keys[id].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)
{
if(i >= 32 && i <= 127)
shiftKeyMappings[i] = defaultShiftTable[i - 32] ? defaultShiftTable[i - 32] : i;
else
shiftKeyMappings[i] = i;
altKeyMappings[i] = i;
}
}
/**
* Returns a copy of the string @a str. The caller does not get ownership of
* the string. The string is valid until it gets overwritten by a new
* allocation. There are at most MAXEVENTS strings allocated at a time.
*
* These are intended for strings in ddevent_t that are valid during the
* processing of an event.
*/
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;
}
}
static void clearQueue(eventqueue_t* q)
{
q->head = q->tail;
}
dd_bool DD_IgnoreInput(dd_bool ignore)
{
dd_bool old = ignoreInput;
ignoreInput = ignore;
LOG_INPUT_VERBOSE("Ignoring input: %b") << ignore;
if(!ignore)
{
// Clear all the event buffers.
postEventsFromInputDevices();
DD_ClearEvents();
}
return old;
}
/**
* Clear the input event queue.
*/
void DD_ClearEvents(void)
{
clearQueue(&queue);
clearQueue(&sharpQueue);
DD_ClearEventStrings();
}
static void postToQueue(eventqueue_t* q, ddevent_t* ev)
{
q->events[q->head] = *ev;
if(ev->type == E_SYMBOLIC)
{
// Allocate a throw-away string from our buffer.
q->events[q->head].symbolic.name = DD_AllocEventString(ev->symbolic.name);
}
q->head++;
q->head &= MAXEVENTS - 1;
}
/**
* Called by the I/O functions when input is detected.
*/
void DD_PostEvent(ddevent_t *ev)
{
eventqueue_t* q = &queue;
if(useSharpInputEvents && (ev->type == E_TOGGLE || ev->type == E_AXIS ||
ev->type == E_ANGLE))
{
q = &sharpQueue;
}
// Cleanup: make sure only keyboard toggles can have a text insert.
if(ev->type == E_TOGGLE && ev->device != IDEV_KEYBOARD)
{
memset(ev->toggle.text, 0, sizeof(ev->toggle.text));
}
postToQueue(q, ev);
#ifdef LIBDENG_CAMERA_MOVEMENT_ANALYSIS
if(ev->device == IDEV_KEYBOARD && ev->type == E_TOGGLE
&& ev->toggle.state == ETOG_DOWN)
{
// Restart timer on each key down.
extern float devCameraMovementStartTime;
extern float devCameraMovementStartTimeRealSecs;
devCameraMovementStartTime = sysTime;
devCameraMovementStartTimeRealSecs = Sys_GetRealSeconds();
}
#endif
}
/**
* Gets the next event from an input event queue.
* @param q Event queue.
* @return @c NULL if no more events are available.
*/
static ddevent_t* nextFromQueue(eventqueue_t* q)
{
ddevent_t *ev;
if(q->head == q->tail)
return NULL;
ev = &q->events[q->tail];
q->tail = (q->tail + 1) & (MAXEVENTS - 1);
return ev;
}
void DD_ConvertEvent(de::Event const &event, ddevent_t *ddEvent)
{
using de::KeyEvent;
memset(ddEvent, 0, sizeof(*ddEvent));
switch(event.type())
{
case de::Event::KeyPress:
case de::Event::KeyRelease:
{
KeyEvent const &kev = event.as<KeyEvent>();
ddEvent->device = IDEV_KEYBOARD;
ddEvent->type = E_TOGGLE;
ddEvent->toggle.id = kev.ddKey();
ddEvent->toggle.state = (kev.state() == KeyEvent::Pressed? ETOG_DOWN : ETOG_UP);
strcpy(ddEvent->toggle.text, kev.text().toLatin1());
break;
}
default:
break;
}
}
bool DD_ConvertEvent(ddevent_t const *ddEvent, event_t *ev)
{
// Copy the essentials into a cutdown version for the game.
// Ensure the format stays the same for future compatibility!
//
/// @todo This is probably broken! (DD_MICKEY_ACCURACY=1000 no longer used...)
//
memset(ev, 0, sizeof(*ev));
if(ddEvent->type == E_SYMBOLIC)