home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / desmet_c / ansi.c next >
Encoding:
C/C++ Source or Header  |  1988-12-27  |  7.3 KB  |  257 lines

  1. /*************************************************************************
  2. main function to test some of the ansi device driving functions
  3. which follow below.  NOTE:  you need your device set to ansi.sys in the
  4. config.sys for this to work.
  5.  
  6. written by Rex Jaeschke of Rockville, MD. 1983 (301) 251-8987
  7. compiler used DeSmet v2.2
  8.  
  9. ci () is a DeSmet compiler specific function which does direct console
  10. input of 1 character without echoing it. It is used in sgr_dsr and the
  11. testing program only. All else should run on any compiler.
  12. *************************************************************************/
  13.  
  14. #define CTRL_C 3
  15. #define BELL 7
  16. #define NULL '\0'
  17. #define LINEMIN 1  /* Minimum screen line # */
  18. #define LINEMAX 25 /* Maximum screen line # */
  19. #define COLMMIN 1  /* Minimum screen column # */
  20. #define COLMMAX 80 /* Maximum screen column # */
  21. #define L_ARROW 75
  22. #define R_ARROW 77
  23. #define U_ARROW 72
  24. #define D_ARROW 80
  25. #define HOME 71
  26. #define END 79
  27. #define C_HOME 119
  28.  
  29. main ()
  30. {
  31.     int c;
  32.  
  33.     scr_ed ();               /* clear screen */
  34.     scr_cup (12,40);         /* move to screen center */
  35.     while ((c = ci()) != CTRL_C) {
  36.          if (c == NULL) {    /* do we have extended code? */
  37.               c = ci();
  38.               switch (c) {
  39.               case L_ARROW:
  40.                    scr_cub (1);
  41.                    break;
  42.               case R_ARROW:
  43.                    scr_cuf (1);
  44.                    break;
  45.               case U_ARROW:
  46.                    scr_cuu (1);
  47.                    break;
  48.               case D_ARROW:
  49.                    scr_cud (1);
  50.                    break;
  51.               case C_HOME:
  52.                    scr_ed ();
  53.                    break;
  54.               case HOME:
  55.                    scr_cup (LINEMIN,COLMMIN);
  56.                    break;
  57.               case END:
  58.                    scr_cup (LINEMAX,COLMMAX);
  59.                    break;
  60.               default:
  61.                    putchar (BELL);
  62.                    break;
  63.               }
  64.          }
  65.          else
  66.               putchar (BELL);
  67.     }         
  68. }
  69.  
  70.  
  71. #define ESCAPE 27  /* ASCII ESC character definition */
  72.  
  73. /*************************************************************************
  74. SCR_CUB - Cursor Backward.
  75.  
  76. Moves the cursor backward n columns. Current line remains unchanged. If # of
  77. columns exceeds left-of-screen, cursor is left at the left-most column.
  78. *************************************************************************/
  79.  
  80. scr_cub (ncolms)
  81. int ncolms;
  82. {
  83.     printf ("%c[%dD",ESCAPE,ncolms);
  84. }
  85.  
  86. /*************************************************************************
  87. SCR_CUD - Cursor Down.
  88.  
  89. Moves the cursor down n lines. Current column remains unchanged. If # of lines
  90. to move down exceeds bottom-of-screen, cursor is left at the bottom.
  91. *************************************************************************/
  92.  
  93. scr_cud (nlines)
  94. int nlines;
  95. {
  96.     printf ("%c[%dB",ESCAPE,nlines);
  97. }
  98.  
  99. /*************************************************************************
  100. SCR_CUF - Cursor Forward.
  101.  
  102. Moves the cursor forward n columns. Current line remains unchanged. If # of
  103. columns exceeds right-of-screen, cursor is left at the right-most column.
  104. *************************************************************************/
  105.  
  106. scr_cuf (ncolms)
  107. int ncolms;
  108. {
  109.     printf ("%c[%dC",ESCAPE,ncolms);
  110. }
  111.  
  112. /*************************************************************************
  113. SCR_CUP - Cursor Position. (same as HVP)
  114.  
  115. Moves the cursor to the specified position line,colm.
  116. *************************************************************************/
  117.  
  118. scr_cup (line,colm)
  119. int line,colm;
  120. {
  121.     printf ("%c[%d;%dH",ESCAPE,line,colm);
  122. }
  123.  
  124. /*************************************************************************
  125. SCR_CUU - Cursor Up.
  126.  
  127. Moves the cursor up n lines. Current column remains unchanged. If # of lines
  128. to move up exceeds top-of-screen, cursor is left at the top.
  129. *************************************************************************/
  130.  
  131. scr_cuu (nlines)
  132. int nlines;
  133. {
  134.     printf ("%c[%dA",ESCAPE,nlines);
  135. }
  136.  
  137. /*************************************************************************
  138. SCR_DSR - Device Status Report.
  139.  
  140. Returns the Cursor Position Report (CPR) sequence in the form ESC[line;colmR
  141.  
  142. ci () is a DeSmet compiler specific function which does direct console
  143. input of 1 character without echoing it.
  144. *************************************************************************/
  145.  
  146. scr_dsr (line,colm)
  147. int *line,*colm;
  148. {
  149.     int i = 0;
  150.     char cpr[10];
  151.  
  152.     printf ("%c[6n",ESCAPE);
  153.     while ((cpr[i++] = ci ()) != 'R')
  154.          ;
  155.     cpr[i] = '\0';
  156.  
  157. /* format of cpr[] is ESC[rr;ccR row and colm are always two digits */
  158.  
  159.     *line = ((cpr[2]-'0')*10)+cpr[3]-'0';
  160.     *colm = ((cpr[5]-'0')*10)+cpr[6]-'0';
  161. }
  162.  
  163. /*************************************************************************
  164. SCR_ED - Erase in Display.
  165.  
  166. Erases all of the screen leaving the cursor at home
  167. *************************************************************************/
  168.  
  169. scr_ed ()
  170. {
  171.     printf ("%c[2J",ESCAPE);
  172. }
  173.  
  174. /*************************************************************************
  175. SCR_EL - Erase in Line.
  176.  
  177. Erases from the cursor to the end of the line including the cursor position.
  178. *************************************************************************/
  179.  
  180. scr_el ()
  181. {
  182.     printf ("%c[2K",ESCAPE);
  183. }
  184.  
  185. /*************************************************************************
  186. SCR_HVP - Horizontal and Vertical Position. (same as CUP)
  187.  
  188. Moves the cursor to the specified position line,colm.
  189. *************************************************************************/
  190.  
  191. scr_hvp (line,colm)
  192. int line,colm;
  193. {
  194.     printf ("%c[%d;%dH",ESCAPE,line,colm);
  195. }
  196.  
  197. /*************************************************************************
  198. SCR_RCP - Restore Cursor Position.
  199.  
  200. Restores the cursor to the value it had when previously saved by scr_scp.
  201. *************************************************************************/
  202.  
  203. scr_rcp ()
  204. {
  205.     printf ("%c[u",ESCAPE);
  206. }
  207.  
  208. /*************************************************************************
  209. SCR_SCP - Save Cursor Position.
  210.  
  211. Saves the current cursor position for later restoration by scr_rcp.
  212. *************************************************************************/
  213.  
  214. scr_scp ()
  215. {
  216.     printf ("%c[s",ESCAPE);
  217. }
  218.  
  219. /*************************************************************************
  220. SCR_SGR - Set Graphics Rendition.
  221.  
  222. Sets the character attribute specified by the parameter.
  223. Attributes remain in force until reset or changed.
  224. *************************************************************************/
  225.  
  226. scr_sgr (attrib)
  227. int attrib;
  228. {
  229.     printf ("%c[%dm",ESCAPE,attrib);
  230. }
  231.  
  232. /*************************************************************************
  233. SCR_SM - Set Mode.
  234.  
  235. Sets the screen width or type specified by the parameter.
  236. *************************************************************************/
  237.  
  238. scr_sm (param)
  239. int param;
  240. {
  241.     printf ("%c[=%dh",ESCAPE,param);
  242. }
  243.  
  244. /*************************************************************************
  245. SCR_RM - Reset Mode.
  246.  
  247. Sets the screen width or type specified by the parameter.
  248. *************************************************************************/
  249.  
  250. scr_rm (param)
  251. int param;
  252. {
  253.     printf ("%c[=%dl",ESCAPE,param);
  254. }
  255. *****************************************************/
  256.  
  257.