src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 28 Sep 2012 00:40:47 -0700
changeset 6482 94e3643928ed
parent 6475 71d39944810b
child 6502 f41a82de351e
permissions -rw-r--r--
Fixed bug where desktop resolution was never restored if Xinerama was enabled.
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
    }
slouken@6475
   155
    if (!xinerama) {
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@6468
   207
        displaydata = (SDL_DisplayData *) SDL_calloc(1, 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@6472
   273
    env = SDL_GetHint(SDL_HINT_VIDEO_X11_XINERAMA);
slouken@2873
   274
    if (env && !SDL_atoi(env)) {
slouken@6468
   275
#ifdef X11MODES_DEBUG
slouken@6472
   276
        printf("Xinerama disabled due to hint\n");
slouken@6468
   277
#endif
slouken@2873
   278
        return SDL_FALSE;
slouken@2873
   279
    }
slouken@2873
   280
slouken@5408
   281
    if (!SDL_X11_HAVE_XINERAMA) {
slouken@6468
   282
#ifdef X11MODES_DEBUG
slouken@6468
   283
        printf("Xinerama support not available\n");
slouken@6468
   284
#endif
slouken@5408
   285
        return SDL_FALSE;
slouken@5408
   286
    }
slouken@5408
   287
slouken@2873
   288
    /* Query the extension version */
icculus@6027
   289
    if (!XineramaQueryExtension(display, &event_base, &error_base) ||
icculus@6027
   290
        !XineramaQueryVersion(display, major, minor) ||
slouken@5408
   291
        !XineramaIsActive(display)) {
slouken@6468
   292
#ifdef X11MODES_DEBUG
slouken@6468
   293
        printf("Xinerama not active on the display\n");
slouken@6468
   294
#endif
slouken@2873
   295
        return SDL_FALSE;
slouken@2873
   296
    }
slouken@6468
   297
#ifdef X11MODES_DEBUG
slouken@6468
   298
    printf("Xinerama available!\n");
slouken@6468
   299
#endif
slouken@2873
   300
    return SDL_TRUE;
slouken@2873
   301
}
slouken@2873
   302
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   303
slouken@2873
   304
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   305
static SDL_bool
slouken@2874
   306
CheckXRandR(Display * display, int *major, int *minor)
slouken@2873
   307
{
slouken@2873
   308
    const char *env;
slouken@2873
   309
slouken@2873
   310
    /* Default the extension not available */
slouken@2873
   311
    *major = *minor = 0;
slouken@2873
   312
slouken@2873
   313
    /* Allow environment override */
slouken@6472
   314
    env = SDL_GetHint(SDL_HINT_VIDEO_X11_XRANDR);
slouken@2873
   315
    if (env && !SDL_atoi(env)) {
slouken@6468
   316
#ifdef X11MODES_DEBUG
slouken@6472
   317
        printf("XRandR disabled due to hint\n");
slouken@6468
   318
#endif
slouken@2873
   319
        return SDL_FALSE;
slouken@2873
   320
    }
slouken@2873
   321
slouken@2873
   322
    if (!SDL_X11_HAVE_XRANDR) {
slouken@6468
   323
#ifdef X11MODES_DEBUG
slouken@6468
   324
        printf("XRandR support not available\n");
slouken@6468
   325
#endif
slouken@2873
   326
        return SDL_FALSE;
slouken@2873
   327
    }
slouken@2873
   328
slouken@2873
   329
    /* Query the extension version */
slouken@2873
   330
    if (!XRRQueryVersion(display, major, minor)) {
slouken@6468
   331
#ifdef X11MODES_DEBUG
slouken@6468
   332
        printf("XRandR not active on the display\n");
slouken@6468
   333
#endif
slouken@2873
   334
        return SDL_FALSE;
slouken@2873
   335
    }
slouken@6468
   336
#ifdef X11MODES_DEBUG
slouken@6468
   337
    printf("XRandR available!\n");
slouken@6468
   338
#endif
slouken@2873
   339
    return SDL_TRUE;
slouken@2873
   340
}
slouken@2873
   341
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   342
slouken@5408
   343
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   344
static SDL_bool
slouken@2874
   345
CheckVidMode(Display * display, int *major, int *minor)
slouken@2873
   346
{
slouken@2873
   347
    const char *env;
slouken@2873
   348
slouken@2873
   349
    /* Default the extension not available */
slouken@2873
   350
    *major = *minor = 0;
slouken@2873
   351
slouken@2873
   352
    /* Allow environment override */
slouken@6472
   353
    env = SDL_GetHint(SDL_HINT_VIDEO_X11_XVIDMODE);
slouken@2873
   354
    if (env && !SDL_atoi(env)) {
slouken@6468
   355
#ifdef X11MODES_DEBUG
slouken@6472
   356
        printf("XVidMode disabled due to hint\n");
slouken@6468
   357
#endif
slouken@2873
   358
        return SDL_FALSE;
slouken@2873
   359
    }
slouken@2874
   360
slouken@5408
   361
    if (!SDL_X11_HAVE_XVIDMODE) {
slouken@6468
   362
#ifdef X11MODES_DEBUG
slouken@6468
   363
        printf("XVidMode support not available\n");
slouken@6468
   364
#endif
slouken@5408
   365
        return SDL_FALSE;
slouken@5408
   366
    }
slouken@5408
   367
slouken@2873
   368
    /* Query the extension version */
slouken@2873
   369
    vm_error = -1;
slouken@5408
   370
    if (!XF86VidModeQueryExtension(display, &vm_event, &vm_error)
slouken@5408
   371
        || !XF86VidModeQueryVersion(display, major, minor)) {
slouken@6468
   372
#ifdef X11MODES_DEBUG
slouken@6468
   373
        printf("XVidMode not active on the display\n");
slouken@6468
   374
#endif
slouken@2873
   375
        return SDL_FALSE;
slouken@2873
   376
    }
slouken@6468
   377
#ifdef X11MODES_DEBUG
slouken@6468
   378
    printf("XVidMode available!\n");
slouken@6468
   379
#endif
slouken@2873
   380
    return SDL_TRUE;
slouken@2873
   381
}
slouken@2873
   382
slouken@4518
   383
static
slouken@5408
   384
Bool XF86VidModeGetModeInfo(Display * dpy, int scr,
slouken@5408
   385
                                       XF86VidModeModeInfo* info)
slouken@2873
   386
{
slouken@2873
   387
    Bool retval;
slouken@2873
   388
    int dotclock;
slouken@5408
   389
    XF86VidModeModeLine l;
slouken@2873
   390
    SDL_zerop(info);
slouken@2873
   391
    SDL_zero(l);
slouken@5408
   392
    retval = XF86VidModeGetModeLine(dpy, scr, &dotclock, &l);
slouken@2873
   393
    info->dotclock = dotclock;
slouken@2873
   394
    info->hdisplay = l.hdisplay;
slouken@2873
   395
    info->hsyncstart = l.hsyncstart;
slouken@2873
   396
    info->hsyncend = l.hsyncend;
slouken@2873
   397
    info->htotal = l.htotal;
slouken@2873
   398
    info->hskew = l.hskew;
slouken@2873
   399
    info->vdisplay = l.vdisplay;
slouken@2873
   400
    info->vsyncstart = l.vsyncstart;
slouken@2873
   401
    info->vsyncend = l.vsyncend;
slouken@2873
   402
    info->vtotal = l.vtotal;
slouken@2873
   403
    info->flags = l.flags;
slouken@2873
   404
    info->privsize = l.privsize;
slouken@2873
   405
    info->private = l.private;
slouken@2873
   406
    return retval;
slouken@2873
   407
}
slouken@2873
   408
slouken@2873
   409
static int
slouken@5408
   410
calculate_rate(XF86VidModeModeInfo * info)
slouken@2873
   411
{
slouken@2874
   412
    return (info->htotal
slouken@2874
   413
            && info->vtotal) ? (1000 * info->dotclock / (info->htotal *
slouken@2874
   414
                                                         info->vtotal)) : 0;
slouken@2873
   415
}
slouken@2873
   416
slouken@2873
   417
static void
slouken@2874
   418
save_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   419
{
slouken@5408
   420
    XF86VidModeGetModeInfo(display, data->screen,
slouken@5408
   421
                                    &data->saved_mode);
slouken@5408
   422
    XF86VidModeGetViewPort(display, data->screen,
slouken@5408
   423
                                    &data->saved_view.x,
slouken@5408
   424
                                    &data->saved_view.y);
slouken@2873
   425
}
slouken@2873
   426
slouken@4518
   427
/*
slouken@2873
   428
static void
slouken@2874
   429
restore_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   430
{
slouken@5408
   431
    XF86VidModeModeInfo mode;
slouken@2873
   432
slouken@5408
   433
    if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
slouken@2873
   434
        if (SDL_memcmp(&mode, &data->saved_mode, sizeof(mode)) != 0) {
slouken@5408
   435
            XF86VidModeSwitchToMode(display, data->screen, &data->saved_mode);
slouken@2873
   436
        }
slouken@2873
   437
    }
slouken@2873
   438
    if ((data->saved_view.x != 0) || (data->saved_view.y != 0)) {
slouken@5408
   439
        XF86VidModeSetViewPort(display, data->screen,
slouken@5408
   440
                                        data->saved_view.x,
slouken@5408
   441
                                        data->saved_view.y);
slouken@2873
   442
    }
slouken@2873
   443
}
slouken@4518
   444
*/
slouken@5408
   445
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   446
slouken@1950
   447
void
slouken@3500
   448
X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
slouken@1950
   449
{
slouken@2870
   450
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   451
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@2873
   452
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   453
    int xrandr_major, xrandr_minor;
slouken@2873
   454
    int nsizes, nrates;
slouken@2873
   455
    XRRScreenSize *sizes;
slouken@2873
   456
    short *rates;
slouken@2873
   457
#endif
slouken@5408
   458
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   459
    int vm_major, vm_minor;
slouken@2873
   460
    int nmodes;
slouken@5408
   461
    XF86VidModeModeInfo ** modes;
slouken@2873
   462
#endif
slouken@2873
   463
    int screen_w;
slouken@2873
   464
    int screen_h;
slouken@1950
   465
    SDL_DisplayMode mode;
slouken@2870
   466
slouken@2873
   467
    /* Unfortunately X11 requires the window to be created with the correct
slouken@2873
   468
     * visual and depth ahead of time, but the SDL API allows you to create
slouken@2873
   469
     * a window before setting the fullscreen display mode.  This means that
slouken@2873
   470
     * we have to use the same format for all windows and all display modes.
slouken@2873
   471
     * (or support recreating the window with a new visual behind the scenes)
slouken@2873
   472
     */
slouken@3500
   473
    mode.format = sdl_display->current_mode.format;
slouken@2873
   474
    mode.driverdata = NULL;
slouken@2873
   475
slouken@2873
   476
    screen_w = DisplayWidth(display, data->screen);
slouken@2873
   477
    screen_h = DisplayHeight(display, data->screen);
slouken@2873
   478
slouken@2873
   479
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   480
    if (data->use_xinerama) {
gabomdq@6331
   481
        /* Add the full (both screens combined) xinerama mode only on the display that starts at 0,0 */
gabomdq@6331
   482
        if (!data->xinerama_info.x_org && !data->xinerama_info.y_org &&
gabomdq@6331
   483
           (screen_w > data->xinerama_info.width || screen_h > data->xinerama_info.height)) {
gabomdq@6331
   484
            mode.w = screen_w;
gabomdq@6331
   485
            mode.h = screen_h;
slouken@2873
   486
            mode.refresh_rate = 0;
slouken@3500
   487
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   488
        }
gabomdq@6331
   489
gabomdq@6331
   490
        /* Add the head xinerama mode */
gabomdq@6331
   491
        mode.w = data->xinerama_info.width;
gabomdq@6331
   492
        mode.h = data->xinerama_info.height;
gabomdq@6331
   493
        mode.refresh_rate = 0;
gabomdq@6331
   494
        SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   495
    }
slouken@2873
   496
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   497
slouken@2873
   498
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   499
    /* XRandR */
slouken@2873
   500
    /* require at least XRandR v1.0 (arbitrary) */
slouken@2874
   501
    if (CheckXRandR(display, &xrandr_major, &xrandr_minor)
slouken@2874
   502
        && xrandr_major >= 1) {
slouken@2873
   503
#ifdef X11MODES_DEBUG
slouken@2873
   504
        fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
slouken@2873
   505
                xrandr_major, xrandr_minor);
slouken@2873
   506
#endif
slouken@2873
   507
slouken@2873
   508
        /* save the screen configuration since we must reference it
slouken@2873
   509
           each time we toggle modes.
slouken@2874
   510
         */
slouken@2874
   511
        data->screen_config =
slouken@2874
   512
            XRRGetScreenInfo(display, RootWindow(display, data->screen));
slouken@2873
   513
slouken@2873
   514
        /* retrieve the list of resolution */
slouken@2873
   515
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   516
        if (nsizes > 0) {
slouken@2873
   517
            int i, j;
slouken@2874
   518
            for (i = 0; i < nsizes; i++) {
slouken@2873
   519
                mode.w = sizes[i].width;
slouken@2873
   520
                mode.h = sizes[i].height;
slouken@2873
   521
slouken@2873
   522
                rates = XRRConfigRates(data->screen_config, i, &nrates);
slouken@2873
   523
                for (j = 0; j < nrates; ++j) {
slouken@2873
   524
                    mode.refresh_rate = rates[j];
slouken@2873
   525
#ifdef X11MODES_DEBUG
slouken@2874
   526
                    fprintf(stderr,
slouken@2874
   527
                            "XRANDR: mode = %4d[%d], w = %4d, h = %4d, rate = %4d\n",
slouken@2873
   528
                            i, j, mode.w, mode.h, mode.refresh_rate);
slouken@2873
   529
#endif
slouken@3500
   530
                    SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   531
                }
slouken@2873
   532
            }
slouken@2873
   533
slouken@2873
   534
            data->use_xrandr = xrandr_major * 100 + xrandr_minor;
slouken@2874
   535
            data->saved_size =
slouken@2874
   536
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   537
                                              &data->saved_rotation);
slouken@2873
   538
            data->saved_rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   539
        }
slouken@2873
   540
    }
slouken@2873
   541
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   542
slouken@5408
   543
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   544
    /* XVidMode */
slouken@2873
   545
    if (!data->use_xrandr &&
slouken@2873
   546
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   547
        (!data->use_xinerama || data->xinerama_info.screen_number == 0) &&
slouken@2873
   548
#endif
slouken@2873
   549
        CheckVidMode(display, &vm_major, &vm_minor) &&
slouken@5408
   550
        XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   551
        int i;
slouken@2873
   552
slouken@2873
   553
#ifdef X11MODES_DEBUG
slouken@2873
   554
        printf("VidMode modes: (unsorted)\n");
slouken@2873
   555
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   556
            printf("Mode %d: %d x %d @ %d\n", i,
slouken@2874
   557
                   modes[i]->hdisplay, modes[i]->vdisplay,
slouken@2874
   558
                   calculate_rate(modes[i]));
slouken@2873
   559
        }
slouken@2873
   560
#endif
slouken@2873
   561
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   562
            mode.w = modes[i]->hdisplay;
slouken@2873
   563
            mode.h = modes[i]->vdisplay;
slouken@2873
   564
            mode.refresh_rate = calculate_rate(modes[i]);
slouken@3500
   565
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   566
        }
slouken@2873
   567
        XFree(modes);
slouken@2873
   568
slouken@2873
   569
        data->use_vidmode = vm_major * 100 + vm_minor;
slouken@2873
   570
        save_mode(display, data);
slouken@2873
   571
    }
slouken@5408
   572
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   573
slouken@2873
   574
    if (!data->use_xrandr && !data->use_vidmode) {
slouken@2873
   575
        mode.w = screen_w;
slouken@2873
   576
        mode.h = screen_h;
slouken@2873
   577
        mode.refresh_rate = 0;
slouken@3500
   578
        SDL_AddDisplayMode(sdl_display, &mode);
slouken@2874
   579
    }
slouken@2873
   580
#ifdef X11MODES_DEBUG
slouken@2873
   581
    if (data->use_xinerama) {
slouken@2873
   582
        printf("Xinerama is enabled\n");
slouken@2873
   583
    }
slouken@2873
   584
slouken@2873
   585
    if (data->use_xrandr) {
slouken@2873
   586
        printf("XRandR is enabled\n");
slouken@2873
   587
    }
slouken@2873
   588
slouken@2873
   589
    if (data->use_vidmode) {
slouken@2873
   590
        printf("VidMode is enabled\n");
slouken@2873
   591
    }
slouken@2873
   592
#endif /* X11MODES_DEBUG */
slouken@2873
   593
}
slouken@2873
   594
slouken@2873
   595
static void
slouken@2874
   596
get_real_resolution(Display * display, SDL_DisplayData * data, int *w, int *h,
slouken@2874
   597
                    int *rate)
slouken@2873
   598
{
slouken@2873
   599
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   600
    if (data->use_xrandr) {
slouken@2873
   601
        int nsizes;
slouken@2873
   602
        XRRScreenSize *sizes;
slouken@2874
   603
slouken@2873
   604
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   605
        if (nsizes > 0) {
slouken@2873
   606
            int cur_size;
slouken@2873
   607
            Rotation cur_rotation;
slouken@2873
   608
slouken@2874
   609
            cur_size =
slouken@2874
   610
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   611
                                              &cur_rotation);
slouken@2873
   612
            *w = sizes[cur_size].width;
slouken@2873
   613
            *h = sizes[cur_size].height;
slouken@2873
   614
            *rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   615
#ifdef X11MODES_DEBUG
slouken@2874
   616
            fprintf(stderr,
slouken@2874
   617
                    "XRANDR: get_real_resolution: w = %d, h = %d, rate = %d\n",
slouken@2874
   618
                    *w, *h, *rate);
slouken@2873
   619
#endif
slouken@2873
   620
            return;
slouken@2873
   621
        }
slouken@2873
   622
    }
slouken@2873
   623
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   624
slouken@5408
   625
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   626
    if (data->use_vidmode) {
slouken@5408
   627
        XF86VidModeModeInfo mode;
slouken@2873
   628
slouken@5408
   629
        if (XF86VidModeGetModeInfo(display, data->screen, &mode)) {
slouken@2873
   630
            *w = mode.hdisplay;
slouken@2873
   631
            *h = mode.vdisplay;
slouken@2873
   632
            *rate = calculate_rate(&mode);
slouken@2873
   633
            return;
slouken@2873
   634
        }
slouken@2873
   635
    }
slouken@5408
   636
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   637
slouken@6482
   638
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6482
   639
    if (data->use_xinerama) {
slouken@6482
   640
        *w = data->xinerama_info.width;
slouken@6482
   641
        *h = data->xinerama_info.height;
slouken@6482
   642
        *rate = 0;
slouken@6482
   643
        return;
slouken@6482
   644
    }
slouken@6482
   645
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6482
   646
slouken@2873
   647
    *w = DisplayWidth(display, data->screen);
slouken@2873
   648
    *h = DisplayHeight(display, data->screen);
slouken@2873
   649
    *rate = 0;
slouken@2873
   650
}
slouken@2873
   651
slouken@2873
   652
static void
slouken@2874
   653
set_best_resolution(Display * display, SDL_DisplayData * data, int w, int h,
slouken@2874
   654
                    int rate)
slouken@2873
   655
{
slouken@2873
   656
    int real_w, real_h, real_rate;
slouken@2873
   657
slouken@2873
   658
    /* check current mode so we can avoid uneccessary mode changes */
slouken@2873
   659
    get_real_resolution(display, data, &real_w, &real_h, &real_rate);
gabomdq@6331
   660
slouken@6482
   661
    if (w == real_w && h == real_h && (!rate || !real_rate || rate == real_rate)) {
gabomdq@6331
   662
        return;
gabomdq@6331
   663
    }
gabomdq@6331
   664
slouken@2873
   665
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   666
    if (data->use_xrandr) {
slouken@2873
   667
#ifdef X11MODES_DEBUG
slouken@2873
   668
        fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
slouken@2873
   669
                w, h);
slouken@2873
   670
#endif
slouken@2873
   671
        int i, nsizes, nrates;
slouken@2873
   672
        int best;
slouken@2873
   673
        int best_rate;
slouken@2873
   674
        XRRScreenSize *sizes;
slouken@2873
   675
        short *rates;
slouken@2873
   676
slouken@2873
   677
        /* find the smallest resolution that is at least as big as the user requested */
slouken@2873
   678
        best = -1;
slouken@2873
   679
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   680
        for (i = 0; i < nsizes; ++i) {
slouken@2873
   681
            if (sizes[i].width < w || sizes[i].height < h) {
slouken@2873
   682
                continue;
slouken@2873
   683
            }
slouken@2873
   684
            if (sizes[i].width == w && sizes[i].height == h) {
slouken@2873
   685
                best = i;
slouken@2873
   686
                break;
slouken@2873
   687
            }
slouken@2873
   688
            if (best == -1 ||
slouken@2873
   689
                (sizes[i].width < sizes[best].width) ||
slouken@2874
   690
                (sizes[i].width == sizes[best].width
slouken@2874
   691
                 && sizes[i].height < sizes[best].height)) {
slouken@2873
   692
                best = i;
slouken@2873
   693
            }
slouken@2873
   694
        }
slouken@2873
   695
slouken@2873
   696
        if (best >= 0) {
slouken@2873
   697
            best_rate = 0;
slouken@2873
   698
            rates = XRRConfigRates(data->screen_config, best, &nrates);
slouken@2873
   699
            for (i = 0; i < nrates; ++i) {
slouken@2873
   700
                if (rates[i] == rate) {
slouken@2873
   701
                    best_rate = rate;
slouken@2873
   702
                    break;
slouken@2873
   703
                }
slouken@2873
   704
                if (!rate) {
slouken@2873
   705
                    /* Higher is better, right? */
slouken@2873
   706
                    if (rates[i] > best_rate) {
slouken@2873
   707
                        best_rate = rates[i];
slouken@2873
   708
                    }
slouken@2873
   709
                } else {
slouken@2874
   710
                    if (SDL_abs(rates[i] - rate) < SDL_abs(best_rate - rate)) {
slouken@2873
   711
                        best_rate = rates[i];
slouken@2873
   712
                    }
slouken@2873
   713
                }
slouken@2873
   714
            }
slouken@2874
   715
            XRRSetScreenConfigAndRate(display, data->screen_config,
slouken@2874
   716
                                      RootWindow(display, data->screen), best,
slouken@2874
   717
                                      data->saved_rotation, best_rate,
slouken@2874
   718
                                      CurrentTime);
slouken@2873
   719
        }
slouken@2873
   720
        return;
slouken@2873
   721
    }
slouken@2873
   722
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   723
slouken@5408
   724
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   725
    if (data->use_vidmode) {
slouken@5408
   726
        XF86VidModeModeInfo ** modes;
slouken@2873
   727
        int i, nmodes;
slouken@2873
   728
        int best;
slouken@2873
   729
slouken@5408
   730
        if (XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   731
            best = -1;
slouken@2873
   732
            for (i = 0; i < nmodes; ++i) {
slouken@2873
   733
                if (modes[i]->hdisplay < w || modes[i]->vdisplay < h) {
slouken@2873
   734
                    continue;
slouken@2873
   735
                }
slouken@2873
   736
                if (best == -1 ||
slouken@2873
   737
                    (modes[i]->hdisplay < modes[best]->hdisplay) ||
slouken@2874
   738
                    (modes[i]->hdisplay == modes[best]->hdisplay
slouken@2874
   739
                     && modes[i]->vdisplay < modes[best]->vdisplay)) {
slouken@2873
   740
                    best = i;
slouken@2873
   741
                    continue;
slouken@2873
   742
                }
slouken@2873
   743
                if ((modes[i]->hdisplay == modes[best]->hdisplay) &&
slouken@2873
   744
                    (modes[i]->vdisplay == modes[best]->vdisplay)) {
slouken@2873
   745
                    if (!rate) {
slouken@2873
   746
                        /* Higher is better, right? */
slouken@2874
   747
                        if (calculate_rate(modes[i]) >
slouken@2874
   748
                            calculate_rate(modes[best])) {
slouken@2873
   749
                            best = i;
slouken@2873
   750
                        }
slouken@2873
   751
                    } else {
slouken@2874
   752
                        if (SDL_abs(calculate_rate(modes[i]) - rate) <
slouken@2874
   753
                            SDL_abs(calculate_rate(modes[best]) - rate)) {
slouken@2873
   754
                            best = i;
slouken@2873
   755
                        }
slouken@2873
   756
                    }
slouken@2873
   757
                }
slouken@2873
   758
            }
slouken@2873
   759
            if (best >= 0) {
slouken@2873
   760
#ifdef X11MODES_DEBUG
slouken@2873
   761
                printf("Best Mode %d: %d x %d @ %d\n", best,
slouken@2874
   762
                       modes[best]->hdisplay, modes[best]->vdisplay,
slouken@2874
   763
                       calculate_rate(modes[best]));
slouken@2873
   764
#endif
slouken@5408
   765
                XF86VidModeSwitchToMode(display, data->screen, modes[best]);
slouken@2873
   766
            }
slouken@2873
   767
            XFree(modes);
slouken@2873
   768
        }
slouken@2873
   769
        return;
slouken@2873
   770
    }
slouken@5408
   771
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@1950
   772
}
slouken@1950
   773
slouken@1950
   774
int
slouken@3500
   775
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
slouken@1950
   776
{
slouken@2873
   777
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   778
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@2873
   779
slouken@2873
   780
    set_best_resolution(display, data, mode->w, mode->h, mode->refresh_rate);
slouken@2873
   781
    return 0;
slouken@1950
   782
}
slouken@1950
   783
slouken@1950
   784
void
slouken@1950
   785
X11_QuitModes(_THIS)
slouken@1950
   786
{
slouken@1950
   787
}
slouken@1950
   788
gabomdq@6331
   789
int
gabomdq@6331
   790
X11_GetDisplayBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
gabomdq@6331
   791
{
gabomdq@6331
   792
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
gabomdq@6331
   793
gabomdq@6331
   794
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   795
    if (data && data->use_xinerama) {
gabomdq@6331
   796
        rect->x = data->xinerama_info.x_org;
gabomdq@6331
   797
        rect->y = data->xinerama_info.y_org;
gabomdq@6331
   798
        rect->w = data->xinerama_info.width;
gabomdq@6331
   799
        rect->h = data->xinerama_info.height;
gabomdq@6331
   800
        return 0;
gabomdq@6331
   801
    }
gabomdq@6331
   802
#endif
slouken@6475
   803
    rect->x = 0;
slouken@6475
   804
    rect->y = 0;
slouken@6475
   805
    rect->w = sdl_display->current_mode.w;
slouken@6475
   806
    rect->h = sdl_display->current_mode.h;
slouken@6475
   807
    return 0;
gabomdq@6331
   808
}
gabomdq@6331
   809
slouken@5481
   810
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   811
slouken@1950
   812
/* vi: set ts=4 sw=4 expandtab: */