1 |
ossman_ |
1071 |
/* -*- c-basic-offset: 8 -*- |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Seamless windows - Remote server executable |
4 |
ossman_ |
1069 |
|
5 |
ossman_ |
1071 |
Based on code copyright (C) 2004-2005 Martin Wickett |
6 |
ossman_ |
1069 |
|
7 |
ossman_ |
1071 |
Copyright (C) Peter Åstrand <astrand@cendio.se> 2005-2006 |
8 |
|
|
Copyright (C) Pierre Ossman <ossman@cendio.se> 2006 |
9 |
ossman_ |
1069 |
|
10 |
ossman_ |
1071 |
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 |
ossman_ |
1069 |
|
15 |
ossman_ |
1071 |
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 |
ossman_ |
1069 |
|
20 |
ossman_ |
1071 |
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 |
ossman_ |
1069 |
|
25 |
ossman_ |
1071 |
#include <windows.h> |
26 |
|
|
#include <stdio.h> |
27 |
ossman_ |
1069 |
|
28 |
ossman_ |
1074 |
#include "vchannel.h" |
29 |
|
|
|
30 |
ossman_ |
1071 |
#include "resource.h" |
31 |
ossman_ |
1069 |
|
32 |
ossman_ |
1071 |
#define APP_NAME "SeamlessRDP Shell" |
33 |
ossman_ |
1069 |
|
34 |
ossman_ |
1071 |
/* Global data */ |
35 |
|
|
static HINSTANCE g_instance; |
36 |
ossman_ |
1069 |
|
37 |
ossman_ |
1071 |
typedef void (*set_hooks_proc_t) (); |
38 |
|
|
typedef void (*remove_hooks_proc_t) (); |
39 |
|
|
typedef int (*get_instance_count_proc_t) (); |
40 |
ossman_ |
1069 |
|
41 |
ossman_ |
1071 |
static void |
42 |
|
|
message(const char *text) |
43 |
ossman_ |
1069 |
{ |
44 |
ossman_ |
1071 |
MessageBox(GetDesktopWindow(), text, "SeamlessRDP Shell", MB_OK); |
45 |
ossman_ |
1069 |
} |
46 |
|
|
|
47 |
ossman_ |
1075 |
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 enum_cb(HWND hwnd, LPARAM lparam) |
71 |
|
|
{ |
72 |
|
|
RECT rect; |
73 |
|
|
char title[150]; |
74 |
|
|
LONG styles; |
75 |
|
|
int state; |
76 |
|
|
|
77 |
|
|
styles = GetWindowLong(hwnd, GWL_STYLE); |
78 |
|
|
|
79 |
|
|
if (!(styles & WS_VISIBLE)) |
80 |
|
|
return TRUE; |
81 |
|
|
|
82 |
|
|
vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0); |
83 |
|
|
|
84 |
|
|
if (!GetWindowRect(hwnd, &rect)) { |
85 |
|
|
debug("GetWindowRect failed!"); |
86 |
|
|
return TRUE; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x", |
90 |
|
|
hwnd, |
91 |
|
|
rect.left, rect.top, |
92 |
|
|
rect.right - rect.left, |
93 |
|
|
rect.bottom - rect.top, |
94 |
|
|
0); |
95 |
|
|
|
96 |
|
|
GetWindowText(hwnd, title, sizeof(title)); |
97 |
|
|
|
98 |
|
|
/* FIXME: Strip title of dangerous characters */ |
99 |
|
|
|
100 |
|
|
if (styles & WS_MAXIMIZE) |
101 |
|
|
state = 2; |
102 |
|
|
else if (styles & WS_MINIMIZE) |
103 |
|
|
state = 1; |
104 |
|
|
else |
105 |
|
|
state = 0; |
106 |
|
|
|
107 |
|
|
vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x", |
108 |
|
|
hwnd, title, state, 0); |
109 |
|
|
|
110 |
|
|
return TRUE; |
111 |
|
|
} |
112 |
|
|
|
113 |
ossman_ |
1074 |
static void |
114 |
ossman_ |
1075 |
do_sync(void) |
115 |
|
|
{ |
116 |
|
|
vchannel_block(); |
117 |
|
|
|
118 |
|
|
vchannel_write("SYNCBEGIN,0x0"); |
119 |
|
|
|
120 |
|
|
EnumWindows(enum_cb, 0); |
121 |
|
|
|
122 |
|
|
vchannel_write("SYNCEND,0x0"); |
123 |
|
|
|
124 |
|
|
vchannel_unblock(); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
static void |
128 |
ossman_ |
1074 |
process_cmds(void) |
129 |
|
|
{ |
130 |
|
|
char line[VCHANNEL_MAX_LINE]; |
131 |
|
|
int size; |
132 |
|
|
|
133 |
ossman_ |
1075 |
char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8; |
134 |
|
|
|
135 |
ossman_ |
1074 |
while ((size = vchannel_read(line, sizeof(line))) >= 0) { |
136 |
|
|
debug("Got: %s", line); |
137 |
ossman_ |
1075 |
|
138 |
|
|
p = line; |
139 |
|
|
|
140 |
|
|
tok1 = get_token(&p); |
141 |
|
|
tok2 = get_token(&p); |
142 |
|
|
tok3 = get_token(&p); |
143 |
|
|
tok4 = get_token(&p); |
144 |
|
|
tok5 = get_token(&p); |
145 |
|
|
tok6 = get_token(&p); |
146 |
|
|
tok7 = get_token(&p); |
147 |
|
|
tok8 = get_token(&p); |
148 |
|
|
|
149 |
|
|
if (strcmp(tok1, "SYNC") == 0) |
150 |
|
|
do_sync(); |
151 |
ossman_ |
1074 |
} |
152 |
|
|
} |
153 |
|
|
|
154 |
ossman_ |
1071 |
int WINAPI |
155 |
|
|
WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow) |
156 |
ossman_ |
1069 |
{ |
157 |
ossman_ |
1071 |
HMODULE hookdll; |
158 |
ossman_ |
1069 |
|
159 |
ossman_ |
1071 |
set_hooks_proc_t set_hooks_fn; |
160 |
|
|
remove_hooks_proc_t remove_hooks_fn; |
161 |
|
|
get_instance_count_proc_t instance_count_fn; |
162 |
ossman_ |
1069 |
|
163 |
ossman_ |
1071 |
g_instance = instance; |
164 |
ossman_ |
1069 |
|
165 |
ossman_ |
1071 |
hookdll = LoadLibrary("hookdll.dll"); |
166 |
|
|
if (!hookdll) |
167 |
|
|
{ |
168 |
|
|
message("Could not load hook DLL. Unable to continue."); |
169 |
|
|
return -1; |
170 |
|
|
} |
171 |
ossman_ |
1069 |
|
172 |
ossman_ |
1071 |
set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks"); |
173 |
|
|
remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks"); |
174 |
|
|
instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount"); |
175 |
ossman_ |
1069 |
|
176 |
ossman_ |
1071 |
if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn) |
177 |
|
|
{ |
178 |
|
|
FreeLibrary(hookdll); |
179 |
|
|
message("Hook DLL doesn't contain the correct functions. Unable to continue."); |
180 |
|
|
return -1; |
181 |
|
|
} |
182 |
ossman_ |
1069 |
|
183 |
ossman_ |
1071 |
/* Check if the DLL is already loaded */ |
184 |
|
|
if (instance_count_fn() != 1) |
185 |
|
|
{ |
186 |
|
|
FreeLibrary(hookdll); |
187 |
|
|
message("Another running instance of Seamless RDP detected."); |
188 |
|
|
return -1; |
189 |
|
|
} |
190 |
ossman_ |
1069 |
|
191 |
ossman_ |
1074 |
vchannel_open(); |
192 |
|
|
|
193 |
ossman_ |
1071 |
set_hooks_fn(); |
194 |
ossman_ |
1069 |
|
195 |
ossman_ |
1077 |
/* Since we don't see the entire desktop we must resize windows |
196 |
|
|
immediatly. */ |
197 |
|
|
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0); |
198 |
|
|
|
199 |
ossman_ |
1071 |
if (strlen(cmdline) == 0) |
200 |
|
|
{ |
201 |
|
|
message("No command line specified."); |
202 |
|
|
return -1; |
203 |
|
|
} |
204 |
|
|
else |
205 |
|
|
{ |
206 |
|
|
BOOL result; |
207 |
|
|
DWORD exitcode; |
208 |
|
|
PROCESS_INFORMATION proc_info; |
209 |
|
|
STARTUPINFO startup_info; |
210 |
ossman_ |
1069 |
|
211 |
ossman_ |
1071 |
memset(&startup_info, 0, sizeof(STARTUPINFO)); |
212 |
|
|
startup_info.cb = sizeof(STARTUPINFO); |
213 |
ossman_ |
1069 |
|
214 |
ossman_ |
1071 |
result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, |
215 |
|
|
NULL, NULL, &startup_info, &proc_info); |
216 |
ossman_ |
1069 |
|
217 |
ossman_ |
1071 |
if (result) |
218 |
|
|
{ |
219 |
|
|
do |
220 |
|
|
{ |
221 |
ossman_ |
1074 |
process_cmds(); |
222 |
|
|
Sleep(100); |
223 |
ossman_ |
1071 |
GetExitCodeProcess(proc_info.hProcess, &exitcode); |
224 |
|
|
} |
225 |
|
|
while (exitcode == STILL_ACTIVE); |
226 |
ossman_ |
1069 |
|
227 |
ossman_ |
1071 |
// Release handles |
228 |
|
|
CloseHandle(proc_info.hProcess); |
229 |
|
|
CloseHandle(proc_info.hThread); |
230 |
|
|
} |
231 |
|
|
else |
232 |
|
|
{ |
233 |
|
|
// CreateProcess failed. |
234 |
|
|
char msg[256]; |
235 |
|
|
_snprintf(msg, sizeof(msg), |
236 |
|
|
"Unable to launch the requested application:\n%s", cmdline); |
237 |
|
|
message(msg); |
238 |
|
|
} |
239 |
|
|
} |
240 |
ossman_ |
1069 |
|
241 |
ossman_ |
1071 |
remove_hooks_fn(); |
242 |
ossman_ |
1069 |
|
243 |
ossman_ |
1071 |
FreeLibrary(hookdll); |
244 |
ossman_ |
1069 |
|
245 |
ossman_ |
1074 |
vchannel_close(); |
246 |
|
|
|
247 |
ossman_ |
1071 |
return 1; |
248 |
ossman_ |
1069 |
} |