home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / os / os2 / programm / 3923 < prev    next >
Encoding:
Text File  |  1992-07-30  |  6.8 KB  |  193 lines

  1. Newsgroups: comp.os.os2.programmer
  2. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!ira.uka.de!chx400!bernina!neptune!rofische
  3. From: rofische@iiic.ethz.ch (Roman Fischer)
  4. Subject: Solved: Sample code for a input hook
  5. Message-ID: <1992Jul31.094117.23462@neptune.inf.ethz.ch>
  6. Originator: rofische@c13
  7. Sender: news@neptune.inf.ethz.ch (Mr News)
  8. Nntp-Posting-Host: c13
  9. Organization: Dept. Informatik, Swiss Federal Institute of Technology (ETH)
  10. Date: Fri, 31 Jul 1992 09:41:17 GMT
  11. Lines: 180
  12.  
  13. XFEEL   a sample program for a input hook
  14. =========================================
  15.  
  16. I finished this version of XFEEL yesterday. It is in no way perfect
  17. or correct, but it shows how to install a input hook. So it may be
  18. a help for those who are currently writting hook-procedures.
  19.  
  20. The only documentation available to me was the OS/2 Programer's
  21. Reference from Microsoft (for OS/2 1.1 !). Hooks are discussed in
  22. chapter 28 in Vol. 1. If you read chapter 28 carefully, you can find
  23. the following inconsitency:
  24. Input hooks are defined (28.3.1) with three parameters, but in the
  25. example (28.5.2) only two parameters are specified. This wouldn't be
  26. to bad if C-calling conventions were used, but since all exported
  27. procedures use PASCAL-callings, specifying the wrong number of para-
  28. meters will crash the program !
  29. And also, the documentation says nothing about the fact that the
  30. program, that installes the hook, MUST have a message loop !
  31.  
  32. Well, after solving the above problems i noticed that the hook
  33. procedure can't access global variables (neither in the installing
  34. program nor in the dll). That's why i used a named piece of sharemem
  35. to store some data.
  36.  
  37. The idea for XFEEL was of course to aktivate a window by just moving
  38. the mouse over it (a la X-Windows). Right now there is one known
  39. problem: you can't drag anything around while XFEEL is active !
  40.  
  41. Since this code is just the start, all kinds of ideas, bugs, hints
  42. and comments are welcome. Just let me know...
  43.  
  44. bye                   Roman
  45.  
  46.  
  47. Some remarks on the code:
  48.  
  49. I'm develloping my OS/2 Programms with TopSpeed C 3.01 from JPI, so you'll
  50. have to write your own make-files (i love jpi-project-managment.. :-)  ).
  51.  
  52.  
  53. /*-------------------------------------------------------------------------*/
  54. /*                                                                         */
  55. /*   XFEEL.C    : Compile this Module to XFEEL.EXE                         */
  56. /*                                                                         */
  57. /*   Version    : V1.00                                                    */
  58. /*                                                                         */
  59. /*   Date       : 31.07.92                                                 */
  60. /*                                                                         */
  61. /*   Copyright  : rf for Animal                                            */
  62. /*                                                                         */
  63. /*-------------------------------------------------------------------------*/
  64.  
  65. #define INCL_DOSPROCESS
  66. #define INCL_WINHOOKS
  67. #define INCL_WINBUTTONS
  68. #define INCL_WINFRAMEMGR
  69. #define INCL_DOSSEMAPHORES
  70. #define INCL_DOSMEMMGR
  71. #define INCL_DOSMODULEMGR
  72. #include <os2pm.h>
  73.  
  74. /*--------------------------------------------------------------------------*/
  75. MRESULT EXPENTRY myproc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  76.     {
  77.     return (WinDefWindowProc (hwnd, msg, mp1, mp2));
  78.     }
  79.  
  80. /*--------------------------------------------------------------------------*/
  81. unsigned long GetAddr(HMODULE *handle, char *filename, char *command)
  82.     {
  83.     char            errorname[6];
  84.     unsigned long   adr;
  85.  
  86.     if (DosLoadModule (errorname, 5, filename, handle))
  87.         return (0L);
  88.  
  89.     if (DosGetProcAddr (*handle, command, &adr))
  90.         return (0L);
  91.  
  92.     return (adr);
  93.     }
  94.  
  95. /*--------------------------------------------------------------------------*/
  96. void main ()
  97.     {
  98.     HAB         hab;
  99.     HMQ         hmq;
  100.     QMSG        qmsg;
  101.     HMODULE     mhand;
  102.     ULONG       adr;
  103.     SEL         sel;
  104.     HWND        hwnd, hwndclient;
  105.     ULONG       ff = FCF_TASKLIST;
  106.  
  107.     hab = WinInitialize (0);
  108.     hmq = WinCreateMsgQueue (hab, 0);
  109.  
  110.     WinRegisterClass (hab, "XFEELCLASS", myproc, 0L, 0);
  111.     hwnd = WinCreateStdWindow (HWND_DESKTOP, 0, &ff, "XFEELCLASS",
  112.                     NULL, 0L, NULL, 0, &hwndclient);
  113.  
  114.  
  115.     if (DosAllocShrSeg (sizeof (HWND), "\\SHAREMEM\\XFEEL.DAT", &sel))
  116.         {
  117.         WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
  118.                  "Could not allocate Memory. Program aborted !",
  119.                  NULL, 0, MB_OK|MB_ICONEXCLAMATION);
  120.         return;
  121.         }
  122.  
  123.     adr = GetAddr (&mhand, "XFEEL", "XFUNC");
  124.     if (!WinSetHook (hab, (HMQ)NULL, HK_INPUT, (PFN)adr, mhand))
  125.         {
  126.         WinMessageBox (HWND_DESKTOP, HWND_DESKTOP,
  127.                  "Could not Install Hook. Program aborted !",
  128.                  NULL, 0, MB_OK|MB_ICONEXCLAMATION);
  129.         return;
  130.         }
  131.  
  132.     while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  133.         WinDispatchMsg (hab, &qmsg);
  134.  
  135.  
  136.     WinReleaseHook (hab, (HMQ)NULL, HK_INPUT, (PFN)adr, mhand);
  137.     DosFreeModule (mhand);
  138.  
  139.     WinDestroyWindow (hwnd);
  140.     WinDestroyMsgQueue (hmq);
  141.     WinTerminate (hab);
  142.     }
  143.  
  144. /*-------------------------------------------------------------------------*/
  145. /*                                                                         */
  146. /*   XFEEL2.C   : Compile this Module to XFEEL.DLL and place it in the     */
  147. /*                LIBPATH.                                                 */
  148. /*                                                                         */
  149. /*   Version    : V1.00                                                    */
  150. /*                                                                         */
  151. /*   Date       : 31.07.92                                                 */
  152. /*                                                                         */
  153. /*   Copyright  : rf for Animal                                            */
  154. /*                                                                         */
  155. /*-------------------------------------------------------------------------*/
  156.  
  157. #define INCL_DOSPROCESS
  158. #define INCL_WININPUT
  159. #define INCL_WINFRAMEMGR
  160. #define INCL_DOSMEMMGR
  161.  
  162. #include <os2pm.h>
  163. #include <stdio.h>
  164.  
  165. BOOL PASCAL XFUNC (HAB hab, PQMSG qmsg, USHORT msgf)
  166.     {
  167.     SEL         sel;
  168.     HWND        *hwndoldp;
  169.  
  170.     if (qmsg->msg != WM_MOUSEMOVE)      /* Intercept only Mouse-Moves */
  171.         return (FALSE);
  172.  
  173.     if (DosGetShrSeg ("\\SHAREMEM\\XFEEL.DAT", &sel))
  174.        return (FALSE);
  175.  
  176.     hwndoldp = (HWND *)MK_FP (sel, 0);
  177.     if (qmsg->hwnd != *hwndoldp)
  178.        {
  179.        WinSetFocus (HWND_DESKTOP, qmsg->hwnd);
  180.        *hwndoldp = qmsg->hwnd;
  181.        }
  182.     DosFreeSeg (sel);
  183.  
  184.     return (FALSE);
  185.     }
  186.  
  187.  
  188. have fun...                        Roman
  189. -- 
  190. ----------------------------------------------
  191. INTERNET: rofische@iiic.ethz.ch
  192. ----------------------------------------------
  193.