forked from KoKuToru/koku-xinput-wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.cpp
208 lines (170 loc) · 5.92 KB
/
device.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
#include "device.h"
#include "main.h"
#include <cstring>
#include <new>
#include <string>
#include <iostream>
using namespace std;
std::string check_bstrNamespace = "\\\\.\\root\\cimv2";
std::string check_bstrClassName = "Win32_PNPEntity";
std::string check_bstrDeviceID = "DeviceID";
const short wine_gamepad[] = {'V','I','D','_','3','E','D','9','&',
'P','I','D','_','9','E','5','7','&',
'I','G','_','0','0'}; //you can get VID/PID from wine-source
//what this does ? well.. hard to explain, please don't mind as long as it works
t_ptr CoSetProxyBlanket_addr = 0;
char CoSetProxyBlanket_hook[sizeof(Sjmp)];
t_ptr CreateInstanceEnum_addr = 0;
t_ptr CreateInstanceEnum_original;
t_ptr Next_addr = 0;
t_ptr Next_original;
void DeviceInit(void* handle)
{
if (debug)
{
clog << "koku-xinput-wine: search for `CoSetProxyBlanket`";
}
//hook functions
t_ptr addr = t_ptr(dlsym(handle, "CoSetProxyBlanket"));
if (addr != 0)
{
if (debug)
{
clog << ", found, redirect it";
}
//backup data
CoSetProxyBlanket_addr = addr;
memcpy(CoSetProxyBlanket_hook, (void*)CoSetProxyBlanket_addr, sizeof(Sjmp));
t_ptr addr_start = (addr - PAGESIZE-1) & ~(PAGESIZE-1);
t_ptr addr_end = (addr + PAGESIZE-1) & ~(PAGESIZE-1);
mprotect((void*)addr_start, addr_end-addr_start , PROT_READ|PROT_WRITE|PROT_EXEC);
new ((void*)addr) Sjmp((void*)CoSetProxyBlanket);
}
if (debug)
{
clog << endl;
}
}
void* WINAPI CoSetProxyBlanket(void* pProxy, unsigned dwAuthnSvc, unsigned dwAuthzSvc, void* pServerPrincName, unsigned dwAuthnLevel, unsigned dwImpLevel, void* pAuthInfo, unsigned dwCapabilities)
{
if (debug)
{
clog << "koku-xinput-wine: CoSetProxyBlanket(...);" << endl;
}
//disable hook
memcpy((void*)CoSetProxyBlanket_addr, CoSetProxyBlanket_hook, sizeof(Sjmp));
//call original
void* result = ((decltype(&CoSetProxyBlanket))CoSetProxyBlanket_addr)(pProxy, dwAuthnSvc, dwAuthzSvc, pServerPrincName, dwAuthnLevel, dwImpLevel, pAuthInfo, dwCapabilities);
//enable hook
new ((void*)CoSetProxyBlanket_addr) Sjmp((void*)CoSetProxyBlanket);
//overwrite the function-table that CreateInstanceEnum goes to our function
t_ptr pProxy_func = *((t_ptr*)pProxy);
t_ptr pProxy_func_createinstanceenum = pProxy_func+0x48;
t_ptr addr_start = (pProxy_func_createinstanceenum - PAGESIZE-1) & ~(PAGESIZE-1);
t_ptr addr_end = (pProxy_func_createinstanceenum + PAGESIZE-1) & ~(PAGESIZE-1);
mprotect((void*)addr_start, addr_end-addr_start , PROT_READ|PROT_WRITE|PROT_EXEC);
if (*((void**)(pProxy_func_createinstanceenum)) != (void*)CreateInstanceEnum)
{
CreateInstanceEnum_original = *((t_ptr*)(pProxy_func_createinstanceenum));
CreateInstanceEnum_addr = pProxy_func_createinstanceenum;
}
*((void**)(pProxy_func_createinstanceenum)) = (void*)CreateInstanceEnum;
return result;
}
void* WINAPI CreateInstanceEnum(void* pIWbemServices, short* bstrClassName, unsigned null1, void* null2, void* pEnumDevices)
{
//check, uhm i have no idea how to work with unicode..
string bstrClassName_s;
for(int i = 0; bstrClassName[i] != 0; ++i)
{
bstrClassName_s += bstrClassName[i];
}
if (debug)
{
clog << "koku-xinput-wine: CreateInstanceEnum(..., \"" << bstrClassName_s << "\", ...);" << endl;
}
//call original
void* result = ((decltype(&CreateInstanceEnum))CreateInstanceEnum_original)(pIWbemServices, bstrClassName, null1, null2, pEnumDevices);
if (bstrClassName_s != check_bstrClassName)
{
return result;
}
//overwrite the function-table that Next goes to our function
t_ptr pEnumDevices_func = **((t_ptr**)pEnumDevices);
t_ptr pEnumDevices_func_next = pEnumDevices_func+0x10;
t_ptr addr_start = (pEnumDevices_func_next - PAGESIZE-1) & ~(PAGESIZE-1);
t_ptr addr_end = (pEnumDevices_func_next + PAGESIZE-1) & ~(PAGESIZE-1);
mprotect((void*)addr_start, addr_end-addr_start , PROT_READ|PROT_WRITE|PROT_EXEC);
if (*((void**)(pEnumDevices_func_next)) != (void*)EnumDevices_Next)
{
Next_addr = pEnumDevices_func_next;
Next_original = *((t_ptr*)(pEnumDevices_func_next));
}
*((void**)(pEnumDevices_func_next)) = (void*)EnumDevices_Next;
return result;
}
void* WINAPI EnumDevices_Next(void* pEnumDevices, unsigned a, unsigned b, void** pDevices, unsigned* uReturned)
{
if (debug)
{
clog << "koku-xinput-wine: EnumDevices_Next(...);" << endl;
}
//call original
void* result = ((decltype(&EnumDevices_Next))Next_original)(pEnumDevices, a, b, pDevices, uReturned);
if (*uReturned == 0)
{
if (debug)
{
clog << "koku-xinput-wine: Return own custom-data;" << endl;
}
//restore original
*((void**)(Next_addr)) = (void*)Next_original;
//end reach add our own stuff ;)
*uReturned = 1;
//Very ugly stuff will happen now:
*pDevices = (void*)(new char[1024]);
*((void**)*pDevices) = (void*)(t_ptr(*pDevices)+1);
t_ptr pDevices_func = **((t_ptr**)pDevices);
t_ptr pDevices_func_get = pDevices_func+0x10;
t_ptr addr_start = (pDevices_func_get - PAGESIZE-1) & ~(PAGESIZE-1);
t_ptr addr_end = (pDevices_func_get + PAGESIZE-1) & ~(PAGESIZE-1);
mprotect((void*)addr_start, addr_end-addr_start , PROT_READ|PROT_WRITE|PROT_EXEC);
*((void**)(pDevices_func_get)) = (void*)Devices_Get;
pDevices_func_get = pDevices_func+0x08;
*((void**)(pDevices_func_get)) = (void*)Devices_Release;
}
return result;
}
bool WINAPI Devices_Get(void* pDevices, short* wszName, unsigned lFlags, VARIANT* pVal, void* o1, void* o2)
{
//check, uhm i have no idea how to work with unicode..
string wszName_s;
for(int i = 0; wszName[i] != 0; ++i)
{
wszName_s += wszName[i];
}
if (debug)
{
clog << "koku-xinput-wine: Devices_Get(..., \""<< wszName_s << "\");" << endl;
}
if (check_bstrDeviceID != wszName_s)
{
//uhm nothing
return 1; //not ERROR_SUCCESS
}
if (debug)
{
clog << "koku-xinput-wine: return wine-gamepad" << endl;
}
pVal->vt = /*VT_BSTR*/8;
pVal->STR = wine_gamepad;
return 0;
}
void WINAPI Devices_Release(void* pDevices)
{
if (debug)
{
clog << "koku-xinput-wine: Devices_Release(...);" << endl;
}
delete[] (char*)pDevices;
}