src/video/x11/SDL_x11window.c
changeset 5466 006883d5fa51
parent 5386 762e67f81fc7
child 5478 6b65ff3cee62
     1.1 --- a/src/video/x11/SDL_x11window.c	Thu Mar 10 01:03:43 2011 -0800
     1.2 +++ b/src/video/x11/SDL_x11window.c	Fri Mar 11 08:49:20 2011 -0800
     1.3 @@ -175,6 +175,7 @@
     1.4              window->flags &= ~SDL_WINDOW_SHOWN;
     1.5          }
     1.6          data->visual = attrib.visual;
     1.7 +        data->colormap = attrib.colormap;
     1.8      }
     1.9  
    1.10      {
    1.11 @@ -316,7 +317,88 @@
    1.12      xattr.override_redirect = False;
    1.13      xattr.background_pixel = 0;
    1.14      xattr.border_pixel = 0;
    1.15 -    xattr.colormap = XCreateColormap(display, RootWindow(display, screen), visual, AllocNone);
    1.16 +
    1.17 +    if (visual->class == DirectColor) {
    1.18 +        Status status;
    1.19 +        XColor *colorcells;
    1.20 +        int i;
    1.21 +        int ncolors;
    1.22 +        int rmax, gmax, bmax;
    1.23 +        int rmask, gmask, bmask;
    1.24 +        int rshift, gshift, bshift;
    1.25 +
    1.26 +        xattr.colormap =
    1.27 +            XCreateColormap(display, RootWindow(display, screen),
    1.28 +                            visual, AllocAll);
    1.29 +
    1.30 +        /* If we can't create a colormap, then we must die */
    1.31 +        if (!xattr.colormap) {
    1.32 +            SDL_SetError("Could not create writable colormap");
    1.33 +            return -1;
    1.34 +        }
    1.35 +
    1.36 +        /* OK, we got a colormap, now fill it in as best as we can */
    1.37 +        colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
    1.38 +        if (!colorcells) {
    1.39 +            SDL_OutOfMemory();
    1.40 +            return -1;
    1.41 +        }
    1.42 +        ncolors = visual->map_entries;
    1.43 +        rmax = 0xffff;
    1.44 +        gmax = 0xffff;
    1.45 +        bmax = 0xffff;
    1.46 +
    1.47 +        rshift = 0;
    1.48 +        rmask = visual->red_mask;
    1.49 +        while (0 == (rmask & 1)) {
    1.50 +            rshift++;
    1.51 +            rmask >>= 1;
    1.52 +        }
    1.53 +
    1.54 +        gshift = 0;
    1.55 +        gmask = visual->green_mask;
    1.56 +        while (0 == (gmask & 1)) {
    1.57 +            gshift++;
    1.58 +            gmask >>= 1;
    1.59 +        }
    1.60 +
    1.61 +        bshift = 0;
    1.62 +        bmask = visual->blue_mask;
    1.63 +        while (0 == (bmask & 1)) {
    1.64 +            bshift++;
    1.65 +            bmask >>= 1;
    1.66 +        }
    1.67 +
    1.68 +        /* build the color table pixel values */
    1.69 +        for (i = 0; i < ncolors; i++) {
    1.70 +            Uint32 red = (rmax * i) / (ncolors - 1);
    1.71 +            Uint32 green = (gmax * i) / (ncolors - 1);
    1.72 +            Uint32 blue = (bmax * i) / (ncolors - 1);
    1.73 +
    1.74 +            Uint32 rbits = (rmask * i) / (ncolors - 1);
    1.75 +            Uint32 gbits = (gmask * i) / (ncolors - 1);
    1.76 +            Uint32 bbits = (bmask * i) / (ncolors - 1);
    1.77 +
    1.78 +            Uint32 pix =
    1.79 +                (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
    1.80 +
    1.81 +            colorcells[i].pixel = pix;
    1.82 +
    1.83 +            colorcells[i].red = red;
    1.84 +            colorcells[i].green = green;
    1.85 +            colorcells[i].blue = blue;
    1.86 +
    1.87 +            colorcells[i].flags = DoRed | DoGreen | DoBlue;
    1.88 +        }
    1.89 +
    1.90 +        XStoreColors(display, xattr.colormap, colorcells, ncolors);
    1.91 +
    1.92 +        SDL_free(colorcells);
    1.93 +    } else {
    1.94 +        xattr.colormap =
    1.95 +            XCreateColormap(display, RootWindow(display, screen),
    1.96 +                            visual, AllocNone);
    1.97 +    }
    1.98  
    1.99      w = XCreateWindow(display, RootWindow(display, screen),
   1.100                        window->x, window->y, window->w, window->h,
   1.101 @@ -861,6 +943,75 @@
   1.102      XFlush(display);
   1.103  }
   1.104  
   1.105 +int
   1.106 +X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
   1.107 +{
   1.108 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   1.109 +    Display *display = data->videodata->display;
   1.110 +    Visual *visual = data->visual;
   1.111 +    Colormap colormap = data->colormap;
   1.112 +    XColor *colorcells;
   1.113 +    int ncolors;
   1.114 +    int rmask, gmask, bmask;
   1.115 +    int rshift, gshift, bshift;
   1.116 +    int i, j;
   1.117 +
   1.118 +    if (visual->class != DirectColor) {
   1.119 +        SDL_SetError("Window doesn't have DirectColor visual");
   1.120 +        return -1;
   1.121 +    }
   1.122 +
   1.123 +    ncolors = visual->map_entries;
   1.124 +    colorcells = SDL_malloc(ncolors * sizeof(XColor));
   1.125 +    if (!colorcells) {
   1.126 +        SDL_OutOfMemory();
   1.127 +        return -1;
   1.128 +    }
   1.129 +
   1.130 +    rshift = 0;
   1.131 +    rmask = visual->red_mask;
   1.132 +    while (0 == (rmask & 1)) {
   1.133 +        rshift++;
   1.134 +        rmask >>= 1;
   1.135 +    }
   1.136 +
   1.137 +    gshift = 0;
   1.138 +    gmask = visual->green_mask;
   1.139 +    while (0 == (gmask & 1)) {
   1.140 +        gshift++;
   1.141 +        gmask >>= 1;
   1.142 +    }
   1.143 +
   1.144 +    bshift = 0;
   1.145 +    bmask = visual->blue_mask;
   1.146 +    while (0 == (bmask & 1)) {
   1.147 +        bshift++;
   1.148 +        bmask >>= 1;
   1.149 +    }
   1.150 +
   1.151 +    /* build the color table pixel values */
   1.152 +    for (i = 0; i < ncolors; i++) {
   1.153 +        Uint32 rbits = (rmask * i) / (ncolors - 1);
   1.154 +        Uint32 gbits = (gmask * i) / (ncolors - 1);
   1.155 +        Uint32 bbits = (bmask * i) / (ncolors - 1);
   1.156 +        Uint32 pix = (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
   1.157 +
   1.158 +        colorcells[i].pixel = pix;
   1.159 +
   1.160 +        colorcells[i].red = ramp[(0 * 256) + i];
   1.161 +        colorcells[i].green = ramp[(1 * 256) + i];
   1.162 +        colorcells[i].blue = ramp[(2 * 256) + i];
   1.163 +
   1.164 +        colorcells[i].flags = DoRed | DoGreen | DoBlue;
   1.165 +    }
   1.166 +
   1.167 +    XStoreColors(display, colormap, colorcells, ncolors);
   1.168 +    XFlush(display);
   1.169 +    SDL_free(colorcells);
   1.170 +
   1.171 +    return 0;
   1.172 +}
   1.173 +
   1.174  void
   1.175  X11_SetWindowGrab(_THIS, SDL_Window * window)
   1.176  {