[comp.windows.x] patches to xwininfo for metric and english display

eichin@ATHENA.MIT.EDU (Mark W. Eichin) (06/20/88)

While finding a bug in XTextExtents, I had an application produce a
really big message window (more than 10000 pixels across.) In order to
get a clearer picture of how big these windows are, I threw together
-metric and -english options to xwininfo. It is so much more
satisfying to be able to say "Oh yes, that window is 10 feet 5 inches
wide, placing the edge over by the water cooler" than "That window is
10000 pixels wide". For the bilingual :-) it will print both
conversions if both are specified.

Here is the output of 'diff -c' for xwininfo.c and xwininfo.man (yes,
Virginia, there is documentation :-) This is relative to unpatched
X11R2.

				Mark Eichin
			<eichin@athena.mit.edu>
		SIPB Member & Project Athena ``Watchmaker'' 

ps. The bug (and fix) in XTextExtents has been sent to xbugs. 
--
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	xwininfo.man.patch
#	xwininfo.c.patch
# This archive created: Sun Jun 19 15:47:52 1988
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'xwininfo.man.patch'
then
	echo shar: "will not over-write existing file 'xwininfo.man.patch'"
else
cat << \SHAR_EOF > 'xwininfo.man.patch'
*** /mit/x11dev/src/clients/xwininfo/xwininfo.man	Sun Feb 28 20:13:58 1988
--- xwininfo.man	Sun Jun 19 15:29:18 1988
***************
*** 5,10 ****
--- 5,11 ----
  .B "xwininfo"
  [-help] [-id \fIid\fP] [-root] [-name \fIname\fP] [-int]
  [-tree] [-stats] [-bits] [-events] [-size] [-wm ] [-all]
+ [-inches] [-metric]
  [-display \fIdisplay\fP]
  .SH DESCRIPTION
  .PP
***************
*** 93,98 ****
--- 94,112 ----
  displayed.  Information displayed may include whether or not the application
  accepts input, what the window's icon window # and name is, where the window's
  icon should go, and what the window's initial state should be.
+ .PP
+ .TP 8
+ .B -metric
+ This option causes all individual height, width, and x and y positions to be
+ displayed in millimeters as well as number of pixels, based on what the
+ server thinks the resolution is. Geometry specifications that are in
+ \fB+x+y\fP form are not changed.
+ .TP 8
+ .B -inches
+ This option causes all individual height, width, and x and y positions to be
+ displayed in inches (and feet, yards, and miles if necessary) as well as
+ number of pixels. \fB-metric\fP and \fB-inches\fP may both be enabled at the
+ same time. 
  .PP
  .TP 8
  .B -all
SHAR_EOF
fi
if test -f 'xwininfo.c.patch'
then
	echo shar: "will not over-write existing file 'xwininfo.c.patch'"
else
cat << \SHAR_EOF > 'xwininfo.c.patch'
*** /mit/x11dev/src/clients/xwininfo/xwininfo.c	Fri Apr  8 16:46:56 1988
--- xwininfo.c	Sun Jun 19 15:14:51 1988
***************
*** 59,64 ****
--- 59,68 ----
      fprintf (stderr,
  	"    -wm                  print out window manager hints\n");
      fprintf (stderr,
+ 	"    -inches              print out sizes in inches\n");
+     fprintf (stderr,
+ 	"    -metric              print out sizes in metric\n");
+     fprintf (stderr,
  	"    -all                 -tree, status, -bits, -events, -size, -wm\n");
      fprintf (stderr,
  	"\n");
***************
*** 65,71 ****
--- 69,180 ----
      exit (1);
  }
  
+ /*
+  * pixel to inch, metric converter.
+  * Hacked in by Mark W. Eichin <eichin@athena> [eichin:19880619.1509EST]
+  *
+  * Simply put: replace the old numbers with string print calls.
+  * Returning a local string is ok, since we only ever get called to
+  * print one x and one y, so as long as they don't collide, they're
+  * fine. This is not meant to be a general purpose routine.
+  *
+  */
  
+ #define getdsp(var,fn) var = fn(dpy, DefaultScreen(dpy))
+ int xp=0, xmm=0;
+ int yp=0, ymm=0;
+ int bp=0, bmm=0;
+ int inches = 0, metric = 0;
+ 
+ void scale_init()
+ {
+   getdsp(yp,  DisplayHeight);
+   getdsp(ymm, DisplayHeightMM);
+   getdsp(xp,  DisplayWidth);
+   getdsp(xmm, DisplayWidthMM);
+   bp  = xp  + yp;
+   bmm = xmm + ymm;
+ }
+ 
+ double drem();
+ 
+ #define MILE (5280*12)
+ #define YARD (3*12)
+ #define FOOT (12)
+ 
+ char *nscale(n, np, nmm, nbuf)
+      int n, np, nmm;
+      char *nbuf;
+ {
+   sprintf(nbuf, "%d", n);
+   if(metric||inches) {
+     sprintf(nbuf+strlen(nbuf), " (");
+   }
+   if(metric) {
+     sprintf(nbuf+strlen(nbuf),"%.2f mm%s", ((float) n)*nmm/np, inches?"; ":"");
+   }
+   if(inches) {
+     float in;
+     int mi, yar, ft, inr;
+     in = ((float) n)*(nmm/25.4)/np;
+     inr = (int)in;
+     if(inr>=MILE) {
+       mi = inr/MILE;
+       inr %= MILE;
+       sprintf(nbuf+strlen(nbuf), "%d %s(?!?), ",
+ 	      mi, (mi==1)?"mile":"miles");
+     }
+     if(inr>=YARD) {
+       yar = inr/YARD;
+       inr %= YARD;
+       sprintf(nbuf+strlen(nbuf), "%d %s, ",
+ 	      yar, (yar==1)?"yard":"yards");
+     }
+     if(inr>=FOOT) {
+       ft = inr/FOOT;
+       inr  %= FOOT;
+       sprintf(nbuf+strlen(nbuf), "%d %s, ",
+ 	      ft, (ft==1)?"foot":"feet");
+     }
+     sprintf(nbuf+strlen(nbuf), "%.2f inches", in);
+   }
+   sprintf(nbuf+strlen(nbuf), ")");
+   return(nbuf);
+ }	  
+   
+ char xbuf[BUFSIZ];
+ char *xscale(x)
+      int x;
+ {
+   if(!xp) {
+     scale_init();
+   }
+   return(nscale(x, xp, xmm, xbuf));
+ }
+ 
+ char ybuf[BUFSIZ];
+ char *yscale(y)
+      int y;
+ {
+   if(!yp) {
+     scale_init();
+   }
+   return(nscale(y, yp, ymm, ybuf));
+ }
+ 
+ char bbuf[BUFSIZ];
+ char *bscale(b)
+      int b;
+ {
+   if(!bp) {
+     scale_init();
+   }
+   return(nscale(b, bp, bmm, bbuf));
+ }
+ 
+ 
+ /* end of pixel to inch, metric converter */
+ 
  main(argc, argv)
       int argc;
       char **argv;
***************
*** 113,118 ****
--- 222,235 ----
        size = 1;
        continue;
      }
+     if (!strcmp(argv[i], "-inches")) {
+       inches = 1;
+       continue;
+     }
+     if (!strcmp(argv[i], "-metric")) {
+       metric = 1;
+       continue;
+     }
      if (!strcmp(argv[i], "-all")) {
        tree = stats = bits = events = wm = size = 1;
        continue;
***************
*** 240,251 ****
    ybelow = (dh - win_attributes.y - win_attributes.border_width * 2 -
  	    win_attributes.height);
  
!   printf("\n         ==> Upper left X: %d\n", win_attributes.x);
!   printf("         ==> Upper left Y: %d\n", win_attributes.y);
!   printf("         ==> Width: %d\n", win_attributes.width);
!   printf("         ==> Height: %d\n", win_attributes.height);
    printf("         ==> Depth: %d\n", win_attributes.depth);
!   printf("         ==> Border width: %d\n", win_attributes.border_width);
    printf("         ==> Window class: %s\n", Lookup(win_attributes.class,
  						   _window_classes));
    printf("         ==> Window Map State: %s\n",
--- 357,368 ----
    ybelow = (dh - win_attributes.y - win_attributes.border_width * 2 -
  	    win_attributes.height);
  
!   printf("\n         ==> Upper left X: %s\n", xscale(win_attributes.x));
!   printf("         ==> Upper left Y: %s\n", yscale(win_attributes.y));
!   printf("         ==> Width: %s\n", xscale(win_attributes.width));
!   printf("         ==> Height: %s\n", yscale(win_attributes.height));
    printf("         ==> Depth: %d\n", win_attributes.depth);
!   printf("         ==> Border width: %s\n", bscale(win_attributes.border_width));
    printf("         ==> Window class: %s\n", Lookup(win_attributes.class,
  						   _window_classes));
    printf("         ==> Window Map State: %s\n",
***************
*** 419,468 ****
  	flags = hints.flags;
  	
  	if (flags & USPosition)
! 	  printf("             ==> User supplied location: %d, %d\n",
! 		 hints.x, hints.y);
  
  	if (flags & PPosition)
! 	  printf("             ==> Program supplied location: %d, %d\n",
! 		 hints.x, hints.y);
  
  	if (flags & USSize) {
! 	  printf("             ==> User supplied size: %d by %d\n",
! 		 hints.width, hints.height);
  	}
  
  	if (flags & PSize)
! 	  printf("             ==> Program supplied size: %d by %d\n",
! 		 hints.width, hints.height);
  
  	if (flags & PMinSize)
! 	  printf("             ==> Program supplied minimum size: %d by %d\n",
! 		 hints.min_width, hints.min_height);
  
  	if (flags & PMaxSize)
! 	  printf("             ==> Program supplied maximum size: %d by %d\n",
! 		 hints.max_width, hints.max_height);
  
  	if (flags & PResizeInc) {
! 	  printf("             ==> Program supplied x resize increment: %d\n",
! 		 hints.width_inc);
! 	  printf("             ==> Program supplied y resize increment: %d\n",
! 		 hints.height_inc);
  	  if (flags & USSize && hints.width_inc != 0 && hints.height_inc != 0)
! 	    printf("             ==> User supplied size in resize increments:  %d by %d\n",
! 		   (hints.width / hints.width_inc), 
! 		   (hints.height / hints.height_inc));
  	  if (flags & PSize && hints.width_inc != 0 && hints.height_inc != 0)
! 	    printf("             ==> Program supplied size in resize increments:  %d by %d\n",
! 		   (hints.width / hints.width_inc), 
! 		   (hints.height / hints.height_inc));
          }
  
  	if (flags & PAspect) {
! 	  printf("             ==> Program supplied min aspect ratio: %d/%d\n",
! 		 hints.min_aspect.x, hints.min_aspect.y);
! 	  printf("             ==> Program supplied max aspect ratio: %d/%d\n",
! 		 hints.max_aspect.x, hints.max_aspect.y);
          }
  }
  
--- 536,585 ----
  	flags = hints.flags;
  	
  	if (flags & USPosition)
! 	  printf("             ==> User supplied location: %s, %s\n",
! 		 xscale(hints.x), yscale(hints.y));
  
  	if (flags & PPosition)
! 	  printf("             ==> Program supplied location: %s, %s\n",
! 		 xscale(hints.x), yscale(hints.y));
  
  	if (flags & USSize) {
! 	  printf("             ==> User supplied size: %s by %s\n",
! 		 xscale(hints.width), yscale(hints.height));
  	}
  
  	if (flags & PSize)
! 	  printf("             ==> Program supplied size: %s by %s\n",
! 		 xscale(hints.width), yscale(hints.height));
  
  	if (flags & PMinSize)
! 	  printf("             ==> Program supplied minimum size: %s by %s\n",
! 		 xscale(hints.min_width), yscale(hints.min_height));
  
  	if (flags & PMaxSize)
! 	  printf("             ==> Program supplied maximum size: %s by %s\n",
! 		 xscale(hints.max_width), yscale(hints.max_height));
  
  	if (flags & PResizeInc) {
! 	  printf("             ==> Program supplied x resize increment: %s\n",
! 		 xscale(hints.width_inc));
! 	  printf("             ==> Program supplied y resize increment: %s\n",
! 		 yscale(hints.height_inc));
  	  if (flags & USSize && hints.width_inc != 0 && hints.height_inc != 0)
! 	    printf("             ==> User supplied size in resize increments:  %s by %s\n",
! 		   (xscale(hints.width / hints.width_inc)), 
! 		   (yscale(hints.height / hints.height_inc)));
  	  if (flags & PSize && hints.width_inc != 0 && hints.height_inc != 0)
! 	    printf("             ==> Program supplied size in resize increments:  %s by %s\n",
! 		   (xscale(hints.width / hints.width_inc)), 
! 		   (yscale(hints.height / hints.height_inc)));
          }
  
  	if (flags & PAspect) {
! 	  printf("             ==> Program supplied min aspect ratio: %s/%s\n",
! 		 xscale(hints.min_aspect.x), yscale(hints.min_aspect.y));
! 	  printf("             ==> Program supplied max aspect ratio: %s/%s\n",
! 		 xscale(hints.max_aspect.x), yscale(hints.max_aspect.y));
          }
  }
  
***************
*** 527,534 ****
  	}
  
  	if (flags & IconPositionHint)
! 	  printf("             ==> Initial icon position: %d, %d\n",
! 		 wmhints->icon_x, wmhints->icon_y);
  
  	if (flags & StateHint)
  	  printf("             ==> Initial state is %s\n",
--- 644,651 ----
  	}
  
  	if (flags & IconPositionHint)
! 	  printf("             ==> Initial icon position: %s, %s\n",
! 		 xscale(wmhints->icon_x), yscale(wmhints->icon_y));
  
  	if (flags & StateHint)
  	  printf("             ==> Initial state is %s\n",
SHAR_EOF
fi
exit 0
#	End of shell archive