24 |
|
|
25 |
#include <windows.h> |
#include <windows.h> |
26 |
#include <stdio.h> |
#include <stdio.h> |
27 |
|
#include <wtsapi32.h> |
28 |
|
#include <cchannel.h> |
29 |
|
|
30 |
#include "vchannel.h" |
#include "vchannel.h" |
31 |
|
|
33 |
|
|
34 |
#define APP_NAME "SeamlessRDP Shell" |
#define APP_NAME "SeamlessRDP Shell" |
35 |
|
|
36 |
|
#define FOCUS_MSG_NAME "WM_SEAMLESS_FOCUS" |
37 |
|
static UINT g_wm_seamless_focus; |
38 |
|
|
39 |
|
#ifndef WM_WTSSESSION_CHANGE |
40 |
|
#define WM_WTSSESSION_CHANGE 0x02B1 |
41 |
|
#endif |
42 |
|
#ifndef WTS_REMOTE_CONNECT |
43 |
|
#define WTS_REMOTE_CONNECT 0x3 |
44 |
|
#endif |
45 |
|
|
46 |
/* Global data */ |
/* Global data */ |
47 |
static HINSTANCE g_instance; |
static HINSTANCE g_instance; |
48 |
|
static HWND g_hwnd; |
49 |
|
|
50 |
typedef void (*set_hooks_proc_t) (); |
typedef void (*set_hooks_proc_t) (); |
51 |
typedef void (*remove_hooks_proc_t) (); |
typedef void (*remove_hooks_proc_t) (); |
52 |
typedef int (*get_instance_count_proc_t) (); |
typedef int (*get_instance_count_proc_t) (); |
53 |
|
|
54 |
|
typedef void (*move_window_proc_t) (HWND hwnd, int x, int y, int width, int height); |
55 |
|
|
56 |
|
static move_window_proc_t g_move_window_fn = NULL; |
57 |
|
|
58 |
static void |
static void |
59 |
message(const char *text) |
message(const char *text) |
60 |
{ |
{ |
84 |
return head; |
return head; |
85 |
} |
} |
86 |
|
|
87 |
static BOOL CALLBACK enum_cb(HWND hwnd, LPARAM lparam) |
static BOOL CALLBACK |
88 |
|
enum_cb(HWND hwnd, LPARAM lparam) |
89 |
{ |
{ |
90 |
RECT rect; |
RECT rect; |
91 |
char title[150]; |
unsigned short title[150]; |
92 |
LONG styles; |
LONG styles; |
93 |
int state; |
int state; |
94 |
|
HWND parent; |
95 |
|
|
96 |
styles = GetWindowLong(hwnd, GWL_STYLE); |
styles = GetWindowLong(hwnd, GWL_STYLE); |
97 |
|
|
98 |
if (!(styles & WS_VISIBLE)) |
if (!(styles & WS_VISIBLE)) |
99 |
return TRUE; |
return TRUE; |
100 |
|
|
101 |
vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0); |
if (styles & WS_POPUP) |
102 |
|
parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT); |
103 |
|
else |
104 |
|
parent = NULL; |
105 |
|
|
106 |
|
vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0); |
107 |
|
|
108 |
if (!GetWindowRect(hwnd, &rect)) { |
if (!GetWindowRect(hwnd, &rect)) |
109 |
|
{ |
110 |
debug("GetWindowRect failed!"); |
debug("GetWindowRect failed!"); |
111 |
return TRUE; |
return TRUE; |
112 |
} |
} |
113 |
|
|
114 |
vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", |
vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x", |
115 |
hwnd, |
hwnd, |
116 |
rect.left, rect.top, |
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); |
|
rect.right - rect.left, |
|
|
rect.bottom - rect.top, |
|
|
0); |
|
117 |
|
|
118 |
GetWindowText(hwnd, title, sizeof(title)); |
GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title)); |
119 |
|
|
120 |
/* FIXME: Strip title of dangerous characters */ |
vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, vchannel_strfilter_unicode(title), 0); |
121 |
|
|
122 |
if (styles & WS_MAXIMIZE) |
if (styles & WS_MAXIMIZE) |
123 |
state = 2; |
state = 2; |
126 |
else |
else |
127 |
state = 0; |
state = 0; |
128 |
|
|
129 |
vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x", |
vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0); |
|
hwnd, title, state, 0); |
|
130 |
|
|
131 |
return TRUE; |
return TRUE; |
132 |
} |
} |
146 |
} |
} |
147 |
|
|
148 |
static void |
static void |
149 |
|
do_state(HWND hwnd, int state) |
150 |
|
{ |
151 |
|
if (state == 0) |
152 |
|
ShowWindow(hwnd, SW_RESTORE); |
153 |
|
else if (state == 1) |
154 |
|
ShowWindow(hwnd, SW_MINIMIZE); |
155 |
|
else if (state == 2) |
156 |
|
ShowWindow(hwnd, SW_MAXIMIZE); |
157 |
|
else |
158 |
|
debug("Invalid state %d sent.", state); |
159 |
|
} |
160 |
|
|
161 |
|
static void |
162 |
|
do_position(HWND hwnd, int x, int y, int width, int height) |
163 |
|
{ |
164 |
|
g_move_window_fn(hwnd, x, y, width, height); |
165 |
|
} |
166 |
|
|
167 |
|
static void |
168 |
|
do_zchange(HWND hwnd, HWND behind) |
169 |
|
{ |
170 |
|
if (behind == NULL) |
171 |
|
behind = HWND_TOP; |
172 |
|
SetWindowPos(hwnd, behind, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE); |
173 |
|
} |
174 |
|
|
175 |
|
static void |
176 |
|
do_focus(HWND hwnd) |
177 |
|
{ |
178 |
|
SendMessage(hwnd, g_wm_seamless_focus, 0, 0); |
179 |
|
} |
180 |
|
|
181 |
|
static void |
182 |
process_cmds(void) |
process_cmds(void) |
183 |
{ |
{ |
184 |
char line[VCHANNEL_MAX_LINE]; |
char line[VCHANNEL_MAX_LINE]; |
186 |
|
|
187 |
char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8; |
char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8; |
188 |
|
|
189 |
while ((size = vchannel_read(line, sizeof(line))) >= 0) { |
while ((size = vchannel_read(line, sizeof(line))) >= 0) |
190 |
debug("Got: %s", line); |
{ |
|
|
|
191 |
p = line; |
p = line; |
192 |
|
|
193 |
tok1 = get_token(&p); |
tok1 = get_token(&p); |
201 |
|
|
202 |
if (strcmp(tok1, "SYNC") == 0) |
if (strcmp(tok1, "SYNC") == 0) |
203 |
do_sync(); |
do_sync(); |
204 |
|
else if (strcmp(tok1, "STATE") == 0) |
205 |
|
do_state((HWND) strtoul(tok2, NULL, 0), strtol(tok3, NULL, 0)); |
206 |
|
else if (strcmp(tok1, "POSITION") == 0) |
207 |
|
do_position((HWND) strtoul(tok2, NULL, 0), strtol(tok3, NULL, 0), |
208 |
|
strtol(tok4, NULL, 0), strtol(tok5, NULL, 0), strtol(tok6, NULL, |
209 |
|
0)); |
210 |
|
else if (strcmp(tok1, "ZCHANGE") == 0) |
211 |
|
do_zchange((HWND) strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0)); |
212 |
|
else if (strcmp(tok1, "FOCUS") == 0) |
213 |
|
do_focus((HWND) strtoul(tok2, NULL, 0)); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
static LRESULT CALLBACK |
218 |
|
wndproc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) |
219 |
|
{ |
220 |
|
if (message == WM_WTSSESSION_CHANGE) |
221 |
|
{ |
222 |
|
if (wparam == WTS_REMOTE_CONNECT) |
223 |
|
{ |
224 |
|
/* These get reset on each reconnect */ |
225 |
|
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0); |
226 |
|
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0); |
227 |
|
vchannel_write("HELLO,0x%08x", 1); |
228 |
|
} |
229 |
} |
} |
230 |
|
|
231 |
|
return DefWindowProc(hwnd, message, wparam, lparam); |
232 |
|
} |
233 |
|
|
234 |
|
static BOOL |
235 |
|
register_class(void) |
236 |
|
{ |
237 |
|
WNDCLASSEX wcx; |
238 |
|
|
239 |
|
memset(&wcx, 0, sizeof(wcx)); |
240 |
|
|
241 |
|
wcx.cbSize = sizeof(wcx); |
242 |
|
wcx.lpfnWndProc = wndproc; |
243 |
|
wcx.hInstance = g_instance; |
244 |
|
wcx.lpszClassName = "SeamlessClass"; |
245 |
|
|
246 |
|
return RegisterClassEx(&wcx); |
247 |
|
} |
248 |
|
|
249 |
|
static BOOL |
250 |
|
create_wnd(void) |
251 |
|
{ |
252 |
|
if (!register_class()) |
253 |
|
return FALSE; |
254 |
|
|
255 |
|
g_hwnd = CreateWindow("SeamlessClass", |
256 |
|
"Seamless Window", |
257 |
|
WS_OVERLAPPEDWINDOW, |
258 |
|
CW_USEDEFAULT, |
259 |
|
CW_USEDEFAULT, |
260 |
|
CW_USEDEFAULT, |
261 |
|
CW_USEDEFAULT, (HWND) NULL, (HMENU) NULL, g_instance, (LPVOID) NULL); |
262 |
|
|
263 |
|
if (!g_hwnd) |
264 |
|
return FALSE; |
265 |
|
|
266 |
|
return TRUE; |
267 |
} |
} |
268 |
|
|
269 |
int WINAPI |
int WINAPI |
277 |
|
|
278 |
g_instance = instance; |
g_instance = instance; |
279 |
|
|
280 |
hookdll = LoadLibrary("hookdll.dll"); |
hookdll = LoadLibrary("seamlessrdp.dll"); |
281 |
if (!hookdll) |
if (!hookdll) |
282 |
{ |
{ |
283 |
message("Could not load hook DLL. Unable to continue."); |
message("Could not load hook DLL. Unable to continue."); |
287 |
set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks"); |
set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks"); |
288 |
remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks"); |
remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks"); |
289 |
instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount"); |
instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount"); |
290 |
|
g_move_window_fn = (move_window_proc_t) GetProcAddress(hookdll, "SafeMoveWindow"); |
291 |
|
|
292 |
if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn) |
if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn || !g_move_window_fn) |
293 |
{ |
{ |
294 |
FreeLibrary(hookdll); |
FreeLibrary(hookdll); |
295 |
message("Hook DLL doesn't contain the correct functions. Unable to continue."); |
message("Hook DLL doesn't contain the correct functions. Unable to continue."); |
304 |
return -1; |
return -1; |
305 |
} |
} |
306 |
|
|
307 |
|
if (!create_wnd()) |
308 |
|
{ |
309 |
|
FreeLibrary(hookdll); |
310 |
|
message("Couldn't create a window to catch events."); |
311 |
|
return -1; |
312 |
|
} |
313 |
|
|
314 |
|
g_wm_seamless_focus = RegisterWindowMessage(FOCUS_MSG_NAME); |
315 |
|
|
316 |
|
WTSRegisterSessionNotification(g_hwnd, NOTIFY_FOR_THIS_SESSION); |
317 |
|
|
318 |
vchannel_open(); |
vchannel_open(); |
319 |
|
|
320 |
|
vchannel_write("HELLO,0x%08x", 0); |
321 |
|
|
322 |
set_hooks_fn(); |
set_hooks_fn(); |
323 |
|
|
324 |
|
/* Since we don't see the entire desktop we must resize windows |
325 |
|
immediatly. */ |
326 |
|
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0); |
327 |
|
|
328 |
|
/* Disable screen saver since we cannot catch its windows. */ |
329 |
|
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0); |
330 |
|
|
331 |
if (strlen(cmdline) == 0) |
if (strlen(cmdline) == 0) |
332 |
{ |
{ |
333 |
message("No command line specified."); |
message("No command line specified."); |
339 |
DWORD exitcode; |
DWORD exitcode; |
340 |
PROCESS_INFORMATION proc_info; |
PROCESS_INFORMATION proc_info; |
341 |
STARTUPINFO startup_info; |
STARTUPINFO startup_info; |
342 |
|
MSG msg; |
343 |
|
|
344 |
memset(&startup_info, 0, sizeof(STARTUPINFO)); |
memset(&startup_info, 0, sizeof(STARTUPINFO)); |
345 |
startup_info.cb = sizeof(STARTUPINFO); |
startup_info.cb = sizeof(STARTUPINFO); |
351 |
{ |
{ |
352 |
do |
do |
353 |
{ |
{ |
354 |
|
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
355 |
|
{ |
356 |
|
TranslateMessage(&msg); |
357 |
|
DispatchMessage(&msg); |
358 |
|
} |
359 |
process_cmds(); |
process_cmds(); |
360 |
Sleep(100); |
Sleep(100); |
361 |
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
376 |
} |
} |
377 |
} |
} |
378 |
|
|
379 |
|
WTSUnRegisterSessionNotification(g_hwnd); |
380 |
|
|
381 |
remove_hooks_fn(); |
remove_hooks_fn(); |
382 |
|
|
383 |
FreeLibrary(hookdll); |
FreeLibrary(hookdll); |