home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / system / csh / src / windowbounds.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  2KB  |  107 lines

  1. /*
  2.  *  get window bounds (dimensions)
  3.  *  Copyright 1994, Andreas M. Kirchwitz
  4.  */
  5.  
  6.  
  7. #include <exec/types.h>
  8. #include <dos/dos.h>
  9. #include <dos/dosextens.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/exec_protos.h>
  12. #include <proto/dos.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. static unsigned char aWSR[] = {0x9b, 0x30, 0x20, 0x71, 0};        /* Window Status Request */
  18. static unsigned char aWBR[] = {0x9b, 0x31, 0x3b, 0x31, 0x3b, 0};    /* Window Bounds Report */
  19.  
  20. #define BUF_LEN            40
  21.  
  22.  
  23.  
  24. void get_WindowBounds_def(int *width, int *height)
  25. {
  26.     *width  = 80;
  27.     *height = 24;
  28. }
  29.  
  30.  
  31.  
  32. void get_WindowBounds_env(int *width, int *height)
  33. {
  34.     unsigned char iline[BUF_LEN + 1];
  35.  
  36.     if (GetVar ("COLUMNS", iline, BUF_LEN, NULL) > 0L)
  37.         *width = atoi(iline);
  38.  
  39.     if (GetVar ("LINES", iline, BUF_LEN, NULL) > 0L)
  40.         *height = atoi(iline);
  41. }
  42.  
  43.  
  44.  
  45. void get_WindowBounds_con(BPTR con, int *width, int *height,long timeout)
  46. {
  47.     unsigned char *p1, *p2, *p3;
  48.     int i = 0, start = 0;
  49.     unsigned char iline[BUF_LEN + 1];
  50.  
  51.     if (!con)
  52.         return;
  53.  
  54.     if (!IsInteractive(con))
  55.         return;
  56.  
  57.     Write (con, aWSR, strlen (aWSR));
  58.  
  59.     /* now fill buffer, start with beginning of WBR */
  60.  
  61.     while (i < BUF_LEN && WaitForChar (con, timeout)) {
  62.         Read (con, &iline[i], 1);
  63.         if (start)
  64.             i++;
  65.         else if (iline[i] == aWBR[0]) {
  66.             i++;
  67.             start = 1;
  68.         }
  69.     }
  70.  
  71.     iline[i] = 0;
  72.  
  73.     if (i > 0) {
  74.         if (p1 = strstr (iline, aWBR)) {
  75.             p1 += strlen (aWBR);
  76.             if (p2 = strchr (p1, ';')) {
  77.                 *p2 = 0;
  78.                 ++p2;
  79.                 if (p3 = strchr (p2, 'r')) {
  80.                     *p3 = 0;
  81.  
  82.                     *height = atoi (p1);
  83.                     *width = atoi (p2);
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     /*
  90.         9b 31 3b 31 3b <height> 3b <width> 20 72
  91.      */
  92. }
  93.  
  94.  
  95.  
  96. void get_WindowBounds_Output(int *width, int *height,long timeout)
  97. {
  98.     if (Output()) {
  99.         if (IsInteractive(Output())) {
  100.             SetMode(Output(),1);    /* raw */
  101.             get_WindowBounds_con(Output(),width,height,timeout);
  102.             SetMode(Output(),0);    /* cooked */
  103.         }
  104.     }
  105. }
  106.  
  107.