home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / thesrc15.zip / getch.c < prev    next >
C/C++ Source or Header  |  1993-11-26  |  11KB  |  296 lines

  1. /*
  2.  * This software is Copyright (c) 1989, 1990, 1991 by Patrick J. Wolfe.
  3.  *
  4.  * Permission is hereby granted to copy, distribute or otherwise
  5.  * use any part of this package as long as you do not try to make
  6.  * money from it or pretend that you wrote it.  This copyright
  7.  * notice must be maintained in any copy made.
  8.  *
  9.  * Use of this software constitutes acceptance for use in an AS IS
  10.  * condition. There are NO warranties with regard to this software.
  11.  * In no event shall the author be liable for any damages whatsoever
  12.  * arising out of or in connection with the use or performance of this
  13.  * software.  Any use of this software is at the user's own risk.
  14.  *
  15.  * If you make modifications to this software that you feel
  16.  * increases it usefulness for the rest of the community, please
  17.  * email the changes, enhancements, bug fixes as well as any and
  18.  * all ideas to me. This software is going to be maintained and
  19.  * enhanced as deemed necessary by the community.
  20.  *
  21.  *              Patrick J. Wolfe
  22.  *              uunet!uiucuxc!kailand!pwolfe
  23.  *              pwolfe@kailand.kai.com
  24.  *
  25.  * Additions of Xenix,Sun,DOS,VMS,AIX and OS2 key handling
  26.  * made by Mark Hessling (M.Hessling@gu.edu.au)
  27.  *
  28.  */
  29.  
  30.  
  31. /*
  32. $Header: C:\THE\RCS\getch.c 1.4 1993/09/01 16:26:30 MH Interim MH $
  33. */
  34.  
  35. #if defined(USE_NCURSES)
  36. #   include <ncurses.h>
  37. #else
  38. #   include <curses.h>
  39. #endif
  40.  
  41. #if !defined(DOS) && !defined(OS2)
  42. #include "getch.h"
  43.  
  44. #define NORMAL 100
  45. #define ESCAPE 200
  46. #define FKEY   300
  47.  
  48. #ifdef PROTO
  49. int my_getch (WINDOW *winptr)
  50. #else
  51. int my_getch (winptr)
  52. WINDOW *winptr;
  53. #endif
  54. {
  55. int c;
  56. int state = NORMAL;
  57. int fkeycount = 0;
  58.  
  59. while (1) {
  60. #ifndef VMS
  61.        c = wgetch(winptr);
  62. #else
  63.        c = keypress();
  64. #endif
  65.        switch (state) {
  66.  
  67.        case FKEY:
  68.                switch (c) {
  69.  
  70.                /* numeric function keys */
  71.                case '0': case '1': case '2': case '3': case '4':
  72.                case '5': case '6': case '7': case '8': case '9':
  73.                        fkeycount = (fkeycount * 10) + (c - '0');
  74.                        break;
  75.  
  76.                case '~':
  77.                        switch (fkeycount) {
  78.  
  79.                        /* Find, Insert Here, Remove, Select, Prev Screen, Next Screen */
  80.                        case 1: return KEY_Find;
  81.                        case 2: return KEY_InsertHere;
  82.                        case 3: return KEY_Remove;
  83.                        case 4: return KEY_Select;
  84.                        case 5: return KEY_PrevScreen;
  85.                        case 6: return KEY_NextScreen;
  86.  
  87.                        /* unshifted vt220 function keys */
  88.                        case 17: case 18: case 19: case 20: case 21:
  89.                                return KEY_F6 + (fkeycount - 17);
  90.                        case 23: case 24: case 25: case 26:
  91.                                return KEY_F11 + (fkeycount - 23);
  92.                        case 28: case 29:
  93.                                return KEY_F15 + (fkeycount - 28);
  94.                        case 31: case 32: case 33: case 34:
  95.                                return KEY_F17 + (fkeycount - 31);
  96.  
  97.                        /* vt220 function keys - control */
  98.                        case 37: case 38: case 39: case 40: case 41:
  99.                                return KEY_SF6 + (fkeycount - 37);
  100.                        case 43: case 44: case 45: case 46:
  101.                                return KEY_SF11 + (fkeycount - 43);
  102.                        case 48: case 49:
  103.                                return KEY_SF15 + (fkeycount - 48);
  104.                        case 51: case 52: case 53: case 54:
  105.                                return KEY_SF17 + (fkeycount - 51);
  106.  
  107.                        /* shifted tvs922 function keys */
  108.         /*             case 37: case 38: case 39: case 40: case 41:
  109.                        case 42: case 43: case 44: case 45: case 46:
  110.                        case 47: case 48: case 49: case 50: case 51:
  111.                                return KEY_SF6 + (fkeycount - 37); */
  112.  
  113.                        default:
  114.                                state = NORMAL;
  115.                                }
  116.                        break;
  117.  
  118.                case 'A':       return KEY_UP;
  119.                case 'B':       return KEY_DOWN;
  120.                case 'C':       return KEY_RIGHT;
  121.                case 'D':       return KEY_LEFT;
  122.                case 'M':       return KEY_PadEnter;
  123.                case 'Z':       return KEY_BackTab;
  124.  
  125.                /* Xenix default key mappings */
  126.                case 'H':       return KEY_HOME;
  127.                case 'F':       return KEY_END;
  128.                case 'L':       return KEY_InsertHere;
  129.                case 'G':       return KEY_NextScrn;
  130.                case 'I':       return KEY_PrevScrn;
  131.  
  132.                case 'N':       return KEY_F2;
  133.                case 'O':       return KEY_F3;
  134.                case 'T':       return KEY_F8;
  135.                case 'U':       return KEY_F9;
  136.                case 'V':       return KEY_F10;
  137.                case 'W':       return KEY_F11;
  138.                case 'X':       return KEY_F12;
  139.  
  140.                /* VT[12]00 PF keys */
  141.                case 'P': case 'Q': case 'R': case 'S':
  142.                        return KEY_PF1 + (c - 'P');
  143.  
  144.                /* VT[12]00 keypad */
  145.                case 'l': case 'm': case 'n': case 'o': case 'p': case 'r':
  146.                case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y':
  147.                        return KEY_PadComma + (c - 'l');
  148.  
  149.                /* Sun Keyboard Function Keys */
  150.                case 'z':
  151.                        switch (fkeycount) {
  152.                        /* SUN F1-F10 */
  153.                        case 224: case 225: case 226: case 227: case 228:
  154.                        case 229: case 230: case 231: case 232: case 233:
  155.                                return KEY_F1 + (fkeycount - 224);
  156.                        /* SUN S-F1-S-F10 */
  157.                        case 324: case 325: case 326: case 327: case 328:
  158.                        case 329: case 330: case 331: case 332: case 333:
  159.                                return KEY_SF1 + (fkeycount - 324);
  160.                        case 192: return KEY_F11;
  161.                        case 193: return KEY_F12;
  162.                        case 195: return KEY_UNDO;
  163.                        case 292: return KEY_SF11;
  164.                        case 293: return KEY_SF12;
  165.                        case 214: return KEY_HOME;
  166.                        case 414: return KEY_CHOME;
  167.                        case 215: return KEY_UP;
  168.                        case 415: return KEY_CUP;
  169.                        case 216: return KEY_PrevScrn;
  170.                        case 416: return KEY_CPGUP;
  171.                        case 217: return KEY_LEFT;
  172.                        case 417: return KEY_CLEFT;
  173.                        case 219: return KEY_RIGHT;
  174.                        case 419: return KEY_CRIGHT;
  175.                        case 220: return KEY_END;
  176.                        case 420: return KEY_CEND;
  177.                        case 221: return KEY_DOWN;
  178.                        case 421: return KEY_CDOWN;
  179.                        case 222: return KEY_NextScrn;
  180.                        case 422: return KEY_CPGDN;
  181.                        case 2:   return KEY_InsertHere;
  182.                        case 423: return KEY_PadComma;
  183.                        default:
  184.                                state = NORMAL;
  185.                                }
  186.                        break;
  187.  
  188.                /* IBM AIX ???????? */
  189.                case 'q':
  190.                        switch (fkeycount) {
  191.                        case 0:  /* VT100/200 keypad */
  192.                                return KEY_PadComma + (c - 'l');
  193.                        /* AIX F1-F12 */
  194.                        case 1: case 2: case 3: case 4: case 5: case 6:
  195.                        case 7: case 8: case 9: case 10: case 11: case 12:
  196.                                return KEY_F1 + (fkeycount - 1);
  197.                        /* AIX SF1-F12 */
  198.                        case 13: case 14: case 15: case 16: case 17: case 18:
  199.                        case 19: case 20: case 21: case 22: case 23: case 24:
  200.                                return KEY_SF1 + (fkeycount - 13);
  201.                        /* AIX CF1-F12 */
  202.                        case 25: case 26: case 27: case 28: case 29: case 30:
  203.                        case 31: case 32: case 33: case 34: case 35: case 36:
  204.                                return KEY_CF1 + (fkeycount - 25);
  205.                        case 150: return KEY_PrevScrn;
  206.                        case 146: return KEY_END;
  207.                        case 154: return KEY_NextScrn;
  208.                        case 139: return KEY_InsertHere;
  209.                        default:
  210.                                state = NORMAL;
  211.                                }
  212.                        break;
  213.  
  214.                default:
  215.                        state = NORMAL;
  216.                        }
  217.                break;
  218.  
  219.        case ESCAPE:
  220.                switch (c) {
  221.                case 'O':     /* vt100 numeric keypad application codes */
  222.                case '?':     /* vt52  numeric keypad application codes */
  223.                case '[':
  224.                        state = FKEY;
  225.                        fkeycount = 0;
  226.                        break;
  227.                /* VT52 PF keys */
  228.                case 'P': case 'Q': case 'R': case 'S':
  229.                        return KEY_PF1 + (c - 'P');
  230.  
  231.                default:
  232.                        state = NORMAL;
  233.                        }
  234.                break;
  235.  
  236.        default:
  237.                switch (c) {
  238.                case Escape:
  239.                        state = ESCAPE;
  240.                        break;
  241.  
  242.                case CSI:
  243.                        state = FKEY;
  244.                        fkeycount = 0;
  245.                        break;
  246.  
  247.                default:
  248.                        return (c);
  249.                        }
  250.                }
  251.        }
  252. }
  253. #endif
  254. #if defined(DOS) || defined(OS2)
  255. int my_getch (winptr)
  256. WINDOW *winptr;
  257. {
  258.  return(wgetch(winptr));
  259. }
  260. #endif
  261. #ifdef VMS
  262. #include iodef
  263. #include descrip
  264. /***********************************************************************/
  265. int keypress()
  266. /***********************************************************************/
  267. {
  268. /*--------------------------- local data ------------------------------*/
  269. struct { long length; char *address; } logical_name;
  270. struct { short status; short length; int remainder; } iosb;
  271.  
  272. static char kb[] = { "sys$input" };
  273. static int chan;
  274.  
  275. static char key = 0;
  276. int new_key;
  277. static int first = 1;
  278. int status;
  279. /*--------------------------- processing ------------------------------*/
  280.  
  281.  key = 0;
  282.  logical_name.length = strlen (kb);
  283.  logical_name.address = kb;
  284.  status = sys$assign (&logical_name, &chan, 0, 0);
  285.  if (status != 1)
  286.    return(-1);
  287.  status = SYS$QIOW(0, chan, IO$_READVBLK | IO$M_NOFILTR | IO$M_NOECHO
  288.         | IO$M_TIMED, &iosb, 0, 0, &key, 1,600, 0,0, 0, 0);
  289.  if (!key)
  290.     return (0);
  291.  new_key = (int)(unsigned)(key);
  292.  status = sys$dassgn (chan);
  293.  return (new_key);
  294. }
  295. #endif
  296.