home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / console / svgatext.3 / svgatext / SVGATextMode-1.3 / contrib / consoletools / gettext_settext_demo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-06  |  2.8 KB  |  132 lines

  1. /*
  2. Unlike "vcs", reading "vcsa" from offset 0 returns a buffer with a 4-byte
  3. header containing (as I recall) rows, cols, cur_col, cur_row, in that order.
  4. When placing new data in the buffer, offset 4 should be treated as 0, but
  5. the whole buffer, header included, should be written back from 0.
  6. */
  7. /*
  8. Here's my "port" of Borland C's gettext() and puttext(), using /dev/vcsa0.
  9. They read/write rectangular areas of the screen.
  10.  
  11. written by Bob McCracken
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <sys/ioctl.h>
  17. #include <termios.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20.  
  21. #define DEV    "/dev/vcsa0"
  22.  
  23. #ifndef uchar
  24. #define uchar unsigned char
  25. #endif
  26.  
  27. int GetText (uchar *buf, int y1, int x1, int y2, int x2)
  28. {
  29.   int Bpr, p, h, v, x, y;
  30.   uchar *M, *Mem;
  31.   struct winsize W;
  32.  
  33.   ioctl (0, TIOCGWINSZ, &W);
  34.  
  35. /*
  36. Borland's version defaults to the real full-screen size if the
  37. parms are invalid. Not liking that, I return an error, instead.
  38. */  
  39.   if (x1 < 0 || y1 < 0 || x2 >= W.ws_col || y2 >= W.ws_row ||
  40.  
  41.     x1 > x2 || y1 > y2) return -1;
  42.       
  43.   if ((y = open (DEV, 0)) < 0) return -1;
  44.   
  45.   Bpr = (W.ws_col << 1);    /* bytes-per-row */
  46.   
  47.   x = (Bpr * W.ws_row) + 4;    /* Include 4-byte header in buffer size */
  48.   
  49.   Mem = (uchar *) malloc (x);    /* Ought to check for failure here! */
  50.   
  51.   M = Mem + 4;        /* Point M to start of actual screen data */
  52.   
  53.   read (y, Mem, x); close (y);
  54.  
  55. /*
  56. "Rectangularize" the screen data and copy to caller's buffer ..
  57. */  
  58.   v = y2-y1+1; h = (x2-x1+1) << 1; p = (y1 * Bpr) + (x1 << 1);
  59.  
  60.   for (x = y = 0; y < v; y++, p += Bpr, x += h) memcpy (buf+x, M+p, h);
  61.  
  62.   free (Mem); return 0;
  63. }
  64.  
  65. /* Same as above, in reverse ... */
  66.  
  67. int PutText (uchar *buf, int y1, int x1, int y2, int x2)
  68. {
  69.   int fd, ss, Bpr, p, h, v, x, y;
  70.   uchar *M, *Mem;
  71.   struct winsize W;
  72.  
  73.   ioctl (0, TIOCGWINSZ, &W);
  74.   
  75.   if (x1 < 0 || y1 < 0 || x2 >= W.ws_col || y2 >= W.ws_row ||
  76.   
  77.       x1 > x2 || y1 > y2) return -1;
  78.       
  79.   if ((fd = open (DEV, 2)) < 0) return -1;
  80.   
  81.   Bpr = (W.ws_col << 1);
  82.   
  83.   ss = (Bpr * W.ws_row) + 4;
  84.   
  85.   Mem = (uchar *) malloc (ss);
  86.   
  87.   M = Mem + 4;
  88.  
  89. /* Get current screen contents and update with caller's "rectangle" */
  90.  
  91.   read (fd, Mem, ss); lseek (fd, 0, 0);
  92.   
  93.   v = y2-y1+1; h = (x2-x1+1) << 1; p = (y1 * Bpr) + (x1 << 1);
  94.  
  95.   for (x = y = 0; y < v; y++, p += Bpr, x += h) memcpy (M+p, buf+x, h);
  96.  
  97.   write (fd, Mem, ss); close (fd);
  98.   
  99.   free (Mem); return 0;
  100. }
  101.  
  102. /* Usage example: Change background color of a small area. */
  103.  
  104. #ifdef TEST_GETPUT
  105.  
  106. #define BGND    0x4F    /* white on red */
  107.  
  108. #define Y1    6
  109. #define    X1    10
  110. #define    Y2    12
  111. #define X2    39
  112.  
  113. #define XLEN    (X2-X1+1)
  114. #define YLEN    (Y2-Y1+1)
  115.  
  116. #define BSIZE    ((XLEN<<1)*YLEN)
  117.  
  118. uchar scrn [BSIZE];
  119.  
  120. void main()
  121. {
  122.   int x;
  123.   
  124.   if (GetText (scrn, Y1,X1, Y2,X2) < 0) printf ("oops"); else
  125.     {  
  126.     for (x = 1; x < BSIZE; x += 2) scrn[x] = BGND;
  127.  
  128.     PutText (scrn, Y1,X1, Y2,X2);
  129.     }    
  130. }
  131. #endif
  132.