/[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 1158 by ossman_, Fri Mar 17 13:11:20 2006 UTC revision 1167 by ossman_, Mon Mar 20 14:35:02 2006 UTC
# Line 45  Line 45 
45  int g_instance_count SHARED = 0;  int g_instance_count SHARED = 0;
46    
47  // blocks for locally generated events  // 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 };  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 };  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;  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")
# Line 68  static void Line 78  static void
78  update_position(HWND hwnd)  update_position(HWND hwnd)
79  {  {
80          RECT rect, blocked;          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          WaitForSingleObject(g_mutex, INFINITE);          if ((hwnd == blocked_hwnd) && (rect.left == blocked.left) && (rect.top == blocked.top)
         memcpy(&blocked, &g_block_move, sizeof(RECT));  
         ReleaseMutex(g_mutex);  
   
         if ((rect.left == blocked.left) && (rect.top == blocked.top)  
99              && (rect.right == blocked.right) && (rect.bottom == blocked.bottom))              && (rect.right == blocked.right) && (rect.bottom == blocked.bottom))
100                  return;                  goto end;
101    
102          vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x",          vchannel_write("POSITION", "0x%p,%d,%d,%d,%d,0x%x",
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%p,0x%p,0x%x", hwnd, behind, 0);
142    
143            vchannel_unblock();
144  }  }
145    
146  static LRESULT CALLBACK  static LRESULT CALLBACK
# Line 124  wndproc_hook_proc(int code, WPARAM cur_t Line 179  wndproc_hook_proc(int code, WPARAM cur_t
179    
180          switch (msg)          switch (msg)
181          {          {
   
182                  case WM_WINDOWPOSCHANGED:                  case WM_WINDOWPOSCHANGED:
183                          {                          {
184                                  WINDOWPOS *wp = (WINDOWPOS *) lparam;                                  WINDOWPOS *wp = (WINDOWPOS *) lparam;
# Line 134  wndproc_hook_proc(int code, WPARAM cur_t Line 188  wndproc_hook_proc(int code, WPARAM cur_t
188                                          unsigned short title[150];                                          unsigned short title[150];
189                                          int state;                                          int state;
190    
191                                          vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0);                                          vchannel_write("CREATE", "0x%p,0x%p,0x%x", hwnd, parent, 0);
192    
193                                          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));                                          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
194    
195                                          vchannel_write("TITLE,0x%x,%s,0x%x", hwnd,                                          vchannel_write("TITLE", "0x%x,%s,0x%x", hwnd,
196                                                         vchannel_strfilter_unicode(title), 0);                                                         vchannel_strfilter_unicode(title), 0);
197    
198                                          if (style & WS_MAXIMIZE)                                          if (style & WS_MAXIMIZE)
# Line 150  wndproc_hook_proc(int code, WPARAM cur_t Line 204  wndproc_hook_proc(int code, WPARAM cur_t
204    
205                                          update_position(hwnd);                                          update_position(hwnd);
206    
207                                          vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0);                                          vchannel_write("STATE", "0x%p,0x%x,0x%x", hwnd, state, 0);
208                                  }                                  }
209    
210                                  if (wp->flags & SWP_HIDEWINDOW)                                  if (wp->flags & SWP_HIDEWINDOW)
211                                          vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);                                          vchannel_write("DESTROY", "0x%p,0x%x", hwnd, 0);
212    
213                                  if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))                                  if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
214                                          break;                                          break;
# Line 162  wndproc_hook_proc(int code, WPARAM cur_t Line 216  wndproc_hook_proc(int code, WPARAM cur_t
216                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))
217                                          update_position(hwnd);                                          update_position(hwnd);
218    
                                 if (!(wp->flags & SWP_NOZORDER))  
                                 {  
                                         HWND block_hwnd, block_behind;  
                                         WaitForSingleObject(g_mutex, INFINITE);  
                                         block_hwnd = g_blocked_zchange[0];  
                                         block_behind = g_blocked_zchange[1];  
                                         ReleaseMutex(g_mutex);  
   
                                         if ((hwnd != block_hwnd)  
                                             || (wp->hwndInsertAfter != block_behind))  
                                         {  
                                                 vchannel_write("ZCHANGE,0x%p,0x%p,0x%x",  
                                                                hwnd,  
                                                                wp->flags & SWP_NOACTIVATE ? wp->  
                                                                hwndInsertAfter : 0, 0);  
                                         }  
                                 }  
   
219                                  break;                                  break;
220                          }                          }
221    
# Line 198  wndproc_hook_proc(int code, WPARAM cur_t Line 234  wndproc_hook_proc(int code, WPARAM cur_t
234                  case WM_DESTROY:                  case WM_DESTROY:
235                          if (!(style & WS_VISIBLE))                          if (!(style & WS_VISIBLE))
236                                  break;                                  break;
237                          vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);                          vchannel_write("DESTROY", "0x%p,0x%x", hwnd, 0);
238                          break;                          break;
239    
240                  default:                  default:
# Line 245  wndprocret_hook_proc(int code, WPARAM cu Line 281  wndprocret_hook_proc(int code, WPARAM cu
281    
282          switch (msg)          switch (msg)
283          {          {
284                    case WM_WINDOWPOSCHANGED:
285                            {
286                                    WINDOWPOS *wp = (WINDOWPOS *) lparam;
287    
288                                    if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
289                                            break;
290    
291                                    if (!(wp->flags & SWP_NOZORDER))
292                                            update_zorder(hwnd);
293    
294                                    break;
295                            }
296    
297    
298                  case WM_SETTEXT:                  case WM_SETTEXT:
299                          {                          {
300                                  unsigned short title[150];                                  unsigned short title[150];
# Line 253  wndprocret_hook_proc(int code, WPARAM cu Line 303  wndprocret_hook_proc(int code, WPARAM cu
303                                  /* We cannot use the string in lparam because                                  /* We cannot use the string in lparam because
304                                     we need unicode. */                                     we need unicode. */
305                                  GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));                                  GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
306                                  vchannel_write("TITLE,0x%p,%s,0x%x", hwnd,                                  vchannel_write("TITLE", "0x%p,%s,0x%x", hwnd,
307                                                 vchannel_strfilter_unicode(title), 0);                                                 vchannel_strfilter_unicode(title), 0);
308                                  break;                                  break;
309                          }                          }
# Line 268  wndprocret_hook_proc(int code, WPARAM cu Line 318  wndprocret_hook_proc(int code, WPARAM cu
318                     way to solve this. */                     way to solve this. */
319                  if ((GetActiveWindow() != hwnd) && !parent)                  if ((GetActiveWindow() != hwnd) && !parent)
320                          SetActiveWindow(hwnd);                          SetActiveWindow(hwnd);
321    
322                    vchannel_write("ACK", "%u", g_blocked_focus_serial);
323          }          }
324    
325        end:        end:
# Line 284  cbt_hook_proc(int code, WPARAM wparam, L Line 336  cbt_hook_proc(int code, WPARAM wparam, L
336          {          {
337                  case HCBT_MINMAX:                  case HCBT_MINMAX:
338                          {                          {
339                                  int show, state;                                  int show, state, blocked;
340                                    HWND blocked_hwnd;
341                                    unsigned int serial;
342    
343                                    WaitForSingleObject(g_mutex, INFINITE);
344                                    blocked_hwnd = g_blocked_state_hwnd;
345                                    serial = g_blocked_state_serial;
346                                    blocked = g_blocked_state;
347                                    ReleaseMutex(g_mutex);
348    
349                                  show = LOWORD(lparam);                                  show = LOWORD(lparam);
350    
# Line 300  cbt_hook_proc(int code, WPARAM wparam, L Line 360  cbt_hook_proc(int code, WPARAM wparam, L
360                                          debug("Unexpected show: %d", show);                                          debug("Unexpected show: %d", show);
361                                          break;                                          break;
362                                  }                                  }
363                                  vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0);  
364                                    if ((blocked_hwnd == (HWND) wparam) && (blocked == state))
365                                            vchannel_write("ACK", "%u", serial);
366                                    else
367                                            vchannel_write("STATE", "0x%p,0x%x,0x%x", (HWND) wparam,
368                                                           state, 0);
369    
370                                  break;                                  break;
371                          }                          }
372    
# Line 340  RemoveHooks(void) Line 406  RemoveHooks(void)
406  }  }
407    
408  DLL_EXPORT void  DLL_EXPORT void
409  SafeMoveWindow(HWND hwnd, int x, int y, int width, int height)  SafeMoveWindow(unsigned int serial, HWND hwnd, int x, int y, int width, int height)
410  {  {
411            RECT rect;
412    
413          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
414            g_block_move_hwnd = hwnd;
415            g_block_move_serial = serial;
416          g_block_move.left = x;          g_block_move.left = x;
417          g_block_move.top = y;          g_block_move.top = y;
418          g_block_move.right = x + width;          g_block_move.right = x + width;
# Line 351  SafeMoveWindow(HWND hwnd, int x, int y, Line 421  SafeMoveWindow(HWND hwnd, int x, int y,
421    
422          SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);          SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
423    
424            vchannel_write("ACK", "%u", serial);
425    
426            if (!GetWindowRect(hwnd, &rect))
427                    debug("GetWindowRect failed!\n");
428            else if ((rect.left != x) || (rect.top != y) || (rect.right != x + width)
429                     || (rect.bottom != y + height))
430                    update_position(hwnd);
431    
432          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
433            g_block_move_hwnd = NULL;
434          memset(&g_block_move, 0, sizeof(RECT));          memset(&g_block_move, 0, sizeof(RECT));
435          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
436  }  }
437    
438  DLL_EXPORT void  DLL_EXPORT void
439  SafeZChange(HWND hwnd, HWND behind)  SafeZChange(unsigned int serial, HWND hwnd, HWND behind)
440  {  {
         if (behind == NULL)  
                 behind = HWND_TOP;  
   
441          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
442            g_blocked_zchange_serial = serial;
443          g_blocked_zchange[0] = hwnd;          g_blocked_zchange[0] = hwnd;
444          g_blocked_zchange[1] = behind;          g_blocked_zchange[1] = behind;
445          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
446    
447            if (behind == NULL)
448                    behind = HWND_TOP;
449    
450          SetWindowPos(hwnd, behind, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);          SetWindowPos(hwnd, behind, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
451    
452          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
# Line 376  SafeZChange(HWND hwnd, HWND behind) Line 456  SafeZChange(HWND hwnd, HWND behind)
456  }  }
457    
458  DLL_EXPORT void  DLL_EXPORT void
459  SafeFocus(HWND hwnd)  SafeFocus(unsigned int serial, HWND hwnd)
460  {  {
461          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
462            g_blocked_focus_serial = serial;
463          g_blocked_focus = hwnd;          g_blocked_focus = hwnd;
464          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
465    
# Line 389  SafeFocus(HWND hwnd) Line 470  SafeFocus(HWND hwnd)
470          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
471  }  }
472    
473    DLL_EXPORT void
474    SafeSetState(unsigned int serial, HWND hwnd, int state)
475    {
476            LONG style;
477            int curstate;
478    
479            vchannel_block();
480    
481            style = GetWindowLong(hwnd, GWL_STYLE);
482    
483            if (style & WS_MAXIMIZE)
484                    curstate = 2;
485            else if (style & WS_MINIMIZE)
486                    curstate = 1;
487            else
488                    curstate = 0;
489    
490            if (state == curstate)
491            {
492                    vchannel_write("ACK", "%u", serial);
493                    vchannel_unblock();
494                    return;
495            }
496    
497            WaitForSingleObject(g_mutex, INFINITE);
498            g_blocked_state_hwnd = hwnd;
499            g_blocked_state_serial = serial;
500            g_blocked_state = state;
501            ReleaseMutex(g_mutex);
502    
503            vchannel_unblock();
504    
505            if (state == 0)
506                    ShowWindow(hwnd, SW_RESTORE);
507            else if (state == 1)
508                    ShowWindow(hwnd, SW_MINIMIZE);
509            else if (state == 2)
510                    ShowWindow(hwnd, SW_MAXIMIZE);
511            else
512                    debug("Invalid state %d sent.", state);
513    
514            WaitForSingleObject(g_mutex, INFINITE);
515            g_blocked_state_hwnd = NULL;
516            g_blocked_state = -1;
517            ReleaseMutex(g_mutex);
518    }
519    
520  DLL_EXPORT int  DLL_EXPORT int
521  GetInstanceCount()  GetInstanceCount()
522  {  {

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

  ViewVC Help
Powered by ViewVC 1.1.26