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 |
|
#ifndef WM_WTSSESSION_CHANGE |
37 |
|
#define WM_WTSSESSION_CHANGE 0x02B1 |
38 |
|
#endif |
39 |
|
#ifndef WTS_REMOTE_CONNECT |
40 |
|
#define WTS_REMOTE_CONNECT 0x3 |
41 |
|
#endif |
42 |
|
|
43 |
/* Global data */ |
/* Global data */ |
44 |
static HINSTANCE g_instance; |
static HINSTANCE g_instance; |
45 |
|
static HWND g_hwnd; |
46 |
|
|
47 |
typedef void (*set_hooks_proc_t) (); |
typedef void (*set_hooks_proc_t) (); |
48 |
typedef void (*remove_hooks_proc_t) (); |
typedef void (*remove_hooks_proc_t) (); |
49 |
typedef int (*get_instance_count_proc_t) (); |
typedef int (*get_instance_count_proc_t) (); |
50 |
|
|
51 |
|
typedef void (*move_window_proc_t) (HWND hwnd, int x, int y, int width, int height); |
52 |
|
typedef void (*zchange_proc_t) (HWND hwnd, HWND behind); |
53 |
|
typedef void (*focus_proc_t) (HWND hwnd); |
54 |
|
typedef void (*set_state_proc_t) (HWND hwnd, int state); |
55 |
|
|
56 |
|
static move_window_proc_t g_move_window_fn = NULL; |
57 |
|
static zchange_proc_t g_zchange_fn = NULL; |
58 |
|
static focus_proc_t g_focus_fn = NULL; |
59 |
|
static set_state_proc_t g_set_state_fn = NULL; |
60 |
|
|
61 |
static void |
static void |
62 |
message(const char *text) |
message(const char *text) |
63 |
{ |
{ |
91 |
enum_cb(HWND hwnd, LPARAM lparam) |
enum_cb(HWND hwnd, LPARAM lparam) |
92 |
{ |
{ |
93 |
RECT rect; |
RECT rect; |
94 |
char title[150]; |
unsigned short title[150]; |
95 |
LONG styles; |
LONG styles; |
96 |
int state; |
int state; |
97 |
HWND parent; |
HWND parent; |
106 |
else |
else |
107 |
parent = NULL; |
parent = NULL; |
108 |
|
|
109 |
vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0); |
vchannel_write("CREATE", "0x%p,0x%p,0x%x", hwnd, parent, 0); |
110 |
|
|
111 |
if (!GetWindowRect(hwnd, &rect)) |
if (!GetWindowRect(hwnd, &rect)) |
112 |
{ |
{ |
114 |
return TRUE; |
return TRUE; |
115 |
} |
} |
116 |
|
|
117 |
vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x", |
vchannel_write("POSITION", "0x%p,%d,%d,%d,%d,0x%x", |
118 |
hwnd, |
hwnd, |
119 |
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); |
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); |
120 |
|
|
121 |
GetWindowText(hwnd, title, sizeof(title)); |
GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title)); |
122 |
|
|
123 |
/* FIXME: Strip title of dangerous characters */ |
vchannel_write("TITLE", "0x%x,%s,0x%x", hwnd, vchannel_strfilter_unicode(title), 0); |
|
|
|
|
vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, title, 0); |
|
124 |
|
|
125 |
if (styles & WS_MAXIMIZE) |
if (styles & WS_MAXIMIZE) |
126 |
state = 2; |
state = 2; |
129 |
else |
else |
130 |
state = 0; |
state = 0; |
131 |
|
|
132 |
vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0); |
vchannel_write("STATE", "0x%p,0x%x,0x%x", hwnd, state, 0); |
133 |
|
|
134 |
return TRUE; |
return TRUE; |
135 |
} |
} |
139 |
{ |
{ |
140 |
vchannel_block(); |
vchannel_block(); |
141 |
|
|
142 |
vchannel_write("SYNCBEGIN,0x0"); |
vchannel_write("SYNCBEGIN", "0x0"); |
143 |
|
|
144 |
EnumWindows(enum_cb, 0); |
EnumWindows(enum_cb, 0); |
145 |
|
|
146 |
vchannel_write("SYNCEND,0x0"); |
vchannel_write("SYNCEND", "0x0"); |
147 |
|
|
148 |
vchannel_unblock(); |
vchannel_unblock(); |
149 |
} |
} |
150 |
|
|
151 |
static void |
static void |
152 |
|
do_state(unsigned int serial, HWND hwnd, int state) |
153 |
|
{ |
154 |
|
g_set_state_fn(hwnd, state); |
155 |
|
} |
156 |
|
|
157 |
|
static void |
158 |
|
do_position(unsigned int serial, HWND hwnd, int x, int y, int width, int height) |
159 |
|
{ |
160 |
|
g_move_window_fn(hwnd, x, y, width, height); |
161 |
|
} |
162 |
|
|
163 |
|
static void |
164 |
|
do_zchange(unsigned int serial, HWND hwnd, HWND behind) |
165 |
|
{ |
166 |
|
g_zchange_fn(hwnd, behind); |
167 |
|
} |
168 |
|
|
169 |
|
static void |
170 |
|
do_focus(unsigned int serial, HWND hwnd) |
171 |
|
{ |
172 |
|
g_focus_fn(hwnd); |
173 |
|
} |
174 |
|
|
175 |
|
static void |
176 |
process_cmds(void) |
process_cmds(void) |
177 |
{ |
{ |
178 |
char line[VCHANNEL_MAX_LINE]; |
char line[VCHANNEL_MAX_LINE]; |
182 |
|
|
183 |
while ((size = vchannel_read(line, sizeof(line))) >= 0) |
while ((size = vchannel_read(line, sizeof(line))) >= 0) |
184 |
{ |
{ |
|
debug("Got: %s", line); |
|
|
|
|
185 |
p = line; |
p = line; |
186 |
|
|
187 |
tok1 = get_token(&p); |
tok1 = get_token(&p); |
195 |
|
|
196 |
if (strcmp(tok1, "SYNC") == 0) |
if (strcmp(tok1, "SYNC") == 0) |
197 |
do_sync(); |
do_sync(); |
198 |
|
else if (strcmp(tok1, "STATE") == 0) |
199 |
|
do_state(strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0), |
200 |
|
strtol(tok4, NULL, 0)); |
201 |
|
else if (strcmp(tok1, "POSITION") == 0) |
202 |
|
do_position(strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0), |
203 |
|
strtol(tok4, NULL, 0), strtol(tok5, NULL, 0), strtol(tok6, NULL, |
204 |
|
0), |
205 |
|
strtol(tok7, NULL, 0)); |
206 |
|
else if (strcmp(tok1, "ZCHANGE") == 0) |
207 |
|
do_zchange(strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0), |
208 |
|
(HWND) strtoul(tok4, NULL, 0)); |
209 |
|
else if (strcmp(tok1, "FOCUS") == 0) |
210 |
|
do_focus(strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0)); |
211 |
} |
} |
212 |
} |
} |
213 |
|
|
214 |
|
static LRESULT CALLBACK |
215 |
|
wndproc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) |
216 |
|
{ |
217 |
|
if (message == WM_WTSSESSION_CHANGE) |
218 |
|
{ |
219 |
|
if (wparam == WTS_REMOTE_CONNECT) |
220 |
|
{ |
221 |
|
/* These get reset on each reconnect */ |
222 |
|
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0); |
223 |
|
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0); |
224 |
|
vchannel_write("HELLO", "0x%08x", 1); |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
|
return DefWindowProc(hwnd, message, wparam, lparam); |
229 |
|
} |
230 |
|
|
231 |
|
static BOOL |
232 |
|
register_class(void) |
233 |
|
{ |
234 |
|
WNDCLASSEX wcx; |
235 |
|
|
236 |
|
memset(&wcx, 0, sizeof(wcx)); |
237 |
|
|
238 |
|
wcx.cbSize = sizeof(wcx); |
239 |
|
wcx.lpfnWndProc = wndproc; |
240 |
|
wcx.hInstance = g_instance; |
241 |
|
wcx.lpszClassName = "SeamlessClass"; |
242 |
|
|
243 |
|
return RegisterClassEx(&wcx); |
244 |
|
} |
245 |
|
|
246 |
|
static BOOL |
247 |
|
create_wnd(void) |
248 |
|
{ |
249 |
|
if (!register_class()) |
250 |
|
return FALSE; |
251 |
|
|
252 |
|
g_hwnd = CreateWindow("SeamlessClass", |
253 |
|
"Seamless Window", |
254 |
|
WS_OVERLAPPEDWINDOW, |
255 |
|
CW_USEDEFAULT, |
256 |
|
CW_USEDEFAULT, |
257 |
|
CW_USEDEFAULT, |
258 |
|
CW_USEDEFAULT, (HWND) NULL, (HMENU) NULL, g_instance, (LPVOID) NULL); |
259 |
|
|
260 |
|
if (!g_hwnd) |
261 |
|
return FALSE; |
262 |
|
|
263 |
|
return TRUE; |
264 |
|
} |
265 |
|
|
266 |
int WINAPI |
int WINAPI |
267 |
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) |
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) |
268 |
{ |
{ |
284 |
set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks"); |
set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks"); |
285 |
remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks"); |
remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks"); |
286 |
instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount"); |
instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount"); |
287 |
|
g_move_window_fn = (move_window_proc_t) GetProcAddress(hookdll, "SafeMoveWindow"); |
288 |
|
g_zchange_fn = (zchange_proc_t) GetProcAddress(hookdll, "SafeZChange"); |
289 |
|
g_focus_fn = (focus_proc_t) GetProcAddress(hookdll, "SafeFocus"); |
290 |
|
g_set_state_fn = (set_state_proc_t) GetProcAddress(hookdll, "SafeSetState"); |
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 |
|
|| !g_zchange_fn || !g_focus_fn || !g_set_state_fn) |
294 |
{ |
{ |
295 |
FreeLibrary(hookdll); |
FreeLibrary(hookdll); |
296 |
message("Hook DLL doesn't contain the correct functions. Unable to continue."); |
message("Hook DLL doesn't contain the correct functions. Unable to continue."); |
305 |
return -1; |
return -1; |
306 |
} |
} |
307 |
|
|
308 |
|
if (!create_wnd()) |
309 |
|
{ |
310 |
|
FreeLibrary(hookdll); |
311 |
|
message("Couldn't create a window to catch events."); |
312 |
|
return -1; |
313 |
|
} |
314 |
|
|
315 |
|
WTSRegisterSessionNotification(g_hwnd, NOTIFY_FOR_THIS_SESSION); |
316 |
|
|
317 |
vchannel_open(); |
vchannel_open(); |
318 |
|
|
319 |
|
vchannel_write("HELLO", "0x%08x", 0); |
320 |
|
|
321 |
set_hooks_fn(); |
set_hooks_fn(); |
322 |
|
|
323 |
/* Since we don't see the entire desktop we must resize windows |
/* Since we don't see the entire desktop we must resize windows |
338 |
DWORD exitcode; |
DWORD exitcode; |
339 |
PROCESS_INFORMATION proc_info; |
PROCESS_INFORMATION proc_info; |
340 |
STARTUPINFO startup_info; |
STARTUPINFO startup_info; |
341 |
|
MSG msg; |
342 |
|
|
343 |
memset(&startup_info, 0, sizeof(STARTUPINFO)); |
memset(&startup_info, 0, sizeof(STARTUPINFO)); |
344 |
startup_info.cb = sizeof(STARTUPINFO); |
startup_info.cb = sizeof(STARTUPINFO); |
350 |
{ |
{ |
351 |
do |
do |
352 |
{ |
{ |
353 |
|
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) |
354 |
|
{ |
355 |
|
TranslateMessage(&msg); |
356 |
|
DispatchMessage(&msg); |
357 |
|
} |
358 |
process_cmds(); |
process_cmds(); |
359 |
Sleep(100); |
Sleep(100); |
360 |
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
375 |
} |
} |
376 |
} |
} |
377 |
|
|
378 |
|
WTSUnRegisterSessionNotification(g_hwnd); |
379 |
|
|
380 |
remove_hooks_fn(); |
remove_hooks_fn(); |
381 |
|
|
382 |
FreeLibrary(hookdll); |
FreeLibrary(hookdll); |