[1678] in Release_7.7_team

home help back first fref pref prev next nref lref last post

Re: Creating a non-default visual CODE!

daemon@ATHENA.MIT.EDU (Bill Cattey)
Tue Feb 23 20:20:33 1999

Date: Tue, 23 Feb 1999 20:20:27 -0500 (EST)
From: Bill Cattey <wdc@MIT.EDU>
To: f_l@MIT.EDU, release-team@MIT.EDU, owls@MIT.EDU
In-Reply-To: <QqkqSs4GgE6e0rUV00@mit.edu>

I have successfuly modified an application to ask for a visual other
than the default.  Regrettably it is more than "6 lines of code".  In
fact, doing this sort of thing is so unusual, there were aspects that
Craig Fields could not advise me upon!  So it's an unusual thing, and
we'd save the vendors a lot of pain if we supplied suggested code.

This code does the following new things:

Creates an array trialVisuals naming depths and color classes to try.
Adds a debug routine to print the color classes by name.
Re-arranges code to set window attributes before creation instead of after.
Tries to get a visual matching an entry in the trialVisuals array.
If that fails, gets the default visual.
Sets stVisual and stDepth with the appropriate depth and visual.
Changes a call of XCreateSimpleWindow to a call to XCreateWindow.

To set up the call to XCreateWindow there is logic to determine if 
we are not using the default visual.  If the visual is not the default
we must create a color map.  The code presented assumes that the
PseudoColor class of visual already has code elsewhere in the
application to create a color map.  (Your application probably does
this.)

Note:  If you use a visual other than the default, and if that visual
has a different depth than the default, you must set a color map, and
the background pixel, and the border pixel.  Otherwise XCreateWindow
tries to inherit colormap, background pixmap, and border pixmap values
from the parent window (usually the root, created with the default
visual) and the application blows out with a Bad Match error.

Below are diffs from my application.  The application is tested.

-wdc

*** /tmp/T0ekIZa_	Fri Feb 19 20:37:48 1999
--- sqXWindow.c	Fri Feb 19 20:35:39 1999
***************
*** 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;
***************
*** 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();

home help back first fref pref prev next nref lref last post