home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / life2 / lifehook.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-12  |  4.0 KB  |  115 lines

  1. #define INCL_DOSMODULEMGR
  2. #define INCL_WINHOOKS
  3. #define INCL_WININPUT
  4. #define INCL_WINMESSAGEMGR
  5. #include <os2.h>
  6. #include "life.h"
  7.  
  8. #define DLL_NAME                 "LIFEHOOK"
  9.  
  10. HOOKDATA hdData;
  11.  
  12. BOOL EXPENTRY LifeHook(HAB habAnchor,PQMSG pqmMsg,ULONG ulFlags)
  13. //-------------------------------------------------------------------------
  14. // This is the input hook that is used to detect activity (the lack of
  15. // this is, thus, inactivity).  Whenever we see a key press or mouse message,
  16. // notify the application.
  17. //
  18. // Since we can be called in the context of *any* application running, we
  19. // cannot simply store a pointer to the instance data of the saver window,
  20. // unless we use shared memory.  However, we can declare a variable in the
  21. // address space of the DLL and (assuming the DLL attributes are DATA SHARED
  22. // SINGLE) store a pointer to it in the saver window instance data (in
  23. // LifeInit() ).
  24. //
  25. // Input:  habAnchor - handle of the anchor block of the application to
  26. //                     receive this message
  27. //         pqmMsg - pointer to the QMSG structure to be passed to the
  28. //                  application via WinGetMsg/WinPeekMsg
  29. //         ulFlags - specifies whether or not the message will be removed
  30. //                   from the queue.
  31. // Returns:  TRUE if the rest of the hook chain is not to be called, or FALSE
  32. //           if the rest of the hook chain should be called.
  33. //-------------------------------------------------------------------------
  34. {
  35.    switch (pqmMsg->msg) {
  36.    case WM_CHAR:
  37.       if (hdData.hwndLife!=NULLHANDLE) {
  38.          WinSendMsg(hdData.hwndLife,LFM_INPUT,0,0);
  39.       } /* endif */
  40.       break;
  41.    case WM_MOUSEMOVE:
  42.    case WM_BUTTON1DOWN:
  43.    case WM_BUTTON2DOWN:
  44.    case WM_BUTTON3DOWN:
  45.    case WM_BUTTON1UP:
  46.    case WM_BUTTON2UP:
  47.    case WM_BUTTON3UP:
  48.    case WM_BUTTON1DBLCLK:
  49.    case WM_BUTTON2DBLCLK:
  50.    case WM_BUTTON3DBLCLK:
  51.       //-------------------------------------------------------------------
  52.       // See the notes in WM_TIMER/TID_SECONDS section of LIFE.C
  53.       //-------------------------------------------------------------------
  54.       if (hdData.bSaving && (hdData.usMouseKludge>0)) {
  55.          hdData.usMouseKludge--;
  56.       } else
  57.       if (hdData.hwndLife!=NULLHANDLE) {
  58.          WinSendMsg(hdData.hwndLife,LFM_INPUT,0,0);
  59.       } /* endif */
  60.       break;
  61.    default:
  62.       break;
  63.    } /* endswitch */
  64.  
  65.    return FALSE;
  66. }
  67.  
  68. BOOL EXPENTRY LifeInit(HWND hwndApp,PPHOOKDATA pphdData)
  69. //-------------------------------------------------------------------------
  70. // This function initializes the DLL and sets the input hook.
  71. //
  72. // Input:  hwndApp - handle of the Life Saver application settings window
  73. //         pphdData - pointer to the pointer to the hook data
  74. // Returns:  TRUE if successful, FALSE otherwise
  75. //-------------------------------------------------------------------------
  76. {
  77.    hdData.usSzStruct=sizeof(HOOKDATA);
  78.    hdData.hwndLife=hwndApp;
  79.  
  80.    if (DosQueryModuleHandle(DLL_NAME,&hdData.hmModule)) {
  81.       return FALSE;
  82.    } /* endif */
  83.  
  84.    hdData.bSaving=FALSE;
  85.    hdData.usMouseKludge=0;
  86.  
  87.    WinSetHook(WinQueryAnchorBlock(hdData.hwndLife),
  88.               NULLHANDLE,
  89.               HK_INPUT,
  90.               (PFN)LifeHook,
  91.               hdData.hmModule);
  92.  
  93.    *pphdData=&hdData;
  94.    return TRUE;
  95. }
  96.  
  97. BOOL EXPENTRY LifeTerm(VOID)
  98. //-------------------------------------------------------------------------
  99. // This function releases the input hook and broadcasts a WM_NULL message
  100. // to all top level windows so that they will release the DLL.  If we don't
  101. // do this, the DLL will remain locked and we'll have to reboot in order to
  102. // recompile.
  103. //
  104. // Returns:  TRUE always
  105. //-------------------------------------------------------------------------
  106. {
  107.    WinReleaseHook(WinQueryAnchorBlock(hdData.hwndLife),
  108.                   NULLHANDLE,
  109.                   HK_INPUT,
  110.                   (PFN)LifeHook,
  111.                   hdData.hmModule);
  112.    WinBroadcastMsg(HWND_DESKTOP,WM_NULL,0,0,BMSG_FRAMEONLY|BMSG_POST);
  113.    return TRUE;
  114. }
  115.