/[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 1145 - (hide annotations)
Thu Mar 16 13:24:18 2006 UTC (18 years, 3 months ago) by ossman_
File MIME type: text/plain
File size: 6666 byte(s)
Prevent moves we do ourself from generating a POSITION. We do this by storing
the coordinate we're moving to in a shared variable. Since SetWindowPos is
synchronous, we know that the variable will have valid data when the hook
is called.

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26