*** /tmp/T0ekIZa_	Fri Feb 19 20:37:48 1999
--- sqXWindow.c	Fri Feb 19 20:35:39 1999
***************
*** 2,8 ****
   *
   * Author: Ian Piumarta (ian.piumarta@inria.fr)
   *
!  * Last edited: Tue Feb  9 15:25:30 1999 by piumarta (Ian Piumarta) on pingu
   *
   * Code to support displays deeper than 8 bits contributed by: Kazuki YASUMATSU
   *	(kyasu@crl.fujixerox.co.jp, Kazuki.Yasumatsu@fujixerox.co.jp)
--- 2,8 ----
   *
   * Author: Ian Piumarta (ian.piumarta@inria.fr)
   *
!  * Last edited: Thu Jan 28 12:43:05 1999 by piumarta (Ian Piumarta) on fricotin
   *
   * Code to support displays deeper than 8 bits contributed by: Kazuki YASUMATSU
   *	(kyasu@crl.fujixerox.co.jp, Kazuki.Yasumatsu@fujixerox.co.jp)
***************
*** 14,23 ****
   *       I got sick of version control ages ago; RCS stuff should be removed.
   *
   * $Log: sqXWindow.c,v $
!  * Revision 1.2  1999/02/20 01:10:06  wdc
!  * Feb 9 revision from Ian:
!  * redefinition of ioMicroMSecs, supports an Icon bitmap.
   *
   * Revision 1.5  1997/08/02 01:33:28  piumarta
   * Fixed colourmap initialisation to match new image.
   * (My desktop was YELLOW! ;-)
--- 14,26 ----
   *       I got sick of version control ages ago; RCS stuff should be removed.
   *
   * $Log: sqXWindow.c,v $
!  * Revision 1.4  1999/02/20 01:35:11  wdc
!  * Added in code to use the DefaultVisual if none of the visuals we wanted
!  * were available.
   *
+  * Revision 1.1  1999/02/18 22:49:51  wdc
+  * Initial revision
+  *
   * Revision 1.5  1997/08/02 01:33:28  piumarta
   * Fixed colourmap initialisation to match new image.
   * (My desktop was YELLOW! ;-)
***************
*** 36,42 ****
   */
  
  static char *rcsid=
!   "$Id: sqXWindow.c,v 1.2 1999/02/20 01:10:06 wdc Exp $";
  
  /* enable use of X Shared Memory extensions
     except on HP-UX which (as usual) can't cope */
--- 39,45 ----
   */
  
  static char *rcsid=
!   "$Id: sqXWindow.c,v 1.4 1999/02/20 01:35:11 wdc Exp wdc $";
  
  /* enable use of X Shared Memory extensions
     except on HP-UX which (as usual) can't cope */
***************
*** 75,80 ****
--- 78,112 ----
  # endif
  #endif
  
+ /*** Array of visuals to test for in order of decreasing priority ***/
+ int trialVisuals[13][2] = {
+   { 24, TrueColor },
+   { 24, DirectColor },
+   { 24, StaticColor },
+   { 24, PseudoColor },
+   { 16, TrueColor },
+   { 16, DirectColor },
+   { 16, StaticColor },
+   { 16, PseudoColor },
+   { 8, PseudoColor },
+   { 8, DirectColor },
+   { 8, TrueColor },
+   { 8, StaticColor },
+   { 0, 0 } };
+ 
+ static char *debugVisual (int x)
+ {
+   switch (x)
+     {
+     case 0: return "StaticGray";
+     case 1: return "GrayScale";
+     case 2: return "StaticColor";
+     case 3: return "PseudoColor";
+     case 4: return "TrueColor";
+     case 5: return "DirectColor";
+     default: return "Invalid";
+     }
+ }
  /*** Variables -- Imported from Virtual Machine ***/
  extern unsigned char *memory;
  extern int interruptKeycode;
***************
*** 136,142 ****
  int		 useXshm= 0;		/* 1 if shared memory is in use */
  int		 asyncUpdate= 0;	/* 1 for asynchronous screen updates */
  # endif
! int		 stDepth;
  int		 stBitsPerPixel= 0;
  unsigned int	 stColors[256];
  unsigned int	 stDownGradingColors[256];
--- 168,174 ----
  int		 useXshm= 0;		/* 1 if shared memory is in use */
  int		 asyncUpdate= 0;	/* 1 for asynchronous screen updates */
  # endif
! int		 stDepth= 0;
  int		 stBitsPerPixel= 0;
  unsigned int	 stColors[256];
  unsigned int	 stDownGradingColors[256];
***************
*** 637,644 ****
    int count;
    XPixmapFormatValues *xpv;
  
-   stVisual= DefaultVisual(stDisplay, DefaultScreen(stDisplay));
-   stDepth= DefaultDepth(stDisplay, DefaultScreen(stDisplay));
    xpv= XListPixmapFormats(stDisplay, &count);
  
    if (xpv)
--- 669,674 ----
***************
*** 701,708 ****
    if (stVisual->class == PseudoColor)
      stColormap= XCreateColormap(stDisplay,
  				stWindow,
! 				DefaultVisual(stDisplay,
! 					      DefaultScreen(stDisplay)),
  				AllocAll);
  
  
--- 731,737 ----
    if (stVisual->class == PseudoColor)
      stColormap= XCreateColormap(stDisplay,
  				stWindow,
! 				stVisual,
  				AllocAll);
  
  
***************
*** 803,808 ****
--- 832,842 ----
  
    int right, bottom;
  
+   XSetWindowAttributes attributes;
+   unsigned long valuemask;
+   XVisualInfo viz;
+   int i = 0;
+ 
    stDisplay= XOpenDisplay(displayName);
    if (!stDisplay)
      {
***************
*** 812,817 ****
--- 846,873 ----
  
    stXfd= ConnectionNumber(stDisplay);
  
+   while (trialVisuals [i][0] != 0)
+     {
+       /*      fprintf (stderr, "Trying %d bit %s.\n", trialVisuals[i][0],
+ 	       debugVisual(trialVisuals[i][1])); */
+       if (XMatchVisualInfo (stDisplay, DefaultScreen(stDisplay),
+ 			    trialVisuals[i][0], trialVisuals[i][1],
+ 			    &viz) != 0) break;
+       i++;
+     }
+   
+   if (trialVisuals [i][0] == 0)
+     {
+       fprintf(stderr, "Using default visual.\n");
+       stVisual= DefaultVisual(stDisplay, DefaultScreen(stDisplay));
+       stDepth= DefaultDepth(stDisplay, DefaultScreen(stDisplay));
+     }
+   else
+     {
+       stVisual= viz.visual;
+       stDepth= trialVisuals[i][0];
+     }
+ 
    if (savedWindowSize != 0)
      {
        right=  windowBounds.x + ((unsigned) savedWindowSize >> 16);
***************
*** 836,860 ****
    windowBounds.width= right - windowBounds.x;
    windowBounds.height= bottom - windowBounds.y;
  
!   stWindow= XCreateSimpleWindow(stDisplay,
! 				DefaultRootWindow(stDisplay),
! 				windowBounds.x, windowBounds.y,
! 				windowBounds.width, windowBounds.height,
! 				1,
! 				BlackPixel(stDisplay, DefaultScreen(stDisplay)),
! 				BlackPixel(stDisplay, DefaultScreen(stDisplay)));
  
!   /* accept the interesting events */
!   {
!     XSetWindowAttributes attributes;
  
!     attributes.event_mask= EVENTMASK;
!     attributes.backing_store= Always;
!     XChangeWindowAttributes(stDisplay, stWindow,
! 			    CWEventMask | CWBackingStore,
! 			    &attributes);
!   }
  
    /* set the window title and resource/class names */
    {
      XClassHint *classHints= XAllocClassHint();
--- 892,925 ----
    windowBounds.width= right - windowBounds.x;
    windowBounds.height= bottom - windowBounds.y;
  
!   attributes.border_pixel= BlackPixel(stDisplay, DefaultScreen(stDisplay));
!   attributes.background_pixel= BlackPixel(stDisplay, DefaultScreen(stDisplay));
!   attributes.event_mask= EVENTMASK;
!   attributes.backing_store= Always;
  
!   valuemask= CWEventMask | CWBackingStore | CWBorderPixel | CWBackPixel;
  
!   /* A visual that is not DefaultVisual requires its own color map */
!   /* If visual is PseudoColor, the new color map is made elsewhere */
!   if ((stVisual != DefaultVisual(stDisplay, DefaultScreen(stDisplay))) &&
!      (stVisual->class != PseudoColor))
!    {
!      stColormap= XCreateColormap(stDisplay,
! 				 RootWindow(stDisplay,
! 					    DefaultScreen(stDisplay)),
! 				 stVisual,
! 				 AllocNone);
!      attributes.colormap = stColormap;
!      valuemask |= CWColormap;
!    }
  
+   stWindow= XCreateWindow(stDisplay,
+ 			  RootWindow(stDisplay, DefaultScreen(stDisplay)),
+ 			  windowBounds.x, windowBounds.y,
+ 			  windowBounds.width, windowBounds.height,
+ 			  1, stDepth, InputOutput, stVisual,
+ 			  valuemask, &attributes);
+ 
    /* set the window title and resource/class names */
    {
      XClassHint *classHints= XAllocClassHint();
