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