src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Nov 2016 13:14:00 -0800
changeset 10604 27d0fb08d755
parent 10586 c8ca00149330
child 10609 d702ecbd8ba7
permissions -rw-r--r--
Define _GNU_SOURCE when building SDL
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
icculus@8093
    21
#include "../../SDL_internal.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"
icculus@9319
    27
#include "SDL_timer.h"
slouken@6796
    28
#include "edid.h"
slouken@1950
    29
gabomdq@7678
    30
/* #define X11MODES_DEBUG */
slouken@6558
    31
slouken@6559
    32
/* I'm becoming more and more convinced that the application should never
slouken@6559
    33
 * use XRandR, and it's the window manager's responsibility to track and
slouken@6559
    34
 * manage display modes for fullscreen windows.  Right now XRandR is completely
slouken@6559
    35
 * broken with respect to window manager behavior on every window manager that
slouken@6559
    36
 * I can find.  For example, on Unity 3D if you show a fullscreen window while
slouken@6559
    37
 * the resolution is changing (within ~250 ms) your window will retain the
slouken@6559
    38
 * fullscreen state hint but be decorated and windowed.
slouken@6796
    39
 *
slouken@6796
    40
 * However, many people swear by it, so let them swear at it. :)
slouken@6558
    41
*/
gabomdq@7678
    42
/* #define XRANDR_DISABLED_BY_DEFAULT */
slouken@6558
    43
slouken@1950
    44
slouken@1950
    45
static int
slouken@1950
    46
get_visualinfo(Display * display, int screen, XVisualInfo * vinfo)
slouken@1950
    47
{
slouken@1950
    48
    const char *visual_id = SDL_getenv("SDL_VIDEO_X11_VISUALID");
slouken@1950
    49
    int depth;
slouken@1950
    50
slouken@1950
    51
    /* Look for an exact visual, if requested */
slouken@1950
    52
    if (visual_id) {
slouken@3697
    53
        XVisualInfo *vi, template;
slouken@3697
    54
        int nvis;
slouken@1950
    55
slouken@1950
    56
        SDL_zero(template);
slouken@1950
    57
        template.visualid = SDL_strtol(visual_id, NULL, 0);
icculus@7827
    58
        vi = X11_XGetVisualInfo(display, VisualIDMask, &template, &nvis);
slouken@1950
    59
        if (vi) {
slouken@1950
    60
            *vinfo = *vi;
icculus@7827
    61
            X11_XFree(vi);
slouken@1950
    62
            return 0;
slouken@1950
    63
        }
slouken@1950
    64
    }
slouken@3697
    65
slouken@1950
    66
    depth = DefaultDepth(display, screen);
slouken@5466
    67
    if ((X11_UseDirectColorVisuals() &&
icculus@7827
    68
         X11_XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
icculus@7827
    69
        X11_XMatchVisualInfo(display, screen, depth, TrueColor, vinfo) ||
icculus@7827
    70
        X11_XMatchVisualInfo(display, screen, depth, PseudoColor, vinfo) ||
icculus@7827
    71
        X11_XMatchVisualInfo(display, screen, depth, StaticColor, vinfo)) {
slouken@1950
    72
        return 0;
slouken@1950
    73
    }
slouken@1950
    74
    return -1;
slouken@1950
    75
}
slouken@1950
    76
slouken@5182
    77
int
slouken@5182
    78
X11_GetVisualInfoFromVisual(Display * display, Visual * visual, XVisualInfo * vinfo)
slouken@5182
    79
{
slouken@5182
    80
    XVisualInfo *vi;
slouken@5182
    81
    int nvis;
slouken@5182
    82
icculus@7827
    83
    vinfo->visualid = X11_XVisualIDFromVisual(visual);
icculus@7827
    84
    vi = X11_XGetVisualInfo(display, VisualIDMask, vinfo, &nvis);
slouken@5182
    85
    if (vi) {
slouken@5182
    86
        *vinfo = *vi;
icculus@7827
    87
        X11_XFree(vi);
slouken@5182
    88
        return 0;
slouken@5182
    89
    }
slouken@5182
    90
    return -1;
slouken@5182
    91
}
slouken@5182
    92
slouken@5182
    93
Uint32
slouken@2874
    94
X11_GetPixelFormatFromVisualInfo(Display * display, XVisualInfo * vinfo)
slouken@2870
    95
{
slouken@2870
    96
    if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
slouken@2870
    97
        int bpp;
slouken@2870
    98
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@2870
    99
slouken@2870
   100
        Rmask = vinfo->visual->red_mask;
slouken@2870
   101
        Gmask = vinfo->visual->green_mask;
slouken@2870
   102
        Bmask = vinfo->visual->blue_mask;
slouken@2870
   103
        if (vinfo->depth == 32) {
slouken@2870
   104
            Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
slouken@2870
   105
        } else {
slouken@2870
   106
            Amask = 0;
slouken@2870
   107
        }
slouken@2870
   108
slouken@2870
   109
        bpp = vinfo->depth;
slouken@2870
   110
        if (bpp == 24) {
slouken@2870
   111
            int i, n;
icculus@7827
   112
            XPixmapFormatValues *p = X11_XListPixmapFormats(display, &n);
slouken@2870
   113
            if (p) {
slouken@2870
   114
                for (i = 0; i < n; ++i) {
slouken@2870
   115
                    if (p[i].depth == 24) {
slouken@2870
   116
                        bpp = p[i].bits_per_pixel;
slouken@2870
   117
                        break;
slouken@2870
   118
                    }
slouken@2870
   119
                }
icculus@7827
   120
                X11_XFree(p);
slouken@2870
   121
            }
slouken@2870
   122
        }
slouken@2870
   123
slouken@2870
   124
        return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2870
   125
    }
slouken@2870
   126
slouken@2870
   127
    if (vinfo->class == PseudoColor || vinfo->class == StaticColor) {
slouken@2870
   128
        switch (vinfo->depth) {
slouken@2870
   129
        case 8:
slouken@2870
   130
            return SDL_PIXELTYPE_INDEX8;
slouken@2870
   131
        case 4:
slouken@2870
   132
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   133
                return SDL_PIXELFORMAT_INDEX4LSB;
slouken@2870
   134
            } else {
slouken@2870
   135
                return SDL_PIXELFORMAT_INDEX4MSB;
slouken@2870
   136
            }
slouken@2870
   137
            break;
slouken@2870
   138
        case 1:
slouken@2870
   139
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   140
                return SDL_PIXELFORMAT_INDEX1LSB;
slouken@2870
   141
            } else {
slouken@2870
   142
                return SDL_PIXELFORMAT_INDEX1MSB;
slouken@2870
   143
            }
slouken@2870
   144
            break;
slouken@2870
   145
        }
slouken@2870
   146
    }
slouken@2870
   147
slouken@2870
   148
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@2870
   149
}
slouken@1950
   150
slouken@2873
   151
/* Global for the error handler */
slouken@2873
   152
int vm_event, vm_error = -1;
slouken@2873
   153
slouken@2873
   154
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   155
static SDL_bool
slouken@2874
   156
CheckXinerama(Display * display, int *major, int *minor)
slouken@2873
   157
{
icculus@6027
   158
    int event_base = 0;
icculus@6027
   159
    int error_base = 0;
slouken@2873
   160
slouken@2873
   161
    /* Default the extension not available */
slouken@2873
   162
    *major = *minor = 0;
slouken@2873
   163
slouken@2873
   164
    /* Allow environment override */
slouken@10499
   165
    if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XINERAMA, SDL_TRUE)) {
slouken@6468
   166
#ifdef X11MODES_DEBUG
slouken@6472
   167
        printf("Xinerama disabled due to hint\n");
slouken@6468
   168
#endif
slouken@2873
   169
        return SDL_FALSE;
slouken@2873
   170
    }
slouken@2873
   171
slouken@5408
   172
    if (!SDL_X11_HAVE_XINERAMA) {
slouken@6468
   173
#ifdef X11MODES_DEBUG
slouken@6468
   174
        printf("Xinerama support not available\n");
slouken@6468
   175
#endif
slouken@5408
   176
        return SDL_FALSE;
slouken@5408
   177
    }
slouken@5408
   178
slouken@2873
   179
    /* Query the extension version */
icculus@7827
   180
    if (!X11_XineramaQueryExtension(display, &event_base, &error_base) ||
icculus@7827
   181
        !X11_XineramaQueryVersion(display, major, minor) ||
icculus@7827
   182
        !X11_XineramaIsActive(display)) {
slouken@6468
   183
#ifdef X11MODES_DEBUG
slouken@6468
   184
        printf("Xinerama not active on the display\n");
slouken@6468
   185
#endif
slouken@2873
   186
        return SDL_FALSE;
slouken@2873
   187
    }
slouken@6468
   188
#ifdef X11MODES_DEBUG
slouken@6548
   189
    printf("Xinerama available at version %d.%d!\n", *major, *minor);
slouken@6468
   190
#endif
slouken@2873
   191
    return SDL_TRUE;
slouken@2873
   192
}
icculus@9789
   193
icculus@9789
   194
/* !!! FIXME: remove this later. */
icculus@9789
   195
/* we have a weird bug where XineramaQueryScreens() throws an X error, so this
icculus@9789
   196
   is here to help track it down (and not crash, too!). */
icculus@9789
   197
static SDL_bool xinerama_triggered_error = SDL_FALSE;
icculus@9789
   198
static int
icculus@9789
   199
X11_XineramaFailed(Display * d, XErrorEvent * e)
icculus@9789
   200
{
icculus@9789
   201
    xinerama_triggered_error = SDL_TRUE;
icculus@9789
   202
    fprintf(stderr, "XINERAMA X ERROR: type=%d serial=%lu err=%u req=%u minor=%u\n",
icculus@9789
   203
            e->type, e->serial, (unsigned int) e->error_code,
icculus@9789
   204
            (unsigned int) e->request_code, (unsigned int) e->minor_code);
icculus@9789
   205
    fflush(stderr);
icculus@9789
   206
    return 0;
icculus@9789
   207
}
slouken@2873
   208
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   209
slouken@2873
   210
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   211
static SDL_bool
slouken@2874
   212
CheckXRandR(Display * display, int *major, int *minor)
slouken@2873
   213
{
slouken@2873
   214
    /* Default the extension not available */
slouken@2873
   215
    *major = *minor = 0;
slouken@2873
   216
slouken@2873
   217
    /* Allow environment override */
slouken@6558
   218
#ifdef XRANDR_DISABLED_BY_DEFAULT
slouken@10499
   219
    if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XRANDR, SDL_FALSE)) {
slouken@6558
   220
#ifdef X11MODES_DEBUG
slouken@6558
   221
        printf("XRandR disabled by default due to window manager issues\n");
slouken@6558
   222
#endif
slouken@6558
   223
        return SDL_FALSE;
slouken@6558
   224
    }
slouken@6558
   225
#else
slouken@10499
   226
    if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XRANDR, SDL_TRUE)) {
slouken@6468
   227
#ifdef X11MODES_DEBUG
slouken@6472
   228
        printf("XRandR disabled due to hint\n");
slouken@6468
   229
#endif
slouken@2873
   230
        return SDL_FALSE;
slouken@2873
   231
    }
slouken@6558
   232
#endif /* XRANDR_ENABLED_BY_DEFAULT */
slouken@2873
   233
slouken@2873
   234
    if (!SDL_X11_HAVE_XRANDR) {
slouken@6468
   235
#ifdef X11MODES_DEBUG
slouken@6468
   236
        printf("XRandR support not available\n");
slouken@6468
   237
#endif
slouken@2873
   238
        return SDL_FALSE;
slouken@2873
   239
    }
slouken@2873
   240
slouken@2873
   241
    /* Query the extension version */
icculus@9834
   242
    *major = 1; *minor = 3;  /* we want 1.3 */
icculus@7827
   243
    if (!X11_XRRQueryVersion(display, major, minor)) {
slouken@6468
   244
#ifdef X11MODES_DEBUG
slouken@6468
   245
        printf("XRandR not active on the display\n");
slouken@6468
   246
#endif
icculus@9834
   247
        *major = *minor = 0;
slouken@2873
   248
        return SDL_FALSE;
slouken@2873
   249
    }
slouken@6468
   250
#ifdef X11MODES_DEBUG
slouken@6548
   251
    printf("XRandR available at version %d.%d!\n", *major, *minor);
slouken@6468
   252
#endif
slouken@2873
   253
    return SDL_TRUE;
slouken@2873
   254
}
slouken@6537
   255
slouken@6537
   256
#define XRANDR_ROTATION_LEFT    (1 << 1)
slouken@6537
   257
#define XRANDR_ROTATION_RIGHT   (1 << 3)
slouken@6537
   258
slouken@6548
   259
static int
slouken@6548
   260
CalculateXRandRRefreshRate(const XRRModeInfo *info)
slouken@6537
   261
{
slouken@10437
   262
    return (info->hTotal && info->vTotal) ?
slouken@10437
   263
        round(((double)info->dotClock / (double)(info->hTotal * info->vTotal))) : 0;
slouken@6548
   264
}
slouken@6548
   265
slouken@6548
   266
static SDL_bool
icculus@9834
   267
SetXRandRModeInfo(Display *display, XRRScreenResources *res, RRCrtc crtc,
slouken@6548
   268
                  RRMode modeID, SDL_DisplayMode *mode)
slouken@6548
   269
{
slouken@6548
   270
    int i;
slouken@6548
   271
    for (i = 0; i < res->nmode; ++i) {
icculus@9834
   272
        const XRRModeInfo *info = &res->modes[i];
icculus@9834
   273
        if (info->id == modeID) {
icculus@9834
   274
            XRRCrtcInfo *crtcinfo;
slouken@6548
   275
            Rotation rotation = 0;
slouken@6548
   276
icculus@9834
   277
            crtcinfo = X11_XRRGetCrtcInfo(display, res, crtc);
icculus@9834
   278
            if (crtcinfo) {
icculus@9834
   279
                rotation = crtcinfo->rotation;
icculus@9834
   280
                X11_XRRFreeCrtcInfo(crtcinfo);
slouken@6548
   281
            }
slouken@6548
   282
slouken@6548
   283
            if (rotation & (XRANDR_ROTATION_LEFT|XRANDR_ROTATION_RIGHT)) {
slouken@6548
   284
                mode->w = info->height;
slouken@6548
   285
                mode->h = info->width;
slouken@6548
   286
            } else {
slouken@6548
   287
                mode->w = info->width;
slouken@6548
   288
                mode->h = info->height;
slouken@6548
   289
            }
slouken@6548
   290
            mode->refresh_rate = CalculateXRandRRefreshRate(info);
slouken@6548
   291
            ((SDL_DisplayModeData*)mode->driverdata)->xrandr_mode = modeID;
slouken@6548
   292
#ifdef X11MODES_DEBUG
icculus@6560
   293
            printf("XRandR mode %d: %dx%d@%dHz\n", (int) modeID, mode->w, mode->h, mode->refresh_rate);
slouken@6548
   294
#endif
slouken@6548
   295
            return SDL_TRUE;
slouken@6548
   296
        }
slouken@6537
   297
    }
slouken@6548
   298
    return SDL_FALSE;
slouken@6537
   299
}
icculus@9834
   300
icculus@9834
   301
static void
icculus@9834
   302
SetXRandRDisplayName(Display *dpy, Atom EDID, char *name, const size_t namelen, RROutput output, const unsigned long widthmm, const unsigned long heightmm)
icculus@9834
   303
{
icculus@9834
   304
    /* See if we can get the EDID data for the real monitor name */
icculus@9834
   305
    int inches;
icculus@9834
   306
    int nprop;
icculus@9834
   307
    Atom *props = X11_XRRListOutputProperties(dpy, output, &nprop);
icculus@9834
   308
    int i;
icculus@9834
   309
icculus@9834
   310
    for (i = 0; i < nprop; ++i) {
icculus@9834
   311
        unsigned char *prop;
icculus@9834
   312
        int actual_format;
icculus@9834
   313
        unsigned long nitems, bytes_after;
icculus@9834
   314
        Atom actual_type;
icculus@9834
   315
icculus@9834
   316
        if (props[i] == EDID) {
icculus@9834
   317
            if (X11_XRRGetOutputProperty(dpy, output, props[i], 0, 100, False,
icculus@9834
   318
                                         False, AnyPropertyType, &actual_type,
icculus@9834
   319
                                         &actual_format, &nitems, &bytes_after,
icculus@9834
   320
                                         &prop) == Success) {
icculus@9834
   321
                MonitorInfo *info = decode_edid(prop);
icculus@9834
   322
                if (info) {
icculus@9834
   323
#ifdef X11MODES_DEBUG
icculus@9834
   324
                    printf("Found EDID data for %s\n", name);
icculus@9834
   325
                    dump_monitor_info(info);
icculus@9834
   326
#endif
icculus@9834
   327
                    SDL_strlcpy(name, info->dsc_product_name, namelen);
icculus@9834
   328
                    free(info);
icculus@9834
   329
                }
icculus@9834
   330
                X11_XFree(prop);
icculus@9834
   331
            }
icculus@9834
   332
            break;
icculus@9834
   333
        }
icculus@9834
   334
    }
icculus@9834
   335
icculus@9834
   336
    if (props) {
icculus@9834
   337
        X11_XFree(props);
icculus@9834
   338
    }
icculus@9834
   339
icculus@10030
   340
    inches = (int)((SDL_sqrtf(widthmm * widthmm + heightmm * heightmm) / 25.4f) + 0.5f);
icculus@9834
   341
    if (*name && inches) {
icculus@9834
   342
        const size_t len = SDL_strlen(name);
icculus@9834
   343
        SDL_snprintf(&name[len], namelen-len, " %d\"", inches);
icculus@9834
   344
    }
icculus@9834
   345
icculus@9834
   346
#ifdef X11MODES_DEBUG
icculus@9834
   347
    printf("Display name: %s\n", name);
icculus@9834
   348
#endif
icculus@9834
   349
}
icculus@9834
   350
icculus@9834
   351
icculus@9834
   352
int
icculus@9834
   353
X11_InitModes_XRandR(_THIS)
icculus@9834
   354
{
icculus@9834
   355
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
icculus@9834
   356
    Display *dpy = data->display;
icculus@9920
   357
    const int screencount = ScreenCount(dpy);
icculus@9920
   358
    const int default_screen = DefaultScreen(dpy);
icculus@9920
   359
    RROutput primary = X11_XRRGetOutputPrimary(dpy, RootWindow(dpy, default_screen));
icculus@9834
   360
    Atom EDID = X11_XInternAtom(dpy, "EDID", False);
icculus@9834
   361
    XRRScreenResources *res = NULL;
icculus@9834
   362
    Uint32 pixelformat;
icculus@9834
   363
    XVisualInfo vinfo;
icculus@9834
   364
    XPixmapFormatValues *pixmapformats;
icculus@9834
   365
    int looking_for_primary;
icculus@9834
   366
    int scanline_pad;
icculus@9834
   367
    int output;
icculus@9920
   368
    int screen, i, n;
icculus@9834
   369
icculus@9834
   370
    for (looking_for_primary = 1; looking_for_primary >= 0; looking_for_primary--) {
icculus@9920
   371
        for (screen = 0; screen < screencount; screen++) {
icculus@9834
   372
icculus@9920
   373
            /* we want the primary output first, and then skipped later. */
icculus@9984
   374
            if (looking_for_primary && (screen != default_screen)) {
icculus@9834
   375
                continue;
icculus@9834
   376
            }
icculus@9834
   377
icculus@9920
   378
            if (get_visualinfo(dpy, screen, &vinfo) < 0) {
icculus@9920
   379
                continue;  /* uh, skip this screen? */
icculus@9920
   380
            }
icculus@9920
   381
icculus@9920
   382
            pixelformat = X11_GetPixelFormatFromVisualInfo(dpy, &vinfo);
icculus@9920
   383
            if (SDL_ISPIXELFORMAT_INDEXED(pixelformat)) {
icculus@9920
   384
                continue;  /* Palettized video modes are no longer supported */
icculus@9920
   385
            }
icculus@9920
   386
icculus@9920
   387
            scanline_pad = SDL_BYTESPERPIXEL(pixelformat) * 8;
icculus@9920
   388
            pixmapformats = X11_XListPixmapFormats(dpy, &n);
icculus@9920
   389
            if (pixmapformats) {
icculus@9920
   390
                for (i = 0; i < n; ++i) {
icculus@9920
   391
                    if (pixmapformats[i].depth == vinfo.depth) {
icculus@9920
   392
                        scanline_pad = pixmapformats[i].scanline_pad;
icculus@9920
   393
                        break;
icculus@9920
   394
                    }
icculus@9920
   395
                }
icculus@9920
   396
                X11_XFree(pixmapformats);
icculus@9920
   397
            }
icculus@9920
   398
icculus@9920
   399
            res = X11_XRRGetScreenResources(dpy, RootWindow(dpy, screen));
icculus@9920
   400
            if (!res) {
icculus@9834
   401
                continue;
icculus@9834
   402
            }
icculus@9834
   403
icculus@9920
   404
            for (output = 0; output < res->noutput; output++) {
icculus@9920
   405
                XRROutputInfo *output_info;
icculus@9920
   406
                int display_x, display_y;
icculus@9920
   407
                unsigned long display_mm_width, display_mm_height;
icculus@9920
   408
                SDL_DisplayData *displaydata;
icculus@9920
   409
                char display_name[128];
icculus@9920
   410
                SDL_DisplayMode mode;
icculus@9920
   411
                SDL_DisplayModeData *modedata;
icculus@9920
   412
                SDL_VideoDisplay display;
icculus@9920
   413
                RRMode modeID;
icculus@9920
   414
                RRCrtc output_crtc;
icculus@9920
   415
                XRRCrtcInfo *crtc;
icculus@9834
   416
icculus@9920
   417
                /* The primary output _should_ always be sorted first, but just in case... */
icculus@9984
   418
                if ((looking_for_primary && (res->outputs[output] != primary)) ||
icculus@9920
   419
                    (!looking_for_primary && (screen == default_screen) && (res->outputs[output] == primary))) {
icculus@9920
   420
                    continue;
icculus@9920
   421
                }
icculus@9920
   422
icculus@9920
   423
                output_info = X11_XRRGetOutputInfo(dpy, res, res->outputs[output]);
icculus@9920
   424
                if (!output_info || !output_info->crtc || output_info->connection == RR_Disconnected) {
icculus@9920
   425
                    X11_XRRFreeOutputInfo(output_info);
icculus@9920
   426
                    continue;
icculus@9920
   427
                }
icculus@9920
   428
icculus@9920
   429
                SDL_strlcpy(display_name, output_info->name, sizeof(display_name));
icculus@9920
   430
                display_mm_width = output_info->mm_width;
icculus@9920
   431
                display_mm_height = output_info->mm_height;
icculus@9920
   432
                output_crtc = output_info->crtc;
icculus@9920
   433
                X11_XRRFreeOutputInfo(output_info);
icculus@9920
   434
icculus@9920
   435
                crtc = X11_XRRGetCrtcInfo(dpy, res, output_crtc);
icculus@9920
   436
                if (!crtc) {
icculus@9920
   437
                    continue;
icculus@9920
   438
                }
icculus@9920
   439
icculus@9920
   440
                SDL_zero(mode);
icculus@9920
   441
                modeID = crtc->mode;
icculus@9920
   442
                mode.w = crtc->width;
icculus@9920
   443
                mode.h = crtc->height;
icculus@9920
   444
                mode.format = pixelformat;
icculus@9920
   445
icculus@9920
   446
                display_x = crtc->x;
icculus@9920
   447
                display_y = crtc->y;
icculus@9920
   448
icculus@9920
   449
                X11_XRRFreeCrtcInfo(crtc);
icculus@9920
   450
icculus@9920
   451
                displaydata = (SDL_DisplayData *) SDL_calloc(1, sizeof(*displaydata));
icculus@9920
   452
                if (!displaydata) {
icculus@9920
   453
                    return SDL_OutOfMemory();
icculus@9920
   454
                }
icculus@9920
   455
icculus@9920
   456
                modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
icculus@9920
   457
                if (!modedata) {
icculus@9920
   458
                    SDL_free(displaydata);
icculus@9920
   459
                    return SDL_OutOfMemory();
icculus@9920
   460
                }
icculus@9920
   461
                modedata->xrandr_mode = modeID;
icculus@9920
   462
                mode.driverdata = modedata;
icculus@9920
   463
icculus@9920
   464
                displaydata->screen = screen;
icculus@9920
   465
                displaydata->visual = vinfo.visual;
icculus@9920
   466
                displaydata->depth = vinfo.depth;
icculus@9920
   467
                displaydata->hdpi = ((float) mode.w) * 25.4f / display_mm_width;
icculus@9920
   468
                displaydata->vdpi = ((float) mode.h) * 25.4f / display_mm_height;
icculus@9920
   469
                displaydata->ddpi = SDL_ComputeDiagonalDPI(mode.w, mode.h, ((float) display_mm_width) / 25.4f,((float) display_mm_height) / 25.4f);
icculus@9920
   470
                displaydata->scanline_pad = scanline_pad;
icculus@9920
   471
                displaydata->x = display_x;
icculus@9920
   472
                displaydata->y = display_y;
icculus@9920
   473
                displaydata->use_xrandr = 1;
icculus@9920
   474
                displaydata->xrandr_output = res->outputs[output];
icculus@9920
   475
icculus@9920
   476
                SetXRandRModeInfo(dpy, res, output_crtc, modeID, &mode);
icculus@9920
   477
                SetXRandRDisplayName(dpy, EDID, display_name, sizeof (display_name), res->outputs[output], display_mm_width, display_mm_height);
icculus@9920
   478
icculus@9920
   479
                SDL_zero(display);
icculus@9920
   480
                if (*display_name) {
icculus@9920
   481
                    display.name = display_name;
icculus@9920
   482
                }
icculus@9920
   483
                display.desktop_mode = mode;
icculus@9920
   484
                display.current_mode = mode;
icculus@9920
   485
                display.driverdata = displaydata;
icculus@9920
   486
                SDL_AddVideoDisplay(&display);
icculus@9834
   487
            }
philipp@9944
   488
philipp@9944
   489
            X11_XRRFreeScreenResources(res);
icculus@9834
   490
        }
icculus@9834
   491
    }
icculus@9834
   492
icculus@9834
   493
    if (_this->num_displays == 0) {
icculus@9834
   494
        return SDL_SetError("No available displays");
icculus@9834
   495
    }
icculus@9834
   496
icculus@9834
   497
    return 0;
icculus@9834
   498
}
slouken@2873
   499
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   500
slouken@5408
   501
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   502
static SDL_bool
slouken@2874
   503
CheckVidMode(Display * display, int *major, int *minor)
slouken@2873
   504
{
slouken@2873
   505
    /* Default the extension not available */
slouken@2873
   506
    *major = *minor = 0;
slouken@2873
   507
slouken@2873
   508
    /* Allow environment override */
slouken@10499
   509
    if (!SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_XVIDMODE, SDL_TRUE)) {
slouken@6468
   510
#ifdef X11MODES_DEBUG
slouken@6472
   511
        printf("XVidMode disabled due to hint\n");
slouken@6468
   512
#endif
slouken@2873
   513
        return SDL_FALSE;
slouken@2873
   514
    }
slouken@2874
   515
slouken@5408
   516
    if (!SDL_X11_HAVE_XVIDMODE) {
slouken@6468
   517
#ifdef X11MODES_DEBUG
slouken@6468
   518
        printf("XVidMode support not available\n");
slouken@6468
   519
#endif
slouken@5408
   520
        return SDL_FALSE;
slouken@5408
   521
    }
slouken@5408
   522
slouken@2873
   523
    /* Query the extension version */
slouken@2873
   524
    vm_error = -1;
icculus@7827
   525
    if (!X11_XF86VidModeQueryExtension(display, &vm_event, &vm_error)
icculus@7827
   526
        || !X11_XF86VidModeQueryVersion(display, major, minor)) {
slouken@6468
   527
#ifdef X11MODES_DEBUG
slouken@6468
   528
        printf("XVidMode not active on the display\n");
slouken@6468
   529
#endif
slouken@2873
   530
        return SDL_FALSE;
slouken@2873
   531
    }
slouken@6468
   532
#ifdef X11MODES_DEBUG
slouken@6548
   533
    printf("XVidMode available at version %d.%d!\n", *major, *minor);
slouken@6468
   534
#endif
slouken@2873
   535
    return SDL_TRUE;
slouken@2873
   536
}
slouken@2873
   537
slouken@4518
   538
static
slouken@5408
   539
Bool XF86VidModeGetModeInfo(Display * dpy, int scr,
slouken@5408
   540
                                       XF86VidModeModeInfo* info)
slouken@2873
   541
{
slouken@2873
   542
    Bool retval;
slouken@2873
   543
    int dotclock;
slouken@5408
   544
    XF86VidModeModeLine l;
slouken@2873
   545
    SDL_zerop(info);
slouken@2873
   546
    SDL_zero(l);
icculus@7827
   547
    retval = X11_XF86VidModeGetModeLine(dpy, scr, &dotclock, &l);
slouken@2873
   548
    info->dotclock = dotclock;
slouken@2873
   549
    info->hdisplay = l.hdisplay;
slouken@2873
   550
    info->hsyncstart = l.hsyncstart;
slouken@2873
   551
    info->hsyncend = l.hsyncend;
slouken@2873
   552
    info->htotal = l.htotal;
slouken@2873
   553
    info->hskew = l.hskew;
slouken@2873
   554
    info->vdisplay = l.vdisplay;
slouken@2873
   555
    info->vsyncstart = l.vsyncstart;
slouken@2873
   556
    info->vsyncend = l.vsyncend;
slouken@2873
   557
    info->vtotal = l.vtotal;
slouken@2873
   558
    info->flags = l.flags;
slouken@2873
   559
    info->privsize = l.privsize;
slouken@2873
   560
    info->private = l.private;
slouken@2873
   561
    return retval;
slouken@2873
   562
}
slouken@2873
   563
slouken@2873
   564
static int
slouken@6548
   565
CalculateXVidModeRefreshRate(const XF86VidModeModeInfo * info)
slouken@2873
   566
{
slouken@2874
   567
    return (info->htotal
slouken@2874
   568
            && info->vtotal) ? (1000 * info->dotclock / (info->htotal *
slouken@2874
   569
                                                         info->vtotal)) : 0;
slouken@2873
   570
}
slouken@2873
   571
slouken@6548
   572
SDL_bool
slouken@6548
   573
SetXVidModeModeInfo(const XF86VidModeModeInfo *info, SDL_DisplayMode *mode)
slouken@2873
   574
{
slouken@6548
   575
    mode->w = info->hdisplay;
slouken@6548
   576
    mode->h = info->vdisplay;
slouken@6548
   577
    mode->refresh_rate = CalculateXVidModeRefreshRate(info);
slouken@6548
   578
    ((SDL_DisplayModeData*)mode->driverdata)->vm_mode = *info;
slouken@6548
   579
    return SDL_TRUE;
slouken@2873
   580
}
slouken@6548
   581
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   582
slouken@6548
   583
int
slouken@6548
   584
X11_InitModes(_THIS)
slouken@2873
   585
{
slouken@6548
   586
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
gabomdq@9167
   587
    int snum, screen, screencount;
slouken@6548
   588
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   589
    int xinerama_major, xinerama_minor;
slouken@6548
   590
    int use_xinerama = 0;
slouken@6548
   591
    XineramaScreenInfo *xinerama = NULL;
slouken@6548
   592
#endif
slouken@6548
   593
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   594
    int xrandr_major, xrandr_minor;
slouken@6548
   595
#endif
slouken@6548
   596
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   597
    int vm_major, vm_minor;
slouken@6548
   598
    int use_vidmode = 0;
slouken@6548
   599
#endif
slouken@2873
   600
icculus@9834
   601
/* XRandR is the One True Modern Way to do this on X11. If it's enabled and
icculus@9834
   602
   available, don't even look at other ways of doing things. */
icculus@9834
   603
#if SDL_VIDEO_DRIVER_X11_XRANDR
icculus@9834
   604
    /* require at least XRandR v1.3 */
icculus@9834
   605
    if (CheckXRandR(data->display, &xrandr_major, &xrandr_minor) &&
icculus@9834
   606
        (xrandr_major >= 2 || (xrandr_major == 1 && xrandr_minor >= 3))) {
icculus@9834
   607
        return X11_InitModes_XRandR(_this);
icculus@9834
   608
    }
icculus@9834
   609
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
icculus@9834
   610
icculus@9834
   611
/* !!! FIXME: eventually remove support for Xinerama and XVidMode (everything below here). */
icculus@9834
   612
icculus@10015
   613
    /* This is a workaround for some apps (UnrealEngine4, for example) until
icculus@10015
   614
       we sort out the ramifications of removing XVidMode support outright.
icculus@10015
   615
       This block should be removed with the XVidMode support. */
icculus@10015
   616
    {
slouken@10499
   617
        if (SDL_GetHintBoolean("SDL_VIDEO_X11_REQUIRE_XRANDR", SDL_FALSE)) {
icculus@10015
   618
            #if SDL_VIDEO_DRIVER_X11_XRANDR
icculus@10015
   619
            return SDL_SetError("XRandR support is required but not available");
icculus@10015
   620
            #else
icculus@10015
   621
            return SDL_SetError("XRandR support is required but not built into SDL!");
icculus@10015
   622
            #endif
icculus@10015
   623
        }
icculus@10015
   624
    }
icculus@10015
   625
slouken@6548
   626
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   627
    /* Query Xinerama extention
slouken@6548
   628
     * NOTE: This works with Nvidia Twinview correctly, but you need version 302.17 (released on June 2012)
slouken@6548
   629
     *       or newer of the Nvidia binary drivers
slouken@6548
   630
     */
slouken@6548
   631
    if (CheckXinerama(data->display, &xinerama_major, &xinerama_minor)) {
icculus@9789
   632
        int (*handler) (Display *, XErrorEvent *);
icculus@9789
   633
        X11_XSync(data->display, False);
icculus@9789
   634
        handler = X11_XSetErrorHandler(X11_XineramaFailed);
icculus@7827
   635
        xinerama = X11_XineramaQueryScreens(data->display, &screencount);
icculus@9789
   636
        X11_XSync(data->display, False);
icculus@9789
   637
        X11_XSetErrorHandler(handler);
icculus@9789
   638
        if (xinerama_triggered_error) {
icculus@9789
   639
            xinerama = 0;
icculus@9789
   640
        }
slouken@6548
   641
        if (xinerama) {
slouken@6548
   642
            use_xinerama = xinerama_major * 100 + xinerama_minor;
slouken@2873
   643
        }
slouken@2873
   644
    }
slouken@6548
   645
    if (!xinerama) {
slouken@6548
   646
        screencount = ScreenCount(data->display);
slouken@2873
   647
    }
slouken@6548
   648
#else
slouken@6548
   649
    screencount = ScreenCount(data->display);
slouken@6548
   650
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6548
   651
slouken@6548
   652
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   653
    if (CheckVidMode(data->display, &vm_major, &vm_minor)) {
slouken@6548
   654
        use_vidmode = vm_major * 100 + vm_minor;
slouken@6548
   655
    }
slouken@6548
   656
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   657
gabomdq@9167
   658
    for (snum = 0; snum < screencount; ++snum) {
slouken@6548
   659
        XVisualInfo vinfo;
slouken@6548
   660
        SDL_VideoDisplay display;
slouken@6548
   661
        SDL_DisplayData *displaydata;
slouken@6548
   662
        SDL_DisplayMode mode;
slouken@6548
   663
        SDL_DisplayModeData *modedata;
slouken@6548
   664
        XPixmapFormatValues *pixmapFormats;
slouken@6796
   665
        char display_name[128];
slouken@6548
   666
        int i, n;
slouken@6548
   667
gabomdq@9167
   668
        /* Re-order screens to always put default screen first */
gabomdq@9167
   669
        if (snum == 0) {
gabomdq@9167
   670
            screen = DefaultScreen(data->display);
gabomdq@9167
   671
        } else if (snum == DefaultScreen(data->display)) {
gabomdq@9167
   672
            screen = 0;
gabomdq@9167
   673
        } else {
gabomdq@9167
   674
            screen = snum;
gabomdq@9167
   675
        }
gabomdq@9167
   676
slouken@6548
   677
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   678
        if (xinerama) {
slouken@6548
   679
            if (get_visualinfo(data->display, 0, &vinfo) < 0) {
slouken@6548
   680
                continue;
slouken@6548
   681
            }
slouken@6548
   682
        } else {
slouken@6548
   683
            if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@6548
   684
                continue;
slouken@6548
   685
            }
slouken@6548
   686
        }
slouken@6548
   687
#else
slouken@6548
   688
        if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@6548
   689
            continue;
slouken@6548
   690
        }
slouken@6548
   691
#endif
slouken@6548
   692
slouken@6548
   693
        displaydata = (SDL_DisplayData *) SDL_calloc(1, sizeof(*displaydata));
slouken@6548
   694
        if (!displaydata) {
slouken@6548
   695
            continue;
slouken@6548
   696
        }
slouken@6796
   697
        display_name[0] = '\0';
slouken@6548
   698
slouken@6548
   699
        mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
slouken@6548
   700
        if (SDL_ISPIXELFORMAT_INDEXED(mode.format)) {
slouken@6548
   701
            /* We don't support palettized modes now */
slouken@6548
   702
            SDL_free(displaydata);
slouken@6548
   703
            continue;
slouken@6548
   704
        }
slouken@6548
   705
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   706
        if (xinerama) {
slouken@6548
   707
            mode.w = xinerama[screen].width;
slouken@6548
   708
            mode.h = xinerama[screen].height;
slouken@6548
   709
        } else {
slouken@6548
   710
            mode.w = DisplayWidth(data->display, screen);
slouken@6548
   711
            mode.h = DisplayHeight(data->display, screen);
slouken@6548
   712
        }
slouken@6548
   713
#else
slouken@6548
   714
        mode.w = DisplayWidth(data->display, screen);
slouken@6548
   715
        mode.h = DisplayHeight(data->display, screen);
slouken@6548
   716
#endif
slouken@6548
   717
        mode.refresh_rate = 0;
slouken@6548
   718
slouken@6548
   719
        modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   720
        if (!modedata) {
slouken@6548
   721
            SDL_free(displaydata);
slouken@6548
   722
            continue;
slouken@6548
   723
        }
slouken@6548
   724
        mode.driverdata = modedata;
slouken@6548
   725
slouken@6548
   726
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   727
        /* Most of SDL's calls to X11 are unwaware of Xinerama, and to X11 standard calls, when Xinerama is active,
slouken@6548
   728
         * there's only one screen available. So we force the screen number to zero and
slouken@6548
   729
         * let Xinerama specific code handle specific functionality using displaydata->xinerama_info
slouken@6548
   730
         */
slouken@6548
   731
        if (use_xinerama) {
slouken@6548
   732
            displaydata->screen = 0;
slouken@6548
   733
            displaydata->use_xinerama = use_xinerama;
slouken@6548
   734
            displaydata->xinerama_info = xinerama[screen];
slouken@6548
   735
            displaydata->xinerama_screen = screen;
slouken@6548
   736
        }
slouken@6548
   737
        else displaydata->screen = screen;
slouken@6548
   738
#else
slouken@6548
   739
        displaydata->screen = screen;
slouken@6548
   740
#endif
slouken@6548
   741
        displaydata->visual = vinfo.visual;
slouken@6548
   742
        displaydata->depth = vinfo.depth;
slouken@6548
   743
alfred@9814
   744
        // We use the displaydata screen index here so that this works
alfred@9814
   745
        // for both the Xinerama case, where we get the overall DPI,
alfred@9814
   746
        // and the regular X11 screen info case.
alfred@9814
   747
        displaydata->hdpi = (float)DisplayWidth(data->display, displaydata->screen) * 25.4f /
alfred@9814
   748
            DisplayWidthMM(data->display, displaydata->screen);
alfred@9814
   749
        displaydata->vdpi = (float)DisplayHeight(data->display, displaydata->screen) * 25.4f /
alfred@9814
   750
            DisplayHeightMM(data->display, displaydata->screen);
alfred@9814
   751
        displaydata->ddpi = SDL_ComputeDiagonalDPI(DisplayWidth(data->display, displaydata->screen),
alfred@9814
   752
                                                   DisplayHeight(data->display, displaydata->screen),
alfred@9814
   753
                                                   (float)DisplayWidthMM(data->display, displaydata->screen) / 25.4f,
alfred@9814
   754
                                                   (float)DisplayHeightMM(data->display, displaydata->screen) / 25.4f);
alfred@9814
   755
slouken@6548
   756
        displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
icculus@7827
   757
        pixmapFormats = X11_XListPixmapFormats(data->display, &n);
slouken@6548
   758
        if (pixmapFormats) {
slouken@6548
   759
            for (i = 0; i < n; ++i) {
slouken@6548
   760
                if (pixmapFormats[i].depth == displaydata->depth) {
slouken@6548
   761
                    displaydata->scanline_pad = pixmapFormats[i].scanline_pad;
slouken@6548
   762
                    break;
slouken@6548
   763
                }
slouken@6548
   764
            }
icculus@7827
   765
            X11_XFree(pixmapFormats);
slouken@6548
   766
        }
slouken@6548
   767
slouken@6548
   768
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
   769
        if (use_xinerama) {
slouken@6548
   770
            displaydata->x = xinerama[screen].x_org;
slouken@6548
   771
            displaydata->y = xinerama[screen].y_org;
slouken@6548
   772
        }
slouken@6548
   773
        else
slouken@6548
   774
#endif
slouken@6548
   775
        {
slouken@6548
   776
            displaydata->x = 0;
slouken@6548
   777
            displaydata->y = 0;
slouken@6548
   778
        }
slouken@6548
   779
slouken@6548
   780
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   781
        if (!displaydata->use_xrandr &&
slouken@6548
   782
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6564
   783
            /* XVidMode only works on the screen at the origin */
slouken@6564
   784
            (!displaydata->use_xinerama ||
slouken@6564
   785
             (displaydata->x == 0 && displaydata->y == 0)) &&
slouken@6548
   786
#endif
slouken@6548
   787
            use_vidmode) {
slouken@6548
   788
            displaydata->use_vidmode = use_vidmode;
slouken@6565
   789
            if (displaydata->use_xinerama) {
slouken@6565
   790
                displaydata->vidmode_screen = 0;
slouken@6565
   791
            } else {
slouken@6565
   792
                displaydata->vidmode_screen = screen;
slouken@6565
   793
            }
slouken@6565
   794
            XF86VidModeGetModeInfo(data->display, displaydata->vidmode_screen, &modedata->vm_mode);
slouken@6548
   795
        }
slouken@6548
   796
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
   797
slouken@6548
   798
        SDL_zero(display);
slouken@6796
   799
        if (*display_name) {
slouken@6796
   800
            display.name = display_name;
slouken@6796
   801
        }
slouken@6548
   802
        display.desktop_mode = mode;
slouken@6548
   803
        display.current_mode = mode;
slouken@6548
   804
        display.driverdata = displaydata;
slouken@6548
   805
        SDL_AddVideoDisplay(&display);
slouken@6548
   806
    }
slouken@6548
   807
slouken@6548
   808
#if SDL_VIDEO_DRIVER_X11_XINERAMA
icculus@7827
   809
    if (xinerama) X11_XFree(xinerama);
slouken@6548
   810
#endif
slouken@6548
   811
slouken@6548
   812
    if (_this->num_displays == 0) {
icculus@7037
   813
        return SDL_SetError("No available displays");
slouken@6548
   814
    }
slouken@6548
   815
    return 0;
slouken@2873
   816
}
slouken@2873
   817
slouken@1950
   818
void
slouken@3500
   819
X11_GetDisplayModes(_THIS, SDL_VideoDisplay * sdl_display)
slouken@1950
   820
{
slouken@2870
   821
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@3500
   822
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@5408
   823
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@2873
   824
    int nmodes;
slouken@5408
   825
    XF86VidModeModeInfo ** modes;
slouken@2873
   826
#endif
slouken@2873
   827
    int screen_w;
slouken@2873
   828
    int screen_h;
slouken@1950
   829
    SDL_DisplayMode mode;
slouken@2870
   830
slouken@2873
   831
    /* Unfortunately X11 requires the window to be created with the correct
slouken@2873
   832
     * visual and depth ahead of time, but the SDL API allows you to create
slouken@2873
   833
     * a window before setting the fullscreen display mode.  This means that
slouken@2873
   834
     * we have to use the same format for all windows and all display modes.
slouken@2873
   835
     * (or support recreating the window with a new visual behind the scenes)
slouken@2873
   836
     */
slouken@3500
   837
    mode.format = sdl_display->current_mode.format;
slouken@2873
   838
    mode.driverdata = NULL;
slouken@2873
   839
slouken@2873
   840
    screen_w = DisplayWidth(display, data->screen);
slouken@2873
   841
    screen_h = DisplayHeight(display, data->screen);
slouken@2873
   842
slouken@2873
   843
#if SDL_VIDEO_DRIVER_X11_XINERAMA
gabomdq@6331
   844
    if (data->use_xinerama) {
david@7361
   845
        if (data->use_vidmode && !data->xinerama_info.x_org && !data->xinerama_info.y_org &&
gabomdq@6331
   846
           (screen_w > data->xinerama_info.width || screen_h > data->xinerama_info.height)) {
slouken@8922
   847
            SDL_DisplayModeData *modedata;
david@7361
   848
            /* Add the full (both screens combined) xinerama mode only on the display that starts at 0,0
david@7361
   849
             * if we're using vidmode.
david@7361
   850
             */
gabomdq@6331
   851
            mode.w = screen_w;
gabomdq@6331
   852
            mode.h = screen_h;
slouken@2873
   853
            mode.refresh_rate = 0;
slouken@6548
   854
            modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   855
            if (modedata) {
slouken@6548
   856
                *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
slouken@6548
   857
            }
slouken@6548
   858
            mode.driverdata = modedata;
icculus@9832
   859
            if (!SDL_AddDisplayMode(sdl_display, &mode)) {
icculus@9832
   860
                SDL_free(modedata);
icculus@9832
   861
            }
slouken@2873
   862
        }
david@7431
   863
        else if (!data->use_xrandr)
david@7361
   864
        {
slouken@8922
   865
            SDL_DisplayModeData *modedata;
david@7431
   866
            /* Add the current mode of each monitor otherwise if we can't get them from xrandr */
david@7361
   867
            mode.w = data->xinerama_info.width;
david@7361
   868
            mode.h = data->xinerama_info.height;
david@7361
   869
            mode.refresh_rate = 0;
david@7361
   870
            modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
david@7361
   871
            if (modedata) {
david@7361
   872
                *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
david@7361
   873
            }
david@7361
   874
            mode.driverdata = modedata;
icculus@9832
   875
            if (!SDL_AddDisplayMode(sdl_display, &mode)) {
icculus@9832
   876
                SDL_free(modedata);
icculus@9832
   877
            }
david@7361
   878
        }
david@7361
   879
slouken@2873
   880
    }
slouken@2873
   881
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   882
slouken@2873
   883
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   884
    if (data->use_xrandr) {
slouken@6548
   885
        XRRScreenResources *res;
slouken@2873
   886
icculus@7827
   887
        res = X11_XRRGetScreenResources (display, RootWindow(display, data->screen));
slouken@6548
   888
        if (res) {
slouken@6548
   889
            SDL_DisplayModeData *modedata;
slouken@6548
   890
            XRROutputInfo *output_info;
slouken@6548
   891
            int i;
slouken@2873
   892
icculus@7827
   893
            output_info = X11_XRRGetOutputInfo(display, res, data->xrandr_output);
slouken@6548
   894
            if (output_info && output_info->connection != RR_Disconnected) {
slouken@6548
   895
                for (i = 0; i < output_info->nmode; ++i) {
slouken@6548
   896
                    modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   897
                    if (!modedata) {
slouken@6548
   898
                        continue;
slouken@6548
   899
                    }
slouken@6548
   900
                    mode.driverdata = modedata;
slouken@2873
   901
icculus@9834
   902
                    if (!SetXRandRModeInfo(display, res, output_info->crtc, output_info->modes[i], &mode) ||
icculus@9832
   903
                        !SDL_AddDisplayMode(sdl_display, &mode)) {
slouken@6548
   904
                        SDL_free(modedata);
slouken@6548
   905
                    }
slouken@2873
   906
                }
slouken@2873
   907
            }
icculus@7827
   908
            X11_XRRFreeOutputInfo(output_info);
icculus@7827
   909
            X11_XRRFreeScreenResources(res);
slouken@2873
   910
        }
slouken@6548
   911
        return;
slouken@2873
   912
    }
slouken@2873
   913
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   914
slouken@5408
   915
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
   916
    if (data->use_vidmode &&
icculus@7827
   917
        X11_XF86VidModeGetAllModeLines(display, data->vidmode_screen, &nmodes, &modes)) {
slouken@2873
   918
        int i;
slouken@8922
   919
        SDL_DisplayModeData *modedata;
slouken@2873
   920
slouken@2873
   921
#ifdef X11MODES_DEBUG
slouken@2873
   922
        printf("VidMode modes: (unsorted)\n");
slouken@2873
   923
        for (i = 0; i < nmodes; ++i) {
slouken@6566
   924
            printf("Mode %d: %d x %d @ %d, flags: 0x%x\n", i,
slouken@2874
   925
                   modes[i]->hdisplay, modes[i]->vdisplay,
slouken@6566
   926
                   CalculateXVidModeRefreshRate(modes[i]), modes[i]->flags);
slouken@2873
   927
        }
slouken@2873
   928
#endif
slouken@2873
   929
        for (i = 0; i < nmodes; ++i) {
slouken@6548
   930
            modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   931
            if (!modedata) {
slouken@6548
   932
                continue;
slouken@6548
   933
            }
slouken@6548
   934
            mode.driverdata = modedata;
slouken@6548
   935
icculus@9832
   936
            if (!SetXVidModeModeInfo(modes[i], &mode) || !SDL_AddDisplayMode(sdl_display, &mode)) {
slouken@6548
   937
                SDL_free(modedata);
slouken@6548
   938
            }
slouken@2873
   939
        }
icculus@7827
   940
        X11_XFree(modes);
slouken@6548
   941
        return;
slouken@2873
   942
    }
slouken@5408
   943
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@2873
   944
slouken@2873
   945
    if (!data->use_xrandr && !data->use_vidmode) {
slouken@8922
   946
        SDL_DisplayModeData *modedata;
slouken@6548
   947
        /* Add the desktop mode */
slouken@6548
   948
        mode = sdl_display->desktop_mode;
slouken@6548
   949
        modedata = (SDL_DisplayModeData *) SDL_calloc(1, sizeof(SDL_DisplayModeData));
slouken@6548
   950
        if (modedata) {
slouken@6548
   951
            *modedata = *(SDL_DisplayModeData *)sdl_display->desktop_mode.driverdata;
slouken@6548
   952
        }
slouken@6548
   953
        mode.driverdata = modedata;
icculus@9832
   954
        if (!SDL_AddDisplayMode(sdl_display, &mode)) {
icculus@9832
   955
            SDL_free(modedata);
icculus@9832
   956
        }
slouken@2874
   957
    }
slouken@1950
   958
}
slouken@1950
   959
slouken@1950
   960
int
slouken@3500
   961
X11_SetDisplayMode(_THIS, SDL_VideoDisplay * sdl_display, SDL_DisplayMode * mode)
slouken@1950
   962
{
icculus@9319
   963
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
icculus@9319
   964
    Display *display = viddata->display;
slouken@3500
   965
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@6548
   966
    SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
slouken@2873
   967
icculus@9319
   968
    viddata->last_mode_change_deadline = SDL_GetTicks() + (PENDING_FOCUS_TIME * 2);
icculus@9319
   969
slouken@6548
   970
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@6548
   971
    if (data->use_xrandr) {
slouken@6548
   972
        XRRScreenResources *res;
slouken@6548
   973
        XRROutputInfo *output_info;
slouken@6548
   974
        XRRCrtcInfo *crtc;
slouken@6548
   975
        Status status;
slouken@6548
   976
icculus@7827
   977
        res = X11_XRRGetScreenResources (display, RootWindow(display, data->screen));
slouken@6548
   978
        if (!res) {
icculus@7037
   979
            return SDL_SetError("Couldn't get XRandR screen resources");
slouken@6548
   980
        }
slouken@6548
   981
icculus@7827
   982
        output_info = X11_XRRGetOutputInfo(display, res, data->xrandr_output);
slouken@6548
   983
        if (!output_info || output_info->connection == RR_Disconnected) {
icculus@7827
   984
            X11_XRRFreeScreenResources(res);
icculus@7037
   985
            return SDL_SetError("Couldn't get XRandR output info");
slouken@6548
   986
        }
slouken@6548
   987
icculus@7827
   988
        crtc = X11_XRRGetCrtcInfo(display, res, output_info->crtc);
slouken@6548
   989
        if (!crtc) {
icculus@7827
   990
            X11_XRRFreeOutputInfo(output_info);
icculus@7827
   991
            X11_XRRFreeScreenResources(res);
icculus@7037
   992
            return SDL_SetError("Couldn't get XRandR crtc info");
slouken@6548
   993
        }
slouken@6548
   994
icculus@7827
   995
        status = X11_XRRSetCrtcConfig (display, res, output_info->crtc, CurrentTime,
slouken@6548
   996
          crtc->x, crtc->y, modedata->xrandr_mode, crtc->rotation,
slouken@6548
   997
          &data->xrandr_output, 1);
slouken@6548
   998
icculus@7827
   999
        X11_XRRFreeCrtcInfo(crtc);
icculus@7827
  1000
        X11_XRRFreeOutputInfo(output_info);
icculus@7827
  1001
        X11_XRRFreeScreenResources(res);
slouken@6548
  1002
slouken@6548
  1003
        if (status != Success) {
icculus@7827
  1004
            return SDL_SetError("X11_XRRSetCrtcConfig failed");
slouken@6548
  1005
        }
slouken@6548
  1006
    }
slouken@6548
  1007
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@6548
  1008
slouken@6548
  1009
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
slouken@6548
  1010
    if (data->use_vidmode) {
icculus@7827
  1011
        X11_XF86VidModeSwitchToMode(display, data->vidmode_screen, &modedata->vm_mode);
slouken@6548
  1012
    }
slouken@6548
  1013
#endif /* SDL_VIDEO_DRIVER_X11_XVIDMODE */
slouken@6548
  1014
slouken@2873
  1015
    return 0;
slouken@1950
  1016
}
slouken@1950
  1017
slouken@1950
  1018
void
slouken@1950
  1019
X11_QuitModes(_THIS)
slouken@1950
  1020
{
slouken@1950
  1021
}
slouken@1950
  1022
gabomdq@6331
  1023
int
gabomdq@6331
  1024
X11_GetDisplayBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
gabomdq@6331
  1025
{
gabomdq@6331
  1026
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
slouken@6502
  1027
slouken@6548
  1028
    rect->x = data->x;
slouken@6548
  1029
    rect->y = data->y;
slouken@6548
  1030
    rect->w = sdl_display->current_mode.w;
slouken@6548
  1031
    rect->h = sdl_display->current_mode.h;
gabomdq@6331
  1032
gabomdq@6331
  1033
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@6548
  1034
    /* Get the real current bounds of the display */
slouken@6502
  1035
    if (data->use_xinerama) {
icculus@7485
  1036
        Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@6548
  1037
        int screencount;
icculus@7827
  1038
        XineramaScreenInfo *xinerama = X11_XineramaQueryScreens(display, &screencount);
slouken@6548
  1039
        if (xinerama) {
slouken@6548
  1040
            rect->x = xinerama[data->xinerama_screen].x_org;
slouken@6548
  1041
            rect->y = xinerama[data->xinerama_screen].y_org;
icculus@7827
  1042
            X11_XFree(xinerama);
slouken@6548
  1043
        }
gabomdq@6331
  1044
    }
slouken@6548
  1045
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@6475
  1046
    return 0;
gabomdq@6331
  1047
}
gabomdq@6331
  1048
alfred@9814
  1049
int
alfred@9814
  1050
X11_GetDisplayDPI(_THIS, SDL_VideoDisplay * sdl_display, float * ddpi, float * hdpi, float * vdpi)
alfred@9814
  1051
{
alfred@9814
  1052
    SDL_DisplayData *data = (SDL_DisplayData *) sdl_display->driverdata;
alfred@9814
  1053
alfred@9814
  1054
    if (ddpi) {
alfred@9814
  1055
        *ddpi = data->ddpi;
alfred@9814
  1056
    }
alfred@9814
  1057
    if (hdpi) {
alfred@9814
  1058
        *hdpi = data->hdpi;
alfred@9814
  1059
    }
alfred@9814
  1060
    if (vdpi) {
alfred@9815
  1061
        *vdpi = data->vdpi;
alfred@9814
  1062
    }
alfred@9814
  1063
philipp@10172
  1064
    return data->ddpi != 0.0f ? 0 : SDL_SetError("Couldn't get DPI");
alfred@9814
  1065
}
alfred@9814
  1066
icculus@10019
  1067
int
icculus@10019
  1068
X11_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * sdl_display, SDL_Rect * rect)
icculus@10019
  1069
{
icculus@10019
  1070
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
icculus@10019
  1071
    Display *display = data->display;
icculus@10019
  1072
    Atom _NET_WORKAREA;
icculus@10019
  1073
    int status, real_format;
icculus@10019
  1074
    int retval = -1;
icculus@10019
  1075
    Atom real_type;
icculus@10019
  1076
    unsigned long items_read = 0, items_left = 0;
icculus@10019
  1077
    unsigned char *propdata = NULL;
icculus@10019
  1078
icculus@10019
  1079
    if (X11_GetDisplayBounds(_this, sdl_display, rect) < 0) {
icculus@10019
  1080
        return -1;
icculus@10019
  1081
    }
icculus@10019
  1082
icculus@10019
  1083
    _NET_WORKAREA = X11_XInternAtom(display, "_NET_WORKAREA", False);
icculus@10019
  1084
    status = X11_XGetWindowProperty(display, DefaultRootWindow(display),
icculus@10019
  1085
                                    _NET_WORKAREA, 0L, 4L, False, XA_CARDINAL,
icculus@10019
  1086
                                    &real_type, &real_format, &items_read,
icculus@10019
  1087
                                    &items_left, &propdata);
icculus@10019
  1088
    if ((status == Success) && (items_read >= 4)) {
icculus@10019
  1089
        const long *p = (long*) propdata;
icculus@10019
  1090
        const SDL_Rect usable = { (int)p[0], (int)p[1], (int)p[2], (int)p[3] };
icculus@10031
  1091
        retval = 0;
icculus@10019
  1092
        if (!SDL_IntersectRect(rect, &usable, rect)) {
icculus@10019
  1093
            SDL_zerop(rect);
icculus@10019
  1094
        }
icculus@10019
  1095
    }
icculus@10019
  1096
icculus@10019
  1097
    if (propdata) {
icculus@10019
  1098
        X11_XFree(propdata);
icculus@10019
  1099
    }
icculus@10019
  1100
icculus@10019
  1101
    return retval;
icculus@10019
  1102
}
icculus@10019
  1103
slouken@5481
  1104
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1105
slouken@1950
  1106
/* vi: set ts=4 sw=4 expandtab: */