/[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 1134 by ossman_, Wed Mar 15 13:19:54 2006 UTC revision 1186 by ossman_, Wed Mar 22 11:56:46 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    HWND g_block_move_hwnd SHARED = NULL;
49    unsigned int g_block_move_serial SHARED = 0;
50    RECT g_block_move SHARED = { 0, 0, 0, 0 };
51    
52    unsigned int g_blocked_zchange_serial SHARED = 0;
53    HWND g_blocked_zchange[2] SHARED = { NULL, NULL };
54    
55    unsigned int g_blocked_focus_serial SHARED = 0;
56    HWND g_blocked_focus SHARED = NULL;
57    
58    unsigned int g_blocked_state_serial SHARED = 0;
59    HWND g_blocked_state_hwnd SHARED = NULL;
60    int g_blocked_state SHARED = -1;
61    
62  #pragma data_seg ()  #pragma data_seg ()
63    
64  #pragma comment(linker, "/section:SHAREDDATA,rws")  #pragma comment(linker, "/section:SHAREDDATA,rws")
65    
66    #define FOCUS_MSG_NAME "WM_SEAMLESS_FOCUS"
67    static UINT g_wm_seamless_focus;
68    
69  static HHOOK g_cbt_hook = NULL;  static HHOOK g_cbt_hook = NULL;
70  static HHOOK g_wndproc_hook = NULL;  static HHOOK g_wndproc_hook = NULL;
71  static HHOOK g_wndprocret_hook = NULL;  static HHOOK g_wndprocret_hook = NULL;
# Line 53  static HANDLE g_mutex = NULL; Line 77  static HANDLE g_mutex = NULL;
77  static void  static void
78  update_position(HWND hwnd)  update_position(HWND hwnd)
79  {  {
80          RECT rect;          RECT rect, blocked;
81            HWND blocked_hwnd;
82            unsigned int serial;
83    
84            WaitForSingleObject(g_mutex, INFINITE);
85            blocked_hwnd = g_block_move_hwnd;
86            serial = g_block_move_serial;
87            memcpy(&blocked, &g_block_move, sizeof(RECT));
88            ReleaseMutex(g_mutex);
89    
90            vchannel_block();
91    
92          if (!GetWindowRect(hwnd, &rect))          if (!GetWindowRect(hwnd, &rect))
93          {          {
94                  debug("GetWindowRect failed!\n");                  debug("GetWindowRect failed!\n");
95                  return;                  goto end;
96          }          }
97    
98          vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x",          if ((hwnd == blocked_hwnd) && (rect.left == blocked.left) && (rect.top == blocked.top)
99                && (rect.right == blocked.right) && (rect.bottom == blocked.bottom))
100                    goto end;
101    
102            vchannel_write("POSITION", "0x%08lx,%d,%d,%d,%d,0x%08x",
103                         hwnd,                         hwnd,
104                         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);
105    
106          end:
107            vchannel_unblock();
108    }
109    
110    static void
111    update_zorder(HWND hwnd)
112    {
113            HWND behind;
114            HWND block_hwnd, block_behind;
115            unsigned int serial;
116    
117            WaitForSingleObject(g_mutex, INFINITE);
118            serial = g_blocked_zchange_serial;
119            block_hwnd = g_blocked_zchange[0];
120            block_behind = g_blocked_zchange[1];
121            ReleaseMutex(g_mutex);
122    
123            vchannel_block();
124    
125            behind = GetNextWindow(hwnd, GW_HWNDPREV);
126            while (behind)
127            {
128                    LONG style;
129    
130                    style = GetWindowLong(behind, GWL_STYLE);
131    
132                    if ((!(style & WS_CHILD) || (style & WS_POPUP)) && (style & WS_VISIBLE))
133                            break;
134    
135                    behind = GetNextWindow(behind, GW_HWNDPREV);
136            }
137    
138            if ((hwnd == block_hwnd) && (behind == block_behind))
139                    vchannel_write("ACK", "%u", serial);
140            else
141                    vchannel_write("ZCHANGE", "0x%08lx,0x%08lx,0x%08x", hwnd, behind, 0);
142    
143            vchannel_unblock();
144    }
145    
146    static HWND
147    get_parent(HWND hwnd)
148    {
149            LONG style;
150            HWND parent;
151    
152            style = GetWindowLong(hwnd, GWL_STYLE);
153    
154            if (style & (WS_POPUP | DS_MODALFRAME))
155            {
156                    parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT);
157    
158                    if (parent)
159                    {
160                            style = GetWindowLong(parent, GWL_STYLE);
161                            if (((style & WS_CHILD) && !(style & WS_POPUP)) || !(style & WS_VISIBLE))
162                                    parent = NULL;
163                    }
164    
165                    if (!parent)
166                            parent = GetWindow(hwnd, GW_OWNER);
167    
168                    if (parent)
169                    {
170                            style = GetWindowLong(parent, GWL_STYLE);
171                            if (((style & WS_CHILD) && !(style & WS_POPUP)) || !(style & WS_VISIBLE))
172                                    parent = NULL;
173                    }
174    
175                    if (!parent)
176                            parent = (HWND) - 1;
177            }
178            else
179                    parent = NULL;
180    
181            return parent;
182  }  }
183    
184  static LRESULT CALLBACK  static LRESULT CALLBACK
# Line 91  wndproc_hook_proc(int code, WPARAM cur_t Line 206  wndproc_hook_proc(int code, WPARAM cur_t
206          if ((style & WS_CHILD) && !(style & WS_POPUP))          if ((style & WS_CHILD) && !(style & WS_POPUP))
207                  goto end;                  goto end;
208    
209          if (style & WS_POPUP)          parent = get_parent(hwnd);
         {  
                 parent = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT);  
                 if (!parent)  
                         parent = (HWND) - 1;  
         }  
         else  
                 parent = NULL;  
210    
211          switch (msg)          switch (msg)
212          {          {
   
213                  case WM_WINDOWPOSCHANGED:                  case WM_WINDOWPOSCHANGED:
214                          {                          {
215                                  WINDOWPOS *wp = (WINDOWPOS *) lparam;                                  WINDOWPOS *wp = (WINDOWPOS *) lparam;
# Line 111  wndproc_hook_proc(int code, WPARAM cur_t Line 218  wndproc_hook_proc(int code, WPARAM cur_t
218                                  {                                  {
219                                          unsigned short title[150];                                          unsigned short title[150];
220                                          int state;                                          int state;
221                                            DWORD pid;
222                                            int flags;
223    
224                                          vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0);                                          GetWindowThreadProcessId(hwnd, &pid);
225    
226                                            flags = 0;
227                                            if (style & DS_MODALFRAME)
228                                                    flags |= SEAMLESS_CREATE_MODAL;
229    
230                                            vchannel_write("CREATE", "0x%08lx,0x%08lx,0x%08lx,0x%08x",
231                                                           (long) hwnd, (long) pid, (long) parent,
232                                                           flags);
233    
234                                          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));                                          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
235    
236                                          vchannel_write("TITLE,0x%x,%s,0x%x", hwnd,                                          vchannel_write("TITLE", "0x%08lx,%s,0x%08x", hwnd,
237                                                         vchannel_strfilter_unicode(title), 0);                                                         vchannel_strfilter_unicode(title), 0);
238    
239                                          if (style & WS_MAXIMIZE)                                          if (style & WS_MAXIMIZE)
# Line 128  wndproc_hook_proc(int code, WPARAM cur_t Line 245  wndproc_hook_proc(int code, WPARAM cur_t
245    
246                                          update_position(hwnd);                                          update_position(hwnd);
247    
248                                          vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0);                                          vchannel_write("STATE", "0x%08lx,0x%08x,0x%08x", hwnd,
249                                                           state, 0);
250                                  }                                  }
251    
252                                  if (wp->flags & SWP_HIDEWINDOW)                                  if (wp->flags & SWP_HIDEWINDOW)
253                                          vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);                                          vchannel_write("DESTROY", "0x%08lx,0x%08x", hwnd, 0);
254    
255                                  if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))                                  if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
256                                          break;                                          break;
# Line 140  wndproc_hook_proc(int code, WPARAM cur_t Line 258  wndproc_hook_proc(int code, WPARAM cur_t
258                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))
259                                          update_position(hwnd);                                          update_position(hwnd);
260    
                                 if (!(wp->flags & SWP_NOZORDER))  
                                 {  
                                         vchannel_write("ZCHANGE,0x%p,0x%p,0x%x",  
                                                        hwnd,  
                                                        wp->flags & SWP_NOACTIVATE ? wp->  
                                                        hwndInsertAfter : 0, 0);  
                                 }  
   
261                                  break;                                  break;
262                          }                          }
263    
# Line 166  wndproc_hook_proc(int code, WPARAM cur_t Line 276  wndproc_hook_proc(int code, WPARAM cur_t
276                  case WM_DESTROY:                  case WM_DESTROY:
277                          if (!(style & WS_VISIBLE))                          if (!(style & WS_VISIBLE))
278                                  break;                                  break;
279                          vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);                          vchannel_write("DESTROY", "0x%08lx,0x%08x", hwnd, 0);
280                          break;                          break;
281    
282                  default:                  default:
# Line 202  wndprocret_hook_proc(int code, WPARAM cu Line 312  wndprocret_hook_proc(int code, WPARAM cu
312          if ((style & WS_CHILD) && !(style & WS_POPUP))          if ((style & WS_CHILD) && !(style & WS_POPUP))
313                  goto end;                  goto end;
314    
315            parent = get_parent(hwnd);
316    
317          switch (msg)          switch (msg)
318          {          {
319                    case WM_WINDOWPOSCHANGED:
320                            {
321                                    WINDOWPOS *wp = (WINDOWPOS *) lparam;
322    
323                                    if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
324                                            break;
325    
326                                    if (!(wp->flags & SWP_NOZORDER))
327                                            update_zorder(hwnd);
328    
329                                    break;
330                            }
331    
332    
333                  case WM_SETTEXT:                  case WM_SETTEXT:
334                          {                          {
335                                  unsigned short title[150];                                  unsigned short title[150];
# Line 212  wndprocret_hook_proc(int code, WPARAM cu Line 338  wndprocret_hook_proc(int code, WPARAM cu
338                                  /* We cannot use the string in lparam because                                  /* We cannot use the string in lparam because
339                                     we need unicode. */                                     we need unicode. */
340                                  GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));                                  GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
341                                  vchannel_write("TITLE,0x%p,%s,0x%x", hwnd,                                  vchannel_write("TITLE", "0x%08lx,%s,0x%08x", hwnd,
342                                                 vchannel_strfilter_unicode(title), 0);                                                 vchannel_strfilter_unicode(title), 0);
343                                  break;                                  break;
344                          }                          }
# Line 221  wndprocret_hook_proc(int code, WPARAM cu Line 347  wndprocret_hook_proc(int code, WPARAM cu
347                          break;                          break;
348          }          }
349    
350            if (msg == g_wm_seamless_focus)
351            {
352                    /* FIXME: SetForegroundWindow() kills menus. Need to find a
353                       clean way to solve this. */
354                    if ((GetForegroundWindow() != hwnd) && !parent)
355                            SetForegroundWindow(hwnd);
356    
357                    vchannel_write("ACK", "%u", g_blocked_focus_serial);
358            }
359    
360        end:        end:
361          return CallNextHookEx(g_wndprocret_hook, code, cur_thread, details);          return CallNextHookEx(g_wndprocret_hook, code, cur_thread, details);
362  }  }
# Line 235  cbt_hook_proc(int code, WPARAM wparam, L Line 371  cbt_hook_proc(int code, WPARAM wparam, L
371          {          {
372                  case HCBT_MINMAX:                  case HCBT_MINMAX:
373                          {                          {
374                                  int show, state;                                  int show, state, blocked;
375                                    HWND blocked_hwnd;
376                                    unsigned int serial;
377    
378                                    WaitForSingleObject(g_mutex, INFINITE);
379                                    blocked_hwnd = g_blocked_state_hwnd;
380                                    serial = g_blocked_state_serial;
381                                    blocked = g_blocked_state;
382                                    ReleaseMutex(g_mutex);
383    
384                                  show = LOWORD(lparam);                                  show = LOWORD(lparam);
385    
# Line 251  cbt_hook_proc(int code, WPARAM wparam, L Line 395  cbt_hook_proc(int code, WPARAM wparam, L
395                                          debug("Unexpected show: %d", show);                                          debug("Unexpected show: %d", show);
396                                          break;                                          break;
397                                  }                                  }
398                                  vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0);  
399                                    if ((blocked_hwnd == (HWND) wparam) && (blocked == state))
400                                            vchannel_write("ACK", "%u", serial);
401                                    else
402                                            vchannel_write("STATE", "0x%08lx,0x%08x,0x%08x",
403                                                           (HWND) wparam, state, 0);
404    
405                                  break;                                  break;
406                          }                          }
407    
# Line 290  RemoveHooks(void) Line 440  RemoveHooks(void)
440                  UnhookWindowsHookEx(g_wndprocret_hook);                  UnhookWindowsHookEx(g_wndprocret_hook);
441  }  }
442    
443    DLL_EXPORT void
444    SafeMoveWindow(unsigned int serial, HWND hwnd, int x, int y, int width, int height)
445    {
446            RECT rect;
447    
448            WaitForSingleObject(g_mutex, INFINITE);
449            g_block_move_hwnd = hwnd;
450            g_block_move_serial = serial;
451            g_block_move.left = x;
452            g_block_move.top = y;
453            g_block_move.right = x + width;
454            g_block_move.bottom = y + height;
455            ReleaseMutex(g_mutex);
456    
457            SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
458    
459            vchannel_write("ACK", "%u", serial);
460    
461            if (!GetWindowRect(hwnd, &rect))
462                    debug("GetWindowRect failed!\n");
463            else if ((rect.left != x) || (rect.top != y) || (rect.right != x + width)
464                     || (rect.bottom != y + height))
465                    update_position(hwnd);
466    
467            WaitForSingleObject(g_mutex, INFINITE);
468            g_block_move_hwnd = NULL;
469            memset(&g_block_move, 0, sizeof(RECT));
470            ReleaseMutex(g_mutex);
471    }
472    
473    DLL_EXPORT void
474    SafeZChange(unsigned int serial, HWND hwnd, HWND behind)
475    {
476            WaitForSingleObject(g_mutex, INFINITE);
477            g_blocked_zchange_serial = serial;
478            g_blocked_zchange[0] = hwnd;
479            g_blocked_zchange[1] = behind;
480            ReleaseMutex(g_mutex);
481    
482            if (behind == NULL)
483                    behind = HWND_TOP;
484    
485            SetWindowPos(hwnd, behind, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
486    
487            WaitForSingleObject(g_mutex, INFINITE);
488            g_blocked_zchange[0] = NULL;
489            g_blocked_zchange[1] = NULL;
490            ReleaseMutex(g_mutex);
491    }
492    
493    DLL_EXPORT void
494    SafeFocus(unsigned int serial, HWND hwnd)
495    {
496            WaitForSingleObject(g_mutex, INFINITE);
497            g_blocked_focus_serial = serial;
498            g_blocked_focus = hwnd;
499            ReleaseMutex(g_mutex);
500    
501            SendMessage(hwnd, g_wm_seamless_focus, 0, 0);
502    
503            WaitForSingleObject(g_mutex, INFINITE);
504            g_blocked_focus = NULL;
505            ReleaseMutex(g_mutex);
506    }
507    
508    DLL_EXPORT void
509    SafeSetState(unsigned int serial, HWND hwnd, int state)
510    {
511            LONG style;
512            int curstate;
513    
514            vchannel_block();
515    
516            style = GetWindowLong(hwnd, GWL_STYLE);
517    
518            if (style & WS_MAXIMIZE)
519                    curstate = 2;
520            else if (style & WS_MINIMIZE)
521                    curstate = 1;
522            else
523                    curstate = 0;
524    
525            if (state == curstate)
526            {
527                    vchannel_write("ACK", "%u", serial);
528                    vchannel_unblock();
529                    return;
530            }
531    
532            WaitForSingleObject(g_mutex, INFINITE);
533            g_blocked_state_hwnd = hwnd;
534            g_blocked_state_serial = serial;
535            g_blocked_state = state;
536            ReleaseMutex(g_mutex);
537    
538            vchannel_unblock();
539    
540            if (state == 0)
541                    ShowWindow(hwnd, SW_RESTORE);
542            else if (state == 1)
543                    ShowWindow(hwnd, SW_MINIMIZE);
544            else if (state == 2)
545                    ShowWindow(hwnd, SW_MAXIMIZE);
546            else
547                    debug("Invalid state %d sent.", state);
548    
549            WaitForSingleObject(g_mutex, INFINITE);
550            g_blocked_state_hwnd = NULL;
551            g_blocked_state = -1;
552            ReleaseMutex(g_mutex);
553    }
554    
555  DLL_EXPORT int  DLL_EXPORT int
556  GetInstanceCount()  GetInstanceCount()
557  {  {
# Line 313  DllMain(HINSTANCE hinstDLL, DWORD ul_rea Line 575  DllMain(HINSTANCE hinstDLL, DWORD ul_rea
575                          ++g_instance_count;                          ++g_instance_count;
576                          ReleaseMutex(g_mutex);                          ReleaseMutex(g_mutex);
577    
578                            g_wm_seamless_focus = RegisterWindowMessage(FOCUS_MSG_NAME);
579    
580                          vchannel_open();                          vchannel_open();
581    
582                          break;                          break;

Legend:
Removed from v.1134  
changed lines
  Added in v.1186

  ViewVC Help
Powered by ViewVC 1.1.26