src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 08:49:20 -0800
changeset 5466 006883d5fa51
parent 5408 35a63789c3f2
child 5481 22dfc3958dc3
permissions -rw-r--r--
Gamma support is back!

New API functions:
SDL_SetWindowBrightness()
SDL_GetWindowBrightness()
SDL_SetWindowGammaRamp()
SDL_GetWindowGammaRamp()
SDL_CalculateGammaRamp()
slouken@1950
     1
/*
slouken@1950
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@4519
    26
/*#define X11MODES_DEBUG*/
slouken@1950
    27
slouken@1950
    28
static int
slouken@1950
    29
get_visualinfo(Display * display, int screen, XVisualInfo * vinfo)
slouken@1950
    30
{
slouken@1950
    31
    const char *visual_id = SDL_getenv("SDL_VIDEO_X11_VISUALID");
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@5466
    50
    if ((X11_UseDirectColorVisuals() &&
slouken@5466
    51
         XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
slouken@5466
    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@5182
    60
int
slouken@5182
    61
X11_GetVisualInfoFromVisual(Display * display, Visual * visual, XVisualInfo * vinfo)
slouken@5182
    62
{
slouken@5182
    63
    XVisualInfo *vi;
slouken@5182
    64
    int nvis;
slouken@5182
    65
slouken@5182
    66
    vinfo->visualid = XVisualIDFromVisual(visual);
slouken@5182
    67
    vi = XGetVisualInfo(display, VisualIDMask, vinfo, &nvis);
slouken@5182
    68
    if (vi) {
slouken@5182
    69
        *vinfo = *vi;
slouken@5182
    70
        XFree(vi);
slouken@5182
    71
        return 0;
slouken@5182
    72
    }
slouken@5182
    73
    return -1;
slouken@5182
    74
}
slouken@5182
    75
slouken@5182
    76
Uint32
slouken@2874
    77
X11_GetPixelFormatFromVisualInfo(Display * display, XVisualInfo * vinfo)
slouken@2870
    78
{
slouken@2870
    79
    if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
slouken@2870
    80
        int bpp;
slouken@2870
    81
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@2870
    82
slouken@2870
    83
        Rmask = vinfo->visual->red_mask;
slouken@2870
    84
        Gmask = vinfo->visual->green_mask;
slouken@2870
    85
        Bmask = vinfo->visual->blue_mask;
slouken@2870
    86
        if (vinfo->depth == 32) {
slouken@2870
    87
            Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
slouken@2870
    88
        } else {
slouken@2870
    89
            Amask = 0;
slouken@2870
    90
        }
slouken@2870
    91
slouken@2870
    92
        bpp = vinfo->depth;
slouken@2870
    93
        if (bpp == 24) {
slouken@2870
    94
            int i, n;
slouken@2870
    95
            XPixmapFormatValues *p = XListPixmapFormats(display, &n);
slouken@2870
    96
            if (p) {
slouken@2870
    97
                for (i = 0; i < n; ++i) {
slouken@2870
    98
                    if (p[i].depth == 24) {
slouken@2870
    99
                        bpp = p[i].bits_per_pixel;
slouken@2870
   100
                        break;
slouken@2870
   101
                    }
slouken@2870
   102
                }
slouken@2870
   103
                XFree(p);
slouken@2870
   104
            }
slouken@2870
   105
        }
slouken@2870
   106
slouken@2870
   107
        return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2870
   108
    }
slouken@2870
   109
slouken@2870
   110
    if (vinfo->class == PseudoColor || vinfo->class == StaticColor) {
slouken@2870
   111
        switch (vinfo->depth) {
slouken@2870
   112
        case 8:
slouken@2870
   113
            return SDL_PIXELTYPE_INDEX8;
slouken@2870
   114
        case 4:
slouken@2870
   115
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   116
                return SDL_PIXELFORMAT_INDEX4LSB;
slouken@2870
   117
            } else {
slouken@2870
   118
                return SDL_PIXELFORMAT_INDEX4MSB;
slouken@2870
   119
            }
slouken@2870
   120
            break;
slouken@2870
   121
        case 1:
slouken@2870
   122
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   123
                return SDL_PIXELFORMAT_INDEX1LSB;
slouken@2870
   124
            } else {
slouken@2870
   125
                return SDL_PIXELFORMAT_INDEX1MSB;
slouken@2870
   126
            }
slouken@2870
   127
            break;
slouken@2870
   128
        }
slouken@2870
   129
    }
slouken@2870
   130
slouken@2870
   131
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@2870
   132
}
slouken@2870
   133
slouken@3521
   134
int
slouken@1950
   135
X11_InitModes(_THIS)
slouken@1950
   136
{
slouken@1950
   137
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1950
   138
    int screen;
slouken@1950
   139
slouken@1950
   140
    for (screen = 0; screen < ScreenCount(data->display); ++screen) {
slouken@1950
   141
        XVisualInfo vinfo;
slouken@1950
   142
        SDL_VideoDisplay display;
slouken@1950
   143
        SDL_DisplayData *displaydata;
slouken@1950
   144
        SDL_DisplayMode mode;
slouken@2916
   145
        XPixmapFormatValues *pixmapFormats;
slouken@2916
   146
        int i, n;
slouken@1950
   147
slouken@1950
   148
        if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@1950
   149
            continue;
slouken@1950
   150
        }
slouken@1950
   151
slouken@2870
   152
        mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
slouken@5149
   153
        if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
slouken@5149
   154
            /* We don't support palettized modes now */
slouken@5149
   155
            continue;
slouken@5149
   156
        }
slouken@1950
   157
        mode.w = DisplayWidth(data->display, screen);
slouken@1950
   158
        mode.h = DisplayHeight(data->display, screen);
slouken@1950
   159
        mode.refresh_rate = 0;
slouken@1950
   160
        mode.driverdata = NULL;
slouken@1950
   161
slouken@1950
   162
        displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
slouken@1950
   163
        if (!displaydata) {
slouken@1950
   164
            continue;
slouken@1950
   165
        }
slouken@1950
   166
        displaydata->screen = screen;
slouken@1950
   167
        displaydata->visual = vinfo.visual;
slouken@1951
   168
        displaydata->depth = vinfo.depth;
slouken@1950
   169
slouken@2920
   170
        displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
slouken@2916
   171
        pixmapFormats = XListPixmapFormats(data->display, &n);
slouken@2916
   172
        if (pixmapFormats) {
slouken@2916
   173
            for (i = 0; i < n; ++i) {
slouken@2916
   174
                if (pixmapFormats[i].depth == displaydata->depth) {
slouken@2916
   175
                    displaydata->scanline_pad = pixmapFormats[i].scanline_pad;
slouken@2916
   176
                    break;
slouken@2916
   177
                }
slouken@2916
   178
            }
slouken@2916
   179
            XFree(pixmapFormats);
slouken@2916
   180
        }
slouken@2916
   181
slouken@1950
   182
        SDL_zero(display);
slouken@1950
   183
        display.desktop_mode = mode;
slouken@1950
   184
        display.current_mode = mode;
slouken@1950
   185
        display.driverdata = displaydata;
slouken@1950
   186
        SDL_AddVideoDisplay(&display);
slouken@1950
   187
    }
slouken@3521
   188
    if (_this->num_displays == 0) {
slouken@3521
   189
        SDL_SetError("No available displays");
slouken@3521
   190
        return -1;
slouken@3521
   191
    }
slouken@3521
   192
    return 0;
slouken@1950
   193
}
slouken@1950
   194
slouken@2873
   195
/* Global for the error handler */
slouken@2873
   196
int vm_event, vm_error = -1;
slouken@2873
   197
slouken@2873
   198
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   199
static SDL_bool
slouken@2874
   200
CheckXinerama(Display * display, int *major, int *minor)
slouken@2873
   201
{
slouken@2873
   202
    const char *env;
slouken@2873
   203
slouken@2873
   204
    /* Default the extension not available */
slouken@2873
   205
    *major = *minor = 0;
slouken@2873
   206
slouken@2873
   207
    /* Allow environment override */
slouken@2873
   208
    env = getenv("SDL_VIDEO_X11_XINERAMA");
slouken@2873
   209
    if (env && !SDL_atoi(env)) {
slouken@2873
   210
        return SDL_FALSE;
slouken@2873
   211
    }
slouken@2873
   212
slouken@5408
   213
    if (!SDL_X11_HAVE_XINERAMA) {
slouken@5408
   214
        return SDL_FALSE;
slouken@5408
   215
    }
slouken@5408
   216
slouken@2873
   217
    /* Query the extension version */
slouken@5408
   218
    if (!XineramaQueryExtension(display, major, minor) ||
slouken@5408
   219
        !XineramaIsActive(display)) {
slouken@2873
   220
        return SDL_FALSE;
slouken@2873
   221
    }
slouken@2873
   222
    return SDL_TRUE;
slouken@2873
   223
}
slouken@2873
   224
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   225
slouken@2873
   226
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   227
static SDL_bool
slouken@2874
   228
CheckXRandR(Display * display, int *major, int *minor)
slouken@2873
   229
{
slouken@2873
   230
    const char *env;
slouken@2873
   231
slouken@2873
   232
    /* Default the extension not available */
slouken@2873
   233
    *major = *minor = 0;
slouken@2873
   234
slouken@2873
   235
    /* Allow environment override */
slouken@2873
   236
    env = getenv("SDL_VIDEO_X11_XRANDR");
slouken@2873
   237
    if (env && !SDL_atoi(env)) {
slouken@2873
   238
        return SDL_FALSE;
slouken@2873
   239
    }
slouken@2873
   240
slouken@2873
   241
    if (!SDL_X11_HAVE_XRANDR) {
slouken@2873
   242
        return SDL_FALSE;
slouken@2873
   243
    }
slouken@2873
   244
slouken@2873
   245
    /* Query the extension version */
slouken@2873
   246
    if (!XRRQueryVersion(display, major, minor)) {
slouken@2873
   247
        return SDL_FALSE;
slouken@2873
   248
    }
slouken@2873
   249
    return SDL_TRUE;
slouken@2873
   250
}
slouken@2873
   251
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   252
slouken@5408
   253
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   254
static SDL_bool
slouken@2874
   255
CheckVidMode(Display * display, int *major, int *minor)
slouken@2873
   256
{
slouken@2873
   257
    const char *env;
slouken@2873
   258
slouken@2873
   259
    /* Default the extension not available */
slouken@2873
   260
    *major = *minor = 0;
slouken@2873
   261
slouken@2873
   262
    /* Allow environment override */
slouken@5408
   263
    env = getenv("SDL_VIDEO_X11_XVIDMODE");
slouken@2873
   264
    if (env && !SDL_atoi(env)) {
slouken@2873
   265
        return SDL_FALSE;
slouken@2873
   266
    }
slouken@2874
   267
slouken@5408
   268
    if (!SDL_X11_HAVE_XVIDMODE) {
slouken@5408
   269
        return SDL_FALSE;
slouken@5408
   270
    }
slouken@5408
   271
slouken@2873
   272
    /* Query the extension version */
slouken@2873
   273
    vm_error = -1;
slouken@5408
   274
    if (!XF86VidModeQueryExtension(display, &vm_event, &vm_error)
slouken@5408
   275
        || !XF86VidModeQueryVersion(display, major, minor)) {
slouken@2873
   276
        return SDL_FALSE;
slouken@2873
   277
    }
slouken@2873
   278
    return SDL_TRUE;
slouken@2873
   279
}
slouken@2873
   280
slouken@4518
   281
static
slouken@5408
   282
Bool XF86VidModeGetModeInfo(Display * dpy, int scr,
slouken@5408
   283
                                       XF86VidModeModeInfo* info)
slouken@2873
   284
{
slouken@2873
   285
    Bool retval;
slouken@2873
   286
    int dotclock;
slouken@5408
   287
    XF86VidModeModeLine l;
slouken@2873
   288
    SDL_zerop(info);
slouken@2873
   289
    SDL_zero(l);
slouken@5408
   290
    retval = XF86VidModeGetModeLine(dpy, scr, &dotclock, &l);
slouken@2873
   291
    info->dotclock = dotclock;
slouken@2873
   292
    info->hdisplay = l.hdisplay;
slouken@2873
   293
    info->hsyncstart = l.hsyncstart;
slouken@2873
   294
    info->hsyncend = l.hsyncend;
slouken@2873
   295
    info->htotal = l.htotal;
slouken@2873
   296
    info->hskew = l.hskew;
slouken@2873
   297
    info->vdisplay = l.vdisplay;
slouken@2873
   298
    info->vsyncstart = l.vsyncstart;
slouken@2873
   299
    info->vsyncend = l.vsyncend;
slouken@2873
   300
    info->vtotal = l.vtotal;
slouken@2873
   301
    info->flags = l.flags;
slouken@2873
   302
    info->privsize = l.privsize;
slouken@2873
   303
    info->private = l.private;
slouken@2873
   304
    return retval;
slouken@2873
   305
}
slouken@2873
   306
slouken@2873
   307
static int
slouken@5408
   308
calculate_rate(XF86VidModeModeInfo * info)
slouken@2873
   309
{
slouken@2874
   310
    return (info->htotal
slouken@2874
   311
            && info->vtotal) ? (1000 * info->dotclock / (info->htotal *
slouken@2874
   312
                                                         info->vtotal)) : 0;
slouken@2873
   313
}
slouken@2873
   314
slouken@2873
   315
static void
slouken@2874
   316
save_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   317
{
slouken@5408
   318
    XF86VidModeGetModeInfo(display, data->screen,
slouken@5408
   319
                                    &data->saved_mode);
slouken@5408
   320
    XF86VidModeGetViewPort(display, data->screen,
slouken@5408
   321
                                    &data->saved_view.x,
slouken@5408
   322
                                    &data->saved_view.y);
slouken@2873
   323
}
slouken@2873
   324
slouken@4518
   325
/*
slouken@2873
   326
static void
slouken@2874
   327
restore_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   328
{
slouken@5408
   329
    XF86VidModeModeInfo mode;
slouken@2873
   330
slouken@5408
   331
    if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
slouken@2873
   332
        if (SDL_memcmp(&mode, &data->saved_mode, sizeof(mode)) != 0) {
slouken@5408
   333
            XF86VidModeSwitchToMode(display, data->screen, &data->saved_mode);
slouken@2873
   334
        }
slouken@2873
   335
    }
slouken@2873
   336
    if ((data->saved_view.x != 0) || (data->saved_view.y != 0)) {
slouken@5408
   337
        XF86VidModeSetViewPort(display, data->screen,
slouken@5408
   338
                                        data->saved_view.x,
slouken@5408
   339
                                        data->saved_view.y);
slouken@2873
   340
    }
slouken@2873
   341
}
slouken@4518
   342
*/
slouken@5408
   343
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   344
slouken@1950
   345
void
slouken@3500
   346
X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
slouken@1950
   347
{
slouken@2870
   348
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   349
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@2873
   350
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   351
    int xinerama_major, xinerama_minor;
slouken@2873
   352
    int screens;
slouken@5408
   353
    XineramaScreenInfo * xinerama;
slouken@2873
   354
#endif
slouken@2873
   355
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   356
    int xrandr_major, xrandr_minor;
slouken@2873
   357
    int nsizes, nrates;
slouken@2873
   358
    XRRScreenSize *sizes;
slouken@2873
   359
    short *rates;
slouken@2873
   360
#endif
slouken@5408
   361
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   362
    int vm_major, vm_minor;
slouken@2873
   363
    int nmodes;
slouken@5408
   364
    XF86VidModeModeInfo ** modes;
slouken@2873
   365
#endif
slouken@2873
   366
    int screen_w;
slouken@2873
   367
    int screen_h;
slouken@1950
   368
    SDL_DisplayMode mode;
slouken@2870
   369
slouken@2873
   370
    /* Unfortunately X11 requires the window to be created with the correct
slouken@2873
   371
     * visual and depth ahead of time, but the SDL API allows you to create
slouken@2873
   372
     * a window before setting the fullscreen display mode.  This means that
slouken@2873
   373
     * we have to use the same format for all windows and all display modes.
slouken@2873
   374
     * (or support recreating the window with a new visual behind the scenes)
slouken@2873
   375
     */
slouken@3500
   376
    mode.format = sdl_display->current_mode.format;
slouken@2873
   377
    mode.driverdata = NULL;
slouken@2873
   378
slouken@2873
   379
    data->use_xinerama = 0;
slouken@2873
   380
    data->use_xrandr = 0;
slouken@2873
   381
    data->use_vidmode = 0;
slouken@2873
   382
    screen_w = DisplayWidth(display, data->screen);
slouken@2873
   383
    screen_h = DisplayHeight(display, data->screen);
slouken@2873
   384
slouken@2873
   385
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   386
    /* Query Xinerama extention */
slouken@2873
   387
    if (CheckXinerama(display, &xinerama_major, &xinerama_minor)) {
slouken@2873
   388
#ifdef X11MODES_DEBUG
slouken@2873
   389
        printf("X11 detected Xinerama:\n");
slouken@2873
   390
#endif
slouken@5408
   391
        xinerama = XineramaQueryScreens(display, &screens);
slouken@2873
   392
        if (xinerama) {
slouken@2873
   393
            int i;
slouken@2873
   394
            for (i = 0; i < screens; i++) {
slouken@2873
   395
#ifdef X11MODES_DEBUG
slouken@2873
   396
                printf("xinerama %d: %dx%d+%d+%d\n",
slouken@2874
   397
                       xinerama[i].screen_number,
slouken@2874
   398
                       xinerama[i].width, xinerama[i].height,
slouken@2874
   399
                       xinerama[i].x_org, xinerama[i].y_org);
slouken@2873
   400
#endif
slouken@2873
   401
                if (xinerama[i].screen_number == data->screen) {
slouken@2874
   402
                    data->use_xinerama =
slouken@2874
   403
                        xinerama_major * 100 + xinerama_minor;
slouken@2873
   404
                    data->xinerama_info = xinerama[i];
slouken@2873
   405
                }
slouken@2873
   406
            }
slouken@2873
   407
            XFree(xinerama);
slouken@2873
   408
        }
slouken@2873
   409
slouken@2873
   410
        if (data->use_xinerama) {
slouken@2873
   411
            /* Add the full xinerama mode */
slouken@2873
   412
            if (screen_w > data->xinerama_info.width ||
slouken@2873
   413
                screen_h > data->xinerama_info.height) {
slouken@2873
   414
                mode.w = screen_w;
slouken@2873
   415
                mode.h = screen_h;
slouken@2873
   416
                mode.refresh_rate = 0;
slouken@3500
   417
                SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   418
            }
slouken@2873
   419
slouken@2873
   420
            /* Add the head xinerama mode */
slouken@2873
   421
            mode.w = data->xinerama_info.width;
slouken@2873
   422
            mode.h = data->xinerama_info.height;
slouken@2873
   423
            mode.refresh_rate = 0;
slouken@3500
   424
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   425
        }
slouken@2873
   426
    }
slouken@2873
   427
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   428
slouken@2873
   429
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   430
    /* XRandR */
slouken@2873
   431
    /* require at least XRandR v1.0 (arbitrary) */
slouken@2874
   432
    if (CheckXRandR(display, &xrandr_major, &xrandr_minor)
slouken@2874
   433
        && xrandr_major >= 1) {
slouken@2873
   434
#ifdef X11MODES_DEBUG
slouken@2873
   435
        fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
slouken@2873
   436
                xrandr_major, xrandr_minor);
slouken@2873
   437
#endif
slouken@2873
   438
slouken@2873
   439
        /* save the screen configuration since we must reference it
slouken@2873
   440
           each time we toggle modes.
slouken@2874
   441
         */
slouken@2874
   442
        data->screen_config =
slouken@2874
   443
            XRRGetScreenInfo(display, RootWindow(display, data->screen));
slouken@2873
   444
slouken@2873
   445
        /* retrieve the list of resolution */
slouken@2873
   446
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   447
        if (nsizes > 0) {
slouken@2873
   448
            int i, j;
slouken@2874
   449
            for (i = 0; i < nsizes; i++) {
slouken@2873
   450
                mode.w = sizes[i].width;
slouken@2873
   451
                mode.h = sizes[i].height;
slouken@2873
   452
slouken@2873
   453
                rates = XRRConfigRates(data->screen_config, i, &nrates);
slouken@2873
   454
                for (j = 0; j < nrates; ++j) {
slouken@2873
   455
                    mode.refresh_rate = rates[j];
slouken@2873
   456
#ifdef X11MODES_DEBUG
slouken@2874
   457
                    fprintf(stderr,
slouken@2874
   458
                            "XRANDR: mode = %4d[%d], w = %4d, h = %4d, rate = %4d\n",
slouken@2873
   459
                            i, j, mode.w, mode.h, mode.refresh_rate);
slouken@2873
   460
#endif
slouken@3500
   461
                    SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   462
                }
slouken@2873
   463
            }
slouken@2873
   464
slouken@2873
   465
            data->use_xrandr = xrandr_major * 100 + xrandr_minor;
slouken@2874
   466
            data->saved_size =
slouken@2874
   467
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   468
                                              &data->saved_rotation);
slouken@2873
   469
            data->saved_rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   470
        }
slouken@2873
   471
    }
slouken@2873
   472
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   473
slouken@5408
   474
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   475
    /* XVidMode */
slouken@2873
   476
    if (!data->use_xrandr &&
slouken@2873
   477
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   478
        (!data->use_xinerama || data->xinerama_info.screen_number == 0) &&
slouken@2873
   479
#endif
slouken@2873
   480
        CheckVidMode(display, &vm_major, &vm_minor) &&
slouken@5408
   481
        XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   482
        int i;
slouken@2873
   483
slouken@2873
   484
#ifdef X11MODES_DEBUG
slouken@2873
   485
        printf("VidMode modes: (unsorted)\n");
slouken@2873
   486
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   487
            printf("Mode %d: %d x %d @ %d\n", i,
slouken@2874
   488
                   modes[i]->hdisplay, modes[i]->vdisplay,
slouken@2874
   489
                   calculate_rate(modes[i]));
slouken@2873
   490
        }
slouken@2873
   491
#endif
slouken@2873
   492
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   493
            mode.w = modes[i]->hdisplay;
slouken@2873
   494
            mode.h = modes[i]->vdisplay;
slouken@2873
   495
            mode.refresh_rate = calculate_rate(modes[i]);
slouken@3500
   496
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   497
        }
slouken@2873
   498
        XFree(modes);
slouken@2873
   499
slouken@2873
   500
        data->use_vidmode = vm_major * 100 + vm_minor;
slouken@2873
   501
        save_mode(display, data);
slouken@2873
   502
    }
slouken@5408
   503
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   504
slouken@2873
   505
    if (!data->use_xrandr && !data->use_vidmode) {
slouken@2873
   506
        mode.w = screen_w;
slouken@2873
   507
        mode.h = screen_h;
slouken@2873
   508
        mode.refresh_rate = 0;
slouken@3500
   509
        SDL_AddDisplayMode(sdl_display, &mode);
slouken@2874
   510
    }
slouken@2873
   511
#ifdef X11MODES_DEBUG
slouken@2873
   512
    if (data->use_xinerama) {
slouken@2873
   513
        printf("Xinerama is enabled\n");
slouken@2873
   514
    }
slouken@2873
   515
slouken@2873
   516
    if (data->use_xrandr) {
slouken@2873
   517
        printf("XRandR is enabled\n");
slouken@2873
   518
    }
slouken@2873
   519
slouken@2873
   520
    if (data->use_vidmode) {
slouken@2873
   521
        printf("VidMode is enabled\n");
slouken@2873
   522
    }
slouken@2873
   523
#endif /* X11MODES_DEBUG */
slouken@2873
   524
}
slouken@2873
   525
slouken@2873
   526
static void
slouken@2874
   527
get_real_resolution(Display * display, SDL_DisplayData * data, int *w, int *h,
slouken@2874
   528
                    int *rate)
slouken@2873
   529
{
slouken@2873
   530
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   531
    if (data->use_xrandr) {
slouken@2873
   532
        int nsizes;
slouken@2873
   533
        XRRScreenSize *sizes;
slouken@2874
   534
slouken@2873
   535
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   536
        if (nsizes > 0) {
slouken@2873
   537
            int cur_size;
slouken@2873
   538
            Rotation cur_rotation;
slouken@2873
   539
slouken@2874
   540
            cur_size =
slouken@2874
   541
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   542
                                              &cur_rotation);
slouken@2873
   543
            *w = sizes[cur_size].width;
slouken@2873
   544
            *h = sizes[cur_size].height;
slouken@2873
   545
            *rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   546
#ifdef X11MODES_DEBUG
slouken@2874
   547
            fprintf(stderr,
slouken@2874
   548
                    "XRANDR: get_real_resolution: w = %d, h = %d, rate = %d\n",
slouken@2874
   549
                    *w, *h, *rate);
slouken@2873
   550
#endif
slouken@2873
   551
            return;
slouken@2873
   552
        }
slouken@2873
   553
    }
slouken@2873
   554
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   555
slouken@5408
   556
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   557
    if (data->use_vidmode) {
slouken@5408
   558
        XF86VidModeModeInfo mode;
slouken@2873
   559
slouken@5408
   560
        if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
slouken@2873
   561
            *w = mode.hdisplay;
slouken@2873
   562
            *h = mode.vdisplay;
slouken@2873
   563
            *rate = calculate_rate(&mode);
slouken@2873
   564
            return;
slouken@2873
   565
        }
slouken@2873
   566
    }
slouken@5408
   567
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   568
slouken@2873
   569
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   570
    if (data->use_xinerama) {
slouken@2873
   571
        *w = data->xinerama_info.width;
slouken@2873
   572
        *h = data->xinerama_info.height;
slouken@2873
   573
        *rate = 0;
slouken@2873
   574
        return;
slouken@2873
   575
    }
slouken@2873
   576
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   577
slouken@2873
   578
    *w = DisplayWidth(display, data->screen);
slouken@2873
   579
    *h = DisplayHeight(display, data->screen);
slouken@2873
   580
    *rate = 0;
slouken@2873
   581
}
slouken@2873
   582
slouken@2873
   583
static void
slouken@2874
   584
set_best_resolution(Display * display, SDL_DisplayData * data, int w, int h,
slouken@2874
   585
                    int rate)
slouken@2873
   586
{
slouken@2873
   587
    int real_w, real_h, real_rate;
slouken@2873
   588
slouken@2873
   589
    /* check current mode so we can avoid uneccessary mode changes */
slouken@2873
   590
    get_real_resolution(display, data, &real_w, &real_h, &real_rate);
slouken@2873
   591
    if (w == real_w && h == real_h && (!rate || rate == real_rate)) {
slouken@2873
   592
        return;
slouken@2873
   593
    }
slouken@2873
   594
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   595
    if (data->use_xrandr) {
slouken@2873
   596
#ifdef X11MODES_DEBUG
slouken@2873
   597
        fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
slouken@2873
   598
                w, h);
slouken@2873
   599
#endif
slouken@2873
   600
        int i, nsizes, nrates;
slouken@2873
   601
        int best;
slouken@2873
   602
        int best_rate;
slouken@2873
   603
        XRRScreenSize *sizes;
slouken@2873
   604
        short *rates;
slouken@2873
   605
slouken@2873
   606
        /* find the smallest resolution that is at least as big as the user requested */
slouken@2873
   607
        best = -1;
slouken@2873
   608
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   609
        for (i = 0; i < nsizes; ++i) {
slouken@2873
   610
            if (sizes[i].width < w || sizes[i].height < h) {
slouken@2873
   611
                continue;
slouken@2873
   612
            }
slouken@2873
   613
            if (sizes[i].width == w && sizes[i].height == h) {
slouken@2873
   614
                best = i;
slouken@2873
   615
                break;
slouken@2873
   616
            }
slouken@2873
   617
            if (best == -1 ||
slouken@2873
   618
                (sizes[i].width < sizes[best].width) ||
slouken@2874
   619
                (sizes[i].width == sizes[best].width
slouken@2874
   620
                 && sizes[i].height < sizes[best].height)) {
slouken@2873
   621
                best = i;
slouken@2873
   622
            }
slouken@2873
   623
        }
slouken@2873
   624
slouken@2873
   625
        if (best >= 0) {
slouken@2873
   626
            best_rate = 0;
slouken@2873
   627
            rates = XRRConfigRates(data->screen_config, best, &nrates);
slouken@2873
   628
            for (i = 0; i < nrates; ++i) {
slouken@2873
   629
                if (rates[i] == rate) {
slouken@2873
   630
                    best_rate = rate;
slouken@2873
   631
                    break;
slouken@2873
   632
                }
slouken@2873
   633
                if (!rate) {
slouken@2873
   634
                    /* Higher is better, right? */
slouken@2873
   635
                    if (rates[i] > best_rate) {
slouken@2873
   636
                        best_rate = rates[i];
slouken@2873
   637
                    }
slouken@2873
   638
                } else {
slouken@2874
   639
                    if (SDL_abs(rates[i] - rate) < SDL_abs(best_rate - rate)) {
slouken@2873
   640
                        best_rate = rates[i];
slouken@2873
   641
                    }
slouken@2873
   642
                }
slouken@2873
   643
            }
slouken@2874
   644
            XRRSetScreenConfigAndRate(display, data->screen_config,
slouken@2874
   645
                                      RootWindow(display, data->screen), best,
slouken@2874
   646
                                      data->saved_rotation, best_rate,
slouken@2874
   647
                                      CurrentTime);
slouken@2873
   648
        }
slouken@2873
   649
        return;
slouken@2873
   650
    }
slouken@2873
   651
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   652
slouken@5408
   653
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   654
    if (data->use_vidmode) {
slouken@5408
   655
        XF86VidModeModeInfo ** modes;
slouken@2873
   656
        int i, nmodes;
slouken@2873
   657
        int best;
slouken@2873
   658
slouken@5408
   659
        if (XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   660
            best = -1;
slouken@2873
   661
            for (i = 0; i < nmodes; ++i) {
slouken@2873
   662
                if (modes[i]->hdisplay < w || modes[i]->vdisplay < h) {
slouken@2873
   663
                    continue;
slouken@2873
   664
                }
slouken@2873
   665
                if (best == -1 ||
slouken@2873
   666
                    (modes[i]->hdisplay < modes[best]->hdisplay) ||
slouken@2874
   667
                    (modes[i]->hdisplay == modes[best]->hdisplay
slouken@2874
   668
                     && modes[i]->vdisplay < modes[best]->vdisplay)) {
slouken@2873
   669
                    best = i;
slouken@2873
   670
                    continue;
slouken@2873
   671
                }
slouken@2873
   672
                if ((modes[i]->hdisplay == modes[best]->hdisplay) &&
slouken@2873
   673
                    (modes[i]->vdisplay == modes[best]->vdisplay)) {
slouken@2873
   674
                    if (!rate) {
slouken@2873
   675
                        /* Higher is better, right? */
slouken@2874
   676
                        if (calculate_rate(modes[i]) >
slouken@2874
   677
                            calculate_rate(modes[best])) {
slouken@2873
   678
                            best = i;
slouken@2873
   679
                        }
slouken@2873
   680
                    } else {
slouken@2874
   681
                        if (SDL_abs(calculate_rate(modes[i]) - rate) <
slouken@2874
   682
                            SDL_abs(calculate_rate(modes[best]) - rate)) {
slouken@2873
   683
                            best = i;
slouken@2873
   684
                        }
slouken@2873
   685
                    }
slouken@2873
   686
                }
slouken@2873
   687
            }
slouken@2873
   688
            if (best >= 0) {
slouken@2873
   689
#ifdef X11MODES_DEBUG
slouken@2873
   690
                printf("Best Mode %d: %d x %d @ %d\n", best,
slouken@2874
   691
                       modes[best]->hdisplay, modes[best]->vdisplay,
slouken@2874
   692
                       calculate_rate(modes[best]));
slouken@2873
   693
#endif
slouken@5408
   694
                XF86VidModeSwitchToMode(display, data->screen, modes[best]);
slouken@2873
   695
            }
slouken@2873
   696
            XFree(modes);
slouken@2873
   697
        }
slouken@2873
   698
        return;
slouken@2873
   699
    }
slouken@5408
   700
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@1950
   701
}
slouken@1950
   702
slouken@1950
   703
int
slouken@3500
   704
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
slouken@1950
   705
{
slouken@2873
   706
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   707
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@2873
   708
slouken@2873
   709
    set_best_resolution(display, data, mode->w, mode->h, mode->refresh_rate);
slouken@2873
   710
    return 0;
slouken@1950
   711
}
slouken@1950
   712
slouken@1950
   713
void
slouken@1950
   714
X11_QuitModes(_THIS)
slouken@1950
   715
{
slouken@1950
   716
}
slouken@1950
   717
slouken@1950
   718
/* vi: set ts=4 sw=4 expandtab: */