src/video/x11/SDL_x11gamma.c
author Bob Pendleton <bob@pendleton.com>
Fri, 07 Mar 2008 20:54:11 +0000
changeset 2325 c7bcf84ba1b9
parent 2221 1d75c38e1e5c
child 2859 99210400e8b9
permissions -rw-r--r--
Next version of internationalized input for X11. On my machine (famous last words :-) with a US English keyboard and locale I can compose ` and e and get a text
input event with the character รจ. You still get the keypress keyrelease events for the individual keys that go into composing the character.
bob@2162
     1
/*
bob@2162
     2
    SDL - Simple DirectMedia Layer
bob@2162
     3
    Copyright (C) 1997-2006 Sam Lantinga
bob@2162
     4
bob@2162
     5
    This library is free software; you can redistribute it and/or
bob@2162
     6
    modify it under the terms of the GNU Lesser General Public
bob@2162
     7
    License as published by the Free Software Foundation; either
bob@2162
     8
    version 2.1 of the License, or (at your option) any later version.
bob@2162
     9
bob@2162
    10
    This library is distributed in the hope that it will be useful,
bob@2162
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bob@2162
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bob@2162
    13
    Lesser General Public License for more details.
bob@2162
    14
bob@2162
    15
    You should have received a copy of the GNU Lesser General Public
bob@2162
    16
    License along with this library; if not, write to the Free Software
bob@2162
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bob@2162
    18
bob@2162
    19
    Sam Lantinga
bob@2162
    20
    slouken@libsdl.org
bob@2162
    21
*/
bob@2162
    22
#include "SDL_config.h"
bob@2162
    23
#include "../SDL_sysvideo.h"
bob@2162
    24
#include "SDL_x11video.h"
bob@2162
    25
bob@2213
    26
static int numCmaps = 0;
bob@2213
    27
bob@2213
    28
typedef struct
bob@2213
    29
{
bob@2213
    30
    Display *display;
bob@2213
    31
    int scrNum;
bob@2214
    32
    Colormap colormap;
bob@2213
    33
    XStandardColormap cmap;
bob@2213
    34
    Visual visual;
bob@2213
    35
} cmapTableEntry;
bob@2213
    36
bob@2213
    37
cmapTableEntry *cmapTable = NULL;
bob@2213
    38
bob@2214
    39
/* To reduce the overhead as much as possible lets do as little as
bob@2214
    40
   possible. When we do have to create a colormap keep track of it and
bob@2214
    41
   reuse it. We're going to do this for both DirectColor and
bob@2214
    42
   PseudoColor colormaps. */
bob@2214
    43
bob@2214
    44
Colormap
bob@2214
    45
X11_LookupColormap(Display * display, int scrNum, VisualID vid)
bob@2214
    46
{
bob@2214
    47
    int i;
bob@2214
    48
bob@2214
    49
    for (i = 0; i < numCmaps; i++) {
bob@2214
    50
        if (cmapTable[i].display == display &&
bob@2214
    51
            cmapTable[i].scrNum == scrNum &&
bob@2214
    52
            cmapTable[i].cmap.visualid == vid) {
bob@2214
    53
            return cmapTable[i].cmap.colormap;
bob@2214
    54
        }
bob@2214
    55
    }
bob@2214
    56
bob@2214
    57
    return 0;
bob@2214
    58
}
bob@2214
    59
bob@2214
    60
bob@2213
    61
void
bob@2214
    62
X11_TrackColormap(Display * display, int scrNum, Colormap colormap,
bob@2213
    63
                  XStandardColormap * cmap, Visual * visual)
bob@2213
    64
{
bob@2213
    65
    int i;
bob@2213
    66
bob@2214
    67
    /* search the table to find out if we already have this one. We
bob@2214
    68
       only want one entry for each display, screen number, visualid,
bob@2214
    69
       and colormap combination */
bob@2213
    70
    for (i = 0; i < numCmaps; i++) {
bob@2213
    71
        if (cmapTable[i].display == display &&
bob@2213
    72
            cmapTable[i].scrNum == scrNum &&
bob@2214
    73
            cmapTable[i].cmap.visualid == cmap->visualid &&
bob@2214
    74
            cmapTable[i].cmap.colormap == colormap) {
bob@2213
    75
            return;
bob@2213
    76
        }
bob@2213
    77
    }
bob@2213
    78
bob@2213
    79
    /* increase the table by one entry. If the table is NULL create the
bob@2213
    80
       first entrty */
slouken@2221
    81
    cmapTable =
slouken@2221
    82
        SDL_realloc(cmapTable, (numCmaps + 1) * sizeof(cmapTableEntry));
bob@2216
    83
    if (NULL == cmapTable) {
bob@2213
    84
        SDL_SetError("Out of memory in X11_TrackColormap()");
bob@2213
    85
        return;
bob@2213
    86
    }
bob@2213
    87
bob@2213
    88
    cmapTable[numCmaps].display = display;
bob@2213
    89
    cmapTable[numCmaps].scrNum = scrNum;
bob@2214
    90
    cmapTable[numCmaps].colormap = colormap;
bob@2213
    91
    SDL_memcpy(&cmapTable[numCmaps].cmap, cmap, sizeof(XStandardColormap));
bob@2213
    92
    SDL_memcpy(&cmapTable[numCmaps].visual, visual, sizeof(Visual));
bob@2213
    93
bob@2213
    94
    numCmaps++;
bob@2213
    95
}
bob@2213
    96
bob@2214
    97
/* The problem is that you have to have at least one DirectColor
bob@2214
    98
   colormap before you can set the gamma ramps or read the gamma
bob@2214
    99
   ramps. If the application has created a DirectColor window then the
bob@2214
   100
   cmapTable will have at least one colormap in it and everything is
bob@2214
   101
   cool. If not, then we just fail  */
bob@2214
   102
bob@2162
   103
int
bob@2162
   104
X11_SetDisplayGammaRamp(_THIS, Uint16 * ramp)
bob@2162
   105
{
bob@2214
   106
    Display *display;
bob@2214
   107
    Colormap colormap;
bob@2214
   108
    XColor *colorcells;
bob@2214
   109
    int ncolors;
bob@2214
   110
    int i;
bob@2214
   111
    int j;
bob@2214
   112
bob@2214
   113
    int rmax, gmax, bmax;
bob@2214
   114
    int rmul, gmul, bmul;
bob@2214
   115
bob@2214
   116
    for (j = 0; j < numCmaps; j++) {
bob@2214
   117
        if (cmapTable[j].visual.class == DirectColor) {
bob@2214
   118
            display = cmapTable[j].display;
bob@2214
   119
            colormap = cmapTable[j].colormap;
bob@2214
   120
            ncolors = cmapTable[j].visual.map_entries;
bob@2214
   121
bob@2214
   122
            colorcells = SDL_malloc(ncolors * sizeof(XColor));
bob@2214
   123
            if (NULL == colorcells) {
bob@2214
   124
                SDL_SetError("out of memory in X11_SetDisplayGammaRamp");
bob@2214
   125
                return -1;
bob@2214
   126
            }
bob@2214
   127
bob@2214
   128
            rmax = cmapTable[j].cmap.red_max + 1;
bob@2214
   129
            gmax = cmapTable[j].cmap.blue_max + 1;
bob@2214
   130
            bmax = cmapTable[j].cmap.green_max + 1;
bob@2214
   131
bob@2214
   132
            rmul = cmapTable[j].cmap.red_mult;
bob@2214
   133
            gmul = cmapTable[j].cmap.blue_mult;
bob@2214
   134
            bmul = cmapTable[j].cmap.green_mult;
bob@2214
   135
bob@2214
   136
            /* build the color table pixel values */
bob@2214
   137
            for (i = 0; i < ncolors; i++) {
bob@2214
   138
                Uint32 red = (rmax * i) / ncolors;
bob@2214
   139
                Uint32 green = (gmax * i) / ncolors;
bob@2214
   140
                Uint32 blue = (bmax * i) / ncolors;
bob@2214
   141
bob@2214
   142
                colorcells[i].pixel =
bob@2214
   143
                    (red * rmul) | (green * gmul) | (blue * bmul);
bob@2214
   144
                colorcells[i].flags = DoRed | DoGreen | DoBlue;
bob@2214
   145
bob@2214
   146
                colorcells[i].red = ramp[(0 * 256) + i];
bob@2214
   147
                colorcells[i].green = ramp[(1 * 256) + i];
bob@2214
   148
                colorcells[i].blue = ramp[(2 * 256) + i];
bob@2214
   149
            }
bob@2214
   150
bob@2214
   151
            XStoreColors(display, colormap, colorcells, ncolors);
bob@2214
   152
            XFlush(display);
bob@2214
   153
            SDL_free(colorcells);
bob@2214
   154
        }
bob@2214
   155
    }
bob@2214
   156
bob@2214
   157
    return 0;
bob@2162
   158
}
bob@2162
   159
bob@2162
   160
int
bob@2162
   161
X11_GetDisplayGammaRamp(_THIS, Uint16 * ramp)
bob@2162
   162
{
bob@2214
   163
    Display *display;
bob@2214
   164
    Colormap colormap;
bob@2214
   165
    XColor *colorcells;
bob@2214
   166
    int ncolors;
bob@2214
   167
    int dc;
bob@2214
   168
    int i;
bob@2214
   169
bob@2214
   170
    int rmax, gmax, bmax;
bob@2214
   171
    int rmul, gmul, bmul;
bob@2214
   172
bob@2214
   173
    /* find the first DirectColor colormap and use it to get the gamma
bob@2214
   174
       ramp */
bob@2214
   175
bob@2214
   176
    dc = -1;
bob@2214
   177
    for (i = 0; i < numCmaps; i++) {
bob@2214
   178
        if (cmapTable[i].visual.class == DirectColor) {
bob@2214
   179
            dc = i;
bob@2214
   180
            break;
bob@2214
   181
        }
bob@2214
   182
    }
bob@2214
   183
bob@2214
   184
    if (dc < 0) {
bob@2214
   185
        return -1;
bob@2214
   186
    }
bob@2214
   187
bob@2214
   188
    /* there is at least one DirectColor colormap in the cmapTable,
bob@2214
   189
       let's just get the entries from that colormap */
bob@2214
   190
bob@2214
   191
    display = cmapTable[dc].display;
bob@2214
   192
    colormap = cmapTable[dc].colormap;
bob@2214
   193
    ncolors = cmapTable[dc].visual.map_entries;
bob@2214
   194
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
bob@2214
   195
    if (NULL == colorcells) {
bob@2214
   196
        SDL_SetError("out of memory in X11_GetDisplayGammaRamp");
bob@2214
   197
        return -1;
bob@2214
   198
    }
bob@2214
   199
bob@2214
   200
    rmax = cmapTable[dc].cmap.red_max + 1;
bob@2214
   201
    gmax = cmapTable[dc].cmap.blue_max + 1;
bob@2214
   202
    bmax = cmapTable[dc].cmap.green_max + 1;
bob@2214
   203
bob@2214
   204
    rmul = cmapTable[dc].cmap.red_mult;
bob@2214
   205
    gmul = cmapTable[dc].cmap.blue_mult;
bob@2214
   206
    bmul = cmapTable[dc].cmap.green_mult;
bob@2214
   207
bob@2214
   208
    /* build the color table pixel values */
bob@2214
   209
    for (i = 0; i < ncolors; i++) {
bob@2214
   210
        Uint32 red = (rmax * i) / ncolors;
bob@2214
   211
        Uint32 green = (gmax * i) / ncolors;
bob@2214
   212
        Uint32 blue = (bmax * i) / ncolors;
bob@2214
   213
bob@2214
   214
        colorcells[i].pixel = (red * rmul) | (green * gmul) | (blue * bmul);
bob@2214
   215
    }
bob@2214
   216
bob@2214
   217
    XQueryColors(display, colormap, colorcells, ncolors);
bob@2214
   218
bob@2214
   219
    /* prepare the values to be returned. Note that SDL assumes that
bob@2214
   220
       gamma ramps are always 3 * 256 entries long with the red entries
bob@2214
   221
       in the first 256 elements, the green in the second 256 elements
bob@2214
   222
       and the blue in the last 256 elements */
bob@2214
   223
bob@2214
   224
    for (i = 0; i < ncolors; i++) {
bob@2214
   225
        ramp[(0 * 256) + i] = colorcells[i].red;
bob@2214
   226
        ramp[(1 * 256) + i] = colorcells[i].green;
bob@2214
   227
        ramp[(2 * 256) + i] = colorcells[i].blue;
bob@2214
   228
    }
bob@2214
   229
bob@2214
   230
    SDL_free(colorcells);
bob@2214
   231
    return 0;
bob@2162
   232
}