/[rdesktop]/sourceforge.net/trunk/seamlessrdp/ServerExe/main.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /sourceforge.net/trunk/seamlessrdp/ServerExe/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1113 - (hide annotations)
Fri Mar 10 16:40:57 2006 UTC (18 years, 2 months ago) by ossman_
File MIME type: text/plain
File size: 6443 byte(s)
Add filtering of dangerous characters in strings that are part of the
protocol.

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 ossman_ 1078 static BOOL CALLBACK
71     enum_cb(HWND hwnd, LPARAM lparam)
72 ossman_ 1075 {
73     RECT rect;
74     char title[150];
75     LONG styles;
76     int state;
77 ossman_ 1097 HWND parent;
78 ossman_ 1075
79     styles = GetWindowLong(hwnd, GWL_STYLE);
80    
81     if (!(styles & WS_VISIBLE))
82     return TRUE;
83    
84 ossman_ 1097 if (styles & WS_POPUP)
85     parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT);
86     else
87     parent = NULL;
88 ossman_ 1075
89 ossman_ 1097 vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0);
90    
91 ossman_ 1078 if (!GetWindowRect(hwnd, &rect))
92     {
93 ossman_ 1075 debug("GetWindowRect failed!");
94     return TRUE;
95     }
96    
97 astrand 1091 vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x",
98 ossman_ 1075 hwnd,
99 ossman_ 1078 rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);
100 ossman_ 1075
101     GetWindowText(hwnd, title, sizeof(title));
102    
103 ossman_ 1113 vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, vchannel_strfilter(title), 0);
104 ossman_ 1075
105     if (styles & WS_MAXIMIZE)
106     state = 2;
107     else if (styles & WS_MINIMIZE)
108     state = 1;
109     else
110     state = 0;
111    
112 ossman_ 1096 vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0);
113 ossman_ 1075
114     return TRUE;
115     }
116    
117 ossman_ 1074 static void
118 ossman_ 1075 do_sync(void)
119     {
120     vchannel_block();
121    
122     vchannel_write("SYNCBEGIN,0x0");
123    
124     EnumWindows(enum_cb, 0);
125    
126     vchannel_write("SYNCEND,0x0");
127    
128     vchannel_unblock();
129     }
130    
131     static void
132 ossman_ 1111 do_state(HWND hwnd, int state)
133     {
134     if (state == 0)
135     ShowWindow(hwnd, SW_RESTORE);
136     else if (state == 1)
137     ShowWindow(hwnd, SW_MINIMIZE);
138     else if (state == 2)
139     ShowWindow(hwnd, SW_MAXIMIZE);
140     else
141     debug("Invalid state %d sent.", state);
142     }
143    
144     static void
145 ossman_ 1112 do_position(HWND hwnd, int x, int y, int width, int height)
146     {
147     SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
148     }
149    
150     static void
151     do_zchange(HWND hwnd, HWND behind)
152     {
153     if (behind == NULL)
154     behind = HWND_TOP;
155     SetWindowPos(hwnd, behind, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
156     }
157    
158     static void
159 ossman_ 1074 process_cmds(void)
160     {
161     char line[VCHANNEL_MAX_LINE];
162     int size;
163    
164 ossman_ 1075 char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8;
165    
166 ossman_ 1078 while ((size = vchannel_read(line, sizeof(line))) >= 0)
167     {
168 ossman_ 1074 debug("Got: %s", line);
169 ossman_ 1075
170     p = line;
171    
172     tok1 = get_token(&p);
173     tok2 = get_token(&p);
174     tok3 = get_token(&p);
175     tok4 = get_token(&p);
176     tok5 = get_token(&p);
177     tok6 = get_token(&p);
178     tok7 = get_token(&p);
179     tok8 = get_token(&p);
180    
181     if (strcmp(tok1, "SYNC") == 0)
182     do_sync();
183 ossman_ 1111 else if (strcmp(tok1, "STATE") == 0)
184     do_state((HWND) strtol(tok2, NULL, 0), strtol(tok3, NULL, 0));
185 ossman_ 1112 else if (strcmp(tok1, "POSITION") == 0)
186     do_position((HWND) strtol(tok2, NULL, 0), strtol(tok3, NULL, 0),
187     strtol(tok4, NULL, 0), strtol(tok5, NULL, 0), strtol(tok6, NULL,
188     0));
189     else if (strcmp(tok1, "ZCHANGE") == 0)
190     do_zchange((HWND) strtol(tok2, NULL, 0), (HWND) strtol(tok3, NULL, 0));
191 ossman_ 1074 }
192     }
193    
194 ossman_ 1071 int WINAPI
195     WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow)
196 ossman_ 1069 {
197 ossman_ 1071 HMODULE hookdll;
198 ossman_ 1069
199 ossman_ 1071 set_hooks_proc_t set_hooks_fn;
200     remove_hooks_proc_t remove_hooks_fn;
201     get_instance_count_proc_t instance_count_fn;
202 ossman_ 1069
203 ossman_ 1071 g_instance = instance;
204 ossman_ 1069
205 ossman_ 1084 hookdll = LoadLibrary("seamlessrdp.dll");
206 ossman_ 1071 if (!hookdll)
207     {
208     message("Could not load hook DLL. Unable to continue.");
209     return -1;
210     }
211 ossman_ 1069
212 ossman_ 1071 set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks");
213     remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks");
214     instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount");
215 ossman_ 1069
216 ossman_ 1071 if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn)
217     {
218     FreeLibrary(hookdll);
219     message("Hook DLL doesn't contain the correct functions. Unable to continue.");
220     return -1;
221     }
222 ossman_ 1069
223 ossman_ 1071 /* Check if the DLL is already loaded */
224     if (instance_count_fn() != 1)
225     {
226     FreeLibrary(hookdll);
227     message("Another running instance of Seamless RDP detected.");
228     return -1;
229     }
230 ossman_ 1069
231 ossman_ 1074 vchannel_open();
232    
233 ossman_ 1071 set_hooks_fn();
234 ossman_ 1069
235 ossman_ 1077 /* Since we don't see the entire desktop we must resize windows
236     immediatly. */
237     SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0);
238    
239 ossman_ 1093 /* Disable screen saver since we cannot catch its windows. */
240     SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
241    
242 ossman_ 1071 if (strlen(cmdline) == 0)
243     {
244     message("No command line specified.");
245     return -1;
246     }
247     else
248     {
249     BOOL result;
250     DWORD exitcode;
251     PROCESS_INFORMATION proc_info;
252     STARTUPINFO startup_info;
253 ossman_ 1069
254 ossman_ 1071 memset(&startup_info, 0, sizeof(STARTUPINFO));
255     startup_info.cb = sizeof(STARTUPINFO);
256 ossman_ 1069
257 ossman_ 1071 result = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0,
258     NULL, NULL, &startup_info, &proc_info);
259 ossman_ 1069
260 ossman_ 1071 if (result)
261     {
262     do
263     {
264 ossman_ 1074 process_cmds();
265     Sleep(100);
266 ossman_ 1071 GetExitCodeProcess(proc_info.hProcess, &exitcode);
267     }
268     while (exitcode == STILL_ACTIVE);
269 ossman_ 1069
270 ossman_ 1071 // Release handles
271     CloseHandle(proc_info.hProcess);
272     CloseHandle(proc_info.hThread);
273     }
274     else
275     {
276     // CreateProcess failed.
277     char msg[256];
278     _snprintf(msg, sizeof(msg),
279     "Unable to launch the requested application:\n%s", cmdline);
280     message(msg);
281     }
282     }
283 ossman_ 1069
284 ossman_ 1071 remove_hooks_fn();
285 ossman_ 1069
286 ossman_ 1071 FreeLibrary(hookdll);
287 ossman_ 1069
288 ossman_ 1074 vchannel_close();
289    
290 ossman_ 1071 return 1;
291 ossman_ 1069 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26