forked from hifi/cnc-ddraw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouse.c
256 lines (217 loc) · 6.98 KB
/
mouse.c
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
/*
* Copyright (c) 2010 Toni Spets <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* This is a special mouse coordinate fix for games that use GetCursorPos and expect to be in fullscreen */
#include <windows.h>
#include <stdio.h>
#include "main.h"
#include "surface.h"
#define MAX_HOOKS 16
BOOL mouse_active = FALSE;
int real_height = 0;
struct hook { char name[32]; void *func; };
struct hack
{
char name[32];
struct hook hooks[MAX_HOOKS];
};
BOOL WINAPI fake_GetCursorPos(LPPOINT lpPoint)
{
POINT pt;
if(mouse_active && ddraw->locked)
{
GetCursorPos(&pt);
if(ddraw->sensitivity > 0 && ddraw->sensitivity < 10)
{
ddraw->cursor.x += (pt.x - ddraw->center.x) * ddraw->sensitivity;
ddraw->cursor.y += (pt.y - ddraw->center.y) * ddraw->sensitivity;
}
else if(ddraw->adjmouse)
{
ddraw->cursor.x += (pt.x - ddraw->center.x) * ((float)ddraw->width / ddraw->render.width);
ddraw->cursor.y += (pt.y - ddraw->center.y) * ((float)ddraw->height / ddraw->render.height);
}
else
{
ddraw->cursor.x += pt.x - ddraw->center.x;
ddraw->cursor.y += pt.y - ddraw->center.y;
}
if(ddraw->cursor.x < 0) ddraw->cursor.x = 0;
if(ddraw->cursor.y < 0) ddraw->cursor.y = 0;
if(ddraw->cursor.x > ddraw->cursorclip.width-1) ddraw->cursor.x = ddraw->cursorclip.width-1;
if(real_height > 0 && real_height < ddraw->cursorclip.height)
{
if(ddraw->cursor.y > real_height-1) ddraw->cursor.y = real_height-1;
}
else
{
if(ddraw->cursor.y > ddraw->cursorclip.height-1) ddraw->cursor.y = ddraw->cursorclip.height-1;
}
if(pt.x != ddraw->center.x || pt.y != ddraw->center.y)
{
SetCursorPos(ddraw->center.x, ddraw->center.y);
}
}
if (lpPoint)
{
lpPoint->x = (int)ddraw->cursor.x;
lpPoint->y = (int)ddraw->cursor.y;
}
return TRUE;
}
BOOL WINAPI fake_ClipCursor(const RECT *lpRect)
{
if(lpRect)
{
/* hack for 640x480 mode */
real_height = lpRect->bottom;
}
return TRUE;
}
int WINAPI fake_ShowCursor(BOOL bShow)
{
return TRUE;
}
HCURSOR WINAPI fake_SetCursor(HCURSOR hCursor)
{
return NULL;
}
struct hack hacks[] =
{
{
"user32.dll",
{
{ "GetCursorPos", fake_GetCursorPos },
{ "ClipCursor", fake_ClipCursor },
{ "ShowCursor", fake_ShowCursor },
{ "SetCursor", fake_SetCursor } ,
{ "", NULL }
}
},
{
"",
{
{ "", NULL }
}
}
};
void hack_iat(struct hack *hck)
{
int i;
char buf[32];
struct hook *hk;
DWORD dwWritten;
IMAGE_DOS_HEADER dos_hdr;
IMAGE_NT_HEADERS nt_hdr;
IMAGE_IMPORT_DESCRIPTOR *dir;
IMAGE_THUNK_DATA thunk;
PDWORD ptmp;
HMODULE base = GetModuleHandle(NULL);
HANDLE hProcess = GetCurrentProcess();
ReadProcessMemory(hProcess, (void *)base, &dos_hdr, sizeof(IMAGE_DOS_HEADER), &dwWritten);
ReadProcessMemory(hProcess, (void *)base+dos_hdr.e_lfanew, &nt_hdr, sizeof(IMAGE_NT_HEADERS), &dwWritten);
dir = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nt_hdr.OptionalHeader.DataDirectory[1].Size));
ReadProcessMemory(hProcess, (void *)base+nt_hdr.OptionalHeader.DataDirectory[1].VirtualAddress, dir, nt_hdr.OptionalHeader.DataDirectory[1].Size, &dwWritten);
while(dir->Name > 0)
{
memset(buf, 0, 32);
ReadProcessMemory(hProcess, (void *)base+dir->Name, buf, 32, &dwWritten);
if(stricmp(buf, hck->name) == 0)
{
ptmp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DWORD) * 64);
ReadProcessMemory(hProcess, (void *)base+dir->Characteristics, ptmp, sizeof(DWORD) * 64, &dwWritten);
i=0;
while(*ptmp)
{
memset(buf, 0, 32);
ReadProcessMemory(hProcess, (void *)base+(*ptmp)+2, buf, 32, &dwWritten);
hk = &hck->hooks[0];
while(hk->func)
{
if(stricmp(hk->name, buf) == 0)
{
thunk.u1.Function = (DWORD)hk->func;
thunk.u1.Ordinal = (DWORD)hk->func;
thunk.u1.AddressOfData = (DWORD)hk->func;
VirtualProtectEx(hProcess, (void *)base+dir->FirstThunk+(sizeof(IMAGE_THUNK_DATA) * i), sizeof(IMAGE_THUNK_DATA), PAGE_EXECUTE_READWRITE, &dwWritten);
WriteProcessMemory(hProcess, (void *)base+dir->FirstThunk+(sizeof(IMAGE_THUNK_DATA) * i), &thunk, sizeof(IMAGE_THUNK_DATA), &dwWritten);
mouse_active = TRUE;
}
hk++;
}
ptmp++;
i++;
}
}
dir++;
}
CloseHandle(hProcess);
}
void mouse_lock()
{
RECT rc;
if (ddraw->devmode)
{
while(ShowCursor(FALSE) > 0);
return;
}
if (mouse_active && !ddraw->locked)
{
GetWindowRect(ddraw->hWnd, &rc);
ddraw->center.x = (rc.right + rc.left) / 2;
ddraw->center.y = (rc.top + rc.bottom) / 2;
SetCursorPos(ddraw->center.x, ddraw->center.y);
SetCapture(ddraw->hWnd);
ClipCursor(&rc);
while(ShowCursor(FALSE) > 0);
ddraw->locked = TRUE;
}
}
void mouse_unlock()
{
RECT rc;
POINT pt;
if (ddraw->devmode)
{
while(ShowCursor(TRUE) < 0);
return;
}
if(!mouse_active)
{
return;
}
if(ddraw->locked)
{
ddraw->locked = FALSE;
GetWindowRect(ddraw->hWnd, &rc);
pt.x = (rc.right - rc.left - ddraw->render.width) / 2;
pt.y = (rc.bottom - rc.top - ddraw->render.height - pt.x);
rc.left += pt.x;
rc.top += pt.y;
SetCursorPos(rc.left + (ddraw->cursor.x * ddraw->render.width / ddraw->width), rc.top + (ddraw->cursor.y * ddraw->render.height / ddraw->height));
while(ShowCursor(TRUE) < 0);
SetCursor(LoadCursor(NULL, IDC_ARROW));
ClipCursor(NULL);
ReleaseCapture();
}
}
void mouse_init(HWND hWnd)
{
if(ddraw->mhack || ddraw->devmode)
{
hack_iat(&hacks[0]);
mouse_active = TRUE;
}
}