/[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 1113 by ossman_, Fri Mar 10 16:40:57 2006 UTC revision 1158 by ossman_, Fri Mar 17 13:11:20 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    typedef void (*zchange_proc_t) (HWND hwnd, HWND behind);
53    typedef void (*focus_proc_t) (HWND hwnd);
54    
55    static move_window_proc_t g_move_window_fn = NULL;
56    static zchange_proc_t g_zchange_fn = NULL;
57    static focus_proc_t g_focus_fn = NULL;
58    
59  static void  static void
60  message(const char *text)  message(const char *text)
61  {  {
# Line 71  static BOOL CALLBACK Line 89  static BOOL CALLBACK
89  enum_cb(HWND hwnd, LPARAM lparam)  enum_cb(HWND hwnd, LPARAM lparam)
90  {  {
91          RECT rect;          RECT rect;
92          char title[150];          unsigned short title[150];
93          LONG styles;          LONG styles;
94          int state;          int state;
95          HWND parent;          HWND parent;
# Line 98  enum_cb(HWND hwnd, LPARAM lparam) Line 116  enum_cb(HWND hwnd, LPARAM lparam)
116                         hwnd,                         hwnd,
117                         rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);                         rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);
118    
119          GetWindowText(hwnd, title, sizeof(title));          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
120    
121          vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, vchannel_strfilter(title), 0);          vchannel_write("TITLE,0x%x,%s,0x%x", hwnd, vchannel_strfilter_unicode(title), 0);
122    
123          if (styles & WS_MAXIMIZE)          if (styles & WS_MAXIMIZE)
124                  state = 2;                  state = 2;
# Line 144  do_state(HWND hwnd, int state) Line 162  do_state(HWND hwnd, int state)
162  static void  static void
163  do_position(HWND hwnd, int x, int y, int width, int height)  do_position(HWND hwnd, int x, int y, int width, int height)
164  {  {
165          SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);          g_move_window_fn(hwnd, x, y, width, height);
166  }  }
167    
168  static void  static void
169  do_zchange(HWND hwnd, HWND behind)  do_zchange(HWND hwnd, HWND behind)
170  {  {
171          if (behind == NULL)          g_zchange_fn(hwnd, behind);
172                  behind = HWND_TOP;  }
173          SetWindowPos(hwnd, behind, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);  
174    static void
175    do_focus(HWND hwnd)
176    {
177            g_focus_fn(hwnd);
178  }  }
179    
180  static void  static void
# Line 165  process_cmds(void) Line 187  process_cmds(void)
187    
188          while ((size = vchannel_read(line, sizeof(line))) >= 0)          while ((size = vchannel_read(line, sizeof(line))) >= 0)
189          {          {
                 debug("Got: %s", line);  
   
190                  p = line;                  p = line;
191    
192                  tok1 = get_token(&p);                  tok1 = get_token(&p);
# Line 181  process_cmds(void) Line 201  process_cmds(void)
201                  if (strcmp(tok1, "SYNC") == 0)                  if (strcmp(tok1, "SYNC") == 0)
202                          do_sync();                          do_sync();
203                  else if (strcmp(tok1, "STATE") == 0)                  else if (strcmp(tok1, "STATE") == 0)
204                          do_state((HWND) strtol(tok2, NULL, 0), strtol(tok3, NULL, 0));                          do_state((HWND) strtoul(tok2, NULL, 0), strtol(tok3, NULL, 0));
205                  else if (strcmp(tok1, "POSITION") == 0)                  else if (strcmp(tok1, "POSITION") == 0)
206                          do_position((HWND) strtol(tok2, NULL, 0), strtol(tok3, NULL, 0),                          do_position((HWND) strtoul(tok2, NULL, 0), strtol(tok3, NULL, 0),
207                                      strtol(tok4, NULL, 0), strtol(tok5, NULL, 0), strtol(tok6, NULL,                                      strtol(tok4, NULL, 0), strtol(tok5, NULL, 0), strtol(tok6, NULL,
208                                                                                           0));                                                                                           0));
209                  else if (strcmp(tok1, "ZCHANGE") == 0)                  else if (strcmp(tok1, "ZCHANGE") == 0)
210                          do_zchange((HWND) strtol(tok2, NULL, 0), (HWND) strtol(tok3, NULL, 0));                          do_zchange((HWND) strtoul(tok2, NULL, 0), (HWND) strtoul(tok3, NULL, 0));
211                    else if (strcmp(tok1, "FOCUS") == 0)
212                            do_focus((HWND) strtoul(tok2, NULL, 0));
213            }
214    }
215    
216    static LRESULT CALLBACK
217    wndproc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
218    {
219            if (message == WM_WTSSESSION_CHANGE)
220            {
221                    if (wparam == WTS_REMOTE_CONNECT)
222                    {
223                            /* These get reset on each reconnect */
224                            SystemParametersInfo(SPI_SETDRAGFULLWINDOWS, TRUE, NULL, 0);
225                            SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, NULL, 0);
226                            vchannel_write("HELLO,0x%08x", 1);
227                    }
228          }          }
229    
230            return DefWindowProc(hwnd, message, wparam, lparam);
231    }
232    
233    static BOOL
234    register_class(void)
235    {
236            WNDCLASSEX wcx;
237    
238            memset(&wcx, 0, sizeof(wcx));
239    
240            wcx.cbSize = sizeof(wcx);
241            wcx.lpfnWndProc = wndproc;
242            wcx.hInstance = g_instance;
243            wcx.lpszClassName = "SeamlessClass";
244    
245            return RegisterClassEx(&wcx);
246    }
247    
248    static BOOL
249    create_wnd(void)
250    {
251            if (!register_class())
252                    return FALSE;
253    
254            g_hwnd = CreateWindow("SeamlessClass",
255                                  "Seamless Window",
256                                  WS_OVERLAPPEDWINDOW,
257                                  CW_USEDEFAULT,
258                                  CW_USEDEFAULT,
259                                  CW_USEDEFAULT,
260                                  CW_USEDEFAULT, (HWND) NULL, (HMENU) NULL, g_instance, (LPVOID) NULL);
261    
262            if (!g_hwnd)
263                    return FALSE;
264    
265            return TRUE;
266  }  }
267    
268  int WINAPI  int WINAPI
# Line 212  WinMain(HINSTANCE instance, HINSTANCE pr Line 286  WinMain(HINSTANCE instance, HINSTANCE pr
286          set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks");          set_hooks_fn = (set_hooks_proc_t) GetProcAddress(hookdll, "SetHooks");
287          remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks");          remove_hooks_fn = (remove_hooks_proc_t) GetProcAddress(hookdll, "RemoveHooks");
288          instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount");          instance_count_fn = (get_instance_count_proc_t) GetProcAddress(hookdll, "GetInstanceCount");
289            g_move_window_fn = (move_window_proc_t) GetProcAddress(hookdll, "SafeMoveWindow");
290            g_zchange_fn = (zchange_proc_t) GetProcAddress(hookdll, "SafeZChange");
291            g_focus_fn = (focus_proc_t) GetProcAddress(hookdll, "SafeFocus");
292    
293          if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn)          if (!set_hooks_fn || !remove_hooks_fn || !instance_count_fn || !g_move_window_fn
294                || !g_zchange_fn || !g_focus_fn)
295          {          {
296                  FreeLibrary(hookdll);                  FreeLibrary(hookdll);
297                  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 228  WinMain(HINSTANCE instance, HINSTANCE pr Line 306  WinMain(HINSTANCE instance, HINSTANCE pr
306                  return -1;                  return -1;
307          }          }
308    
309            if (!create_wnd())
310            {
311                    FreeLibrary(hookdll);
312                    message("Couldn't create a window to catch events.");
313                    return -1;
314            }
315    
316            WTSRegisterSessionNotification(g_hwnd, NOTIFY_FOR_THIS_SESSION);
317    
318          vchannel_open();          vchannel_open();
319    
320            vchannel_write("HELLO,0x%08x", 0);
321    
322          set_hooks_fn();          set_hooks_fn();
323    
324          /* Since we don't see the entire desktop we must resize windows          /* Since we don't see the entire desktop we must resize windows
# Line 250  WinMain(HINSTANCE instance, HINSTANCE pr Line 339  WinMain(HINSTANCE instance, HINSTANCE pr
339                  DWORD exitcode;                  DWORD exitcode;
340                  PROCESS_INFORMATION proc_info;                  PROCESS_INFORMATION proc_info;
341                  STARTUPINFO startup_info;                  STARTUPINFO startup_info;
342                    MSG msg;
343    
344                  memset(&startup_info, 0, sizeof(STARTUPINFO));                  memset(&startup_info, 0, sizeof(STARTUPINFO));
345                  startup_info.cb = sizeof(STARTUPINFO);                  startup_info.cb = sizeof(STARTUPINFO);
# Line 261  WinMain(HINSTANCE instance, HINSTANCE pr Line 351  WinMain(HINSTANCE instance, HINSTANCE pr
351                  {                  {
352                          do                          do
353                          {                          {
354                                    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
355                                    {
356                                            TranslateMessage(&msg);
357                                            DispatchMessage(&msg);
358                                    }
359                                  process_cmds();                                  process_cmds();
360                                  Sleep(100);                                  Sleep(100);
361                                  GetExitCodeProcess(proc_info.hProcess, &exitcode);                                  GetExitCodeProcess(proc_info.hProcess, &exitcode);
# Line 281  WinMain(HINSTANCE instance, HINSTANCE pr Line 376  WinMain(HINSTANCE instance, HINSTANCE pr
376                  }                  }
377          }          }
378    
379            WTSUnRegisterSessionNotification(g_hwnd);
380    
381          remove_hooks_fn();          remove_hooks_fn();
382    
383          FreeLibrary(hookdll);          FreeLibrary(hookdll);

Legend:
Removed from v.1113  
changed lines
  Added in v.1158

  ViewVC Help
Powered by ViewVC 1.1.26