1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Seamless windows - Remote server hook DLL |
4 |
|
5 |
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 |
#include <stdio.h> |
26 |
#include <stdarg.h> |
27 |
|
28 |
#include <windows.h> |
29 |
#include <winuser.h> |
30 |
|
31 |
#include "../vchannel.h" |
32 |
|
33 |
#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 |
int g_instance_count = 0; |
40 |
|
41 |
#pragma data_seg () |
42 |
|
43 |
#pragma comment(linker, "/section:SHAREDDATA,rws") |
44 |
|
45 |
static HHOOK g_cbt_hook = NULL; |
46 |
static HHOOK g_wndproc_hook = NULL; |
47 |
|
48 |
static HINSTANCE g_instance = NULL; |
49 |
|
50 |
static HANDLE g_mutex = NULL; |
51 |
|
52 |
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 |
vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x", |
64 |
hwnd, |
65 |
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); |
66 |
} |
67 |
|
68 |
static LRESULT CALLBACK |
69 |
wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details) |
70 |
{ |
71 |
HWND hwnd, parent; |
72 |
UINT msg; |
73 |
WPARAM wparam; |
74 |
LPARAM lparam; |
75 |
|
76 |
LONG style; |
77 |
|
78 |
if (code < 0) |
79 |
goto end; |
80 |
|
81 |
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 |
/* 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 |
goto end; |
92 |
|
93 |
if (style & WS_POPUP) |
94 |
{ |
95 |
parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT); |
96 |
if (!parent) |
97 |
parent = (HWND) - 1; |
98 |
} |
99 |
else |
100 |
parent = NULL; |
101 |
|
102 |
switch (msg) |
103 |
{ |
104 |
|
105 |
case WM_WINDOWPOSCHANGED: |
106 |
{ |
107 |
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
108 |
|
109 |
if (wp->flags & SWP_SHOWWINDOW) |
110 |
{ |
111 |
char title[150]; |
112 |
int state; |
113 |
|
114 |
vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0); |
115 |
|
116 |
GetWindowText(hwnd, title, sizeof(title)); |
117 |
|
118 |
vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, |
119 |
vchannel_strfilter(title), 0); |
120 |
|
121 |
if (style & WS_MAXIMIZE) |
122 |
state = 2; |
123 |
else if (style & WS_MINIMIZE) |
124 |
state = 1; |
125 |
else |
126 |
state = 0; |
127 |
|
128 |
vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0); |
129 |
|
130 |
update_position(hwnd); |
131 |
|
132 |
/* FIXME: Figure out z order */ |
133 |
} |
134 |
|
135 |
if (wp->flags & SWP_HIDEWINDOW) |
136 |
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
137 |
|
138 |
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE)) |
139 |
break; |
140 |
|
141 |
if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE)) |
142 |
update_position(hwnd); |
143 |
|
144 |
if (!(wp->flags & SWP_NOZORDER)) |
145 |
{ |
146 |
vchannel_write("ZCHANGE,0x%p,0x%p,0x%x", |
147 |
hwnd, |
148 |
wp->flags & SWP_NOACTIVATE ? wp-> |
149 |
hwndInsertAfter : 0, 0); |
150 |
} |
151 |
|
152 |
break; |
153 |
} |
154 |
|
155 |
case WM_SIZE: |
156 |
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE)) |
157 |
break; |
158 |
update_position(hwnd); |
159 |
break; |
160 |
|
161 |
case WM_MOVE: |
162 |
if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE)) |
163 |
break; |
164 |
update_position(hwnd); |
165 |
break; |
166 |
|
167 |
case WM_SETTEXT: |
168 |
{ |
169 |
char *title; |
170 |
if (!(style & WS_VISIBLE)) |
171 |
break; |
172 |
title = _strdup((char *) lparam); |
173 |
vchannel_write("TITLE,0x%p,%s,0x%x", hwnd, |
174 |
vchannel_strfilter(title), 0); |
175 |
free(title); |
176 |
break; |
177 |
} |
178 |
|
179 |
case WM_DESTROY: |
180 |
if (!(style & WS_VISIBLE)) |
181 |
break; |
182 |
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
183 |
break; |
184 |
|
185 |
default: |
186 |
break; |
187 |
} |
188 |
|
189 |
end: |
190 |
return CallNextHookEx(g_wndproc_hook, code, cur_thread, details); |
191 |
} |
192 |
|
193 |
static LRESULT CALLBACK |
194 |
cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam) |
195 |
{ |
196 |
if (code < 0) |
197 |
goto end; |
198 |
|
199 |
switch (code) |
200 |
{ |
201 |
case HCBT_MINMAX: |
202 |
{ |
203 |
int show, state; |
204 |
|
205 |
show = LOWORD(lparam); |
206 |
|
207 |
if ((show == SW_NORMAL) || (show == SW_SHOWNORMAL) |
208 |
|| (show == SW_RESTORE)) |
209 |
state = 0; |
210 |
else if ((show == SW_MINIMIZE) || (show == SW_SHOWMINIMIZED)) |
211 |
state = 1; |
212 |
else if ((show == SW_MAXIMIZE) || (show == SW_SHOWMAXIMIZED)) |
213 |
state = 2; |
214 |
else |
215 |
{ |
216 |
debug("Unexpected show: %d", show); |
217 |
break; |
218 |
} |
219 |
vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0); |
220 |
break; |
221 |
} |
222 |
|
223 |
default: |
224 |
break; |
225 |
} |
226 |
|
227 |
end: |
228 |
return CallNextHookEx(g_cbt_hook, code, wparam, lparam); |
229 |
} |
230 |
|
231 |
DLL_EXPORT void |
232 |
SetHooks(void) |
233 |
{ |
234 |
if (!g_cbt_hook) |
235 |
g_cbt_hook = SetWindowsHookEx(WH_CBT, cbt_hook_proc, g_instance, 0); |
236 |
|
237 |
if (!g_wndproc_hook) |
238 |
g_wndproc_hook = SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0); |
239 |
} |
240 |
|
241 |
DLL_EXPORT void |
242 |
RemoveHooks(void) |
243 |
{ |
244 |
if (g_cbt_hook) |
245 |
UnhookWindowsHookEx(g_cbt_hook); |
246 |
|
247 |
if (g_wndproc_hook) |
248 |
UnhookWindowsHookEx(g_wndproc_hook); |
249 |
} |
250 |
|
251 |
DLL_EXPORT int |
252 |
GetInstanceCount() |
253 |
{ |
254 |
return g_instance_count; |
255 |
} |
256 |
|
257 |
BOOL APIENTRY |
258 |
DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved) |
259 |
{ |
260 |
switch (ul_reason_for_call) |
261 |
{ |
262 |
case DLL_PROCESS_ATTACH: |
263 |
// remember our instance handle |
264 |
g_instance = hinstDLL; |
265 |
|
266 |
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL"); |
267 |
if (!g_mutex) |
268 |
return FALSE; |
269 |
|
270 |
WaitForSingleObject(g_mutex, INFINITE); |
271 |
++g_instance_count; |
272 |
ReleaseMutex(g_mutex); |
273 |
|
274 |
vchannel_open(); |
275 |
|
276 |
break; |
277 |
|
278 |
case DLL_THREAD_ATTACH: |
279 |
break; |
280 |
|
281 |
case DLL_THREAD_DETACH: |
282 |
break; |
283 |
|
284 |
case DLL_PROCESS_DETACH: |
285 |
WaitForSingleObject(g_mutex, INFINITE); |
286 |
--g_instance_count; |
287 |
ReleaseMutex(g_mutex); |
288 |
|
289 |
vchannel_close(); |
290 |
|
291 |
CloseHandle(g_mutex); |
292 |
|
293 |
break; |
294 |
} |
295 |
|
296 |
return TRUE; |
297 |
} |