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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26