home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / dev / c / curses / src / wgetch.c < prev    next >
C/C++ Source or Header  |  1994-02-21  |  6KB  |  207 lines

  1. /* -*-C-*-
  2.  *
  3.  *
  4.  * Filename : wgetch.c
  5.  *
  6.  * Author   : Simon J Raybould.    (sie@fulcrum.bt.co.uk).
  7.  *
  8.  * Date     : Friday 23rd August 1991.
  9.  *
  10.  * Desc     : Read a character.
  11.  *
  12.  *
  13.  * THIS CODE IS NOT PUBLIC DOMAIN
  14.  * ==============================
  15.  * 
  16.  * This code is copyright Simon J Raybould 1991, all rights are reserved.
  17.  * All code, ideas, data structures and algorithms remain the property of the
  18.  * author. Neither the whole nor sections of this code may be used as part
  19.  * of other code without the authors consent. If you wish to use some of this
  20.  * code then please email me at (sie@fulcrum.bt.co.uk).
  21.  *
  22.  * This source is not public domain, so you do not have any right to alter it
  23.  * or sell it for personal gain. The source is provided purely for reference
  24.  * purposes. You may re-compile the source with any compiler you choose.
  25.  * You must not distribute altered copies without the authors consent. My
  26.  * intention is that the source will help people isolate any bugs much more
  27.  * effectivly.
  28.  *
  29.  * Disclaimer
  30.  * ==========
  31.  *
  32.  * No implication is made as to this code being fit for any purpose at all.
  33.  * I (the author) shall not be held responsible for any loss of data or damage 
  34.  * to property that may result from its use or misuse.
  35.  *
  36.  *
  37.  * Revision History
  38.  * ================
  39.  *
  40.  * $Log: wgetch.c,v $
  41.  * Revision 1.6  1993/05/17  23:31:47  sie
  42.  * Underscores added to names.
  43.  * Speed optimisations for V2.10
  44.  *
  45.  * Revision 1.5  1992/12/25  23:39:35  sie
  46.  * GNU C port.
  47.  *
  48.  * Revision 1.4  92/06/10  23:44:59  sie
  49.  * Added serial support.
  50.  * 
  51.  * Revision 1.3  91/12/28  22:45:46  sie
  52.  * changed attrs to UBYTE from short + some tidying up.
  53.  * 
  54.  * Revision 1.2  91/12/27  10:02:38  sie
  55.  * Attempted to speed up wgetch a bit.
  56.  * 
  57.  * Revision 1.1  91/09/07  11:50:10  sie
  58.  * Initial revision
  59.  * 
  60.  *
  61.  */
  62.  
  63. static char *rcsid = "$Header: /SRC/lib/curses/src/RCS/wgetch.c,v 1.6 1993/05/17 23:31:47 sie Exp $";
  64.  
  65. #include <fcntl.h>
  66. #include "acurses.h"
  67.  
  68. wgetch(WINDOW *WinPtr)
  69. {
  70.   static unsigned char buffer[RAWBUFSIZ];
  71.   int Class, i;
  72.   struct IntuiMessage *Message;
  73.   static struct InputEvent ievent = { NULL, IECLASS_RAWKEY, 0, 0, 0 };
  74.   char c;
  75.   
  76.   if(!(_CursesFlags & CFLAG_INITSCR))  /* Haven't called initscr() */
  77.     return ERR;
  78.   
  79.   if(_CursesType == CUST_CURSES) {
  80.     if(_GetchBufPos < _GetchNChars) /* If we still have some chars then return next */
  81.       return (int)buffer[_GetchBufPos++];
  82.  
  83.     /* Else read a new buffer */
  84.     _GetchBufPos = _GetchNChars = 0;
  85.     while(!_GetchNChars) {
  86. #ifdef LATTICE
  87.       chkabort(); /* Check if INTR */
  88. #endif
  89.       /* Get message if there is one already queued */
  90.       Message = (struct IntuiMessage *)GetMsg(_CursesWindow->UserPort);
  91.       if(!Message) {
  92.     /* Nuffin yet */
  93.     if(WinPtr->_nodelay)  /* If non-blocking return ERR */
  94.       return ERR;
  95.     else {    /* Wait for character */
  96.       Wait(1<<_CursesWindow->UserPort->mp_SigBit);
  97.       Message = (struct IntuiMessage *)GetMsg(_CursesWindow->UserPort);
  98.     }
  99.       }
  100.       if(!Message)  /* Try again */
  101.     continue;
  102.       
  103.       Class = Message->Class;
  104.       switch(Class) {
  105.       case RAWKEY:
  106.     _GetchBufPos = 0;
  107.     ievent.ie_Code = Message->Code;
  108.     ievent.ie_Qualifier = Message->Qualifier;
  109.     ievent.ie_position.ie_addr = *((APTR*)Message->IAddress);
  110.     ReplyMsg((struct Message *)Message);
  111.     _GetchNChars = RawKeyConvert(&ievent, buffer, RAWBUFSIZ, 0L);
  112.     if(!_GetchNChars)  /* If no characters then try again */
  113.       break;
  114.     if(_CursesFlags & CFLAG_ECHO)
  115.       for(i=0; i<_GetchNChars; i++)
  116.         _DoEcho(WinPtr, buffer[i]);
  117.     /* Translate ANSI sequence if keypad set to TRUE */
  118.     if(_CursesFlags & CFLAG_KEYPAD) {
  119.       switch(_GetchNChars) {
  120.       case 1:
  121.         _GetchNChars = 0;  /* Translation will use up all chars */
  122.         if((_CursesFlags & CFLAG_NLCR) && (buffer[0] == '\r'))
  123.           return (int)'\n';
  124.         return (int)*buffer;
  125.       case 2:    /* ARROW KEY */
  126.         _GetchNChars = 0;  /* Translation will use up all chars */
  127.         if(buffer[0] != 155)
  128.           return -1;
  129.         switch(buffer[1]) {
  130.         case 'A':
  131.           return KEY_UP;
  132.         case 'B':
  133.           return KEY_DOWN;
  134.         case 'C':
  135.           return KEY_RIGHT;
  136.         case 'D':
  137.           return KEY_LEFT;
  138.         default:
  139.           return -1;
  140.         }
  141.       case 3:    /* FUNCTION KEY */
  142.         _GetchNChars = 0;  /* Translation will use up all chars */
  143.         if(buffer[0] != 155)
  144.           return -1;
  145.         if(buffer[2] != 126)
  146.           return -1;
  147.         if(buffer[1] == 63)
  148.           return KEY_HELP;
  149.         return KEY_F0 + (buffer[1] - 47);  /* KEY_F(1) = F1 */
  150.       default:
  151.         _GetchNChars = 0;  /* Translation will use up all chars */
  152.         return -1;
  153.       }
  154.     }
  155.     break;
  156.       default:
  157.     ReplyMsg((struct Message *)Message);
  158.     break;
  159.       }
  160.     }
  161.     if((_CursesFlags & CFLAG_NLCR) && (buffer[_GetchBufPos] == '\r')) {
  162.       _GetchBufPos++;
  163.       return (int)'\n';
  164.     }
  165.     return (int)buffer[_GetchBufPos++];
  166.   } else if(_CursesType == ANSI_CURSES) {
  167.     if((WinPtr->_nodelay) && (!WaitForChar(_ifh, 10000L)))
  168.       return ERR;        /* if nodelay and no char ready */
  169.     read(0, &c, 1);
  170.     if(_CursesFlags & CFLAG_ECHO)
  171.       _DoEcho(WinPtr, c);
  172.     if((_CursesFlags & CFLAG_NLCR) && (c == '\r'))
  173.       return (int)'\n';
  174.  
  175.     if(c == ESC && WaitForChar(_ifh, 10000L)) { /* if ESC wait for [ */
  176.       read(0, &c, 1);
  177.       if(_CursesFlags & CFLAG_ECHO)
  178.     _DoEcho(WinPtr, c);
  179.       if(c != '[')
  180.     return -1;
  181.     } else if((unsigned char)c != 0x9b)    /* if not ESC of 9b then return */
  182.       return (int)c;
  183.  
  184.     /* Got CSI, wait for rest */
  185.     if(!WaitForChar(_ifh, 10000L))
  186.       return -1;
  187.     read(0, &c, 1);
  188.     if(_CursesFlags & CFLAG_ECHO)
  189.       _DoEcho(WinPtr, c);
  190.  
  191.     if(_CursesFlags & CFLAG_KEYPAD) {
  192.       switch(c) {
  193.       case 'A':
  194.     return KEY_UP;
  195.       case 'B':
  196.     return KEY_DOWN;
  197.       case 'C':
  198.     return KEY_RIGHT;
  199.       case 'D':
  200.     return KEY_LEFT;
  201.       default:
  202.     return -1;
  203.       }      
  204.     }
  205.   }
  206. }
  207.