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 |
parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT); |
95 |
else |
96 |
parent = NULL; |
97 |
|
98 |
switch (msg) |
99 |
{ |
100 |
|
101 |
case WM_WINDOWPOSCHANGED: |
102 |
{ |
103 |
WINDOWPOS *wp = (WINDOWPOS *) lparam; |
104 |
|
105 |
if (wp->flags & SWP_SHOWWINDOW) |
106 |
{ |
107 |
char title[150]; |
108 |
int state; |
109 |
|
110 |
vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0); |
111 |
|
112 |
GetWindowText(hwnd, title, sizeof(title)); |
113 |
|
114 |
/* 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 |
update_position(hwnd); |
128 |
|
129 |
/* FIXME: Figure out z order */ |
130 |
} |
131 |
|
132 |
if (wp->flags & SWP_HIDEWINDOW) |
133 |
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
134 |
|
135 |
if (!(style & WS_VISIBLE)) |
136 |
break; |
137 |
|
138 |
if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE)) |
139 |
update_position(hwnd); |
140 |
|
141 |
if (!(wp->flags & SWP_NOZORDER)) |
142 |
{ |
143 |
vchannel_write("ZCHANGE,0x%p,0x%p,0x%x", |
144 |
hwnd, |
145 |
wp->flags & SWP_NOACTIVATE ? wp-> |
146 |
hwndInsertAfter : 0, 0); |
147 |
} |
148 |
|
149 |
break; |
150 |
} |
151 |
|
152 |
case WM_SIZE: |
153 |
if (!(style & WS_VISIBLE)) |
154 |
break; |
155 |
update_position(hwnd); |
156 |
break; |
157 |
|
158 |
case WM_MOVE: |
159 |
if (!(style & WS_VISIBLE)) |
160 |
break; |
161 |
update_position(hwnd); |
162 |
break; |
163 |
|
164 |
case WM_DESTROY: |
165 |
if (!(style & WS_VISIBLE)) |
166 |
break; |
167 |
vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0); |
168 |
break; |
169 |
|
170 |
default: |
171 |
break; |
172 |
} |
173 |
|
174 |
end: |
175 |
return CallNextHookEx(g_wndproc_hook, code, cur_thread, details); |
176 |
} |
177 |
|
178 |
static LRESULT CALLBACK |
179 |
cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam) |
180 |
{ |
181 |
if (code < 0) |
182 |
goto end; |
183 |
|
184 |
switch (code) |
185 |
{ |
186 |
case HCBT_MINMAX: |
187 |
{ |
188 |
int show, state; |
189 |
|
190 |
show = LOWORD(lparam); |
191 |
|
192 |
if (show == SW_SHOWNORMAL) |
193 |
state = 0; |
194 |
else if (show == SW_SHOWMINIMIZED) |
195 |
state = 1; |
196 |
else if (show == SW_SHOWMAXIMIZED) |
197 |
state = 2; |
198 |
else |
199 |
break; |
200 |
vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0); |
201 |
break; |
202 |
} |
203 |
|
204 |
default: |
205 |
break; |
206 |
} |
207 |
|
208 |
end: |
209 |
return CallNextHookEx(g_cbt_hook, code, wparam, lparam); |
210 |
} |
211 |
|
212 |
DLL_EXPORT void |
213 |
SetHooks(void) |
214 |
{ |
215 |
if (!g_cbt_hook) |
216 |
g_cbt_hook = SetWindowsHookEx(WH_CBT, cbt_hook_proc, g_instance, 0); |
217 |
|
218 |
if (!g_wndproc_hook) |
219 |
g_wndproc_hook = SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0); |
220 |
} |
221 |
|
222 |
DLL_EXPORT void |
223 |
RemoveHooks(void) |
224 |
{ |
225 |
if (g_cbt_hook) |
226 |
UnhookWindowsHookEx(g_cbt_hook); |
227 |
|
228 |
if (g_wndproc_hook) |
229 |
UnhookWindowsHookEx(g_wndproc_hook); |
230 |
} |
231 |
|
232 |
DLL_EXPORT int |
233 |
GetInstanceCount() |
234 |
{ |
235 |
return g_instance_count; |
236 |
} |
237 |
|
238 |
BOOL APIENTRY |
239 |
DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call, LPVOID lpReserved) |
240 |
{ |
241 |
switch (ul_reason_for_call) |
242 |
{ |
243 |
case DLL_PROCESS_ATTACH: |
244 |
// remember our instance handle |
245 |
g_instance = hinstDLL; |
246 |
|
247 |
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessDLL"); |
248 |
if (!g_mutex) |
249 |
return FALSE; |
250 |
|
251 |
WaitForSingleObject(g_mutex, INFINITE); |
252 |
++g_instance_count; |
253 |
ReleaseMutex(g_mutex); |
254 |
|
255 |
vchannel_open(); |
256 |
|
257 |
break; |
258 |
|
259 |
case DLL_THREAD_ATTACH: |
260 |
break; |
261 |
|
262 |
case DLL_THREAD_DETACH: |
263 |
break; |
264 |
|
265 |
case DLL_PROCESS_DETACH: |
266 |
WaitForSingleObject(g_mutex, INFINITE); |
267 |
--g_instance_count; |
268 |
ReleaseMutex(g_mutex); |
269 |
|
270 |
vchannel_close(); |
271 |
|
272 |
CloseHandle(g_mutex); |
273 |
|
274 |
break; |
275 |
} |
276 |
|
277 |
return TRUE; |
278 |
} |