/[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 2 by twoaday, Mon Jan 31 11:02:21 2005 UTC revision 46 by werner, Fri Oct 28 12:57:05 2005 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-2005 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    #endif
24  HINSTANCE glob_hinst;  
25    #include <windows.h>
26  /* We need a special section in the DLL for storing our shared data */  #include <windows.h>
27  #pragma data_seg(".SHARDAT")  #include <stdio.h>
28  static HHOOK cbt_hook = NULL;  #include <ocidl.h>
29  static DWORD tray_proc_id = 0;  #include <olectl.h>
30  static HWND shell_traywnd = 0;  
31  static HWND curr_focus = NULL;  #include "wptJPG.h"
32  static HWND rebar_w32 = NULL;  
33  static HWND mstask_swc = NULL;  HINSTANCE glob_hinst;
34  static HWND systab_c32 = NULL;  
35  #pragma data_seg()  /* We need a special section in the DLL for storing our shared data */
36    #pragma data_seg(".SHARDAT")
37  static unsigned * journ_keys = 0;  static HHOOK    cbt_hook = NULL;    /* CTB hook handle. */
38  static unsigned key_idx = 0;  static DWORD    tray_proc_id = 0;   /* WinPT PID */
39  static unsigned nkeys = 0;  static HWND     shell_traywnd = 0;  /* Tray window handle. */
40  static HHOOK journ_hook = NULL;  static HWND     curr_focus = NULL;  /* Current window focus handle. */
41    static HWND     rebar_w32 = NULL;
42    static HWND     mstask_swc = NULL;
43  static LRESULT CALLBACK  static HWND     systab_c32 = NULL;
44  PTD_CBT_proc (int code, WPARAM wparam, LPARAM lparam)  #pragma data_seg()
45  {  
46      HWND curr_hwnd = NULL;        static unsigned *journ_keys = 0;
47      DWORD proc_id = 0;  static unsigned key_idx = 0;
48        static unsigned nkeys = 0;
49      if (code >= 0 && code == HCBT_SETFOCUS) {  static HHOOK journ_hook = NULL;     /* Journaling hook  handle. */
50          /* A window is about to receive the keyboard focus */            
51            
52          curr_hwnd = (HWND)wparam;  static LRESULT CALLBACK
53          GetWindowThreadProcessId( curr_hwnd, &proc_id );  PTD_CBT_proc (int code, WPARAM wparam, LPARAM lparam)
54          /* Only if the current window is different from this window list,  {
55             we set the new focus. For more information read the hint in      HWND curr_hwnd = NULL;      
56             PTD_initialize. */      DWORD proc_id = 0;
57          if ((proc_id != tray_proc_id)      
58               && (curr_hwnd != NULL)      if (code >= 0 && code == HCBT_SETFOCUS) {
59               && (curr_hwnd != curr_focus)          /* A window is about to receive the keyboard focus */          
60               && (curr_hwnd != shell_traywnd)          
61               && (curr_hwnd != systab_c32)          curr_hwnd = (HWND)wparam;
62               && (curr_hwnd != mstask_swc)          GetWindowThreadProcessId( curr_hwnd, &proc_id );
63               && (curr_hwnd != rebar_w32)) {          /* Only if the current window is different from this window list,
64              curr_focus = curr_hwnd;             we set the new focus. For more information read the hint in
65          }             PTD_initialize. */
66      }          if ((proc_id != tray_proc_id)
67                     && (curr_hwnd != NULL)
68      return  CallNextHookEx (cbt_hook , code, wparam, lparam);               && (curr_hwnd != curr_focus)
69  } /* PTD_CBT_proc */               && (curr_hwnd != shell_traywnd)
70                 && (curr_hwnd != systab_c32)
71                 && (curr_hwnd != mstask_swc)
72  HWND               && (curr_hwnd != rebar_w32)) {
73  PTD_get_curr_hwnd (void)              curr_focus = curr_hwnd;
74  {          }
75      return curr_focus;      }
76  } /* PTD_get_curr_hwnd */      
77        return  CallNextHookEx (cbt_hook , code, wparam, lparam);
78    }
79  BOOL  
80  PTD_initialize (void)  
81  {  /* Return the last selected window */
82      tray_proc_id = GetCurrentProcessId( );  extern "C" HWND
83      cbt_hook = SetWindowsHookEx( WH_CBT, PTD_CBT_proc, glob_hinst, 0 );  PTD_get_curr_hwnd (void)
84      if( cbt_hook == NULL )                {
85          return FALSE;      return curr_focus;
86        }
87      /* Okay, what are we doing here:  
88       * In the past I used the Spy++ application to find out why the old current window  
89       * doesn't work on so much systems. Now we try to use the computer based training  /* Initialize CTB hook and try to find the system windows. */
90       * hook, more precise the HCBT_SETFOCUS to find out the last window focus. But to  extern "C" BOOL
91       * succeed we need to ignore some windows because otherwise our own window or a  PTD_initialize (void)
92       * window from the taskbar would be recognized.  {
93       *      tray_proc_id = GetCurrentProcessId ();
94       * Here is what the Spy++ says about the windows order:      cbt_hook = SetWindowsHookEx (WH_CBT, PTD_CBT_proc, glob_hinst, 0);
95       * Shell_TrayWnd      if (cbt_hook == NULL)
96       *  \ ReBarWindow32          return FALSE;
97       *                \ MSTaskSwWClass      
98       *                               \ SysTabControl32      /* Okay, what are we doing here:
99       *       * In the past I used the Spy++ application to find out why the old current
100       * As a result we need to ignore those windows to prevent failured in the code.       * window doesn't work on so much systems. Now we try to use the computer
101       */       * based training hook, more precise the HCBT_SETFOCUS to find out the
102      shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL);       * last window focus. But to succeed we need to ignore some windows
103      rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL);       * because otherwise our own window or a window from the taskbar
104      mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL);       * would be recognized.
105      systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL);       *
106             * Here is what the Spy++ says about the windows order:
107      /*_log_box ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p "       * Shell_TrayWnd
108                  "mstask_swc=%p systab_c32=%p",       *  \ ReBarWindow32
109                  tray_proc_id, cbt_hook, shell_traywnd, rebar_w32,       *                \ MSTaskSwWClass
110                  mstask_swc, systab_c32);*/       *                               \ SysTabControl32
111             *
112      return TRUE;       * As a result we need to ignore those windows to prevent failured
113  } /* PTD_initialize */       * in the code.
114         */
115        shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL);
116  void      rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL);
117  PTD_delete (void)      mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL);
118  {      systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL);
119      if (!cbt_hook)      
120          return;      /*log_foo ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p "
121      UnhookWindowsHookEx (cbt_hook);                 "mstask_swc=%p systab_c32=%p",
122      cbt_hook = NULL;                 tray_proc_id, cbt_hook, shell_traywnd, rebar_w32,
123      shell_traywnd = NULL;                 mstask_swc, systab_c32);*/
124      tray_proc_id = 0;      
125  } /* PTD_delete */      return TRUE;
126    }
127    
128  int  
129  PTD_is_used (void)  /* Remove the CTB hook from the system and reset all variables. */
130  {  extern "C" void
131      return shell_traywnd && cbt_hook;  PTD_delete (void)
132  } /* PTD_is_used */  {
133        if (!cbt_hook)
134            return;
135  LRESULT CALLBACK      UnhookWindowsHookEx (cbt_hook);
136  PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam)      cbt_hook = NULL;
137  {      shell_traywnd = NULL;
138      LPEVENTMSG em;      tray_proc_id = 0;
139        }
140      if (code == HC_SKIP) {  
141          key_idx++;  
142          if (key_idx == nkeys) {  /* Return if the CTB hook is currently used. */
143              UnhookWindowsHookEx (journ_hook);  extern "C" int
144              journ_hook = NULL;  PTD_is_used (void)
145          }  {
146          return 0;      return shell_traywnd && cbt_hook;
147      }  }
148      else if (code == HC_GETNEXT) {  
149          /* Here is an overview what the event message struct can contain:  
150           * message - WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN:  static LRESULT CALLBACK
151           * paramL - specifies the virtual key code of the key that was pressed.  PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam)
152           * paramH - specifies the scan code.                              {
153           */      LPEVENTMSG em;
154          em = (LPEVENTMSG )lparam;      
155          if (journ_keys[key_idx] & 0x8000)      if (code == HC_SKIP) {
156              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN;          key_idx++;
157          else          if (key_idx == nkeys) {
158              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP;              UnhookWindowsHookEx (journ_hook);
159          em->paramL = LOBYTE( journ_keys[key_idx] )              journ_hook = NULL;
160              | (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8);          }
161          em->paramH = 1;          return 0;
162          em->time = GetTickCount ();      }
163          return 0;      else if (code == HC_GETNEXT) {
164      }          /* Here is an overview what the event message struct can contain:
165                 * message - WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN:
166      return CallNextHookEx (journ_hook, code, wparam, lparam);           * paramL - specifies the virtual key code of the key that was pressed.
167  } /* PTD_Playback_proc */           * paramH - specifies the scan code.                            
168             */
169            em = (LPEVENTMSG )lparam;
170  BOOL          if (journ_keys[key_idx] & 0x8000)
171  PTD_keyb_send (UINT * keys, UINT n_keys)              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN;
172  {          else
173      MSG msg;              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP;
174            em->paramL = LOBYTE( journ_keys[key_idx] )
175      journ_keys = keys;              | (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8);
176      key_idx = 0;          em->paramH = 1;
177      nkeys =  n_keys;              em->time = GetTickCount ();
178                return 0;
179      if (journ_hook)      }
180          return FALSE;      
181            return CallNextHookEx (journ_hook, code, wparam, lparam);
182      while (GetAsyncKeyState (VK_MENU) & 0x8000  } /* PTD_Playback_proc */
183              || GetAsyncKeyState (VK_SHIFT) & 0x8000  
184              || GetAsyncKeyState (VK_CONTROL) & 0x8000) {  
185          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {  
186              if (msg.message == WM_QUIT) {  extern "C" BOOL
187                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam);  PTD_keyb_send (UINT *keys, UINT n_keys)
188                  return FALSE;  {
189              }      MSG msg;
190              TranslateMessage (&msg);      journ_keys = keys;
191              DispatchMessage (&msg);      key_idx = 0;
192          }      nkeys =  n_keys;    
193      }      
194      journ_hook = SetWindowsHookEx (WH_JOURNALPLAYBACK,      if (journ_hook)
195                                     (HOOKPROC)PTD_playback_proc,          return FALSE;
196                                     glob_hinst, 0);      
197      if (journ_hook == NULL)      while (GetAsyncKeyState (VK_MENU) & 0x8000
198          return FALSE;              || GetAsyncKeyState (VK_SHIFT) & 0x8000
199                    || GetAsyncKeyState (VK_CONTROL) & 0x8000) {
200      while (journ_hook) {          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
201          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {              if (msg.message == WM_QUIT) {
202              if (msg.message == WM_QUIT) {                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam);
203                  if (journ_hook)                  return FALSE;
204                      UnhookWindowsHookEx (journ_hook);              }
205                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.wParam);              TranslateMessage (&msg);
206              }              DispatchMessage (&msg);
207              TranslateMessage (&msg);          }
208              DispatchMessage (&msg);      }
209          }      journ_hook = SetWindowsHookEx (WH_JOURNALPLAYBACK,
210      }                                     (HOOKPROC)PTD_playback_proc,
211                                           glob_hinst, 0);
212      return TRUE;      if (journ_hook == NULL)
213  } /* PTD_keyb_send */          return FALSE;
214        
215        while (journ_hook) {
216  const char *          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
217  PTD_get_version (void)              if (msg.message == WM_QUIT) {
218  {                  if (journ_hook)
219      return "0.8.0";                      UnhookWindowsHookEx (journ_hook);
220  } /* PTD_get_version */                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.wParam);
221                }
222                                TranslateMessage (&msg);
223  BOOL WINAPI              DispatchMessage (&msg);
224  DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv)          }
225  {      }
226      switch (reason)  {      
227      case DLL_PROCESS_ATTACH:      return TRUE;
228          glob_hinst = hinst;  }
229          break;  
230      case DLL_THREAD_ATTACH:    
231          break;  /* Return version of the PTD library. */
232      case DLL_THREAD_DETACH:    extern "C" const char *
233          break;  PTD_get_version (void)
234      case DLL_PROCESS_DETACH:  {
235          break;      return PACKAGE_VERSION;
236      }  }
237      return TRUE;  
238  } /* DllMain */  
239    /* Display a JPG picture in the given window at the given point. */
240    extern "C" int
241    PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name)
242    {
243        CJPG jpg;
244        HDC hdc;
245        POINT p2;
246        BOOL rc;
247    
248        rc = jpg.load (name);
249        if (!rc)
250            return -1; /* XXX: use real return code. */
251        hdc = GetWindowDC (hwnd);
252        rc = jpg.updateSizeOnDC (hdc);
253        if (!rc) {
254            ReleaseDC (hwnd, hdc);
255            return -2; /* XXX: use real return code. */
256        }
257    
258        p2.x = jpg.m_Width;
259        p2.y = jpg.m_Height;
260        rc = jpg.show (hdc, p, &p2, 0, 0);
261    
262        ReleaseDC (hwnd, hdc);
263        jpg.freePictureData ();
264        return rc;
265    }
266    
267    
268                    
269    BOOL WINAPI
270    DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv)
271    {
272        switch (reason)  {
273        case DLL_PROCESS_ATTACH:
274            glob_hinst = hinst;
275            break;
276        case DLL_THREAD_ATTACH:  
277            break;
278        case DLL_THREAD_DETACH:  
279            break;
280        case DLL_PROCESS_DETACH:
281            break;
282        }
283        return TRUE;
284    }
285    

Legend:
Removed from v.2  
changed lines
  Added in v.46

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26