-
Notifications
You must be signed in to change notification settings - Fork 21
/
inputw32.cpp
1450 lines (1264 loc) · 34.4 KB
/
inputw32.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
//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
// in_win.c -- windows 95 mouse and joystick code
// 02/21/97 JCB Added extended DirectInput code to support external controllers.
#include <tier2/tier2.h>
#include <vgui/ISurface.h>
#include <vgui/Cursor.h>
#include "port.h"
#include "hud.h"
#include "cl_util.h"
#include "camera.h"
#include "kbutton.h"
#include "cvardef.h"
#include "usercmd.h"
#include "const.h"
#include "camera.h"
#include "in_defs.h"
#include "../public/keydefs.h"
#include "view.h"
#include "Exports.h"
#include "sdl_rt.h"
#ifdef PLATFORM_WINDOWS
#include <dinput.h>
#endif
#define MOUSE_BUTTON_COUNT 5
enum class MouseMode
{
Auto = -1,
Engine = 0,
DirectInput = 1,
RawInput = 2,
};
extern cl_enginefunc_t gEngfuncs;
extern int iMouseInUse;
extern kbutton_t in_strafe;
extern kbutton_t in_mlook;
extern kbutton_t in_speed;
extern kbutton_t in_jlook;
extern cvar_t *m_pitch;
extern cvar_t *m_yaw;
extern cvar_t *m_forward;
extern cvar_t *m_side;
extern cvar_t *lookstrafe;
extern cvar_t *lookspring;
extern cvar_t *cl_pitchdown;
extern cvar_t *cl_pitchup;
extern cvar_t *cl_yawspeed;
extern cvar_t *cl_sidespeed;
extern cvar_t *cl_forwardspeed;
extern cvar_t *cl_pitchspeed;
extern cvar_t *cl_movespeedkey;
static double s_flRawInputUpdateTime = 0.0f;
static bool m_bRawInput = false;
static bool m_bMouseThread = false;
extern globalvars_t *gpGlobals;
// mouse variables
cvar_t *m_filter;
cvar_t *sensitivity;
static cvar_t *m_rawinput = nullptr;
ConVar m_input("m_input", "-1", FCVAR_BHL_ARCHIVE,
"Mouse input mode\n"
"- 0: Engine\n"
"- 1: DirectInput (Windows-only)\n"
"- 2: Raw Input (Steam-only)");
static MouseMode m_mode = MouseMode::Engine;
// Custom mouse acceleration (0 disable, 1 to enable, 2 enable with separate yaw/pitch rescale)
static cvar_t *m_customaccel;
//Formula: mousesensitivity = ( rawmousedelta^m_customaccel_exponent ) * m_customaccel_scale + sensitivity
// If mode is 2, then x and y sensitivity are scaled by m_pitch and m_yaw respectively.
// Custom mouse acceleration value.
static cvar_t *m_customaccel_scale;
//Max mouse move scale factor, 0 for no limit
static cvar_t *m_customaccel_max;
//Mouse move is raised to this power before being scaled by scale factor
static cvar_t *m_customaccel_exponent;
// if threaded mouse is enabled then the time to sleep between polls
static cvar_t *m_mousethread_sleep;
int mouse_buttons;
int mouse_oldbuttonstate;
POINT current_pos;
int old_mouse_x, old_mouse_y, mx_accum, my_accum;
float mouse_x, mouse_y;
static int restore_spi;
static int originalmouseparms[3], newmouseparms[3] = { 0, 0, 1 };
static int mouseactive = 0;
int mouseinitialized;
static int mouseparmsvalid;
static int mouseshowtoggle = 1;
static bool s_bIsRelativeMode = false;
// joystick defines and variables
// where should defines be moved?
#define JOY_ABSOLUTE_AXIS 0x00000000 // control like a joystick
#define JOY_RELATIVE_AXIS 0x00000010 // control like a mouse, spinner, trackball
#define JOY_MAX_AXES 6 // X, Y, Z, R, U, V
#define JOY_AXIS_X 0
#define JOY_AXIS_Y 1
#define JOY_AXIS_Z 2
#define JOY_AXIS_R 3
#define JOY_AXIS_U 4
#define JOY_AXIS_V 5
enum _ControlList
{
AxisNada = 0,
AxisForward,
AxisLook,
AxisSide,
AxisTurn
};
DWORD dwAxisMap[JOY_MAX_AXES];
DWORD dwControlMap[JOY_MAX_AXES];
int pdwRawValue[JOY_MAX_AXES];
DWORD joy_oldbuttonstate, joy_oldpovstate;
int joy_id;
DWORD joy_numbuttons;
SDL_GameController *s_pJoystick = NULL;
// none of these cvars are saved over a session
// this means that advanced controller configuration needs to be executed
// each time. this avoids any problems with getting back to a default usage
// or when changing from one controller to another. this way at least something
// works.
cvar_t *in_joystick;
cvar_t *joy_name;
cvar_t *joy_advanced;
cvar_t *joy_advaxisx;
cvar_t *joy_advaxisy;
cvar_t *joy_advaxisz;
cvar_t *joy_advaxisr;
cvar_t *joy_advaxisu;
cvar_t *joy_advaxisv;
cvar_t *joy_forwardthreshold;
cvar_t *joy_sidethreshold;
cvar_t *joy_pitchthreshold;
cvar_t *joy_yawthreshold;
cvar_t *joy_forwardsensitivity;
cvar_t *joy_sidesensitivity;
cvar_t *joy_pitchsensitivity;
cvar_t *joy_yawsensitivity;
cvar_t *joy_wwhack1;
cvar_t *joy_wwhack2;
int joy_avail, joy_advancedinit, joy_haspov;
#ifdef _WIN32
DWORD s_hMouseThreadId = 0;
HANDLE s_hMouseThread = 0;
HANDLE s_hMouseQuitEvent = 0;
HANDLE s_hMouseDoneQuitEvent = 0;
// DirectInput
static LPDIRECTINPUT dinput_lpdi = NULL; // DirectInput interface
static LPDIRECTINPUTDEVICE dinput_lpdiMouse = NULL; // mouse device interface
static int dinput_mouse_acquired = 0;
static DIMOUSESTATE dinput_mousestate;
#endif
void IN_SetRelativeMouseMode(bool state)
{
if (s_bIsRelativeMode == state)
return;
s_bIsRelativeMode = state;
GetSDL()->SetRelativeMouseMode((SDL_bool)state);
}
/**
* Updates m_mode based on value of m_input cvar.
*/
void IN_UpdateMouseMode()
{
MouseMode newMode = (MouseMode)clamp(m_input.GetInt(), -1, 2);
if (newMode != m_mode)
{
if (newMode == MouseMode::Auto)
{
if (IsWindows())
{
// Prefer DirectInput on Windows
newMode = MouseMode::DirectInput;
}
else if (GetSDL()->IsGood())
{
// Prefer raw input on SteamPipe engine
newMode = MouseMode::RawInput;
}
else
{
newMode = MouseMode::Engine;
}
}
if (m_mode == MouseMode::RawInput)
{
// Disable relative mouse mode if switching off raw input
IN_SetRelativeMouseMode(false);
}
ConVarRef rawinput(m_rawinput);
switch (newMode)
{
case MouseMode::Engine:
{
ConPrintf("Set mouse input mode to engine input.\n");
if (rawinput.IsValid())
rawinput.SetValue(0);
break;
}
case MouseMode::DirectInput:
{
if (IsWindows())
{
ConPrintf("Set mouse input mode to DirectInput.\n");
if (rawinput.IsValid())
rawinput.SetValue(0);
if (m_mode == MouseMode::RawInput)
ConPrintf(ConColor::Yellow, "Warning: you may need to restart the game.\n");
}
else
{
ConPrintf(ConColor::Cyan, "DirectInput is not supported on Linux.\n");
ConPrintf(ConColor::Cyan, "Setting to Raw Input instead.\n");
newMode = MouseMode::RawInput;
rawinput.SetValue(1);
}
break;
}
case MouseMode::RawInput:
{
if (GetSDL()->IsGood() && rawinput.IsValid())
{
ConPrintf("Set mouse input mode to Raw Input.\n");
rawinput.SetValue(1);
}
else
{
ConPrintf(ConColor::Cyan, "Raw Input is not supported on your client.\n");
ConPrintf(ConColor::Cyan, "Setting to DirectInput instead.\n");
newMode = MouseMode::DirectInput;
}
break;
}
}
m_mode = newMode;
m_input.SetValue((int)newMode);
}
// We should never have Auto as mouse mode
Assert(m_mode != MouseMode::Auto);
// Only allow raw input if SDL2 is working
Assert(GetSDL()->IsGood() || m_mode != MouseMode::RawInput);
#ifndef PLATFORM_WINDOWS
// DirectInput is Windows-only
Assert(m_mode != MouseMode::DirectInput);
#endif
}
void IN_RunFrame()
{
// Wait a few frames for config to load
static int iDelay = 5;
if (iDelay > 0)
{
iDelay--;
return;
}
IN_UpdateMouseMode();
// Check m_rawinput
ConVarRef rawinput(m_rawinput);
if (rawinput.IsValid() && GetSDL()->IsGood())
{
if (rawinput.GetBool() && m_mode != MouseMode::RawInput)
{
m_input.SetValue((int)MouseMode::RawInput);
IN_UpdateMouseMode();
if (IsWindows())
ConPrintf(ConColor::Cyan, "NOTE: It is recommended to use `m_input 1` on Windows.\n");
}
else if (!rawinput.GetBool() && m_mode == MouseMode::RawInput)
{
m_input.SetValue((int)MouseMode::Engine);
IN_UpdateMouseMode();
}
}
}
void IN_SteamOverlayHidden()
{
gHUD.CallOnNextFrame([]() {
if (mouseactive && m_mode == MouseMode::RawInput && !g_pVGuiSurface->IsCursorVisible())
{
// Hiding Steam overlay in SDL2 input mode breaks the mouse.
// Relative mode needs to be reset
IN_SetRelativeMouseMode(false);
IN_SetRelativeMouseMode(true);
}
});
}
/*
===========
Force_CenterView_f
===========
*/
void Force_CenterView_f(void)
{
Vector viewangles;
if (!iMouseInUse)
{
gEngfuncs.GetViewAngles((float *)viewangles);
viewangles[PITCH] = 0;
gEngfuncs.SetViewAngles((float *)viewangles);
}
}
#ifdef _WIN32
long s_mouseDeltaX = 0;
long s_mouseDeltaY = 0;
POINT old_mouse_pos;
long ThreadInterlockedExchange(long *pDest, long value)
{
return InterlockedExchange(pDest, value);
}
DWORD WINAPI MousePos_ThreadFunction(LPVOID p)
{
s_hMouseDoneQuitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
while (1)
{
if (WaitForSingleObject(s_hMouseQuitEvent, (int)m_mousethread_sleep->value) == WAIT_OBJECT_0)
{
return 0;
}
if (mouseactive)
{
POINT mouse_pos;
GetCursorPos(&mouse_pos);
volatile int mx = mouse_pos.x - old_mouse_pos.x + s_mouseDeltaX;
volatile int my = mouse_pos.y - old_mouse_pos.y + s_mouseDeltaY;
ThreadInterlockedExchange(&old_mouse_pos.x, mouse_pos.x);
ThreadInterlockedExchange(&old_mouse_pos.y, mouse_pos.y);
ThreadInterlockedExchange(&s_mouseDeltaX, mx);
ThreadInterlockedExchange(&s_mouseDeltaY, my);
}
}
SetEvent(s_hMouseDoneQuitEvent);
return 0;
}
void DINPUT_SetBufferSize(void)
{
static DIPROPDWORD dipdw = { { sizeof(dipdw), sizeof(dipdw.diph), 0, DIPH_DEVICE }, 0 };
dipdw.dwData = dipdw.dwData + max(1ul, dipdw.dwData / 2);
gEngfuncs.Con_DPrintf("DirectInput overflow, increasing buffer size to %u.\n", dipdw.dwData);
IN_DeactivateMouse();
HRESULT hr = dinput_lpdiMouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph);
IN_ActivateMouse();
if (hr != DI_OK)
gEngfuncs.Con_DPrintf("Failed to increase DirectInput buffer size.\n");
}
#endif
/*
===========
IN_ActivateMouse
===========
*/
void CL_DLLEXPORT IN_ActivateMouse(void)
{
if (!mouseinitialized)
return;
#ifdef _WIN32
if (mouseparmsvalid)
restore_spi = SystemParametersInfo(SPI_SETMOUSE, 0, newmouseparms, 0);
if (dinput_lpdiMouse && !dinput_mouse_acquired)
{
if (dinput_lpdiMouse->Acquire() == DI_OK)
dinput_mouse_acquired = 1;
}
// Always clip cursor in fullscreen mode
// Except when SDL is used
HWND hwndA = GetActiveWindow();
if (hwndA && m_mode != MouseMode::RawInput)
{
HWND hwndF = GetForegroundWindow();
if (hwndA == hwndF && hwndF != GetDesktopWindow() && hwndF != GetShellWindow())
{
RECT appBounds, rect;
GetWindowRect(hwndA, &appBounds);
GetWindowRect(GetDesktopWindow(), &rect);
if (appBounds.left == rect.left && appBounds.right == rect.right && appBounds.top == rect.top && appBounds.bottom == rect.bottom)
ClipCursor(&rect);
}
}
#endif
if (m_mode == MouseMode::RawInput)
{
IN_SetRelativeMouseMode(true);
}
mouseactive = 1;
}
/*
===========
IN_DeactivateMouse
===========
*/
void CL_DLLEXPORT IN_DeactivateMouse(void)
{
if (!mouseinitialized)
return;
#ifdef _WIN32
if (restore_spi)
SystemParametersInfo(SPI_SETMOUSE, 0, originalmouseparms, 0);
if (dinput_lpdiMouse && dinput_mouse_acquired)
{
if (dinput_lpdiMouse->Unacquire() == DI_OK)
dinput_mouse_acquired = 0;
}
ClipCursor(NULL);
#endif
if (m_mode == MouseMode::RawInput)
{
IN_SetRelativeMouseMode(false);
}
mouseactive = 0;
}
/*
===========
IN_StartupMouse
===========
*/
void IN_StartupMouse(void)
{
if (gEngfuncs.CheckParm("-nomouse", NULL))
return;
if (mouseinitialized)
return;
mouseinitialized = 1;
mouse_buttons = MOUSE_BUTTON_COUNT;
#ifdef _WIN32
mouseparmsvalid = SystemParametersInfo(SPI_GETMOUSE, 0, originalmouseparms, 0);
if (mouseparmsvalid)
{
if (gEngfuncs.CheckParm("-noforcemspd", NULL))
newmouseparms[2] = originalmouseparms[2];
if (gEngfuncs.CheckParm("-noforcemaccel", NULL))
{
newmouseparms[0] = originalmouseparms[0];
newmouseparms[1] = originalmouseparms[1];
}
if (gEngfuncs.CheckParm("-noforcemparms", NULL))
{
newmouseparms[0] = originalmouseparms[0];
newmouseparms[1] = originalmouseparms[1];
newmouseparms[2] = originalmouseparms[2];
}
}
// Program instance
HINSTANCE hinst;
hinst = GetModuleHandle("client.dll");
if (DirectInput8Create(hinst, DIRECTINPUT_VERSION, IID_IDirectInput8A, (LPVOID *)&dinput_lpdi, NULL) != DI_OK)
return;
// We'll skip the enumeration step, since we care only about the standard system mouse.
if (dinput_lpdi->CreateDevice(GUID_SysMouse, &dinput_lpdiMouse, NULL) != DI_OK)
return;
HWND hwnd = GetActiveWindow();
if (dinput_lpdiMouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK)
return;
// Note: c_dfDIMouse is an external DIDATAFORMAT structure supplied by DirectInput.
if (dinput_lpdiMouse->SetDataFormat(&c_dfDIMouse) != DI_OK)
return;
#endif
}
/*
===========
IN_Shutdown
===========
*/
void IN_Shutdown(void)
{
IN_DeactivateMouse();
#ifdef _WIN32
if (s_hMouseQuitEvent)
{
SetEvent(s_hMouseQuitEvent);
WaitForSingleObject(s_hMouseDoneQuitEvent, 100);
}
if (s_hMouseThread)
{
TerminateThread(s_hMouseThread, 0);
CloseHandle(s_hMouseThread);
s_hMouseThread = (HANDLE)0;
}
if (s_hMouseQuitEvent)
{
CloseHandle(s_hMouseQuitEvent);
s_hMouseQuitEvent = (HANDLE)0;
}
if (s_hMouseDoneQuitEvent)
{
CloseHandle(s_hMouseDoneQuitEvent);
s_hMouseDoneQuitEvent = (HANDLE)0;
}
if (dinput_lpdiMouse)
{
IDirectInputDevice_Release(dinput_lpdiMouse);
dinput_lpdiMouse = nullptr;
}
if (dinput_lpdi)
{
IDirectInput_Release(dinput_lpdi);
dinput_lpdi = nullptr;
}
#endif
}
/*
===========
IN_GetMousePos
Ask for mouse position from engine
===========
*/
void IN_GetMousePos(int *mx, int *my)
{
gEngfuncs.GetMousePosition(mx, my);
}
/*
===========
IN_ResetMouse
FIXME: Call through to engine?
===========
*/
void IN_ResetMouse(void)
{
// no work to do in SDL
#ifdef _WIN32
if (!m_bRawInput && mouseactive && gEngfuncs.GetWindowCenterX && gEngfuncs.GetWindowCenterY)
{
SetCursorPos(gEngfuncs.GetWindowCenterX(), gEngfuncs.GetWindowCenterY());
ThreadInterlockedExchange(&old_mouse_pos.x, gEngfuncs.GetWindowCenterX());
ThreadInterlockedExchange(&old_mouse_pos.y, gEngfuncs.GetWindowCenterY());
}
if (gpGlobals && gpGlobals->time - s_flRawInputUpdateTime > 1.0f)
{
s_flRawInputUpdateTime = gpGlobals->time;
if (m_bRawInput)
m_bRawInput = m_rawinput->value != 0;
}
#endif
}
/*
===========
IN_MouseEvent
===========
*/
void CL_DLLEXPORT IN_MouseEvent(int mstate)
{
int i;
if (iMouseInUse || (g_pVGuiSurface && g_pVGuiSurface->IsCursorVisible()))
{
// Allow to release keys if they were pressed before mouse became used
for (int i = 0; i < mouse_buttons; i++)
{
if (!(mstate & (1 << i)) && (mouse_oldbuttonstate & (1 << i)))
{
gEngfuncs.Key_Event(K_MOUSE1 + i, 0);
mouse_oldbuttonstate &= ~(1 << i);
}
}
return;
}
// perform button actions
for (i = 0; i < mouse_buttons; i++)
{
if ((mstate & (1 << i)) && !(mouse_oldbuttonstate & (1 << i)))
{
gEngfuncs.Key_Event(K_MOUSE1 + i, 1);
}
if (!(mstate & (1 << i)) && (mouse_oldbuttonstate & (1 << i)))
{
gEngfuncs.Key_Event(K_MOUSE1 + i, 0);
}
}
mouse_oldbuttonstate = mstate;
}
//-----------------------------------------------------------------------------
// Purpose: Allows modulation of mouse scaling/senstivity value and application
// of custom algorithms.
// Input : *x -
// *y -
//-----------------------------------------------------------------------------
void IN_ScaleMouse(float *x, float *y)
{
float mx = *x;
float my = *y;
// This is the default sensitivity
float mouse_senstivity = (gHUD.GetSensitivity() != 0) ? gHUD.GetSensitivity() : sensitivity->value;
// Using special accleration values
if (m_customaccel->value != 0)
{
float raw_mouse_movement_distance = sqrt(mx * mx + my * my);
float acceleration_scale = m_customaccel_scale->value;
float accelerated_sensitivity_max = m_customaccel_max->value;
float accelerated_sensitivity_exponent = m_customaccel_exponent->value;
float accelerated_sensitivity = ((float)pow(raw_mouse_movement_distance, accelerated_sensitivity_exponent) * acceleration_scale + mouse_senstivity);
if (accelerated_sensitivity_max > 0.0001f && accelerated_sensitivity > accelerated_sensitivity_max)
{
accelerated_sensitivity = accelerated_sensitivity_max;
}
*x *= accelerated_sensitivity;
*y *= accelerated_sensitivity;
// Further re-scale by yaw and pitch magnitude if user requests alternate mode 2
// This means that they will need to up their value for m_customaccel_scale greatly (>40x) since m_pitch/yaw default
// to 0.022
if (m_customaccel->value == 2)
{
*x *= m_yaw->value;
*y *= m_pitch->value;
}
}
else
{
// Just apply the default
*x *= mouse_senstivity;
*y *= mouse_senstivity;
}
}
/*
===========
IN_MouseMove
===========
*/
void IN_MouseMove(float frametime, usercmd_t *cmd)
{
int mx, my;
Vector viewangles;
gEngfuncs.GetViewAngles((float *)viewangles);
if (in_mlook.state & 1)
{
V_StopPitchDrift();
}
//jjb - this disbles normal mouse control if the user is trying to
// move the camera, or if the mouse cursor is visible or if we're in intermission
if (!iMouseInUse && !gHUD.m_iIntermission && !g_pVGuiSurface->IsCursorVisible())
{
int deltaX, deltaY;
#ifdef _WIN32
if (m_mode == MouseMode::Engine)
{
if (m_bMouseThread)
{
ThreadInterlockedExchange(¤t_pos.x, s_mouseDeltaX);
ThreadInterlockedExchange(¤t_pos.y, s_mouseDeltaY);
ThreadInterlockedExchange(&s_mouseDeltaX, 0);
ThreadInterlockedExchange(&s_mouseDeltaY, 0);
}
else
{
GetCursorPos(¤t_pos);
}
}
else if (m_mode == MouseMode::DirectInput)
{
if (dinput_mouse_acquired)
{
HRESULT hr = dinput_lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&dinput_mousestate);
if (hr == DIERR_NOTACQUIRED || hr == DIERR_INPUTLOST)
dinput_lpdiMouse->Acquire();
else if (hr == DI_BUFFEROVERFLOW)
DINPUT_SetBufferSize();
}
}
else if (m_mode == MouseMode::RawInput)
#endif
{
IN_SetRelativeMouseMode(true);
GetSDL()->GetRelativeMouseState(&deltaX, &deltaY);
current_pos.x = deltaX;
current_pos.y = deltaY;
}
#ifdef _WIN32
if (m_mode == MouseMode::Engine)
{
if (m_bMouseThread)
{
mx = current_pos.x;
my = current_pos.y;
}
else
{
mx = current_pos.x - gEngfuncs.GetWindowCenterX() + mx_accum;
my = current_pos.y - gEngfuncs.GetWindowCenterY() + my_accum;
}
}
else if (m_mode == MouseMode::DirectInput)
{
if (dinput_mouse_acquired)
{
mx = dinput_mousestate.lX + mx_accum;
my = dinput_mousestate.lY + my_accum;
}
}
else if (m_mode == MouseMode::RawInput)
#endif
{
mx = deltaX + mx_accum;
my = deltaY + my_accum;
}
mx_accum = 0;
my_accum = 0;
if (m_filter && m_filter->value)
{
mouse_x = (mx + old_mouse_x) * 0.5;
mouse_y = (my + old_mouse_y) * 0.5;
}
else
{
mouse_x = mx;
mouse_y = my;
}
old_mouse_x = mx;
old_mouse_y = my;
// Apply custom mouse scaling/acceleration
IN_ScaleMouse(&mouse_x, &mouse_y);
// add mouse X/Y movement to cmd
if ((in_strafe.state & 1) || (lookstrafe->value && (in_mlook.state & 1)))
cmd->sidemove += m_side->value * mouse_x;
else
viewangles[YAW] -= m_yaw->value * mouse_x;
if ((in_mlook.state & 1) && !(in_strafe.state & 1))
{
viewangles[PITCH] += m_pitch->value * mouse_y;
if (viewangles[PITCH] > cl_pitchdown->value)
viewangles[PITCH] = cl_pitchdown->value;
if (viewangles[PITCH] < -cl_pitchup->value)
viewangles[PITCH] = -cl_pitchup->value;
}
else
{
if ((in_strafe.state & 1) && gEngfuncs.IsNoClipping())
{
cmd->upmove -= m_forward->value * mouse_y;
}
else
{
cmd->forwardmove -= m_forward->value * mouse_y;
}
}
// if the mouse has moved, force it to the center, so there's room to move
if (mx || my)
{
IN_ResetMouse();
}
}
else
{
#ifdef _WIN32
if (m_mode == MouseMode::DirectInput && dinput_mouse_acquired)
{
// Discard any info when mouse is not active
DIMOUSESTATE state;
dinput_lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&state);
}
else if (m_mode == MouseMode::RawInput)
#endif
{
// Disable realative mouse input
IN_SetRelativeMouseMode(false);
}
}
gEngfuncs.SetViewAngles((float *)viewangles);
/*
//#define TRACE_TEST
#if defined( TRACE_TEST )
{
int mx, my;
void V_Move( int mx, int my );
IN_GetMousePos( &mx, &my );
V_Move( mx, my );
}
#endif
*/
}
/*
===========
IN_Accumulate
===========
*/
void CL_DLLEXPORT IN_Accumulate(void)
{
//only accumulate mouse if we are not moving the camera with the mouse
if (!iMouseInUse && !g_pVGuiSurface->IsCursorVisible())
{
if (mouseactive)
{
#ifdef _WIN32
if (m_mode == MouseMode::Engine)
{
if (!m_bMouseThread)
{
GetCursorPos(¤t_pos);
mx_accum += current_pos.x - gEngfuncs.GetWindowCenterX();
my_accum += current_pos.y - gEngfuncs.GetWindowCenterY();
}
}
else if (m_mode == MouseMode::DirectInput)
{
if (dinput_mouse_acquired)
{
HRESULT hr = dinput_lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&dinput_mousestate);
if (hr == DIERR_NOTACQUIRED || hr == DIERR_INPUTLOST)
dinput_lpdiMouse->Acquire();
else if (hr == DI_BUFFEROVERFLOW)
DINPUT_SetBufferSize();
mx_accum += dinput_mousestate.lX;
my_accum += dinput_mousestate.lY;
}
}
else if (m_mode == MouseMode::RawInput)
#endif
{
int deltaX, deltaY;
GetSDL()->GetRelativeMouseState(&deltaX, &deltaY);
mx_accum += deltaX;
my_accum += deltaY;
}
// force the mouse to the center, so there's room to move
IN_ResetMouse();
}
}
}
/*
===================
IN_ClearStates
===================
*/
void CL_DLLEXPORT IN_ClearStates(void)
{
if (!mouseactive)
return;
mx_accum = 0;
my_accum = 0;
mouse_oldbuttonstate = 0;
}
/*
===============
IN_StartupJoystick
===============
*/
void IN_StartupJoystick(void)
{
// abort startup if user requests no joystick
if (gEngfuncs.CheckParm("-nojoy", NULL))
return;
// assume no joystick
joy_avail = 0;
// Joystick only with SDL
if (!GetSDL()->IsGood())
{
gEngfuncs.Con_DPrintf("joystick not found -- SDL2 is required\n\n");
return;
}
int nJoysticks = GetSDL()->NumJoysticks();
if (nJoysticks > 0)
{
for (int i = 0; i < nJoysticks; i++)
{
if (GetSDL()->IsGameController(i))
{
s_pJoystick = GetSDL()->GameControllerOpen(i);
if (s_pJoystick)
{
//save the joystick's number of buttons and POV status
joy_numbuttons = SDL_CONTROLLER_BUTTON_MAX;
joy_haspov = 0;