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