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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26