/[winpt]/trunk/PTD/PTD.cpp
ViewVC logotype

Diff of /trunk/PTD/PTD.cpp

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

revision 32 by twoaday, Mon Oct 24 08:03:48 2005 UTC revision 261 by twoaday, Sat Sep 30 10:24:23 2006 UTC
# Line 1  Line 1 
1  /* PTD.cpp - Privacy Tray Dynamic  /* PTD.cpp - Privacy Tray Dynamic
2   *      Copyright (C) 2002-2005 Timo Schulz   *      Copyright (C) 2002-2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
15   *   *
16   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20    
21  #include <windows.h>  #ifdef HAVE_CONFIG_H
22  #include <stdio.h>  #include <config.h>
23  #include <ocidl.h>  #endif
24  #include <olectl.h>  
25    #include <windows.h>
26  #include "wptJPG.h"  #include <stdio.h>
27    #include <ocidl.h>
28  HINSTANCE glob_hinst;  #include <olectl.h>
29    
30  /* We need a special section in the DLL for storing our shared data */  #include "wptJPG.h"
31  #pragma data_seg(".SHARDAT")  
32  static HHOOK cbt_hook = NULL;  HINSTANCE glob_hinst;
33  static DWORD tray_proc_id = 0;  
34  static HWND shell_traywnd = 0;  /* We need a special section in the DLL for storing our shared data.
35  static HWND curr_focus = NULL;     This shared data is required by the hook function because that
36  static HWND rebar_w32 = NULL;     function is called in the context of the actual thread running the
37  static HWND mstask_swc = NULL;     hook and thus we need a way to communicate the result of our hook
38  static HWND systab_c32 = NULL;     function back to us.  We use a shared data segment for this. */
39  #pragma data_seg()  #ifdef __GNUC__
40    #define ATTR_SEC __attribute__((section (".SHARDAT"), shared))
41  static unsigned * journ_keys = 0;  #else
42  static unsigned key_idx = 0;  #define ATTR_SEC
43  static unsigned nkeys = 0;  #pragma data_seg(".SHARDAT")
44  static HHOOK journ_hook = NULL;  #endif
45    static HHOOK cbt_hook      ATTR_SEC = NULL; /* CTB hook handle. */
46    static DWORD tray_proc_id  ATTR_SEC = 0;    /* WinPT PID */
47  static LRESULT CALLBACK  static HWND  shell_traywnd ATTR_SEC = 0;    /* Tray window handle. */
48  PTD_CBT_proc (int code, WPARAM wparam, LPARAM lparam)  static HWND  curr_focus    ATTR_SEC = NULL; /* Current window focus handle. */
49  {  static HWND  rebar_w32     ATTR_SEC = NULL;
50      HWND curr_hwnd = NULL;        static HWND  mstask_swc    ATTR_SEC = NULL;
51      DWORD proc_id = 0;  static HWND  systab_c32    ATTR_SEC = NULL;
52        #ifndef __GNUC__
53      if (code >= 0 && code == HCBT_SETFOCUS) {  #pragma data_seg()
54          /* A window is about to receive the keyboard focus */            #endif
55            #undef ATTR_SEC
56          curr_hwnd = (HWND)wparam;  
57          GetWindowThreadProcessId( curr_hwnd, &proc_id );  static unsigned *journ_keys = 0;
58          /* Only if the current window is different from this window list,  static unsigned key_idx = 0;
59             we set the new focus. For more information read the hint in  static unsigned nkeys = 0;
60             PTD_initialize. */  static HHOOK journ_hook = NULL;     /* Journaling hook  handle. */
61          if ((proc_id != tray_proc_id)  
62               && (curr_hwnd != NULL)  
63               && (curr_hwnd != curr_focus)  void dns_cleanup (void);
64               && (curr_hwnd != shell_traywnd)  
65               && (curr_hwnd != systab_c32)  
66               && (curr_hwnd != mstask_swc)  static LRESULT CALLBACK
67               && (curr_hwnd != rebar_w32)) {  PTD_CBT_proc (int code, WPARAM wparam, LPARAM lparam)
68              curr_focus = curr_hwnd;  {
69          }      HWND curr_hwnd = NULL;      
70      }      DWORD proc_id = 0;
71            
72      return  CallNextHookEx (cbt_hook , code, wparam, lparam);      if (code >= 0 && code == HCBT_SETFOCUS) {
73  }          /* A window is about to receive the keyboard focus */          
74            
75            curr_hwnd = (HWND)wparam;
76  /* Return the last selected window */          GetWindowThreadProcessId( curr_hwnd, &proc_id );
77  HWND          /* Only if the current window is different from this window list,
78  PTD_get_curr_hwnd (void)             we set the new focus. For more information read the hint in
79  {             PTD_initialize. */
80      return curr_focus;          if ((proc_id != tray_proc_id)
81  }               && (curr_hwnd != NULL)
82                 && (curr_hwnd != curr_focus)
83                 && (curr_hwnd != shell_traywnd)
84  /* Initialize CTB hook and try to find the system windows. */               && (curr_hwnd != systab_c32)
85  BOOL               && (curr_hwnd != mstask_swc)
86  PTD_initialize (void)               && (curr_hwnd != rebar_w32)) {
87  {              curr_focus = curr_hwnd;
88      tray_proc_id = GetCurrentProcessId ();          }
89      cbt_hook = SetWindowsHookEx (WH_CBT, PTD_CBT_proc, glob_hinst, 0);      }
90      if (cbt_hook == NULL)      
91          return FALSE;      return  CallNextHookEx (cbt_hook , code, wparam, lparam);
92        }
93      /* Okay, what are we doing here:  
94       * In the past I used the Spy++ application to find out why the old current window  
95       * doesn't work on so much systems. Now we try to use the computer based training  /* Return the last selected window */
96       * hook, more precise the HCBT_SETFOCUS to find out the last window focus. But to  extern "C" HWND
97       * succeed we need to ignore some windows because otherwise our own window or a  PTD_get_curr_hwnd (void)
98       * window from the taskbar would be recognized.  {
99       *      return curr_focus;
100       * Here is what the Spy++ says about the windows order:  }
101       * Shell_TrayWnd  
102       *  \ ReBarWindow32  
103       *                \ MSTaskSwWClass  /* Initialize CTB hook and try to find the system windows. */
104       *                               \ SysTabControl32  extern "C" BOOL
105       *  PTD_initialize (void)
106       * As a result we need to ignore those windows to prevent failured in the code.  {
107       */      tray_proc_id = GetCurrentProcessId ();
108      shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL);      cbt_hook = SetWindowsHookEx (WH_CBT, PTD_CBT_proc, glob_hinst, 0);
109      rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL);      if (cbt_hook == NULL)
110      mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL);          return FALSE;
111      systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL);      
112            /* Okay, what are we doing here:
113      /*_log_box ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p "       * In the past I used the Spy++ application to find out why the old current
114                  "mstask_swc=%p systab_c32=%p",       * window doesn't work on so much systems. Now we try to use the computer
115                  tray_proc_id, cbt_hook, shell_traywnd, rebar_w32,       * based training hook, more precise the HCBT_SETFOCUS to find out the
116                  mstask_swc, systab_c32);*/       * last window focus. But to succeed we need to ignore some windows
117             * because otherwise our own window or a window from the taskbar
118      return TRUE;       * would be recognized.
119  }       *
120         * Here is what the Spy++ says about the windows order:
121         * Shell_TrayWnd
122  /* Remove the CTB hook from the system and reset all variables. */       *  \ ReBarWindow32
123  void       *                \ MSTaskSwWClass
124  PTD_delete (void)       *                               \ SysTabControl32
125  {       *
126      if (!cbt_hook)       * As a result we need to ignore those windows to prevent failured
127          return;       * in the code.
128      UnhookWindowsHookEx (cbt_hook);       */
129      cbt_hook = NULL;      shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL);
130      shell_traywnd = NULL;      rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL);
131      tray_proc_id = 0;      mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL);
132  }      systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL);
133        
134        /*log_foo ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p "
135  /* Return if the CTB hook is currently used. */                 "mstask_swc=%p systab_c32=%p",
136  int                 tray_proc_id, cbt_hook, shell_traywnd, rebar_w32,
137  PTD_is_used (void)                 mstask_swc, systab_c32);*/
138  {      
139      return shell_traywnd && cbt_hook;      return TRUE;
140  }  }
141    
142    
143  static LRESULT CALLBACK  /* Remove the CTB hook from the system and reset all variables. */
144  PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam)  extern "C" void
145  {  PTD_delete (void)
146      LPEVENTMSG em;  {
147            if (!cbt_hook)
148      if (code == HC_SKIP) {          return;
149          key_idx++;      dns_cleanup ();
150          if (key_idx == nkeys) {      UnhookWindowsHookEx (cbt_hook);
151              UnhookWindowsHookEx (journ_hook);      cbt_hook = NULL;
152              journ_hook = NULL;      shell_traywnd = NULL;
153          }      tray_proc_id = 0;
154          return 0;  }
155      }  
156      else if (code == HC_GETNEXT) {  
157          /* Here is an overview what the event message struct can contain:  /* Return if the CTB hook is currently used. */
158           * message - WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN:  extern "C" int
159           * paramL - specifies the virtual key code of the key that was pressed.  PTD_is_used (void)
160           * paramH - specifies the scan code.                              {
161           */      return shell_traywnd && cbt_hook;
162          em = (LPEVENTMSG )lparam;  }
163          if (journ_keys[key_idx] & 0x8000)  
164              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN;  
165          else  static LRESULT CALLBACK
166              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP;  PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam)
167          em->paramL = LOBYTE( journ_keys[key_idx] )  {
168              | (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8);      LPEVENTMSG em;
169          em->paramH = 1;      
170          em->time = GetTickCount ();      if (code == HC_SKIP) {
171          return 0;          key_idx++;
172      }          if (key_idx == nkeys) {
173                    UnhookWindowsHookEx (journ_hook);
174      return CallNextHookEx (journ_hook, code, wparam, lparam);              journ_hook = NULL;
175  } /* PTD_Playback_proc */          }
176            return 0;
177        }
178        else if (code == HC_GETNEXT) {
179  BOOL          /* Here is an overview what the event message struct can contain:
180  PTD_keyb_send (UINT *keys, UINT n_keys)           * message - WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN:
181  {           * paramL - specifies the virtual key code of the key that was pressed.
182      MSG msg;           * paramH - specifies the scan code.                            
183      journ_keys = keys;           */
184      key_idx = 0;          em = (LPEVENTMSG )lparam;
185      nkeys =  n_keys;              if (journ_keys[key_idx] & 0x8000)
186                    em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN;
187      if (journ_hook)          else
188          return FALSE;              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP;
189                em->paramL = LOBYTE( journ_keys[key_idx] )
190      while (GetAsyncKeyState (VK_MENU) & 0x8000              | (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8);
191              || GetAsyncKeyState (VK_SHIFT) & 0x8000          em->paramH = 1;
192              || GetAsyncKeyState (VK_CONTROL) & 0x8000) {          em->time = GetTickCount ();
193          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {          return 0;
194              if (msg.message == WM_QUIT) {      }
195                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam);      
196                  return FALSE;      return CallNextHookEx (journ_hook, code, wparam, lparam);
197              }  }
198              TranslateMessage (&msg);  
199              DispatchMessage (&msg);  
200          }  
201      }  extern "C" BOOL
202      journ_hook = SetWindowsHookEx (WH_JOURNALPLAYBACK,  PTD_keyb_send (UINT *keys, UINT n_keys)
203                                     (HOOKPROC)PTD_playback_proc,  {
204                                     glob_hinst, 0);      MSG msg;
205      if (journ_hook == NULL)      journ_keys = keys;
206          return FALSE;      key_idx = 0;
207            nkeys =  n_keys;    
208      while (journ_hook) {      
209          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {      if (journ_hook)
210              if (msg.message == WM_QUIT) {          return FALSE;
211                  if (journ_hook)      
212                      UnhookWindowsHookEx (journ_hook);      while (GetAsyncKeyState (VK_MENU) & 0x8000
213                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.wParam);              || GetAsyncKeyState (VK_SHIFT) & 0x8000
214              }              || GetAsyncKeyState (VK_CONTROL) & 0x8000) {
215              TranslateMessage (&msg);          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
216              DispatchMessage (&msg);              if (msg.message == WM_QUIT) {
217          }                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam);
218      }                  return FALSE;
219                    }
220      return TRUE;              TranslateMessage (&msg);
221  }              DispatchMessage (&msg);
222            }
223        }
224  const char *      journ_hook = SetWindowsHookEx (WH_JOURNALPLAYBACK,
225  PTD_get_version (void)                                     (HOOKPROC)PTD_playback_proc,
226  {                                     glob_hinst, 0);
227      return "0.8.1";      if (journ_hook == NULL)
228  }          return FALSE;
229        
230        while (journ_hook) {
231  /* Display a JPG picture in the given window at the given point. */          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
232  int              if (msg.message == WM_QUIT) {
233  PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name)                  if (journ_hook)
234  {                      UnhookWindowsHookEx (journ_hook);
235      CJPG jpg;                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.wParam);
236      HDC hdc;              }
237      POINT p2;              TranslateMessage (&msg);
238      BOOL rc;              DispatchMessage (&msg);
239            }
240      rc = jpg.Load (name);      }
241      if (!rc)      
242          return -1;      return TRUE;
243      hdc = GetWindowDC (hwnd);  }
244      rc = jpg.UpdateSizeOnDC (hdc);  
245      if (!rc) {  
246          ReleaseDC (hwnd, hdc);  /* Display a JPG picture in the given window at the given point. */
247          return -2;  extern "C" int
248      }  PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name)
249    {
250      p2.x = jpg.m_Width;      CJPG jpg;
251      p2.y = jpg.m_Height;      HDC hdc;
252      rc = jpg.Show (hdc, p, &p2, 0, 0);      POINT sizewnd;
253        RECT rwnd;
254      ReleaseDC (hwnd, hdc);      BOOL rc;
255      jpg.FreePictureData ();  
256      return rc;      rc = jpg.load (name);
257  }      if (!rc)
258            return -1; /* XXX: use real return code. */
259        hdc = GetWindowDC (hwnd);
260                        rc = jpg.updateSizeOnDC (hdc);
261  BOOL WINAPI      if (!rc) {
262  DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv)          ReleaseDC (hwnd, hdc);
263  {          return -2; /* XXX: use real return code. */
264      switch (reason)  {      }
265      case DLL_PROCESS_ATTACH:  
266          glob_hinst = hinst;      GetWindowRect (hwnd, &rwnd);
267          break;      sizewnd.x = rwnd.right - rwnd.left;
268      case DLL_THREAD_ATTACH:        sizewnd.y = rwnd.bottom - rwnd.top;
269          break;      rc = jpg.show (hdc, p, &sizewnd, 0, 0);
270      case DLL_THREAD_DETACH:    
271          break;      ReleaseDC (hwnd, hdc);
272      case DLL_PROCESS_DETACH:      jpg.freePictureData ();
273          break;      return rc;
274      }  }
275      return TRUE;  
276  }                  
277    extern "C" BOOL WINAPI
278    DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv)
279    {
280        switch (reason)  {
281        case DLL_PROCESS_ATTACH:
282            glob_hinst = hinst;
283            break;
284        case DLL_THREAD_ATTACH:  
285            break;
286        case DLL_THREAD_DETACH:  
287            break;
288        case DLL_PROCESS_DETACH:
289            break;
290        }
291        return TRUE;
292    }

Legend:
Removed from v.32  
changed lines
  Added in v.261

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26