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