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 |
{ |
{ |
161 |
static void |
static void |
162 |
do_position(HWND hwnd, int x, int y, int width, int height) |
do_position(HWND hwnd, int x, int y, int width, int height) |
163 |
{ |
{ |
164 |
SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER); |
g_move_window_fn(hwnd, x, y, width, height); |
165 |
} |
} |
166 |
|
|
167 |
static void |
static void |
173 |
} |
} |
174 |
|
|
175 |
static void |
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]; |
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); |
209 |
0)); |
0)); |
210 |
else if (strcmp(tok1, "ZCHANGE") == 0) |
else if (strcmp(tok1, "ZCHANGE") == 0) |
211 |
do_zchange((HWND) strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0)); |
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 |
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 |
/* Since we don't see the entire desktop we must resize windows |
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); |