home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ue312os2.zip / src / os2npm.BAK < prev    next >
Text File  |  1994-08-30  |  27KB  |  904 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.  /*
  15.     The biggest change in this file compared with the older (MSC) 16-bit
  16.     version    deals the problem, that the Vio*-Functions are old and pure
  17.     16-bit Funktions.
  18.  
  19.     You have to keep in mind, that the 16bit Vio- 
  20.     Kbd- and Mou- Calls are undocumented since 2.x, they exist only for
  21.     compatibility reasons. 
  22.  
  23.     The way it is handled is quite different for the GNU- and the IBM-
  24.     compiler. They are both real 32-bit Compiler using flat 0:32 pointer.
  25.     You have to "convince" them to use the older 16:16-pointers.
  26.     
  27.     This is achieved for GNU using bla bla bla.
  28.  
  29.     IBMs Compiler has a keyword _Seg16, which enables icc to declare
  30.     lvb internally as a 16:16-pointer.
  31.     
  32.     If someone uses a different 32-bit flat-mode compiler, he/she has to find
  33.     out an equivalent to this _Seg16 statement.
  34.     */
  35.  
  36. #define INCL_BASE
  37. #define INCL_DOSDEVIOCTL
  38. #include <os2.h>
  39. #include <signal.h>
  40.  
  41. #define    termdef    1            /* don't define "term" external */
  42.  
  43. #include <stdio.h>
  44.  
  45. #undef    PASCAL
  46. #undef    NEAR
  47. #undef    HIBYTE
  48.  
  49. #include "estruct.h"
  50. #include "eproto.h"
  51. #include "edef.h"
  52. #include "elang.h"
  53.  
  54. #if     OS2NPM
  55. /*
  56.  * Os2def.h defines COLOR, but no MSC stuff needs it.
  57.  * We need COLOR as defined in estruct.h, so edit it out of os2def.h.
  58.  */
  59. #include    <conio.h>
  60.  
  61. #define NROW    102             /* Screen size.                 */
  62. #define NCOL    132              /* Edit if you want to.         */
  63. #define MARGIN  8               /* size of minimim margin and   */
  64. #define SCRSIZ  64              /* scroll size for extended lines */
  65. #define NPAUSE  100             /* # times thru update to pause */
  66.  
  67. #define CDCGA   0               /* color graphics adapter       */
  68. #define CDMONO  1               /* monochrome display adapter   */
  69. #define CDEGA   2               /* EGA                          */
  70. #define CDVGA   3               /* VGA                          */
  71. #define CDSVGA1 4               /* SVGA 132 x 25                */
  72. #define CDSVGA2 5               /* SVGA 132 x 43                */
  73.  
  74. #define NDRIVE  6               /* number of video modes        */
  75.  
  76. int dtype = -1;                         /* current video mode   */
  77. char drvname[][8] = {                   /* names of video modes */
  78.         "CGA", "MONO", "EGA", "VGA", "SVGA1", "SVGA2"
  79. };
  80.  
  81. /* Forward references.          */
  82. /* The following declarations where changed to reduce the number of
  83.  * warnings. Changed by Ralf Seidel */
  84. int PASCAL NEAR os2move( int, int );
  85. int PASCAL NEAR os2eeol( void );
  86. int PASCAL NEAR os2eeop( void );
  87. int PASCAL NEAR os2beep( void );
  88. int PASCAL NEAR os2open( void );
  89. int PASCAL NEAR os2close( void );
  90. int PASCAL NEAR os2getc( void );
  91. int PASCAL NEAR os2putc( int );
  92. int PASCAL NEAR os2flush( void );
  93. int PASCAL NEAR os2rev( int );
  94. int PASCAL NEAR os2kclose( void );
  95. int PASCAL NEAR os2kopen( void );
  96. int PASCAL NEAR os2cres( char* );
  97. int PASCAL NEAR os2parm( void );
  98. #if    COLOR
  99. int PASCAL NEAR os2fcol( int );
  100. int PASCAL NEAR os2bcol( int );
  101. #endif
  102. /* neu: damit auch die screens fluppen: */
  103. int PASCAL NEAR os2clrdesk( void );
  104.  
  105. struct {         /* Current screen attribute for ORing    */
  106.     BYTE    filler;        /* with character to be displayed.    */
  107.     BYTE    attr;
  108. } os2cell = {0, 0x07};
  109.  
  110. struct {         /* Current reverse screen attribute for    */
  111.     BYTE    filler;        /* ORing with character to be displayed.*/
  112.     BYTE    attr;
  113. } os2rcell = {0, 0x07};
  114.  
  115. static struct {                     /* initial states       */
  116.     USHORT          ansiState;      /* ANSI translation     */
  117.     VIOCONFIGINFO   vioConfigInfo;  /* video configuration  */
  118.     VIOMODEINFO     vioModeInfo;    /* video mode           */
  119.     KBDINFO         kbdInfo;        /* keyboard info        */
  120.  
  121. } initial;
  122.  
  123. static int cfcolor = -1;        /* current foreground color */
  124. static int cbcolor = -1;        /* current background color */
  125. static int ctrans[] =           /* ansi to ibm color translation table */
  126.     {0, 4, 2, 6, 1, 5, 3, 7,
  127.      8, 12, 10, 14, 9, 13, 11, 15};
  128.  
  129. static short os2row;            /* current cursor row   */
  130. static short os2col;            /* current cursor col   */
  131. static short os2mrow;            /* dimensions of current fullscreen */
  132. static short os2mcol;            /* dimensions of current fullscreen */
  133.  
  134. int revflag = FALSE;            /* are we currently in rev video? */
  135.  
  136. /*
  137.  * To minimize the amount of buffer that VioShowBuf has to update, we
  138.  * keep track of the lowest and highest bytes in the logical video
  139.  * buffer which have been modified.
  140.  */
  141. #if ICC
  142. static USHORT * _Seg16 lvb;  /* logical video buffer       */
  143. #else
  144. static USHORT *lvb;         /* logical video buffer       */
  145. #endif
  146. static USHORT lvbLen;       /* length of buffer           */
  147. static USHORT lvbMin;       /* min index of modified byte */
  148. static USHORT lvbMax;       /* max index of modified byte */
  149.  
  150. /*
  151.  * Standard terminal interface dispatch table.
  152.  */
  153. TERM term = {
  154.     NROW-1,
  155.     NROW-1,
  156.     NCOL,
  157.     NCOL,
  158.     0, 0,
  159.     MARGIN,
  160.     SCRSIZ,
  161.     NPAUSE,
  162.     os2open,
  163.     os2close,
  164.     os2kopen,
  165.     os2kclose,
  166.     os2getc,
  167.     os2putc,
  168.     os2flush,
  169.     os2move,
  170.     os2eeol,
  171.     os2eeop,
  172.     os2clrdesk,
  173.     os2beep,
  174.     os2rev,
  175.     os2cres
  176. #if    COLOR
  177.     , os2fcol,
  178.     os2bcol
  179. #endif
  180. };
  181.  
  182. static int mexist;      /* is the mouse driver installed?  */
  183. #if MOUSE
  184. /* Mousing global variable */
  185. static USHORT nbuttons; /* number of buttons on the mouse  */
  186. static USHORT oldbut;   /* Previous state of mouse buttons */
  187. static int oldcol;      /* previous x position of mouse    */
  188. static int oldrow;      /* previous y position of mouse    */
  189. HMOU mouse_handle;      /* handle to opened mouse          */
  190. #endif
  191.  
  192. /* input buffers and pointers */
  193.  
  194. #define    IBUFSIZE 64 /* this must be a power of 2 */
  195.  
  196. unsigned char in_buf[IBUFSIZE];    /* input character buffer */
  197. int in_next = 0;        /* pos to retrieve next input character */
  198. int in_last = 0;        /* pos to place most recent input character */
  199.  
  200. in_init()    /* initialize the input buffer */
  201.  
  202. {
  203.     in_next = in_last = 0;
  204. }
  205.  
  206. in_check()    /* is the input buffer non-empty? */
  207.  
  208. {
  209.     if (in_next == in_last)
  210.         return(FALSE);
  211.     else
  212.         return(TRUE);
  213. }
  214.  
  215. in_put(event)
  216.  
  217. int event;    /* event to enter into the input buffer */
  218.  
  219. {
  220.     in_buf[in_last++] = event;
  221.     in_last &= (IBUFSIZE - 1);
  222. }
  223.  
  224. int in_get()    /* get an event from the input buffer */
  225.  
  226. {
  227.     register int event;    /* event to return */
  228.  
  229.     event = in_buf[in_next++];
  230.     in_next &= (IBUFSIZE - 1);
  231.     return(event);
  232. }
  233.  
  234. #if    COLOR
  235. /*----------------------------------------------------------------------*/
  236. /* os2fcol()                            */
  237. /* Set the current foreground color.                    */
  238. /*----------------------------------------------------------------------*/
  239.  
  240. int PASCAL NEAR os2fcol( int color)         /* color to set */
  241. {
  242.     if (dtype != CDMONO)
  243.         cfcolor = ctrans[color];
  244.     else
  245.         cfcolor = 7;
  246.  
  247.     /* set the normal attribute */
  248.     os2cell.attr &= 0xF0;
  249.     os2cell.attr |= cfcolor;
  250.  
  251.     /* set the reverse attribute */
  252.     os2rcell.attr &= 0x07;
  253.     os2rcell.attr |= cfcolor << 4;
  254. }
  255.  
  256. /*----------------------------------------------------------------------*/
  257. /* os2bcol()                            */
  258. /* Set the current background color.                    */
  259. /*----------------------------------------------------------------------*/
  260.  
  261. int PASCAL NEAR os2bcol( int color)        /* color to set */
  262. {
  263.     if (dtype != CDMONO)
  264.         cbcolor = ctrans[color];
  265.     else
  266.         cbcolor = 0;
  267.  
  268.     /* set normal background attribute */
  269.     os2cell.attr &= 0x0F;
  270.     os2cell.attr |= cbcolor << 4;
  271.  
  272.     /* set reverse background attribute */
  273.     os2rcell.attr &= 0x70;
  274.     os2rcell.attr |= cbcolor;
  275. }
  276. #endif
  277.  
  278.  
  279.  
  280. /*----------------------------------------------------------------------*/
  281. /* os2clrdesk()                        */
  282. /* Clears the screen                        */
  283. /*----------------------------------------------------------------------*/
  284. int PASCAL NEAR os2clrdesk( void )
  285. {
  286.     BYTE abCell[2]={' ',0x07};
  287.     VioScrollUp(0,0,0xFFFF,0xFFFF,0xFFFF,abCell,0);
  288. }    
  289.  
  290. /*----------------------------------------------------------------------*/
  291. /* os2move()                            */
  292. /* Move the cursor.                            */
  293. /*----------------------------------------------------------------------*/
  294.  
  295. int PASCAL NEAR os2move( int row, int col)
  296. {
  297.     os2row = row+term.t_roworg;
  298.     os2col = col+term.t_colorg;
  299.     VioSetCurPos(os2row, os2col, 0);
  300. }
  301.  
  302.  
  303. /*----------------------------------------------------------------------*/
  304. /* os2flush()                            */
  305. /* Update the physical video buffer from the logical video buffer.    */
  306. /*----------------------------------------------------------------------*/
  307.  
  308. int PASCAL NEAR os2flush()
  309. {
  310.     if (lvbMin <= lvbMax) {     /* did anything change? */
  311.         VioShowBuf(lvbMin * 2, (lvbMax - lvbMin + 1) * 2, 0);
  312.         VioSetCurPos(os2row, os2col, 0);
  313.     }
  314.     lvbMin = lvbLen;
  315.     lvbMax = 0;
  316. }
  317.  
  318.  
  319. /*----------------------------------------------------------------------*/
  320. /* os2char()                            */
  321. /* Get a character from the keyboard.                    */
  322. /* Function keys, editing keys and alt- keys queue extra bytes into        */
  323. /* input queue.
  324. /*----------------------------------------------------------------------*/
  325.  
  326. int PASCAL NEAR os2char()
  327. {
  328.     register c;     /* extended key code for special keys */
  329.     KBDKEYINFO keyInfo;
  330.  
  331.     KbdCharIn(&keyInfo, IO_WAIT, 0); /* get a character    */
  332.  
  333.     /* Function, edit or alt- key?    */
  334.     if (keyInfo.chChar == 0  ||  keyInfo.chChar == 0xE0) {
  335.         c = extcode(keyInfo.chScan); /* hold on to scan code */
  336.         in_put(c >> 8);        /* queue up prefix byte */
  337.         in_put(c & 0xFF); /* queue up event byte */
  338.         return(0);
  339.     }
  340.     return(keyInfo.chChar & 255);
  341. }
  342.  
  343. /*
  344.  * Read a character from the terminal, performing no editing and doing no echo
  345.  * at all. Also mouse events are forced into the input stream here.
  346.  */
  347. int PASCAL NEAR os2getc()
  348. {
  349.     register int c;        /* character read */
  350. #if    MOUSE
  351.     NOPTRRECT mouse_rect;
  352. #endif
  353.  
  354.     os2flush();
  355.  
  356. ttc:    /* return any keystrokes waiting in the
  357.         type ahead buffer */
  358.     if (in_check())
  359.         return(in_get());
  360.  
  361. #if    TYPEAH
  362.     if (typahead())
  363.         return(os2char());
  364.  
  365.     /* with no mouse, this is a simple get char routine */
  366.     if (mexist == FALSE || mouseflag == FALSE)
  367.         return(os2char());
  368.  
  369. #if    MOUSE
  370.     /* turn the mouse cursor on */
  371.     MouDrawPtr(mouse_handle);
  372.  
  373.     /* loop waiting for something to happen */
  374.     while (TRUE) {
  375.         if (typahead())
  376.             break;
  377.         if (checkmouse())
  378.             break;
  379.         DosSleep(10L); /* let others have some CPU! */
  380.     }
  381.  
  382.     /* turn the mouse cursor back off */
  383.     mouse_rect.row = 0;
  384.     mouse_rect.col = 0;
  385.     mouse_rect.cRow = 24;
  386.     mouse_rect.cCol = 79;
  387.     MouRemovePtr(&mouse_rect, mouse_handle);
  388.  
  389.     goto ttc;
  390. #endif    /* MOUSE */
  391. #else /* TYPEAH */
  392.     return(os2char());
  393. #endif    /* TYPEAH */
  394. }
  395.  
  396. #if    MOUSE
  397. checkmouse()
  398.  
  399. {
  400.     register int k;        /* current bit/button of mouse */
  401.     register int etype;    /* event type byte */
  402.     register int event;    /* encoded mouse event */
  403.     int mousecol;        /* current mouse column */
  404.     int mouserow;        /* current mouse row */
  405.     int sstate;     /* current shift key status */
  406.     int newbut;     /* new state of the mouse buttons */
  407.     MOUEVENTINFO mouse_event;    /* info about a mouse event */
  408.     MOUQUEINFO mouse_queue; /* holds info about the mouse queue */
  409.     USHORT wait_flag; /* wait flag for read mouse queue call */
  410.     KBDINFO kbd_info; /* keyboard information */
  411.  
  412.     /* is there a mouse event queued up? */
  413.     MouGetNumQueEl(&mouse_queue, mouse_handle);
  414.     if (mouse_queue.cEvents == 0)
  415.         return(FALSE); 
  416.  
  417.     /* get the current mouse event */
  418.     wait_flag = MOU_WAIT;
  419.     MouReadEventQue(&mouse_event, &wait_flag, mouse_handle);
  420.  
  421.     /* build a simple bit field out of OS/2's rather complex one */
  422.     newbut = 0;
  423.     if (mouse_event.fs & MOUSE_BN1_DOWN)
  424.         newbut |= 1;
  425.     if (mouse_event.fs & MOUSE_MOTION_WITH_BN1_DOWN)
  426.         newbut |= 1;
  427.     if (mouse_event.fs & MOUSE_BN2_DOWN)
  428.         newbut |= 2;
  429.     if (mouse_event.fs & MOUSE_MOTION_WITH_BN2_DOWN)
  430.         newbut |= 2;
  431.     if (mouse_event.fs & MOUSE_BN3_DOWN)
  432.         newbut |= 4;
  433.     if (mouse_event.fs & MOUSE_MOTION_WITH_BN3_DOWN)
  434.         newbut |= 4;
  435.  
  436.     /* check to see if any mouse buttons are different */
  437.     mousecol = mouse_event.col;
  438.     mouserow = mouse_event.row;
  439.  
  440.     /* only notice changes */
  441.     if ((oldbut == newbut) && (mousecol == oldcol)
  442.          && (mouserow == oldrow))
  443.         return(FALSE);
  444.  
  445.     /* get the shift key status as well */
  446.     KbdGetStatus(&kbd_info, 0);
  447.     etype = MOUS >> 8;
  448.     sstate = kbd_info.fsState;
  449.     if (sstate & (RIGHTSHIFT | LEFTSHIFT)) /* shifted */
  450.         etype |= (SHFT >> 8);
  451.     else if (sstate & CONTROL) /* controled? */
  452.         etype |= (CTRL >> 8);
  453.  
  454.     /* no buttons changes */
  455.     if (oldbut == newbut) {
  456.  
  457.         /* generate a mouse movement */
  458.         if (((mouse_move == 1) && (mmove_flag == TRUE)) ||
  459.              (mouse_move == 2)) {
  460.             in_put(0);
  461.             in_put(etype);
  462.             in_put(mousecol);
  463.             in_put(mouserow);
  464.             in_put('m');
  465.         }
  466.         oldcol = mousecol;
  467.         oldrow = mouserow;
  468.         return(TRUE);
  469.     }
  470.  
  471.     /* only on screen presses are legit! */
  472.     if (mousecol < 0)
  473.         mousecol = 0;
  474.     if (mouserow < 0)
  475.         mouserow = 0;
  476.  
  477.     for (k = 1; k != (1 << nbuttons); k = k << 1) {
  478.  
  479.         /* For each button on the mouse */
  480.         if ((oldbut&k) != (newbut&k)) {
  481.             /* This button changed, generate an event */
  482.             in_put(0);
  483.             in_put(etype);
  484.             in_put(mousecol);
  485.             in_put(mouserow);
  486.  
  487.             event = ((newbut&k) ? 0 : 1); /* up or down? */
  488.             if (k == 2)         /* center button? */
  489.                 event += 4;
  490.             if (k == 4)         /* right button? */
  491.                 event += 2;
  492.             event += 'a';        /* plain */
  493.             in_put(event);
  494.             oldbut = newbut;
  495.             oldcol = mousecol;
  496.             oldrow = mouserow;
  497.             return(TRUE);
  498.         }
  499.     }
  500.     return(TRUE);
  501. }
  502. #endif
  503.  
  504. #if    TYPEAH
  505. /*----------------------------------------------------------------------*/
  506. /* typahead()                                                           */
  507. /* Returns true if a key has been pressed.                              */
  508. /*----------------------------------------------------------------------*/
  509.  
  510. int PASCAL NEAR typahead()
  511. {
  512.     KBDKEYINFO kbdinfo;
  513.  
  514.     KbdPeek(&kbdinfo, 0);
  515.     return(kbdinfo.fbStatus != 0);
  516. }
  517. #endif
  518.  
  519.  
  520. /*----------------------------------------------------------------------*/
  521. /* os2putc()                                                            */
  522. /* Put a character at the current position in the current colors.       */
  523. /* Note that this does not behave the same as putc() or VioWrtTTy().    */
  524. /* This routine does nothing with returns and linefeeds.  For backspace */
  525. /* it puts a space in the previous column and moves the cursor to the   */
  526. /* previous column.     For all other characters, it will display the   */
  527. /* graphic representation of the character and put the cursor in the    */
  528. /* next column (even if that is off the screen.  In practice this isn't */
  529. /* a problem.                                                           */
  530. /*----------------------------------------------------------------------*/
  531.  
  532. int PASCAL NEAR os2putc(int c)
  533. {
  534.     USHORT  cell;
  535.     USHORT  i;
  536.  
  537.     if (c == '\n' || c == '\r') {   /* returns and linefeeds */
  538.         return;
  539.     }
  540.     if (c == '\b') {                /* backspace             */
  541.         cell = ' ' | (revflag ? *(USHORT *)&os2rcell : *(USHORT *)&os2cell);
  542.         --os2col;                  /* move cursor back       */
  543.         i = os2row  * os2mcol + os2col; 
  544.     }
  545.     else {
  546.         cell = (0x00ff & c) | (revflag ? *(USHORT *)&os2rcell : *(USHORT *)&os2cell);
  547.         i = os2row  * os2mcol + os2col;
  548.         ++os2col;           /* move cursor forward  */
  549.     }
  550.     lvb[i] = cell;
  551.     if (i < lvbMin)
  552.         lvbMin = i;
  553.     if (i > lvbMax)
  554.         lvbMax = i;
  555. }
  556.  
  557.  
  558. /*----------------------------------------------------------------------*/
  559. /* os2eeol()                                                            */
  560. /* Erase to end of line.                                                */
  561. /*----------------------------------------------------------------------*/
  562.  
  563. int PASCAL NEAR os2eeol()
  564. {
  565.     USHORT  cell = ' ';
  566.     USHORT  i;
  567.  
  568.     cell |= (revflag ? *(USHORT *)&os2rcell : *(USHORT *)&os2cell);
  569.    
  570.     i = os2row * os2mcol + os2col;  /* current cursor position */
  571.     if (i < lvbMin)
  572.         lvbMin = i;
  573.     while (i < os2row * os2mcol + term.t_ncol+term.t_colorg)
  574.         lvb[ i++] = cell;
  575.     if (--i > lvbMax)
  576.         lvbMax = i;
  577. }
  578.  
  579.  
  580. /*----------------------------------------------------------------------*/
  581. /* os2eeop()                                                            */
  582. /* Erase to end of page.                                                */
  583. /*----------------------------------------------------------------------*/
  584.  
  585. int PASCAL NEAR os2eeop()
  586. {
  587.     USHORT  cell = ' ';
  588.     USHORT  i;
  589.  
  590. #if COLOR
  591.     if (dtype != CDMONO)
  592.         cell |= (ctrans[gbcolor] << 4 | ctrans[gfcolor]) << 8;
  593.     else
  594.         cell |= 0x0700;
  595. #else
  596.     cell |= 0x0700;
  597. #endif
  598.  
  599.     VioScrollUp(term.t_roworg,term.t_colorg,
  600.                 term.t_nrow-1+term.t_roworg,
  601.                 term.t_ncol-1+term.t_colorg,
  602.                 0xFFFF,&cell,0);
  603. }
  604.  
  605. /*----------------------------------------------------------------------*/
  606. /* os2rev()                                                             */
  607. /* Change reverse video state.                                          */
  608. /*----------------------------------------------------------------------*/
  609.  
  610. int PASCAL NEAR os2rev( int state) /* state: TRUE = reverse, FALSE = normal */
  611. {
  612.     revflag = state;
  613. }
  614.  
  615. /*----------------------------------------------------------------------*/
  616. /* os2cres()                                                            */
  617. /* Change the screen resolution.                                        */
  618. /*----------------------------------------------------------------------*/
  619.  
  620. int PASCAL NEAR os2cres(char *res)  /* name of desired video mode */
  621. {
  622.     USHORT  err;
  623.     int type;       /* video mode type  */
  624.     VIOMODEINFO vioModeInfo;
  625.  
  626.     vioModeInfo = initial.vioModeInfo;
  627.    
  628.     /* From the name, find the type of video mode.  */
  629.     for (type = 0; type < NDRIVE; type++) {
  630.         if (strcmp(res, drvname[type]) == 0)
  631.             break;
  632.     }
  633.     if (type == NDRIVE)
  634.         return(FALSE); /* not a mode we know about  */
  635.  
  636.     vioModeInfo.col = 80;
  637.     switch (type) {
  638.         case CDMONO:
  639.         case CDCGA:
  640.             vioModeInfo.row = 25;
  641.             break;
  642.         case CDEGA:
  643.             vioModeInfo.row = 43;
  644.             break;
  645.         case CDVGA:
  646.             vioModeInfo.row = 50;
  647.             break;
  648.  
  649.         case CDSVGA1:
  650.             vioModeInfo.col = 132;
  651.             vioModeInfo.row = 25;
  652.             break;
  653.  
  654.         case CDSVGA2:
  655.             vioModeInfo.col = 132;
  656.             vioModeInfo.row = 44;
  657.             break;
  658.     }
  659.    
  660.     if (VioSetMode(&vioModeInfo, 0)) /* change modes    */
  661.     {
  662.         os2cres("CGA");                 /* couldn't do it, use CGA instead */
  663.         return(TRUE);                /* will do in every case */
  664.     }
  665.  
  666.     newsize(TRUE, vioModeInfo.row);
  667.     newwidth(TRUE, vioModeInfo.col);
  668.  
  669.     os2mcol = vioModeInfo.col;
  670.     term.t_mcol= vioModeInfo.col;
  671.     os2mrow = vioModeInfo.row;
  672.     
  673.     /* reset the $sres environment variable */
  674.     strcpy(sres, drvname[type]);
  675.     dtype = type;               /* set the current mode */
  676.    
  677.     return TRUE;
  678. }
  679.  
  680.  
  681. /*----------------------------------------------------------------------*/
  682. /* spal()                                                               */
  683. /* Change pallette settings.    (Does nothing.)                         */
  684. /*----------------------------------------------------------------------*/
  685.  
  686. PASCAL NEAR spal(char *dummy)
  687. {
  688. }
  689.  
  690.  
  691. /*----------------------------------------------------------------------*/
  692. /* os2beep()                                                            */
  693. /*----------------------------------------------------------------------*/
  694.  
  695. int PASCAL NEAR os2beep()
  696. {
  697.     DosBeep(1200, 175);
  698. }
  699.  
  700.  
  701. /*----------------------------------------------------------------------*/
  702. /* os2open()                            */
  703. /* Find out what kind of video adapter we have and the current video */
  704. /* mode.  Even if the adapter supports a higher resolution mode than is */
  705. /* in use, we still use the current mode.           */
  706. /*----------------------------------------------------------------------*/
  707.  
  708. int PASCAL NEAR os2open()
  709. {
  710. #if MOUSE
  711.     PTRLOC mouse_pos; /* position to place mouse */
  712.     USHORT event_mask;      /* event mask for mouse handling */
  713. #endif
  714.     USHORT rc;
  715.  
  716. /*    This prevents the abnormal termination of emacs caused by CTRL-C
  717.     or CTRL-BREAK,    otherwise CTRL_BREAK terminates EVERYTIME,
  718.     CTRL-C in saving action leaving some tmp-files and no workfile
  719.     and me swearing. This seems to works for all OS/2 Compilers.
  720.     (WULF BRAMESFELD)
  721. */
  722.     signal(SIGBREAK,SIG_IGN);
  723.     signal(SIGINT,SIG_IGN);
  724.  
  725.     initial.vioConfigInfo.cb = 0x0A;
  726.     VioGetConfig(0, &initial.vioConfigInfo, 0);
  727.     switch (initial.vioConfigInfo.adapter) {
  728.         case DISPLAY_CGA:
  729.             dtype = CDCGA;
  730.             break;
  731.         case DISPLAY_EGA:
  732.             dtype = CDEGA;
  733.             break;
  734.  
  735.         case DISPLAY_MONOCHROME:
  736.             dtype = CDMONO;
  737.  
  738.         case DISPLAY_VGA:    /* because of all that fantastic new modes and color
  739.                                is better than BW CGA is my favourite default */
  740.         default:
  741.             dtype = CDVGA;
  742.             break;
  743.     }
  744.     strcpy(sres, drvname[dtype]);
  745.  
  746.     initial.vioModeInfo.cb = 0x0E;
  747.     rc = 
  748.     VioGetMode(&initial.vioModeInfo, 0);
  749.     if ( rc != 0) { perror("VioGetMode"); exit(1); }
  750.     newsize(TRUE, initial.vioModeInfo.row);
  751.     newwidth(TRUE, initial.vioModeInfo.col);
  752.  
  753.     os2mcol = initial.vioModeInfo.col;
  754.     term.t_mcol= initial.vioModeInfo.col;
  755.     os2mrow = initial.vioModeInfo.row;
  756.            
  757.     rc = 
  758.     VioGetAnsi(&initial.ansiState, 0);
  759.     if ( rc != 0) { perror("VioGetAnsi"); exit(1); }
  760.     rc = 
  761.     VioGetBuf((PULONG)&lvb, &lvbLen, 0);
  762.     if ( rc != 0) { perror("VioGetBuf"); exit(1); }
  763.  
  764. #if !ICC && OS2 >= 2
  765. #ifndef __EMX__
  766. #warning The last function returned a 16:16 ptr 
  767. #warning which has to be converted to a 32 bit 
  768. #warning flat pointer. If your compiler does not 
  769. #warning do this automaticly you have to insert 
  770. #warning some code here. Otherwise the programm 
  771. #warning will not run (starting produces an 
  772. #warning error).
  773. #else
  774.     /* Convert the return 16 bit far pointer to 32 bit flat pointer */
  775.     lvb = (USHORT*)_emx_16to32( (_far16ptr)lvb );
  776. #endif /* __EMX__ */ 
  777. #endif /* !ICC && OS2 >= 2 */
  778.     lvbMin = lvbLen;
  779.     lvbMax = 0;
  780.  
  781.     revexist = TRUE;
  782.     revflag = FALSE;
  783.  
  784.     /* initialize our character input queue */
  785.     in_init();
  786.     strcpy(os, "OS2");
  787.  
  788. #if MOUSE
  789.     /* find out if we have a mouse */
  790.     if (MouOpen((PSZ)0, &mouse_handle) != 0) {
  791.         mexist = FALSE;
  792.         return;
  793.     }
  794.  
  795.     /* find out some info about the mouse */
  796.     mexist = TRUE;
  797.     MouGetNumButtons(&nbuttons, mouse_handle);
  798.  
  799.     /* tell it what events were interested in */
  800.     /* event_mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;*/
  801.     event_mask = 0xff;
  802.     MouSetEventMask(&event_mask, mouse_handle);
  803.  
  804.     /* get the mouse in the upper right corner */
  805.     oldrow = mouse_pos.row = 0; /* top row of display */
  806.     oldcol = mouse_pos.col = (term.t_ncol - 1); /* last col of display */
  807.     MouSetPtrPos(&mouse_pos, mouse_handle);
  808. #else
  809.     mexist = FALSE;
  810. #endif
  811. }
  812.  
  813. /*----------------------------------------------------------------------*/
  814. /* os2close()                                                           */
  815. /* Restore the original video settings.                                 */
  816. /*----------------------------------------------------------------------*/
  817.  
  818. int PASCAL NEAR os2close()
  819. {
  820. #if MOUSE
  821.     /* close our use of the mouse */
  822.     if (mexist)
  823.         MouClose(mouse_handle);
  824. #endif
  825.  
  826.     VioSetAnsi(initial.ansiState, 0);
  827.     VioSetMode(&initial.vioModeInfo, 0);
  828.     VioSetCurPos(initial.vioModeInfo.row - 1,
  829.              initial.vioModeInfo.col - 1, 0);
  830. }
  831.  
  832. /*----------------------------------------------------------------------*/
  833. /* os2kopen()                                                           */
  834. /* Open the keyboard.                                                   */
  835. /*----------------------------------------------------------------------*/
  836.  
  837. int PASCAL NEAR os2kopen()
  838. {
  839.     KBDINFO kbdInfo;
  840.  
  841.     initial.kbdInfo.cb = 0x000A;
  842.     KbdGetStatus(&initial.kbdInfo, 0);  
  843.     kbdInfo = initial.kbdInfo;
  844.     kbdInfo.fsMask &= ~0x0001;  /* not echo on  */
  845.     kbdInfo.fsMask |= 0x0002;       /* echo off     */
  846.     kbdInfo.fsMask &= ~0x0008;  /* cooked mode off  */
  847.     kbdInfo.fsMask |= 0x0004;       /* raw mode     */
  848.     kbdInfo.fsMask &= ~0x0100;  /* shift report off */
  849.     KbdSetStatus(&kbdInfo, 0);
  850. }
  851.  
  852.  
  853. /*----------------------------------------------------------------------*/
  854. /* os2kclose()                                                          */
  855. /* Close the keyboard.                                                  */
  856. /*----------------------------------------------------------------------*/
  857.  
  858. int PASCAL NEAR os2kclose()
  859. {
  860.     KbdSetStatus(&initial.kbdInfo, 0); /* restore original state    */
  861. }
  862.  
  863. #if FLABEL
  864. PASCAL NEAR fnclabel(f, n)  /* label a function key */
  865.  
  866. int f,n;    /* default flag, numeric argument [unused] */
  867.  
  868. {
  869.     /* on machines with no function keys...don't bother */
  870.     return(TRUE);
  871. }
  872. #endif
  873.  
  874. #if 0
  875. /*----------------------------------------------------------------------*/
  876. /*  scwrite()                                                           */
  877. /* Write a line to the screen.                                          */
  878. /* I tried using this routine with MEMMAP = 1, but there were too many  */
  879. /* problems with the cursor and flushing the buffer.                    */
  880. /*----------------------------------------------------------------------*/
  881.  
  882. scwrite(
  883.     int  row,     /* row of screen to place outstr on */
  884.     char *outstr, /* string to write out (must be term.t_ncol long) */
  885.     int  forg,    /* foreground color of string to write */
  886.     int  bacg,    /* background color */
  887. {
  888.     USHORT   attr;
  889.     int  i;
  890.  
  891.     attr = (((ctrans[bacg] & 15) << 4) | (forg & 15)) << 8;
  892.  
  893.     for (i = row * term.t_ncol;  i < (row + 1) * term.t_ncol;  i++)
  894.         lvb[i] = attr | *(outstr++);
  895.  
  896.     if (i < lvbMin)
  897.         lvbMin = i;
  898.     if (i > lvbMax)
  899.         lvbMax = i;
  900. }
  901. #endif
  902. #endif
  903.  
  904.