home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snpd9707.zip / KB_STUFF.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  108 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  KB_STUFF.C - Functions to stuff characters and/or strings into a PC's
  5. **               (BIOS) keyboard buffer.
  6. **
  7. **  Original Copyright 1988-1991 by Bob Stout as part of
  8. **  the MicroFirm Function Library (MFL)
  9. **
  10. **  The user is granted a free limited license to use this source file
  11. **  to create royalty-free programs, subject to the terms of the
  12. **  license restrictions specified in the LICENSE.MFL file.
  13. */
  14.  
  15. #include <dos.h>
  16. #include "sniptype.h"
  17. #include "pchwio.h"
  18. #include "snipkbio.h"
  19.  
  20. static unsigned head, tail, start, end;
  21. static int idx = 0;
  22. static unsigned short keystack[16][2];
  23.  
  24. /*
  25. **  ungetkey()
  26. **
  27. **  Stuffs characters into the keyboard buffer.
  28. **
  29. **  Parameters: 1 - Extended character to stuff
  30. **
  31. **  Returns: Success_ or EOF
  32. **
  33. **  Note: This function assumes that the keyboard buffer is in
  34. **        the normal (for IBM) location of 40:1E.
  35. **
  36. */
  37.  
  38. int ungetkey(unsigned short key)
  39. {
  40.       int count;
  41.  
  42.       head  = Peekw(0x40, 0x1a);
  43.       tail  = Peekw(0x40, 0x1c);
  44.       start = Peekw(0x40, 0x80);
  45.       end   = Peekw(0x40, 0x82);
  46.  
  47.       count = tail - head;
  48.       if (0 > count)
  49.             count += (16 * sizeof(unsigned));
  50.       count >>= 1;
  51.  
  52.       if (15 > count)
  53.       {
  54.             disable();
  55.             keystack[idx][0] = Peekw(0x40, tail);
  56.             keystack[idx][1] = tail;
  57.             Pokew(0x40, tail, key);
  58.             tail += sizeof(unsigned);
  59.             if (end <= tail)
  60.                   tail = start;
  61.             Pokew(0x40, 0x1c, tail);
  62.             enable();
  63.             return key;
  64.       }
  65.       return EOF;
  66. }
  67.  
  68. /*
  69. **  KB_stuff()
  70. **
  71. **  Stuffs strings into the keyboard buffer.
  72. **
  73. **  Parameters: 1 - String to stuff
  74. **
  75. **  Returns: Success_ if successful
  76. **           Error_   in case of error, plus keyboard buffer is
  77. **                    restored
  78. **
  79. **  Note: This function assumes that the keyboard buffer is in
  80. **        the normal (for IBM) location of 40:1E.
  81. */
  82.  
  83. int KB_stuff(char *str)
  84. {
  85.       int ercode = Success_;
  86.  
  87.       idx = 0;
  88.       while (*str)
  89.       {
  90.             if (EOF == ungetkey((unsigned)(*str++)))
  91.             {
  92.                   disable();
  93.                   while (0 <= --idx)
  94.                   {
  95.                         tail = keystack[idx][1];
  96.                         Pokew(0x40, tail, keystack[idx][0]);
  97.                   }
  98.                   Pokew(0x40, 0x1c, tail);
  99.                   enable();
  100.                   ercode = Error_;
  101.                   break;
  102.             }
  103.             else  ++idx;
  104.       }
  105.       idx = 0;
  106.       return ercode;
  107. }
  108.