src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Oct 2012 19:02:46 -0700
changeset 6549 db16b819e1d1
parent 6548 fc9e6be24ca9
child 6550 191f00a080ba
permissions -rw-r--r--
Added a delay hack to let the window manager adjust to the mode change. I'm not sure if this is a good idea, but at least it works sometimes now. :)
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@6538
    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
}
slouken@1950
   135
slouken@2873
   136
/* Global for the error handler */
slouken@2873
   137
int vm_event, vm_error = -1;
slouken@2873
   138
slouken@2873
   139
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   140
static SDL_bool
slouken@2874
   141
CheckXinerama(Display * display, int *major, int *minor)
slouken@2873
   142
{
icculus@6027
   143
    int event_base = 0;
icculus@6027
   144
    int error_base = 0;
slouken@2873
   145
    const char *env;
slouken@2873
   146
slouken@2873
   147
    /* Default the extension not available */
slouken@2873
   148
    *major = *minor = 0;
slouken@2873
   149
slouken@2873
   150
    /* Allow environment override */
slouken@6472
   151
    env = SDL_GetHint(SDL_HINT_VIDEO_X11_XINERAMA);
slouken@2873
   152
    if (env && !SDL_atoi(env)) {
slouken@6468
   153
#ifdef X11MODES_DEBUG
slouken@6472
   154
        printf("Xinerama disabled due to hint\n");
slouken@6468
   155
#endif
slouken@2873
   156
        return SDL_FALSE;
slouken@2873
   157
    }
slouken@2873
   158
slouken@5408
   159
    if (!SDL_X11_HAVE_XINERAMA) {
slouken@6468
   160
#ifdef X11MODES_DEBUG
slouken@6468
   161
        printf("Xinerama support not available\n");
slouken@6468
   162
#endif
slouken@5408
   163
        return SDL_FALSE;
slouken@5408
   164
    }
slouken@5408
   165
slouken@2873
   166
    /* Query the extension version */
icculus@6027
   167
    if (!XineramaQueryExtension(display, &event_base, &error_base) ||
icculus@6027
   168
        !XineramaQueryVersion(display, major, minor) ||
slouken@5408
   169
        !XineramaIsActive(display)) {
slouken@6468
   170
#ifdef X11MODES_DEBUG
slouken@6468
   171
        printf("Xinerama not active on the display\n");
slouken@6468
   172
#endif
slouken@2873
   173
        return SDL_FALSE;
slouken@2873
   174
    }
slouken@6468
   175
#ifdef X11MODES_DEBUG
slouken@6548
   176
    printf("Xinerama available at version %d.%d!\n", *major, *minor);
slouken@6468
   177
#endif
slouken@2873
   178
    return SDL_TRUE;
slouken@2873
   179
}
slouken@2873
   180
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   181
slouken@2873
   182
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   183
static SDL_bool
slouken@2874
   184
CheckXRandR(Display * display, int *major, int *minor)
slouken@2873
   185
{
slouken@2873
   186
    const char *env;
slouken@2873
   187
slouken@2873
   188
    /* Default the extension not available */
slouken@2873
   189
    *major = *minor = 0;
slouken@2873
   190
slouken@2873
   191
    /* Allow environment override */
slouken@6472
   192
    env = SDL_GetHint(SDL_HINT_VIDEO_X11_XRANDR);
slouken@2873
   193
    if (env && !SDL_atoi(env)) {
slouken@6468
   194
#ifdef X11MODES_DEBUG
slouken@6472
   195
        printf("XRandR disabled due to hint\n");
slouken@6468
   196
#endif
slouken@2873
   197
        return SDL_FALSE;
slouken@2873
   198
    }
slouken@2873
   199
slouken@2873
   200
    if (!SDL_X11_HAVE_XRANDR) {
slouken@6468
   201
#ifdef X11MODES_DEBUG
slouken@6468
   202
        printf("XRandR support not available\n");
slouken@6468
   203
#endif
slouken@2873
   204
        return SDL_FALSE;
slouken@2873
   205
    }
slouken@2873
   206
slouken@2873
   207
    /* Query the extension version */
slouken@2873
   208
    if (!XRRQueryVersion(display, major, minor)) {
slouken@6468
   209
#ifdef X11MODES_DEBUG
slouken@6468
   210
        printf("XRandR not active on the display\n");
slouken@6468
   211
#endif
slouken@2873
   212
        return SDL_FALSE;
slouken@2873
   213
    }
slouken@6468
   214
#ifdef X11MODES_DEBUG
slouken@6548
   215
    printf("XRandR available at version %d.%d!\n", *major, *minor);
slouken@6468
   216
#endif
slouken@2873
   217
    return SDL_TRUE;
slouken@2873
   218
}
slouken@6537
   219
slouken@6537
   220
#define XRANDR_ROTATION_LEFT    (1 << 1)
slouken@6537
   221
#define XRANDR_ROTATION_RIGHT   (1 << 3)
slouken@6537
   222
slouken@6548
   223
static int
slouken@6548
   224
CalculateXRandRRefreshRate(const XRRModeInfo *info)
slouken@6548
   225
{
slouken@6548
   226
    return (info->hTotal
slouken@6548
   227
            && info->vTotal) ? (1000 * info->dotClock / (info->hTotal *
slouken@6548
   228
                                                         info->vTotal)) : 0;
slouken@6548
   229
}
slouken@6548
   230
slouken@6548
   231
static SDL_bool
slouken@6548
   232
SetXRandRModeInfo(Display *display, XRRScreenResources *res, XRROutputInfo *output_info,
slouken@6548
   233
                  RRMode modeID, SDL_DisplayMode *mode)
slouken@6537
   234
{
slouken@6548
   235
    int i;
slouken@6548
   236
    for (i = 0; i < res->nmode; ++i) {
slouken@6548
   237
        if (res->modes[i].id == modeID) {
slouken@6548
   238
            XRRCrtcInfo *crtc;
slouken@6548
   239
            Rotation rotation = 0;
slouken@6548
   240
            const XRRModeInfo *info = &res->modes[i];
slouken@6548
   241
slouken@6548
   242
            crtc = XRRGetCrtcInfo(display, res, output_info->crtc);
slouken@6548
   243
            if (crtc) {
slouken@6548
   244
                rotation = crtc->rotation;
slouken@6548
   245
                XRRFreeCrtcInfo(crtc);
slouken@6548
   246
            }
slouken@6548
   247
slouken@6548
   248
            if (rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT)) {
slouken@6548
   249
                mode->w = info->height;
slouken@6548
   250
                mode->h = info->width;
slouken@6548
   251
            } else {
slouken@6548
   252
                mode->w = info->width;
slouken@6548
   253
                mode->h = info->height;
slouken@6548
   254
            }
slouken@6548
   255
            mode->refresh_rate = CalculateXRandRRefreshRate(info);
slouken@6548
   256
            ((SDL_DisplayModeData*)mode->driverdata)->xrandr_mode = modeID;
slouken@6548
   257
#ifdef X11MODES_DEBUG
slouken@6548
   258
            printf("XRandR mode %d: %dx%d@%dHz\n", modeID, mode->w, mode->h, mode->refresh_rate);
slouken@6548
   259
#endif
slouken@6548
   260
            return SDL_TRUE;
slouken@6548
   261
        }
slouken@6537
   262
    }
slouken@6548
   263
    return SDL_FALSE;
slouken@6537
   264
}
slouken@2873
   265
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   266
slouken@5408
   267
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   268
static SDL_bool
slouken@2874
   269
CheckVidMode(Display * display, int *major, int *minor)
slouken@2873
   270
{
slouken@2873
   271
    const char *env;
slouken@2873
   272
slouken@2873
   273
    /* Default the extension not available */
slouken@2873
   274
    *major = *minor = 0;
slouken@2873
   275
slouken@2873
   276
    /* Allow environment override */
slouken@6472
   277
    env = SDL_GetHint(SDL_HINT_VIDEO_X11_XVIDMODE);
slouken@2873
   278
    if (env && !SDL_atoi(env)) {
slouken@6468
   279
#ifdef X11MODES_DEBUG
slouken@6472
   280
        printf("XVidMode disabled due to hint\n");
slouken@6468
   281
#endif
slouken@2873
   282
        return SDL_FALSE;
slouken@2873
   283
    }
slouken@2874
   284
slouken@5408
   285
    if (!SDL_X11_HAVE_XVIDMODE) {
slouken@6468
   286
#ifdef X11MODES_DEBUG
slouken@6468
   287
        printf("XVidMode support not available\n");
slouken@6468
   288
#endif
slouken@5408
   289
        return SDL_FALSE;
slouken@5408
   290
    }
slouken@5408
   291
slouken@2873
   292
    /* Query the extension version */
slouken@2873
   293
    vm_error = -1;
slouken@5408
   294
    if (!XF86VidModeQueryExtension(display, &vm_event, &vm_error)
slouken@5408
   295
        || !XF86VidModeQueryVersion(display, major, minor)) {
slouken@6468
   296
#ifdef X11MODES_DEBUG
slouken@6468
   297
        printf("XVidMode not active on the display\n");
slouken@6468
   298
#endif
slouken@2873
   299
        return SDL_FALSE;
slouken@2873
   300
    }
slouken@6468
   301
#ifdef X11MODES_DEBUG
slouken@6548
   302
    printf("XVidMode available at version %d.%d!\n", *major, *minor);
slouken@6468
   303
#endif
slouken@2873
   304
    return SDL_TRUE;
slouken@2873
   305
}
slouken@2873
   306
slouken@4518
   307
static
slouken@5408
   308
Bool XF86VidModeGetModeInfo(Display * dpy, int scr,
slouken@5408
   309
                                       XF86VidModeModeInfo* info)
slouken@2873
   310
{
slouken@2873
   311
    Bool retval;
slouken@2873
   312
    int dotclock;
slouken@5408
   313
    XF86VidModeModeLine l;
slouken@2873
   314
    SDL_zerop(info);
slouken@2873
   315
    SDL_zero(l);
slouken@5408
   316
    retval = XF86VidModeGetModeLine(dpy, scr, &dotclock, &l);
slouken@2873
   317
    info->dotclock = dotclock;
slouken@2873
   318
    info->hdisplay = l.hdisplay;
slouken@2873
   319
    info->hsyncstart = l.hsyncstart;
slouken@2873
   320
    info->hsyncend = l.hsyncend;
slouken@2873
   321
    info->htotal = l.htotal;
slouken@2873
   322
    info->hskew = l.hskew;
slouken@2873
   323
    info->vdisplay = l.vdisplay;
slouken@2873
   324
    info->vsyncstart = l.vsyncstart;
slouken@2873
   325
    info->vsyncend = l.vsyncend;
slouken@2873
   326
    info->vtotal = l.vtotal;
slouken@2873
   327
    info->flags = l.flags;
slouken@2873
   328
    info->privsize = l.privsize;
slouken@2873
   329
    info->private = l.private;
slouken@2873
   330
    return retval;
slouken@2873
   331
}
slouken@2873
   332
slouken@2873
   333
static int
slouken@6548
   334
CalculateXVidModeRefreshRate(const XF86VidModeModeInfo * info)
slouken@2873
   335
{
slouken@2874
   336
    return (info->htotal
slouken@2874
   337
            && info->vtotal) ? (1000 * info->dotclock / (info->htotal *
slouken@2874
   338
                                                         info->vtotal)) : 0;
slouken@2873
   339
}
slouken@2873
   340
slouken@6548
   341
SDL_bool
slouken@6548
   342
SetXVidModeModeInfo(const XF86VidModeModeInfo *info, SDL_DisplayMode *mode)
slouken@2873
   343
{
slouken@6548
   344
    mode->w = info->hdisplay;
slouken@6548
   345
    mode->h = info->vdisplay;
slouken@6548
   346
    mode->refresh_rate = CalculateXVidModeRefreshRate(info);
slouken@6548
   347
    ((SDL_DisplayModeData*)mode->driverdata)->vm_mode = *info;
slouken@6548
   348
    return SDL_TRUE;
slouken@2873
   349
}
slouken@6548
   350
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   351
slouken@6548
   352
int
slouken@6548
   353
X11_InitModes(_THIS)
slouken@2873
   354
{
slouken@6548
   355
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@6548
   356
    int screen, screencount;
slouken@6548
   357
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   358
    int xinerama_major, xinerama_minor;
slouken@6548
   359
    int use_xinerama = 0;
slouken@6548
   360
    XineramaScreenInfo *xinerama = NULL;
slouken@6548
   361
#endif
slouken@6548
   362
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   363
    int xrandr_major, xrandr_minor;
slouken@6548
   364
    int use_xrandr = 0;
slouken@6548
   365
    XRRScreenResources *res = NULL;
slouken@6548
   366
#endif
slouken@6548
   367
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   368
    int vm_major, vm_minor;
slouken@6548
   369
    int use_vidmode = 0;
slouken@6548
   370
#endif
slouken@2873
   371
slouken@6548
   372
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   373
    /* Query Xinerama extention
slouken@6548
   374
     * NOTE: This works with Nvidia Twinview correctly, but you need version 302.17 (released on June 2012)
slouken@6548
   375
     *       or newer of the Nvidia binary drivers
slouken@6548
   376
     */
slouken@6548
   377
    if (CheckXinerama(data->display, &xinerama_major, &xinerama_minor)) {
slouken@6548
   378
        xinerama = XineramaQueryScreens(data->display, &screencount);
slouken@6548
   379
        if (xinerama) {
slouken@6548
   380
            use_xinerama = xinerama_major * 100 + xinerama_minor;
slouken@2873
   381
        }
slouken@2873
   382
    }
slouken@6548
   383
    if (!xinerama) {
slouken@6548
   384
        screencount = ScreenCount(data->display);
slouken@6548
   385
    }
slouken@6548
   386
#else
slouken@6548
   387
    screencount = ScreenCount(data->display);
slouken@6548
   388
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6548
   389
slouken@6548
   390
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   391
    /* require at least XRandR v1.2 */
slouken@6548
   392
    if (CheckXRandR(data->display, &xrandr_major, &xrandr_minor) &&
slouken@6548
   393
        (xrandr_major >= 2 || (xrandr_major == 1 && xrandr_minor >= 2))) {
slouken@6548
   394
        use_xrandr = xrandr_major * 100 + xrandr_minor;
slouken@6548
   395
    }
slouken@6548
   396
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
   397
slouken@6548
   398
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   399
    if (CheckVidMode(data->display, &vm_major, &vm_minor)) {
slouken@6548
   400
        use_vidmode = vm_major * 100 + vm_minor;
slouken@2873
   401
    }
slouken@6548
   402
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   403
slouken@6548
   404
    for (screen = 0; screen < screencount; ++screen) {
slouken@6548
   405
        XVisualInfo vinfo;
slouken@6548
   406
        SDL_VideoDisplay display;
slouken@6548
   407
        SDL_DisplayData *displaydata;
slouken@6548
   408
        SDL_DisplayMode mode;
slouken@6548
   409
        SDL_DisplayModeData *modedata;
slouken@6548
   410
        XPixmapFormatValues *pixmapFormats;
slouken@6548
   411
        int i, n;
slouken@6548
   412
slouken@6548
   413
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   414
        if (xinerama) {
slouken@6548
   415
            if (get_visualinfo(data->display, 0, &vinfo) < 0) {
slouken@6548
   416
                continue;
slouken@6548
   417
            }
slouken@6548
   418
        } else {
slouken@6548
   419
            if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@6548
   420
                continue;
slouken@6548
   421
            }
slouken@6548
   422
        }
slouken@6548
   423
#else
slouken@6548
   424
        if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@6548
   425
            continue;
slouken@6548
   426
        }
slouken@6548
   427
#endif
slouken@6548
   428
slouken@6548
   429
        displaydata = (SDL_DisplayData *) SDL_calloc(1, sizeof(*displaydata));
slouken@6548
   430
        if (!displaydata) {
slouken@6548
   431
            continue;
slouken@6548
   432
        }
slouken@6548
   433
slouken@6548
   434
        mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
slouken@6548
   435
        if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
slouken@6548
   436
            /* We don't support palettized modes now */
slouken@6548
   437
            SDL_free(displaydata);
slouken@6548
   438
            continue;
slouken@6548
   439
        }
slouken@6548
   440
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   441
        if (xinerama) {
slouken@6548
   442
            mode.w = xinerama[screen].width;
slouken@6548
   443
            mode.h = xinerama[screen].height;
slouken@6548
   444
        } else {
slouken@6548
   445
            mode.w = DisplayWidth(data->display, screen);
slouken@6548
   446
            mode.h = DisplayHeight(data->display, screen);
slouken@6548
   447
        }
slouken@6548
   448
#else
slouken@6548
   449
        mode.w = DisplayWidth(data->display, screen);
slouken@6548
   450
        mode.h = DisplayHeight(data->display, screen);
slouken@6548
   451
#endif
slouken@6548
   452
        mode.refresh_rate = 0;
slouken@6548
   453
slouken@6548
   454
        modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   455
        if (!modedata) {
slouken@6548
   456
            SDL_free(displaydata);
slouken@6548
   457
            continue;
slouken@6548
   458
        }
slouken@6548
   459
        mode.driverdata = modedata;
slouken@6548
   460
slouken@6548
   461
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   462
        /* Most of SDL's calls to X11 are unwaware of Xinerama, and to X11 standard calls, when Xinerama is active,
slouken@6548
   463
         * there's only one screen available. So we force the screen number to zero and
slouken@6548
   464
         * let Xinerama specific code handle specific functionality using displaydata->xinerama_info
slouken@6548
   465
         */
slouken@6548
   466
        if (use_xinerama) {
slouken@6548
   467
            displaydata->screen = 0;
slouken@6548
   468
            displaydata->use_xinerama = use_xinerama;
slouken@6548
   469
            displaydata->xinerama_info = xinerama[screen];
slouken@6548
   470
            displaydata->xinerama_screen = screen;
slouken@6548
   471
        }
slouken@6548
   472
        else displaydata->screen = screen;
slouken@6548
   473
#else
slouken@6548
   474
        displaydata->screen = screen;
slouken@6548
   475
#endif
slouken@6548
   476
        displaydata->visual = vinfo.visual;
slouken@6548
   477
        displaydata->depth = vinfo.depth;
slouken@6548
   478
slouken@6548
   479
        displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
slouken@6548
   480
        pixmapFormats = XListPixmapFormats(data->display, &n);
slouken@6548
   481
        if (pixmapFormats) {
slouken@6548
   482
            for (i = 0; i < n; ++i) {
slouken@6548
   483
                if (pixmapFormats[i].depth == displaydata->depth) {
slouken@6548
   484
                    displaydata->scanline_pad = pixmapFormats[i].scanline_pad;
slouken@6548
   485
                    break;
slouken@6548
   486
                }
slouken@6548
   487
            }
slouken@6548
   488
            XFree(pixmapFormats);
slouken@6548
   489
        }
slouken@6548
   490
slouken@6548
   491
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   492
        if (use_xinerama) {
slouken@6548
   493
            displaydata->x = xinerama[screen].x_org;
slouken@6548
   494
            displaydata->y = xinerama[screen].y_org;
slouken@6548
   495
        }
slouken@6548
   496
        else
slouken@6548
   497
#endif
slouken@6548
   498
        {
slouken@6548
   499
            displaydata->x = 0;
slouken@6548
   500
            displaydata->y = 0;
slouken@6548
   501
        }
slouken@6548
   502
slouken@6548
   503
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   504
        if (use_xrandr) {
slouken@6548
   505
            res = XRRGetScreenResources(data->display, RootWindow(data->display, displaydata->screen));
slouken@6548
   506
        }
slouken@6548
   507
        if (res) {
slouken@6548
   508
            XRROutputInfo *output_info;
slouken@6548
   509
            XRRCrtcInfo *crtc;
slouken@6548
   510
            int output;
slouken@6548
   511
slouken@6548
   512
            for (output = 0; output < res->noutput; output++) {
slouken@6548
   513
                output_info = XRRGetOutputInfo(data->display, res, res->outputs[output]);
slouken@6548
   514
                if (!output_info || output_info->connection == RR_Disconnected) {
slouken@6548
   515
                    XRRFreeOutputInfo(output_info);
slouken@6548
   516
                    continue;
slouken@6548
   517
                }
slouken@6548
   518
slouken@6548
   519
                /* Is this the output that corresponds to the current screen?
slouken@6548
   520
                   We're checking the crtc position, but that may not be a valid test
slouken@6548
   521
                   in all cases.  Anybody want to give this some love?
slouken@6548
   522
                 */
slouken@6548
   523
                crtc = XRRGetCrtcInfo(data->display, res, output_info->crtc);
slouken@6548
   524
                if (!crtc || crtc->x != displaydata->x || crtc->y != displaydata->y) {
slouken@6548
   525
                    XRRFreeOutputInfo(output_info);
slouken@6548
   526
                    XRRFreeCrtcInfo(crtc);
slouken@6548
   527
                    continue;
slouken@6548
   528
                }
slouken@6548
   529
slouken@6548
   530
                displaydata->use_xrandr = use_xrandr;
slouken@6548
   531
                displaydata->xrandr_output = res->outputs[output];
slouken@6548
   532
                SetXRandRModeInfo(data->display, res, output_info, crtc->mode, &mode);
slouken@6548
   533
slouken@6548
   534
                XRRFreeOutputInfo(output_info);
slouken@6548
   535
                XRRFreeCrtcInfo(crtc);
slouken@6548
   536
                break;
slouken@6548
   537
            }
slouken@6548
   538
#ifdef X11MODES_DEBUG
slouken@6548
   539
            if (output == res->noutput) {
slouken@6548
   540
                printf("Couldn't find XRandR CRTC at %d,%d\n", displaydata->x, displaydata->y);
slouken@6548
   541
            }
slouken@6548
   542
#endif
slouken@6548
   543
            XRRFreeScreenResources(res);
slouken@6548
   544
        }
slouken@6548
   545
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
   546
slouken@6548
   547
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   548
        if (!displaydata->use_xrandr &&
slouken@6548
   549
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   550
            (!displaydata->use_xinerama || displaydata->xinerama_info.screen_number == 0) &&
slouken@6548
   551
#endif
slouken@6548
   552
            use_vidmode) {
slouken@6548
   553
            displaydata->use_vidmode = use_vidmode;
slouken@6548
   554
            XF86VidModeGetModeInfo(data->display, screen, &modedata->vm_mode);
slouken@6548
   555
        }
slouken@6548
   556
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   557
slouken@6548
   558
        SDL_zero(display);
slouken@6548
   559
        display.desktop_mode = mode;
slouken@6548
   560
        display.current_mode = mode;
slouken@6548
   561
        display.driverdata = displaydata;
slouken@6548
   562
        SDL_AddVideoDisplay(&display);
slouken@6548
   563
    }
slouken@6548
   564
slouken@6548
   565
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   566
    if (xinerama) XFree(xinerama);
slouken@6548
   567
#endif
slouken@6548
   568
slouken@6548
   569
    if (_this->num_displays == 0) {
slouken@6548
   570
        SDL_SetError("No available displays");
slouken@6548
   571
        return -1;
slouken@6548
   572
    }
slouken@6548
   573
    return 0;
slouken@2873
   574
}
slouken@2873
   575
slouken@1950
   576
void
slouken@3500
   577
X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
slouken@1950
   578
{
slouken@2870
   579
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   580
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@5408
   581
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   582
    int nmodes;
slouken@5408
   583
    XF86VidModeModeInfo ** modes;
slouken@2873
   584
#endif
slouken@2873
   585
    int screen_w;
slouken@2873
   586
    int screen_h;
slouken@1950
   587
    SDL_DisplayMode mode;
slouken@6548
   588
    SDL_DisplayModeData *modedata;
slouken@2870
   589
slouken@2873
   590
    /* Unfortunately X11 requires the window to be created with the correct
slouken@2873
   591
     * visual and depth ahead of time, but the SDL API allows you to create
slouken@2873
   592
     * a window before setting the fullscreen display mode.  This means that
slouken@2873
   593
     * we have to use the same format for all windows and all display modes.
slouken@2873
   594
     * (or support recreating the window with a new visual behind the scenes)
slouken@2873
   595
     */
slouken@3500
   596
    mode.format = sdl_display->current_mode.format;
slouken@2873
   597
    mode.driverdata = NULL;
slouken@2873
   598
slouken@2873
   599
    screen_w = DisplayWidth(display, data->screen);
slouken@2873
   600
    screen_h = DisplayHeight(display, data->screen);
slouken@2873
   601
slouken@2873
   602
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   603
    if (data->use_xinerama) {
gabomdq@6331
   604
        /* Add the full (both screens combined) xinerama mode only on the display that starts at 0,0 */
gabomdq@6331
   605
        if (!data->xinerama_info.x_org && !data->xinerama_info.y_org &&
gabomdq@6331
   606
           (screen_w > data->xinerama_info.width || screen_h > data->xinerama_info.height)) {
gabomdq@6331
   607
            mode.w = screen_w;
gabomdq@6331
   608
            mode.h = screen_h;
slouken@2873
   609
            mode.refresh_rate = 0;
slouken@6548
   610
            modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   611
            if (modedata) {
slouken@6548
   612
                *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
slouken@6548
   613
            }
slouken@6548
   614
            mode.driverdata = modedata;
slouken@3500
   615
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   616
        }
slouken@2873
   617
    }
slouken@2873
   618
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   619
slouken@2873
   620
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   621
    if (data->use_xrandr) {
slouken@6548
   622
        XRRScreenResources *res;
slouken@2873
   623
slouken@6548
   624
        res = XRRGetScreenResources (display, RootWindow(display, data->screen));
slouken@6548
   625
        if (res) {
slouken@6548
   626
            SDL_DisplayModeData *modedata;
slouken@6548
   627
            XRROutputInfo *output_info;
slouken@6548
   628
            int i;
slouken@2873
   629
slouken@6548
   630
            output_info = XRRGetOutputInfo(display, res, data->xrandr_output);
slouken@6548
   631
            if (output_info && output_info->connection != RR_Disconnected) {
slouken@6548
   632
                for (i = 0; i < output_info->nmode; ++i) {
slouken@6548
   633
                    modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   634
                    if (!modedata) {
slouken@6548
   635
                        continue;
slouken@6548
   636
                    }
slouken@6548
   637
                    mode.driverdata = modedata;
slouken@2873
   638
slouken@6548
   639
                    if (SetXRandRModeInfo(display, res, output_info, output_info->modes[i], &mode)) {
slouken@6548
   640
                        SDL_AddDisplayMode(sdl_display, &mode);
slouken@6548
   641
                    } else {
slouken@6548
   642
                        SDL_free(modedata);
slouken@6548
   643
                    }
slouken@2873
   644
                }
slouken@2873
   645
            }
slouken@6548
   646
            XRRFreeOutputInfo(output_info);
slouken@6548
   647
            XRRFreeScreenResources(res);
slouken@2873
   648
        }
slouken@6548
   649
        return;
slouken@2873
   650
    }
slouken@2873
   651
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   652
slouken@5408
   653
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   654
    if (data->use_vidmode &&
slouken@5408
   655
        XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   656
        int i;
slouken@2873
   657
slouken@2873
   658
#ifdef X11MODES_DEBUG
slouken@2873
   659
        printf("VidMode modes: (unsorted)\n");
slouken@2873
   660
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   661
            printf("Mode %d: %d x %d @ %d\n", i,
slouken@2874
   662
                   modes[i]->hdisplay, modes[i]->vdisplay,
slouken@6548
   663
                   CalculateXVidModeRefreshRate(modes[i]));
slouken@2873
   664
        }
slouken@2873
   665
#endif
slouken@2873
   666
        for (i = 0; i < nmodes; ++i) {
slouken@6548
   667
            modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   668
            if (!modedata) {
slouken@6548
   669
                continue;
slouken@6548
   670
            }
slouken@6548
   671
            mode.driverdata = modedata;
slouken@6548
   672
slouken@6548
   673
            if (SetXVidModeModeInfo(modes[i], &mode)) {
slouken@6548
   674
                SDL_AddDisplayMode(sdl_display, &mode);
slouken@6548
   675
            } else {
slouken@6548
   676
                SDL_free(modedata);
slouken@6548
   677
            }
slouken@2873
   678
        }
slouken@2873
   679
        XFree(modes);
slouken@6548
   680
        return;
slouken@2873
   681
    }
slouken@5408
   682
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   683
slouken@2873
   684
    if (!data->use_xrandr && !data->use_vidmode) {
slouken@6548
   685
        /* Add the desktop mode */
slouken@6548
   686
        mode = sdl_display->desktop_mode;
slouken@6548
   687
        modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   688
        if (modedata) {
slouken@6548
   689
            *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
slouken@6548
   690
        }
slouken@6548
   691
        mode.driverdata = modedata;
slouken@3500
   692
        SDL_AddDisplayMode(sdl_display, &mode);
slouken@2874
   693
    }
slouken@1950
   694
}
slouken@1950
   695
slouken@1950
   696
int
slouken@3500
   697
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
slouken@1950
   698
{
slouken@2873
   699
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   700
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@6548
   701
    SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
slouken@2873
   702
slouken@6548
   703
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   704
    if (data->use_xrandr) {
slouken@6548
   705
        XRRScreenResources *res;
slouken@6548
   706
        XRROutputInfo *output_info;
slouken@6548
   707
        XRRCrtcInfo *crtc;
slouken@6548
   708
        Status status;
slouken@6548
   709
slouken@6548
   710
        res = XRRGetScreenResources (display, RootWindow(display, data->screen));
slouken@6548
   711
        if (!res) {
slouken@6548
   712
            SDL_SetError("Couldn't get XRandR screen resources");
slouken@6548
   713
            return -1;
slouken@6548
   714
        }
slouken@6548
   715
slouken@6548
   716
        output_info = XRRGetOutputInfo(display, res, data->xrandr_output);
slouken@6548
   717
        if (!output_info || output_info->connection == RR_Disconnected) {
slouken@6548
   718
            SDL_SetError("Couldn't get XRandR output info");
slouken@6548
   719
            XRRFreeScreenResources(res);
slouken@6548
   720
            return -1;
slouken@6548
   721
        }
slouken@6548
   722
slouken@6548
   723
        crtc = XRRGetCrtcInfo(display, res, output_info->crtc);
slouken@6548
   724
        if (!crtc) {
slouken@6548
   725
            SDL_SetError("Couldn't get XRandR crtc info");
slouken@6548
   726
            XRRFreeOutputInfo(output_info);
slouken@6548
   727
            XRRFreeScreenResources(res);
slouken@6548
   728
            return -1;
slouken@6548
   729
        }
slouken@6548
   730
slouken@6548
   731
        status = XRRSetCrtcConfig (display, res, output_info->crtc, CurrentTime,
slouken@6548
   732
          crtc->x, crtc->y, modedata->xrandr_mode, crtc->rotation,
slouken@6548
   733
          &data->xrandr_output, 1);
slouken@6548
   734
slouken@6548
   735
        XRRFreeCrtcInfo(crtc);
slouken@6548
   736
        XRRFreeOutputInfo(output_info);
slouken@6548
   737
        XRRFreeScreenResources(res);
slouken@6548
   738
slouken@6548
   739
        if (status != Success) {
slouken@6548
   740
            SDL_SetError("XRRSetCrtcConfig failed");
slouken@6548
   741
            return -1;
slouken@6548
   742
        }
slouken@6549
   743
slouken@6549
   744
        /* Hack to let the window manager adjust to the mode change */
slouken@6549
   745
        const int WINDOW_MANAGER_DELAY_HACK = 250;
slouken@6549
   746
        SDL_Delay(WINDOW_MANAGER_DELAY_HACK);
slouken@6548
   747
    }
slouken@6548
   748
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
   749
slouken@6548
   750
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   751
    if (data->use_vidmode) {
slouken@6548
   752
        XF86VidModeSwitchToMode(display, data->screen, &modedata->vm_mode);
slouken@6548
   753
    }
slouken@6548
   754
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   755
slouken@2873
   756
    return 0;
slouken@1950
   757
}
slouken@1950
   758
slouken@1950
   759
void
slouken@1950
   760
X11_QuitModes(_THIS)
slouken@1950
   761
{
slouken@1950
   762
}
slouken@1950
   763
gabomdq@6331
   764
int
gabomdq@6331
   765
X11_GetDisplayBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
gabomdq@6331
   766
{
slouken@6502
   767
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
gabomdq@6331
   768
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@6502
   769
slouken@6548
   770
    rect->x = data->x;
slouken@6548
   771
    rect->y = data->y;
slouken@6548
   772
    rect->w = sdl_display->current_mode.w;
slouken@6548
   773
    rect->h = sdl_display->current_mode.h;
gabomdq@6331
   774
gabomdq@6331
   775
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   776
    /* Get the real current bounds of the display */
slouken@6502
   777
    if (data->use_xinerama) {
slouken@6548
   778
        int screencount;
slouken@6548
   779
        XineramaScreenInfo *xinerama = XineramaQueryScreens(display, &screencount);
slouken@6548
   780
        if (xinerama) {
slouken@6548
   781
            rect->x = xinerama[data->xinerama_screen].x_org;
slouken@6548
   782
            rect->y = xinerama[data->xinerama_screen].y_org;
slouken@6548
   783
        }
gabomdq@6331
   784
    }
slouken@6548
   785
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6475
   786
    return 0;
gabomdq@6331
   787
}
gabomdq@6331
   788
slouken@5481
   789
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   790
slouken@1950
   791
/* vi: set ts=4 sw=4 expandtab: */