home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / editor / elvis / sources / os2.c < prev    next >
C/C++ Source or Header  |  1994-01-30  |  7KB  |  304 lines

  1. /* os2.c */
  2.  
  3. /*
  4.  * Author:
  5.  *   Greg Roelofs (roe2@midway.uchicago.edu).  Based on Guntram Blohm's
  6.  *   pc.c; MSC 6.0 headers; and PC Magazine Environments columns, 22 Dec
  7.  *   1987 and 15 Nov 1988.
  8.  *
  9.  * This file implements the OS/2 VIO interface.
  10.  *   Most recent revisions:  4 Jun 92
  11.  */
  12.  
  13. #if OS2
  14.  
  15. #define INCL_NOPM
  16. #define INCL_DOS
  17. #define INCL_KBD
  18. #define INCL_VIO
  19. #include <os2.h>
  20.  
  21. #define GOT_OS2_H
  22.  
  23. #include "config.h"
  24. #include "vi.h"
  25.  
  26. #ifndef MAX
  27. #  define MAX(a,b)  ((a) > (b)? (a) : (b))
  28. #endif
  29.  
  30. /* vmode contains the screen attribute index and is set by attrset
  31.  * (stupid name, sigh...) */
  32. int vmode;
  33.  
  34. /* The following array contains attribute definitions for color/monochrome
  35.  * attributes.  The variable "screen" selects one of the sets.  Maybe some-
  36.  * one will put them into elvis options one day.
  37.  */
  38. static int screen;
  39. static char attr[2][8] =
  40. { /* :se:  :so:  :VB:  :ul:  :as:  popup visible quit  */
  41.   /*{0x1f, 0x1d, 0x1e, 0x1a, 0x1c, 0x2f,  0x3f,  0x07},*/ /* color */
  42.     {0x02, 0x0c, 0x0a, 0x01, 0x09, 0x17,  0x20,  0x02},   /* color */
  43.     {0x07, 0x70, 0x0f, 0x01, 0x0f, 0x70,  0x70,  0x07},   /* mono */
  44. };
  45.  
  46. static USHORT row, col;        /* static for addressing efficiency */
  47. static VIOMODEINFO viomi_init;
  48. static VIOCURSORINFO vioci_init;
  49. #if !FASTPUT
  50. static ansi_flag = FALSE;
  51. #endif
  52.  
  53.  
  54. int biosquit()
  55. {
  56.     vmode = 7;    /* "quit" attribute */
  57.     v_ce();
  58. }
  59.  
  60.  
  61. /* this function changes the table of attribute bytes used during BIOS output */
  62. int bioscolor(mode, attrbyte)
  63.     int    mode;        /* e.g., A_NORMAL */
  64.     int    attrbyte;    /* color code, as a PC attribute byte */
  65. {
  66.     attr[0][mode] = attrbyte;
  67.     return 0;
  68. }
  69.  
  70.  
  71. /* one routine to set (or clear) raw mode on both stdin and stdout */
  72. void raw_set_stdio(rawstate)
  73.     int rawstate;    /* TRUE -> set raw mode; FALSE -> clear raw mode */
  74. {
  75.     if (rawstate) {
  76.         setmode(0, O_BINARY);
  77.         setmode(1, O_BINARY);   /* GRR:  may need to trap ^C here */
  78.     } else {
  79.         setmode(0, O_TEXT);
  80.         setmode(1, O_TEXT);
  81.     }
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. /* cursor up: determine current position, decrement row, set position */
  90. void v_up()
  91. {
  92.     VioGetCurPos(&row, &col, 0);
  93.     --row;
  94.     VioSetCurPos(MAX(row,0), col, 0);
  95. }
  96.  
  97. #ifndef NO_CURSORSHAPE
  98.  
  99. /* cursor big: set begin scan to end scan - 4 */
  100. void v_cb()
  101. {
  102.     VIOCURSORINFO vioci = vioci_init;
  103.  
  104.     vioci.yStart = vioci.cEnd - 4;        /* cEnd never changes */
  105.     VioSetCurType(&vioci, 0);
  106. }
  107.  
  108. /* cursor small:  set begin scan to end scan - 1 */
  109. void v_cs()
  110. {
  111.     VIOCURSORINFO vioci = vioci_init;
  112.  
  113.     vioci.yStart = vioci.cEnd - 1;        /* cEnd never changes */
  114.     VioSetCurType(&vioci, 0);
  115. }
  116.  
  117. #endif /* !NO_CURSORSHAPE */
  118.  
  119. /* clear to end:  get cursor position and emit the aproppriate number
  120.  * of spaces, without moving cursor */
  121. void v_ce()
  122. {
  123.     CHAR cell[3] = {' ', (CHAR)attr[screen][vmode], '\0'};
  124.     USHORT count;
  125.  
  126.     VioGetCurPos(&row, &col, 0);
  127.     count = COLS - col;
  128.     VioWrtNCell(cell, count, row, col, 0);
  129. }
  130.  
  131. /* clear screen:  clear all and set cursor home */
  132. void v_cl()
  133. {
  134.     CHAR cell[3] = {' ', (CHAR)attr[screen][vmode], '\0'};
  135.  
  136.     VioScrollUp(0, 0, -1, -1, -1, cell, 0);
  137.     VioSetCurPos(0, 0, 0);
  138. }
  139.  
  140. /* clear to bottom:  get position, clear to eol, clear next line to end */
  141. void v_cd()
  142. {
  143.     CHAR cell[3] = {' ', (CHAR)attr[screen][vmode], '\0'};
  144.     USHORT count;
  145.  
  146.     VioGetCurPos(&row, &col, 0);
  147.     count = COLS - col;
  148.     VioWrtNCell(cell, count, row, col, 0);
  149.     VioScrollUp(row+1, 0, -1, -1, -1, cell, 0);
  150. /*    VioScrollUp(row+1, 0, LINES-1, COLS-1, -1, cell, 0); */
  151. }
  152.  
  153. /* add line:  scroll rest of screen down */
  154. void v_al()
  155. {
  156.     CHAR cell[3] = {' ', (CHAR)attr[screen][vmode], '\0'};
  157.  
  158.     VioGetCurPos(&row, &col, 0);
  159.     VioScrollDn(row, 0, -1, -1, 1, cell, 0);
  160. }
  161.  
  162. /* delete line:  scroll rest up */
  163. void v_dl()
  164. {
  165.     CHAR cell[3] = {' ', (CHAR)attr[screen][vmode], '\0'};
  166.  
  167.     VioGetCurPos(&row, &col, 0);
  168.     VioScrollUp(row, 0, -1, -1, 1, cell, 0);
  169. }
  170.  
  171. /* scroll reverse:  scroll whole screen */
  172. void v_sr()
  173. {
  174.     CHAR cell[3] = {' ', (CHAR)attr[screen][vmode], '\0'};
  175.  
  176.     VioScrollDn(0, 0, -1, -1, 1, cell, 0);
  177. }
  178.  
  179. /* void v_move(x,y)  VioSetCurPos() call put directly in curses move() macro */
  180.  
  181.  
  182. /* put character (under DOS, set attribute first, then execute char--to do
  183.  * so under OS/2 requires several more function calls and is more than twice
  184.  * as slow, so allow choice with FASTPUT); also remember if current line has
  185.  * changed */
  186. int v_put(ch)
  187.     int ch;
  188. {
  189.     CHAR cell[3] = {' ', (CHAR)attr[screen][vmode], '\0'};
  190.  
  191.     *cell = (CHAR) (ch &= 0xff);
  192. #if FASTPUT
  193.     VioWrtTTY(cell, 1, 0);        /* only using char, not attribute */
  194.     if (ch == '\n')
  195.         exwrote = TRUE;
  196. #else /* !FASTPUT */
  197.     VioGetCurPos(&row, &col, 0);
  198.     if (ch >= ' ')
  199.         VioWrtNCell(cell, 1, row, col, 0);    /* write attributes */
  200.     VioWrtTTY(cell, 1, 0);                /* move cursor */
  201.     if (ch == '\n')
  202.     {
  203.         exwrote = TRUE;
  204.     /*    video(0xe0d, (int *)0, (int *)0);  */    /* GRR:  necessary? */
  205.     }
  206. #endif /* ?FASTPUT */
  207.     return ch;
  208. }
  209.  
  210.  
  211. /* determine size of screen and set of attributes to use, and store
  212.  * initial state */
  213. void v_vio_init(pRows, pCols)
  214.     int *pRows, *pCols;
  215. {
  216.     CHAR cell[3];
  217.     USHORT count = 2;
  218.  
  219.     viomi_init.cb = sizeof(viomi_init);
  220.     VioGetMode(&viomi_init, 0);
  221.     VioGetCurType(&vioci_init, 0);
  222.  
  223.     *pRows = viomi_init.row;
  224.     *pCols = viomi_init.col;
  225.     screen = (viomi_init.color == COLORS_16)? 0 : 1;
  226.  
  227.     /* get attribute of first character on last row and save for exit */
  228.         VioReadCellStr(cell, &count, viomi_init.row-1, 0, 0);
  229.     if (count > 0)
  230.         attr[0][7] = cell[1];    /* "quit" attribute */
  231.  
  232. #if !FASTPUT    /* if we're going to spend time writing attributes,
  233.          * make sure it's not wasted... */
  234.     VioGetAnsi(&count, 0);
  235.     if (count == ANSI_ON) {
  236.         VioSetAnsi(ANSI_OFF, 0);
  237.         ansi_flag = TRUE;
  238.     }
  239. #endif /* !FASTPUT */
  240. }
  241.  
  242. /* restore initial video state, at least partially */
  243. void v_vio_restore()
  244. {
  245. #if !FASTPUT
  246.     if (ansi_flag)
  247.         VioSetAnsi(ANSI_ON, 0);
  248. #endif /* !FASTPUT */
  249. }
  250.  
  251. /* this function returns the DOS time as a 32-bit long int representing
  252.  * hundredths of a second since midnight */
  253. static long dostime()
  254. {
  255.     DATETIME dt;
  256.  
  257.     DosGetDateTime(&dt);
  258.     return (((dt.hours*60L) + dt.minutes)*60L + dt.seconds)*100L +
  259.                                 dt.hundredths;
  260. }
  261.  
  262.  
  263. /*ARGSUSED*/
  264. /* This function implements a raw read from the keyboard, with timeout. */
  265. int ttyread(buf, len, time)
  266.     char    *buf;    /* where to store the keystrokes */
  267.     int    len;    /* maximum number of characters to get -- ignored */
  268.     int    time;    /* maximum time to wait, in 1/9th second increments */
  269. {
  270.     long    stop;
  271.  
  272.     /* are we going to time out? */
  273.     if (time != 0)
  274.     {
  275.         /* compute the time when we'll give up */
  276.         stop = dostime() + time * 10L;
  277.  
  278.         /* wait for either keystroke or timeout */
  279.         while (!kbhit())
  280.             if (dostime() > stop)    /* couldn't read any chars */
  281.                 return 0;    /*  before timeout */
  282.     }
  283.  
  284.     /* get a keystroke */
  285.     *buf = getch();
  286.     if (*buf == 0) /* function key? */
  287.     {
  288.         buf[0] = '#';
  289.         buf[1] = getch();
  290.         return 2;
  291.     }
  292.     else
  293.         return 1;
  294. }
  295.  
  296. int sleep(seconds)
  297.     unsigned seconds;
  298. {
  299.     DosSleep((ULONG)seconds);
  300.     return 0;
  301. }
  302.  
  303. #endif /* OS2 */
  304.