home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34exe.zip / me / misc / emx1io.c next >
C/C++ Source or Header  |  1995-01-21  |  11KB  |  389 lines

  1. /*
  2.  * OS2IO.C: Terminal IO for OS/2
  3.  * J Burnell    3/92    Public Domain
  4.  * P McPhee    replaced signal handling code (to compile with emx)
  5.  *        copied set_color and set_cursor stuff from pcfv.c
  6.  */
  7.  
  8. /*
  9.  * The functions in this file negotiate with the operating system for
  10.  * characters, and write characters in a barely buffered fashion on the
  11.  * display.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #define INCL_BASE
  16. #define INCL_NOPM
  17. #define INCL_VIO
  18. #define INCL_KBD
  19. #include <os2.h>
  20. #include <time.h>
  21. #include "ed.h"
  22. #include "term.h"
  23. #include <signal.h>
  24. #include <string.h>
  25.  
  26. #include "me2.h"
  27. #include "config.h"
  28.  
  29.  
  30. KBDINFO initialKbdInfo; /* keyboard info        */
  31. void t_eeol ();
  32.  
  33. /*
  34.  * This function is called once to set up the terminal device streams.
  35.  */
  36. void ttopen()
  37. {
  38. #ifdef SIG_HANDLER_IS_THERE
  39.    PFNSIGHANDLER oldhandler;
  40. #endif
  41.    USHORT oldact;
  42.    KBDINFO kbdInfo;
  43.  
  44. #ifdef SIG_HANDLER_IS_THERE
  45.   /* turn off control C checking */
  46.    DosSetSigHandler((PFNSIGHANDLER) NULL, &oldhandler, &oldact,
  47.                      SIGA_IGNORE, SIG_CTRLBREAK);
  48.    DosSetSigHandler((PFNSIGHANDLER) NULL, &oldhandler, &oldact,
  49.                      SIGA_IGNORE, SIG_CTRLC);
  50. #else
  51.    /* I think this does the same thing */
  52.    signal(SIGBREAK, SIG_IGN);
  53.    signal(SIGINT, SIG_IGN);
  54. #endif
  55.   /* set up the keyboard */
  56.  
  57.     initialKbdInfo.cb = sizeof(initialKbdInfo);
  58.     KbdGetStatus(&initialKbdInfo, 0);
  59.     kbdInfo = initialKbdInfo;
  60.     kbdInfo.fsMask &= ~0x0001;      /* not echo on      */
  61.     kbdInfo.fsMask |= 0x0002;       /* echo off     */
  62.     kbdInfo.fsMask &= ~0x0008;      /* cooked mode off  */
  63.     kbdInfo.fsMask |= 0x0004;       /* raw mode     */
  64.     kbdInfo.fsMask &= ~0x0100;      /* shift report off */
  65.     KbdSetStatus(&kbdInfo, 0);
  66.  
  67. }
  68.  
  69. /*
  70.  * This function gets called just before we go back home to the command
  71.  *   interpreter.
  72.  */
  73. void t_close()
  74. {
  75.   /* close the keyboard */
  76.   KbdSetStatus(&initialKbdInfo, 0); /* restore original state   */
  77. }
  78.  
  79. /*
  80.  * Write a character to the display.
  81.  * Use the rawest console output routine that handles backspace, \r and \n.
  82.  * On MS-DOS terminal I/O is unbuffered, so we just write the byte out.
  83.  */
  84. void t_putchar(c) unsigned char c;
  85. {
  86.   VioWrtTTY ((PCH)&c, 1, 0);
  87. }
  88.  
  89. /*
  90.  * Flush terminal buffer.  Does real work where the terminal output is
  91.  *   buffered up.  A no-operation on systems where byte-at-a-time terminal
  92.  *   I/O is done.
  93.  */
  94. void t_flush() {}
  95.  
  96. /*
  97.  * Read a character from the terminal, performing no editing and doing no
  98.  *   echo at all.
  99.  * Map terminal softkeys, etc to ME keys.  Do *NOT* map control keys.
  100.  */
  101. EKeyCode t_getchar()
  102. {
  103.    EKeyCode keycode;
  104.    KBDKEYINFO keyInfo;
  105.  
  106.    KbdCharIn(&keyInfo, IO_WAIT, 0); /* get a character  */
  107.  
  108.    if (keyInfo.chChar == 0 || (keyInfo.chChar == 0xE0 && keyInfo.chChar != 0))
  109.     { if (map_key(keyInfo.chScan | (keyInfo.chChar << 8),&keycode))
  110.       return keycode; }
  111.     else return (EKeyCode)keyInfo.chChar;
  112. }
  113.  
  114. /*
  115.  * Check to see is a key is waiting.
  116.  * Used to interrupt processing if input is pending or to detect if a soft
  117.  *   key has been hit (the terminal sends multiple characters for each
  118.  *   softkey).
  119.  * Don't use function B because it checks for ^C.
  120.  */
  121. int keywaiting()
  122. {
  123.    KBDKEYINFO keyInfo;
  124.  
  125.    KbdPeek(&keyInfo, 0); /* look for a character */
  126.    return keyInfo.fbStatus & KBDTRF_FINAL_CHAR_IN;
  127. }
  128.  
  129.    /* Wait for a key to be pressed for no more than sec seconds.
  130.     * Use 0 seconds to check to see if a key is in the input que.
  131.     * Warning:  This rouine is only good for 32K seconds (about 9 hours).
  132.     *   If sec is a long the limit is still 24 hours.
  133.     */
  134. wait_for_key(sec)
  135. {
  136.   long int times_up;
  137.  
  138.   if (sec == 0) return keywaiting();
  139.   times_up = time(NULL) +sec;
  140.   do
  141.   {
  142.     if (keywaiting()) return TRUE;
  143.     /* you can't poll the keyboard if you don't take a break */
  144.     DosSleep(100);    /* 100 milliseconds (tenth of a second rule) */
  145.   } while (time(NULL) < times_up);
  146.   return FALSE;
  147. }
  148.  
  149.   /* Things to note about FastVideo:
  150.    *   putline() will do all of the writing to the text area (except for
  151.    *     help messages).
  152.    *   t_eeol() will only be called for the message line or for help
  153.    *     messages.
  154.    *   t_eeop() need only clear the message line.  It should use t_eeol() to
  155.    *     do this.
  156.    *   t_putchar() will only write to the message line, help messages or
  157.    *     (puts "message").
  158.    * So, this means that the message line is really seprate from the text
  159.    *   areas and t_putchar() and t_eeol() should use the same color (others
  160.    *   will use tcolor and mcolor).  Default for the message line would be
  161.    *   tcolor.  Help and (puts ...)  messages will be written in message
  162.    *   color (which is (I hope) reasonable).
  163.    * PC note:  Due to the way the BIOS is done, it is easier to have
  164.    *   t_putchar() and t_eeol() use the BIOS character attribute as the
  165.    *   color.
  166.    */
  167.  
  168. int t_nrow = 24, t_ncol = 0;
  169.  
  170. int tcolor = 7, mcolor = 96;    /* text, modeline color defaults */
  171. int zcolor = 7;
  172.  
  173.  /*
  174.   * Called to set up the terminal.  Also called after a spawn.
  175.   */
  176. void t_open()
  177. {
  178.   VIOCURSORINFO cursorInfo, OldcursorInfo;
  179.  
  180.   ttopen();
  181.   if (t_ncol == 0) fv_init();       /* only call fv_init() once */
  182. /* set the cursor to a block */
  183.  
  184.   VioGetCurType (&OldcursorInfo, 0);
  185.   cursorInfo.yStart = 1;
  186.   cursorInfo.cEnd = 15;
  187.   cursorInfo.cx = 1;
  188.   cursorInfo.attr = 1;
  189.   if (VioSetCurType (&cursorInfo, 0))
  190.     VioSetCurType (&OldcursorInfo, 0);   /* reset to previous value */
  191. }
  192.  
  193. int fv_init () {
  194.   VIOMODEINFO vioModeInfo;
  195.   vioModeInfo.cb = sizeof(vioModeInfo);
  196.   VioGetMode (&vioModeInfo, 0);
  197.   t_ncol = vioModeInfo.col;
  198.   t_nrow = vioModeInfo.row - 1;   /* leave room for the message line */
  199. }
  200.  
  201. void putline(row, buf, attr)    /* note: row is 0 relative */
  202.   int row,attr; char *buf;
  203. {
  204.   VioWrtCharStrAtt ((PCH) buf, t_ncol, row, 0, (PBYTE) &attr, 0);
  205. }
  206.  
  207.  /* Erase to end of page or screen.
  208.   * For fast video, each line of the screen (except for the message line)
  209.   *   will be completely filled by putline() so all this routine needs to
  210.   *   clear is the message (last) line of the screen.
  211.   */
  212. void t_eeop() { movecursor(t_nrow,0,TRUE); t_eeol(); }
  213.  
  214.   /* Ring the bell.  beeper controls the volume:  0 (don't beep), n (some
  215.    *   volume value).
  216.    */
  217. void t_beep()
  218. {
  219.   extern int beeper;
  220.   static int direction = 1;
  221.  
  222.   if (beeper) {
  223.     if (direction) {
  224.        DosBeep(440, beeper * 2);
  225.        DosBeep(660, beeper * 2);
  226.        DosBeep(880, beeper * 4);
  227.        }
  228.     else {
  229.        DosBeep(880, beeper * 2);
  230.        DosBeep(660, beeper * 2);
  231.        DosBeep(440, beeper * 4);
  232.        }
  233.     direction ^= 1;
  234.    }
  235. }
  236.  
  237. void t_move(row,col) int row, col;  /* move cursor to (row,col) */
  238. {
  239.   VioSetCurPos(row, col, 0);
  240. }
  241.  
  242.  
  243. void t_eeol()       /* erase from cursor to end of line */
  244. {
  245.   CHAR c = ' ';
  246.   USHORT CurCol, CurRow;
  247.  
  248.   /* find the current cursor position */
  249.   VioGetCurPos ((PUSHORT) &CurRow, (PUSHORT) &CurCol, 0);
  250.  
  251.   VioWrtNChar ((PCH) &c, t_ncol - CurCol, CurRow, CurCol, 0);
  252. }
  253.  
  254. #include "driver.h"
  255.  
  256. #define RGB_BITS(bits)        ((bits) & 0xF)
  257. #define FOREGROUND_BITS(bits)    ((bits) & 0xF)
  258. #define BACKGROUND_BITS(bits)    (((bits) >> 4) & 0x7)
  259. #define MAKE_RGB(fg, bg)    ( ((fg) & 0xF) | (((bg) & 7) << 4) )
  260.  
  261. static char *pc_color[] =
  262. {            /* IRGB   I can be 1 for forground colors only */
  263.   "black",        /* 0000 ==  0 */
  264.   "blue",        /* 0001 ==  1 */
  265.   "green",        /* 0010 ==  2 */
  266.   "cyan",        /* 0011 ==  3 */
  267.   "red",        /* 0100 ==  4 */
  268.   "magenta",        /* 0101 ==  5 */
  269.   "brown",        /* 0110 ==  6 */
  270.   "lightgrey",        /* 0111 ==  7 */
  271.   "darkgrey",        /* 1000 ==  8 */
  272.   "lightblue",        /* 1001 ==  9 */
  273.   "lightgreen",        /* 1010 == 10 */
  274.   "lightcyan",        /* 1011 == 11 */
  275.   "lightred",        /* 1100 == 12 */
  276.   "lightmagenta",    /* 1101 == 13 */
  277.   "yellow",        /* 1110 == 14 */
  278.   "white",        /* 1111 == 15 */
  279. };
  280.  
  281. static int color_to_rgb(color_name, rgb, foreground)
  282.   char *color_name; int *rgb;
  283. {
  284.   int i, max;
  285.  
  286.   strlwr(color_name);
  287.   max = foreground ? NITEMS(pc_color) : (NITEMS(pc_color) + 1)/2;
  288.   for (i = 0; i < max; i++)
  289.     if (0 == strcmp(pc_color[i], color_name))
  290.     {
  291.       *rgb = i;
  292.       return TRUE;
  293.     }
  294.   return FALSE;
  295. }
  296.  
  297. static char *rgb_to_color(rgb)
  298. {
  299.   return pc_color[RGB_BITS(rgb)];
  300. }
  301.  
  302. static void do_cursor(char * cursor_shape, int set);
  303.  
  304.     /* "forground:background"
  305.      *   ":"      => no change
  306.      *   "color"  => only change foreground
  307.      *   "color:" => only change foreground
  308.      *   ":color" => only change background
  309.      * Input:
  310.      *   color : name of color.
  311.      *   rgb : current color (incase name of new color is bogus)
  312.      * Returns:
  313.      *   New rgb bits.
  314.      */
  315. static int set_color(color, rgb) char *color;
  316. {
  317.   char *ptr, buf[100];
  318.   int fg, bg;
  319.  
  320.   strcpy(buf, color);
  321.   if (ptr = strchr(buf, ':')) *ptr++ = '\0';
  322.   if (!color_to_rgb(buf, &fg, TRUE))        fg = FOREGROUND_BITS(rgb);
  323.   if (!ptr || !color_to_rgb(ptr, &bg, FALSE))    bg = BACKGROUND_BITS(rgb);
  324.   return MAKE_RGB(fg, bg);
  325. }
  326.  
  327. static void get_color(buf,rgb) char *buf;
  328. {
  329.   strcpy(buf, rgb_to_color(FOREGROUND_BITS(rgb)));
  330.   strcat(buf, ":");
  331.   strcat(buf, rgb_to_color(BACKGROUND_BITS(rgb)));
  332. }
  333.  
  334. int do_color(which, color, set) char *color;
  335. {
  336.   int *c;
  337.  
  338.   switch(which)
  339.   {
  340.     case TEXT_COLOR:     c = &tcolor; break;        /* text color */
  341.     case MODELINE_COLOR: c = &mcolor; break;        /* modeline color */
  342.     case CURSOR_COLOR:                    /* cursor color */
  343.       if (set) return FALSE;    /* can't change cursor color */
  344.       c = &tcolor;        /* it is always the text color */
  345.       break;
  346.     case CURSOR_SHAPE: do_cursor(color, set); return TRUE;
  347.     default: return FALSE;            /* boo boo */
  348.   }
  349.   if (set)
  350.   {
  351.     int x = *c;
  352.     return (x != (*c = set_color(color, x)));
  353.   }
  354.   else get_color(color, *c);
  355.  
  356.   return FALSE;
  357. }
  358.  
  359. static void do_cursor(cursor_shape, set) char *cursor_shape;
  360. {
  361.   VIOCURSORINFO cursorInfo;
  362.  
  363.   if (set)
  364.   {
  365.     char *ptr, buf[100];
  366.  
  367.     strcpy(buf, cursor_shape);
  368.     if (ptr = strchr(buf, ':')) *ptr++ = '\0';
  369.     cursorInfo.yStart = (atoi(buf) & 0xF);
  370.     cursorInfo.cEnd = ptr ? (atoi(ptr) & 0xF) : 15;
  371.     cursorInfo.cx = 1;
  372.     cursorInfo.attr = 1;
  373.     VioSetCurType (&cursorInfo, 0);
  374.  
  375.     return;
  376.   }
  377.   else {
  378.     VioSetCurType (&cursorInfo, 0);
  379.     sprintf(cursor_shape, "%d:%d", cursorInfo.yStart, cursorInfo.cEnd);
  380.     }
  381. }
  382.  
  383. static MouseInfo mouse_info;    /* !!!??? initialize? */
  384.  
  385. MouseInfo *get_mouse_info()
  386. {
  387.   return &mouse_info;
  388. }
  389.