# HG changeset patch # User Herman Bloggs # Date 1044488761 0 # Node ID 516061abad03e3aeb736bfaaa20a51046ad44ef0 # Parent 6488535322a184959472449d04058a412611deac [gaim-migrate @ 4812] Fix for buggy idle tracker. committer: Tailor Script diff -r 6488535322a1 -r 516061abad03 src/win32/IdleTracker/idletrack.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/IdleTracker/idletrack.c Wed Feb 05 23:46:01 2003 +0000 @@ -0,0 +1,139 @@ +/* + * idletrack.c + * + * Authors: mrgentry @ http://www.experts-exchange.com + * Herman Bloggs + * Date: February, 2003 + * Description: Track user inactivity. + */ +#include + +#define EXPORT __declspec(dllexport) + +static HANDLE hMapObject = NULL; +static DWORD *lastTime = NULL; +static HHOOK keyHook = NULL; +static HHOOK mouseHook = NULL; +static HINSTANCE g_hInstance = NULL; + +static DWORD* setup_shared_mem() { + BOOL fInit; + + // Set up the shared memory. + hMapObject = CreateFileMapping((HANDLE) 0xFFFFFFFF, // use paging file + NULL, // no security attributes + PAGE_READWRITE, // read/write access + 0, // size: high 32-bits + sizeof(DWORD), // size: low 32-bits + "timermem"); // name of map object + + if (hMapObject == NULL) + return NULL; + + // The first process to attach initializes memory. + fInit = (GetLastError() != ERROR_ALREADY_EXISTS); + + // Get a pointer to the file-mapped shared memory. + lastTime = (DWORD*) MapViewOfFile(hMapObject, // object to map view of + FILE_MAP_WRITE, // read/write access + 0, // high offset: map from + 0, // low offset: beginning + 0); // default: map entire file + + if (lastTime == NULL) + return NULL; + + *lastTime = GetTickCount(); + + return lastTime; +} + + +LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam) { + if (code < 0) + return CallNextHookEx(keyHook, code, wParam, lParam); + + if (lastTime == NULL) + lastTime = setup_shared_mem(); + + if (lastTime) + *lastTime = GetTickCount(); + + return 0; +} + + +LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam) { + if (code < 0) + return CallNextHookEx(mouseHook, code, wParam, lParam); + + if (lastTime == NULL) + lastTime = setup_shared_mem(); + + if (lastTime) + *lastTime = GetTickCount(); + + return 0; +} + + +EXPORT DWORD wgaim_get_lastactive() { + if (lastTime == NULL) + lastTime = setup_shared_mem(); + + if (lastTime) + return *lastTime; + + return 0; +} + + +EXPORT BOOL wgaim_set_idlehooks() { + // Set up the shared memory. + lastTime = setup_shared_mem(); + if (lastTime == NULL) + return FALSE; + *lastTime = GetTickCount(); + + // Set up the keyboard hook. + keyHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, 0); + if (keyHook == NULL) { + UnmapViewOfFile(lastTime); + CloseHandle(hMapObject); + return FALSE; + } + + // Set up the mouse hook. + mouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstance, 0); + if (mouseHook == NULL) { + UnhookWindowsHookEx(keyHook); + UnmapViewOfFile(lastTime); + CloseHandle(hMapObject); + return FALSE; + } + + return TRUE; +} + + +EXPORT void wgaim_remove_idlehooks() { + if (keyHook) + UnhookWindowsHookEx(keyHook); + if (mouseHook) + UnhookWindowsHookEx(mouseHook); + if (lastTime) + UnmapViewOfFile(lastTime); + if (hMapObject) + CloseHandle(hMapObject); +} + +int WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) { + switch(dwReason) { + case DLL_PROCESS_ATTACH: + g_hInstance = hInstance; + break; + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} diff -r 6488535322a1 -r 516061abad03 src/win32/IdleTracker/idletrack.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/win32/IdleTracker/idletrack.h Wed Feb 05 23:46:01 2003 +0000 @@ -0,0 +1,8 @@ +/* + * idletrack.h + */ +#include + +extern DWORD wgaim_get_lastactive(); +extern BOOL wgaim_set_idlehooks(); +extern void wgaim_remove_idlehooks();