|
4 | 4 | #include "NativeCore.hpp" |
5 | 5 | #include "../Keys.hpp" |
6 | 6 |
|
7 | | -const int STATE_PRESSED = 0x80; |
8 | | -const int STATE_NOT_PRESSED = 0; |
9 | | -const int STATE_ON = 0x1; |
10 | | - |
11 | | -IDirectInput8W* directInputInterface = nullptr; |
12 | | -IDirectInputDevice8W* keyboardDevice = nullptr; |
13 | | - |
14 | 7 | Keys mapping[]; |
15 | | -std::vector<Keys> currentState(16); |
16 | 8 |
|
17 | | -void __stdcall ReleaseInput(); |
18 | | - |
19 | | -bool __stdcall InitializeInput() |
| 9 | +class DirectInput |
20 | 10 | { |
21 | | - if (DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8W, reinterpret_cast<void**>(&directInputInterface), nullptr) != DI_OK) |
| 11 | +public: |
| 12 | + ~DirectInput() |
22 | 13 | { |
23 | | - return false; |
| 14 | + if (keyboardDevice) |
| 15 | + { |
| 16 | + keyboardDevice->Unacquire(); |
| 17 | + keyboardDevice->Release(); |
| 18 | + keyboardDevice = nullptr; |
| 19 | + } |
| 20 | + if (directInputInterface) |
| 21 | + { |
| 22 | + directInputInterface->Release(); |
| 23 | + directInputInterface = nullptr; |
| 24 | + } |
24 | 25 | } |
25 | 26 |
|
26 | | - if (directInputInterface->CreateDevice(GUID_SysKeyboard, &keyboardDevice, nullptr) != DI_OK |
27 | | - || keyboardDevice->SetDataFormat(&c_dfDIKeyboard) != DI_OK |
28 | | - /*|| keyboardDevice->SetCooperativeLevel(target, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE) != DI_OK*/) |
| 27 | + bool Initialize() |
29 | 28 | { |
30 | | - ReleaseInput(); |
| 29 | + if (DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8W, reinterpret_cast<void**>(&directInputInterface), nullptr) != DI_OK) |
| 30 | + { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + if (directInputInterface->CreateDevice(GUID_SysKeyboard, &keyboardDevice, nullptr) != DI_OK |
| 35 | + || keyboardDevice->SetDataFormat(&c_dfDIKeyboard) != DI_OK |
| 36 | + /*|| keyboardDevice->SetCooperativeLevel(target, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE) != DI_OK*/) |
| 37 | + { |
| 38 | + return false; |
| 39 | + } |
31 | 40 |
|
32 | | - return false; |
| 41 | + return true; |
33 | 42 | } |
34 | 43 |
|
35 | | - return true; |
36 | | -} |
| 44 | + bool GetPressedKeys(Keys* state[], int* count) |
| 45 | + { |
| 46 | + const int STATE_PRESSED = 0x80; |
37 | 47 |
|
38 | | -bool __stdcall GetPressedKeys(Keys* state[], int* count) |
39 | | -{ |
40 | | - currentState.clear(); |
| 48 | + currentState.clear(); |
41 | 49 |
|
42 | | - BYTE keyBuffer[256] = {}; |
43 | | - const auto result = keyboardDevice->GetDeviceState(sizeof(keyBuffer), &keyBuffer); |
44 | | - if (result != DI_OK) |
45 | | - { |
46 | | - if (result == DIERR_NOTACQUIRED || result == DIERR_INPUTLOST) |
| 50 | + BYTE keyBuffer[256] = {}; |
| 51 | + const auto result = keyboardDevice->GetDeviceState(sizeof(keyBuffer), &keyBuffer); |
| 52 | + if (result != DI_OK) |
47 | 53 | { |
48 | | - keyboardDevice->Acquire(); |
| 54 | + if (result == DIERR_NOTACQUIRED || result == DIERR_INPUTLOST) |
| 55 | + { |
| 56 | + keyboardDevice->Acquire(); |
| 57 | + } |
| 58 | + return false; |
49 | 59 | } |
50 | | - return false; |
51 | | - } |
52 | 60 |
|
53 | | - auto modifier = Keys::None; |
54 | | - if (keyBuffer[DIK_LSHIFT] & STATE_PRESSED || keyBuffer[DIK_RSHIFT] & STATE_PRESSED) |
55 | | - { |
56 | | - modifier |= Keys::Shift; |
57 | | - } |
58 | | - if (keyBuffer[DIK_LCONTROL] & STATE_PRESSED || keyBuffer[DIK_RCONTROL] & STATE_PRESSED) |
59 | | - { |
60 | | - modifier |= Keys::Control; |
61 | | - } |
62 | | - if (keyBuffer[DIK_LMENU] & STATE_PRESSED) |
63 | | - { |
64 | | - modifier |= Keys::Alt; |
65 | | - } |
66 | | - if (keyBuffer[DIK_RMENU] & STATE_PRESSED) |
67 | | - { |
68 | | - modifier |= Keys::Alt; |
69 | | - modifier |= Keys::Control; |
70 | | - } |
| 61 | + auto modifier = Keys::None; |
| 62 | + if (keyBuffer[DIK_LSHIFT] & STATE_PRESSED || keyBuffer[DIK_RSHIFT] & STATE_PRESSED) |
| 63 | + { |
| 64 | + modifier |= Keys::Shift; |
| 65 | + } |
| 66 | + if (keyBuffer[DIK_LCONTROL] & STATE_PRESSED || keyBuffer[DIK_RCONTROL] & STATE_PRESSED) |
| 67 | + { |
| 68 | + modifier |= Keys::Control; |
| 69 | + } |
| 70 | + if (keyBuffer[DIK_LMENU] & STATE_PRESSED) |
| 71 | + { |
| 72 | + modifier |= Keys::Alt; |
| 73 | + } |
| 74 | + if (keyBuffer[DIK_RMENU] & STATE_PRESSED) |
| 75 | + { |
| 76 | + modifier |= Keys::Alt; |
| 77 | + modifier |= Keys::Control; |
| 78 | + } |
71 | 79 |
|
72 | | - for (auto i = 0u; i < 0xEF; ++i) |
73 | | - { |
74 | | - if (keyBuffer[i] & STATE_PRESSED) |
| 80 | + for (auto i = 0u; i < 0xEF; ++i) |
75 | 81 | { |
76 | | - auto currentKey = mapping[i]; |
77 | | - if (currentKey != Keys::None) |
| 82 | + if (keyBuffer[i] & STATE_PRESSED) |
78 | 83 | { |
79 | | - switch (currentKey) |
| 84 | + auto currentKey = mapping[i]; |
| 85 | + if (currentKey != Keys::None) |
80 | 86 | { |
81 | | - case Keys::LControlKey: |
82 | | - case Keys::RControlKey: |
83 | | - currentKey = Keys::ControlKey; |
84 | | - break; |
85 | | - case Keys::LShiftKey: |
86 | | - case Keys::RShiftKey: |
87 | | - currentKey = Keys::ControlKey; |
88 | | - break; |
89 | | - case Keys::LMenu: |
90 | | - case Keys::RMenu: |
91 | | - currentKey = Keys::Menu; |
92 | | - break; |
93 | | - } |
| 87 | + switch (currentKey) |
| 88 | + { |
| 89 | + case Keys::LControlKey: |
| 90 | + case Keys::RControlKey: |
| 91 | + currentKey = Keys::ControlKey; |
| 92 | + break; |
| 93 | + case Keys::LShiftKey: |
| 94 | + case Keys::RShiftKey: |
| 95 | + currentKey = Keys::ControlKey; |
| 96 | + break; |
| 97 | + case Keys::LMenu: |
| 98 | + case Keys::RMenu: |
| 99 | + currentKey = Keys::Menu; |
| 100 | + break; |
| 101 | + } |
94 | 102 |
|
95 | | - currentKey |= modifier; |
| 103 | + currentKey |= modifier; |
96 | 104 |
|
97 | | - currentState.push_back(currentKey); |
| 105 | + currentState.push_back(currentKey); |
| 106 | + } |
98 | 107 | } |
99 | 108 | } |
100 | | - } |
101 | 109 |
|
102 | | - *state = currentState.data(); |
103 | | - *count = currentState.size(); |
| 110 | + *state = currentState.data(); |
| 111 | + *count = currentState.size(); |
104 | 112 |
|
105 | | - return true; |
106 | | -} |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | +private: |
| 117 | + IDirectInput8W* directInputInterface = nullptr; |
| 118 | + IDirectInputDevice8W* keyboardDevice = nullptr; |
| 119 | + std::vector<Keys> currentState; |
| 120 | +}; |
107 | 121 |
|
108 | | -void __stdcall ReleaseInput() |
| 122 | +RC_Pointer __stdcall InitializeInput() |
109 | 123 | { |
110 | | - if (keyboardDevice) |
| 124 | + auto input = new DirectInput(); |
| 125 | + if (!input->Initialize()) |
111 | 126 | { |
112 | | - keyboardDevice->Unacquire(); |
113 | | - keyboardDevice->Release(); |
114 | | - keyboardDevice = nullptr; |
115 | | - } |
116 | | - if (directInputInterface) |
117 | | - { |
118 | | - directInputInterface->Release(); |
119 | | - directInputInterface = nullptr; |
| 127 | + delete input; |
| 128 | + |
| 129 | + return nullptr; |
120 | 130 | } |
| 131 | + return static_cast<RC_Pointer>(input); |
| 132 | +} |
| 133 | + |
| 134 | +bool __stdcall GetPressedKeys(RC_Pointer handle, Keys* state[], int* count) |
| 135 | +{ |
| 136 | + return static_cast<DirectInput*>(handle)->GetPressedKeys(state, count); |
| 137 | +} |
| 138 | + |
| 139 | +void __stdcall ReleaseInput(RC_Pointer handle) |
| 140 | +{ |
| 141 | + delete static_cast<DirectInput*>(handle); |
121 | 142 | } |
122 | 143 |
|
123 | 144 | Keys mapping[] = |
|
0 commit comments