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

  1. /*
  2.  * OS2NONPM.C
  3.  *
  4.  * The routines in this file provide video and keyboard support using the
  5.  * OS/2 Vio and Kbd functions (not the presentation manager).
  6.  *
  7.  * The os2putc, os2eeol and os2eeop routines modify the logical video
  8.  * buffer.  Os2flush calls VioShowBuf to update the physical video buffer.
  9.  * An earlier version used VioWrtTTy with ANSI processing (easy to do, but
  10.  * sloooow).  A later version using VioWrtNCell was better, but not as
  11.  * good as manipulating the logical buffer.
  12.  */
  13.  
  14. #define    termdef    1            /* don't define "term" external */
  15.  
  16. #include    <stdio.h>
  17. #include    "estruct.h"
  18. #include    "etype.h"
  19. #include    "edef.h"
  20.  
  21. #if     OS2NONPM
  22. /*
  23.  * Os2def.h defines COLOR, but no MSC stuff needs it.
  24.  * We need COLOR as defined in estruct.h, so edit it out of os2def.h.
  25.  */
  26. #define INCL_BASE
  27. #include    <os2.h>
  28. #include    <conio.h>
  29.  
  30. #define    NROW    50        /* Screen size.                 */
  31. #define    NCOL    80        /* Edit if you want to.         */
  32. #define    NPAUSE    100        /* # times thru update to pause */
  33. #define    MARGIN    8        /* size of minimim margin and    */
  34. #define    SCRSIZ    64        /* scroll size for extended lines */
  35.  
  36. #define CDCGA    0        /* color graphics adapter    */
  37. #define CDMONO    1        /* monochrome display adapter    */
  38. #define CDEGA    2        /* EGA                */
  39. #define CDVGA    3        /* VGA                */
  40.  
  41. #define NDRIVE    4        /* number of video modes    */
  42.  
  43. int dtype = -1;                /* current video mode    */
  44. char drvname[][8] = {            /* names of video modes    */
  45.     "CGA", "MONO", "EGA", "VGA"
  46. };
  47.  
  48. /* Forward references.          */
  49. os2move();
  50. os2eeol();
  51. os2eeop();
  52. os2beep();
  53. os2open();
  54. os2close();
  55. os2getc();
  56. os2putc();
  57. int os2flush(void);
  58. os2rev();
  59. os2kclose();
  60. os2kopen();
  61. os2cres();
  62. os2parm();
  63. #if    COLOR
  64. os2fcol();
  65. os2bcol();
  66. #endif
  67.  
  68.  
  69. struct {            /* Current screen attribute for ORing    */
  70.     BYTE    filler;        /* with character to be displayed.    */
  71.     BYTE    attr;
  72. } os2cell = {0, 0x07};
  73.  
  74. static struct {                /* initial states        */
  75.     USHORT        ansiState;    /* ANSI translation        */
  76.     VIOCONFIGINFO    vioConfigInfo;    /* video configuration        */
  77.     VIOMODEINFO    vioModeInfo;    /* video mode            */
  78.     KBDINFO        kbdInfo;    /* keyboard info        */
  79. } initial;
  80.  
  81. static int    cfcolor = -1;        /* current foreground color    */
  82. static int    cbcolor = -1;        /* current background color    */
  83. static int    ctrans[] =    /* ansi to ibm color translation table    */
  84.     {0, 4, 2, 6, 1, 5, 3, 7};
  85.  
  86. static short     os2row;        /* current cursor row    */
  87. static short     os2col;        /* current cursor col    */
  88.  
  89. /*
  90.  * To minimize the amount of buffer that VioShowBuf has to update, we
  91.  * keep track of the lowest and highest bytes in the logical video
  92.  * buffer which have been modified.
  93.  */
  94. static USHORT    *lvb;            /* logical video buffer    */
  95. static USHORT     lvbLen;        /* length of buffer    */
  96. static USHORT     lvbMin;        /* min index of modified byte    */
  97. static USHORT     lvbMax;        /* max index of modified byte    */
  98.  
  99.  
  100. /*
  101.  * Standard terminal interface dispatch table.
  102.  */
  103. TERM    term    = {
  104.     NROW-1,
  105.     NROW-1,
  106.     NCOL,
  107.     NCOL,
  108.     MARGIN,
  109.     SCRSIZ,
  110.     NPAUSE,
  111.     os2open,
  112.     os2close,
  113.     os2kopen,
  114.     os2kclose,
  115.     os2getc,
  116.     os2putc,
  117.     os2flush,
  118.     os2move,
  119.     os2eeol,
  120.     os2eeop,
  121.     os2beep,
  122.     os2rev,
  123.     os2cres
  124. #if    COLOR
  125.     , os2fcol,
  126.     os2bcol
  127. #endif
  128. };
  129.  
  130.  
  131. #if    COLOR
  132. /*----------------------------------------------------------------------*/
  133. /*    os2fcol()                            */
  134. /* Set the current foreground color.                    */
  135. /*----------------------------------------------------------------------*/
  136.  
  137. os2fcol(
  138.     int color)            /* color to set */
  139. {
  140.     if (dtype != CDMONO)
  141.         cfcolor = ctrans[color];
  142.     else
  143.         cfcolor = 7;
  144.     os2cell.attr &= 0xF0;
  145.     os2cell.attr |= cfcolor;
  146.     
  147. }
  148.  
  149.  
  150. /*----------------------------------------------------------------------*/
  151. /*    os2bcol()                            */
  152. /* Set the current background color.                    */
  153. /*----------------------------------------------------------------------*/
  154.  
  155. os2bcol(
  156.     int color)        /* color to set */
  157. {
  158.     if (dtype != CDMONO)
  159.         cbcolor = ctrans[color];
  160.     else
  161.         cbcolor = 0;
  162.     os2cell.attr &= 0x0F;
  163.     os2cell.attr |= cbcolor << 4;
  164. }
  165. #endif
  166.  
  167.  
  168. /*----------------------------------------------------------------------*/
  169. /*    os2move()                            */
  170. /* Move the cursor.                            */
  171. /*----------------------------------------------------------------------*/
  172.  
  173. os2move(
  174.     int row,
  175.     int col)
  176. {
  177.     os2row = row;
  178.     os2col = col;
  179.     VioSetCurPos( os2row, os2col, 0);
  180. }
  181.  
  182.  
  183. /*----------------------------------------------------------------------*/
  184. /*    os2flush()                            */
  185. /* Update the physical video buffer from the logical video buffer.    */
  186. /*----------------------------------------------------------------------*/
  187.  
  188. int os2flush( void)
  189. {
  190.     if (lvbMin <= lvbMax) {        /* did anything change?    */
  191.         VioShowBuf( lvbMin * 2, (lvbMax - lvbMin + 1) * 2, 0);
  192.         VioSetCurPos( os2row, os2col, 0);
  193.     }
  194.     lvbMin = lvbLen;
  195.     lvbMax = 0;
  196. }
  197.  
  198.  
  199. /*----------------------------------------------------------------------*/
  200. /*    os2getc()                            */
  201. /* Get a character from the keyboard.                    */
  202. /* Function keys, editing keys and alt- keys take two consecutive calls    */
  203. /* to os2getc().  The first returns zero and the second returns the    */
  204. /* key's scan code.                            */
  205. /* Nextc holds the scan code until we are called again.            */
  206. /*----------------------------------------------------------------------*/
  207.  
  208. os2getc()
  209. {
  210.     KBDKEYINFO    keyInfo;
  211.     static int    nextc = -1;  /* -1 when not holding a scan code    */
  212.  
  213.     if (nextc != -1) {
  214.         keyInfo.chChar = (char)nextc;
  215.         nextc = -1;
  216.         return keyInfo.chChar;        /* return the scan code    */
  217.     }
  218.     
  219.     KbdCharIn( &keyInfo, IO_WAIT, 0);    /* get a character    */
  220.  
  221.     /* Function, edit or alt- key?    */
  222.     if (keyInfo.chChar == 0  ||  keyInfo.chChar == 0xE0) {
  223.         nextc = keyInfo.chScan;        /* hold on to scan code    */
  224.         return 0;
  225.     }
  226.     return keyInfo.chChar;
  227. }
  228.  
  229.  
  230. #if    TYPEAH
  231. /*----------------------------------------------------------------------*/
  232. /*    typahead()                            */
  233. /* Returns true if a key has been pressed.                */
  234. /*----------------------------------------------------------------------*/
  235.  
  236. typahead()
  237.  
  238. {
  239.     return kbhit();
  240. }
  241. #endif
  242.  
  243.  
  244. /*----------------------------------------------------------------------*/
  245. /*    os2putc()                            */
  246. /* Put a character at the current position in the current colors.    */
  247. /* Note that this does not behave the same as putc() or VioWrtTTy().    */
  248. /* This routine does nothing with returns and linefeeds.  For backspace */
  249. /* it puts a space in the previous column and moves the cursor to the    */
  250. /* previous column.  For all other characters, it will display the    */
  251. /* graphic representation of the character and put the cursor in the    */
  252. /* next column (even if that is off the screen.  In practice this isn't    */
  253. /* a problem.                                */
  254. /*----------------------------------------------------------------------*/
  255.  
  256. os2putc( int c)
  257. {
  258.     USHORT    cell;
  259.     USHORT    i;
  260.  
  261.     if (c == '\n' || c == '\r') {        /* returns and linefeeds */
  262.         return;
  263.     }
  264.     if (c == '\b') {            /* backspace        */
  265.         cell = ' ' | *(USHORT *)&os2cell;
  266.         --os2col;            /* move cursor back    */
  267.         i = os2row * term.t_ncol + os2col;
  268.     }
  269.     else {
  270.         cell = (0x00ff & c) | *(USHORT *)&os2cell;
  271.         i = os2row * term.t_ncol + os2col;
  272.         ++os2col;            /* move cursor forward    */
  273.     }
  274.     lvb[i] = cell;
  275.     if (i < lvbMin)
  276.         lvbMin = i;
  277.     if (i > lvbMax)
  278.         lvbMax = i;
  279. }
  280.  
  281.  
  282. /*----------------------------------------------------------------------*/
  283. /*    os2eeol()                            */
  284. /* Erase to end of line.                        */
  285. /*----------------------------------------------------------------------*/
  286.  
  287. os2eeol()
  288. {
  289.     USHORT    cell = ' ';
  290.     USHORT  i;
  291.  
  292.     cell |= *(USHORT *)&os2cell;
  293.     
  294.     i = os2row * term.t_ncol + os2col;  /* current cursor position    */
  295.     if (i < lvbMin)
  296.         lvbMin = i;
  297.     while (i < os2row * term.t_ncol + term.t_ncol)
  298.         lvb[ i++] = cell;
  299.     if (--i > lvbMax)
  300.         lvbMax = i;
  301. }
  302.  
  303.  
  304. /*----------------------------------------------------------------------*/
  305. /*    os2eeop()                            */
  306. /* Erase to end of page.                        */
  307. /*----------------------------------------------------------------------*/
  308.  
  309. os2eeop()
  310. {
  311.     USHORT    cell = ' ';
  312.     USHORT  i;
  313.  
  314. #if COLOR
  315.     if (dtype != CDMONO)
  316.         cell |= (ctrans[gbcolor] << 4 | ctrans[gfcolor]) << 8;
  317.     else
  318.         cell |= 0x0700;
  319. #else
  320.     cell |= 0x0700;
  321. #endif
  322.  
  323.     i = os2row * term.t_ncol + os2col;  /* current cursor position    */
  324.     if (i < lvbMin)
  325.         lvbMin = i;
  326.     while (i < term.t_nrow * term.t_ncol + term.t_ncol)
  327.         lvb[ i++] = cell;
  328.     if (--i > lvbMax)
  329.         lvbMax = i;
  330. }
  331.  
  332. /*----------------------------------------------------------------------*/
  333. /*    os2rev()                            */
  334. /* Change reverse video state.  (Does nothing.)                */
  335. /*----------------------------------------------------------------------*/
  336.  
  337. os2rev( int state)
  338. {
  339. }
  340.  
  341. /*----------------------------------------------------------------------*/
  342. /*    os2cres()                            */
  343. /* Change the screen resolution.                    */
  344. /*----------------------------------------------------------------------*/
  345.  
  346. os2cres( char *res)        /* name of desired video mode    */
  347. {
  348.     USHORT    err;
  349.     int    type;            /* video mode type    */
  350.     VIOMODEINFO vioModeInfo;
  351.  
  352.     vioModeInfo = initial.vioModeInfo;
  353.     
  354.     /* From the name, find the type of video mode.    */
  355.     for (type = 0; type < NDRIVE; type++) {
  356.         if (strcmp(res, drvname[type]) == 0)
  357.             break;
  358.     }
  359.     if (type == NDRIVE)
  360.         return FALSE;    /* not a mode we know about    */
  361.  
  362.         
  363.     switch (type) {
  364.         case CDMONO:
  365.         case CDCGA:
  366.             vioModeInfo.row = 25;
  367.             break;
  368.         case CDEGA:
  369.             vioModeInfo.row = 43;
  370.             break;
  371.         case CDVGA:
  372.             vioModeInfo.row = 50;
  373.             break;
  374.     }
  375.     
  376.     if (VioSetMode( &vioModeInfo, 0))    /* change modes     */
  377.         return FALSE;            /* couldn't do it    */
  378.  
  379.     newsize( TRUE, vioModeInfo.row);
  380.  
  381.     /* reset the $sres environment variable */
  382.     strcpy( sres, drvname[type]);
  383.     dtype = type;                /* set the current mode    */
  384.     
  385.     return TRUE;
  386. }
  387.  
  388.  
  389. /*----------------------------------------------------------------------*/
  390. /*    spal()                                */
  391. /* Change pallette settings.  (Does nothing.)                */
  392. /*----------------------------------------------------------------------*/
  393.  
  394. spal(dummy)
  395. {
  396. }
  397.  
  398.  
  399. /*----------------------------------------------------------------------*/
  400. /*    os2beep()                            */
  401. /*----------------------------------------------------------------------*/
  402.  
  403. os2beep()
  404. {
  405.     DosBeep(1200, 175);
  406. }
  407.  
  408.  
  409. /*----------------------------------------------------------------------*/
  410. /*    os2open()                            */
  411. /* Find out what kind of video adapter we have and the current video    */
  412. /* mode.  Even if the adapter supports a higher resolution mode than is */
  413. /* in use, we still use the current mode.                */
  414. /*----------------------------------------------------------------------*/
  415.  
  416. os2open()
  417. {
  418.     initial.vioConfigInfo.cb = 0x0A;
  419.     VioGetConfig( 0, &initial.vioConfigInfo, 0);
  420.     switch (initial.vioConfigInfo.adapter) {
  421.         case 3:
  422.             dtype = CDVGA;
  423.             break;
  424.         case 2:
  425.             dtype = CDEGA;
  426.             break;
  427.         case 1:
  428.             dtype = CDCGA;
  429.             break;
  430.         case 0:
  431.         default:
  432.             dtype = CDMONO;
  433.     }
  434.     strcpy( sres, drvname[dtype]);
  435.  
  436.     initial.vioModeInfo.cb = 0x0E;
  437.     VioGetMode( &initial.vioModeInfo, 0);
  438.     newsize( TRUE, initial.vioModeInfo.row);
  439.             
  440.     VioGetAnsi( &initial.ansiState, 0);
  441.     VioGetBuf( &lvb, &lvbLen, 0);
  442.     lvbMin = lvbLen;
  443.     lvbMax = 0;
  444. }
  445.  
  446.  
  447. /*----------------------------------------------------------------------*/
  448. /*    os2close()                            */
  449. /* Restore the original video settings.                    */
  450. /*----------------------------------------------------------------------*/
  451.  
  452. os2close()
  453. {
  454.     VioSetAnsi( initial.ansiState, 0);
  455.     VioSetMode( &initial.vioModeInfo, 0);
  456.     VioSetCurPos( initial.vioModeInfo.row - 1,
  457.              initial.vioModeInfo.col - 1, 0);
  458. }
  459.  
  460. /*----------------------------------------------------------------------*/
  461. /*    os2kopen()                            */
  462. /* Open the keyboard.                            */
  463. /*----------------------------------------------------------------------*/
  464.  
  465. os2kopen()
  466. {
  467.     KBDINFO    kbdInfo;
  468.  
  469.     initial.kbdInfo.cb = 0x000A;
  470.     KbdGetStatus( &initial.kbdInfo, 0);    
  471.     kbdInfo = initial.kbdInfo;
  472.     kbdInfo.fsMask &= ~0x0001;        /* not echo on        */
  473.     kbdInfo.fsMask |= 0x0002;        /* echo off        */
  474.     kbdInfo.fsMask &= ~0x0008;        /* cooked mode off    */
  475.     kbdInfo.fsMask |= 0x0004;        /* raw mode        */
  476.     kbdInfo.fsMask &= ~0x0100;        /* shift report    off    */
  477.     KbdSetStatus( &kbdInfo, 0);
  478. }
  479.  
  480.  
  481. /*----------------------------------------------------------------------*/
  482. /*    os2kclose()                            */
  483. /* Close the keyboard.                            */
  484. /*----------------------------------------------------------------------*/
  485.  
  486. os2kclose()
  487. {
  488.     KbdSetStatus( &initial.kbdInfo, 0); /* restore original state    */
  489. }
  490.  
  491.  
  492. #if 0
  493. /*----------------------------------------------------------------------*/
  494. /*    scwrite()                            */
  495. /* Write a line to the screen.
  496. /* I tried using this routine with MEMMAP = 1, but there were too many    */
  497. /* problems with the cursor and flushing the buffer.            */
  498. /*----------------------------------------------------------------------*/
  499.  
  500. scwrite(
  501.     int     row,    /* row of screen to place outstr on */
  502.     char    *outstr,/* string to write out (must be term.t_ncol long) */
  503.     int     forg,    /* foreground color of string to write */
  504.     int     bacg)    /* background color */
  505. {
  506.     USHORT     attr;
  507.     int     i;
  508.  
  509.     attr = (((ctrans[bacg] & 15) << 4) | (forg & 15)) << 8;
  510.  
  511.     for (i = row * term.t_ncol;  i < (row + 1) * term.t_ncol;  i++)
  512.         lvb[i] = attr | *(outstr++);
  513.  
  514.     if (i < lvbMin)
  515.         lvbMin = i;
  516.     if (i > lvbMax)
  517.         lvbMax = i;
  518. }
  519. #endif
  520.     
  521.         
  522. #endif
  523.  
  524.