comparison 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
comparison
equal deleted inserted replaced
4532:6488535322a1 4533:516061abad03
1 /*
2 * idletrack.c
3 *
4 * Authors: mrgentry @ http://www.experts-exchange.com
5 * Herman Bloggs <hermanator12002@yahoo.com>
6 * Date: February, 2003
7 * Description: Track user inactivity.
8 */
9 #include <windows.h>
10
11 #define EXPORT __declspec(dllexport)
12
13 static HANDLE hMapObject = NULL;
14 static DWORD *lastTime = NULL;
15 static HHOOK keyHook = NULL;
16 static HHOOK mouseHook = NULL;
17 static HINSTANCE g_hInstance = NULL;
18
19 static DWORD* setup_shared_mem() {
20 BOOL fInit;
21
22 // Set up the shared memory.
23 hMapObject = CreateFileMapping((HANDLE) 0xFFFFFFFF, // use paging file
24 NULL, // no security attributes
25 PAGE_READWRITE, // read/write access
26 0, // size: high 32-bits
27 sizeof(DWORD), // size: low 32-bits
28 "timermem"); // name of map object
29
30 if (hMapObject == NULL)
31 return NULL;
32
33 // The first process to attach initializes memory.
34 fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
35
36 // Get a pointer to the file-mapped shared memory.
37 lastTime = (DWORD*) MapViewOfFile(hMapObject, // object to map view of
38 FILE_MAP_WRITE, // read/write access
39 0, // high offset: map from
40 0, // low offset: beginning
41 0); // default: map entire file
42
43 if (lastTime == NULL)
44 return NULL;
45
46 *lastTime = GetTickCount();
47
48 return lastTime;
49 }
50
51
52 LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam) {
53 if (code < 0)
54 return CallNextHookEx(keyHook, code, wParam, lParam);
55
56 if (lastTime == NULL)
57 lastTime = setup_shared_mem();
58
59 if (lastTime)
60 *lastTime = GetTickCount();
61
62 return 0;
63 }
64
65
66 LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam) {
67 if (code < 0)
68 return CallNextHookEx(mouseHook, code, wParam, lParam);
69
70 if (lastTime == NULL)
71 lastTime = setup_shared_mem();
72
73 if (lastTime)
74 *lastTime = GetTickCount();
75
76 return 0;
77 }
78
79
80 EXPORT DWORD wgaim_get_lastactive() {
81 if (lastTime == NULL)
82 lastTime = setup_shared_mem();
83
84 if (lastTime)
85 return *lastTime;
86
87 return 0;
88 }
89
90
91 EXPORT BOOL wgaim_set_idlehooks() {
92 // Set up the shared memory.
93 lastTime = setup_shared_mem();
94 if (lastTime == NULL)
95 return FALSE;
96 *lastTime = GetTickCount();
97
98 // Set up the keyboard hook.
99 keyHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, 0);
100 if (keyHook == NULL) {
101 UnmapViewOfFile(lastTime);
102 CloseHandle(hMapObject);
103 return FALSE;
104 }
105
106 // Set up the mouse hook.
107 mouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstance, 0);
108 if (mouseHook == NULL) {
109 UnhookWindowsHookEx(keyHook);
110 UnmapViewOfFile(lastTime);
111 CloseHandle(hMapObject);
112 return FALSE;
113 }
114
115 return TRUE;
116 }
117
118
119 EXPORT void wgaim_remove_idlehooks() {
120 if (keyHook)
121 UnhookWindowsHookEx(keyHook);
122 if (mouseHook)
123 UnhookWindowsHookEx(mouseHook);
124 if (lastTime)
125 UnmapViewOfFile(lastTime);
126 if (hMapObject)
127 CloseHandle(hMapObject);
128 }
129
130 int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
131 switch(dwReason) {
132 case DLL_PROCESS_ATTACH:
133 g_hInstance = hInstance;
134 break;
135 case DLL_PROCESS_DETACH:
136 break;
137 }
138 return TRUE;
139 }