home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / GETSCRN.EXE / SOURCE / MONITOR.C < prev    next >
Text File  |  1990-06-24  |  5KB  |  189 lines

  1. /*
  2.  
  3.  
  4.  
  5.  
  6.                                 MONITOR.c
  7.  
  8.         Copyright (c) 1990 by:  Arthur Kevin McGrath
  9.                                 Contract Engineers
  10.                                 P. O. Box 128
  11.                                 Barboursville, VA  22923
  12.  
  13.                                 703/832-7025
  14.  
  15.  
  16.   ALL RIGHTS ARE RESERVED.  You may not copy this program in any way
  17.   except to make back-up copies FOR YOUR OWN USE.  If you copy this
  18.   program for any reason without WRITTEN PERMISSION from the above
  19.   named copyright owner (except to make back-up copies FOR YOUR OWN USE),
  20.   you are breaking the Copyright Laws of the United States.  You will go
  21.   to jail for one year and pay a $50,000 fine.
  22.  
  23.  
  24.  
  25.  
  26. */
  27.  
  28.  
  29.  
  30. #include        <stdio.h>
  31. #include        <string.h>
  32. #include        <stdlib.h>
  33. #include        <ctype.h>
  34.  
  35.  
  36.  
  37. #define INCL_BASE
  38. #define INCL_VIO
  39. #include <os2.h>
  40.  
  41. #include "capture.h"
  42. #include "extern.h"
  43.  
  44.  
  45. #define DEFAULT         0
  46. #define END_KEY         0x75
  47. #define PRINT_SCREEN    0x72
  48.  
  49.  
  50. struct kbd_packet
  51. {
  52.     USHORT      monitor_flag;
  53.     KBDKEYINFO  c;
  54.     ULONG       time;
  55.     USHORT      device_driver_flag;
  56. };
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /*  MONITOR_THE_KEYBOARD() will pass every character that comes from
  67.     the keyboard untill it sees an <CONTROL><PRINT SCREEN> keystroke
  68.     or a <CONTROL><END> keystroke.
  69.     At that time, it will:
  70.             1.  eat the <CONTROL><PRINT SCREEN> keystroke so that it
  71.                 goes no further
  72.                 (Scan Code 0x72    Character Code 0x00 )
  73.             2.  Capture the Physical Screen Buffer to a file of
  74.                 the user's choice
  75.                 or
  76.             3.  END the monitor when it receives a <CONTROL><END>
  77.                 keystroke.
  78.                 (Scan Code 0x75     Character Code 0x00 or 0xE0)
  79.  
  80.     Only one buffer capture can be active at any one time.  This
  81.     function shall guarantee that the capture code is never
  82.     re-entered.
  83.  
  84.     This routine requires the following parameters in order to do its job:
  85.  
  86.     This routine returns the following eror codes:
  87.         NO_ERROR        if everything went well.
  88.         something else  if there was an error.  */
  89. int monitor_the_keyboard()
  90. {
  91.     USHORT              error;
  92.     HMONITOR            handle;
  93.     MONIN               input[10];
  94.     MONOUT              output[10];
  95.     SEL                 local, global, selector;
  96.     USHORT              session;
  97.     PLINFOSEG           ldt;
  98.     struct kbd_packet   raw;
  99.     USHORT              packet_size;
  100.     BYTE                *new_stack;
  101.     TID                 id;
  102.  
  103.  
  104.     /*  Get some data that we need in order to register
  105.         a monitor with the keyboard device driver.  */
  106.     error = DosGetInfoSeg( &global, &local );
  107.     ldt = MAKEPLINFOSEG( local );
  108.     session = ldt -> sgCurrent;
  109.  
  110.     input[0].cb = sizeof( input );
  111.     output[0].cb = sizeof( output );
  112.     packet_size = sizeof( raw );
  113.  
  114.  
  115.     error = DosMonOpen( "KBD$", &handle );
  116.  
  117.     /*  Register ourselves as a character monitor with the KEYBOARD.    */
  118.     error = DosMonReg( handle,
  119.                     ( BYTE *)&input,
  120.                     ( BYTE *)&output,
  121.                     DEFAULT,
  122.                     session );
  123.  
  124.     do
  125.     {
  126.         /*  Read from the monitor chain.    */
  127.         error = DosMonRead( ( BYTE *)&input,
  128.                             DCWW_WAIT,
  129.                             ( BYTE *)&raw,
  130.                             &packet_size    );
  131.  
  132.  
  133.         /*  Don't allow the <CONTROL><PrintScreen> character AND
  134.             don't allow the <CONTROL><END> character to go back
  135.             into the keyboard data stream.          */
  136.         if(     !(raw.c.fsState & CONTROL)  ||  (raw.c.chScan != END_KEY)
  137.                                     &&
  138.                 !(raw.c.fsState & CONTROL)  ||  (raw.c.chScan != PRINT_SCREEN)     )
  139.         {
  140.             /*  Write the character back to the monitor chain.  */
  141.             error = DosMonWrite( (BYTE *)&output,
  142.                             ( BYTE *)&raw,
  143.                             packet_size    );
  144.  
  145.  
  146.         }
  147.         else if( (raw.c.fsState & CONTROL)
  148.                         &&
  149.             (raw.c.chScan == PRINT_SCREEN)
  150.                         &&
  151.             (       !capturing       )          )
  152.         {
  153.             /*  Set the flag that shows that we are currently
  154.                 capturing the contents of the Physical Video Buffer */
  155.             capturing = TRUE;
  156.  
  157.             /*  Allocate the memory for a stack for a new thread.   */
  158.             DosAllocSeg( STACK_SIZE,
  159.                         &selector,
  160.                         RESERVED    );
  161.  
  162.             new_stack = MAKEP( selector, 0 );
  163.  
  164.             /*  Mark the stack so we can trace stack useage.    */
  165.             mark_stack( ( CHAR *)new_stack,
  166.                         STACK_SIZE          );
  167.  
  168.             /*  Capture the contents of the current PHYSICAL VIDEO BUFFER.  */
  169.             DosCreateThread(  capture_the_screen,
  170.                                 &id,
  171.                                 ( new_stack + STACK_SIZE - 2 )  );
  172.  
  173.  
  174.         }
  175.  
  176.  
  177.  
  178.     }while( !(raw.c.fsState & CONTROL)
  179.                     ||
  180.             (raw.c.chScan != END_KEY)   );
  181.  
  182.     /*  Close the monitor when we are done with it. */
  183.     error = DosMonClose( handle );
  184.  
  185.  
  186.     return( error );
  187.  
  188. }
  189.