/[rdesktop]/sourceforge.net/trunk/seamlessrdp/ServerExe/HookDll/hookdll.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/HookDll/hookdll.c

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

revision 1081 by ossman_, Thu Mar 9 16:13:26 2006 UTC revision 1153 by ossman_, Fri Mar 17 09:55:35 2006 UTC
# Line 32  Line 32 
32    
33  #define DLL_EXPORT __declspec(dllexport)  #define DLL_EXPORT __declspec(dllexport)
34    
35    #ifdef __GNUC__
36    #define SHARED __attribute__((section ("SHAREDDATA"), shared))
37    #else
38    #define SHARED
39    #endif
40    
41  // Shared DATA  // Shared DATA
42  #pragma data_seg ( "SHAREDDATA" )  #pragma data_seg ( "SHAREDDATA" )
43    
44  // this is the total number of processes this dll is currently attached to  // this is the total number of processes this dll is currently attached to
45  int g_instance_count = 0;  int g_instance_count SHARED = 0;
46    
47    // blocks for locally generated events
48    RECT g_block_move SHARED = { 0, 0, 0, 0 };
49    
50  #pragma data_seg ()  #pragma data_seg ()
51    
52  #pragma comment(linker, "/section:SHAREDDATA,rws")  #pragma comment(linker, "/section:SHAREDDATA,rws")
53    
54    #define FOCUS_MSG_NAME "WM_SEAMLESS_FOCUS"
55    static UINT g_wm_seamless_focus;
56    
57  static HHOOK g_cbt_hook = NULL;  static HHOOK g_cbt_hook = NULL;
58  static HHOOK g_wndproc_hook = NULL;  static HHOOK g_wndproc_hook = NULL;
59    static HHOOK g_wndprocret_hook = NULL;
60    
61  static HINSTANCE g_instance = NULL;  static HINSTANCE g_instance = NULL;
62    
# Line 52  static HANDLE g_mutex = NULL; Line 65  static HANDLE g_mutex = NULL;
65  static void  static void
66  update_position(HWND hwnd)  update_position(HWND hwnd)
67  {  {
68          RECT rect;          RECT rect, blocked;
69    
70          if (!GetWindowRect(hwnd, &rect))          if (!GetWindowRect(hwnd, &rect))
71          {          {
# Line 60  update_position(HWND hwnd) Line 73  update_position(HWND hwnd)
73                  return;                  return;
74          }          }
75    
76          vchannel_write("POSITION1,0x%p,%d,%d,%d,%d,0x%x",          WaitForSingleObject(g_mutex, INFINITE);
77            memcpy(&blocked, &g_block_move, sizeof(RECT));
78            ReleaseMutex(g_mutex);
79    
80            if ((rect.left == blocked.left) && (rect.top == blocked.top)
81                && (rect.right == blocked.right) && (rect.bottom == blocked.bottom))
82                    return;
83    
84            vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x",
85                         hwnd,                         hwnd,
86                         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);
87  }  }
# Line 68  update_position(HWND hwnd) Line 89  update_position(HWND hwnd)
89  static LRESULT CALLBACK  static LRESULT CALLBACK
90  wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details)  wndproc_hook_proc(int code, WPARAM cur_thread, LPARAM details)
91  {  {
92          HWND hwnd;          HWND hwnd, parent;
93          UINT msg;          UINT msg;
94          WPARAM wparam;          WPARAM wparam;
95          LPARAM lparam;          LPARAM lparam;
# Line 85  wndproc_hook_proc(int code, WPARAM cur_t Line 106  wndproc_hook_proc(int code, WPARAM cur_t
106    
107          style = GetWindowLong(hwnd, GWL_STYLE);          style = GetWindowLong(hwnd, GWL_STYLE);
108    
109          if (style & WS_CHILD)          /* Docs say that WS_CHILD and WS_POPUP is an illegal combination,
110               but they exist nonetheless. */
111            if ((style & WS_CHILD) && !(style & WS_POPUP))
112                  goto end;                  goto end;
113    
114            if (style & WS_POPUP)
115            {
116                    parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT);
117                    if (!parent)
118                            parent = (HWND) - 1;
119            }
120            else
121                    parent = NULL;
122    
123          switch (msg)          switch (msg)
124          {          {
125    
# Line 97  wndproc_hook_proc(int code, WPARAM cur_t Line 129  wndproc_hook_proc(int code, WPARAM cur_t
129    
130                                  if (wp->flags & SWP_SHOWWINDOW)                                  if (wp->flags & SWP_SHOWWINDOW)
131                                  {                                  {
132                                          // FIXME: Now, just like create!                                          unsigned short title[150];
133                                          debug("SWP_SHOWWINDOW for %p!", hwnd);                                          int state;
134                                          vchannel_write("CREATE1,0x%p,0x%x", hwnd, 0);  
135                                            vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0);
136    
137                                          // FIXME: SETSTATE                                          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
138    
139                                            vchannel_write("TITLE,0x%x,%s,0x%x", hwnd,
140                                                           vchannel_strfilter_unicode(title), 0);
141    
142                                            if (style & WS_MAXIMIZE)
143                                                    state = 2;
144                                            else if (style & WS_MINIMIZE)
145                                                    state = 1;
146                                            else
147                                                    state = 0;
148    
149                                          update_position(hwnd);                                          update_position(hwnd);
150    
151                                            vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0);
152                                  }                                  }
153    
154                                  if (wp->flags & SWP_HIDEWINDOW)                                  if (wp->flags & SWP_HIDEWINDOW)
155                                          vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0);                                          vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);
156    
157                                  if (!(style & WS_VISIBLE))                                  if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
158                                          break;                                          break;
159    
160                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))
# Line 117  wndproc_hook_proc(int code, WPARAM cur_t Line 162  wndproc_hook_proc(int code, WPARAM cur_t
162    
163                                  if (!(wp->flags & SWP_NOZORDER))                                  if (!(wp->flags & SWP_NOZORDER))
164                                  {                                  {
165                                          vchannel_write("ZCHANGE1,0x%p,0x%p,0x%x",                                          vchannel_write("ZCHANGE,0x%p,0x%p,0x%x",
166                                                         hwnd,                                                         hwnd,
167                                                         wp->flags & SWP_NOACTIVATE ? wp->                                                         wp->flags & SWP_NOACTIVATE ? wp->
168                                                         hwndInsertAfter : 0, 0);                                                         hwndInsertAfter : 0, 0);
# Line 127  wndproc_hook_proc(int code, WPARAM cur_t Line 172  wndproc_hook_proc(int code, WPARAM cur_t
172                          }                          }
173    
174                  case WM_SIZE:                  case WM_SIZE:
175                            if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
176                                    break;
177                          update_position(hwnd);                          update_position(hwnd);
178                          break;                          break;
179    
180                  case WM_MOVE:                  case WM_MOVE:
181                            if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
182                                    break;
183                          update_position(hwnd);                          update_position(hwnd);
184                          break;                          break;
185    
186                  case WM_DESTROY:                  case WM_DESTROY:
187                          vchannel_write("DESTROY1,0x%p,0x%x", hwnd, 0);                          if (!(style & WS_VISIBLE))
188                                    break;
189                            vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);
190                          break;                          break;
191    
192                  default:                  default:
# Line 147  wndproc_hook_proc(int code, WPARAM cur_t Line 198  wndproc_hook_proc(int code, WPARAM cur_t
198  }  }
199    
200  static LRESULT CALLBACK  static LRESULT CALLBACK
201  cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam)  wndprocret_hook_proc(int code, WPARAM cur_thread, LPARAM details)
202  {  {
203          char title[150];          HWND hwnd, parent;
204            UINT msg;
205            WPARAM wparam;
206            LPARAM lparam;
207    
208            LONG style;
209    
210            if (code < 0)
211                    goto end;
212    
213            hwnd = ((CWPRETSTRUCT *) details)->hwnd;
214            msg = ((CWPRETSTRUCT *) details)->message;
215            wparam = ((CWPRETSTRUCT *) details)->wParam;
216            lparam = ((CWPRETSTRUCT *) details)->lParam;
217    
218            style = GetWindowLong(hwnd, GWL_STYLE);
219    
220            /* Docs say that WS_CHILD and WS_POPUP is an illegal combination,
221               but they exist nonetheless. */
222            if ((style & WS_CHILD) && !(style & WS_POPUP))
223                    goto end;
224    
225            switch (msg)
226            {
227                    case WM_SETTEXT:
228                            {
229                                    unsigned short title[150];
230                                    if (!(style & WS_VISIBLE))
231                                            break;
232                                    /* We cannot use the string in lparam because
233                                       we need unicode. */
234                                    GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
235                                    vchannel_write("TITLE,0x%p,%s,0x%x", hwnd,
236                                                   vchannel_strfilter_unicode(title), 0);
237                                    break;
238                            }
239    
240                    default:
241                            break;
242            }
243    
244            if (msg == g_wm_seamless_focus)
245                    SetFocus(hwnd);
246    
247          end:
248            return CallNextHookEx(g_wndprocret_hook, code, cur_thread, details);
249    }
250    
251    static LRESULT CALLBACK
252    cbt_hook_proc(int code, WPARAM wparam, LPARAM lparam)
253    {
254          if (code < 0)          if (code < 0)
255                  goto end;                  goto end;
256    
# Line 162  cbt_hook_proc(int code, WPARAM wparam, L Line 262  cbt_hook_proc(int code, WPARAM wparam, L
262    
263                                  show = LOWORD(lparam);                                  show = LOWORD(lparam);
264    
265                                  if ((show == SW_SHOWMINIMIZED) || (show == SW_MINIMIZE))                                  if ((show == SW_NORMAL) || (show == SW_SHOWNORMAL)
266                                  {                                      || (show == SW_RESTORE))
                                         MessageBox(0,  
                                                    "Minimizing windows is not allowed in this version. Sorry!",  
                                                    "SeamlessRDP", MB_OK);  
                                         return 1;  
                                 }  
   
                                 GetWindowText((HWND) wparam, title, sizeof(title));  
   
                                 /* FIXME: Strip title of dangerous characters */  
   
                                 if (show == SW_SHOWNORMAL)  
267                                          state = 0;                                          state = 0;
268                                  else if (show == SW_SHOWMINIMIZED)                                  else if ((show == SW_MINIMIZE) || (show == SW_SHOWMINIMIZED))
269                                          state = 1;                                          state = 1;
270                                  else if (show == SW_SHOWMAXIMIZED)                                  else if ((show == SW_MAXIMIZE) || (show == SW_SHOWMAXIMIZED))
271                                          state = 2;                                          state = 2;
272                                  vchannel_write("SETSTATE1,0x%p,%s,0x%x,0x%x",                                  else
273                                                 (HWND) wparam, title, state, 0);                                  {
274                                            debug("Unexpected show: %d", show);
275                                            break;
276                                    }
277                                    vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0);
278                                  break;                                  break;
279                          }                          }
280    
# Line 201  SetHooks(void) Line 294  SetHooks(void)
294    
295          if (!g_wndproc_hook)          if (!g_wndproc_hook)
296                  g_wndproc_hook = SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0);                  g_wndproc_hook = SetWindowsHookEx(WH_CALLWNDPROC, wndproc_hook_proc, g_instance, 0);
297    
298            if (!g_wndprocret_hook)
299                    g_wndprocret_hook =
300                            SetWindowsHookEx(WH_CALLWNDPROCRET, wndprocret_hook_proc, g_instance, 0);
301  }  }
302    
303  DLL_EXPORT void  DLL_EXPORT void
# Line 211  RemoveHooks(void) Line 308  RemoveHooks(void)
308    
309          if (g_wndproc_hook)          if (g_wndproc_hook)
310                  UnhookWindowsHookEx(g_wndproc_hook);                  UnhookWindowsHookEx(g_wndproc_hook);
311    
312            if (g_wndprocret_hook)
313                    UnhookWindowsHookEx(g_wndprocret_hook);
314    }
315    
316    DLL_EXPORT void
317    SafeMoveWindow(HWND hwnd, int x, int y, int width, int height)
318    {
319            WaitForSingleObject(g_mutex, INFINITE);
320            g_block_move.left = x;
321            g_block_move.top = y;
322            g_block_move.right = x + width;
323            g_block_move.bottom = y + height;
324            ReleaseMutex(g_mutex);
325    
326            SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
327    
328            WaitForSingleObject(g_mutex, INFINITE);
329            memset(&g_block_move, 0, sizeof(RECT));
330            ReleaseMutex(g_mutex);
331  }  }
332    
333  DLL_EXPORT int  DLL_EXPORT int
# Line 236  DllMain(HINSTANCE hinstDLL, DWORD ul_rea Line 353  DllMain(HINSTANCE hinstDLL, DWORD ul_rea
353                          ++g_instance_count;                          ++g_instance_count;
354                          ReleaseMutex(g_mutex);                          ReleaseMutex(g_mutex);
355    
356                            g_wm_seamless_focus = RegisterWindowMessage(FOCUS_MSG_NAME);
357    
358                          vchannel_open();                          vchannel_open();
359    
360                          break;                          break;

Legend:
Removed from v.1081  
changed lines
  Added in v.1153

  ViewVC Help
Powered by ViewVC 1.1.26