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 |
|
|
|
24 |
|
|
HINSTANCE glob_hinst; |
25 |
|
|
|
26 |
|
|
/* We need a special section in the DLL for storing our shared data */ |
27 |
|
|
#pragma data_seg(".SHARDAT") |
28 |
|
|
static HHOOK cbt_hook = NULL; |
29 |
|
|
static DWORD tray_proc_id = 0; |
30 |
|
|
static HWND shell_traywnd = 0; |
31 |
|
|
static HWND curr_focus = NULL; |
32 |
|
|
static HWND rebar_w32 = NULL; |
33 |
|
|
static HWND mstask_swc = NULL; |
34 |
|
|
static HWND systab_c32 = NULL; |
35 |
|
|
#pragma data_seg() |
36 |
|
|
|
37 |
|
|
static unsigned * journ_keys = 0; |
38 |
|
|
static unsigned key_idx = 0; |
39 |
|
|
static unsigned nkeys = 0; |
40 |
|
|
static HHOOK journ_hook = NULL; |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
static LRESULT CALLBACK |
44 |
|
|
PTD_CBT_proc (int code, WPARAM wparam, LPARAM lparam) |
45 |
|
|
{ |
46 |
|
|
HWND curr_hwnd = NULL; |
47 |
|
|
DWORD proc_id = 0; |
48 |
|
|
|
49 |
|
|
if (code >= 0 && code == HCBT_SETFOCUS) { |
50 |
|
|
/* A window is about to receive the keyboard focus */ |
51 |
|
|
|
52 |
|
|
curr_hwnd = (HWND)wparam; |
53 |
|
|
GetWindowThreadProcessId( curr_hwnd, &proc_id ); |
54 |
|
|
/* Only if the current window is different from this window list, |
55 |
|
|
we set the new focus. For more information read the hint in |
56 |
|
|
PTD_initialize. */ |
57 |
|
|
if ((proc_id != tray_proc_id) |
58 |
|
|
&& (curr_hwnd != NULL) |
59 |
|
|
&& (curr_hwnd != curr_focus) |
60 |
|
|
&& (curr_hwnd != shell_traywnd) |
61 |
|
|
&& (curr_hwnd != systab_c32) |
62 |
|
|
&& (curr_hwnd != mstask_swc) |
63 |
|
|
&& (curr_hwnd != rebar_w32)) { |
64 |
|
|
curr_focus = curr_hwnd; |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
return CallNextHookEx (cbt_hook , code, wparam, lparam); |
69 |
|
|
} /* PTD_CBT_proc */ |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
HWND |
73 |
|
|
PTD_get_curr_hwnd (void) |
74 |
|
|
{ |
75 |
|
|
return curr_focus; |
76 |
|
|
} /* PTD_get_curr_hwnd */ |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
BOOL |
80 |
|
|
PTD_initialize (void) |
81 |
|
|
{ |
82 |
|
|
tray_proc_id = GetCurrentProcessId( ); |
83 |
|
|
cbt_hook = SetWindowsHookEx( WH_CBT, PTD_CBT_proc, glob_hinst, 0 ); |
84 |
|
|
if( cbt_hook == NULL ) |
85 |
|
|
return FALSE; |
86 |
|
|
|
87 |
|
|
/* Okay, what are we doing here: |
88 |
|
|
* In the past I used the Spy++ application to find out why the old current window |
89 |
|
|
* doesn't work on so much systems. Now we try to use the computer based training |
90 |
|
|
* hook, more precise the HCBT_SETFOCUS to find out the last window focus. But to |
91 |
|
|
* succeed we need to ignore some windows because otherwise our own window or a |
92 |
|
|
* window from the taskbar would be recognized. |
93 |
|
|
* |
94 |
|
|
* Here is what the Spy++ says about the windows order: |
95 |
|
|
* Shell_TrayWnd |
96 |
|
|
* \ ReBarWindow32 |
97 |
|
|
* \ MSTaskSwWClass |
98 |
|
|
* \ SysTabControl32 |
99 |
|
|
* |
100 |
|
|
* As a result we need to ignore those windows to prevent failured in the code. |
101 |
|
|
*/ |
102 |
|
|
shell_traywnd = FindWindowEx (NULL, NULL, "Shell_TrayWnd", NULL); |
103 |
|
|
rebar_w32 = FindWindowEx (shell_traywnd, NULL, "ReBarWindow32", NULL); |
104 |
|
|
mstask_swc = FindWindowEx (rebar_w32, NULL, "MSTaskSwWClass", NULL); |
105 |
|
|
systab_c32 = FindWindowEx (mstask_swc, NULL, "SysTabControl32", NULL); |
106 |
|
|
|
107 |
|
|
/*_log_box ("tray_proc_id=%u cbt_hook=%p shell_traywnd=%p rebar_w32=%p " |
108 |
|
|
"mstask_swc=%p systab_c32=%p", |
109 |
|
|
tray_proc_id, cbt_hook, shell_traywnd, rebar_w32, |
110 |
|
|
mstask_swc, systab_c32);*/ |
111 |
|
|
|
112 |
|
|
return TRUE; |
113 |
|
|
} /* PTD_initialize */ |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
void |
117 |
|
|
PTD_delete (void) |
118 |
|
|
{ |
119 |
|
|
if (!cbt_hook) |
120 |
|
|
return; |
121 |
|
|
UnhookWindowsHookEx (cbt_hook); |
122 |
|
|
cbt_hook = NULL; |
123 |
|
|
shell_traywnd = NULL; |
124 |
|
|
tray_proc_id = 0; |
125 |
|
|
} /* PTD_delete */ |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
int |
129 |
|
|
PTD_is_used (void) |
130 |
|
|
{ |
131 |
|
|
return shell_traywnd && cbt_hook; |
132 |
|
|
} /* PTD_is_used */ |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
LRESULT CALLBACK |
136 |
|
|
PTD_playback_proc (int code, WPARAM wparam, LPARAM lparam) |
137 |
|
|
{ |
138 |
|
|
LPEVENTMSG em; |
139 |
|
|
|
140 |
|
|
if (code == HC_SKIP) { |
141 |
|
|
key_idx++; |
142 |
|
|
if (key_idx == nkeys) { |
143 |
|
|
UnhookWindowsHookEx (journ_hook); |
144 |
|
|
journ_hook = NULL; |
145 |
|
|
} |
146 |
|
|
return 0; |
147 |
|
|
} |
148 |
|
|
else if (code == HC_GETNEXT) { |
149 |
|
|
/* Here is an overview what the event message struct can contain: |
150 |
|
|
* message - WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN: |
151 |
|
|
* paramL - specifies the virtual key code of the key that was pressed. |
152 |
|
|
* paramH - specifies the scan code. |
153 |
|
|
*/ |
154 |
|
|
em = (LPEVENTMSG )lparam; |
155 |
|
|
if (journ_keys[key_idx] & 0x8000) |
156 |
|
|
em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYDOWN : WM_KEYDOWN; |
157 |
|
|
else |
158 |
|
|
em->message = journ_keys[key_idx] & 0x4000 ? WM_SYSKEYUP : WM_KEYUP; |
159 |
|
|
em->paramL = LOBYTE( journ_keys[key_idx] ) |
160 |
|
|
| (MapVirtualKey (LOBYTE (journ_keys[key_idx]), 0) << 8); |
161 |
|
|
em->paramH = 1; |
162 |
|
|
em->time = GetTickCount (); |
163 |
|
|
return 0; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
return CallNextHookEx (journ_hook, code, wparam, lparam); |
167 |
|
|
} /* PTD_Playback_proc */ |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
BOOL |
171 |
|
|
PTD_keyb_send (UINT * keys, UINT n_keys) |
172 |
|
|
{ |
173 |
|
|
MSG msg; |
174 |
|
|
|
175 |
|
|
journ_keys = keys; |
176 |
|
|
key_idx = 0; |
177 |
|
|
nkeys = n_keys; |
178 |
|
|
|
179 |
|
|
if (journ_hook) |
180 |
|
|
return FALSE; |
181 |
|
|
|
182 |
|
|
while (GetAsyncKeyState (VK_MENU) & 0x8000 |
183 |
|
|
|| GetAsyncKeyState (VK_SHIFT) & 0x8000 |
184 |
|
|
|| GetAsyncKeyState (VK_CONTROL) & 0x8000) { |
185 |
|
|
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) { |
186 |
|
|
if (msg.message == WM_QUIT) { |
187 |
|
|
PostMessage (msg.hwnd, msg.message, msg.wParam, msg.lParam); |
188 |
|
|
return FALSE; |
189 |
|
|
} |
190 |
|
|
TranslateMessage (&msg); |
191 |
|
|
DispatchMessage (&msg); |
192 |
|
|
} |
193 |
|
|
} |
194 |
|
|
journ_hook = SetWindowsHookEx (WH_JOURNALPLAYBACK, |
195 |
|
|
(HOOKPROC)PTD_playback_proc, |
196 |
|
|
glob_hinst, 0); |
197 |
|
|
if (journ_hook == NULL) |
198 |
|
|
return FALSE; |
199 |
|
|
|
200 |
|
|
while (journ_hook) { |
201 |
|
|
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) { |
202 |
|
|
if (msg.message == WM_QUIT) { |
203 |
|
|
if (journ_hook) |
204 |
|
|
UnhookWindowsHookEx (journ_hook); |
205 |
|
|
PostMessage (msg.hwnd, msg.message, msg.wParam, msg.wParam); |
206 |
|
|
} |
207 |
|
|
TranslateMessage (&msg); |
208 |
|
|
DispatchMessage (&msg); |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
return TRUE; |
213 |
|
|
} /* PTD_keyb_send */ |
214 |
|
|
|
215 |
|
|
|
216 |
|
|
const char * |
217 |
|
|
PTD_get_version (void) |
218 |
|
|
{ |
219 |
|
|
return "0.8.0"; |
220 |
|
|
} /* PTD_get_version */ |
221 |
|
|
|
222 |
|
|
|
223 |
|
|
BOOL WINAPI |
224 |
|
|
DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserv) |
225 |
|
|
{ |
226 |
|
|
switch (reason) { |
227 |
|
|
case DLL_PROCESS_ATTACH: |
228 |
|
|
glob_hinst = hinst; |
229 |
|
|
break; |
230 |
|
|
case DLL_THREAD_ATTACH: |
231 |
|
|
break; |
232 |
|
|
case DLL_THREAD_DETACH: |
233 |
|
|
break; |
234 |
|
|
case DLL_PROCESS_DETACH: |
235 |
|
|
break; |
236 |
|
|
} |
237 |
|
|
return TRUE; |
238 |
|
|
} /* DllMain */ |