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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26