home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / HP150.C < prev    next >
C/C++ Source or Header  |  1991-10-11  |  13KB  |  569 lines

  1. /*
  2.  * The routines in this file provide support for HP150 screens
  3.  * and routines to access the Keyboard through KEYCODE mode.
  4.  * It compiles into nothing if not an HP150 screen device.
  5.  * added by Daniel Lawrence
  6.  */
  7.  
  8. #define    termdef    1            /* don't define "term" external */
  9.  
  10. #include        <stdio.h>
  11. #include        "estruct.h"
  12. #include    "eproto.h"
  13. #include    "edef.h"
  14. #include    "elang.h"
  15.  
  16. #if     HP150
  17.  
  18. #define NROW    24                      /* Screen size.                 */
  19. #define NCOL    80                      /* Edit if you want to.         */
  20. #define    MARGIN    8            /* size of minimim margin and    */
  21. #define    SCRSIZ    64            /* scroll size for extended lines */
  22. #define    NPAUSE    15            /* # times thru update to pause */
  23. #define BEL     0x07                    /* BEL character.               */
  24. #define ESC     0x1B                    /* ESC character.               */
  25.  
  26. extern int PASCAL NEAR  openhp();    /* Forward references.          */
  27. extern int PASCAL NEAR    hpflush();
  28. extern int PASCAL NEAR  closehp();
  29. extern int PASCAL NEAR    hp15kopen();
  30. extern int PASCAL NEAR    hp15kclose();
  31. extern int PASCAL NEAR  hp15move();
  32. extern int PASCAL NEAR  hp15eeol();
  33. extern int PASCAL NEAR  hp15eeop();
  34. extern int PASCAL NEAR  hp15beep();
  35. extern int PASCAL NEAR    gethpkey();
  36. extern int PASCAL NEAR    hp15rev();
  37. extern int PASCAL NEAR    hp15cres();
  38. #if    COLOR
  39. extern int PASCAL NEAR    hp15fcol();
  40. extern int PASCAL NEAR    hp15bcol();
  41. #endif
  42.  
  43. PASCAL NEAR hp15parm();
  44. PASCAL NEAR rawon();
  45. PASCAL NEAR rawoff();
  46. PASCAL NEAR ckeyoff();
  47. PASCAL NEAR ckeyon();
  48. PASCAL NEAR agios();
  49. PASCAL NEAR keycon();
  50. PASCAL NEAR keycoff();
  51. PASCAL NEAR defkey();
  52. PASCAL NEAR undefkey();
  53. PASCAL NEAR dsplbls();
  54.  
  55. /*    Some needed locals    */
  56.  
  57. union REGS r;        /* register set for bios and dos (AGIOS) calls */
  58. int capslock = 0;    /* caps lock flag */
  59.  
  60. /*
  61.  * Standard terminal interface dispatch table. Most of the fields point into
  62.  * "termio" code.
  63.  */
  64. TERM    term    = {
  65.     NROW-1,
  66.         NROW-1,
  67.         NCOL,
  68.         NCOL,
  69.     MARGIN,
  70.     SCRSIZ,
  71.     NPAUSE,
  72.     openhp,
  73.         closehp,
  74.     hp15kopen,
  75.     hp15kclose,
  76.     gethpkey,
  77.         ttputc,
  78.         hpflush,
  79.         hp15move,
  80.         hp15eeol,
  81.         hp15eeop,
  82.         hp15eeop,
  83.         hp15beep,
  84.         hp15rev,
  85.         hp15cres
  86. #if    COLOR
  87.     , hp15fcol,
  88.     hp15bcol
  89. #endif
  90. };
  91.  
  92. PASCAL NEAR hp15move(row, col)
  93. {
  94.         ttputc(ESC);
  95.         ttputc('&');
  96.         ttputc('a');
  97.         hp15parm(col);
  98.         ttputc('c');
  99.         hp15parm(row);
  100.         ttputc('R');
  101. }
  102.  
  103. PASCAL NEAR hpflush()
  104.  
  105. {
  106.  
  107. }
  108.  
  109. PASCAL NEAR hp15eeol()
  110. {
  111.         ttputc(ESC);
  112.         ttputc('K');
  113. }
  114.  
  115. PASCAL NEAR hp15eeop()
  116. {
  117.         ttputc(ESC);
  118.         ttputc('J');
  119. }
  120.  
  121. PASCAL NEAR hp15rev(status)    /* change the reverse video status */
  122.  
  123. int status;    /* TRUE = on, FALSE = off */
  124.  
  125. {
  126.     ttputc(ESC);
  127.     ttputc('&');
  128.     ttputc('d');
  129.     ttputc((status != FALSE) ? 'B': '@');
  130. }
  131.  
  132. PASCAL NEAR hp15cres()    /* change screen resolution */
  133.  
  134. {
  135.     return(TRUE);
  136. }
  137.  
  138. PASCAL NEAR spal()        /* change pallette register */
  139.  
  140. {
  141.     /*   not here */
  142. }
  143.  
  144. PASCAL NEAR hp15beep()
  145. {
  146.         ttputc(BEL);
  147.         ttflush();
  148. }
  149.  
  150. PASCAL NEAR hp15parm(n)
  151.  
  152. register int    n;
  153.  
  154. {
  155.         register int    q;
  156.  
  157.         q = n/10;
  158.         if (q != 0)
  159.                 hp15parm(q);
  160.         ttputc((n%10) + '0');
  161. }
  162.  
  163. #if    COLOR
  164. PASCAL NEAR hp15fcol()    /* we really can't do colors here, so just ignore it */
  165. {
  166. }
  167.  
  168. PASCAL NEAR hp15bcol()    /* we really can't do colors here, so just ignore it */
  169. {
  170. }
  171. #endif
  172.  
  173. PASCAL NEAR gethpkey()    /* get a key from the HP keyboard while in keycode mode */
  174.  
  175. {
  176.     unsigned c;        /* character to translate */
  177.     int devid;        /* device ID */
  178.     int ctype;        /* type of character gotten */
  179.     unsigned shiftb;    /* state of shift keys */
  180.     int i;            /* index in first translation loop */
  181.  
  182.     /* return any keystrokes waiting in the
  183.        type ahead buffer */
  184.     if (in_check())
  185.         return(in_get());
  186.  
  187.     /* grab the next 4 char sequence */
  188. next:    shiftb = ttgetc();
  189.     devid = ttgetc();
  190.     c = ttgetc();
  191.     ttgetc();        /* skip null byte */
  192.  
  193.     /* make sure we are from the keyboard */
  194.     if (devid != 192)
  195.         goto next;
  196.  
  197.     /* if normal ascii, return it */
  198.     if ((shiftb & 0x80) == 0) {
  199.         if (capslock && c >= 'a' && c <= 'z')
  200.             c -= 32;
  201.         return(c);
  202.     }
  203.  
  204.     /* check specifically for the caps lock key */
  205.     if (c == 0x56) {
  206.         capslock = ~capslock;
  207.         goto next;
  208.     }
  209.  
  210.     /* interpet it as an extended HP sequence */
  211.     c = extcode(shiftb, c);
  212.  
  213.     /* if it becomes standard ascii... just return it */
  214.     if ((c >> 8) == 0)
  215.         return(c & 255);
  216.  
  217.     /* or return it as en extended emacs internal sequence */
  218.     in_put(c >> 8);        /* prefix byte */
  219.     in_put(c & 255);    /* event code byte */
  220.     return(0);        /* extended escape sequence */
  221. }
  222.  
  223. /*    extcode:    resolve MSDOS extended character codes
  224.             encoding the proper sequences into emacs
  225.             printable character specifications
  226. */
  227.  
  228. int extcode(shiftb, c)
  229.  
  230. unsigned shiftb;    /* shift flag */
  231. unsigned c;        /* byte following a zero extended char byte */
  232.  
  233. {
  234.     int sstate;    /* state of the various shift keys */
  235.  
  236.     /* remember if we are shifted */
  237.     if ((shiftb & 0x04) != 0)
  238.         sstate = SHFT;
  239.     else
  240.         sstate = 0;
  241.  
  242.     /* remember if we are alted (extended char keys) */
  243.     if ((shiftb & 0x30) != 0)
  244.         sstate |= ALTD;
  245.  
  246.     /* remember if we are controled */
  247.     if ((shiftb & 0x08) != 0)
  248.         sstate |= CTRL;
  249.  
  250.     /* function keys 1 through 9 */
  251.     if (c >= 0 && c < 9)
  252.         return(sstate | SPEC | c + 1 + '0');
  253.  
  254.     /* function key 10 */
  255.     if (c == 9)
  256.         return(sstate | SPEC | '0');
  257.  
  258.     /* function key 11 */
  259.     if (c == 10)
  260.         return(sstate | SPEC | 'E');
  261.  
  262.     /* function key 12 */
  263.     if (c == 11)
  264.         return(sstate | SPEC | 'T');
  265.  
  266.     /* some others as well */
  267.     switch (c) {
  268.  
  269.         case 36:    return(sstate | 9);    /* tab */
  270.         case 37:    return(sstate | 13);    /* ret */
  271.         case 39:    return(sstate | 8);    /* backspace */
  272.         case 48:    return(sstate | 48);    /* zero */
  273.         case 49:    return(sstate | 49);    /* one */
  274.         case 50:    return(sstate | 50);    /* two */
  275.         case 51:    return(sstate | 51);    /* three */
  276.         case 52:    return(sstate | 52);    /* four */
  277.         case 53:    return(sstate | 53);    /* five */
  278.         case 54:    return(sstate | 54);    /* six */
  279.         case 55:    return(sstate | 55);    /* seven */
  280.         case 56:    return(sstate | 56);    /* eight */
  281.         case 57:    return(sstate | 57);    /* nine */
  282.         case 80:    return(sstate | 13);    /* enter */
  283.         case 84:    return(sstate | 27);    /* break -> ESC */
  284.         case 85:    return(sstate | 27);    /* esc */
  285.         case 88:    return(sstate | 24);    /* stop -> ^X */
  286.         case 112:    return(sstate | 45);    /* N-minus */
  287.         case 113:    return(sstate | 42);    /* N-asterisk */
  288.         case 114:    return(sstate | 43);    /* N-plus */
  289.         case 115:    return(sstate | 47);    /* N-slash */
  290.         case 116:    return(sstate | 44);    /* N-comma */
  291.         case 117:    return(sstate | 13);    /* N-enter */
  292.         case 118:    return(sstate | 9);    /* N-tab */
  293.         case 119:    return(sstate | 46);    /* N-period */
  294.  
  295.         case 44:
  296.         case 45:
  297.         case 110:    return(sstate | SPEC | '<');    /* HOME */
  298.  
  299.         case 32:
  300.         case 41:
  301.         case 101:    return(sstate | SPEC | 'P');    /* cursor up */
  302.  
  303.         case 47:    return(sstate | SPEC | 'Z');    /* page up */
  304.  
  305.         case 35:
  306.         case 42:
  307.         case 97:    return(sstate | SPEC | 'B');    /* cursor left */
  308.  
  309.         case 34:
  310.         case 43:
  311.         case 99:    return(sstate | SPEC | 'F');    /* cursor right */
  312.  
  313.         case 82:    return(sstate | SPEC | '>');    /* end */
  314.  
  315.         case 33:
  316.         case 40:
  317.         case 98:    return(sstate | SPEC | 'N');    /* cursor down */
  318.  
  319.         case 46:
  320.         case 108:    return(sstate | SPEC | 'V');    /* page down */
  321.  
  322.         case 64:
  323.         case 70:
  324.         case 107:    return(sstate | SPEC | 'C');    /* insert */
  325.  
  326.         case 65:
  327.         case 71:
  328.         case 109:    return(sstate | SPEC | 'D');    /* delete */
  329.  
  330.         /* the HP has some extra keys we need to map */
  331.  
  332.         case 83:
  333.         case 89:    return(sstate | SPEC | 'Q');    /* reformat paragraph */
  334.         case 81:    return(sstate | CTLX | 'C');    /* shell up to system */
  335.         case 67:    return(sstate | SPEC | CTRL | 'L'); /* center display */
  336.         case 68:    return(sstate | CTRL | 'O');    /* open line */
  337.         case 69:    return(sstate | CTRL | 'K');    /* Kill to end of line */
  338.     }
  339.  
  340.     return(sstate | c);
  341. }
  342.  
  343. PASCAL NEAR openhp()        /* open the HP150 screen for input */
  344.  
  345. {
  346.     strcpy(sres, "NORMAL");
  347.     revexist = TRUE;
  348. }
  349.  
  350. PASCAL NEAR closehp()        /* close the HP150 screen for input */
  351.  
  352. {
  353. }
  354.  
  355. PASCAL NEAR hp15kopen()        /* open the HP150 keyboard for input */
  356.  
  357. {
  358.     /* define key charectoristics with AGIOS call (0, 40) */
  359.     defkey();
  360.  
  361.     /* Turn on RAW mode with MSDOS call 44h */
  362.     rawon();
  363.  
  364.     /* Turn off Control-C checking  MS-DOS 33h */
  365.     ckeyoff();
  366.  
  367.     /* Turn on keycode mode with AGIOS call (0,43) */
  368.     keycon();
  369.  
  370.     /* display the application softkey labels */
  371.     dsplbls();
  372. }
  373.  
  374. PASCAL NEAR hp15kclose()    /* close the HP150 keyboard for input */
  375.  
  376. {
  377.     /* define key charectoristics with AGIOS call (0, 40) */
  378.     undefkey();
  379.     
  380.     /* Turn off RAW mode with MSDOS call 44h */
  381.     rawoff();
  382.  
  383.     /* Turn on Control-C checking  MS-DOS 33h */
  384.     ckeyon();
  385.  
  386.     /* Turn off keycode mode with AGIOS call (0,43) */
  387.     keycoff();
  388. }
  389.  
  390. PASCAL NEAR rawon()    /* put the HP150 keyboard into RAW mode */
  391.  
  392. {
  393.     /* get the IO control info */
  394.  
  395.     r.x.ax = 0x4400;    /* IO ctrl get device information */
  396.     r.x.bx = 0x0001;    /* File handle; 1 for console */
  397.     intdos(&r, &r);        /* go fer it */
  398.  
  399.     r.h.dh = 0;        /* clear high byte for put */
  400.     r.h.dl |= 0x20;        /* set raw bit */
  401.  
  402.     /* and put it back */
  403.  
  404.     r.x.ax = 0x4401;    /* IO ctrl put device information */
  405.     r.x.bx = 0x0001;    /* File handle; 1 for console */
  406.     intdos(&r, &r);        /* go fer it */
  407. }
  408.  
  409. PASCAL NEAR rawoff()    /* put the HP150 keyboard into COOKED mode */
  410.  
  411. {
  412.     /* get the IO control info */
  413.  
  414.     r.x.ax = 0x4400;    /* IO ctrl get device information */
  415.     r.x.bx = 0x0001;    /* File handle; 1 for console */
  416.     intdos(&r, &r);        /* go fer it */
  417.  
  418.     r.h.dh = 0;        /* clear high byte for put */
  419.     r.h.dl &= 0xdf;        /* set raw bit */
  420.  
  421.     /* and put it back */
  422.  
  423.     r.x.ax = 0x4401;    /* IO ctrl put device information */
  424.     r.x.bx = 0x0001;    /* File handle; 1 for console */
  425.     intdos(&r, &r);        /* go fer it */
  426. }
  427.  
  428. PASCAL NEAR ckeyoff()    /* turn control-C trapping off */
  429.  
  430. {
  431.     r.h.ah = 0x33;    /* ctrl-break check */
  432.     r.h.al = 1;    /* set the state of the ctrl-break check */
  433.     r.h.dl = 0;    /* turn it off */
  434.     intdos(&r, &r);
  435. }
  436.  
  437. PASCAL NEAR ckeyon()    /* turn control-C trapping on */
  438.  
  439. {
  440.     r.h.ah = 0x33;    /* ctrl-break check */
  441.     r.h.al = 1;    /* set the state of the ctrl-break check */
  442.     r.h.dl = 1;    /* turn it on */
  443.     intdos(&r, &r);
  444. }
  445.  
  446. #ifdef    unsigned
  447. #undef    unsigned
  448. #endif
  449.  
  450. PASCAL NEAR agios(buf, len)    /* perform an AGIOS call */
  451.  
  452. char *buf;    /* sequence of bytes in command */
  453. int len;    /* length of command in bytes */
  454.  
  455. {
  456.     r.x.ax = 0x4403;    /* I/O ctrl write */
  457.     r.x.bx = 1;        /* console handle */
  458.     r.x.cx = len;        /* buffer length */
  459.     r.x.dx = (unsigned)buf;    /* buffer address */
  460.     return(intdos(&r, &r));    /* do it */
  461. }
  462.  
  463. PASCAL NEAR keycon()    /* turn keycode mode on */
  464.  
  465. {
  466.     static char cmd[] = {43, 0, 1};
  467.  
  468.     return(agios(&cmd[0], 3));
  469. }
  470.  
  471. PASCAL NEAR keycoff()    /* turn keycode mode off */
  472.  
  473. {
  474.     static char cmd[] = {43, 0, 0};
  475.  
  476.     return(agios(&cmd[0], 3));
  477. }
  478.  
  479. PASCAL NEAR defkey()    /* change all special keys to intercept mode */
  480.  
  481. {
  482.     static char cmd[] = {40, 0, 2, 0, 0xfe, 0};
  483.  
  484.     return(agios(&cmd[0], 6));
  485. }
  486.  
  487. PASCAL NEAR undefkey()    /* change all special keys to intercept mode */
  488.  
  489. {
  490.     static char cmd[] = {40, 0, 0, 0, 0xfe, 0};
  491.  
  492.     return(agios(&cmd[0], 6));
  493. }
  494.  
  495. PASCAL NEAR dsplbls()    /* display the application softkey labels on the screen */
  496.  
  497. {
  498.     static char cmd[] = {11, 0};
  499.  
  500.     return(agios(&cmd[0], 2));
  501. }
  502.  
  503. #if    FLABEL
  504. PASCAL NEAR fnclabel(f, n)        /* label a function key */
  505.  
  506. int f,n;    /* default flag, numeric argument */
  507.  
  508. {
  509.     register int status;    /* return status */
  510.     register int i;        /* loop index */
  511.     char lbl[17];    /* returned label contents */
  512.     /* AGIOS command buffer */
  513.     static char cmd[] = {8, 0, 1, 0, 7, 7, 7, 7, 10, 0, 10, 0};
  514.     /*                   code  key#  ptr to      top    bottom
  515.                                      label string  attribute */
  516.     union {        /* union to cast ptr into AGIOS arg string */
  517.         char *ptr;    /* pointer to arg string */
  518.         char cstr[4];
  519.     } ptru;
  520.  
  521.     /* must have a numeric argument */
  522.     if (f == FALSE) {
  523.         mlwrite(TEXT159);
  524. /*                      "%Need function key number" */
  525.         return(FALSE);
  526.     }
  527.  
  528.     /* and it must be a legal key number */
  529.     if (n < 1 || n > 8) {
  530.         mlwrite(TEXT160);
  531. /*                      "%Function key number out of range" */
  532.         return(FALSE);
  533.     }
  534.  
  535.     /* get the string to send */
  536.     status = mlreply(TEXT161, &lbl[0], 17);
  537. /*                       "Label contents: " */
  538.     if (status != TRUE)
  539.         return(status);
  540.  
  541.     /* pad the label out */
  542.     for (i=0; i < 17; i++) {
  543.         if (lbl[i] == 0)
  544.             break;
  545.     }
  546.     for (; i < 16; i++)
  547.         lbl[i] = ' ';
  548.     lbl[16] = 0;
  549.  
  550.     /* set up the parameters */
  551.     cmd[2] = n;            /* function key number */
  552.     ptru.ptr = &lbl[0];        /* set up pointer to label string */
  553. force:    cmd[4] = ptru.cstr[0];
  554.     cmd[5] = ptru.cstr[1];
  555.     cmd[6] = ptru.cstr[2];
  556.     cmd[7] = ptru.cstr[3];
  557.  
  558.     /* and send it out */
  559.     agios(&cmd[0], 12);
  560.     return(TRUE);
  561. }
  562. #endif
  563. #else
  564. PASCAL NEAR h15hello()
  565.  
  566. {
  567. }
  568. #endif
  569.