1 |
ossman_ |
1071 |
/* -*- c-basic-offset: 8 -*- |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Seamless windows - Remote server hook DLL |
4 |
ossman_ |
1069 |
|
5 |
ossman_ |
1071 |
Based on code copyright (C) 2004-2005 Martin Wickett |
6 |
|
|
|
7 |
|
|
Copyright (C) Peter Åstrand <astrand@cendio.se> 2005-2006 |
8 |
|
|
Copyright (C) Pierre Ossman <ossman@cendio.se> 2006 |
9 |
|
|
|
10 |
|
|
This program is free software; you can redistribute it and/or modify |
11 |
|
|
it under the terms of the GNU General Public License as published by |
12 |
|
|
the Free Software Foundation; either version 2 of the License, or |
13 |
|
|
(at your option) any later version. |
14 |
|
|
|
15 |
|
|
This program is distributed in the hope that it will be useful, |
16 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
GNU General Public License for more details. |
19 |
|
|
|
20 |
|
|
You should have received a copy of the GNU General Public License |
21 |
|
|
along with this program; if not, write to the Free Software |
22 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
23 |
|
|
*/ |
24 |
|
|
|
25 |
ossman_ |
1069 |
#include <stdio.h> |
26 |
|
|
#include <stdarg.h> |
27 |
|
|
|
28 |
ossman_ |
1071 |
#include <windows.h> |
29 |
|
|
#include <winuser.h> |
30 |
ossman_ |
1069 |
|
31 |
ossman_ |
1073 |
#include "../vchannel.h" |
32 |
ossman_ |
1071 |
|
33 |
ossman_ |
1069 |
#define DLL_EXPORT __declspec(dllexport) |
34 |
|
|
|
35 |
|
|
// Shared DATA |
36 |
|
|
#pragma data_seg ( "SHAREDDATA" ) |
37 |
|
|
|
38 |
|
|
// this is the total number of processes this dll is currently attached to |
39 |
ossman_ |
1071 |
int g_instance_count = 0; |
40 |
ossman_ |
1069 |
|
41 |
|
|
#pragma data_seg () |
42 |
|
|
|
43 |
|
|
#pragma comment(linker, "/section:SHAREDDATA,rws") |
44 |
|
|
|
45 |
ossman_ |
1071 |
static HHOOK g_cbt_hook = NULL; |
46 |
|
|
static HHOOK g_wndproc_hook = NULL; |
47 |
ossman_ |
1069 |
|
48 |
ossman_ |
1071 |
static HINSTANCE g_instance = NULL; |
49 |
ossman_ |
1069 |
|
50 |
ossman_ |
1071 |
static HANDLE g_mutex = NULL; |
51 |
ossman_ |
1069 |
|
52 |
ossman_ |
1081 |
static void |
53 |
|
|
update_position(HWND hwnd) |
54 |
|
|
{ |
55 |
|
|
RECT rect; |
56 |
|
|
|
57 |
|
|
if (!GetWindowRect(hwnd, &rect)) |
58 |
|
|
{ |
59 |
|
|
debug("GetWindowRect failed!\n"); |
60 |
|
|
return; |
61 |
|
|
} |
62 |
|
|
|
63 |
astrand |
1091 |
vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x", |
64 |
ossman_ |
1081 |
hwnd, |
65 |
|
|
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); |
66 |
|
|
} |
67 |
|
|
|
68 |
ossman_ |
1071 |
static LRESULT CALLBACK |
69 |
|
|
wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) |
70 |
ossman_ |
1069 |
{ |
71 |
ossman_ |
1097 |
HWND hwnd, parent; |
72 |
ossman_ |
1079 |
UINT msg; |
73 |
|
|
WPARAM wparam; |
74 |
|
|
LPARAM lparam; |
75 |
ossman_ |
1069 |
|
76 |
ossman_ |
1079 |
LONG style; |
77 |
ossman_ |
1069 |
|
78 |
ossman_ |
1071 |
if (code < 0) |
79 |
|
|
goto end; |
80 |
ossman_ |
1069 |
|
81 |
ossman_ |
1079 |
hwnd = ((CWPSTRUCT *) details)->hwnd; |
82 |
|
|
msg = ((CWPSTRUCT *) details)->message; |
83 |
|
|
wparam = ((CWPSTRUCT *) details)->wParam; |
84 |
|
|
lparam = ((CWPSTRUCT *) details)->lParam; |
85 |
|
|
|
86 |
|
|
style = GetWindowLong(hwnd, GWL_STYLE); |
87 |
|
|
|
88 |
ossman_ |
1082 |
/* Docs say that WS_CHILD and WS_POPUP is an illegal combination, |
89 |
|
|
but they exist nonetheless. */ |
90 |
|
|
if ((style & WS_CHILD) && !(style & WS_POPUP)) |
91 |
ossman_ |
1079 |
goto end; |
92 |
|
|
|
93 |
ossman_ |
1097 |
if (style & WS_POPUP) |
94 |
|
|
parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT); |
95 |
|
|
else |
96 |
|
|
parent = NULL; |
97 |
|
|
|
98 |
ossman_ |
1071 |
switch (msg) |
99 |
|
|
{ |
100 |
ossman_ |
1069 |
|
101 |
ossman_ |
1071 |
case WM_WINDOWPOSCHANGED: |
102 |
ossman_ |
1079 |
{ |
103 |
|
|
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
104 |
ossman_ |
1069 |
|
105 |
ossman_ |
1079 |
if (wp->flags & SWP_SHOWWINDOW) |
106 |
|
|
{ |
107 |
ossman_ |
1099 |
char title[150]; |
108 |
|
|
int state; |
109 |
|
|
|
110 |
ossman_ |
1097 |
vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0); |
111 |
ossman_ |
1069 |
|
112 |
ossman_ |
1099 |
GetWindowText(hwnd, title, sizeof(title)); |
113 |
ossman_ |
1069 |
|
114 |
ossman_ |
1099 |
/* FIXME: Strip title of dangerous characters */ |
115 |
|
|
|
116 |
|
|
vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, title, 0); |
117 |
|
|
|
118 |
|
|
if (style & WS_MAXIMIZE) |
119 |
|
|
state = 2; |
120 |
|
|
else if (style & WS_MINIMIZE) |
121 |
|
|
state = 1; |
122 |
|
|
else |
123 |
|
|
state = 0; |
124 |
|
|
|
125 |
|
|
vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0); |
126 |
|
|
|
127 |
ossman_ |
1081 |
update_position(hwnd); |
128 |
ossman_ |
1099 |
|
129 |
|
|
/* FIXME: Figure out z order */ |
130 |
ossman_ |
1079 |
} |
131 |
ossman_ |
1069 |
|
132 |
ossman_ |
1079 |
if (wp->flags & SWP_HIDEWINDOW) |
133 |
astrand |
1091 |
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
134 |
ossman_ |
1079 |
|
135 |
|
|
if (!(style & WS_VISIBLE)) |
136 |
|
|
break; |
137 |
|
|
|
138 |
ossman_ |
1080 |
if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE)) |
139 |
ossman_ |
1081 |
update_position(hwnd); |
140 |
ossman_ |
1079 |
|
141 |
|
|
if (!(wp->flags & SWP_NOZORDER)) |
142 |
ossman_ |
1080 |
{ |
143 |
astrand |
1091 |
vchannel_write("ZCHANGE,0x%p,0x%p,0x%x", |
144 |
ossman_ |
1079 |
hwnd, |
145 |
|
|
wp->flags & SWP_NOACTIVATE ? wp-> |
146 |
|
|
hwndInsertAfter : 0, 0); |
147 |
ossman_ |
1080 |
} |
148 |
ossman_ |
1069 |
|
149 |
ossman_ |
1071 |
break; |
150 |
ossman_ |
1079 |
} |
151 |
ossman_ |
1069 |
|
152 |
ossman_ |
1081 |
case WM_SIZE: |
153 |
ossman_ |
1083 |
if (!(style & WS_VISIBLE)) |
154 |
|
|
break; |
155 |
ossman_ |
1081 |
update_position(hwnd); |
156 |
|
|
break; |
157 |
|
|
|
158 |
|
|
case WM_MOVE: |
159 |
ossman_ |
1083 |
if (!(style & WS_VISIBLE)) |
160 |
|
|
break; |
161 |
ossman_ |
1081 |
update_position(hwnd); |
162 |
|
|
break; |
163 |
|
|
|
164 |
ossman_ |
1102 |
case WM_SETTEXT: |
165 |
|
|
if (!(style & WS_VISIBLE)) |
166 |
|
|
break; |
167 |
|
|
/* FIXME: Strip title of dangerous characters */ |
168 |
|
|
vchannel_write("TITLE,0x%p,%s,0x%x", hwnd, (char *) lparam, 0); |
169 |
|
|
break; |
170 |
|
|
|
171 |
ossman_ |
1071 |
case WM_DESTROY: |
172 |
ossman_ |
1083 |
if (!(style & WS_VISIBLE)) |
173 |
|
|
break; |
174 |
astrand |
1091 |
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
175 |
ossman_ |
1071 |
break; |
176 |
ossman_ |
1069 |
|
177 |
ossman_ |
1071 |
default: |
178 |
|
|
break; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
end: |
182 |
|
|
return CallNextHookEx(g_wndproc_hook, code, cur_thread, details); |
183 |
ossman_ |
1069 |
} |
184 |
|
|
|
185 |
ossman_ |
1071 |
static LRESULT CALLBACK |
186 |
|
|
cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam) |
187 |
ossman_ |
1069 |
{ |
188 |
ossman_ |
1071 |
if (code < 0) |
189 |
|
|
goto end; |
190 |
ossman_ |
1069 |
|
191 |
ossman_ |
1071 |
switch (code) |
192 |
|
|
{ |
193 |
|
|
case HCBT_MINMAX: |
194 |
|
|
{ |
195 |
ossman_ |
1079 |
int show, state; |
196 |
ossman_ |
1069 |
|
197 |
ossman_ |
1071 |
show = LOWORD(lparam); |
198 |
ossman_ |
1069 |
|
199 |
ossman_ |
1079 |
if (show == SW_SHOWNORMAL) |
200 |
|
|
state = 0; |
201 |
|
|
else if (show == SW_SHOWMINIMIZED) |
202 |
|
|
state = 1; |
203 |
|
|
else if (show == SW_SHOWMAXIMIZED) |
204 |
|
|
state = 2; |
205 |
ossman_ |
1100 |
else |
206 |
|
|
break; |
207 |
ossman_ |
1096 |
vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0); |
208 |
ossman_ |
1071 |
break; |
209 |
|
|
} |
210 |
ossman_ |
1069 |
|
211 |
ossman_ |
1071 |
default: |
212 |
|
|
break; |
213 |
|
|
} |
214 |
ossman_ |
1069 |
|
215 |
ossman_ |
1071 |
end: |
216 |
|
|
return CallNextHookEx(g_cbt_hook, code, wparam, lparam); |
217 |
ossman_ |
1069 |
} |
218 |
|
|
|
219 |
ossman_ |
1071 |
DLL_EXPORT void |
220 |
|
|
SetHooks(void) |
221 |
ossman_ |
1069 |
{ |
222 |
ossman_ |
1071 |
if (!g_cbt_hook) |
223 |
|
|
g_cbt_hook = SetWindowsHookEx(WH_CBT, cbt_hook_proc, g_instance, 0); |
224 |
ossman_ |
1069 |
|
225 |
ossman_ |
1071 |
if (!g_wndproc_hook) |
226 |
|
|
g_wndproc_hook = SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0); |
227 |
ossman_ |
1069 |
} |
228 |
|
|
|
229 |
ossman_ |
1071 |
DLL_EXPORT void |
230 |
|
|
RemoveHooks(void) |
231 |
ossman_ |
1069 |
{ |
232 |
ossman_ |
1071 |
if (g_cbt_hook) |
233 |
|
|
UnhookWindowsHookEx(g_cbt_hook); |
234 |
ossman_ |
1069 |
|
235 |
ossman_ |
1071 |
if (g_wndproc_hook) |
236 |
|
|
UnhookWindowsHookEx(g_wndproc_hook); |
237 |
ossman_ |
1069 |
} |
238 |
|
|
|
239 |
ossman_ |
1071 |
DLL_EXPORT int |
240 |
|
|
GetInstanceCount() |
241 |
ossman_ |
1069 |
{ |
242 |
ossman_ |
1071 |
return g_instance_count; |
243 |
ossman_ |
1069 |
} |
244 |
|
|
|
245 |
ossman_ |
1071 |
BOOL APIENTRY |
246 |
|
|
DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved) |
247 |
ossman_ |
1069 |
{ |
248 |
ossman_ |
1071 |
switch (ul_reason_for_call) |
249 |
|
|
{ |
250 |
|
|
case DLL_PROCESS_ATTACH: |
251 |
|
|
// remember our instance handle |
252 |
|
|
g_instance = hinstDLL; |
253 |
ossman_ |
1069 |
|
254 |
ossman_ |
1073 |
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL"); |
255 |
ossman_ |
1071 |
if (!g_mutex) |
256 |
|
|
return FALSE; |
257 |
ossman_ |
1069 |
|
258 |
ossman_ |
1071 |
WaitForSingleObject(g_mutex, INFINITE); |
259 |
|
|
++g_instance_count; |
260 |
|
|
ReleaseMutex(g_mutex); |
261 |
ossman_ |
1069 |
|
262 |
ossman_ |
1071 |
vchannel_open(); |
263 |
ossman_ |
1069 |
|
264 |
ossman_ |
1071 |
break; |
265 |
ossman_ |
1069 |
|
266 |
ossman_ |
1071 |
case DLL_THREAD_ATTACH: |
267 |
|
|
break; |
268 |
ossman_ |
1069 |
|
269 |
ossman_ |
1071 |
case DLL_THREAD_DETACH: |
270 |
|
|
break; |
271 |
ossman_ |
1069 |
|
272 |
ossman_ |
1071 |
case DLL_PROCESS_DETACH: |
273 |
|
|
WaitForSingleObject(g_mutex, INFINITE); |
274 |
|
|
--g_instance_count; |
275 |
|
|
ReleaseMutex(g_mutex); |
276 |
ossman_ |
1069 |
|
277 |
ossman_ |
1071 |
vchannel_close(); |
278 |
ossman_ |
1069 |
|
279 |
ossman_ |
1071 |
CloseHandle(g_mutex); |
280 |
ossman_ |
1069 |
|
281 |
ossman_ |
1071 |
break; |
282 |
|
|
} |
283 |
ossman_ |
1069 |
|
284 |
ossman_ |
1071 |
return TRUE; |
285 |
ossman_ |
1069 |
} |