home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / EDITOR / MG2A_SRC.ZIP / SYS / ATARI / TTYIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-23  |  5.3 KB  |  201 lines

  1. /* ttyio.c -- Low-level Atari ST terminal input/output handling
  2.  *
  3.  * author :  Sandra Loosemore (from an earlier version by dec-rex!conroy)
  4.  * date   :  24 Oct 1987
  5.  * changes:  Marion Hakanson -- Jan 1988
  6.  *
  7.  */
  8.  
  9. #include    "..\..\def.h"
  10.  
  11. int    nrow;                /* Terminal size, rows.        */
  12. int    ncol;                /* Terminal size, columns.    */
  13.  
  14. #ifdef DO_METAKEY
  15. #define RSHIFT   (0x01)
  16. #define LSHIFT     (0x02)
  17. #define CTRL     (0x04)
  18. #define ALTKEY     (0x08)
  19. #define CAPSLOCK (0x10)
  20.  
  21. static struct keytab {
  22.     char *unshift;
  23.     char *shift;
  24.     char *capslock;
  25.     } *keytable;
  26.  
  27.  
  28. /* Mess with the bit that controls whether we get back all the shift keys
  29.  *    on each keystroke.
  30.  */
  31.  
  32. static unsigned char oldconterm;
  33. static unsigned char *conterm = (char *)0x00000484;
  34.  
  35. static savect ()
  36. {   oldconterm = *conterm;
  37.     *conterm = (oldconterm | 0x8);
  38.     }
  39.  
  40. static restct ()
  41. {   *conterm = oldconterm;
  42.     }
  43. #endif /* DO_METAKEY */
  44.  
  45.  
  46. /* Setup routines.  "getnrow" and "getncol" are assembly language routines.
  47.  */
  48.  
  49. VOID ttopen()
  50. {   nrow = getnrow();
  51.     if (nrow > NROW)
  52.     nrow = NROW;
  53.     ncol = getncol();
  54.     if (ncol > NCOL)
  55.     ncol = NCOL;
  56.     tcinsl = tcdell = (nrow * 2) + (ncol / 10);
  57. #ifdef DO_METAKEY
  58.     (VOID) Supexec(savect);
  59.     keytable = (struct keytab *)Keytbl(-1L,-1L,-1L);
  60. #endif /* DO_METAKEY */
  61.     }
  62.  
  63. VOID ttclose()
  64. {
  65. #ifdef DO_METAKEY
  66.     (VOID) Supexec(restct);
  67. #endif /* DO_METAKEY */
  68.     }
  69.  
  70.  
  71. /* Keystrokes are returned as 10-bit quantities.
  72.  *
  73.  * Codes 0-255 are used for the usual character set.
  74.  * Codes 256-511 are used for function keys.
  75.  * Bit 10 (0x200) is the meta bit.
  76.  *
  77.  */
  78. #ifdef FKEYS
  79.  
  80. static int keycodes[] = {
  81.     0x00,    /* dummy for F0 entry */
  82.     0x3b,    0x3c,    0x3d,    0x3e,    0x3f,
  83.     0x40,    0x41,    0x42,    0x43,    0x44,
  84.     0x54,    0x55,    0x56,    0x57,    0x58,
  85.     0x59,    0x5a,    0x5b,    0x5c,    0x5d,
  86.     0x62,    0x61,    0x52,    0x48,    0x47,
  87.     0x4b,    0x50,    0x4d
  88.         };
  89.  
  90. char *keystrings[] = {
  91. "",        /* dummy for F0 entry */
  92. "F1",        "F2",        "F3",        "F4",        "F5",
  93. "F6",        "F7",        "F8",        "F9",        "F10",
  94. "F11",        "F12",        "F13",        "F14",        "F15",
  95. "F16",        "F17",        "F18",        "F19",        "F20",
  96. "Help",        "Undo",        "Insert",    "Up",        "Clr/Home",
  97. "Left",        "Down",        "Right"
  98. };
  99. #endif /* FKEYS */
  100.  
  101. /* Use the ALT key as a meta key.  The problem with this is that it
  102.  *    appears to trash the ascii key code in the low order byte.
  103.  *    Therefore, we have to resort to looking up the key in the
  104.  *    system keystroke translation table.
  105.  * Some non-US keyboards apparently use some ALT combinations to
  106.  *    get real, printing characters.  If you've got one of these
  107.  *    beasts you can use meta-key-mode to turn off recognition
  108.  *    of the ALT key, in which case this routine just returns
  109.  *    whatever BIOS gave as the key value.  If that approach is
  110.  *    distasteful, you can also bind a function key to return
  111.  *    the ALT characters usurped by the meta key mode.
  112.  */
  113.  
  114. KCHAR getkbd()
  115. {   register int code, bchar;
  116. #ifdef FKEYS
  117.     register int i;
  118. #endif /* FKEYS */
  119. #ifdef DO_METAKEY
  120.     register int shifts;
  121.     extern int use_metakey;        /* set in the generic kbd.c */
  122. #endif /* DO_METAKEY */
  123.     union {
  124.         unsigned long k_rawkey;
  125.         struct {
  126.             unsigned char kb_shifts;    /* shift bits */
  127.             unsigned char kb_code;    /* scan code */
  128.             unsigned char kb_notused;
  129.             unsigned char kb_bchar;    /* bios char */
  130.         } k_break;
  131.     } keyval;
  132.  
  133.     keyval.k_rawkey = Bconin(2);
  134.     code   = keyval.k_break.kb_code;
  135.  
  136. #ifdef FKEYS
  137.     for (i=KFIRST; i<=KLAST; i++)            /* Was it an Fkey? */
  138.         if (code == keycodes[i-KFIRST])
  139.         return((KCHAR)i);
  140. #endif /* FKEYS */
  141.  
  142.     bchar  = keyval.k_break.kb_bchar;
  143. #ifdef DO_METAKEY
  144.     shifts = keyval.k_break.kb_shifts;
  145.  
  146.     /*
  147.      * Rule out the case where some shift bit other than what
  148.      * we're interested in is set (if such a beast ever exists).
  149.      * If otherwise, just forget about any special handling here.
  150.      */
  151.     if (use_metakey == TRUE &&
  152.             (shifts & ~(CTRL|LSHIFT|RSHIFT|CAPSLOCK)) == ALTKEY)
  153.         if ((shifts & (CTRL|LSHIFT|RSHIFT|CAPSLOCK)) == 0) /* ALTKEY only */
  154.         return ((KCHAR)(keytable->unshift[code] | METABIT));
  155.         else if (shifts & CTRL)
  156.         return ((KCHAR)(bchar | METABIT));
  157.         else if (shifts & (LSHIFT|RSHIFT))
  158.         return ((KCHAR)(keytable->shift[code] | METABIT));
  159.         else /* (shifts & CAPSLOCK) */
  160.         return ((KCHAR)(keytable->capslock[code] | METABIT));
  161.     else
  162.         return ((KCHAR)bchar);
  163. #else
  164.     return ((KCHAR)bchar);
  165. #endif
  166.     }
  167.  
  168. /* Establish default keypad bindings.  I've only done the arrow keys
  169.  *    here.
  170.  */
  171.  
  172.  
  173. VOID ttykeymapinit() { 
  174. /*  excline("global-set-key f13 previous-line"); */    /* Up arrow */
  175. /*  excline("global-set-key f15 backward-char"); */     /* Left arrow */  
  176. /*  excline("global-set-key f16 next-line"); */        /* Down arrow */
  177. /*  excline("global-set-key f17 forward-char"); */        /* Right arrow */
  178.     }
  179.  
  180.  
  181. #ifndef NO_DPROMPT
  182. /*
  183.  * Return TRUE if we busy-wait for 2 seconds without any keystrokes;
  184.  * otherwise return FALSE.  See sleep() in misc.c for details.
  185.  */
  186.  
  187. ttwait() {
  188.     extern long read200hz();
  189.     register keyhit;
  190.     register long waitfor = 400L + Supexec(read200hz);
  191.  
  192.     while ((keyhit = typeahead()) == 0 && Supexec(read200hz) < waitfor);
  193.     if (keyhit)
  194.         return FALSE;
  195.  
  196.     return TRUE;
  197.     }
  198. #endif /* NO_DPROMPT */
  199.  
  200.  
  201.