src/video/x11/SDL_x11modes.c
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Jun 2012 14:19:05 -0300
changeset 6331 5732e1a80bde
parent 6138 4c64952a58fb
child 6468 6af2a8db95d0
permissions -rwxr-xr-x
Fixes #1522, improved Xinerama / Twinview support
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1950
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1950
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1950
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1950
    20
*/
slouken@1950
    21
#include "SDL_config.h"
slouken@1950
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@1950
    25
#include "SDL_x11video.h"
slouken@1950
    26
slouken@4519
    27
/*#define X11MODES_DEBUG*/
slouken@1950
    28
slouken@1950
    29
static int
slouken@1950
    30
get_visualinfo(Display * display, int screen, XVisualInfo * vinfo)
slouken@1950
    31
{
slouken@1950
    32
    const char *visual_id = SDL_getenv("SDL_VIDEO_X11_VISUALID");
slouken@1950
    33
    int depth;
slouken@1950
    34
slouken@1950
    35
    /* Look for an exact visual, if requested */
slouken@1950
    36
    if (visual_id) {
slouken@1950
    37
        XVisualInfo *vi, template;
slouken@1950
    38
        int nvis;
slouken@1950
    39
slouken@1950
    40
        SDL_zero(template);
slouken@1950
    41
        template.visualid = SDL_strtol(visual_id, NULL, 0);
slouken@1950
    42
        vi = XGetVisualInfo(display, VisualIDMask, &template, &nvis);
slouken@1950
    43
        if (vi) {
slouken@1950
    44
            *vinfo = *vi;
slouken@1950
    45
            XFree(vi);
slouken@1950
    46
            return 0;
slouken@1950
    47
        }
slouken@1950
    48
    }
slouken@1950
    49
slouken@1950
    50
    depth = DefaultDepth(display, screen);
slouken@5466
    51
    if ((X11_UseDirectColorVisuals() &&
slouken@5466
    52
         XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
slouken@5466
    53
        XMatchVisualInfo(display, screen, depth, TrueColor, vinfo) ||
slouken@1950
    54
        XMatchVisualInfo(display, screen, depth, PseudoColor, vinfo) ||
slouken@1950
    55
        XMatchVisualInfo(display, screen, depth, StaticColor, vinfo)) {
slouken@1950
    56
        return 0;
slouken@1950
    57
    }
slouken@1950
    58
    return -1;
slouken@1950
    59
}
slouken@1950
    60
slouken@5182
    61
int
slouken@5182
    62
X11_GetVisualInfoFromVisual(Display * display, Visual * visual, XVisualInfo * vinfo)
slouken@5182
    63
{
slouken@5182
    64
    XVisualInfo *vi;
slouken@5182
    65
    int nvis;
slouken@5182
    66
slouken@5182
    67
    vinfo->visualid = XVisualIDFromVisual(visual);
slouken@5182
    68
    vi = XGetVisualInfo(display, VisualIDMask, vinfo, &nvis);
slouken@5182
    69
    if (vi) {
slouken@5182
    70
        *vinfo = *vi;
slouken@5182
    71
        XFree(vi);
slouken@5182
    72
        return 0;
slouken@5182
    73
    }
slouken@5182
    74
    return -1;
slouken@5182
    75
}
slouken@5182
    76
slouken@5182
    77
Uint32
slouken@2874
    78
X11_GetPixelFormatFromVisualInfo(Display * display, XVisualInfo * vinfo)
slouken@2870
    79
{
slouken@2870
    80
    if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
slouken@2870
    81
        int bpp;
slouken@2870
    82
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@2870
    83
slouken@2870
    84
        Rmask = vinfo->visual->red_mask;
slouken@2870
    85
        Gmask = vinfo->visual->green_mask;
slouken@2870
    86
        Bmask = vinfo->visual->blue_mask;
slouken@2870
    87
        if (vinfo->depth == 32) {
slouken@2870
    88
            Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
slouken@2870
    89
        } else {
slouken@2870
    90
            Amask = 0;
slouken@2870
    91
        }
slouken@2870
    92
slouken@2870
    93
        bpp = vinfo->depth;
slouken@2870
    94
        if (bpp == 24) {
slouken@2870
    95
            int i, n;
slouken@2870
    96
            XPixmapFormatValues *p = XListPixmapFormats(display, &n);
slouken@2870
    97
            if (p) {
slouken@2870
    98
                for (i = 0; i < n; ++i) {
slouken@2870
    99
                    if (p[i].depth == 24) {
slouken@2870
   100
                        bpp = p[i].bits_per_pixel;
slouken@2870
   101
                        break;
slouken@2870
   102
                    }
slouken@2870
   103
                }
slouken@2870
   104
                XFree(p);
slouken@2870
   105
            }
slouken@2870
   106
        }
slouken@2870
   107
slouken@2870
   108
        return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2870
   109
    }
slouken@2870
   110
slouken@2870
   111
    if (vinfo->class == PseudoColor || vinfo->class == StaticColor) {
slouken@2870
   112
        switch (vinfo->depth) {
slouken@2870
   113
        case 8:
slouken@2870
   114
            return SDL_PIXELTYPE_INDEX8;
slouken@2870
   115
        case 4:
slouken@2870
   116
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   117
                return SDL_PIXELFORMAT_INDEX4LSB;
slouken@2870
   118
            } else {
slouken@2870
   119
                return SDL_PIXELFORMAT_INDEX4MSB;
slouken@2870
   120
            }
slouken@2870
   121
            break;
slouken@2870
   122
        case 1:
slouken@2870
   123
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   124
                return SDL_PIXELFORMAT_INDEX1LSB;
slouken@2870
   125
            } else {
slouken@2870
   126
                return SDL_PIXELFORMAT_INDEX1MSB;
slouken@2870
   127
            }
slouken@2870
   128
            break;
slouken@2870
   129
        }
slouken@2870
   130
    }
slouken@2870
   131
slouken@2870
   132
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@2870
   133
}
gabomdq@6331
   134
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   135
static SDL_bool CheckXinerama(Display * display, int *major, int *minor);
gabomdq@6331
   136
#endif
slouken@2870
   137
slouken@3521
   138
int
slouken@1950
   139
X11_InitModes(_THIS)
slouken@1950
   140
{
slouken@1950
   141
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
gabomdq@6331
   142
    int screen, screencount;
slouken@1950
   143
gabomdq@6331
   144
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   145
    int xinerama_major, xinerama_minor;
gabomdq@6331
   146
    XineramaScreenInfo * xinerama = NULL;
gabomdq@6331
   147
    /* Query Xinerama extention
gabomdq@6331
   148
     * NOTE: This works with Nvidia Twinview correctly, but you need version 302.17 (released on June 2012)
gabomdq@6331
   149
     *       or newer of the Nvidia binary drivers
gabomdq@6331
   150
     */
gabomdq@6331
   151
    if (CheckXinerama(data->display, &xinerama_major, &xinerama_minor)) {
gabomdq@6331
   152
        xinerama = XineramaQueryScreens(data->display, &screencount);
gabomdq@6331
   153
        if (!xinerama) screencount = ScreenCount(data->display);
gabomdq@6331
   154
    }
gabomdq@6331
   155
    else {
gabomdq@6331
   156
        screencount = ScreenCount(data->display);
gabomdq@6331
   157
    }
gabomdq@6331
   158
#else
gabomdq@6331
   159
    screencount = ScreenCount(data->display);
gabomdq@6331
   160
#endif
gabomdq@6331
   161
gabomdq@6331
   162
    for (screen = 0; screen < screencount; ++screen) {
slouken@1950
   163
        XVisualInfo vinfo;
slouken@1950
   164
        SDL_VideoDisplay display;
slouken@1950
   165
        SDL_DisplayData *displaydata;
slouken@1950
   166
        SDL_DisplayMode mode;
slouken@2916
   167
        XPixmapFormatValues *pixmapFormats;
slouken@2916
   168
        int i, n;
gabomdq@6331
   169
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   170
        if (xinerama) {
gabomdq@6331
   171
            if (get_visualinfo(data->display, 0, &vinfo) < 0) {
gabomdq@6331
   172
                continue;
gabomdq@6331
   173
            }
gabomdq@6331
   174
        }
gabomdq@6331
   175
        else {
gabomdq@6331
   176
            if (get_visualinfo(data->display, screen, &vinfo) < 0) {
gabomdq@6331
   177
                continue;
gabomdq@6331
   178
            }
gabomdq@6331
   179
        }
gabomdq@6331
   180
#else
slouken@1950
   181
        if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@1950
   182
            continue;
slouken@1950
   183
        }
gabomdq@6331
   184
#endif
slouken@1950
   185
slouken@2870
   186
        mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
slouken@5149
   187
        if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
slouken@5149
   188
            /* We don't support palettized modes now */
slouken@5149
   189
            continue;
slouken@5149
   190
        }
gabomdq@6331
   191
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   192
        if (xinerama) {
gabomdq@6331
   193
            mode.w = xinerama[screen].width;
gabomdq@6331
   194
            mode.h = xinerama[screen].height;
gabomdq@6331
   195
        }
gabomdq@6331
   196
        else {
gabomdq@6331
   197
            mode.w = DisplayWidth(data->display, screen);
gabomdq@6331
   198
            mode.h = DisplayHeight(data->display, screen);
gabomdq@6331
   199
        }
gabomdq@6331
   200
#else
slouken@1950
   201
        mode.w = DisplayWidth(data->display, screen);
slouken@1950
   202
        mode.h = DisplayHeight(data->display, screen);
gabomdq@6331
   203
#endif
slouken@1950
   204
        mode.refresh_rate = 0;
slouken@1950
   205
        mode.driverdata = NULL;
slouken@1950
   206
slouken@1950
   207
        displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
slouken@1950
   208
        if (!displaydata) {
slouken@1950
   209
            continue;
slouken@1950
   210
        }
gabomdq@6331
   211
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   212
        /* Most of SDL's calls to X11 are unwaware of Xinerama, and to X11 standard calls, when Xinerama is active,
gabomdq@6331
   213
         * there's only one screen available. So we force the screen number to zero and
gabomdq@6331
   214
         * let Xinerama specific code handle specific functionality using displaydata->xinerama_info
gabomdq@6331
   215
         */
gabomdq@6331
   216
        if (xinerama) {
gabomdq@6331
   217
            displaydata->screen = 0;
gabomdq@6331
   218
            displaydata->use_xinerama = xinerama_major * 100 + xinerama_minor;
gabomdq@6331
   219
            displaydata->xinerama_info = xinerama[screen];
gabomdq@6331
   220
        }
gabomdq@6331
   221
        else displaydata->screen = screen;
gabomdq@6331
   222
#else
slouken@1950
   223
        displaydata->screen = screen;
gabomdq@6331
   224
#endif
slouken@1950
   225
        displaydata->visual = vinfo.visual;
slouken@1951
   226
        displaydata->depth = vinfo.depth;
slouken@1950
   227
slouken@2920
   228
        displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
slouken@2916
   229
        pixmapFormats = XListPixmapFormats(data->display, &n);
slouken@2916
   230
        if (pixmapFormats) {
slouken@2916
   231
            for (i = 0; i < n; ++i) {
slouken@2916
   232
                if (pixmapFormats[i].depth == displaydata->depth) {
slouken@2916
   233
                    displaydata->scanline_pad = pixmapFormats[i].scanline_pad;
slouken@2916
   234
                    break;
slouken@2916
   235
                }
slouken@2916
   236
            }
slouken@2916
   237
            XFree(pixmapFormats);
slouken@2916
   238
        }
slouken@2916
   239
slouken@1950
   240
        SDL_zero(display);
slouken@1950
   241
        display.desktop_mode = mode;
slouken@1950
   242
        display.current_mode = mode;
slouken@1950
   243
        display.driverdata = displaydata;
slouken@1950
   244
        SDL_AddVideoDisplay(&display);
slouken@1950
   245
    }
gabomdq@6331
   246
gabomdq@6331
   247
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   248
    if (xinerama) XFree(xinerama);
gabomdq@6331
   249
#endif
gabomdq@6331
   250
slouken@3521
   251
    if (_this->num_displays == 0) {
slouken@3521
   252
        SDL_SetError("No available displays");
slouken@3521
   253
        return -1;
slouken@3521
   254
    }
slouken@3521
   255
    return 0;
slouken@1950
   256
}
slouken@1950
   257
slouken@2873
   258
/* Global for the error handler */
slouken@2873
   259
int vm_event, vm_error = -1;
slouken@2873
   260
slouken@2873
   261
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   262
static SDL_bool
slouken@2874
   263
CheckXinerama(Display * display, int *major, int *minor)
slouken@2873
   264
{
icculus@6027
   265
    int event_base = 0;
icculus@6027
   266
    int error_base = 0;
slouken@2873
   267
    const char *env;
slouken@2873
   268
slouken@2873
   269
    /* Default the extension not available */
slouken@2873
   270
    *major = *minor = 0;
slouken@2873
   271
slouken@2873
   272
    /* Allow environment override */
slouken@2873
   273
    env = getenv("SDL_VIDEO_X11_XINERAMA");
slouken@2873
   274
    if (env && !SDL_atoi(env)) {
slouken@2873
   275
        return SDL_FALSE;
slouken@2873
   276
    }
slouken@2873
   277
slouken@5408
   278
    if (!SDL_X11_HAVE_XINERAMA) {
slouken@5408
   279
        return SDL_FALSE;
slouken@5408
   280
    }
slouken@5408
   281
slouken@2873
   282
    /* Query the extension version */
icculus@6027
   283
    if (!XineramaQueryExtension(display, &event_base, &error_base) ||
icculus@6027
   284
        !XineramaQueryVersion(display, major, minor) ||
slouken@5408
   285
        !XineramaIsActive(display)) {
slouken@2873
   286
        return SDL_FALSE;
slouken@2873
   287
    }
slouken@2873
   288
    return SDL_TRUE;
slouken@2873
   289
}
slouken@2873
   290
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   291
slouken@2873
   292
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   293
static SDL_bool
slouken@2874
   294
CheckXRandR(Display * display, int *major, int *minor)
slouken@2873
   295
{
slouken@2873
   296
    const char *env;
slouken@2873
   297
slouken@2873
   298
    /* Default the extension not available */
slouken@2873
   299
    *major = *minor = 0;
slouken@2873
   300
slouken@2873
   301
    /* Allow environment override */
slouken@2873
   302
    env = getenv("SDL_VIDEO_X11_XRANDR");
slouken@2873
   303
    if (env && !SDL_atoi(env)) {
slouken@2873
   304
        return SDL_FALSE;
slouken@2873
   305
    }
slouken@2873
   306
slouken@2873
   307
    if (!SDL_X11_HAVE_XRANDR) {
slouken@2873
   308
        return SDL_FALSE;
slouken@2873
   309
    }
slouken@2873
   310
slouken@2873
   311
    /* Query the extension version */
slouken@2873
   312
    if (!XRRQueryVersion(display, major, minor)) {
slouken@2873
   313
        return SDL_FALSE;
slouken@2873
   314
    }
slouken@2873
   315
    return SDL_TRUE;
slouken@2873
   316
}
slouken@2873
   317
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   318
slouken@5408
   319
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   320
static SDL_bool
slouken@2874
   321
CheckVidMode(Display * display, int *major, int *minor)
slouken@2873
   322
{
slouken@2873
   323
    const char *env;
slouken@2873
   324
slouken@2873
   325
    /* Default the extension not available */
slouken@2873
   326
    *major = *minor = 0;
slouken@2873
   327
slouken@2873
   328
    /* Allow environment override */
slouken@5408
   329
    env = getenv("SDL_VIDEO_X11_XVIDMODE");
slouken@2873
   330
    if (env && !SDL_atoi(env)) {
slouken@2873
   331
        return SDL_FALSE;
slouken@2873
   332
    }
slouken@2874
   333
slouken@5408
   334
    if (!SDL_X11_HAVE_XVIDMODE) {
slouken@5408
   335
        return SDL_FALSE;
slouken@5408
   336
    }
slouken@5408
   337
slouken@2873
   338
    /* Query the extension version */
slouken@2873
   339
    vm_error = -1;
slouken@5408
   340
    if (!XF86VidModeQueryExtension(display, &vm_event, &vm_error)
slouken@5408
   341
        || !XF86VidModeQueryVersion(display, major, minor)) {
slouken@2873
   342
        return SDL_FALSE;
slouken@2873
   343
    }
slouken@2873
   344
    return SDL_TRUE;
slouken@2873
   345
}
slouken@2873
   346
slouken@4518
   347
static
slouken@5408
   348
Bool XF86VidModeGetModeInfo(Display * dpy, int scr,
slouken@5408
   349
                                       XF86VidModeModeInfo* info)
slouken@2873
   350
{
slouken@2873
   351
    Bool retval;
slouken@2873
   352
    int dotclock;
slouken@5408
   353
    XF86VidModeModeLine l;
slouken@2873
   354
    SDL_zerop(info);
slouken@2873
   355
    SDL_zero(l);
slouken@5408
   356
    retval = XF86VidModeGetModeLine(dpy, scr, &dotclock, &l);
slouken@2873
   357
    info->dotclock = dotclock;
slouken@2873
   358
    info->hdisplay = l.hdisplay;
slouken@2873
   359
    info->hsyncstart = l.hsyncstart;
slouken@2873
   360
    info->hsyncend = l.hsyncend;
slouken@2873
   361
    info->htotal = l.htotal;
slouken@2873
   362
    info->hskew = l.hskew;
slouken@2873
   363
    info->vdisplay = l.vdisplay;
slouken@2873
   364
    info->vsyncstart = l.vsyncstart;
slouken@2873
   365
    info->vsyncend = l.vsyncend;
slouken@2873
   366
    info->vtotal = l.vtotal;
slouken@2873
   367
    info->flags = l.flags;
slouken@2873
   368
    info->privsize = l.privsize;
slouken@2873
   369
    info->private = l.private;
slouken@2873
   370
    return retval;
slouken@2873
   371
}
slouken@2873
   372
slouken@2873
   373
static int
slouken@5408
   374
calculate_rate(XF86VidModeModeInfo * info)
slouken@2873
   375
{
slouken@2874
   376
    return (info->htotal
slouken@2874
   377
            && info->vtotal) ? (1000 * info->dotclock / (info->htotal *
slouken@2874
   378
                                                         info->vtotal)) : 0;
slouken@2873
   379
}
slouken@2873
   380
slouken@2873
   381
static void
slouken@2874
   382
save_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   383
{
slouken@5408
   384
    XF86VidModeGetModeInfo(display, data->screen,
slouken@5408
   385
                                    &data->saved_mode);
slouken@5408
   386
    XF86VidModeGetViewPort(display, data->screen,
slouken@5408
   387
                                    &data->saved_view.x,
slouken@5408
   388
                                    &data->saved_view.y);
slouken@2873
   389
}
slouken@2873
   390
slouken@4518
   391
/*
slouken@2873
   392
static void
slouken@2874
   393
restore_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   394
{
slouken@5408
   395
    XF86VidModeModeInfo mode;
slouken@2873
   396
slouken@5408
   397
    if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
slouken@2873
   398
        if (SDL_memcmp(&mode, &data->saved_mode, sizeof(mode)) != 0) {
slouken@5408
   399
            XF86VidModeSwitchToMode(display, data->screen, &data->saved_mode);
slouken@2873
   400
        }
slouken@2873
   401
    }
slouken@2873
   402
    if ((data->saved_view.x != 0) || (data->saved_view.y != 0)) {
slouken@5408
   403
        XF86VidModeSetViewPort(display, data->screen,
slouken@5408
   404
                                        data->saved_view.x,
slouken@5408
   405
                                        data->saved_view.y);
slouken@2873
   406
    }
slouken@2873
   407
}
slouken@4518
   408
*/
slouken@5408
   409
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   410
slouken@1950
   411
void
slouken@3500
   412
X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
slouken@1950
   413
{
slouken@2870
   414
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   415
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@2873
   416
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   417
    int xrandr_major, xrandr_minor;
slouken@2873
   418
    int nsizes, nrates;
slouken@2873
   419
    XRRScreenSize *sizes;
slouken@2873
   420
    short *rates;
slouken@2873
   421
#endif
slouken@5408
   422
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   423
    int vm_major, vm_minor;
slouken@2873
   424
    int nmodes;
slouken@5408
   425
    XF86VidModeModeInfo ** modes;
slouken@2873
   426
#endif
slouken@2873
   427
    int screen_w;
slouken@2873
   428
    int screen_h;
slouken@1950
   429
    SDL_DisplayMode mode;
slouken@2870
   430
slouken@2873
   431
    /* Unfortunately X11 requires the window to be created with the correct
slouken@2873
   432
     * visual and depth ahead of time, but the SDL API allows you to create
slouken@2873
   433
     * a window before setting the fullscreen display mode.  This means that
slouken@2873
   434
     * we have to use the same format for all windows and all display modes.
slouken@2873
   435
     * (or support recreating the window with a new visual behind the scenes)
slouken@2873
   436
     */
slouken@3500
   437
    mode.format = sdl_display->current_mode.format;
slouken@2873
   438
    mode.driverdata = NULL;
slouken@2873
   439
slouken@2873
   440
    data->use_xrandr = 0;
slouken@2873
   441
    data->use_vidmode = 0;
slouken@2873
   442
    screen_w = DisplayWidth(display, data->screen);
slouken@2873
   443
    screen_h = DisplayHeight(display, data->screen);
slouken@2873
   444
slouken@2873
   445
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   446
    if (data->use_xinerama) {
gabomdq@6331
   447
        /* Add the full (both screens combined) xinerama mode only on the display that starts at 0,0 */
gabomdq@6331
   448
        if (!data->xinerama_info.x_org && !data->xinerama_info.y_org &&
gabomdq@6331
   449
           (screen_w > data->xinerama_info.width || screen_h > data->xinerama_info.height)) {
gabomdq@6331
   450
            mode.w = screen_w;
gabomdq@6331
   451
            mode.h = screen_h;
slouken@2873
   452
            mode.refresh_rate = 0;
slouken@3500
   453
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   454
        }
gabomdq@6331
   455
gabomdq@6331
   456
        /* Add the head xinerama mode */
gabomdq@6331
   457
        mode.w = data->xinerama_info.width;
gabomdq@6331
   458
        mode.h = data->xinerama_info.height;
gabomdq@6331
   459
        mode.refresh_rate = 0;
gabomdq@6331
   460
        SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   461
    }
slouken@2873
   462
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   463
slouken@2873
   464
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   465
    /* XRandR */
slouken@2873
   466
    /* require at least XRandR v1.0 (arbitrary) */
slouken@2874
   467
    if (CheckXRandR(display, &xrandr_major, &xrandr_minor)
slouken@2874
   468
        && xrandr_major >= 1) {
slouken@2873
   469
#ifdef X11MODES_DEBUG
slouken@2873
   470
        fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
slouken@2873
   471
                xrandr_major, xrandr_minor);
slouken@2873
   472
#endif
slouken@2873
   473
slouken@2873
   474
        /* save the screen configuration since we must reference it
slouken@2873
   475
           each time we toggle modes.
slouken@2874
   476
         */
slouken@2874
   477
        data->screen_config =
slouken@2874
   478
            XRRGetScreenInfo(display, RootWindow(display, data->screen));
slouken@2873
   479
slouken@2873
   480
        /* retrieve the list of resolution */
slouken@2873
   481
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   482
        if (nsizes > 0) {
slouken@2873
   483
            int i, j;
slouken@2874
   484
            for (i = 0; i < nsizes; i++) {
slouken@2873
   485
                mode.w = sizes[i].width;
slouken@2873
   486
                mode.h = sizes[i].height;
slouken@2873
   487
slouken@2873
   488
                rates = XRRConfigRates(data->screen_config, i, &nrates);
slouken@2873
   489
                for (j = 0; j < nrates; ++j) {
slouken@2873
   490
                    mode.refresh_rate = rates[j];
slouken@2873
   491
#ifdef X11MODES_DEBUG
slouken@2874
   492
                    fprintf(stderr,
slouken@2874
   493
                            "XRANDR: mode = %4d[%d], w = %4d, h = %4d, rate = %4d\n",
slouken@2873
   494
                            i, j, mode.w, mode.h, mode.refresh_rate);
slouken@2873
   495
#endif
slouken@3500
   496
                    SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   497
                }
slouken@2873
   498
            }
slouken@2873
   499
slouken@2873
   500
            data->use_xrandr = xrandr_major * 100 + xrandr_minor;
slouken@2874
   501
            data->saved_size =
slouken@2874
   502
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   503
                                              &data->saved_rotation);
slouken@2873
   504
            data->saved_rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   505
        }
slouken@2873
   506
    }
slouken@2873
   507
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   508
slouken@5408
   509
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   510
    /* XVidMode */
slouken@2873
   511
    if (!data->use_xrandr &&
slouken@2873
   512
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   513
        (!data->use_xinerama || data->xinerama_info.screen_number == 0) &&
slouken@2873
   514
#endif
slouken@2873
   515
        CheckVidMode(display, &vm_major, &vm_minor) &&
slouken@5408
   516
        XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   517
        int i;
slouken@2873
   518
slouken@2873
   519
#ifdef X11MODES_DEBUG
slouken@2873
   520
        printf("VidMode modes: (unsorted)\n");
slouken@2873
   521
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   522
            printf("Mode %d: %d x %d @ %d\n", i,
slouken@2874
   523
                   modes[i]->hdisplay, modes[i]->vdisplay,
slouken@2874
   524
                   calculate_rate(modes[i]));
slouken@2873
   525
        }
slouken@2873
   526
#endif
slouken@2873
   527
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   528
            mode.w = modes[i]->hdisplay;
slouken@2873
   529
            mode.h = modes[i]->vdisplay;
slouken@2873
   530
            mode.refresh_rate = calculate_rate(modes[i]);
slouken@3500
   531
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   532
        }
slouken@2873
   533
        XFree(modes);
slouken@2873
   534
slouken@2873
   535
        data->use_vidmode = vm_major * 100 + vm_minor;
slouken@2873
   536
        save_mode(display, data);
slouken@2873
   537
    }
slouken@5408
   538
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   539
slouken@2873
   540
    if (!data->use_xrandr && !data->use_vidmode) {
slouken@2873
   541
        mode.w = screen_w;
slouken@2873
   542
        mode.h = screen_h;
slouken@2873
   543
        mode.refresh_rate = 0;
slouken@3500
   544
        SDL_AddDisplayMode(sdl_display, &mode);
slouken@2874
   545
    }
slouken@2873
   546
#ifdef X11MODES_DEBUG
slouken@2873
   547
    if (data->use_xinerama) {
slouken@2873
   548
        printf("Xinerama is enabled\n");
slouken@2873
   549
    }
slouken@2873
   550
slouken@2873
   551
    if (data->use_xrandr) {
slouken@2873
   552
        printf("XRandR is enabled\n");
slouken@2873
   553
    }
slouken@2873
   554
slouken@2873
   555
    if (data->use_vidmode) {
slouken@2873
   556
        printf("VidMode is enabled\n");
slouken@2873
   557
    }
slouken@2873
   558
#endif /* X11MODES_DEBUG */
slouken@2873
   559
}
slouken@2873
   560
slouken@2873
   561
static void
slouken@2874
   562
get_real_resolution(Display * display, SDL_DisplayData * data, int *w, int *h,
slouken@2874
   563
                    int *rate)
slouken@2873
   564
{
gabomdq@6331
   565
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   566
    if (data->use_xinerama) {
gabomdq@6331
   567
        *w = data->xinerama_info.width;
gabomdq@6331
   568
        *h = data->xinerama_info.height;
gabomdq@6331
   569
        *rate = 0;
gabomdq@6331
   570
        return;
gabomdq@6331
   571
    }
gabomdq@6331
   572
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
gabomdq@6331
   573
slouken@2873
   574
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   575
    if (data->use_xrandr) {
slouken@2873
   576
        int nsizes;
slouken@2873
   577
        XRRScreenSize *sizes;
slouken@2874
   578
slouken@2873
   579
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   580
        if (nsizes > 0) {
slouken@2873
   581
            int cur_size;
slouken@2873
   582
            Rotation cur_rotation;
slouken@2873
   583
slouken@2874
   584
            cur_size =
slouken@2874
   585
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   586
                                              &cur_rotation);
slouken@2873
   587
            *w = sizes[cur_size].width;
slouken@2873
   588
            *h = sizes[cur_size].height;
slouken@2873
   589
            *rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   590
#ifdef X11MODES_DEBUG
slouken@2874
   591
            fprintf(stderr,
slouken@2874
   592
                    "XRANDR: get_real_resolution: w = %d, h = %d, rate = %d\n",
slouken@2874
   593
                    *w, *h, *rate);
slouken@2873
   594
#endif
slouken@2873
   595
            return;
slouken@2873
   596
        }
slouken@2873
   597
    }
slouken@2873
   598
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   599
slouken@5408
   600
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   601
    if (data->use_vidmode) {
slouken@5408
   602
        XF86VidModeModeInfo mode;
slouken@2873
   603
slouken@5408
   604
        if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
slouken@2873
   605
            *w = mode.hdisplay;
slouken@2873
   606
            *h = mode.vdisplay;
slouken@2873
   607
            *rate = calculate_rate(&mode);
slouken@2873
   608
            return;
slouken@2873
   609
        }
slouken@2873
   610
    }
slouken@5408
   611
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   612
slouken@2873
   613
    *w = DisplayWidth(display, data->screen);
slouken@2873
   614
    *h = DisplayHeight(display, data->screen);
slouken@2873
   615
    *rate = 0;
slouken@2873
   616
}
slouken@2873
   617
slouken@2873
   618
static void
slouken@2874
   619
set_best_resolution(Display * display, SDL_DisplayData * data, int w, int h,
slouken@2874
   620
                    int rate)
slouken@2873
   621
{
slouken@2873
   622
    int real_w, real_h, real_rate;
slouken@2873
   623
slouken@2873
   624
    /* check current mode so we can avoid uneccessary mode changes */
slouken@2873
   625
    get_real_resolution(display, data, &real_w, &real_h, &real_rate);
gabomdq@6331
   626
gabomdq@6331
   627
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   628
    if (w == real_w && h == real_h && (data->use_xinerama || !rate || rate == real_rate)) {
gabomdq@6331
   629
        return;
gabomdq@6331
   630
    }
gabomdq@6331
   631
#else
slouken@2873
   632
    if (w == real_w && h == real_h && (!rate || rate == real_rate)) {
slouken@2873
   633
        return;
slouken@2873
   634
    }
gabomdq@6331
   635
#endif
gabomdq@6331
   636
slouken@2873
   637
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   638
    if (data->use_xrandr) {
slouken@2873
   639
#ifdef X11MODES_DEBUG
slouken@2873
   640
        fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
slouken@2873
   641
                w, h);
slouken@2873
   642
#endif
slouken@2873
   643
        int i, nsizes, nrates;
slouken@2873
   644
        int best;
slouken@2873
   645
        int best_rate;
slouken@2873
   646
        XRRScreenSize *sizes;
slouken@2873
   647
        short *rates;
slouken@2873
   648
slouken@2873
   649
        /* find the smallest resolution that is at least as big as the user requested */
slouken@2873
   650
        best = -1;
slouken@2873
   651
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   652
        for (i = 0; i < nsizes; ++i) {
slouken@2873
   653
            if (sizes[i].width < w || sizes[i].height < h) {
slouken@2873
   654
                continue;
slouken@2873
   655
            }
slouken@2873
   656
            if (sizes[i].width == w && sizes[i].height == h) {
slouken@2873
   657
                best = i;
slouken@2873
   658
                break;
slouken@2873
   659
            }
slouken@2873
   660
            if (best == -1 ||
slouken@2873
   661
                (sizes[i].width < sizes[best].width) ||
slouken@2874
   662
                (sizes[i].width == sizes[best].width
slouken@2874
   663
                 && sizes[i].height < sizes[best].height)) {
slouken@2873
   664
                best = i;
slouken@2873
   665
            }
slouken@2873
   666
        }
slouken@2873
   667
slouken@2873
   668
        if (best >= 0) {
slouken@2873
   669
            best_rate = 0;
slouken@2873
   670
            rates = XRRConfigRates(data->screen_config, best, &nrates);
slouken@2873
   671
            for (i = 0; i < nrates; ++i) {
slouken@2873
   672
                if (rates[i] == rate) {
slouken@2873
   673
                    best_rate = rate;
slouken@2873
   674
                    break;
slouken@2873
   675
                }
slouken@2873
   676
                if (!rate) {
slouken@2873
   677
                    /* Higher is better, right? */
slouken@2873
   678
                    if (rates[i] > best_rate) {
slouken@2873
   679
                        best_rate = rates[i];
slouken@2873
   680
                    }
slouken@2873
   681
                } else {
slouken@2874
   682
                    if (SDL_abs(rates[i] - rate) < SDL_abs(best_rate - rate)) {
slouken@2873
   683
                        best_rate = rates[i];
slouken@2873
   684
                    }
slouken@2873
   685
                }
slouken@2873
   686
            }
slouken@2874
   687
            XRRSetScreenConfigAndRate(display, data->screen_config,
slouken@2874
   688
                                      RootWindow(display, data->screen), best,
slouken@2874
   689
                                      data->saved_rotation, best_rate,
slouken@2874
   690
                                      CurrentTime);
slouken@2873
   691
        }
slouken@2873
   692
        return;
slouken@2873
   693
    }
slouken@2873
   694
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   695
slouken@5408
   696
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   697
    if (data->use_vidmode) {
slouken@5408
   698
        XF86VidModeModeInfo ** modes;
slouken@2873
   699
        int i, nmodes;
slouken@2873
   700
        int best;
slouken@2873
   701
slouken@5408
   702
        if (XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   703
            best = -1;
slouken@2873
   704
            for (i = 0; i < nmodes; ++i) {
slouken@2873
   705
                if (modes[i]->hdisplay < w || modes[i]->vdisplay < h) {
slouken@2873
   706
                    continue;
slouken@2873
   707
                }
slouken@2873
   708
                if (best == -1 ||
slouken@2873
   709
                    (modes[i]->hdisplay < modes[best]->hdisplay) ||
slouken@2874
   710
                    (modes[i]->hdisplay == modes[best]->hdisplay
slouken@2874
   711
                     && modes[i]->vdisplay < modes[best]->vdisplay)) {
slouken@2873
   712
                    best = i;
slouken@2873
   713
                    continue;
slouken@2873
   714
                }
slouken@2873
   715
                if ((modes[i]->hdisplay == modes[best]->hdisplay) &&
slouken@2873
   716
                    (modes[i]->vdisplay == modes[best]->vdisplay)) {
slouken@2873
   717
                    if (!rate) {
slouken@2873
   718
                        /* Higher is better, right? */
slouken@2874
   719
                        if (calculate_rate(modes[i]) >
slouken@2874
   720
                            calculate_rate(modes[best])) {
slouken@2873
   721
                            best = i;
slouken@2873
   722
                        }
slouken@2873
   723
                    } else {
slouken@2874
   724
                        if (SDL_abs(calculate_rate(modes[i]) - rate) <
slouken@2874
   725
                            SDL_abs(calculate_rate(modes[best]) - rate)) {
slouken@2873
   726
                            best = i;
slouken@2873
   727
                        }
slouken@2873
   728
                    }
slouken@2873
   729
                }
slouken@2873
   730
            }
slouken@2873
   731
            if (best >= 0) {
slouken@2873
   732
#ifdef X11MODES_DEBUG
slouken@2873
   733
                printf("Best Mode %d: %d x %d @ %d\n", best,
slouken@2874
   734
                       modes[best]->hdisplay, modes[best]->vdisplay,
slouken@2874
   735
                       calculate_rate(modes[best]));
slouken@2873
   736
#endif
slouken@5408
   737
                XF86VidModeSwitchToMode(display, data->screen, modes[best]);
slouken@2873
   738
            }
slouken@2873
   739
            XFree(modes);
slouken@2873
   740
        }
slouken@2873
   741
        return;
slouken@2873
   742
    }
slouken@5408
   743
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@1950
   744
}
slouken@1950
   745
slouken@1950
   746
int
slouken@3500
   747
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
slouken@1950
   748
{
slouken@2873
   749
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   750
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@2873
   751
slouken@2873
   752
    set_best_resolution(display, data, mode->w, mode->h, mode->refresh_rate);
slouken@2873
   753
    return 0;
slouken@1950
   754
}
slouken@1950
   755
slouken@1950
   756
void
slouken@1950
   757
X11_QuitModes(_THIS)
slouken@1950
   758
{
slouken@1950
   759
}
slouken@1950
   760
gabomdq@6331
   761
int
gabomdq@6331
   762
X11_GetDisplayBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
gabomdq@6331
   763
{
gabomdq@6331
   764
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
gabomdq@6331
   765
gabomdq@6331
   766
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   767
    if (data && data->use_xinerama) {
gabomdq@6331
   768
        rect->x = data->xinerama_info.x_org;
gabomdq@6331
   769
        rect->y = data->xinerama_info.y_org;
gabomdq@6331
   770
        rect->w = data->xinerama_info.width;
gabomdq@6331
   771
        rect->h = data->xinerama_info.height;
gabomdq@6331
   772
        return 0;
gabomdq@6331
   773
    }
gabomdq@6331
   774
#endif
gabomdq@6331
   775
    if (_this->windows) {
gabomdq@6331
   776
        rect->x = 0;
gabomdq@6331
   777
        rect->y = 0;
gabomdq@6331
   778
        rect->w = _this->windows->w;
gabomdq@6331
   779
        rect->h = _this->windows->h;
gabomdq@6331
   780
        return 0;
gabomdq@6331
   781
    }
gabomdq@6331
   782
    return -1;
gabomdq@6331
   783
}
gabomdq@6331
   784
slouken@5481
   785
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   786
slouken@1950
   787
/* vi: set ts=4 sw=4 expandtab: */