1 |
/* PTD.cpp - Privacy Tray Dynamic |
/* PTD.cpp - Privacy Tray Dynamic |
2 |
* Copyright (C) 2002-2005 Timo Schulz |
* Copyright (C) 2002-2009, 2012 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
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. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
15 |
*/ |
*/ |
|
|
|
16 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
#include <config.h> |
18 |
#endif |
#endif |
19 |
|
|
20 |
#include <windows.h> |
#include <windows.h> |
|
#include <windows.h> |
|
21 |
#include <stdio.h> |
#include <stdio.h> |
|
#include <ocidl.h> |
|
|
#include <olectl.h> |
|
|
|
|
|
#include "wptJPG.h" |
|
22 |
|
|
23 |
|
/* Reference to the DLL module handle */ |
24 |
HINSTANCE glob_hinst; |
HINSTANCE glob_hinst; |
25 |
|
|
26 |
/* We need a special section in the DLL for storing our shared data */ |
/* We need a special section in the DLL for storing our shared data. |
27 |
|
This shared data is required by the hook function because that |
28 |
|
function is called in the context of the actual thread running the |
29 |
|
hook and thus we need a way to communicate the result of our hook |
30 |
|
function back to us. We use a shared data segment for this. */ |
31 |
|
#ifdef __GNUC__ |
32 |
|
#define ATTR_SEC __attribute__((section (".SHARDAT"), shared)) |
33 |
|
#else |
34 |
|
#define ATTR_SEC |
35 |
#pragma data_seg(".SHARDAT") |
#pragma data_seg(".SHARDAT") |
36 |
static HHOOK cbt_hook = NULL; /* CTB hook handle. */ |
#endif |
37 |
static DWORD tray_proc_id = 0; /* WinPT PID */ |
static HHOOK cbt_hook ATTR_SEC = NULL; /* CTB hook handle. */ |
38 |
static HWND shell_traywnd = 0; /* Tray window handle. */ |
static DWORD tray_proc_id ATTR_SEC = 0; /* WinPT PID */ |
39 |
static HWND curr_focus = NULL; /* Current window focus handle. */ |
static HWND shell_traywnd ATTR_SEC = 0; /* Tray window handle. */ |
40 |
static HWND rebar_w32 = NULL; |
static HWND curr_focus ATTR_SEC = NULL; /* Current window focus handle. */ |
41 |
static HWND mstask_swc = NULL; |
static HWND rebar_w32 ATTR_SEC = NULL; |
42 |
static HWND systab_c32 = NULL; |
static HWND mstask_swc ATTR_SEC = NULL; |
43 |
|
static HWND systab_c32 ATTR_SEC = NULL; |
44 |
|
#ifndef __GNUC__ |
45 |
#pragma data_seg() |
#pragma data_seg() |
46 |
|
#endif |
47 |
|
#undef ATTR_SEC |
48 |
|
|
49 |
|
/* Static variables that are used to record and replay key strokes */ |
50 |
static unsigned *journ_keys = 0; |
static unsigned *journ_keys = 0; |
51 |
static unsigned key_idx = 0; |
static unsigned keyidx = 0; |
52 |
static unsigned nkeys = 0; |
static unsigned nkeys = 0; |
53 |
static HHOOK journ_hook = NULL; /* Journaling hook handle. */ |
static HHOOK journ_hook = NULL; /* Journaling hook handle. */ |
54 |
|
|
55 |
|
|
56 |
|
/* Callback function for the Computer Based Training (CBT) hook */ |
57 |
static LRESULT CALLBACK |
static LRESULT CALLBACK |
58 |
PTD_CBT_proc (int code, WPARAM wparam, LPARAM lparam) |
PTD_CBT_proc (int code, WPARAM wparam, LPARAM lparam) |
59 |
{ |
{ |
60 |
HWND curr_hwnd = NULL; |
HWND curr_hwnd; |
61 |
DWORD proc_id = 0; |
DWORD proc_id; |
62 |
|
|
63 |
if (code >= 0 && code == HCBT_SETFOCUS) { |
if (code >= 0 && code == HCBT_SETFOCUS) { |
64 |
/* A window is about to receive the keyboard focus */ |
/* A window is about to receive the keyboard focus */ |
65 |
|
|
66 |
|
proc_id = 0; |
67 |
curr_hwnd = (HWND)wparam; |
curr_hwnd = (HWND)wparam; |
68 |
GetWindowThreadProcessId( curr_hwnd, &proc_id ); |
GetWindowThreadProcessId (curr_hwnd, &proc_id); |
69 |
/* Only if the current window is different from this window list, |
/* Only if the current window is different from this window list, |
70 |
we set the new focus. For more information read the hint in |
we set the new focus. For more information read the hint in |
71 |
PTD_initialize. */ |
PTD_initialize. */ |
72 |
if ((proc_id != tray_proc_id) |
if ((proc_id != tray_proc_id) && |
73 |
&& (curr_hwnd != NULL) |
(curr_hwnd != NULL) && |
74 |
&& (curr_hwnd != curr_focus) |
(curr_hwnd != curr_focus) && |
75 |
&& (curr_hwnd != shell_traywnd) |
(curr_hwnd != shell_traywnd) && |
76 |
&& (curr_hwnd != systab_c32) |
(curr_hwnd != systab_c32) && |
77 |
&& (curr_hwnd != mstask_swc) |
(curr_hwnd != mstask_swc) && |
78 |
&& (curr_hwnd != rebar_w32)) { |
(curr_hwnd != rebar_w32)) { |
79 |
curr_focus = curr_hwnd; |
curr_focus = curr_hwnd; |
80 |
} |
} |
81 |
} |
} |
86 |
|
|
87 |
/* Return the last selected window */ |
/* Return the last selected window */ |
88 |
extern "C" HWND |
extern "C" HWND |
89 |
PTD_get_curr_hwnd (void) |
PTD_get_current_window (void) |
90 |
{ |
{ |
91 |
return curr_focus; |
return curr_focus; |
92 |
} |
} |
99 |
tray_proc_id = GetCurrentProcessId (); |
tray_proc_id = GetCurrentProcessId (); |
100 |
cbt_hook = SetWindowsHookEx (WH_CBT, PTD_CBT_proc, glob_hinst, 0); |
cbt_hook = SetWindowsHookEx (WH_CBT, PTD_CBT_proc, glob_hinst, 0); |
101 |
if (cbt_hook == NULL) |
if (cbt_hook == NULL) |
|
{ |
|
|
char buf[500]; |
|
|
|
|
|
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (), |
|
|
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), |
|
|
buf, sizeof buf -1, NULL); |
|
|
MessageBox (NULL, buf, "Error", MB_OK|MB_ICONSTOP); |
|
102 |
return FALSE; |
return FALSE; |
|
} |
|
103 |
|
|
|
|
|
104 |
/* Okay, what are we doing here: |
/* Okay, what are we doing here: |
105 |
* In the past I used the Spy++ application to find out why the old current |
* In the past I used the Spy++ application to find out why the old current |
106 |
* window doesn't work on so much systems. Now we try to use the computer |
* window doesn't work on so much systems. Now we try to use the computer |
107 |
* based training hook, more precise the HCBT_SETFOCUS to find out the |
* based training hook, more precisely the HCBT_SETFOCUS to find out the |
108 |
* last window focus. But to succeed we need to ignore some windows |
* last window focus. But to succeed we need to ignore some windows |
109 |
* because otherwise our own window or a window from the taskbar |
* because otherwise our own window or a window from the taskbar |
110 |
* would be recognized. |
* would be recognized. |
118 |
* As a result we need to ignore those windows to prevent failured |
* As a result we need to ignore those windows to prevent failured |
119 |
* in the code. |
* in the code. |
120 |
*/ |
*/ |
121 |
|
|
122 |
|
// FIXME: I doubt this is working for Windows 7 |
123 |
shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL); |
shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL); |
124 |
rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL); |
rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL); |
125 |
mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL); |
mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL); |
126 |
systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL); |
systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL); |
127 |
|
|
|
/*log_foo ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p " |
|
|
"mstask_swc=%p systab_c32=%p", |
|
|
tray_proc_id, cbt_hook, shell_traywnd, rebar_w32, |
|
|
mstask_swc, systab_c32);*/ |
|
|
|
|
128 |
return TRUE; |
return TRUE; |
129 |
} |
} |
130 |
|
|
131 |
|
|
132 |
/* Remove the CTB hook from the system and reset all variables. */ |
/* Remove the CTB hook from the system and reset all variables. */ |
133 |
extern "C" void |
extern "C" void |
134 |
PTD_delete (void) |
PTD_delete_hook (void) |
135 |
{ |
{ |
136 |
if (!cbt_hook) |
if (!cbt_hook) |
137 |
return; |
return; |
144 |
|
|
145 |
/* Return if the CTB hook is currently used. */ |
/* Return if the CTB hook is currently used. */ |
146 |
extern "C" int |
extern "C" int |
147 |
PTD_is_used (void) |
PTD_is_hook_used (void) |
148 |
{ |
{ |
149 |
return shell_traywnd && cbt_hook; |
return shell_traywnd && cbt_hook; |
150 |
} |
} |
151 |
|
|
152 |
|
/* From the MSDN: |
153 |
|
* "Typically, an application uses this function to play back a series |
154 |
|
* of mouse and keyboard messages recorded previously by the |
155 |
|
* JournalRecordProc hook procedure. As long as a JournalPlaybackProc |
156 |
|
* hook procedure is installed, regular mouse and keyboard input is disabled." |
157 |
|
*/ |
158 |
static LRESULT CALLBACK |
static LRESULT CALLBACK |
159 |
PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam) |
PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam) |
160 |
{ |
{ |
161 |
LPEVENTMSG em; |
LPEVENTMSG em; |
162 |
|
|
163 |
if (code == HC_SKIP) { |
if (code == HC_SKIP) { |
164 |
key_idx++; |
keyidx++; |
165 |
if (key_idx == nkeys) { |
if (keyidx >= nkeys) { |
166 |
UnhookWindowsHookEx (journ_hook); |
UnhookWindowsHookEx (journ_hook); |
167 |
journ_hook = NULL; |
journ_hook = NULL; |
168 |
} |
} |
175 |
* paramH - specifies the scan code. |
* paramH - specifies the scan code. |
176 |
*/ |
*/ |
177 |
em = (LPEVENTMSG )lparam; |
em = (LPEVENTMSG )lparam; |
178 |
if (journ_keys[key_idx] & 0x8000) |
if (journ_keys[keyidx] & 0x8000) |
179 |
em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN; |
em->message = journ_keys[keyidx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN; |
180 |
else |
else |
181 |
em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP; |
em->message = journ_keys[keyidx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP; |
182 |
em->paramL = LOBYTE( journ_keys[key_idx] ) |
em->paramL = LOBYTE (journ_keys[keyidx]) |
183 |
| (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8); |
| (MapVirtualKey (LOBYTE (journ_keys[keyidx]), 0) << 8); |
184 |
em->paramH = 1; |
em->paramH = 1; |
185 |
em->time = GetTickCount (); |
em->time = GetTickCount (); |
186 |
return 0; |
return 0; |
187 |
} |
} |
188 |
|
|
189 |
return CallNextHookEx (journ_hook, code, wparam, lparam); |
return CallNextHookEx (journ_hook, code, wparam, lparam); |
190 |
} /* PTD_Playback_proc */ |
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
extern "C" BOOL |
extern "C" BOOL |
195 |
PTD_keyb_send (UINT *keys, UINT n_keys) |
PTD_keyboard_send_keys (UINT *keys, UINT n_keys) |
196 |
{ |
{ |
197 |
MSG msg; |
MSG msg; |
198 |
journ_keys = keys; |
journ_keys = keys; |
199 |
key_idx = 0; |
keyidx = 0; |
200 |
nkeys = n_keys; |
nkeys = n_keys; |
201 |
|
|
202 |
|
/* Return immediately if there is an active hook */ |
203 |
if (journ_hook) |
if (journ_hook) |
204 |
return FALSE; |
return FALSE; |
205 |
|
|
206 |
while (GetAsyncKeyState (VK_MENU) & 0x8000 |
while (GetAsyncKeyState (VK_MENU) & 0x8000 || |
207 |
|| GetAsyncKeyState (VK_SHIFT) & 0x8000 |
GetAsyncKeyState (VK_SHIFT) & 0x8000 || |
208 |
|| GetAsyncKeyState (VK_CONTROL) & 0x8000) { |
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 |
PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam); |
PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam); |
237 |
} |
} |
238 |
|
|
239 |
|
|
240 |
/* Display a JPG picture in the given window at the given point. */ |
/* Main entry point for the DLL */ |
241 |
extern "C" int |
extern "C" BOOL WINAPI |
|
PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name) |
|
|
{ |
|
|
CJPG jpg; |
|
|
HDC hdc; |
|
|
POINT p2; |
|
|
BOOL rc; |
|
|
|
|
|
rc = jpg.load (name); |
|
|
if (!rc) |
|
|
return -1; /* XXX: use real return code. */ |
|
|
hdc = GetWindowDC (hwnd); |
|
|
rc = jpg.updateSizeOnDC (hdc); |
|
|
if (!rc) { |
|
|
ReleaseDC (hwnd, hdc); |
|
|
return -2; /* XXX: use real return code. */ |
|
|
} |
|
|
|
|
|
p2.x = jpg.m_Width; |
|
|
p2.y = jpg.m_Height; |
|
|
rc = jpg.show (hdc, p, &p2, 0, 0); |
|
|
|
|
|
ReleaseDC (hwnd, hdc); |
|
|
jpg.freePictureData (); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
BOOL WINAPI |
|
242 |
DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv) |
DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv) |
243 |
{ |
{ |
244 |
switch (reason) { |
switch (reason) { |
245 |
case DLL_PROCESS_ATTACH: |
case DLL_PROCESS_ATTACH: |
246 |
glob_hinst = hinst; |
glob_hinst = hinst; |
247 |
break; |
break; |
248 |
case DLL_THREAD_ATTACH: |
|
249 |
|
case DLL_PROCESS_DETACH: |
250 |
break; |
break; |
251 |
|
|
252 |
|
case DLL_THREAD_ATTACH: |
253 |
case DLL_THREAD_DETACH: |
case DLL_THREAD_DETACH: |
254 |
break; |
break; |
255 |
case DLL_PROCESS_DETACH: |
|
|
break; |
|
256 |
} |
} |
257 |
|
|
258 |
return TRUE; |
return TRUE; |
259 |
} |
} |
|
|
|