home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / KBD.C < prev    next >
Text File  |  1995-04-20  |  23KB  |  396 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   kbd.c                                                                   */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*   keyboard processing                                                     */
  7. /*                                                                           */
  8. /* Static Functions:                                                         */
  9. /*                                                                           */
  10. /* External Functions:                                                       */
  11. /*                                                                           */
  12. /*                                                                           */
  13. /* History:                                                                  */
  14. /*                                                                           */
  15. /*   02/08/91 Creation of 32-bit SD86, from 16-bit version.                  */
  16. /*                                                                           */
  17. /*...16->32 port.                                                            */
  18. /*...                                                                        */
  19. /*... 02/08/91  100   Philip    port to 32 bit.                              */
  20. /*... 02/08/91  101   Joe       port to 32 bit.                              */
  21. /*...                                                                        */
  22. /*...Release 1.01 (07/10/92)                                                 */
  23. /*...                                                                        */
  24. /*... 05/08/92  701   Joe       Cua Interface.                               */
  25. /*...                                                                        */
  26. /*...Release 1.02 (10/22/92)                                                 */
  27. /*...                                                                        */
  28. /*... 01/12/93  808   Selwyn    Profile fixes/improvements.                  */
  29. /*... 03/29/93  818   Selwyn    Changes in the useability of ESC/ALT keys.   */
  30. /*... 04/14/93  819   Selwyn    Add /k option for keyboard only use.         */
  31. /*... 04/14/93  820   Selwyn    Add /u option to not flush k/b buffer.       */
  32. /**Includes*******************************************************************/
  33. #define INCL_16                         /* for 16-bit API                 100*/
  34. #define INCL_SUB                        /* kbd, vio, mouse routines       100*/
  35. #include "all.h"                        /* SD86 include files                */
  36.  
  37. /*
  38. Keydata structure is defined in INCLUDE\SUBCALLS.H
  39. CursorData structure is defined in INCLUDE\SUBCALLS.H
  40. KBDFLUSHBUFFER is a dll in KBDCALLS.DLL and part of DOSCALLS.LIB.
  41. KBDCHARIN is a dll in KBDCALLS.DLL and part of DOSCALLS.LIB.
  42.  
  43. */
  44.  
  45. extern VIOCURSORINFO      NormalCursor;
  46. extern VIOCURSORINFO      InsertCursor;
  47. extern VIOCURSORINFO      HiddenCursor;
  48.  
  49. extern CmdParms cmd;
  50. /*****************************************************************************/
  51. /* GetKey()                                                                  */
  52. /*                                                                           */
  53. /* Description:                                                              */
  54. /*   Get a key from the k/b and encode for our purposes.                     */
  55. /*                                                                           */
  56. /* Parameters:                                                               */
  57. /*   none                                                                    */
  58. /*                                                                           */
  59. /* Return:                                                                   */
  60. /*    k         encoded key.                                                 */
  61. /*                                                                           */
  62. /* Assumptions                                                               */
  63. /*                                                                           */
  64. /*                                                                           */
  65. /* typedef struct _KBDKEYINFO {                                              */
  66. /*        UCHAR    chChar;              --  ASCII character code             */
  67. /*        UCHAR    chScan;              --  Scan Code                        */
  68. /*        UCHAR    fbStatus;            --  State of the character           */
  69. /*        UCHAR    bNlsShift;           --  Reserved (must equal 0)          */
  70. /*        USHORT   fsState;             --  state of the shift keys          */
  71. /*        ULONG    time;                --  time stamp of key stroke         */
  72. /*        }KBDKEYINFO;                                                       */
  73. /*                                                                           */
  74. /*                                                                           */
  75. /*  if chChar = 0  then chScan contains extended codes. (for example alt-esc)*/
  76. /*              E0 then chScan contains extended codes for:                  */
  77. /*                 Insert,Home,PgUp,Delete,End,PgDn,left,right,up,down       */
  78. /*  if chScan = E0 then we have the PadEnter key.                            */
  79. /*                                                                           */
  80. /*  if chChar is not 0 or E0, then chChar contains the ASCII representation  */
  81. /*  of the key and chScan "may", I repeat, "may" contain the scan code.      */
  82. /*                                                                           */
  83. /*****************************************************************************/
  84.  
  85. #define SHIFT_REPORT    0x0001                                          /*701*/
  86. #define ALT_KEYDOWN     0x0008                                          /*701*/
  87.  
  88.  
  89. uint                                    /* return an int for the key      101*/
  90. GetKey( )                               /* get a character from the K/B      */
  91. {                                       /* begin main loop                   */
  92.  uint   k;                              /* encoded key.                   101*/
  93.  KBDKEYINFO  key;                       /* define k/b parms                  */
  94.  KBDINFO     KbdInfo;
  95.  USHORT      fWait;                     /* wait/nowait on character.      101*/
  96.  HKBD        hkbd;                      /* keyboard handle. ( = 0 )       101*/
  97.  static uint StatusSet = FALSE;                                         /*701*/
  98.  
  99. /*****************************************************************************/
  100. /* First we flush the keyboard buffer and get key packet.                    */
  101. /*                                                                           */
  102. /*****************************************************************************/
  103.  hkbd  = 0;                             /* default kbd handle.            101*/
  104.  fWait = 0;                             /* wait on the key.               101*/
  105.  
  106.  if( !NoFlushBuffer() )                                                 /*820*/
  107.    KbdFlushBuffer(hkbd);                /* clear the keystroke buffer     101*/
  108.  
  109.  if( StatusSet == FALSE )                                               /*701*/
  110.  {                                                                      /*701*/
  111.    KbdInfo.cb = 10;                     /* length of record (always 10).  701*/
  112.    KbdInfo.fsMask = 0x0006;             /*                            818 701*/
  113.    KbdInfo.fsInterim = 0;                                               /*701*/
  114.    KbdInfo.fsState = 0;                                                 /*701*/
  115.  
  116.    KbdSetStatus( &KbdInfo, hkbd );                                      /*701*/
  117.    StatusSet = TRUE;                                                    /*701*/
  118.  }                                                                      /*701*/
  119.  
  120.  for( ;; )
  121.  {
  122.   (void)KbdCharIn((PKBDKEYINFO)&key,fWait,hkbd);      /* read a char.     101*/
  123.   /***************************************************************************/
  124.   /* We are going to encode the key for our usage.                           */
  125.   /*                                                                         */
  126.   /*     chScan     chChar                                                   */
  127.   /* k = ________   ________                                                 */
  128.   /*                                                                         */
  129.   /* Yes, we have flopped them. DO NOT ASK ME WHY!                           */
  130.   /*                                                                         */
  131.   /*                                                                         */
  132.   /***************************************************************************/
  133.   if( key.chScan == 0xE0 )              /* if this is the PadEnter key       */
  134.      key.chScan = 0;                    /* then clear scan code.             */
  135.   if( key.chChar == 0xE0 )              /* if this is insert,home,etc. as    */
  136.      key.chChar = 0;                    /* explained above, then clear the   */
  137.                                         /* char code.                        */
  138.   k = ( key.chScan << 8) | key.chChar;  /* encode the key...right.           */
  139.  
  140. /*****************************************************************************/
  141. /* Our encoding scheme will encode all spacebars the same do we have to      */
  142. /* check individual shift states and modify coding accordingly.              */
  143. /*                                                                           */
  144. /*****************************************************************************/
  145.   if( k == SPACEBAR )                   /* if the key encodes to a spacebar  */
  146.   {                                     /* then we want to check for alt and */
  147.    if( key.fsState & 0x0A00 )           /* shift states. check for alt keys. */
  148.     k = A_SPACEBAR;                     /*                                   */
  149.    if( key.fsState & 0x0500 )           /* check for ctrl keys.              */
  150.     k = C_SPACEBAR;                     /*                                   */
  151.    if( key.fsState & 0x0003 )           /* check for ctrl keys.           808*/
  152.     k = S_SPACEBAR;                     /*                                808*/
  153.   }                                     /*                                   */
  154. /*****************************************************************************/
  155. /* Insert and Shift-insert will return the same chChar and chScan. We have   */
  156. /* to look at the shift state to distinguish.                                */
  157. /*                                                                           */
  158. /*****************************************************************************/
  159.   if( k == INS )                        /* if the key encodes to an insert   */
  160.   {                                     /* then we want to check for the     */
  161.    if( key.fsState & 0x0003 )           /* shift states and change to        */
  162.     k = S_INS;                          /* shift insert if necessary.        */
  163.    if( key.fsState & 0x0A00 )           /* shift states and change to     808*/
  164.     k = A_INS;                          /* Alt   insert if necessary.     808*/
  165.   }                                     /*                                   */
  166.   if( !NoFlushBuffer() )                                                /*820*/
  167.     KbdFlushBuffer(hkbd);               /* clear the keystroke buffer     101*/
  168.   return( k );                          /* return k/b character k            */
  169.  }
  170. }
  171.  
  172.     void
  173. PutCsr( CSR *csr )
  174. {
  175.  VIOCURSORINFO  *p;                     /*                                101*/
  176.  USHORT          usRow;                 /*                                101*/
  177.  USHORT          usColumn;              /*                                101*/
  178.  HVIO            hvio;                  /*                                101*/
  179.                                         /*                                101*/
  180.  usRow    = csr->row;                   /*                                101*/
  181.  usColumn = csr->col;                   /*                                101*/
  182.  hvio     = 0;                          /*                                101*/
  183.                                         /*                                101*/
  184.  VioSetCurPos( usRow, usColumn, hvio ); /*                                101*/
  185.  
  186.     switch( csr->mode ){
  187.       default:
  188.       case CSR_NORMAL:
  189.         p = &NormalCursor;  break;
  190.       case CSR_INSERT:
  191.         p = &InsertCursor;  break;
  192.       case CSR_HIDDEN:
  193.         p = &HiddenCursor;  break;
  194.     }
  195.     VioSetCurType(p,hvio);              /*                                101*/
  196. }
  197.  
  198.     void
  199. HideCursor( )
  200. {
  201.  HVIO            hvio;                  /*                                101*/
  202.  VIOCURSORINFO  *p;                     /*                                101*/
  203.  
  204.  hvio = 0;                              /*                                101*/
  205.  p = (PVIOCURSORINFO)&HiddenCursor;     /*                                101*/
  206.  VioSetCurType( p, hvio );              /*                                101*/
  207. }
  208.  
  209.     void
  210. beep( )
  211. {
  212.     DosBeep( 1800/*Hz*/ , 1/*millisecond*/ );
  213. }
  214.  
  215. /*****************************************************************************/
  216. /* NoFlushBuffer()                                                        820*/
  217. /*                                                                           */
  218. /* Description:                                                              */
  219. /*                                                                           */
  220. /*   Checks whether the keyboard buffer flush flag is set.                   */
  221. /*                                                                           */
  222. /* Parameters:                                                               */
  223. /*                                                                           */
  224. /*   None.                                                                   */
  225. /*                                                                           */
  226. /* Return:                                                                   */
  227. /*                                                                           */
  228. /*   TRUE       Keyboard flush buffer flag is turned on.                     */
  229. /*   FALSE      Keyboard flush buffer flag is turned off.                    */
  230. /*                                                                           */
  231. /*****************************************************************************/
  232. int  NoFlushBuffer()
  233. {
  234.   if( cmd.KBDBufferFlush )
  235.     return( TRUE );
  236.   else
  237.     return( FALSE );
  238. }
  239.  
  240. /*****************************************************************************/
  241. /* SetFlushBufferFlag()                                                   820*/
  242. /*                                                                           */
  243. /* Description:                                                              */
  244. /*                                                                           */
  245. /*   Sets the keyboard flush buffer flag.                                    */
  246. /*                                                                           */
  247. /* Parameters:                                                               */
  248. /*                                                                           */
  249. /*   Flag       Value of the keyboard flush buffer flag.                     */
  250. /*                                                                           */
  251. /* Return:                                                                   */
  252. /*                                                                           */
  253. /*   None.                                                                   */
  254. /*                                                                           */
  255. /*****************************************************************************/
  256. void SetFlushBufferFlag( int Flag )
  257. {
  258.   /***************************************************************************/
  259.   /* If the flag was set to "No buffer flush all times" dont change it.      */
  260.   /***************************************************************************/
  261.   if( cmd.KBDBufferFlush != NOFLUSHALLTIMES )
  262.     cmd.KBDBufferFlush = Flag;
  263. }
  264.  
  265. /*****************************************************************************/
  266. /* ResetFlushBufferFlag()                                                 820*/
  267. /*                                                                           */
  268. /* Description:                                                              */
  269. /*                                                                           */
  270. /*   Resets the keyboard flush buffer flag.                                  */
  271. /*                                                                           */
  272. /* Parameters:                                                               */
  273. /*                                                                           */
  274. /*   None.                                                                   */
  275. /*                                                                           */
  276. /* Return:                                                                   */
  277. /*                                                                           */
  278. /*   None.                                                                   */
  279. /*                                                                           */
  280. /*****************************************************************************/
  281. void ResetFlushBufferFlag()
  282. {
  283.   /***************************************************************************/
  284.   /* If the flag was set to "No buffer flush all times" dont reset it.       */
  285.   /***************************************************************************/
  286.   if( cmd.KBDBufferFlush != NOFLUSHALLTIMES )
  287.     cmd.KBDBufferFlush = 0;
  288. }
  289.  
  290. /*****************************************************************************/
  291. /* SetHotKey()                                                               */
  292. /*                                                                           */
  293. /* Description:                                                              */
  294. /*                                                                           */
  295. /*   Turn off/on the alt-esc and ctrl-esc keys.                              */
  296. /*                                                                           */
  297. /* Parameters:                                                               */
  298. /*                                                                           */
  299. /* Return:                                                                   */
  300. /*                                                                           */
  301. /* Assumptions:                                                              */
  302. /*                                                                           */
  303. /*****************************************************************************/
  304. void SetHotKey()
  305. {
  306.  APIRET rc;
  307.  HFILE  FileHandle = 0;
  308.  
  309.  /****************************************************************************/
  310.  /* - First, open the keyboard.                                              */
  311.  /****************************************************************************/
  312.  {
  313.   ULONG ActionTaken   = 0;
  314.   ULONG FileSize      = 0;
  315.   ULONG FileAttribute = FILE_SYSTEM;
  316.   ULONG OpenFlag      = OPEN_ACTION_OPEN_IF_EXISTS;
  317.   ULONG OpenMode      = OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE;
  318.  
  319.   rc = DosOpen((PSZ)"KBD$",
  320.                (PHFILE)&FileHandle,
  321.                (PULONG)&ActionTaken,
  322.                (ULONG)FileSize,
  323.                (ULONG)FileAttribute,
  324.                (ULONG)OpenFlag,
  325.                (ULONG)OpenMode,
  326.                (PEAOP2) NULL) ;
  327.  }
  328.  
  329.  
  330.  /****************************************************************************/
  331.  /* - Turn off ctrl-esc and alt-esc.                                         */
  332.  /****************************************************************************/
  333.  if( rc == 0 )
  334.  {
  335.   HOTKEY HotKey;
  336.   ULONG  ParmLengthInOut;
  337.   ULONG  DataLengthInOut;
  338.  
  339.   HotKey.fsHotKey         = 0;
  340.   HotKey.uchScancodeMake  = 0;
  341.   HotKey.uchScancodeBreak = 0;
  342.   HotKey.idHotKey         = 0xFFFF;
  343.  
  344.   ParmLengthInOut = sizeof (HotKey);
  345.   DataLengthInOut = 0;
  346.  
  347.   rc = DosDevIOCtl( FileHandle,
  348.             (ULONG) IOCTL_KEYBOARD,
  349.             (ULONG) KBD_SETSESMGRHOTKEY,
  350.             (PVOID) &HotKey,
  351.             (ULONG) ParmLengthInOut,
  352.             (PULONG) &ParmLengthInOut,
  353.             (PVOID) NULL,
  354.             (ULONG) 0L,
  355.             (PULONG) &DataLengthInOut);
  356.  
  357.  
  358.  }
  359.  
  360.  /****************************************************************************/
  361.  /* - Close the keyboard.                                                    */
  362.  /****************************************************************************/
  363.  if( FileHandle != 0) DosClose( FileHandle ) ;
  364. }
  365.  
  366. /*****************************************************************************/
  367. /* Convert                                                                   */
  368. /*                                                                           */
  369. /* Description:                                                              */
  370. /*                                                                           */
  371. /*   Convert a key scan code to its appropriate function code.               */
  372. /*                                                                           */
  373. /* Parameters:                                                               */
  374. /*                                                                           */
  375. /*   keycode    scan code passed in to be converted.                         */
  376. /*                                                                           */
  377. /* Return:                                                                   */
  378. /*   funcode    function code associated with the scan code passed in.       */
  379. /*                                                                           */
  380. /*****************************************************************************/
  381. extern KEY2FUNC defk2f[];
  382. int Convert(uint keycode)
  383. {
  384.   uint i;
  385.  
  386.   for(
  387.        i = 0;
  388.        i < KEYNUMSOC;
  389.        i++
  390.      )
  391.     if ( defk2f[i].scode == keycode )
  392.       return( defk2f[i].fcode );
  393.  
  394.   return( DONOTHING );
  395. }
  396.