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