src/video/x11/SDL_x11window.c
changeset 2214 e7164a4dac62
parent 2213 59a667370c57
child 2321 c5feceb0395e
     1.1 --- a/src/video/x11/SDL_x11window.c	Tue Jul 24 18:46:45 2007 +0000
     1.2 +++ b/src/video/x11/SDL_x11window.c	Wed Jul 25 21:22:55 2007 +0000
     1.3 @@ -181,46 +181,92 @@
     1.4      }
     1.5      xattr.background_pixel = 0;
     1.6      xattr.border_pixel = 0;
     1.7 +
     1.8      if (visual->class == DirectColor || visual->class == PseudoColor) {
     1.9          int nmaps;
    1.10 +        XStandardColormap cmap;
    1.11          XStandardColormap *stdmaps;
    1.12 +        XColor *colorcells;
    1.13 +        Colormap colormap;
    1.14          Bool found = False;
    1.15 +        int i;
    1.16 +        int ncolors;
    1.17 +        int rmax, gmax, bmax;
    1.18 +        int rmul, gmul, bmul;
    1.19  
    1.20 -        /* check to see if the colormap we need already exists */
    1.21 -        if (0 != XGetRGBColormaps(data->display,
    1.22 -                                  RootWindow(data->display,
    1.23 -                                             displaydata->screen), &stdmaps,
    1.24 -                                  &nmaps, XA_RGB_BEST_MAP)) {
    1.25 -            int i;
    1.26 -            for (i = 0; i < nmaps; i++) {
    1.27 -                if (stdmaps[i].visualid == visual->visualid) {
    1.28 -                    xattr.colormap = stdmaps[i].colormap;
    1.29 -                    X11_TrackColormap(data->display, displaydata->screen,
    1.30 -                                      &stdmaps[i], visual);
    1.31 -                    found = True;
    1.32 -                    break;
    1.33 +        if (colormap =
    1.34 +            X11_LookupColormap(data->display, displaydata->screen,
    1.35 +                               visual->visualid)) {
    1.36 +            xattr.colormap = colormap;
    1.37 +        } else {
    1.38 +            /* check to see if the colormap we need already exists */
    1.39 +            if (0 != XGetRGBColormaps(data->display,
    1.40 +                                      RootWindow(data->display,
    1.41 +                                                 displaydata->screen),
    1.42 +                                      &stdmaps, &nmaps, XA_RGB_BEST_MAP)) {
    1.43 +                for (i = 0; i < nmaps; i++) {
    1.44 +                    if (stdmaps[i].visualid == visual->visualid) {
    1.45 +                        SDL_memcpy(&cmap, &stdmaps[i],
    1.46 +                                   sizeof(XStandardColormap));
    1.47 +                        found = True;
    1.48 +                        break;
    1.49 +                    }
    1.50                  }
    1.51 +                XFree(stdmaps);
    1.52              }
    1.53 -            XFree(stdmaps);
    1.54 -        }
    1.55  
    1.56 -        /* it doesn't exist, so create it */
    1.57 -        if (!found) {
    1.58 -            int max = visual->map_entries - 1;
    1.59 -            XStandardColormap *cmap =
    1.60 -                XmuStandardColormap(data->display, displaydata->screen,
    1.61 -                                    visual->visualid, depth,
    1.62 -                                    XA_RGB_BEST_MAP, None,
    1.63 -                                    max, max, max);
    1.64 -            if (NULL != cmap && cmap->visualid == visual->visualid) {
    1.65 -                xattr.colormap = cmap->colormap;
    1.66 -                X11_TrackColormap(data->display, displaydata->screen, cmap,
    1.67 -                                  visual);
    1.68 -            } else {
    1.69 -                SDL_SetError
    1.70 -                    ("Couldn't create window:XA_RGB_BEST_MAP not found");
    1.71 +            /* it doesn't exist, so create it */
    1.72 +            if (!found) {
    1.73 +                int max = visual->map_entries - 1;
    1.74 +                stdmaps =
    1.75 +                    XmuStandardColormap(data->display, displaydata->screen,
    1.76 +                                        visual->visualid, depth,
    1.77 +                                        XA_RGB_BEST_MAP, None, max, max, max);
    1.78 +                if (NULL == stdmaps || stdmaps->visualid != visual->visualid) {
    1.79 +                    SDL_SetError
    1.80 +                        ("Couldn't create window:XA_RGB_BEST_MAP not found and could not be created");
    1.81 +                    return -1;
    1.82 +                }
    1.83 +                SDL_memcpy(&cmap, stdmaps, sizeof(XStandardColormap));
    1.84 +            }
    1.85 +
    1.86 +            /* OK, we have the best color map, now copy it for use by the
    1.87 +               program */
    1.88 +
    1.89 +            colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
    1.90 +            if (NULL == colorcells) {
    1.91 +                SDL_SetError("out of memory in X11_CreateWindow");
    1.92                  return -1;
    1.93              }
    1.94 +            ncolors = visual->map_entries;
    1.95 +            rmax = cmap.red_max + 1;
    1.96 +            gmax = cmap.blue_max + 1;
    1.97 +            bmax = cmap.green_max + 1;
    1.98 +
    1.99 +            rmul = cmap.red_mult;
   1.100 +            gmul = cmap.blue_mult;
   1.101 +            bmul = cmap.green_mult;
   1.102 +
   1.103 +            /* build the color table pixel values */
   1.104 +            for (i = 0; i < ncolors; i++) {
   1.105 +                Uint32 red = (rmax * i) / ncolors;
   1.106 +                Uint32 green = (gmax * i) / ncolors;
   1.107 +                Uint32 blue = (bmax * i) / ncolors;
   1.108 +
   1.109 +                colorcells[i].pixel =
   1.110 +                    (red * rmul) | (green * gmul) | (blue * bmul);
   1.111 +            }
   1.112 +            XQueryColors(data->display, cmap.colormap, colorcells, ncolors);
   1.113 +            colormap = XCreateColormap(data->display,
   1.114 +                                       RootWindow(data->display,
   1.115 +                                                  displaydata->screen),
   1.116 +                                       visual, AllocAll);
   1.117 +            XStoreColors(data->display, colormap, colorcells, ncolors);
   1.118 +            SDL_free(colorcells);
   1.119 +
   1.120 +            xattr.colormap = colormap;
   1.121 +            X11_TrackColormap(data->display, displaydata->screen, colormap,
   1.122 +                              &cmap, visual);
   1.123          }
   1.124      } else {
   1.125          xattr.colormap =