/[winpt]/trunk/PTD/PTD.cpp
ViewVC logotype

Contents of /trunk/PTD/PTD.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations)
Tue Oct 25 07:46:20 2005 UTC (19 years, 4 months ago) by twoaday
File size: 8175 byte(s)
More bug fixes and cleanups.
See ChangeLog for details.

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; /* 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 #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; /* Journaling hook handle. */
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
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 *
101 * Here is what the Spy++ says about the windows order:
102 * Shell_TrayWnd
103 * \ ReBarWindow32
104 * \ MSTaskSwWClass
105 * \ SysTabControl32
106 *
107 * As a result we need to ignore those windows to prevent failured
108 * in the code.
109 */
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 /*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
120 return TRUE;
121 }
122
123
124 /* Remove the CTB hook from the system and reset all variables. */
125 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 }
135
136
137 /* Return if the CTB hook is currently used. */
138 int
139 PTD_is_used (void)
140 {
141 return shell_traywnd && cbt_hook;
142 }
143
144
145 static LRESULT CALLBACK
146 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
181 BOOL
182 PTD_keyb_send (UINT *keys, UINT n_keys)
183 {
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 }
224
225
226 /* Return version of the PTD library. */
227 const char *
228 PTD_get_version (void)
229 {
230 return "0.8.1";
231 }
232
233
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 rc = jpg.load (name);
244 if (!rc)
245 return -1; /* XXX: use real return code. */
246 hdc = GetWindowDC (hwnd);
247 rc = jpg.updateSizeOnDC (hdc);
248 if (!rc) {
249 ReleaseDC (hwnd, hdc);
250 return -2; /* XXX: use real return code. */
251 }
252
253 p2.x = jpg.m_Width;
254 p2.y = jpg.m_Height;
255 rc = jpg.show (hdc, p, &p2, 0, 0);
256
257 ReleaseDC (hwnd, hdc);
258 jpg.freePictureData ();
259 return rc;
260 }
261
262
263
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 }
280

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26