annotate src/win32/IdleTracker/idletrack.c @ 4533:516061abad03

[gaim-migrate @ 4812] Fix for buggy idle tracker. committer: Tailor Script <tailor@pidgin.im>
author Herman Bloggs <hermanator12002@yahoo.com>
date Wed, 05 Feb 2003 23:46:01 +0000
parents
children ae85f27e4948
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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.
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
8 */
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
9 #include <windows.h>
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
10
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
11 #define EXPORT __declspec(dllexport)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
12
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
13 static HANDLE hMapObject = NULL;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
14 static DWORD *lastTime = NULL;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
15 static HHOOK keyHook = NULL;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
16 static HHOOK mouseHook = NULL;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
17 static HINSTANCE g_hInstance = NULL;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
18
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
19 static DWORD* setup_shared_mem() {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
20 BOOL fInit;
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 // Set up the shared memory.
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
23 hMapObject = CreateFileMapping((HANDLE) 0xFFFFFFFF, // use paging file
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
24 NULL, // no security attributes
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
25 PAGE_READWRITE, // read/write access
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
26 0, // size: high 32-bits
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
27 sizeof(DWORD), // size: low 32-bits
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
28 "timermem"); // name of map object
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
29
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
30 if (hMapObject == NULL)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
31 return NULL;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
32
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
33 // The first process to attach initializes memory.
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
34 fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
35
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
36 // Get a pointer to the file-mapped shared memory.
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
37 lastTime = (DWORD*) MapViewOfFile(hMapObject, // object to map view of
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
38 FILE_MAP_WRITE, // read/write access
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
39 0, // high offset: map from
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
40 0, // low offset: beginning
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
41 0); // default: map entire file
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
42
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
43 if (lastTime == NULL)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
44 return NULL;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
45
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
46 *lastTime = GetTickCount();
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
47
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
48 return lastTime;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
49 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
50
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
51
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
52 LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam) {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
53 if (code < 0)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
54 return CallNextHookEx(keyHook, code, wParam, lParam);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
55
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
56 if (lastTime == NULL)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
57 lastTime = setup_shared_mem();
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
58
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
59 if (lastTime)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
60 *lastTime = GetTickCount();
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
61
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
62 return 0;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
63 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
64
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
65
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
66 LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam) {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
67 if (code < 0)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
68 return CallNextHookEx(mouseHook, code, wParam, lParam);
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 if (lastTime == NULL)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
71 lastTime = setup_shared_mem();
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
72
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
73 if (lastTime)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
74 *lastTime = GetTickCount();
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
75
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
76 return 0;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
77 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
78
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
79
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
80 EXPORT DWORD wgaim_get_lastactive() {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
81 if (lastTime == NULL)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
82 lastTime = setup_shared_mem();
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 if (lastTime)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
85 return *lastTime;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
86
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
87 return 0;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
88 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
89
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
90
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
91 EXPORT BOOL wgaim_set_idlehooks() {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
92 // Set up the shared memory.
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
93 lastTime = setup_shared_mem();
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
94 if (lastTime == NULL)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
95 return FALSE;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
96 *lastTime = GetTickCount();
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
97
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
98 // Set up the keyboard hook.
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
99 keyHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, 0);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
100 if (keyHook == NULL) {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
101 UnmapViewOfFile(lastTime);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
102 CloseHandle(hMapObject);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
103 return FALSE;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
104 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
105
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
106 // Set up the mouse hook.
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
107 mouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstance, 0);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
108 if (mouseHook == NULL) {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
109 UnhookWindowsHookEx(keyHook);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
110 UnmapViewOfFile(lastTime);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
111 CloseHandle(hMapObject);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
112 return FALSE;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
113 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
114
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
115 return TRUE;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
116 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
117
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
118
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
119 EXPORT void wgaim_remove_idlehooks() {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
120 if (keyHook)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
121 UnhookWindowsHookEx(keyHook);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
122 if (mouseHook)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
123 UnhookWindowsHookEx(mouseHook);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
124 if (lastTime)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
125 UnmapViewOfFile(lastTime);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
126 if (hMapObject)
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
127 CloseHandle(hMapObject);
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
128 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
129
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
130 int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
131 switch(dwReason) {
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
132 case DLL_PROCESS_ATTACH:
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
133 g_hInstance = hInstance;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
134 break;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
135 case DLL_PROCESS_DETACH:
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
136 break;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
137 }
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
138 return TRUE;
516061abad03 [gaim-migrate @ 4812]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
139 }