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