Mercurial > pidgin
annotate src/win32/IdleTracker/idletrack.c @ 13395:ff4e85e04adf
[gaim-migrate @ 15768]
Remove some dead code.
Resolves Coverity CID 1 of 113 ;)
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Mon, 06 Mar 2006 07:53:52 +0000 |
parents | 5c50be815a4e |
children | 099bb2149158 |
rev | line source |
---|---|
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
1 /* |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
2 * idletrack.c |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
3 * |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
4 * Authors: mrgentry @ http://www.experts-exchange.com |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
5 * Herman Bloggs <hermanator12002@yahoo.com> |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
6 * Date: February, 2003 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
7 * Description: Track user inactivity. |
9417 | 8 * |
9 * Andrew Whewell <awhewell@users.sourceforge.net> - 25th June 2004. Added | |
10 * support for GetLastInputInfo under Windows 2000 and above. This avoids having | |
11 * IDLETRACK.DLL hook itself into every process on the machine, which makes | |
12 * upgrades easier. The hook mechanism is also used by key loggers, so not | |
13 * using hooks doesn't put the willys up programs that keep an eye out for | |
14 * loggers. | |
15 * | |
9419 | 16 * Windows 9x doesn't have GetLastInputInfo - when Gaim runs on these machines |
9417 | 17 * the code silently falls back onto the old hooking scheme. |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
18 */ |
9417 | 19 #define _WIN32_WINNT 0x0500 |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
20 #include <windows.h> |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
21 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
22 #define EXPORT __declspec(dllexport) |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
23 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
24 static HANDLE hMapObject = NULL; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
25 static DWORD *lastTime = NULL; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
26 static HHOOK keyHook = NULL; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
27 static HHOOK mouseHook = NULL; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
28 static HINSTANCE g_hInstance = NULL; |
4548
ae85f27e4948
[gaim-migrate @ 4827]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4533
diff
changeset
|
29 static POINT g_point; |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
30 |
9418 | 31 /* GetLastInputInfo address and module - if g_GetLastInputInfo == NULL then |
32 * we fall back on the old "hook the world" method. GetLastInputInfo was brought | |
33 * in with Windows 2000 so Windows 9x will still hook everything. | |
34 */ | |
9417 | 35 typedef BOOL (WINAPI *GETLASTINPUTINFO)(LASTINPUTINFO *); |
36 static HMODULE g_user32 = NULL; | |
37 static GETLASTINPUTINFO g_GetLastInputInfo = NULL; | |
38 | |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
39 static DWORD* setup_shared_mem() { |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
40 BOOL fInit; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
41 |
9418 | 42 /* Set up the shared memory. */ |
11828
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
43 hMapObject = CreateFileMapping((HANDLE) 0xFFFFFFFF, /* use paging file */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
44 NULL, /* no security attributes */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
45 PAGE_READWRITE, /* read/write access */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
46 0, /* size: high 32-bits */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
47 sizeof(DWORD), /* size: low 32-bits */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
48 "timermem"); /* name of map object */ |
9418 | 49 |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
50 if (hMapObject == NULL) |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
51 return NULL; |
9418 | 52 |
53 /* The first process to attach initializes memory. */ | |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
54 fInit = (GetLastError() != ERROR_ALREADY_EXISTS); |
9418 | 55 |
56 /* Get a pointer to the file-mapped shared memory. */ | |
11828
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
57 lastTime = (DWORD*) MapViewOfFile(hMapObject, /* object to map view of */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
58 FILE_MAP_WRITE, /* read/write access */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
59 0, /* high offset: map from */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
60 0, /* low offset: beginning */ |
5c50be815a4e
[gaim-migrate @ 14119]
Richard Laager <rlaager@wiktel.com>
parents:
9865
diff
changeset
|
61 0); /* default: map entire file */ |
9418 | 62 |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
63 if (lastTime == NULL) |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
64 return NULL; |
9418 | 65 |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
66 *lastTime = GetTickCount(); |
9418 | 67 |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
68 return lastTime; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
69 } |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
70 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
71 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
72 LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam) { |
6561
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
73 if (!(code < 0)) { |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
74 if (lastTime == NULL) |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
75 lastTime = setup_shared_mem(); |
9418 | 76 |
6561
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
77 if (lastTime) |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
78 *lastTime = GetTickCount(); |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
79 } |
5336
5480a73b2696
[gaim-migrate @ 5712]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4548
diff
changeset
|
80 return CallNextHookEx(keyHook, code, wParam, lParam); |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
81 } |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
82 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
83 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
84 LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam) { |
4548
ae85f27e4948
[gaim-migrate @ 4827]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4533
diff
changeset
|
85 /* We need to verify that the Mouse pointer has actually moved. */ |
6561
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
86 if(!(code < 0) && |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
87 !((g_point.x == ((MOUSEHOOKSTRUCT*)lParam)->pt.x) && |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
88 (g_point.y == ((MOUSEHOOKSTRUCT*)lParam)->pt.y))) { |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
89 g_point.x = ((MOUSEHOOKSTRUCT*)lParam)->pt.x; |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
90 g_point.y = ((MOUSEHOOKSTRUCT*)lParam)->pt.y; |
9418 | 91 |
6561
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
92 if (lastTime == NULL) |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
93 lastTime = setup_shared_mem(); |
9418 | 94 |
6561
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
95 if (lastTime) |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
96 *lastTime = GetTickCount(); |
33ceba0dfd9b
[gaim-migrate @ 7083]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5336
diff
changeset
|
97 } |
5336
5480a73b2696
[gaim-migrate @ 5712]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4548
diff
changeset
|
98 return CallNextHookEx(mouseHook, code, wParam, lParam); |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
99 } |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
100 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
101 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
102 EXPORT DWORD wgaim_get_lastactive() { |
9417 | 103 DWORD result = 0; |
9418 | 104 |
105 /* If we have GetLastInputInfo then use it, otherwise use the hooks*/ | |
9417 | 106 if (g_GetLastInputInfo != NULL) { |
107 LASTINPUTINFO lii; | |
108 memset(&lii, 0, sizeof(lii)); | |
109 lii.cbSize = sizeof(lii); | |
110 if (g_GetLastInputInfo(&lii)) { | |
111 result = lii.dwTime; | |
112 } | |
113 } else { | |
114 if (lastTime == NULL) | |
115 lastTime = setup_shared_mem(); | |
9418 | 116 |
9417 | 117 if (lastTime) |
118 result = *lastTime; | |
119 } | |
9418 | 120 |
9417 | 121 return result; |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
122 } |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
123 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
124 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
125 EXPORT BOOL wgaim_set_idlehooks() { |
9418 | 126 /* Is GetLastInputInfo available?*/ |
9417 | 127 g_user32 = LoadLibrary("user32.dll"); |
128 if (g_user32) { | |
129 g_GetLastInputInfo = (GETLASTINPUTINFO)GetProcAddress(g_user32, "GetLastInputInfo"); | |
130 } | |
131 | |
9418 | 132 /* If we couldn't find GetLastInputInfo then fall back onto the hooking scheme*/ |
9417 | 133 if (g_GetLastInputInfo == NULL) { |
9418 | 134 /* Set up the shared memory.*/ |
9417 | 135 lastTime = setup_shared_mem(); |
136 if (lastTime == NULL) | |
137 return FALSE; | |
138 *lastTime = GetTickCount(); | |
9418 | 139 |
140 /* Set up the keyboard hook.*/ | |
9417 | 141 keyHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, 0); |
142 if (keyHook == NULL) { | |
143 UnmapViewOfFile(lastTime); | |
144 CloseHandle(hMapObject); | |
145 return FALSE; | |
146 } | |
9418 | 147 |
148 /* Set up the mouse hook.*/ | |
9417 | 149 mouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstance, 0); |
150 if (mouseHook == NULL) { | |
151 UnhookWindowsHookEx(keyHook); | |
152 UnmapViewOfFile(lastTime); | |
153 CloseHandle(hMapObject); | |
154 return FALSE; | |
155 } | |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
156 } |
9418 | 157 |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
158 return TRUE; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
159 } |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
160 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
161 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
162 EXPORT void wgaim_remove_idlehooks() { |
9417 | 163 if (g_user32 != NULL) |
164 FreeLibrary(g_user32); | |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
165 if (keyHook) |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
166 UnhookWindowsHookEx(keyHook); |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
167 if (mouseHook) |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
168 UnhookWindowsHookEx(mouseHook); |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
169 if (lastTime) |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
170 UnmapViewOfFile(lastTime); |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
171 if (hMapObject) |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
172 CloseHandle(hMapObject); |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
173 } |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
174 |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
175 int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
176 switch(dwReason) { |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
177 case DLL_PROCESS_ATTACH: |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
178 g_hInstance = hInstance; |
4548
ae85f27e4948
[gaim-migrate @ 4827]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4533
diff
changeset
|
179 g_point.x = 0; |
ae85f27e4948
[gaim-migrate @ 4827]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
4533
diff
changeset
|
180 g_point.y = 0; |
4533
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
181 break; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
182 case DLL_PROCESS_DETACH: |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
183 break; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
184 } |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
185 return TRUE; |
516061abad03
[gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff
changeset
|
186 } |