src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 14 Dec 2008 04:36:32 +0000
changeset 2870 b801df19835f
parent 2859 99210400e8b9
child 2873 b33e38aaa027
permissions -rw-r--r--
The X11 window and all pixmaps and images share the same visual and depth.
slouken@1950
     1
/*
slouken@1950
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1950
     4
slouken@1950
     5
    This library is free software; you can redistribute it and/or
slouken@1950
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1950
     7
    License as published by the Free Software Foundation; either
slouken@1950
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1950
     9
slouken@1950
    10
    This library is distributed in the hope that it will be useful,
slouken@1950
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1950
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1950
    13
    Lesser General Public License for more details.
slouken@1950
    14
slouken@1950
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1950
    16
    License along with this library; if not, write to the Free Software
slouken@1950
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1950
    18
slouken@1950
    19
    Sam Lantinga
slouken@1950
    20
    slouken@libsdl.org
slouken@1950
    21
*/
slouken@1950
    22
#include "SDL_config.h"
slouken@1950
    23
slouken@1950
    24
#include "SDL_x11video.h"
slouken@1950
    25
slouken@1950
    26
slouken@1950
    27
static int
slouken@1950
    28
get_visualinfo(Display * display, int screen, XVisualInfo * vinfo)
slouken@1950
    29
{
slouken@1950
    30
    const char *visual_id = SDL_getenv("SDL_VIDEO_X11_VISUALID");
slouken@1950
    31
    int use_directcolor = 1;
slouken@1950
    32
    int depth;
slouken@1950
    33
slouken@1950
    34
    /* Look for an exact visual, if requested */
slouken@1950
    35
    if (visual_id) {
slouken@1950
    36
        XVisualInfo *vi, template;
slouken@1950
    37
        int nvis;
slouken@1950
    38
slouken@1950
    39
        SDL_zero(template);
slouken@1950
    40
        template.visualid = SDL_strtol(visual_id, NULL, 0);
slouken@1950
    41
        vi = XGetVisualInfo(display, VisualIDMask, &template, &nvis);
slouken@1950
    42
        if (vi) {
slouken@1950
    43
            *vinfo = *vi;
slouken@1950
    44
            XFree(vi);
slouken@1950
    45
            return 0;
slouken@1950
    46
        }
slouken@1950
    47
    }
slouken@1950
    48
slouken@1950
    49
    depth = DefaultDepth(display, screen);
slouken@1950
    50
    if ((use_directcolor &&
slouken@1950
    51
         XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
slouken@1950
    52
        XMatchVisualInfo(display, screen, depth, TrueColor, vinfo) ||
slouken@1950
    53
        XMatchVisualInfo(display, screen, depth, PseudoColor, vinfo) ||
slouken@1950
    54
        XMatchVisualInfo(display, screen, depth, StaticColor, vinfo)) {
slouken@1950
    55
        return 0;
slouken@1950
    56
    }
slouken@1950
    57
    return -1;
slouken@1950
    58
}
slouken@1950
    59
slouken@2870
    60
static Uint32
slouken@2870
    61
X11_GetPixelFormatFromVisualInfo(Display *display, XVisualInfo *vinfo)
slouken@2870
    62
{
slouken@2870
    63
    if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
slouken@2870
    64
        int bpp;
slouken@2870
    65
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@2870
    66
slouken@2870
    67
        Rmask = vinfo->visual->red_mask;
slouken@2870
    68
        Gmask = vinfo->visual->green_mask;
slouken@2870
    69
        Bmask = vinfo->visual->blue_mask;
slouken@2870
    70
        if (vinfo->depth == 32) {
slouken@2870
    71
            Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
slouken@2870
    72
        } else {
slouken@2870
    73
            Amask = 0;
slouken@2870
    74
        }
slouken@2870
    75
slouken@2870
    76
        bpp = vinfo->depth;
slouken@2870
    77
        if (bpp == 24) {
slouken@2870
    78
            int i, n;
slouken@2870
    79
            XPixmapFormatValues *p = XListPixmapFormats(display, &n);
slouken@2870
    80
            if (p) {
slouken@2870
    81
                for (i = 0; i < n; ++i) {
slouken@2870
    82
                    if (p[i].depth == 24) {
slouken@2870
    83
                        bpp = p[i].bits_per_pixel;
slouken@2870
    84
                        break;
slouken@2870
    85
                    }
slouken@2870
    86
                }
slouken@2870
    87
                XFree(p);
slouken@2870
    88
            }
slouken@2870
    89
        }
slouken@2870
    90
slouken@2870
    91
        return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2870
    92
    }
slouken@2870
    93
slouken@2870
    94
    if (vinfo->class == PseudoColor || vinfo->class == StaticColor) {
slouken@2870
    95
        switch (vinfo->depth) {
slouken@2870
    96
        case 8:
slouken@2870
    97
            return SDL_PIXELTYPE_INDEX8;
slouken@2870
    98
        case 4:
slouken@2870
    99
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   100
                return SDL_PIXELFORMAT_INDEX4LSB;
slouken@2870
   101
            } else {
slouken@2870
   102
                return SDL_PIXELFORMAT_INDEX4MSB;
slouken@2870
   103
            }
slouken@2870
   104
            break;
slouken@2870
   105
        case 1:
slouken@2870
   106
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   107
                return SDL_PIXELFORMAT_INDEX1LSB;
slouken@2870
   108
            } else {
slouken@2870
   109
                return SDL_PIXELFORMAT_INDEX1MSB;
slouken@2870
   110
            }
slouken@2870
   111
            break;
slouken@2870
   112
        }
slouken@2870
   113
    }
slouken@2870
   114
slouken@2870
   115
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@2870
   116
}
slouken@2870
   117
slouken@1950
   118
void
slouken@1950
   119
X11_InitModes(_THIS)
slouken@1950
   120
{
slouken@1950
   121
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1950
   122
    int screen;
slouken@1950
   123
slouken@1950
   124
    for (screen = 0; screen < ScreenCount(data->display); ++screen) {
slouken@1950
   125
        XVisualInfo vinfo;
slouken@1950
   126
        SDL_VideoDisplay display;
slouken@1950
   127
        SDL_DisplayData *displaydata;
slouken@1950
   128
        SDL_DisplayMode mode;
slouken@1950
   129
slouken@1950
   130
        if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@1950
   131
            continue;
slouken@1950
   132
        }
slouken@1950
   133
slouken@2870
   134
        mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
slouken@1950
   135
        mode.w = DisplayWidth(data->display, screen);
slouken@1950
   136
        mode.h = DisplayHeight(data->display, screen);
slouken@1950
   137
        mode.refresh_rate = 0;
slouken@1950
   138
        mode.driverdata = NULL;
slouken@1950
   139
slouken@1950
   140
        displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
slouken@1950
   141
        if (!displaydata) {
slouken@1950
   142
            continue;
slouken@1950
   143
        }
slouken@1950
   144
        displaydata->screen = screen;
slouken@1950
   145
        displaydata->visual = vinfo.visual;
slouken@1951
   146
        displaydata->depth = vinfo.depth;
slouken@1950
   147
slouken@1950
   148
        SDL_zero(display);
slouken@1950
   149
        display.desktop_mode = mode;
slouken@1950
   150
        display.current_mode = mode;
slouken@1950
   151
        display.driverdata = displaydata;
slouken@1950
   152
        SDL_AddVideoDisplay(&display);
slouken@1950
   153
    }
slouken@1950
   154
}
slouken@1950
   155
slouken@1950
   156
void
slouken@1950
   157
X11_GetDisplayModes(_THIS)
slouken@1950
   158
{
slouken@2870
   159
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1950
   160
    SDL_DisplayData *data = (SDL_DisplayData *) SDL_CurrentDisplay.driverdata;
slouken@1950
   161
    SDL_DisplayMode mode;
slouken@2870
   162
slouken@1950
   163
}
slouken@1950
   164
slouken@1950
   165
int
slouken@1950
   166
X11_SetDisplayMode(_THIS, SDL_DisplayMode * mode)
slouken@1950
   167
{
slouken@1950
   168
    //SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata;
slouken@1950
   169
    return -1;
slouken@1950
   170
}
slouken@1950
   171
slouken@1950
   172
void
slouken@1950
   173
X11_QuitModes(_THIS)
slouken@1950
   174
{
slouken@1950
   175
}
slouken@1950
   176
slouken@1950
   177
/* vi: set ts=4 sw=4 expandtab: */