Changeset 2271

Show
Ignore:
Timestamp:
07/18/08 05:17:06 (4 months ago)
Author:
jteh
Message:

keyHook and mouseHook:

  • Remove the isInitialized variable, which is unnecessary and was never used properly anyway. The hook ID Is now used to determine whether the hook has been initialized.
  • initialize():
    • SetWindowsHookEx? returns NULL on failure, so only check for NULL instead of <= 0.
    • Can no longer be called twice without an intervening call to terminate().
  • terminate():
    • Fix the issue where this function never unregistered the hook at all due to the broken use of isInitialized.
    • Now returns an int like initialize(): 0 on success, -1 on failure.
    • -1 is returned if already initialized or the hook could not be unregistered.
    • Clean up state so that initialize() can be called again if desired.
  • Update copyright headers.
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • keyHook/keyHook.c

    r1146 r2271  
    11//keyHook.c 
    2 //Copyright (c) 2007 Michael Curran <mick@kulgan.net> 
     2//Copyright (c) 2007-2008 Michael Curran <mick@kulgan.net> 
    33//This file is covered by the GNU General Public Licence 
    4 //See the file Copying for details. 
     4//See the file copying.txt for details. 
    55 
    66#define UNICODE 
     
    1313HINSTANCE moduleHandle; 
    1414HHOOK keyHookID=0; 
    15 BOOL isInitialized=FALSE; 
    1615 
    1716#pragma comment(linker,"/entry:_DllMainCRTStartup@12") 
     
    4746 
    4847int initialize(keyCallback_t downCallback, keyCallback_t upCallback) { 
    49         if(isInitialized) { 
     48        if(keyHookID) { 
    5049                fprintf(stderr,"Already initialized\n"); 
    5150                return -1; 
     
    5352        keyDownCallback=downCallback; 
    5453        keyUpCallback=upCallback; 
    55         if((keyHookID=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)keyHook,moduleHandle,0))<=0) { 
     54        if(!(keyHookID=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)keyHook,moduleHandle,0))) { 
    5655                fprintf(stderr,"Error registering key hook\n"); 
    5756                return -1; 
     
    6059} 
    6160 
    62 void terminate() { 
    63         if(isInitialized) { 
    64                 UnhookWindowsHookEx(keyHookID); 
     61int terminate() { 
     62        if(keyHookID) { 
     63                if(UnhookWindowsHookEx(keyHookID)) { 
     64                        keyHookID=0; 
     65                        return 0; 
     66                } 
    6567        } 
     68        return -1; 
    6669} 
  • keyHook/keyHook.h

    r1146 r2271  
    1 //Copyright (c) 2007 Michael Curran <mick@kulgan.net> 
     1//Copyright (c) 2007-2008 Michael Curran <mick@kulgan.net> 
    22//This file is covered by the GNU General Public Licence 
    33 
     
    1212//Functions 
    1313DLLEXPORT int initialize(keyCallback_t downCallback, keyCallback_t upCallback); 
    14 DLLEXPORT void terminate(); 
     14DLLEXPORT int terminate(); 
    1515 
    1616#endif 
  • mouseHook/MOUSEHook.c

    r1147 r2271  
    11//mouseHook.c 
    2 //Copyright (c) 2007 Michael Curran <mick@kulgan.net> 
     2//Copyright (c) 2007-2008 Michael Curran <mick@kulgan.net> 
    33//This file is covered by the GNU General Public Licence 
    4 //See the file Copying for details. 
     4//See the file copying.txt for details. 
    55 
    66#define UNICODE 
     
    1212HINSTANCE moduleHandle; 
    1313HHOOK mouseHookID=0; 
    14 BOOL isInitialized=FALSE; 
    1514 
    1615#pragma comment(linker,"/entry:_DllMainCRTStartup@12") 
     
    3736 
    3837int initialize(mouseCallback_t callback) { 
    39         if(isInitialized) { 
     38        if(mouseHookID) { 
    4039                fprintf(stderr,"Already initialized\n"); 
    4140                return -1; 
    4241        } 
    4342        mouseCallback=callback; 
    44         if((mouseHookID=SetWindowsHookEx(WH_MOUSE_LL,(HOOKPROC)mouseHook,moduleHandle,0))<=0) { 
     43        if(!(mouseHookID=SetWindowsHookEx(WH_MOUSE_LL,(HOOKPROC)mouseHook,moduleHandle,0))) { 
    4544                fprintf(stderr,"Error registering mouse hook\n"); 
    4645                return -1; 
     
    4948} 
    5049 
    51 void terminate() { 
    52         if(isInitialized) { 
    53                 UnhookWindowsHookEx(mouseHookID); 
     50int terminate() { 
     51        if(mouseHookID) { 
     52                if(UnhookWindowsHookEx(mouseHookID)) { 
     53                        mouseHookID=0; 
     54                        return 0; 
     55                } 
    5456        } 
     57        return -1; 
    5558} 
  • mouseHook/MOUSEHook.h

    r1147 r2271  
    1 //Copyright (c) 2007 Michael Curran <mick@kulgan.net> 
     1//Copyright (c) 2008 Michael Curran <mick@kulgan.net> 
    22//This file is covered by the GNU General Public Licence 
    33 
     
    1212//Functions 
    1313DLLEXPORT int initialize(mouseCallback_t callback); 
    14 DLLEXPORT void terminate(); 
     14DLLEXPORT int terminate(); 
    1515 
    1616#endif