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