home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / monitr.txt < prev    next >
Text File  |  1995-03-23  |  5KB  |  180 lines

  1. /* monitor.c - a demonstration keyboard monitor program that runs in
  2.    any full screen session. The program is an example of the C Set++
  3.    32-bit mixed model.
  4.  
  5.    This monitor "encrypts" keystrokes by incrementing the ASCII code.
  6.  
  7.    To run this monitor:
  8.  
  9.         DETACH monitor > MON.TXT <CR>
  10.  
  11.    To terminate - press the ESCape key.
  12.  
  13.    compile with: ICC /Kb monitor.c
  14.  
  15. */
  16.  
  17. #define INCL_BASE
  18. #include <os2.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23.  
  24. #define OFFSET_SESSION_ID 0x18
  25. #define HEAD_OF_LIST      1
  26. #define ESCAPE            27
  27. #define FOREVER           for (;;)
  28.  
  29.  
  30. /* The following two sets of lines would normally be contained in a .H
  31.    "monitor.h" file. Instead they are embedded in this file to emphasize
  32.    how the 16-bit APIs are treated within a 32-bit mixed model program.
  33.  
  34.    The define statements are used to create the monitor API names as they
  35.    are known inside the os2386.lib file.
  36.  
  37.    The APIRET16... statements are the prototypes for the 16-bit monitor
  38.    APIs.
  39. */
  40.  
  41.  
  42. #define DosGetInfoSeg   DOS16GETINFOSEG
  43. #define DosMonOpen      DOS16MONOPEN
  44. #define DosMonReg       DOS16MONREG
  45. #define DosMonRead      DOS16MONREAD
  46. #define DosMonWrite     DOS16MONWRITE
  47.  
  48. APIRET16 APIENTRY16 DosGetInfoSeg ( PUSHORT, PUSHORT );
  49. APIRET16 APIENTRY16 DosMonOpen    ( PSZ, PUSHORT);
  50. APIRET16 APIENTRY16 DosMonReg     ( SHANDLE, PBYTE, PBYTE, USHORT, SHORT);
  51. APIRET16 APIENTRY16 DosMonRead    ( PBYTE, USHORT, PBYTE, PUSHORT);
  52. APIRET16 APIENTRY16 DosMonWrite   ( PBYTE, PBYTE, USHORT);
  53.  
  54. /* monbuff - structure for monitor input and output buffers */
  55.  
  56. typedef struct _monbuff
  57. {
  58.    USHORT  usLength;
  59.    BYTE    reserved [18];
  60.    BYTE    data [16];
  61. }  MONBUFF, *PMONBUFF;
  62.  
  63. /* workbuff - structure that monitor uses to process keystroke packets */
  64.  
  65. typedef struct _workbuff
  66. {
  67.    USHORT       usMonFlag;     /* monitor flag word */
  68.    KBDKEYINFO   keybuff;       /* KdbCharIn structure */
  69.    USHORT       keyddFflags;   /* device driver flags */
  70. }  WORKBUFF, *PWORKBUFF;
  71.  
  72. int main (void)
  73.  
  74. {
  75.    USHORT    usGlobalSel;     /* selector for global info */
  76.    USHORT    usLocalSel;      /* selector for local info */
  77.    PBYTE     pGinfo;          /* 16:16 pointer to global info */
  78.  
  79. /* variables for monitor APIs */
  80.  
  81.    MONBUFF   monBuffIn;       /* device driver puts data here */
  82.    MONBUFF   monBuffOut;      /* device driver gets data from here */
  83.    WORKBUFF  workBuff;        /* application work area for filtering */
  84.    USHORT    cbWorkLength;    /* length of work buffer */
  85.    USHORT hMonitor;           /* monitor handle */
  86.    APIRET16 rc;
  87.  
  88. /* obtain a selector for the global information segment so we can
  89.    determine the foreground full screen session ID - this 16-bit API
  90.    is required since there is no corresponding 32-bit API that will
  91.    accomplish this job */
  92.  
  93.    rc = DosGetInfoSeg (&usGlobalSel, &usLocalSel);
  94.  
  95.    if (rc)
  96.      {
  97.        printf ("DosGetInfoSeg Error, rc = %1d\n",rc);
  98.        exit (1);
  99.      }
  100.  
  101. /* convert 16 bit selector and offset to a 16:16 style address.
  102.    MAKEP is a toolkt20 supplied macro that shifts left by 16 bits the
  103.    first argument and then ORs in the second argument */
  104.  
  105.    pGinfo = MAKEP (usGlobalSel, OFFSET_SESSION_ID);
  106.  
  107.    printf ("screen group (session ID) = %1d\n",*pGinfo);
  108.  
  109. /* see if the driver supports a monitor and if so, get the handle */
  110.  
  111.    rc = DosMonOpen ("KBD$",&hMonitor);
  112.  
  113.    if (rc)
  114.      {
  115.        printf ("DosMonOpen Error, rc = %1d\n",rc);
  116.        exit (1);
  117.      }
  118.  
  119. /* initialize the input/output buffer fields */
  120.  
  121.    memset (&monBuffIn,  0, sizeof (MONBUFF));
  122.    memset (&monBuffOut, 0, sizeof (MONBUFF));
  123.    monBuffIn.usLength  = sizeof (MONBUFF);
  124.    monBuffOut.usLength = sizeof (MONBUFF);
  125.  
  126. /* register the monitor buffers into the chain and
  127.    request head of the list position and register the
  128.    current full screen foreground session using the
  129.    session id variable found with DodGetInfoSeg */
  130.  
  131.    rc = DosMonReg (hMonitor,
  132.                    (PBYTE)&monBuffIn,
  133.                    (PBYTE)&monBuffOut,
  134.                    (USHORT)HEAD_OF_LIST,
  135.                    (SHORT)*pGinfo);
  136.  
  137.    if (rc)
  138.      {
  139.        printf ("DosMonReg Error, rc = %1d\n",rc);
  140.        exit (1);
  141.      }
  142.  
  143. /* read keystrokes until the ESCape key is pressed
  144.    "encrypt" each keystroke by incrementing the ASCII code */
  145.  
  146.    FOREVER
  147.  
  148.    {
  149.  
  150.        cbWorkLength = sizeof ( WORKBUFF );
  151.  
  152.        rc = DosMonRead ((PBYTE)&monBuffIn,
  153.                         (USHORT)0,
  154.                         (PBYTE)&workBuff,
  155.                         (PUSHORT)&cbWorkLength);
  156.  
  157.        if (rc)
  158.          {
  159.            printf ("DosMonRead Error, rc = %1d\n",rc);
  160.            exit (1);
  161.          }
  162.  
  163.        if (workBuff.keybuff.chChar == ESCAPE)
  164.            exit (0);
  165.  
  166.        workBuff.keybuff.chChar++;
  167.  
  168.        rc = DosMonWrite ((PBYTE)&monBuffOut,
  169.                          (PBYTE)&workBuff,
  170.                           cbWorkLength);
  171.  
  172.        if (rc)
  173.          {
  174.            printf ("DosMonWrite Error, rc = %1d\n",rc);
  175.            exit (1);
  176.          }
  177.    }
  178.    return 0;
  179. }
  180.