/
i_input.cpp
915 lines (807 loc) · 21.1 KB
/
i_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
/*
** i_input.cpp
** Handles input from keyboard, mouse, and joystick
**
**---------------------------------------------------------------------------
** Copyright 1998-2009 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
// DI3 only supports up to 4 mouse buttons, and I want the joystick to
// be read using DirectInput instead of winmm.
#define WIN32_LEAN_AND_MEAN
#define __BYTEBOOL__
#ifndef __GNUC__
#define INITGUID
#endif
#define DIRECTINPUT_VERSION 0x800
#include <windows.h>
#include <dbt.h>
#include <dinput.h>
#include <malloc.h>
#ifdef _MSC_VER
#pragma warning(disable:4244)
#endif
// Compensate for w32api's lack
#ifndef WM_WTSSESSION_CHANGE
#define WM_WTSSESSION_CHANGE 0x02B1
#define WTS_CONSOLE_CONNECT 1
#define WTS_CONSOLE_DISCONNECT 2
#define WTS_SESSION_LOCK 7
#define WTS_SESSION_UNLOCK 8
#endif
#ifndef PBT_APMSUSPEND
// w32api does not #define the PBT_ macros in winuser.h like the PSDK does
#include <pbt.h>
#endif
#ifndef GET_RAWINPUT_CODE_WPARAM
#define GET_RAWINPUT_CODE_WPARAM(wParam) ((wParam) & 0xff)
#endif
#include "c_dispatch.h"
#include "m_argv.h"
#include "i_input.h"
#include "v_video.h"
#include "i_sound.h"
#include "d_gui.h"
#include "c_console.h"
#include "s_soundinternal.h"
#include "gameconfigfile.h"
#include "hardware.h"
#include "d_eventbase.h"
#include "v_text.h"
#include "version.h"
#include "engineerrors.h"
#include "i_system.h"
#include "i_interface.h"
#include "printf.h"
#include "c_buttons.h"
#include "cmdlib.h"
// Compensate for w32api's lack
#ifndef GET_XBUTTON_WPARAM
#define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
#endif
#ifdef _DEBUG
#define INGAME_PRIORITY_CLASS NORMAL_PRIORITY_CLASS
#else
//#define INGAME_PRIORITY_CLASS HIGH_PRIORITY_CLASS
#define INGAME_PRIORITY_CLASS NORMAL_PRIORITY_CLASS
#endif
FJoystickCollection *JoyDevices[NUM_JOYDEVICES];
extern HINSTANCE g_hInst;
extern DWORD SessionID;
static HMODULE DInputDLL;
bool GUICapture;
extern FMouse *Mouse;
extern FKeyboard *Keyboard;
extern bool ToggleFullscreen;
bool VidResizing;
extern BOOL vidactive;
extern HWND Window, ConWindow;
EXTERN_CVAR (String, language)
EXTERN_CVAR (Bool, lookstrafe)
EXTERN_CVAR (Bool, use_joystick)
EXTERN_CVAR (Bool, use_mouse)
static int WheelDelta;
extern bool CursorState;
extern BOOL paused;
static bool noidle = false;
LPDIRECTINPUT8 g_pdi;
LPDIRECTINPUT g_pdi3;
extern bool AppActive;
int SessionState = 0;
int BlockMouseMove;
static bool EventHandlerResultForNativeMouse;
CVAR (Bool, k_allowfullscreentoggle, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
extern int chatmodeon;
static void I_CheckGUICapture ()
{
bool wantCapt = sysCallbacks.WantGuiCapture && sysCallbacks.WantGuiCapture();
if (wantCapt != GUICapture)
{
GUICapture = wantCapt;
if (wantCapt && Keyboard != NULL)
{
Keyboard->AllKeysUp();
}
}
}
void I_SetMouseCapture()
{
SetCapture(Window);
}
void I_ReleaseMouseCapture()
{
ReleaseCapture();
}
bool GUIWndProcHook(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result)
{
event_t ev = { EV_GUI_Event };
*result = 0;
switch (message)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
if (message == WM_KEYUP || message == WM_SYSKEYUP)
{
ev.subtype = EV_GUI_KeyUp;
}
else
{
ev.subtype = (lParam & 0x40000000) ? EV_GUI_KeyRepeat : EV_GUI_KeyDown;
}
if (GetKeyState(VK_SHIFT) & 0x8000) ev.data3 |= GKM_SHIFT;
if (GetKeyState(VK_CONTROL) & 0x8000) ev.data3 |= GKM_CTRL;
if (GetKeyState(VK_MENU) & 0x8000) ev.data3 |= GKM_ALT;
if (wParam == VK_PROCESSKEY)
{ // Use the scan code to determine the real virtual-key code.
// ImmGetVirtualKey() will supposedly do this, but it just returns
// VK_PROCESSKEY again.
wParam = MapVirtualKey((lParam >> 16) & 255, 1);
}
if ( (ev.data1 = MapVirtualKey(wParam, 2)) )
{
D_PostEvent(&ev);
}
else
{
switch (wParam)
{
case VK_PRIOR: ev.data1 = GK_PGUP; break;
case VK_NEXT: ev.data1 = GK_PGDN; break;
case VK_END: ev.data1 = GK_END; break;
case VK_HOME: ev.data1 = GK_HOME; break;
case VK_LEFT: ev.data1 = GK_LEFT; break;
case VK_RIGHT: ev.data1 = GK_RIGHT; break;
case VK_UP: ev.data1 = GK_UP; break;
case VK_DOWN: ev.data1 = GK_DOWN; break;
case VK_DELETE: ev.data1 = GK_DEL; break;
case VK_ESCAPE: ev.data1 = GK_ESCAPE; break;
case VK_F1: ev.data1 = GK_F1; break;
case VK_F2: ev.data1 = GK_F2; break;
case VK_F3: ev.data1 = GK_F3; break;
case VK_F4: ev.data1 = GK_F4; break;
case VK_F5: ev.data1 = GK_F5; break;
case VK_F6: ev.data1 = GK_F6; break;
case VK_F7: ev.data1 = GK_F7; break;
case VK_F8: ev.data1 = GK_F8; break;
case VK_F9: ev.data1 = GK_F9; break;
case VK_F10: ev.data1 = GK_F10; break;
case VK_F11: ev.data1 = GK_F11; break;
case VK_F12: ev.data1 = GK_F12; break;
case VK_BROWSER_BACK: ev.data1 = GK_BACK; break;
}
if (ev.data1 != 0)
{
D_PostEvent(&ev);
}
}
// Return false for key downs so that we can handle special hotkeys
// in the main WndProc.
return ev.subtype == EV_GUI_KeyUp;
case WM_CHAR:
case WM_SYSCHAR:
if (wParam >= ' ') // only send displayable characters
{
ev.subtype = EV_GUI_Char;
ev.data1 = wParam;
ev.data2 = (message == WM_SYSCHAR);
D_PostEvent(&ev);
return true;
}
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
case WM_MOUSEMOVE:
if (message >= WM_LBUTTONDOWN && message <= WM_LBUTTONDBLCLK)
{
ev.subtype = message - WM_LBUTTONDOWN + EV_GUI_LButtonDown;
}
else if (message >= WM_RBUTTONDOWN && message <= WM_RBUTTONDBLCLK)
{
ev.subtype = message - WM_RBUTTONDOWN + EV_GUI_RButtonDown;
}
else if (message >= WM_MBUTTONDOWN && message <= WM_MBUTTONDBLCLK)
{
ev.subtype = message - WM_MBUTTONDOWN + EV_GUI_MButtonDown;
}
else if (message >= WM_XBUTTONDOWN && message <= WM_XBUTTONUP)
{
ev.subtype = message - WM_XBUTTONDOWN + EV_GUI_BackButtonDown;
if (GET_XBUTTON_WPARAM(wParam) == 2)
{
ev.subtype += EV_GUI_FwdButtonDown - EV_GUI_BackButtonDown;
}
else if (GET_XBUTTON_WPARAM(wParam) != 1)
{
break;
}
}
else if (message == WM_MOUSEMOVE)
{
ev.subtype = EV_GUI_MouseMove;
if (BlockMouseMove > 0) return true;
}
ev.data1 = LOWORD(lParam);
ev.data2 = HIWORD(lParam);
if (screen != NULL)
{
screen->ScaleCoordsFromWindow(ev.data1, ev.data2);
}
if (wParam & MK_SHIFT) ev.data3 |= GKM_SHIFT;
if (wParam & MK_CONTROL) ev.data3 |= GKM_CTRL;
if (GetKeyState(VK_MENU) & 0x8000) ev.data3 |= GKM_ALT;
if (use_mouse) D_PostEvent(&ev);
return true;
// Note: If the mouse is grabbed, it sends the mouse wheel events itself.
case WM_MOUSEWHEEL:
if (!use_mouse) return false;
if (wParam & MK_SHIFT) ev.data3 |= GKM_SHIFT;
if (wParam & MK_CONTROL) ev.data3 |= GKM_CTRL;
if (GetKeyState(VK_MENU) & 0x8000) ev.data3 |= GKM_ALT;
WheelDelta += (SHORT)HIWORD(wParam);
if (WheelDelta < 0)
{
ev.subtype = EV_GUI_WheelDown;
while (WheelDelta <= -WHEEL_DELTA)
{
D_PostEvent(&ev);
WheelDelta += WHEEL_DELTA;
}
}
else
{
ev.subtype = EV_GUI_WheelUp;
while (WheelDelta >= WHEEL_DELTA)
{
D_PostEvent(&ev);
WheelDelta -= WHEEL_DELTA;
}
}
return true;
}
return false;
}
bool CallHook(FInputDevice *device, HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result)
{
if (device == NULL)
{
return false;
}
*result = 0;
return device->WndProcHook(hWnd, message, wParam, lParam, result);
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT result;
if (message == WM_INPUT)
{
UINT size;
if (!GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER)) &&
size != 0)
{
uint8_t *buffer = (uint8_t *)alloca(size);
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, buffer, &size, sizeof(RAWINPUTHEADER)) == size)
{
int code = GET_RAWINPUT_CODE_WPARAM(wParam);
if (Keyboard == NULL || !Keyboard->ProcessRawInput((RAWINPUT *)buffer, code))
{
if (Mouse == NULL || !Mouse->ProcessRawInput((RAWINPUT *)buffer, code))
{
if (JoyDevices[INPUT_RawPS2] != NULL)
{
JoyDevices[INPUT_RawPS2]->ProcessRawInput((RAWINPUT *)buffer, code);
}
}
}
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
if (CallHook(Keyboard, hWnd, message, wParam, lParam, &result))
{
return result;
}
if (CallHook(Mouse, hWnd, message, wParam, lParam, &result))
{
return result;
}
for (int i = 0; i < NUM_JOYDEVICES; ++i)
{
if (CallHook(JoyDevices[i], hWnd, message, wParam, lParam, &result))
{
return result;
}
}
if (GUICapture && GUIWndProcHook(hWnd, message, wParam, lParam, &result))
{
return result;
}
if (message == WM_LBUTTONDOWN && sysCallbacks.WantLeftButton() && sysCallbacks.WantLeftButton())
{
if (GUIWndProcHook(hWnd, message, wParam, lParam, &result))
{
return result;
}
}
switch (message)
{
case WM_DESTROY:
SetPriorityClass (GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
PostQuitMessage (0);
break;
case WM_HOTKEY:
break;
case WM_PAINT:
return DefWindowProc (hWnd, message, wParam, lParam);
case WM_SETTINGCHANGE:
// If regional settings were changed, reget preferred languages
if (wParam == 0 && lParam != 0 && strcmp ((const char *)lParam, "intl") == 0)
{
language.Callback ();
}
return 0;
case WM_KILLFOCUS:
I_CheckNativeMouse (true, false); // Make sure mouse gets released right away
break;
case WM_SETFOCUS:
I_CheckNativeMouse (false, EventHandlerResultForNativeMouse); // This cannot call the event handler. Doing it from here is unsafe.
break;
case WM_SETCURSOR:
if (!CursorState)
{
SetCursor(NULL); // turn off window cursor
return TRUE; // Prevent Windows from setting cursor to window class cursor
}
else
{
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_SIZE:
InvalidateRect (Window, NULL, FALSE);
break;
case WM_KEYDOWN:
break;
case WM_SYSKEYDOWN:
// Pressing Alt+Enter can toggle between fullscreen and windowed.
if (wParam == VK_RETURN && k_allowfullscreentoggle && !(lParam & 0x40000000))
{
ToggleFullscreen = !ToggleFullscreen;
}
// Pressing Alt+F4 quits the program.
if (wParam == VK_F4 && !(lParam & 0x40000000))
{
PostQuitMessage(0);
}
break;
case WM_SYSCOMMAND:
// Prevent activation of the window menu with Alt+Space
if ((wParam & 0xFFF0) != SC_KEYMENU)
{
return DefWindowProc (hWnd, message, wParam, lParam);
}
break;
case WM_DISPLAYCHANGE:
case WM_STYLECHANGED:
return DefWindowProc(hWnd, message, wParam, lParam);
case WM_GETMINMAXINFO:
if (screen && !VidResizing)
{
LPMINMAXINFO mmi = (LPMINMAXINFO)lParam;
if (screen->IsFullscreen())
{
RECT rect = { 0, 0, screen->GetWidth(), screen->GetHeight() };
AdjustWindowRectEx(&rect, WS_VISIBLE | WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW);
mmi->ptMinTrackSize.x = rect.right - rect.left;
mmi->ptMinTrackSize.y = rect.bottom - rect.top;
}
else
{
RECT rect = { 0, 0, VID_MIN_WIDTH, VID_MIN_HEIGHT };
AdjustWindowRectEx(&rect, GetWindowLongW(hWnd, GWL_STYLE), FALSE, GetWindowLongW(hWnd, GWL_EXSTYLE));
mmi->ptMinTrackSize.x = rect.right - rect.left;
mmi->ptMinTrackSize.y = rect.bottom - rect.top;
}
return 0;
}
break;
case WM_ACTIVATEAPP:
AppActive = wParam == TRUE;
if (wParam)
{
SetPriorityClass (GetCurrentProcess (), INGAME_PRIORITY_CLASS);
}
else if (!noidle && !(sysCallbacks.NetGame && sysCallbacks.NetGame()))
{
SetPriorityClass (GetCurrentProcess (), IDLE_PRIORITY_CLASS);
}
S_SetSoundPaused (wParam);
break;
case WM_WTSSESSION_CHANGE:
case WM_POWERBROADCAST:
{
if (message == WM_WTSSESSION_CHANGE && lParam == (LPARAM)SessionID)
{
#ifdef _DEBUG
OutputDebugStringA ("SessionID matched\n");
#endif
// When using fast user switching, XP will lock a session before
// disconnecting it, and the session will be unlocked before reconnecting it.
// For our purposes, video output will only happen when the session is
// both unlocked and connected (that is, SessionState is 0).
switch (wParam)
{
case WTS_SESSION_LOCK:
SessionState |= 1;
break;
case WTS_SESSION_UNLOCK:
SessionState &= ~1;
break;
case WTS_CONSOLE_DISCONNECT:
SessionState |= 2;
break;
case WTS_CONSOLE_CONNECT:
SessionState &= ~2;
break;
}
}
else if (message == WM_POWERBROADCAST)
{
switch (wParam)
{
case PBT_APMSUSPEND:
SessionState |= 4;
break;
case PBT_APMRESUMESUSPEND:
SessionState &= ~4;
break;
}
}
if (GSnd != NULL)
{
#if 0
// Do we actually need this here?
if (!oldstate && SessionState)
{
GSnd->SuspendSound ();
}
#endif
}
#ifdef _DEBUG
char foo[256];
mysnprintf (foo, countof(foo), "Session Change: %ld %d\n", (long)lParam, (int)wParam);
OutputDebugStringA (foo);
#endif
}
break;
case WM_ERASEBKGND:
return true;
case WM_DEVICECHANGE:
if (wParam == DBT_DEVNODES_CHANGED ||
wParam == DBT_DEVICEARRIVAL ||
wParam == DBT_CONFIGCHANGED)
{
event_t ev = { EV_DeviceChange };
D_PostEvent(&ev);
}
return DefWindowProc (hWnd, message, wParam, lParam);
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
return 0;
}
bool I_InitInput (void *hwnd)
{
HRESULT hr;
Printf ("I_InitInput\n");
noidle = !!Args->CheckParm ("-noidle");
g_pdi = NULL;
g_pdi3 = NULL;
// Try for DirectInput 8 first, then DirectInput 3 for NT 4's benefit.
DInputDLL = LoadLibraryA("dinput8.dll");
if (DInputDLL != NULL)
{
typedef HRESULT (WINAPI *blah)(HINSTANCE, DWORD, REFIID, LPVOID *, LPUNKNOWN);
blah di8c = (blah)GetProcAddress(DInputDLL, "DirectInput8Create");
if (di8c != NULL)
{
hr = di8c(g_hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&g_pdi, NULL);
if (FAILED(hr))
{
Printf(TEXTCOLOR_ORANGE "DirectInput8Create failed: %08lx\n", hr);
g_pdi = NULL; // Just to be sure DirectInput8Create didn't change it
}
}
else
{
Printf(TEXTCOLOR_ORANGE "Could not find DirectInput8Create in dinput8.dll\n");
}
}
if (g_pdi == NULL)
{
if (DInputDLL != NULL)
{
FreeLibrary(DInputDLL);
}
DInputDLL = LoadLibraryA ("dinput.dll");
if (DInputDLL == NULL)
{
I_FatalError ("Could not load dinput.dll: %08lx", GetLastError());
}
typedef HRESULT (WINAPI *blah)(HINSTANCE, DWORD, LPDIRECTINPUT*, LPUNKNOWN);
#ifdef UNICODE
blah dic = (blah)GetProcAddress (DInputDLL, "DirectInputCreateW");
#else
blah dic = (blah)GetProcAddress(DInputDLL, "DirectInputCreateA");
#endif
if (dic == NULL)
{
I_FatalError ("dinput.dll is corrupt");
}
hr = dic (g_hInst, 0x0300, &g_pdi3, NULL);
if (FAILED(hr))
{
I_FatalError ("DirectInputCreate failed: %08lx", hr);
}
}
Printf ("I_StartupMouse\n");
I_StartupMouse();
Printf ("I_StartupKeyboard\n");
I_StartupKeyboard();
Printf ("I_StartupXInput\n");
I_StartupXInput();
Printf ("I_StartupRawPS2\n");
I_StartupRawPS2();
Printf ("I_StartupDirectInputJoystick\n");
I_StartupDirectInputJoystick();
return TRUE;
}
// Free all input resources
void I_ShutdownInput ()
{
if (Keyboard != NULL)
{
delete Keyboard;
Keyboard = NULL;
}
if (Mouse != NULL)
{
delete Mouse;
Mouse = NULL;
}
for (int i = 0; i < NUM_JOYDEVICES; ++i)
{
if (JoyDevices[i] != NULL)
{
delete JoyDevices[i];
JoyDevices[i] = NULL;
}
}
if (g_pdi)
{
g_pdi->Release ();
g_pdi = NULL;
}
if (g_pdi3)
{
g_pdi3->Release ();
g_pdi3 = NULL;
}
if (DInputDLL != NULL)
{
FreeLibrary (DInputDLL);
DInputDLL = NULL;
}
}
void I_GetEvent ()
{
MSG mess;
// Briefly enter an alertable state so that if a secondary thread
// crashed, we will execute the APC it sent now.
SleepEx (0, TRUE);
while (PeekMessage (&mess, NULL, 0, 0, PM_REMOVE))
{
if (mess.message == WM_QUIT)
throw CExitEvent(mess.wParam);
if (GUICapture)
{
TranslateMessage (&mess);
}
DispatchMessage (&mess);
}
if (Keyboard != NULL)
{
Keyboard->ProcessInput();
}
if (Mouse != NULL)
{
Mouse->ProcessInput();
}
}
//
// I_StartTic
//
void I_StartTic ()
{
BlockMouseMove--;
buttonMap.ResetButtonTriggers ();
I_CheckGUICapture ();
EventHandlerResultForNativeMouse = sysCallbacks.WantNativeMouse && sysCallbacks.WantNativeMouse();
I_CheckNativeMouse (false, EventHandlerResultForNativeMouse);
I_GetEvent ();
}
//
// I_StartFrame
//
void I_StartFrame ()
{
if (use_joystick)
{
for (int i = 0; i < NUM_JOYDEVICES; ++i)
{
if (JoyDevices[i] != NULL)
{
JoyDevices[i]->ProcessInput();
}
}
}
}
void I_GetAxes(float axes[NUM_JOYAXIS])
{
int i;
for (i = 0; i < NUM_JOYAXIS; ++i)
{
axes[i] = 0;
}
if (use_joystick)
{
for (i = 0; i < NUM_JOYDEVICES; ++i)
{
if (JoyDevices[i] != NULL)
{
JoyDevices[i]->AddAxes(axes);
}
}
}
}
void I_GetJoysticks(TArray<IJoystickConfig *> &sticks)
{
sticks.Clear();
for (int i = 0; i < NUM_JOYDEVICES; ++i)
{
if (JoyDevices[i] != NULL)
{
JoyDevices[i]->GetDevices(sticks);
}
}
}
// If a new controller was added, returns a pointer to it.
IJoystickConfig *I_UpdateDeviceList()
{
IJoystickConfig *newone = NULL;
for (int i = 0; i < NUM_JOYDEVICES; ++i)
{
if (JoyDevices[i] != NULL)
{
IJoystickConfig *thisnewone = JoyDevices[i]->Rescan();
if (newone == NULL)
{
newone = thisnewone;
}
}
}
return newone;
}
void I_PutInClipboard (const char *str)
{
if (str == NULL || !OpenClipboard (Window))
return;
EmptyClipboard ();
auto wstr = WideString(str);
HGLOBAL cliphandle = GlobalAlloc (GMEM_DDESHARE, wstr.length() * 2 + 2);
if (cliphandle != NULL)
{
wchar_t *ptr = (wchar_t *)GlobalLock (cliphandle);
wcscpy (ptr, wstr.c_str());
GlobalUnlock (cliphandle);
SetClipboardData (CF_UNICODETEXT, cliphandle);
}
CloseClipboard ();
}
FString I_GetFromClipboard (bool return_nothing)
{
FString retstr;
HGLOBAL cliphandle;
wchar_t *clipstr;
if (return_nothing || !IsClipboardFormatAvailable (CF_UNICODETEXT) || !OpenClipboard (Window))
return retstr;
cliphandle = GetClipboardData (CF_UNICODETEXT);
if (cliphandle != nullptr)
{
clipstr = (wchar_t *)GlobalLock (cliphandle);
if (clipstr != nullptr)
{
// Convert CR-LF pairs to just LF.
retstr = clipstr;
GlobalUnlock(clipstr);
retstr.Substitute("\r\n", "\n");
}
}
CloseClipboard ();
return retstr;
}
//==========================================================================
//
// FInputDevice - Destructor
//
//==========================================================================
FInputDevice::~FInputDevice()
{
}
//==========================================================================
//
// FInputDevice :: ProcessInput
//
// Gives subclasses an opportunity to do input handling that doesn't involve
// window messages.
//
//==========================================================================
void FInputDevice::ProcessInput()
{
}
//==========================================================================
//
// FInputDevice :: ProcessRawInput
//
// Gives subclasses a chance to handle WM_INPUT messages. This is not part
// of WndProcHook so that we only need to fill the RAWINPUT buffer once
// per message and be sure it gets cleaned up properly.
//
//==========================================================================
bool FInputDevice::ProcessRawInput(RAWINPUT *raw, int code)
{
return false;
}
//==========================================================================
//
// FInputDevice :: WndProcHook
//
// Gives subclasses a chance to intercept window messages.
//
//==========================================================================
bool FInputDevice::WndProcHook(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result)
{
return false;
}