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 |
process_cmds(void) |
135 |
{ |
136 |
char line[VCHANNEL_MAX_LINE]; |
137 |
int size; |
138 |
|
139 |
char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8; |
140 |
|
141 |
while ((size = vchannel_read(line, sizeof(line))) >= 0) |
142 |
{ |
143 |
debug("Got: %s", line); |
144 |
|
145 |
p = line; |
146 |
|
147 |
tok1 = get_token(&p); |
148 |
tok2 = get_token(&p); |
149 |
tok3 = get_token(&p); |
150 |
tok4 = get_token(&p); |
151 |
tok5 = get_token(&p); |
152 |
tok6 = get_token(&p); |
153 |
tok7 = get_token(&p); |
154 |
tok8 = get_token(&p); |
155 |
|
156 |
if (strcmp(tok1, "SYNC") == 0) |
157 |
do_sync(); |
158 |
} |
159 |
} |
160 |
|
161 |
int WINAPI |
162 |
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) |
163 |
{ |
164 |
HMODULE hookdll; |
165 |
|
166 |
set_hooks_proc_t set_hooks_fn; |
167 |
remove_hooks_proc_t remove_hooks_fn; |
168 |
get_instance_count_proc_t instance_count_fn; |
169 |
|
170 |
g_instance = instance; |
171 |
|
172 |
hookdll = LoadLibrary("seamlessrdp.dll"); |
173 |
if (!hookdll) |
174 |
{ |
175 |
message("Could not load hook DLL. Unable to continue."); |
176 |
return -1; |
177 |
} |
178 |
|
179 |
set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks"); |
180 |
remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks"); |
181 |
instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount"); |
182 |
|
183 |
if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn) |
184 |
{ |
185 |
FreeLibrary(hookdll); |
186 |
message("Hook DLL doesn't contain the correct functions. Unable to continue."); |
187 |
return -1; |
188 |
} |
189 |
|
190 |
/* Check if the DLL is already loaded */ |
191 |
if (instance_count_fn() != 1) |
192 |
{ |
193 |
FreeLibrary(hookdll); |
194 |
message("Another running instance of Seamless RDP detected."); |
195 |
return -1; |
196 |
} |
197 |
|
198 |
vchannel_open(); |
199 |
|
200 |
set_hooks_fn(); |
201 |
|
202 |
/* Since we don't see the entire desktop we must resize windows |
203 |
immediatly. */ |
204 |
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0); |
205 |
|
206 |
/* Disable screen saver since we cannot catch its windows. */ |
207 |
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0); |
208 |
|
209 |
if (strlen(cmdline) == 0) |
210 |
{ |
211 |
message("No command line specified."); |
212 |
return -1; |
213 |
} |
214 |
else |
215 |
{ |
216 |
BOOL result; |
217 |
DWORD exitcode; |
218 |
PROCESS_INFORMATION proc_info; |
219 |
STARTUPINFO startup_info; |
220 |
|
221 |
memset(&startup_info, 0, sizeof(STARTUPINFO)); |
222 |
startup_info.cb = sizeof(STARTUPINFO); |
223 |
|
224 |
result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, |
225 |
NULL, NULL, &startup_info, &proc_info); |
226 |
|
227 |
if (result) |
228 |
{ |
229 |
do |
230 |
{ |
231 |
process_cmds(); |
232 |
Sleep(100); |
233 |
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
234 |
} |
235 |
while (exitcode == STILL_ACTIVE); |
236 |
|
237 |
// Release handles |
238 |
CloseHandle(proc_info.hProcess); |
239 |
CloseHandle(proc_info.hThread); |
240 |
} |
241 |
else |
242 |
{ |
243 |
// CreateProcess failed. |
244 |
char msg[256]; |
245 |
_snprintf(msg, sizeof(msg), |
246 |
"Unable to launch the requested application:\n%s", cmdline); |
247 |
message(msg); |
248 |
} |
249 |
} |
250 |
|
251 |
remove_hooks_fn(); |
252 |
|
253 |
FreeLibrary(hookdll); |
254 |
|
255 |
vchannel_close(); |
256 |
|
257 |
return 1; |
258 |
} |