src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Oct 2012 19:24:11 -0700
changeset 6550 191f00a080ba
parent 6549 db16b819e1d1
child 6551 b0679a0d5751
permissions -rw-r--r--
Don't try to query outputs that are not hooked up to a crtc
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@6550
   514
                if (!output_info || !output_info->crtc ||
slouken@6550
   515
                    output_info->connection == RR_Disconnected) {
slouken@6548
   516
                    XRRFreeOutputInfo(output_info);
slouken@6548
   517
                    continue;
slouken@6548
   518
                }
slouken@6548
   519
slouken@6548
   520
                /* Is this the output that corresponds to the current screen?
slouken@6548
   521
                   We're checking the crtc position, but that may not be a valid test
slouken@6548
   522
                   in all cases.  Anybody want to give this some love?
slouken@6548
   523
                 */
slouken@6548
   524
                crtc = XRRGetCrtcInfo(data->display, res, output_info->crtc);
slouken@6548
   525
                if (!crtc || crtc->x != displaydata->x || crtc->y != displaydata->y) {
slouken@6548
   526
                    XRRFreeOutputInfo(output_info);
slouken@6548
   527
                    XRRFreeCrtcInfo(crtc);
slouken@6548
   528
                    continue;
slouken@6548
   529
                }
slouken@6548
   530
slouken@6548
   531
                displaydata->use_xrandr = use_xrandr;
slouken@6548
   532
                displaydata->xrandr_output = res->outputs[output];
slouken@6548
   533
                SetXRandRModeInfo(data->display, res, output_info, crtc->mode, &mode);
slouken@6548
   534
slouken@6548
   535
                XRRFreeOutputInfo(output_info);
slouken@6548
   536
                XRRFreeCrtcInfo(crtc);
slouken@6548
   537
                break;
slouken@6548
   538
            }
slouken@6548
   539
#ifdef X11MODES_DEBUG
slouken@6548
   540
            if (output == res->noutput) {
slouken@6548
   541
                printf("Couldn't find XRandR CRTC at %d,%d\n", displaydata->x, displaydata->y);
slouken@6548
   542
            }
slouken@6548
   543
#endif
slouken@6548
   544
            XRRFreeScreenResources(res);
slouken@6548
   545
        }
slouken@6548
   546
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
   547
slouken@6548
   548
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   549
        if (!displaydata->use_xrandr &&
slouken@6548
   550
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   551
            (!displaydata->use_xinerama || displaydata->xinerama_info.screen_number == 0) &&
slouken@6548
   552
#endif
slouken@6548
   553
            use_vidmode) {
slouken@6548
   554
            displaydata->use_vidmode = use_vidmode;
slouken@6548
   555
            XF86VidModeGetModeInfo(data->display, screen, &modedata->vm_mode);
slouken@6548
   556
        }
slouken@6548
   557
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   558
slouken@6548
   559
        SDL_zero(display);
slouken@6548
   560
        display.desktop_mode = mode;
slouken@6548
   561
        display.current_mode = mode;
slouken@6548
   562
        display.driverdata = displaydata;
slouken@6548
   563
        SDL_AddVideoDisplay(&display);
slouken@6548
   564
    }
slouken@6548
   565
slouken@6548
   566
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   567
    if (xinerama) XFree(xinerama);
slouken@6548
   568
#endif
slouken@6548
   569
slouken@6548
   570
    if (_this->num_displays == 0) {
slouken@6548
   571
        SDL_SetError("No available displays");
slouken@6548
   572
        return -1;
slouken@6548
   573
    }
slouken@6548
   574
    return 0;
slouken@2873
   575
}
slouken@2873
   576
slouken@1950
   577
void
slouken@3500
   578
X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
slouken@1950
   579
{
slouken@2870
   580
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   581
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@5408
   582
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   583
    int nmodes;
slouken@5408
   584
    XF86VidModeModeInfo ** modes;
slouken@2873
   585
#endif
slouken@2873
   586
    int screen_w;
slouken@2873
   587
    int screen_h;
slouken@1950
   588
    SDL_DisplayMode mode;
slouken@6548
   589
    SDL_DisplayModeData *modedata;
slouken@2870
   590
slouken@2873
   591
    /* Unfortunately X11 requires the window to be created with the correct
slouken@2873
   592
     * visual and depth ahead of time, but the SDL API allows you to create
slouken@2873
   593
     * a window before setting the fullscreen display mode.  This means that
slouken@2873
   594
     * we have to use the same format for all windows and all display modes.
slouken@2873
   595
     * (or support recreating the window with a new visual behind the scenes)
slouken@2873
   596
     */
slouken@3500
   597
    mode.format = sdl_display->current_mode.format;
slouken@2873
   598
    mode.driverdata = NULL;
slouken@2873
   599
slouken@2873
   600
    screen_w = DisplayWidth(display, data->screen);
slouken@2873
   601
    screen_h = DisplayHeight(display, data->screen);
slouken@2873
   602
slouken@2873
   603
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   604
    if (data->use_xinerama) {
gabomdq@6331
   605
        /* Add the full (both screens combined) xinerama mode only on the display that starts at 0,0 */
gabomdq@6331
   606
        if (!data->xinerama_info.x_org && !data->xinerama_info.y_org &&
gabomdq@6331
   607
           (screen_w > data->xinerama_info.width || screen_h > data->xinerama_info.height)) {
gabomdq@6331
   608
            mode.w = screen_w;
gabomdq@6331
   609
            mode.h = screen_h;
slouken@2873
   610
            mode.refresh_rate = 0;
slouken@6548
   611
            modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   612
            if (modedata) {
slouken@6548
   613
                *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
slouken@6548
   614
            }
slouken@6548
   615
            mode.driverdata = modedata;
slouken@3500
   616
            SDL_AddDisplayMode(sdl_display, &mode);
slouken@2873
   617
        }
slouken@2873
   618
    }
slouken@2873
   619
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   620
slouken@2873
   621
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   622
    if (data->use_xrandr) {
slouken@6548
   623
        XRRScreenResources *res;
slouken@2873
   624
slouken@6548
   625
        res = XRRGetScreenResources (display, RootWindow(display, data->screen));
slouken@6548
   626
        if (res) {
slouken@6548
   627
            SDL_DisplayModeData *modedata;
slouken@6548
   628
            XRROutputInfo *output_info;
slouken@6548
   629
            int i;
slouken@2873
   630
slouken@6548
   631
            output_info = XRRGetOutputInfo(display, res, data->xrandr_output);
slouken@6548
   632
            if (output_info && output_info->connection != RR_Disconnected) {
slouken@6548
   633
                for (i = 0; i < output_info->nmode; ++i) {
slouken@6548
   634
                    modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   635
                    if (!modedata) {
slouken@6548
   636
                        continue;
slouken@6548
   637
                    }
slouken@6548
   638
                    mode.driverdata = modedata;
slouken@2873
   639
slouken@6548
   640
                    if (SetXRandRModeInfo(display, res, output_info, output_info->modes[i], &mode)) {
slouken@6548
   641
                        SDL_AddDisplayMode(sdl_display, &mode);
slouken@6548
   642
                    } else {
slouken@6548
   643
                        SDL_free(modedata);
slouken@6548
   644
                    }
slouken@2873
   645
                }
slouken@2873
   646
            }
slouken@6548
   647
            XRRFreeOutputInfo(output_info);
slouken@6548
   648
            XRRFreeScreenResources(res);
slouken@2873
   649
        }
slouken@6548
   650
        return;
slouken@2873
   651
    }
slouken@2873
   652
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   653
slouken@5408
   654
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   655
    if (data->use_vidmode &&
slouken@5408
   656
        XF86VidModeGetAllModeLines(display, data->screen, &nmodes, &modes)) {
slouken@2873
   657
        int i;
slouken@2873
   658
slouken@2873
   659
#ifdef X11MODES_DEBUG
slouken@2873
   660
        printf("VidMode modes: (unsorted)\n");
slouken@2873
   661
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   662
            printf("Mode %d: %d x %d @ %d\n", i,
slouken@2874
   663
                   modes[i]->hdisplay, modes[i]->vdisplay,
slouken@6548
   664
                   CalculateXVidModeRefreshRate(modes[i]));
slouken@2873
   665
        }
slouken@2873
   666
#endif
slouken@2873
   667
        for (i = 0; i < nmodes; ++i) {
slouken@6548
   668
            modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   669
            if (!modedata) {
slouken@6548
   670
                continue;
slouken@6548
   671
            }
slouken@6548
   672
            mode.driverdata = modedata;
slouken@6548
   673
slouken@6548
   674
            if (SetXVidModeModeInfo(modes[i], &mode)) {
slouken@6548
   675
                SDL_AddDisplayMode(sdl_display, &mode);
slouken@6548
   676
            } else {
slouken@6548
   677
                SDL_free(modedata);
slouken@6548
   678
            }
slouken@2873
   679
        }
slouken@2873
   680
        XFree(modes);
slouken@6548
   681
        return;
slouken@2873
   682
    }
slouken@5408
   683
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   684
slouken@2873
   685
    if (!data->use_xrandr && !data->use_vidmode) {
slouken@6548
   686
        /* Add the desktop mode */
slouken@6548
   687
        mode = sdl_display->desktop_mode;
slouken@6548
   688
        modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   689
        if (modedata) {
slouken@6548
   690
            *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
slouken@6548
   691
        }
slouken@6548
   692
        mode.driverdata = modedata;
slouken@3500
   693
        SDL_AddDisplayMode(sdl_display, &mode);
slouken@2874
   694
    }
slouken@1950
   695
}
slouken@1950
   696
slouken@1950
   697
int
slouken@3500
   698
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
slouken@1950
   699
{
slouken@2873
   700
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   701
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@6548
   702
    SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
slouken@2873
   703
slouken@6548
   704
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   705
    if (data->use_xrandr) {
slouken@6548
   706
        XRRScreenResources *res;
slouken@6548
   707
        XRROutputInfo *output_info;
slouken@6548
   708
        XRRCrtcInfo *crtc;
slouken@6548
   709
        Status status;
slouken@6548
   710
slouken@6548
   711
        res = XRRGetScreenResources (display, RootWindow(display, data->screen));
slouken@6548
   712
        if (!res) {
slouken@6548
   713
            SDL_SetError("Couldn't get XRandR screen resources");
slouken@6548
   714
            return -1;
slouken@6548
   715
        }
slouken@6548
   716
slouken@6548
   717
        output_info = XRRGetOutputInfo(display, res, data->xrandr_output);
slouken@6548
   718
        if (!output_info || output_info->connection == RR_Disconnected) {
slouken@6548
   719
            SDL_SetError("Couldn't get XRandR output info");
slouken@6548
   720
            XRRFreeScreenResources(res);
slouken@6548
   721
            return -1;
slouken@6548
   722
        }
slouken@6548
   723
slouken@6548
   724
        crtc = XRRGetCrtcInfo(display, res, output_info->crtc);
slouken@6548
   725
        if (!crtc) {
slouken@6548
   726
            SDL_SetError("Couldn't get XRandR crtc info");
slouken@6548
   727
            XRRFreeOutputInfo(output_info);
slouken@6548
   728
            XRRFreeScreenResources(res);
slouken@6548
   729
            return -1;
slouken@6548
   730
        }
slouken@6548
   731
slouken@6548
   732
        status = XRRSetCrtcConfig (display, res, output_info->crtc, CurrentTime,
slouken@6548
   733
          crtc->x, crtc->y, modedata->xrandr_mode, crtc->rotation,
slouken@6548
   734
          &data->xrandr_output, 1);
slouken@6548
   735
slouken@6548
   736
        XRRFreeCrtcInfo(crtc);
slouken@6548
   737
        XRRFreeOutputInfo(output_info);
slouken@6548
   738
        XRRFreeScreenResources(res);
slouken@6548
   739
slouken@6548
   740
        if (status != Success) {
slouken@6548
   741
            SDL_SetError("XRRSetCrtcConfig failed");
slouken@6548
   742
            return -1;
slouken@6548
   743
        }
slouken@6549
   744
slouken@6549
   745
        /* Hack to let the window manager adjust to the mode change */
slouken@6549
   746
        const int WINDOW_MANAGER_DELAY_HACK = 250;
slouken@6549
   747
        SDL_Delay(WINDOW_MANAGER_DELAY_HACK);
slouken@6548
   748
    }
slouken@6548
   749
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
   750
slouken@6548
   751
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   752
    if (data->use_vidmode) {
slouken@6548
   753
        XF86VidModeSwitchToMode(display, data->screen, &modedata->vm_mode);
slouken@6548
   754
    }
slouken@6548
   755
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   756
slouken@2873
   757
    return 0;
slouken@1950
   758
}
slouken@1950
   759
slouken@1950
   760
void
slouken@1950
   761
X11_QuitModes(_THIS)
slouken@1950
   762
{
slouken@1950
   763
}
slouken@1950
   764
gabomdq@6331
   765
int
gabomdq@6331
   766
X11_GetDisplayBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
gabomdq@6331
   767
{
slouken@6502
   768
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
gabomdq@6331
   769
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@6502
   770
slouken@6548
   771
    rect->x = data->x;
slouken@6548
   772
    rect->y = data->y;
slouken@6548
   773
    rect->w = sdl_display->current_mode.w;
slouken@6548
   774
    rect->h = sdl_display->current_mode.h;
gabomdq@6331
   775
gabomdq@6331
   776
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   777
    /* Get the real current bounds of the display */
slouken@6502
   778
    if (data->use_xinerama) {
slouken@6548
   779
        int screencount;
slouken@6548
   780
        XineramaScreenInfo *xinerama = XineramaQueryScreens(display, &screencount);
slouken@6548
   781
        if (xinerama) {
slouken@6548
   782
            rect->x = xinerama[data->xinerama_screen].x_org;
slouken@6548
   783
            rect->y = xinerama[data->xinerama_screen].y_org;
slouken@6548
   784
        }
gabomdq@6331
   785
    }
slouken@6548
   786
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6475
   787
    return 0;
gabomdq@6331
   788
}
gabomdq@6331
   789
slouken@5481
   790
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   791
slouken@1950
   792
/* vi: set ts=4 sw=4 expandtab: */