1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Seamless windows - Remote server executable |
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 <windows.h> |
26 |
#include <stdio.h> |
27 |
|
28 |
#include "vchannel.h" |
29 |
|
30 |
#include "resource.h" |
31 |
|
32 |
#define APP_NAME "SeamlessRDP Shell" |
33 |
|
34 |
/* Global data */ |
35 |
static HINSTANCE g_instance; |
36 |
|
37 |
typedef void (*set_hooks_proc_t) (); |
38 |
typedef void (*remove_hooks_proc_t) (); |
39 |
typedef int (*get_instance_count_proc_t) (); |
40 |
|
41 |
static void |
42 |
message(const char *text) |
43 |
{ |
44 |
MessageBox(GetDesktopWindow(), text, "SeamlessRDP Shell", MB_OK); |
45 |
} |
46 |
|
47 |
static char * |
48 |
get_token(char **s) |
49 |
{ |
50 |
char *comma, *head; |
51 |
head = *s; |
52 |
|
53 |
if (!head) |
54 |
return NULL; |
55 |
|
56 |
comma = strchr(head, ','); |
57 |
if (comma) |
58 |
{ |
59 |
*comma = '\0'; |
60 |
*s = comma + 1; |
61 |
} |
62 |
else |
63 |
{ |
64 |
*s = NULL; |
65 |
} |
66 |
|
67 |
return head; |
68 |
} |
69 |
|
70 |
static BOOL CALLBACK |
71 |
enum_cb(HWND hwnd, LPARAM lparam) |
72 |
{ |
73 |
RECT rect; |
74 |
char title[150]; |
75 |
LONG styles; |
76 |
int state; |
77 |
HWND parent; |
78 |
|
79 |
styles = GetWindowLong(hwnd, GWL_STYLE); |
80 |
|
81 |
if (!(styles & WS_VISIBLE)) |
82 |
return TRUE; |
83 |
|
84 |
if (styles & WS_POPUP) |
85 |
parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT); |
86 |
else |
87 |
parent = NULL; |
88 |
|
89 |
vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0); |
90 |
|
91 |
if (!GetWindowRect(hwnd, &rect)) |
92 |
{ |
93 |
debug("GetWindowRect failed!"); |
94 |
return TRUE; |
95 |
} |
96 |
|
97 |
vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x", |
98 |
hwnd, |
99 |
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0); |
100 |
|
101 |
GetWindowText(hwnd, title, sizeof(title)); |
102 |
|
103 |
/* FIXME: Strip title of dangerous characters */ |
104 |
|
105 |
vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, title, 0); |
106 |
|
107 |
if (styles & WS_MAXIMIZE) |
108 |
state = 2; |
109 |
else if (styles & WS_MINIMIZE) |
110 |
state = 1; |
111 |
else |
112 |
state = 0; |
113 |
|
114 |
vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0); |
115 |
|
116 |
return TRUE; |
117 |
} |
118 |
|
119 |
static void |
120 |
do_sync(void) |
121 |
{ |
122 |
vchannel_block(); |
123 |
|
124 |
vchannel_write("SYNCBEGIN,0x0"); |
125 |
|
126 |
EnumWindows(enum_cb, 0); |
127 |
|
128 |
vchannel_write("SYNCEND,0x0"); |
129 |
|
130 |
vchannel_unblock(); |
131 |
} |
132 |
|
133 |
static void |
134 |
do_state(HWND hwnd, int state) |
135 |
{ |
136 |
if (state == 0) |
137 |
ShowWindow(hwnd, SW_RESTORE); |
138 |
else if (state == 1) |
139 |
ShowWindow(hwnd, SW_MINIMIZE); |
140 |
else if (state == 2) |
141 |
ShowWindow(hwnd, SW_MAXIMIZE); |
142 |
else |
143 |
debug("Invalid state %d sent.", state); |
144 |
} |
145 |
|
146 |
static void |
147 |
process_cmds(void) |
148 |
{ |
149 |
char line[VCHANNEL_MAX_LINE]; |
150 |
int size; |
151 |
|
152 |
char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8; |
153 |
|
154 |
while ((size = vchannel_read(line, sizeof(line))) >= 0) |
155 |
{ |
156 |
debug("Got: %s", line); |
157 |
|
158 |
p = line; |
159 |
|
160 |
tok1 = get_token(&p); |
161 |
tok2 = get_token(&p); |
162 |
tok3 = get_token(&p); |
163 |
tok4 = get_token(&p); |
164 |
tok5 = get_token(&p); |
165 |
tok6 = get_token(&p); |
166 |
tok7 = get_token(&p); |
167 |
tok8 = get_token(&p); |
168 |
|
169 |
if (strcmp(tok1, "SYNC") == 0) |
170 |
do_sync(); |
171 |
else if (strcmp(tok1, "STATE") == 0) |
172 |
do_state((HWND) strtol(tok2, NULL, 0), strtol(tok3, NULL, 0)); |
173 |
} |
174 |
} |
175 |
|
176 |
int WINAPI |
177 |
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) |
178 |
{ |
179 |
HMODULE hookdll; |
180 |
|
181 |
set_hooks_proc_t set_hooks_fn; |
182 |
remove_hooks_proc_t remove_hooks_fn; |
183 |
get_instance_count_proc_t instance_count_fn; |
184 |
|
185 |
g_instance = instance; |
186 |
|
187 |
hookdll = LoadLibrary("seamlessrdp.dll"); |
188 |
if (!hookdll) |
189 |
{ |
190 |
message("Could not load hook DLL. Unable to continue."); |
191 |
return -1; |
192 |
} |
193 |
|
194 |
set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks"); |
195 |
remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks"); |
196 |
instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount"); |
197 |
|
198 |
if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn) |
199 |
{ |
200 |
FreeLibrary(hookdll); |
201 |
message("Hook DLL doesn't contain the correct functions. Unable to continue."); |
202 |
return -1; |
203 |
} |
204 |
|
205 |
/* Check if the DLL is already loaded */ |
206 |
if (instance_count_fn() != 1) |
207 |
{ |
208 |
FreeLibrary(hookdll); |
209 |
message("Another running instance of Seamless RDP detected."); |
210 |
return -1; |
211 |
} |
212 |
|
213 |
vchannel_open(); |
214 |
|
215 |
set_hooks_fn(); |
216 |
|
217 |
/* Since we don't see the entire desktop we must resize windows |
218 |
immediatly. */ |
219 |
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0); |
220 |
|
221 |
/* Disable screen saver since we cannot catch its windows. */ |
222 |
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0); |
223 |
|
224 |
if (strlen(cmdline) == 0) |
225 |
{ |
226 |
message("No command line specified."); |
227 |
return -1; |
228 |
} |
229 |
else |
230 |
{ |
231 |
BOOL result; |
232 |
DWORD exitcode; |
233 |
PROCESS_INFORMATION proc_info; |
234 |
STARTUPINFO startup_info; |
235 |
|
236 |
memset(&startup_info, 0, sizeof(STARTUPINFO)); |
237 |
startup_info.cb = sizeof(STARTUPINFO); |
238 |
|
239 |
result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, |
240 |
NULL, NULL, &startup_info, &proc_info); |
241 |
|
242 |
if (result) |
243 |
{ |
244 |
do |
245 |
{ |
246 |
process_cmds(); |
247 |
Sleep(100); |
248 |
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
249 |
} |
250 |
while (exitcode == STILL_ACTIVE); |
251 |
|
252 |
// Release handles |
253 |
CloseHandle(proc_info.hProcess); |
254 |
CloseHandle(proc_info.hThread); |
255 |
} |
256 |
else |
257 |
{ |
258 |
// CreateProcess failed. |
259 |
char msg[256]; |
260 |
_snprintf(msg, sizeof(msg), |
261 |
"Unable to launch the requested application:\n%s", cmdline); |
262 |
message(msg); |
263 |
} |
264 |
} |
265 |
|
266 |
remove_hooks_fn(); |
267 |
|
268 |
FreeLibrary(hookdll); |
269 |
|
270 |
vchannel_close(); |
271 |
|
272 |
return 1; |
273 |
} |