/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1077 by ossman_, Thu Mar 9 15:26:35 2006 UTC revision 1150 by ossman_, Thu Mar 16 15:42:32 2006 UTC
# Line 24  Line 24 
24    
25  #include <windows.h>  #include <windows.h>
26  #include <stdio.h>  #include <stdio.h>
27    #include <wtsapi32.h>
28    #include <cchannel.h>
29    
30  #include "vchannel.h"  #include "vchannel.h"
31    
# Line 31  Line 33 
33    
34  #define APP_NAME "SeamlessRDP Shell"  #define APP_NAME "SeamlessRDP Shell"
35    
36    #ifndef WM_WTSSESSION_CHANGE
37    #define WM_WTSSESSION_CHANGE 0x02B1
38    #endif
39    #ifndef WTS_REMOTE_CONNECT
40    #define WTS_REMOTE_CONNECT 0x3
41    #endif
42    
43  /* Global data */  /* Global data */
44  static HINSTANCE g_instance;  static HINSTANCE g_instance;
45    static HWND g_hwnd;
46    
47  typedef void (*set_hooks_proc_t) ();  typedef void (*set_hooks_proc_t) ();
48  typedef void (*remove_hooks_proc_t) ();  typedef void (*remove_hooks_proc_t) ();
49  typedef int (*get_instance_count_proc_t) ();  typedef int (*get_instance_count_proc_t) ();
50    
51    typedef void (*move_window_proc_t) (HWND hwnd, int x, int y, int width, int height);
52    
53    static move_window_proc_t g_move_window_fn = NULL;
54    
55  static void  static void
56  message(const char *text)  message(const char *text)
57  {  {
# Line 67  get_token(char **s) Line 81  get_token(char **s)
81          return head;          return head;
82  }  }
83    
84  static BOOL CALLBACK enum_cb(HWND hwnd, LPARAM lparam)  static BOOL CALLBACK
85    enum_cb(HWND hwnd, LPARAM lparam)
86  {  {
87          RECT rect;          RECT rect;
88          char title[150];          unsigned short title[150];
89          LONG styles;          LONG styles;
90          int state;          int state;
91            HWND parent;
92    
93          styles = GetWindowLong(hwnd, GWL_STYLE);          styles = GetWindowLong(hwnd, GWL_STYLE);
94    
95          if (!(styles & WS_VISIBLE))          if (!(styles & WS_VISIBLE))
96                  return TRUE;                  return TRUE;
97    
98          vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0);          if (styles & WS_POPUP)
99                    parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT);
100            else
101                    parent = NULL;
102    
103          if (!GetWindowRect(hwnd, &rect)) {          vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0);
104    
105            if (!GetWindowRect(hwnd, &rect))
106            {
107                  debug("GetWindowRect failed!");                  debug("GetWindowRect failed!");
108                  return TRUE;                  return TRUE;
109          }          }
110    
111          vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x",          vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x",
112                         hwnd,                         hwnd,
113                         rect.left, rect.top,                         rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);
                        rect.right - rect.left,  
                        rect.bottom - rect.top,  
                        0);  
114    
115          GetWindowText(hwnd, title, sizeof(title));          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
116    
117          /* FIXME: Strip title of dangerous characters */          vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, vchannel_strfilter_unicode(title), 0);
118    
119          if (styles & WS_MAXIMIZE)          if (styles & WS_MAXIMIZE)
120                  state = 2;                  state = 2;
# Line 104  static BOOL CALLBACK enum_cb(HWND hwnd, Line 123  static BOOL CALLBACK enum_cb(HWND hwnd,
123          else          else
124                  state = 0;                  state = 0;
125    
126          vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x",          vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0);
                        hwnd, title, state, 0);  
127    
128          return TRUE;          return TRUE;
129  }  }
# Line 125  do_sync(void) Line 143  do_sync(void)
143  }  }
144    
145  static void  static void
146    do_state(HWND hwnd, int state)
147    {
148            if (state == 0)
149                    ShowWindow(hwnd, SW_RESTORE);
150            else if (state == 1)
151                    ShowWindow(hwnd, SW_MINIMIZE);
152            else if (state == 2)
153                    ShowWindow(hwnd, SW_MAXIMIZE);
154            else
155                    debug("Invalid state %d sent.", state);
156    }
157    
158    static void
159    do_position(HWND hwnd, int x, int y, int width, int height)
160    {
161            g_move_window_fn(hwnd, x, y, width, height);
162    }
163    
164    static void
165    do_zchange(HWND hwnd, HWND behind)
166    {
167            if (behind == NULL)
168                    behind = HWND_TOP;
169            SetWindowPos(hwnd, behind, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
170    }
171    
172    static void
173  process_cmds(void)  process_cmds(void)
174  {  {
175          char line[VCHANNEL_MAX_LINE];          char line[VCHANNEL_MAX_LINE];
# Line 132  process_cmds(void) Line 177  process_cmds(void)
177    
178          char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8;          char *p, *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7, *tok8;
179    
180          while ((size = vchannel_read(line, sizeof(line))) >= 0) {          while ((size = vchannel_read(line, sizeof(line))) >= 0)
181                  debug("Got: %s", line);          {
   
182                  p = line;                  p = line;
183    
184                  tok1 = get_token(&p);                  tok1 = get_token(&p);
# Line 148  process_cmds(void) Line 192  process_cmds(void)
192    
193                  if (strcmp(tok1, "SYNC") == 0)                  if (strcmp(tok1, "SYNC") == 0)
194                          do_sync();                          do_sync();
195                    else if (strcmp(tok1, "STATE") == 0)
196                            do_state((HWND) strtoul(tok2, NULL, 0), strtol(tok3, NULL, 0));
197                    else if (strcmp(tok1, "POSITION") == 0)
198                            do_position((HWND) strtoul(tok2, NULL, 0), strtol(tok3, NULL, 0),
199                                        strtol(tok4, NULL, 0), strtol(tok5, NULL, 0), strtol(tok6, NULL,
200                                                                                             0));
201                    else if (strcmp(tok1, "ZCHANGE") == 0)
202                            do_zchange((HWND) strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0));
203          }          }
204  }  }
205    
206    static LRESULT CALLBACK
207    wndproc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
208    {
209            if (message == WM_WTSSESSION_CHANGE)
210            {
211                    if (wparam == WTS_REMOTE_CONNECT)
212                    {
213                            /* These get reset on each reconnect */
214                            SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0);
215                            SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
216                            vchannel_write("HELLO,0x%08x", 1);
217                    }
218            }
219    
220            return DefWindowProc(hwnd, message, wparam, lparam);
221    }
222    
223    static BOOL
224    register_class(void)
225    {
226            WNDCLASSEX wcx;
227    
228            memset(&wcx, 0, sizeof(wcx));
229    
230            wcx.cbSize = sizeof(wcx);
231            wcx.lpfnWndProc = wndproc;
232            wcx.hInstance = g_instance;
233            wcx.lpszClassName = "SeamlessClass";
234    
235            return RegisterClassEx(&wcx);
236    }
237    
238    static BOOL
239    create_wnd(void)
240    {
241            if (!register_class())
242                    return FALSE;
243    
244            g_hwnd = CreateWindow("SeamlessClass",
245                                  "Seamless Window",
246                                  WS_OVERLAPPEDWINDOW,
247                                  CW_USEDEFAULT,
248                                  CW_USEDEFAULT,
249                                  CW_USEDEFAULT,
250                                  CW_USEDEFAULT, (HWND) NULL, (HMENU) NULL, g_instance, (LPVOID) NULL);
251    
252            if (!g_hwnd)
253                    return FALSE;
254    
255            return TRUE;
256    }
257    
258  int WINAPI  int WINAPI
259  WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow)  WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int cmdshow)
260  {  {
# Line 162  WinMain(HINSTANCE instance, HINSTANCE pr Line 266  WinMain(HINSTANCE instance, HINSTANCE pr
266    
267          g_instance = instance;          g_instance = instance;
268    
269          hookdll = LoadLibrary("hookdll.dll");          hookdll = LoadLibrary("seamlessrdp.dll");
270          if (!hookdll)          if (!hookdll)
271          {          {
272                  message("Could not load hook DLL. Unable to continue.");                  message("Could not load hook DLL. Unable to continue.");
# Line 172  WinMain(HINSTANCE instance, HINSTANCE pr Line 276  WinMain(HINSTANCE instance, HINSTANCE pr
276          set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks");          set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks");
277          remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks");          remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks");
278          instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount");          instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount");
279            g_move_window_fn = (move_window_proc_t) GetProcAddress(hookdll, "SafeMoveWindow");
280    
281          if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn)          if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn || !g_move_window_fn)
282          {          {
283                  FreeLibrary(hookdll);                  FreeLibrary(hookdll);
284                  message("Hook DLL doesn't contain the correct functions. Unable to continue.");                  message("Hook DLL doesn't contain the correct functions. Unable to continue.");
# Line 188  WinMain(HINSTANCE instance, HINSTANCE pr Line 293  WinMain(HINSTANCE instance, HINSTANCE pr
293                  return -1;                  return -1;
294          }          }
295    
296            if (!create_wnd())
297            {
298                    FreeLibrary(hookdll);
299                    message("Couldn't create a window to catch events.");
300                    return -1;
301            }
302    
303            WTSRegisterSessionNotification(g_hwnd, NOTIFY_FOR_THIS_SESSION);
304    
305          vchannel_open();          vchannel_open();
306    
307            vchannel_write("HELLO,0x%08x", 0);
308    
309          set_hooks_fn();          set_hooks_fn();
310    
311          /* Since we don't see the entire desktop we must resize windows          /* Since we don't see the entire desktop we must resize windows
312             immediatly. */             immediatly. */
313          SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0);          SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0);
314    
315            /* Disable screen saver since we cannot catch its windows. */
316            SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
317    
318          if (strlen(cmdline) == 0)          if (strlen(cmdline) == 0)
319          {          {
320                  message("No command line specified.");                  message("No command line specified.");
# Line 207  WinMain(HINSTANCE instance, HINSTANCE pr Line 326  WinMain(HINSTANCE instance, HINSTANCE pr
326                  DWORD exitcode;                  DWORD exitcode;
327                  PROCESS_INFORMATION proc_info;                  PROCESS_INFORMATION proc_info;
328                  STARTUPINFO startup_info;                  STARTUPINFO startup_info;
329                    MSG msg;
330    
331                  memset(&startup_info, 0, sizeof(STARTUPINFO));                  memset(&startup_info, 0, sizeof(STARTUPINFO));
332                  startup_info.cb = sizeof(STARTUPINFO);                  startup_info.cb = sizeof(STARTUPINFO);
# Line 218  WinMain(HINSTANCE instance, HINSTANCE pr Line 338  WinMain(HINSTANCE instance, HINSTANCE pr
338                  {                  {
339                          do                          do
340                          {                          {
341                                    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
342                                    {
343                                            TranslateMessage(&msg);
344                                            DispatchMessage(&msg);
345                                    }
346                                  process_cmds();                                  process_cmds();
347                                  Sleep(100);                                  Sleep(100);
348                                  GetExitCodeProcess(proc_info.hProcess, &exitcode);                                  GetExitCodeProcess(proc_info.hProcess, &exitcode);
# Line 238  WinMain(HINSTANCE instance, HINSTANCE pr Line 363  WinMain(HINSTANCE instance, HINSTANCE pr
363                  }                  }
364          }          }
365    
366            WTSUnRegisterSessionNotification(g_hwnd);
367    
368          remove_hooks_fn();          remove_hooks_fn();
369    
370          FreeLibrary(hookdll);          FreeLibrary(hookdll);

Legend:
Removed from v.1077  
changed lines
  Added in v.1150

  ViewVC Help
Powered by ViewVC 1.1.26