home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / courses / cursesio.c < prev    next >
Text File  |  1992-07-10  |  10KB  |  349 lines

  1. /****************************************************************/
  2. /* Low-level I/O functions of the PCcurses package, 'C' version */
  3. /*                                                              */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,  */
  8. /* and therefore consider myself free make it public domain.    */
  9. /*              Bjorn Larsson (...mcvax!enea!infovax!bl)        */
  10. /****************************************************************/
  11. /* BUT: this particular module was written by                   */
  12. /*      Steve Creps (creps@silver.bacs.indiana.edu)             */
  13. /* It provides 'C' versions of the low-level I/O functions      */
  14. /* that are also available in assembler in cursesio.asm.        */
  15. /* B. Larsson took the liberty of modifying its style slightly  */
  16. /* when incorporating it into PCcurses v.1.2.                   */
  17. /****************************************************************/
  18. /* 1.0: Original, S. Creps:                             880827  */
  19. /* 1.2: Style clean-up, rcsid[] string for maintenance: 881002  */
  20. /* 1.3: MSC -W3, Turbo'C' -w -w-pro checkes:            881005  */
  21. /****************************************************************/
  22.  
  23. #ifdef __OS2__
  24. # define INCL_SUB
  25. # define INCL_DOS
  26. # include <os2.h>
  27. # include <signal.h>
  28. #else
  29. # include <dos.h>
  30. static union REGS regs;
  31. #endif
  32.  
  33. #include <curses.h>
  34. #include <curspriv.h>
  35.  
  36. char _curses_curseio_rcsid[] = "@(#)cursesio.c v1.3 - 881005";
  37.  
  38. /****************************************************************/
  39. /* _Cursescattr() writes char 'chr' with attributes 'attr' to   */
  40. /* the current cursor location.                                 */
  41. /****************************************************************/
  42.  
  43. void _cursescattr(chr, attr)
  44.   char  chr;
  45.   char  attr;
  46.   {
  47. #ifdef __OS2__
  48.   USHORT x, y;
  49.   UCHAR buf[2];
  50.   
  51.   buf[0]=chr; buf[1]=attr;
  52.   VioGetCurPos(&y, &x, (HVIO)0);
  53.   VioWrtCellStr(buf, 2, y, x, 0);
  54. #else
  55.   regs.h.ah = 0x09;
  56.   regs.h.al = (unsigned char)chr;
  57.   regs.h.bh = 0x00;
  58.   regs.h.bl = (unsigned char)attr;
  59.   regs.x.cx = 0x01;
  60.   int86(0x10, ®s, ®s);
  61. #endif
  62.   } /* _cursescattr */
  63.  
  64. /****************************************************************/
  65. /* _Cursescursor() sets the cursor position in video page 0.    */
  66. /* 'row' and 'column' are the cursor address. If 'row' is set   */
  67. /*  to _cursesgrows(), no cursor at all is displayed.           */
  68. /****************************************************************/
  69.  
  70. void _cursescursor(row, column)
  71.   int   row;
  72.   int   column;
  73.   {
  74. #ifdef __OS2__
  75.   VioSetCurPos(row, column, 0);
  76. #else
  77.   regs.h.ah = 0x02;
  78.   regs.h.bh = 0x00;
  79.   regs.h.dh = (unsigned char)row;
  80.   regs.h.dl = (unsigned char)column;
  81.   int86(0x10, ®s, ®s);
  82. #endif
  83.   }/* _cursescursor */
  84.  
  85. /****************************************************************/
  86. /* _Cursesgcols() returns the current number of columns on the  */
  87. /* screen.                                                      */
  88. /****************************************************************/
  89.  
  90. #ifdef __OS2__
  91. static VIOMODEINFO *modeinfo = NULL;
  92. #endif
  93.  
  94. int _cursesgcols()
  95.   {
  96. #ifdef __OS2__
  97.   if (modeinfo == NULL)
  98.     modeinfo = (VIOMODEINFO *)malloc(sizeof(VIOMODEINFO));
  99.  
  100.   VioGetMode((PVIOMODEINFO)&modeinfo,0);
  101.   if (modeinfo->col == 0) return 80;
  102.   return modeinfo->col;
  103. #else
  104.   regs.h.ah = 0x0f;
  105.   int86(0x10, ®s, ®s);
  106.   return (int)regs.h.ah;
  107. #endif
  108.   } /* _cursesgcols */
  109.  
  110. /****************************************************************/
  111. /* _Cursesgrows() returns the current number of rows on the     */
  112. /* screen.                                                      */
  113. /****************************************************************/
  114.  
  115. int _cursesgrows()
  116.   {
  117. #ifdef __OS2__
  118.   if (modeinfo == NULL)
  119.     modeinfo = (VIOMODEINFO *)malloc(sizeof(VIOMODEINFO));
  120.  
  121.   VioGetMode(modeinfo,0);
  122.   if (modeinfo->row == 0) return 25;
  123.   return modeinfo->row;
  124. #else
  125.   return 25;
  126. #endif
  127.   } /* _cursesgcols */
  128.  
  129. /****************************************************************/
  130. /* _Cursesputc() outputs character 'chr' to screen in tty       */
  131. /* fashion. If a colour mode is active, the character is writ-  */
  132. /* ten with colour 'colour'.                                    */
  133. /****************************************************************/
  134.  
  135. void _cursesputc(chr, color)
  136.   char  chr;
  137.   char  color;
  138.   {
  139. #ifdef __OS2__
  140.   USHORT x, y;
  141.  
  142.   VioGetCurPos(&y, &x, (HVIO)0);
  143.   VioWrtNAttr(&color,1,y,x,0);
  144.   VioWrtTTY(&chr,1,0);
  145. #else
  146.   regs.h.ah = 0x0e;
  147.   regs.h.al = (unsigned char)chr;
  148.   regs.h.bh = 0x00;
  149.   regs.h.bl = (unsigned char)color;
  150.   int86(0x10, ®s, ®s);
  151. #endif
  152.   } /* _cursesputc */
  153.  
  154. /****************************************************************/
  155. /* _Cursesscroll() scrolls a window in the current page up or   */
  156. /*  down. Urow, lcol, lrow,rcol are the window coordinates.     */
  157. /* Lines is the number of lines to scroll. If 0, clears the     */
  158. /* window, if < 0 scrolls down, > 0 scrolls up. Blanks areas    */
  159. /* that are left, and sets character attributes to attr. If in  */
  160. /* a colour graphics mode, fills them with the colour 'attr'    */
  161. /* instead.                                                     */
  162. /****************************************************************/
  163.  
  164. void _cursesscroll(urow, lcol, lrow, rcol, lines, attr)
  165.   int   urow;
  166.   int   lcol;
  167.   int   lrow;
  168.   int   rcol;
  169.   int   lines;
  170.   char  attr;
  171.   {
  172. #ifdef __OS2__
  173.   char  buf[2];
  174.  
  175.   buf[0]=' '; buf[1]=attr;
  176.   if(lines == 0)    /* Clear window */
  177.      {
  178.      lines=-1;
  179.      VioScrollDn(urow,lcol,lrow,rcol,lines,buf,0);
  180.      }
  181.   else if (lines < 0) 
  182.      {
  183.      VioScrollDn(urow,lcol,lrow,rcol,-lines,buf,0);
  184.      }
  185.   else
  186.      {
  187.      VioScrollUp(urow,lcol,lrow,rcol,lines,buf,0);
  188.      }
  189. #else
  190.   if (lines >= 0)
  191.     {
  192.     regs.h.ah = 0x06;
  193.     regs.h.al = (unsigned char)lines;
  194.     } /* if */
  195.   else
  196.     {
  197.     regs.h.ah = 0x07;
  198.     regs.h.al = (unsigned char)(-lines);
  199.     } /* else */
  200.   regs.h.bh = (unsigned char)attr;
  201.   regs.h.ch = (unsigned char)urow;
  202.   regs.h.cl = (unsigned char)lcol;
  203.   regs.h.dh = (unsigned char)lrow;
  204.   regs.h.dl = (unsigned char)rcol;
  205.   int86(0x10, ®s, ®s);
  206. #endif
  207.   } /* _cursesscroll */
  208.  
  209. /****************************************************************/
  210. /* _Cursesgcmode() returns the current cursor type. Bits 8-15   */
  211. /* of the return value is the start scan row, and bits 0-7 is   */
  212. /* the end scan row.                                            */
  213. /****************************************************************/
  214.  
  215. int _cursesgcmode()
  216.   {
  217. #ifdef __OS2__
  218. VIOCURSORINFO curinfo;
  219.  
  220.   VioGetCurType(&curinfo,0);
  221.   return ((curinfo.yStart & 0xFF)*256+(curinfo.cEnd & 0xFF));
  222. #else
  223.   regs.h.ah = 0x03;
  224.   regs.h.bh = 0x00;
  225.   int86(0x10, ®s, ®s);
  226.   return (int)regs.x.cx;
  227. #endif
  228.   } /* _cursesgcmode */
  229.  
  230. /****************************************************************/
  231. /* _Cursescmode() sets the cursor type to begin in scan line    */
  232. /* startrow and end in scan line endrow. Both values should be  */
  233. /* 0-31.                                                        */
  234. /****************************************************************/
  235.  
  236. void _cursescmode(startrow, endrow)
  237.   int   startrow;
  238.   int   endrow;
  239.   {
  240. #ifdef __OS2__
  241. VIOCURSORINFO curinfo;
  242.  
  243.   curinfo.yStart=startrow;
  244.   curinfo.cEnd=endrow;
  245.   curinfo.cx=0;
  246.   curinfo.attr=0;
  247.   VioSetCurType(&curinfo,0);
  248. #else
  249.   regs.h.ah = 0x01;
  250.   regs.h.ch = (unsigned char)startrow;
  251.   regs.h.cl = (unsigned char)endrow;
  252.   int86(0x10, ®s, ®s);
  253. #endif
  254.   } /* _cursescmode */
  255.  
  256. /****************************************************************/
  257. /* _Curseskey() returns the next key code struck at the key-    */
  258. /* board. If the low 8 bits are 0, the upper bits contain the   */
  259. /* extended character code. If bit 0-7 are non-zero, the upper  */
  260. /* bits = 0.                                                    */
  261. /****************************************************************/
  262.  
  263. int _curseskey()
  264.   {
  265. #ifdef __OS2__
  266. KBDKEYINFO keyinfo;
  267.   KbdCharIn(&keyinfo,0,0);
  268.   if (keyinfo.chChar != 0)
  269.      return((int)keyinfo.chChar & 0xFF);
  270.   return((int)keyinfo.chScan<<8);
  271. #else
  272.   regs.h.ah = 0x00;
  273.   int86(0x16, ®s, ®s);
  274.   if (regs.h.al != 0)
  275.     return (int)(regs.x.ax & 0x00ff);
  276.   return (int)regs.x.ax;
  277. #endif
  278.   } /* _curseskey */
  279.  
  280. /****************************************************************/
  281. /* _Curseskeytst() returns 1 if a keyboard character is avail-  */
  282. /* able, 0 otherwise.                                           */
  283. /****************************************************************/
  284.  
  285. bool _curseskeytst()
  286.   {
  287. #ifdef __OS2__
  288.   if (kbhit())
  289.      return 1;
  290.   else
  291.      return 0;
  292. #else
  293.   regs.h.ah = 0x01;
  294.   int86(0x16, ®s, ®s);
  295.   return ((bool)((regs.x.cflag & 0x40) ? 1 : 0));
  296. #endif
  297.   } /*_curseskeytst */
  298.  
  299. /****************************************************************/
  300. /* _Cursesgcb() returns 1 if MSDOS BREAK CHECK is on, else 0.   */
  301. /****************************************************************/
  302. #ifdef __OS2__
  303. static int signal_enab = 1;
  304. void FAR null_hand()
  305. {
  306. }
  307. #endif
  308.  
  309. int _cursesgcb()
  310.   {
  311. #ifdef __OS2__
  312.   return signal_enab;
  313. #else
  314.   regs.h.ah = 0x33;
  315.   regs.h.al = 0x00;
  316.   int86(0x21, ®s, ®s);
  317.   return (int)regs.h.dl;
  318. #endif
  319.   } /* _cursesgcb */
  320.  
  321. /****************************************************************/
  322. /* _Cursesscb() sets MSDOS BREAK CHECK according to 'setting'.  */
  323. /****************************************************************/
  324.  
  325. void _cursesscb(setting)
  326.   int setting;
  327.   {
  328. #ifdef __OS2__
  329. static void *old_action;
  330.  
  331.   if((setting == 0)&&(signal_enab == 1))
  332.      {
  333.      old_action = signal(SIGBREAK, SIG_IGN);
  334.      signal_enab = 0;
  335.      }
  336.   else if ((setting == 1)&&(signal_enab == 0))
  337.      {
  338.      (void) signal(SIGBREAK, old_action);
  339.      signal_enab = 1;
  340.      }
  341. #else
  342.   regs.h.ah = 0x33;
  343.   regs.h.al = 0x00;
  344.   regs.h.dl = (unsigned char)(setting ? 1 : 0);
  345.   int86(0x21, ®s, ®s);
  346. #endif
  347.   } /* _cursesscb */
  348. 
  349.