Skip to content

Commit f10ef08

Browse files
committed
Changed signature to support multiple instances.
1 parent 0ba775d commit f10ef08

5 files changed

Lines changed: 126 additions & 107 deletions

File tree

‎Core/CoreFunctionsManager.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,19 @@ public bool DisassembleCode(IntPtr address, int length, IntPtr virtualAddress, o
212212
return internalCoreFunctions.DisassembleCode(address, length, virtualAddress, out instruction);
213213
}
214214

215-
public bool InitializeInput()
215+
public IntPtr InitializeInput()
216216
{
217217
return internalCoreFunctions.InitializeInput();
218218
}
219219

220-
public Keys[] GetPressedKeys()
220+
public Keys[] GetPressedKeys(IntPtr handle)
221221
{
222-
return internalCoreFunctions.GetPressedKeys();
222+
return internalCoreFunctions.GetPressedKeys(handle);
223223
}
224224

225-
public void ReleaseInput()
225+
public void ReleaseInput(IntPtr handle)
226226
{
227-
internalCoreFunctions.ReleaseInput();
227+
internalCoreFunctions.ReleaseInput(handle);
228228
}
229229

230230
#endregion

‎Core/InternalCoreFunctions.cs‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ internal class InternalCoreFunctions : NativeCoreWrapper
99
[return: MarshalAs(UnmanagedType.I1)]
1010
private delegate bool DisassembleCodeDelegate(IntPtr address, IntPtr length, IntPtr virtualAddress, out InstructionData instruction);
1111

12-
[return: MarshalAs(UnmanagedType.I1)]
13-
private delegate bool InitializeInputDelegate();
12+
private delegate IntPtr InitializeInputDelegate();
1413

15-
[return: MarshalAs(UnmanagedType.I1)]
16-
private delegate bool GetPressedKeysDelegate(out IntPtr address, out int length);
14+
private delegate bool GetPressedKeysDelegate(IntPtr handle, out IntPtr pressedKeysArrayPtr, out int length);
1715

18-
private delegate void ReleaseInputDelegate();
16+
private delegate void ReleaseInputDelegate(IntPtr handle);
1917

2018
private readonly DisassembleCodeDelegate disassembleCodeDelegate;
2119

@@ -38,16 +36,16 @@ public bool DisassembleCode(IntPtr address, int length, IntPtr virtualAddress, o
3836
return disassembleCodeDelegate(address, (IntPtr)length, virtualAddress, out instruction);
3937
}
4038

41-
public bool InitializeInput()
39+
public IntPtr InitializeInput()
4240
{
4341
return initializeInputDelegate();
4442
}
4543

4644
private static readonly Keys[] empty = new Keys[0];
4745

48-
public Keys[] GetPressedKeys()
46+
public Keys[] GetPressedKeys(IntPtr handle)
4947
{
50-
if (!getPressedKeysDelegate(out var buffer, out var length) || length == 0)
48+
if (!getPressedKeysDelegate(handle, out var buffer, out var length) || length == 0)
5149
{
5250
return empty;
5351
}
@@ -58,9 +56,9 @@ public Keys[] GetPressedKeys()
5856
//return Array.ConvertAll(keys, k => (Keys)k);
5957
}
6058

61-
public void ReleaseInput()
59+
public void ReleaseInput(IntPtr handle)
6260
{
63-
releaseInputDelegate();
61+
releaseInputDelegate(handle);
6462
}
6563
}
6664
}

‎NativeCore/Unix/Input.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
void ReleaseInput();
55

6-
bool InitializeInput()
6+
RC_Pointer InitializeInput()
77
{
8-
return false;
8+
return nullptr;
99
}
1010

11-
bool GetPressedKeys(Keys* state[], int* count)
11+
bool GetPressedKeys(RC_Pointer handle, Keys* state[], int* count)
1212
{
1313
return false;
1414
}
1515

16-
void ReleaseInput()
16+
void ReleaseInput(RC_Pointer handle)
1717
{
1818

1919
}

‎NativeCore/Unix/NativeCore.hpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ extern "C"
3131
void HandleDebugEvent(DebugEvent* evt);
3232
bool SetHardwareBreakpoint(RC_Pointer id, RC_Pointer address, HardwareBreakpointRegister reg, HardwareBreakpointTrigger type, HardwareBreakpointSize size, bool set);
3333

34-
bool InitializeInput();
35-
bool GetPressedKeys(Keys* state[], int* count);
36-
void ReleaseInput();
34+
RC_Pointer InitializeInput();
35+
bool GetPressedKeys(RC_Pointer handle, Keys* state[], int* count);
36+
void ReleaseInput(RC_Pointer handle);
3737
}
3838

3939
inline bool is_number(const std::string& s)

‎NativeCore/Windows/Input.cpp‎

Lines changed: 106 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,120 +4,141 @@
44
#include "NativeCore.hpp"
55
#include "../Keys.hpp"
66

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-
147
Keys mapping[];
15-
std::vector<Keys> currentState(16);
168

17-
void __stdcall ReleaseInput();
18-
19-
bool __stdcall InitializeInput()
9+
class DirectInput
2010
{
21-
if (DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8W, reinterpret_cast<void**>(&directInputInterface), nullptr) != DI_OK)
11+
public:
12+
~DirectInput()
2213
{
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+
}
2425
}
2526

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()
2928
{
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+
}
3140

32-
return false;
41+
return true;
3342
}
3443

35-
return true;
36-
}
44+
bool GetPressedKeys(Keys* state[], int* count)
45+
{
46+
const int STATE_PRESSED = 0x80;
3747

38-
bool __stdcall GetPressedKeys(Keys* state[], int* count)
39-
{
40-
currentState.clear();
48+
currentState.clear();
4149

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)
4753
{
48-
keyboardDevice->Acquire();
54+
if (result == DIERR_NOTACQUIRED || result == DIERR_INPUTLOST)
55+
{
56+
keyboardDevice->Acquire();
57+
}
58+
return false;
4959
}
50-
return false;
51-
}
5260

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+
}
7179

72-
for (auto i = 0u; i < 0xEF; ++i)
73-
{
74-
if (keyBuffer[i] & STATE_PRESSED)
80+
for (auto i = 0u; i < 0xEF; ++i)
7581
{
76-
auto currentKey = mapping[i];
77-
if (currentKey != Keys::None)
82+
if (keyBuffer[i] & STATE_PRESSED)
7883
{
79-
switch (currentKey)
84+
auto currentKey = mapping[i];
85+
if (currentKey != Keys::None)
8086
{
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+
}
94102

95-
currentKey |= modifier;
103+
currentKey |= modifier;
96104

97-
currentState.push_back(currentKey);
105+
currentState.push_back(currentKey);
106+
}
98107
}
99108
}
100-
}
101109

102-
*state = currentState.data();
103-
*count = currentState.size();
110+
*state = currentState.data();
111+
*count = currentState.size();
104112

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+
};
107121

108-
void __stdcall ReleaseInput()
122+
RC_Pointer __stdcall InitializeInput()
109123
{
110-
if (keyboardDevice)
124+
auto input = new DirectInput();
125+
if (!input->Initialize())
111126
{
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;
120130
}
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);
121142
}
122143

123144
Keys mapping[] =

0 commit comments

Comments
 (0)