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