Fri, 07 Feb 2003 01:10:29 +0000
[gaim-migrate @ 4827]
Idle time was incorrect, because we were getting Mouse
move messages despite the coords not having changed. There is
now a check to verify that the coords have changed.
| 4533 | 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 | ||
|
4548
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
13 | /* from msdn docs */ |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
14 | typedef struct tagMOUSEHOOKSTRUCT { |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
15 | POINT pt; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
16 | HWND hwnd; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
17 | UINT wHitTestCode; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
18 | DWORD dwExtraInfo; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
19 | } MOUSEHOOKSTRUCT; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
20 | |
| 4533 | 21 | static HANDLE hMapObject = NULL; |
| 22 | static DWORD *lastTime = NULL; | |
| 23 | static HHOOK keyHook = NULL; | |
| 24 | static HHOOK mouseHook = NULL; | |
| 25 | static HINSTANCE g_hInstance = NULL; | |
|
4548
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
26 | static POINT g_point; |
| 4533 | 27 | |
| 28 | static DWORD* setup_shared_mem() { | |
| 29 | BOOL fInit; | |
| 30 | ||
| 31 | // Set up the shared memory. | |
| 32 | hMapObject = CreateFileMapping((HANDLE) 0xFFFFFFFF, // use paging file | |
| 33 | NULL, // no security attributes | |
| 34 | PAGE_READWRITE, // read/write access | |
| 35 | 0, // size: high 32-bits | |
| 36 | sizeof(DWORD), // size: low 32-bits | |
| 37 | "timermem"); // name of map object | |
| 38 | ||
| 39 | if (hMapObject == NULL) | |
| 40 | return NULL; | |
| 41 | ||
| 42 | // The first process to attach initializes memory. | |
| 43 | fInit = (GetLastError() != ERROR_ALREADY_EXISTS); | |
| 44 | ||
| 45 | // Get a pointer to the file-mapped shared memory. | |
| 46 | lastTime = (DWORD*) MapViewOfFile(hMapObject, // object to map view of | |
| 47 | FILE_MAP_WRITE, // read/write access | |
| 48 | 0, // high offset: map from | |
| 49 | 0, // low offset: beginning | |
| 50 | 0); // default: map entire file | |
| 51 | ||
| 52 | if (lastTime == NULL) | |
| 53 | return NULL; | |
| 54 | ||
| 55 | *lastTime = GetTickCount(); | |
| 56 | ||
| 57 | return lastTime; | |
| 58 | } | |
| 59 | ||
| 60 | ||
| 61 | LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam) { | |
| 62 | if (code < 0) | |
| 63 | return CallNextHookEx(keyHook, code, wParam, lParam); | |
| 64 | if (lastTime == NULL) | |
| 65 | lastTime = setup_shared_mem(); | |
| 66 | ||
| 67 | if (lastTime) | |
| 68 | *lastTime = GetTickCount(); | |
| 69 | ||
| 70 | return 0; | |
| 71 | } | |
| 72 | ||
| 73 | ||
| 74 | LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam) { | |
| 75 | if (code < 0) | |
| 76 | return CallNextHookEx(mouseHook, code, wParam, lParam); | |
|
4548
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
77 | |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
78 | /* We need to verify that the Mouse pointer has actually moved. */ |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
79 | if((g_point.x == ((MOUSEHOOKSTRUCT*)lParam)->pt.x) && |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
80 | (g_point.y == ((MOUSEHOOKSTRUCT*)lParam)->pt.y)) |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
81 | return 0; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
82 | |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
83 | g_point.x = ((MOUSEHOOKSTRUCT*)lParam)->pt.x; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
84 | g_point.y = ((MOUSEHOOKSTRUCT*)lParam)->pt.y; |
| 4533 | 85 | |
| 86 | if (lastTime == NULL) | |
| 87 | lastTime = setup_shared_mem(); | |
| 88 | ||
| 89 | if (lastTime) | |
| 90 | *lastTime = GetTickCount(); | |
| 91 | ||
| 92 | return 0; | |
| 93 | } | |
| 94 | ||
| 95 | ||
| 96 | EXPORT DWORD wgaim_get_lastactive() { | |
| 97 | if (lastTime == NULL) | |
| 98 | lastTime = setup_shared_mem(); | |
| 99 | ||
| 100 | if (lastTime) | |
| 101 | return *lastTime; | |
| 102 | ||
| 103 | return 0; | |
| 104 | } | |
| 105 | ||
| 106 | ||
| 107 | EXPORT BOOL wgaim_set_idlehooks() { | |
| 108 | // Set up the shared memory. | |
| 109 | lastTime = setup_shared_mem(); | |
| 110 | if (lastTime == NULL) | |
| 111 | return FALSE; | |
| 112 | *lastTime = GetTickCount(); | |
| 113 | ||
| 114 | // Set up the keyboard hook. | |
| 115 | keyHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, 0); | |
| 116 | if (keyHook == NULL) { | |
| 117 | UnmapViewOfFile(lastTime); | |
| 118 | CloseHandle(hMapObject); | |
| 119 | return FALSE; | |
| 120 | } | |
| 121 | ||
| 122 | // Set up the mouse hook. | |
| 123 | mouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstance, 0); | |
| 124 | if (mouseHook == NULL) { | |
| 125 | UnhookWindowsHookEx(keyHook); | |
| 126 | UnmapViewOfFile(lastTime); | |
| 127 | CloseHandle(hMapObject); | |
| 128 | return FALSE; | |
| 129 | } | |
| 130 | ||
| 131 | return TRUE; | |
| 132 | } | |
| 133 | ||
| 134 | ||
| 135 | EXPORT void wgaim_remove_idlehooks() { | |
| 136 | if (keyHook) | |
| 137 | UnhookWindowsHookEx(keyHook); | |
| 138 | if (mouseHook) | |
| 139 | UnhookWindowsHookEx(mouseHook); | |
| 140 | if (lastTime) | |
| 141 | UnmapViewOfFile(lastTime); | |
| 142 | if (hMapObject) | |
| 143 | CloseHandle(hMapObject); | |
| 144 | } | |
| 145 | ||
| 146 | int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { | |
| 147 | switch(dwReason) { | |
| 148 | case DLL_PROCESS_ATTACH: | |
| 149 | g_hInstance = hInstance; | |
|
4548
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
150 | g_point.x = 0; |
|
f89a7e6e9508
[gaim-migrate @ 4827]
Herman Bloggs <herman@bluedigits.com>
parents:
4533
diff
changeset
|
151 | g_point.y = 0; |
| 4533 | 152 | break; |
| 153 | case DLL_PROCESS_DETACH: | |
| 154 | break; | |
| 155 | } | |
| 156 | return TRUE; | |
| 157 | } |