/[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 45 by twoaday, Tue Oct 25 07:46:20 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  #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;    /* CTB hook handle. */  
33  static DWORD    tray_proc_id = 0;   /* WinPT PID */  HINSTANCE glob_hinst;
34  static HWND     shell_traywnd = 0;  /* Tray window handle. */  
35  static HWND     curr_focus = NULL;  /* Current window focus handle. */  /* 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;     /* Journaling hook  handle. */  #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      cbt_hook = SetWindowsHookEx (WH_CBT, PTD_CBT_proc, glob_hinst, 0);
95       * window doesn't work on so much systems. Now we try to use the computer      if (cbt_hook == NULL)
96       * based training hook, more precise the HCBT_SETFOCUS to find out the          return FALSE;
97       * last window focus. But to succeed we need to ignore some windows      
98       * because otherwise our own window or a window from the taskbar      /* Okay, what are we doing here:
99       * would be recognized.       * In the past I used the Spy++ application to find out why the old current
100       *       * window doesn't work on so much systems. Now we try to use the computer
101       * Here is what the Spy++ says about the windows order:       * based training hook, more precise the HCBT_SETFOCUS to find out the
102       * Shell_TrayWnd       * last window focus. But to succeed we need to ignore some windows
103       *  \ ReBarWindow32       * because otherwise our own window or a window from the taskbar
104       *                \ MSTaskSwWClass       * would be recognized.
105       *                               \ SysTabControl32       *
106       *       * Here is what the Spy++ says about the windows order:
107       * As a result we need to ignore those windows to prevent failured       * Shell_TrayWnd
108       * in the code.       *  \ ReBarWindow32
109       */       *                \ MSTaskSwWClass
110      shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL);       *                               \ SysTabControl32
111      rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL);       *
112      mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL);       * As a result we need to ignore those windows to prevent failured
113      systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL);       * in the code.
114             */
115      /*log_foo ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p "      shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL);
116                 "mstask_swc=%p systab_c32=%p",      rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL);
117                 tray_proc_id, cbt_hook, shell_traywnd, rebar_w32,      mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL);
118                 mstask_swc, systab_c32);*/      systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL);
119            
120      return TRUE;      /*log_foo ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p "
121  }                 "mstask_swc=%p systab_c32=%p",
122                   tray_proc_id, cbt_hook, shell_traywnd, rebar_w32,
123                   mstask_swc, systab_c32);*/
124  /* Remove the CTB hook from the system and reset all variables. */      
125  void      return TRUE;
126  PTD_delete (void)  }
127  {  
128      if (!cbt_hook)  
129          return;  /* Remove the CTB hook from the system and reset all variables. */
130      UnhookWindowsHookEx (cbt_hook);  extern "C" void
131      cbt_hook = NULL;  PTD_delete (void)
132      shell_traywnd = NULL;  {
133      tray_proc_id = 0;      if (!cbt_hook)
134  }          return;
135        UnhookWindowsHookEx (cbt_hook);
136        cbt_hook = NULL;
137  /* Return if the CTB hook is currently used. */      shell_traywnd = NULL;
138  int      tray_proc_id = 0;
139  PTD_is_used (void)  }
140  {  
141      return shell_traywnd && cbt_hook;  
142  }  /* Return if the CTB hook is currently used. */
143    extern "C" int
144    PTD_is_used (void)
145  static LRESULT CALLBACK  {
146  PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam)      return shell_traywnd && cbt_hook;
147  {  }
148      LPEVENTMSG em;  
149        
150      if (code == HC_SKIP) {  static LRESULT CALLBACK
151          key_idx++;  PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam)
152          if (key_idx == nkeys) {  {
153              UnhookWindowsHookEx (journ_hook);      LPEVENTMSG em;
154              journ_hook = NULL;      
155          }      if (code == HC_SKIP) {
156          return 0;          key_idx++;
157      }          if (key_idx == nkeys) {
158      else if (code == HC_GETNEXT) {              UnhookWindowsHookEx (journ_hook);
159          /* Here is an overview what the event message struct can contain:              journ_hook = NULL;
160           * message - WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN:          }
161           * paramL - specifies the virtual key code of the key that was pressed.          return 0;
162           * paramH - specifies the scan code.                                  }
163           */      else if (code == HC_GETNEXT) {
164          em = (LPEVENTMSG )lparam;          /* Here is an overview what the event message struct can contain:
165          if (journ_keys[key_idx] & 0x8000)           * message - WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN:
166              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN;           * paramL - specifies the virtual key code of the key that was pressed.
167          else           * paramH - specifies the scan code.                            
168              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP;           */
169          em->paramL = LOBYTE( journ_keys[key_idx] )          em = (LPEVENTMSG )lparam;
170              | (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8);          if (journ_keys[key_idx] & 0x8000)
171          em->paramH = 1;              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN;
172          em->time = GetTickCount ();          else
173          return 0;              em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP;
174      }          em->paramL = LOBYTE( journ_keys[key_idx] )
175                    | (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8);
176      return CallNextHookEx (journ_hook, code, wparam, lparam);          em->paramH = 1;
177  } /* PTD_Playback_proc */          em->time = GetTickCount ();
178            return 0;
179        }
180        
181  BOOL      return CallNextHookEx (journ_hook, code, wparam, lparam);
182  PTD_keyb_send (UINT *keys, UINT n_keys)  } /* PTD_Playback_proc */
183  {  
184      MSG msg;  
185      journ_keys = keys;  
186      key_idx = 0;  extern "C" BOOL
187      nkeys =  n_keys;      PTD_keyb_send (UINT *keys, UINT n_keys)
188        {
189      if (journ_hook)      MSG msg;
190          return FALSE;      journ_keys = keys;
191            key_idx = 0;
192      while (GetAsyncKeyState (VK_MENU) & 0x8000      nkeys =  n_keys;    
193              || GetAsyncKeyState (VK_SHIFT) & 0x8000      
194              || GetAsyncKeyState (VK_CONTROL) & 0x8000) {      if (journ_hook)
195          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {          return FALSE;
196              if (msg.message == WM_QUIT) {      
197                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam);      while (GetAsyncKeyState (VK_MENU) & 0x8000
198                  return FALSE;              || GetAsyncKeyState (VK_SHIFT) & 0x8000
199              }              || GetAsyncKeyState (VK_CONTROL) & 0x8000) {
200              TranslateMessage (&msg);          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
201              DispatchMessage (&msg);              if (msg.message == WM_QUIT) {
202          }                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam);
203      }                  return FALSE;
204      journ_hook = SetWindowsHookEx (WH_JOURNALPLAYBACK,              }
205                                     (HOOKPROC)PTD_playback_proc,              TranslateMessage (&msg);
206                                     glob_hinst, 0);              DispatchMessage (&msg);
207      if (journ_hook == NULL)          }
208          return FALSE;      }
209            journ_hook = SetWindowsHookEx (WH_JOURNALPLAYBACK,
210      while (journ_hook) {                                     (HOOKPROC)PTD_playback_proc,
211          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {                                     glob_hinst, 0);
212              if (msg.message == WM_QUIT) {      if (journ_hook == NULL)
213                  if (journ_hook)          return FALSE;
214                      UnhookWindowsHookEx (journ_hook);      
215                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.wParam);      while (journ_hook) {
216              }          if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
217              TranslateMessage (&msg);              if (msg.message == WM_QUIT) {
218              DispatchMessage (&msg);                  if (journ_hook)
219          }                      UnhookWindowsHookEx (journ_hook);
220      }                  PostMessage (msg.hwnd, msg.message, msg.wParam, msg.wParam);
221                    }
222      return TRUE;              TranslateMessage (&msg);
223  }              DispatchMessage (&msg);
224            }
225        }
226  /* Return version of the PTD library. */      
227  const char *      return TRUE;
228  PTD_get_version (void)  }
229  {  
230      return "0.8.1";  
231  }  /* Return version of the PTD library. */
232    extern "C" const char *
233    PTD_get_version (void)
234  /* Display a JPG picture in the given window at the given point. */  {
235  int      return PACKAGE_VERSION;
236  PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name)  }
237  {  
238      CJPG jpg;  
239      HDC hdc;  /* Display a JPG picture in the given window at the given point. */
240      POINT p2;  extern "C" int
241      BOOL rc;  PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name)
242    {
243      rc = jpg.load (name);      CJPG jpg;
244      if (!rc)      HDC hdc;
245          return -1; /* XXX: use real return code. */      POINT p2;
246      hdc = GetWindowDC (hwnd);      BOOL rc;
247      rc = jpg.updateSizeOnDC (hdc);  
248      if (!rc) {      rc = jpg.load (name);
249          ReleaseDC (hwnd, hdc);      if (!rc)
250          return -2; /* XXX: use real return code. */          return -1; /* XXX: use real return code. */
251      }      hdc = GetWindowDC (hwnd);
252        rc = jpg.updateSizeOnDC (hdc);
253      p2.x = jpg.m_Width;      if (!rc) {
254      p2.y = jpg.m_Height;          ReleaseDC (hwnd, hdc);
255      rc = jpg.show (hdc, p, &p2, 0, 0);          return -2; /* XXX: use real return code. */
256        }
257      ReleaseDC (hwnd, hdc);  
258      jpg.freePictureData ();      p2.x = jpg.m_Width;
259      return rc;      p2.y = jpg.m_Height;
260  }      rc = jpg.show (hdc, p, &p2, 0, 0);
261    
262        ReleaseDC (hwnd, hdc);
263                        jpg.freePictureData ();
264  BOOL WINAPI      return rc;
265  DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv)  }
266  {  
267      switch (reason)  {  
268      case DLL_PROCESS_ATTACH:                  
269          glob_hinst = hinst;  BOOL WINAPI
270          break;  DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv)
271      case DLL_THREAD_ATTACH:    {
272          break;      switch (reason)  {
273      case DLL_THREAD_DETACH:        case DLL_PROCESS_ATTACH:
274          break;          glob_hinst = hinst;
275      case DLL_PROCESS_DETACH:          break;
276          break;      case DLL_THREAD_ATTACH:  
277      }          break;
278      return TRUE;      case DLL_THREAD_DETACH:  
279  }          break;
280        case DLL_PROCESS_DETACH:
281            break;
282        }
283        return TRUE;
284    }
285    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26