/[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 1165 by ossman_, Mon Mar 20 12:28:03 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          if (!GetWindowRect(hwnd, &rect))          if (!GetWindowRect(hwnd, &rect))
85          {          {
# Line 76  update_position(HWND hwnd) Line 88  update_position(HWND hwnd)
88          }          }
89    
90          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
91            blocked_hwnd = g_block_move_hwnd;
92            serial = g_block_move_serial;
93          memcpy(&blocked, &g_block_move, sizeof(RECT));          memcpy(&blocked, &g_block_move, sizeof(RECT));
94          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
95    
96          if ((rect.left == blocked.left) && (rect.top == blocked.top)          if ((hwnd == blocked_hwnd) && (rect.left == blocked.left) && (rect.top == blocked.top)
97              && (rect.right == blocked.right) && (rect.bottom == blocked.bottom))              && (rect.right == blocked.right) && (rect.bottom == blocked.bottom))
98                  return;                  vchannel_write("ACK", "%u", serial);
99            else
100                    vchannel_write("POSITION", "0x%p,%d,%d,%d,%d,0x%x",
101                                   hwnd,
102                                   rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
103                                   0);
104    }
105    
106          vchannel_write("POSITION,0x%p,%d,%d,%d,%d,0x%x",  static void
107                         hwnd,  update_zorder(HWND hwnd)
108                         rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);  {
109            HWND behind;
110            HWND block_hwnd, block_behind;
111            unsigned int serial;
112    
113            WaitForSingleObject(g_mutex, INFINITE);
114            serial = g_blocked_zchange_serial;
115            block_hwnd = g_blocked_zchange[0];
116            block_behind = g_blocked_zchange[1];
117            ReleaseMutex(g_mutex);
118    
119            behind = GetNextWindow(hwnd, GW_HWNDPREV);
120            while (behind)
121            {
122                    LONG style;
123    
124                    style = GetWindowLong(behind, GWL_STYLE);
125    
126                    if ((!(style & WS_CHILD) || (style & WS_POPUP)) && (style & WS_VISIBLE))
127                            break;
128    
129                    behind = GetNextWindow(behind, GW_HWNDPREV);
130            }
131    
132            if ((hwnd == block_hwnd) && (behind == block_behind))
133                    vchannel_write("ACK", "%u", serial);
134            else
135                    vchannel_write("ZCHANGE", "0x%p,0x%p,0x%x", hwnd, behind, 0);
136  }  }
137    
138  static LRESULT CALLBACK  static LRESULT CALLBACK
# Line 124  wndproc_hook_proc(int code, WPARAM cur_t Line 171  wndproc_hook_proc(int code, WPARAM cur_t
171    
172          switch (msg)          switch (msg)
173          {          {
   
174                  case WM_WINDOWPOSCHANGED:                  case WM_WINDOWPOSCHANGED:
175                          {                          {
176                                  WINDOWPOS *wp = (WINDOWPOS *) lparam;                                  WINDOWPOS *wp = (WINDOWPOS *) lparam;
# Line 134  wndproc_hook_proc(int code, WPARAM cur_t Line 180  wndproc_hook_proc(int code, WPARAM cur_t
180                                          unsigned short title[150];                                          unsigned short title[150];
181                                          int state;                                          int state;
182    
183                                          vchannel_write("CREATE,0x%p,0x%p,0x%x", hwnd, parent, 0);                                          vchannel_write("CREATE", "0x%p,0x%p,0x%x", hwnd, parent, 0);
184    
185                                          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));                                          GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
186    
187                                          vchannel_write("TITLE,0x%x,%s,0x%x", hwnd,                                          vchannel_write("TITLE", "0x%x,%s,0x%x", hwnd,
188                                                         vchannel_strfilter_unicode(title), 0);                                                         vchannel_strfilter_unicode(title), 0);
189    
190                                          if (style & WS_MAXIMIZE)                                          if (style & WS_MAXIMIZE)
# Line 150  wndproc_hook_proc(int code, WPARAM cur_t Line 196  wndproc_hook_proc(int code, WPARAM cur_t
196    
197                                          update_position(hwnd);                                          update_position(hwnd);
198    
199                                          vchannel_write("STATE,0x%p,0x%x,0x%x", hwnd, state, 0);                                          vchannel_write("STATE", "0x%p,0x%x,0x%x", hwnd, state, 0);
200                                  }                                  }
201    
202                                  if (wp->flags & SWP_HIDEWINDOW)                                  if (wp->flags & SWP_HIDEWINDOW)
203                                          vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);                                          vchannel_write("DESTROY", "0x%p,0x%x", hwnd, 0);
204    
205                                  if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))                                  if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
206                                          break;                                          break;
# Line 162  wndproc_hook_proc(int code, WPARAM cur_t Line 208  wndproc_hook_proc(int code, WPARAM cur_t
208                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))                                  if (!(wp->flags & SWP_NOMOVE && wp->flags & SWP_NOSIZE))
209                                          update_position(hwnd);                                          update_position(hwnd);
210    
                                 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);  
                                         }  
                                 }  
   
211                                  break;                                  break;
212                          }                          }
213    
# Line 198  wndproc_hook_proc(int code, WPARAM cur_t Line 226  wndproc_hook_proc(int code, WPARAM cur_t
226                  case WM_DESTROY:                  case WM_DESTROY:
227                          if (!(style & WS_VISIBLE))                          if (!(style & WS_VISIBLE))
228                                  break;                                  break;
229                          vchannel_write("DESTROY,0x%p,0x%x", hwnd, 0);                          vchannel_write("DESTROY", "0x%p,0x%x", hwnd, 0);
230                          break;                          break;
231    
232                  default:                  default:
# Line 245  wndprocret_hook_proc(int code, WPARAM cu Line 273  wndprocret_hook_proc(int code, WPARAM cu
273    
274          switch (msg)          switch (msg)
275          {          {
276                    case WM_WINDOWPOSCHANGED:
277                            {
278                                    WINDOWPOS *wp = (WINDOWPOS *) lparam;
279    
280                                    if (!(style & WS_VISIBLE) || (style & WS_MINIMIZE))
281                                            break;
282    
283                                    if (!(wp->flags & SWP_NOZORDER))
284                                            update_zorder(hwnd);
285    
286                                    break;
287                            }
288    
289    
290                  case WM_SETTEXT:                  case WM_SETTEXT:
291                          {                          {
292                                  unsigned short title[150];                                  unsigned short title[150];
# Line 253  wndprocret_hook_proc(int code, WPARAM cu Line 295  wndprocret_hook_proc(int code, WPARAM cu
295                                  /* We cannot use the string in lparam because                                  /* We cannot use the string in lparam because
296                                     we need unicode. */                                     we need unicode. */
297                                  GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));                                  GetWindowTextW(hwnd, title, sizeof(title) / sizeof(*title));
298                                  vchannel_write("TITLE,0x%p,%s,0x%x", hwnd,                                  vchannel_write("TITLE", "0x%p,%s,0x%x", hwnd,
299                                                 vchannel_strfilter_unicode(title), 0);                                                 vchannel_strfilter_unicode(title), 0);
300                                  break;                                  break;
301                          }                          }
# Line 268  wndprocret_hook_proc(int code, WPARAM cu Line 310  wndprocret_hook_proc(int code, WPARAM cu
310                     way to solve this. */                     way to solve this. */
311                  if ((GetActiveWindow() != hwnd) && !parent)                  if ((GetActiveWindow() != hwnd) && !parent)
312                          SetActiveWindow(hwnd);                          SetActiveWindow(hwnd);
313    
314                    vchannel_write("ACK", "%u", g_blocked_focus_serial);
315          }          }
316    
317        end:        end:
# Line 284  cbt_hook_proc(int code, WPARAM wparam, L Line 328  cbt_hook_proc(int code, WPARAM wparam, L
328          {          {
329                  case HCBT_MINMAX:                  case HCBT_MINMAX:
330                          {                          {
331                                  int show, state;                                  int show, state, blocked;
332                                    HWND blocked_hwnd;
333                                    unsigned int serial;
334    
335                                    WaitForSingleObject(g_mutex, INFINITE);
336                                    blocked_hwnd = g_blocked_state_hwnd;
337                                    serial = g_blocked_state_serial;
338                                    blocked = g_blocked_state;
339                                    ReleaseMutex(g_mutex);
340    
341                                  show = LOWORD(lparam);                                  show = LOWORD(lparam);
342    
# Line 300  cbt_hook_proc(int code, WPARAM wparam, L Line 352  cbt_hook_proc(int code, WPARAM wparam, L
352                                          debug("Unexpected show: %d", show);                                          debug("Unexpected show: %d", show);
353                                          break;                                          break;
354                                  }                                  }
355                                  vchannel_write("STATE,0x%p,0x%x,0x%x", (HWND) wparam, state, 0);  
356                                    if ((blocked_hwnd == (HWND) wparam) && (blocked == state))
357                                            vchannel_write("ACK", "%u", serial);
358                                    else
359                                            vchannel_write("STATE", "0x%p,0x%x,0x%x", (HWND) wparam,
360                                                           state, 0);
361    
362                                  break;                                  break;
363                          }                          }
364    
# Line 340  RemoveHooks(void) Line 398  RemoveHooks(void)
398  }  }
399    
400  DLL_EXPORT void  DLL_EXPORT void
401  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)
402  {  {
403            RECT rect;
404    
405            vchannel_block();
406    
407            if (!GetWindowRect(hwnd, &rect))
408            {
409                    vchannel_unblock();
410                    debug("GetWindowRect failed!\n");
411                    return;
412            }
413    
414            if ((rect.left == x) && (rect.top == y) && (rect.right == x + width)
415                && (rect.bottom == y + height))
416            {
417                    vchannel_write("ACK", "%u", serial);
418                    vchannel_unblock();
419            }
420    
421          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
422            g_block_move_hwnd = hwnd;
423            g_block_move_serial = serial;
424          g_block_move.left = x;          g_block_move.left = x;
425          g_block_move.top = y;          g_block_move.top = y;
426          g_block_move.right = x + width;          g_block_move.right = x + width;
427          g_block_move.bottom = y + height;          g_block_move.bottom = y + height;
428          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
429    
430            vchannel_unblock();
431    
432          SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);          SetWindowPos(hwnd, NULL, x, y, width, height, SWP_NOACTIVATE | SWP_NOZORDER);
433    
434          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
435            g_block_move_hwnd = NULL;
436          memset(&g_block_move, 0, sizeof(RECT));          memset(&g_block_move, 0, sizeof(RECT));
437          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
438  }  }
439    
440  DLL_EXPORT void  DLL_EXPORT void
441  SafeZChange(HWND hwnd, HWND behind)  SafeZChange(unsigned int serial, HWND hwnd, HWND behind)
442  {  {
         if (behind == NULL)  
                 behind = HWND_TOP;  
   
443          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
444            g_blocked_zchange_serial = serial;
445          g_blocked_zchange[0] = hwnd;          g_blocked_zchange[0] = hwnd;
446          g_blocked_zchange[1] = behind;          g_blocked_zchange[1] = behind;
447          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
448    
449            if (behind == NULL)
450                    behind = HWND_TOP;
451    
452          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);
453    
454          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
# Line 376  SafeZChange(HWND hwnd, HWND behind) Line 458  SafeZChange(HWND hwnd, HWND behind)
458  }  }
459    
460  DLL_EXPORT void  DLL_EXPORT void
461  SafeFocus(HWND hwnd)  SafeFocus(unsigned int serial, HWND hwnd)
462  {  {
463          WaitForSingleObject(g_mutex, INFINITE);          WaitForSingleObject(g_mutex, INFINITE);
464            g_blocked_focus_serial = serial;
465          g_blocked_focus = hwnd;          g_blocked_focus = hwnd;
466          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
467    
# Line 389  SafeFocus(HWND hwnd) Line 472  SafeFocus(HWND hwnd)
472          ReleaseMutex(g_mutex);          ReleaseMutex(g_mutex);
473  }  }
474    
475    DLL_EXPORT void
476    SafeSetState(unsigned int serial, HWND hwnd, int state)
477    {
478            LONG style;
479            int curstate;
480    
481            vchannel_block();
482    
483            style = GetWindowLong(hwnd, GWL_STYLE);
484    
485            if (style & WS_MAXIMIZE)
486                    curstate = 2;
487            else if (style & WS_MINIMIZE)
488                    curstate = 1;
489            else
490                    curstate = 0;
491    
492            if (state == curstate)
493            {
494                    vchannel_write("ACK", "%u", serial);
495                    vchannel_unblock();
496            }
497    
498            WaitForSingleObject(g_mutex, INFINITE);
499            g_blocked_state_hwnd = hwnd;
500            g_blocked_state_serial = serial;
501            g_blocked_state = state;
502            ReleaseMutex(g_mutex);
503    
504            vchannel_unblock();
505    
506            if (state == 0)
507                    ShowWindow(hwnd, SW_RESTORE);
508            else if (state == 1)
509                    ShowWindow(hwnd, SW_MINIMIZE);
510            else if (state == 2)
511                    ShowWindow(hwnd, SW_MAXIMIZE);
512            else
513                    debug("Invalid state %d sent.", state);
514    
515            WaitForSingleObject(g_mutex, INFINITE);
516            g_blocked_state_hwnd = NULL;
517            g_blocked_state = -1;
518            ReleaseMutex(g_mutex);
519    }
520    
521  DLL_EXPORT int  DLL_EXPORT int
522  GetInstanceCount()  GetInstanceCount()
523  {  {

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

  ViewVC Help
Powered by ViewVC 1.1.26