src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Oct 2012 19:39:12 -0700
changeset 6553 2f03111a2105
parent 6551 b0679a0d5751
child 6558 90f231aa77b9
permissions -rw-r--r--
This hack doesn't work very well. I'd rather just show the problem so it can be fixed.
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@6551
   227
            && info->vTotal) ? (info->dotClock / (info->hTotal * info->vTotal)) : 0;
slouken@6548
   228
}
slouken@6548
   229
slouken@6548
   230
static SDL_bool
slouken@6548
   231
SetXRandRModeInfo(Display *display, XRRScreenResources *res, XRROutputInfo *output_info,
slouken@6548
   232
                  RRMode modeID, SDL_DisplayMode *mode)
slouken@6537
   233
{
slouken@6548
   234
    int i;
slouken@6548
   235
    for (i = 0; i < res->nmode; ++i) {
slouken@6548
   236
        if (res->modes[i].id == modeID) {
slouken@6548
   237
            XRRCrtcInfo *crtc;
slouken@6548
   238
            Rotation rotation = 0;
slouken@6548
   239
            const XRRModeInfo *info = &res->modes[i];
slouken@6548
   240
slouken@6548
   241
            crtc = XRRGetCrtcInfo(display, res, output_info->crtc);
slouken@6548
   242
            if (crtc) {
slouken@6548
   243
                rotation = crtc->rotation;
slouken@6548
   244
                XRRFreeCrtcInfo(crtc);
slouken@6548
   245
            }
slouken@6548
   246
slouken@6548
   247
            if (rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT)) {
slouken@6548
   248
                mode->w = info->height;
slouken@6548
   249
                mode->h = info->width;
slouken@6548
   250
            } else {
slouken@6548
   251
                mode->w = info->width;
slouken@6548
   252
                mode->h = info->height;
slouken@6548
   253
            }
slouken@6548
   254
            mode->refresh_rate = CalculateXRandRRefreshRate(info);
slouken@6548
   255
            ((SDL_DisplayModeData*)mode->driverdata)->xrandr_mode = modeID;
slouken@6548
   256
#ifdef X11MODES_DEBUG
slouken@6548
   257
            printf("XRandR mode %d: %dx%d@%dHz\n", modeID, mode->w, mode->h, mode->refresh_rate);
slouken@6548
   258
#endif
slouken@6548
   259
            return SDL_TRUE;
slouken@6548
   260
        }
slouken@6537
   261
    }
slouken@6548
   262
    return SDL_FALSE;
slouken@6537
   263
}
slouken@2873
   264
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   265
slouken@5408
   266
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   267
static SDL_bool
slouken@2874
   268
CheckVidMode(Display * display, int *major, int *minor)
slouken@2873
   269
{
slouken@2873
   270
    const char *env;
slouken@2873
   271
slouken@2873
   272
    /* Default the extension not available */
slouken@2873
   273
    *major = *minor = 0;
slouken@2873
   274
slouken@2873
   275
    /* Allow environment override */
slouken@6472
   276
    env = SDL_GetHint(SDL_HINT_VIDEO_X11_XVIDMODE);
slouken@2873
   277
    if (env && !SDL_atoi(env)) {
slouken@6468
   278
#ifdef X11MODES_DEBUG
slouken@6472
   279
        printf("XVidMode disabled due to hint\n");
slouken@6468
   280
#endif
slouken@2873
   281
        return SDL_FALSE;
slouken@2873
   282
    }
slouken@2874
   283
slouken@5408
   284
    if (!SDL_X11_HAVE_XVIDMODE) {
slouken@6468
   285
#ifdef X11MODES_DEBUG
slouken@6468
   286
        printf("XVidMode support not available\n");
slouken@6468
   287
#endif
slouken@5408
   288
        return SDL_FALSE;
slouken@5408
   289
    }
slouken@5408
   290
slouken@2873
   291
    /* Query the extension version */
slouken@2873
   292
    vm_error = -1;
slouken@5408
   293
    if (!XF86VidModeQueryExtension(display, &vm_event, &vm_error)
slouken@5408
   294
        || !XF86VidModeQueryVersion(display, major, minor)) {
slouken@6468
   295
#ifdef X11MODES_DEBUG
slouken@6468
   296
        printf("XVidMode not active on the display\n");
slouken@6468
   297
#endif
slouken@2873
   298
        return SDL_FALSE;
slouken@2873
   299
    }
slouken@6468
   300
#ifdef X11MODES_DEBUG
slouken@6548
   301
    printf("XVidMode available at version %d.%d!\n", *major, *minor);
slouken@6468
   302
#endif
slouken@2873
   303
    return SDL_TRUE;
slouken@2873
   304
}
slouken@2873
   305
slouken@4518
   306
static
slouken@5408
   307
Bool XF86VidModeGetModeInfo(Display * dpy, int scr,
slouken@5408
   308
                                       XF86VidModeModeInfo* info)
slouken@2873
   309
{
slouken@2873
   310
    Bool retval;
slouken@2873
   311
    int dotclock;
slouken@5408
   312
    XF86VidModeModeLine l;
slouken@2873
   313
    SDL_zerop(info);
slouken@2873
   314
    SDL_zero(l);
slouken@5408
   315
    retval = XF86VidModeGetModeLine(dpy, scr, &dotclock, &l);
slouken@2873
   316
    info->dotclock = dotclock;
slouken@2873
   317
    info->hdisplay = l.hdisplay;
slouken@2873
   318
    info->hsyncstart = l.hsyncstart;
slouken@2873
   319
    info->hsyncend = l.hsyncend;
slouken@2873
   320
    info->htotal = l.htotal;
slouken@2873
   321
    info->hskew = l.hskew;
slouken@2873
   322
    info->vdisplay = l.vdisplay;
slouken@2873
   323
    info->vsyncstart = l.vsyncstart;
slouken@2873
   324
    info->vsyncend = l.vsyncend;
slouken@2873
   325
    info->vtotal = l.vtotal;
slouken@2873
   326
    info->flags = l.flags;
slouken@2873
   327
    info->privsize = l.privsize;
slouken@2873
   328
    info->private = l.private;
slouken@2873
   329
    return retval;
slouken@2873
   330
}
slouken@2873
   331
slouken@2873
   332
static int
slouken@6548
   333
CalculateXVidModeRefreshRate(const XF86VidModeModeInfo * info)
slouken@2873
   334
{
slouken@2874
   335
    return (info->htotal
slouken@2874
   336
            && info->vtotal) ? (1000 * info->dotclock / (info->htotal *
slouken@2874
   337
                                                         info->vtotal)) : 0;
slouken@2873
   338
}
slouken@2873
   339
slouken@6548
   340
SDL_bool
slouken@6548
   341
SetXVidModeModeInfo(const XF86VidModeModeInfo *info, SDL_DisplayMode *mode)
slouken@2873
   342
{
slouken@6548
   343
    mode->w = info->hdisplay;
slouken@6548
   344
    mode->h = info->vdisplay;
slouken@6548
   345
    mode->refresh_rate = CalculateXVidModeRefreshRate(info);
slouken@6548
   346
    ((SDL_DisplayModeData*)mode->driverdata)->vm_mode = *info;
slouken@6548
   347
    return SDL_TRUE;
slouken@2873
   348
}
slouken@6548
   349
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   350
slouken@6548
   351
int
slouken@6548
   352
X11_InitModes(_THIS)
slouken@2873
   353
{
slouken@6548
   354
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@6548
   355
    int screen, screencount;
slouken@6548
   356
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   357
    int xinerama_major, xinerama_minor;
slouken@6548
   358
    int use_xinerama = 0;
slouken@6548
   359
    XineramaScreenInfo *xinerama = NULL;
slouken@6548
   360
#endif
slouken@6548
   361
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   362
    int xrandr_major, xrandr_minor;
slouken@6548
   363
    int use_xrandr = 0;
slouken@6548
   364
    XRRScreenResources *res = NULL;
slouken@6548
   365
#endif
slouken@6548
   366
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   367
    int vm_major, vm_minor;
slouken@6548
   368
    int use_vidmode = 0;
slouken@6548
   369
#endif
slouken@2873
   370
slouken@6548
   371
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   372
    /* Query Xinerama extention
slouken@6548
   373
     * NOTE: This works with Nvidia Twinview correctly, but you need version 302.17 (released on June 2012)
slouken@6548
   374
     *       or newer of the Nvidia binary drivers
slouken@6548
   375
     */
slouken@6548
   376
    if (CheckXinerama(data->display, &xinerama_major, &xinerama_minor)) {
slouken@6548
   377
        xinerama = XineramaQueryScreens(data->display, &screencount);
slouken@6548
   378
        if (xinerama) {
slouken@6548
   379
            use_xinerama = xinerama_major * 100 + xinerama_minor;
slouken@2873
   380
        }
slouken@2873
   381
    }
slouken@6548
   382
    if (!xinerama) {
slouken@6548
   383
        screencount = ScreenCount(data->display);
slouken@6548
   384
    }
slouken@6548
   385
#else
slouken@6548
   386
    screencount = ScreenCount(data->display);
slouken@6548
   387
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6548
   388
slouken@6548
   389
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   390
    /* require at least XRandR v1.2 */
slouken@6548
   391
    if (CheckXRandR(data->display, &xrandr_major, &xrandr_minor) &&
slouken@6548
   392
        (xrandr_major >= 2 || (xrandr_major == 1 && xrandr_minor >= 2))) {
slouken@6548
   393
        use_xrandr = xrandr_major * 100 + xrandr_minor;
slouken@6548
   394
    }
slouken@6548
   395
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
   396
slouken@6548
   397
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   398
    if (CheckVidMode(data->display, &vm_major, &vm_minor)) {
slouken@6548
   399
        use_vidmode = vm_major * 100 + vm_minor;
slouken@2873
   400
    }
slouken@6548
   401
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   402
slouken@6548
   403
    for (screen = 0; screen < screencount; ++screen) {
slouken@6548
   404
        XVisualInfo vinfo;
slouken@6548
   405
        SDL_VideoDisplay display;
slouken@6548
   406
        SDL_DisplayData *displaydata;
slouken@6548
   407
        SDL_DisplayMode mode;
slouken@6548
   408
        SDL_DisplayModeData *modedata;
slouken@6548
   409
        XPixmapFormatValues *pixmapFormats;
slouken@6548
   410
        int i, n;
slouken@6548
   411
slouken@6548
   412
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   413
        if (xinerama) {
slouken@6548
   414
            if (get_visualinfo(data->display, 0, &vinfo) < 0) {
slouken@6548
   415
                continue;
slouken@6548
   416
            }
slouken@6548
   417
        } else {
slouken@6548
   418
            if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@6548
   419
                continue;
slouken@6548
   420
            }
slouken@6548
   421
        }
slouken@6548
   422
#else
slouken@6548
   423
        if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@6548
   424
            continue;
slouken@6548
   425
        }
slouken@6548
   426
#endif
slouken@6548
   427
slouken@6548
   428
        displaydata = (SDL_DisplayData *) SDL_calloc(1, sizeof(*displaydata));
slouken@6548
   429
        if (!displaydata) {
slouken@6548
   430
            continue;
slouken@6548
   431
        }
slouken@6548
   432
slouken@6548
   433
        mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
slouken@6548
   434
        if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
slouken@6548
   435
            /* We don't support palettized modes now */
slouken@6548
   436
            SDL_free(displaydata);
slouken@6548
   437
            continue;
slouken@6548
   438
        }
slouken@6548
   439
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   440
        if (xinerama) {
slouken@6548
   441
            mode.w = xinerama[screen].width;
slouken@6548
   442
            mode.h = xinerama[screen].height;
slouken@6548
   443
        } else {
slouken@6548
   444
            mode.w = DisplayWidth(data->display, screen);
slouken@6548
   445
            mode.h = DisplayHeight(data->display, screen);
slouken@6548
   446
        }
slouken@6548
   447
#else
slouken@6548
   448
        mode.w = DisplayWidth(data->display, screen);
slouken@6548
   449
        mode.h = DisplayHeight(data->display, screen);
slouken@6548
   450
#endif
slouken@6548
   451
        mode.refresh_rate = 0;
slouken@6548
   452
slouken@6548
   453
        modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   454
        if (!modedata) {
slouken@6548
   455
            SDL_free(displaydata);
slouken@6548
   456
            continue;
slouken@6548
   457
        }
slouken@6548
   458
        mode.driverdata = modedata;
slouken@6548
   459
slouken@6548
   460
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   461
        /* Most of SDL's calls to X11 are unwaware of Xinerama, and to X11 standard calls, when Xinerama is active,
slouken@6548
   462
         * there's only one screen available. So we force the screen number to zero and
slouken@6548
   463
         * let Xinerama specific code handle specific functionality using displaydata->xinerama_info
slouken@6548
   464
         */
slouken@6548
   465
        if (use_xinerama) {
slouken@6548
   466
            displaydata->screen = 0;
slouken@6548
   467
            displaydata->use_xinerama = use_xinerama;
slouken@6548
   468
            displaydata->xinerama_info = xinerama[screen];
slouken@6548
   469
            displaydata->xinerama_screen = screen;
slouken@6548
   470
        }
slouken@6548
   471
        else displaydata->screen = screen;
slouken@6548
   472
#else
slouken@6548
   473
        displaydata->screen = screen;
slouken@6548
   474
#endif
slouken@6548
   475
        displaydata->visual = vinfo.visual;
slouken@6548
   476
        displaydata->depth = vinfo.depth;
slouken@6548
   477
slouken@6548
   478
        displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
slouken@6548
   479
        pixmapFormats = XListPixmapFormats(data->display, &n);
slouken@6548
   480
        if (pixmapFormats) {
slouken@6548
   481
            for (i = 0; i < n; ++i) {
slouken@6548
   482
                if (pixmapFormats[i].depth == displaydata->depth) {
slouken@6548
   483
                    displaydata->scanline_pad = pixmapFormats[i].scanline_pad;
slouken@6548
   484
                    break;
slouken@6548
   485
                }
slouken@6548
   486
            }
slouken@6548
   487
            XFree(pixmapFormats);
slouken@6548
   488
        }
slouken@6548
   489
slouken@6548
   490
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   491
        if (use_xinerama) {
slouken@6548
   492
            displaydata->x = xinerama[screen].x_org;
slouken@6548
   493
            displaydata->y = xinerama[screen].y_org;
slouken@6548
   494
        }
slouken@6548
   495
        else
slouken@6548
   496
#endif
slouken@6548
   497
        {
slouken@6548
   498
            displaydata->x = 0;
slouken@6548
   499
            displaydata->y = 0;
slouken@6548
   500
        }
slouken@6548
   501
slouken@6548
   502
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   503
        if (use_xrandr) {
slouken@6548
   504
            res = XRRGetScreenResources(data->display, RootWindow(data->display, displaydata->screen));
slouken@6548
   505
        }
slouken@6548
   506
        if (res) {
slouken@6548
   507
            XRROutputInfo *output_info;
slouken@6548
   508
            XRRCrtcInfo *crtc;
slouken@6548
   509
            int output;
slouken@6548
   510
slouken@6548
   511
            for (output = 0; output < res->noutput; output++) {
slouken@6548
   512
                output_info = XRRGetOutputInfo(data->display, res, res->outputs[output]);
slouken@6550
   513
                if (!output_info || !output_info->crtc ||
slouken@6550
   514
                    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@6548
   743
    }
slouken@6548
   744
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
   745
slouken@6548
   746
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   747
    if (data->use_vidmode) {
slouken@6548
   748
        XF86VidModeSwitchToMode(display, data->screen, &modedata->vm_mode);
slouken@6548
   749
    }
slouken@6548
   750
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   751
slouken@2873
   752
    return 0;
slouken@1950
   753
}
slouken@1950
   754
slouken@1950
   755
void
slouken@1950
   756
X11_QuitModes(_THIS)
slouken@1950
   757
{
slouken@1950
   758
}
slouken@1950
   759
gabomdq@6331
   760
int
gabomdq@6331
   761
X11_GetDisplayBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
gabomdq@6331
   762
{
slouken@6502
   763
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
gabomdq@6331
   764
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@6502
   765
slouken@6548
   766
    rect->x = data->x;
slouken@6548
   767
    rect->y = data->y;
slouken@6548
   768
    rect->w = sdl_display->current_mode.w;
slouken@6548
   769
    rect->h = sdl_display->current_mode.h;
gabomdq@6331
   770
gabomdq@6331
   771
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   772
    /* Get the real current bounds of the display */
slouken@6502
   773
    if (data->use_xinerama) {
slouken@6548
   774
        int screencount;
slouken@6548
   775
        XineramaScreenInfo *xinerama = XineramaQueryScreens(display, &screencount);
slouken@6548
   776
        if (xinerama) {
slouken@6548
   777
            rect->x = xinerama[data->xinerama_screen].x_org;
slouken@6548
   778
            rect->y = xinerama[data->xinerama_screen].y_org;
slouken@6548
   779
        }
gabomdq@6331
   780
    }
slouken@6548
   781
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6475
   782
    return 0;
gabomdq@6331
   783
}
gabomdq@6331
   784
slouken@5481
   785
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   786
slouken@1950
   787
/* vi: set ts=4 sw=4 expandtab: */