src/video/x11/SDL_x11modes.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Aug 2009 18:39:57 +0000
changeset 3227 458e53d8662c
parent 2963 ee331407574f
child 3500 4b594623401b
permissions -rw-r--r--
Clarified API documentation
slouken@1950
     1
/*
slouken@1950
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1950
     4
slouken@1950
     5
    This library is free software; you can redistribute it and/or
slouken@1950
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1950
     7
    License as published by the Free Software Foundation; either
slouken@1950
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1950
     9
slouken@1950
    10
    This library is distributed in the hope that it will be useful,
slouken@1950
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1950
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1950
    13
    Lesser General Public License for more details.
slouken@1950
    14
slouken@1950
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1950
    16
    License along with this library; if not, write to the Free Software
slouken@1950
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1950
    18
slouken@1950
    19
    Sam Lantinga
slouken@1950
    20
    slouken@libsdl.org
slouken@1950
    21
*/
slouken@1950
    22
#include "SDL_config.h"
slouken@1950
    23
slouken@1950
    24
#include "SDL_x11video.h"
slouken@1950
    25
slouken@2873
    26
//#define X11MODES_DEBUG
slouken@2873
    27
#undef SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
    28
#undef SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
    29
#undef SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@1950
    30
slouken@1950
    31
static int
slouken@1950
    32
get_visualinfo(Display * display, int screen, XVisualInfo * vinfo)
slouken@1950
    33
{
slouken@1950
    34
    const char *visual_id = SDL_getenv("SDL_VIDEO_X11_VISUALID");
slouken@1950
    35
    int depth;
slouken@1950
    36
slouken@1950
    37
    /* Look for an exact visual, if requested */
slouken@1950
    38
    if (visual_id) {
slouken@1950
    39
        XVisualInfo *vi, template;
slouken@1950
    40
        int nvis;
slouken@1950
    41
slouken@1950
    42
        SDL_zero(template);
slouken@1950
    43
        template.visualid = SDL_strtol(visual_id, NULL, 0);
slouken@1950
    44
        vi = XGetVisualInfo(display, VisualIDMask, &template, &nvis);
slouken@1950
    45
        if (vi) {
slouken@1950
    46
            *vinfo = *vi;
slouken@1950
    47
            XFree(vi);
slouken@1950
    48
            return 0;
slouken@1950
    49
        }
slouken@1950
    50
    }
slouken@1950
    51
slouken@1950
    52
    depth = DefaultDepth(display, screen);
slouken@2963
    53
    if ((X11_UseDirectColorVisuals() &&
slouken@1950
    54
         XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
slouken@1950
    55
        XMatchVisualInfo(display, screen, depth, TrueColor, vinfo) ||
slouken@1950
    56
        XMatchVisualInfo(display, screen, depth, PseudoColor, vinfo) ||
slouken@1950
    57
        XMatchVisualInfo(display, screen, depth, StaticColor, vinfo)) {
slouken@1950
    58
        return 0;
slouken@1950
    59
    }
slouken@1950
    60
    return -1;
slouken@1950
    61
}
slouken@1950
    62
slouken@2870
    63
static Uint32
slouken@2874
    64
X11_GetPixelFormatFromVisualInfo(Display * display, XVisualInfo * vinfo)
slouken@2870
    65
{
slouken@2870
    66
    if (vinfo->class == DirectColor || vinfo->class == TrueColor) {
slouken@2870
    67
        int bpp;
slouken@2870
    68
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@2870
    69
slouken@2870
    70
        Rmask = vinfo->visual->red_mask;
slouken@2870
    71
        Gmask = vinfo->visual->green_mask;
slouken@2870
    72
        Bmask = vinfo->visual->blue_mask;
slouken@2870
    73
        if (vinfo->depth == 32) {
slouken@2870
    74
            Amask = (0xFFFFFFFF & ~(Rmask | Gmask | Bmask));
slouken@2870
    75
        } else {
slouken@2870
    76
            Amask = 0;
slouken@2870
    77
        }
slouken@2870
    78
slouken@2870
    79
        bpp = vinfo->depth;
slouken@2870
    80
        if (bpp == 24) {
slouken@2870
    81
            int i, n;
slouken@2870
    82
            XPixmapFormatValues *p = XListPixmapFormats(display, &n);
slouken@2870
    83
            if (p) {
slouken@2870
    84
                for (i = 0; i < n; ++i) {
slouken@2870
    85
                    if (p[i].depth == 24) {
slouken@2870
    86
                        bpp = p[i].bits_per_pixel;
slouken@2870
    87
                        break;
slouken@2870
    88
                    }
slouken@2870
    89
                }
slouken@2870
    90
                XFree(p);
slouken@2870
    91
            }
slouken@2870
    92
        }
slouken@2870
    93
slouken@2870
    94
        return SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask);
slouken@2870
    95
    }
slouken@2870
    96
slouken@2870
    97
    if (vinfo->class == PseudoColor || vinfo->class == StaticColor) {
slouken@2870
    98
        switch (vinfo->depth) {
slouken@2870
    99
        case 8:
slouken@2870
   100
            return SDL_PIXELTYPE_INDEX8;
slouken@2870
   101
        case 4:
slouken@2870
   102
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   103
                return SDL_PIXELFORMAT_INDEX4LSB;
slouken@2870
   104
            } else {
slouken@2870
   105
                return SDL_PIXELFORMAT_INDEX4MSB;
slouken@2870
   106
            }
slouken@2870
   107
            break;
slouken@2870
   108
        case 1:
slouken@2870
   109
            if (BitmapBitOrder(display) == LSBFirst) {
slouken@2870
   110
                return SDL_PIXELFORMAT_INDEX1LSB;
slouken@2870
   111
            } else {
slouken@2870
   112
                return SDL_PIXELFORMAT_INDEX1MSB;
slouken@2870
   113
            }
slouken@2870
   114
            break;
slouken@2870
   115
        }
slouken@2870
   116
    }
slouken@2870
   117
slouken@2870
   118
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@2870
   119
}
slouken@2870
   120
slouken@1950
   121
void
slouken@1950
   122
X11_InitModes(_THIS)
slouken@1950
   123
{
slouken@1950
   124
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1950
   125
    int screen;
slouken@1950
   126
slouken@1950
   127
    for (screen = 0; screen < ScreenCount(data->display); ++screen) {
slouken@1950
   128
        XVisualInfo vinfo;
slouken@1950
   129
        SDL_VideoDisplay display;
slouken@1950
   130
        SDL_DisplayData *displaydata;
slouken@1950
   131
        SDL_DisplayMode mode;
slouken@2916
   132
        XPixmapFormatValues *pixmapFormats;
slouken@2916
   133
        int i, n;
slouken@1950
   134
slouken@1950
   135
        if (get_visualinfo(data->display, screen, &vinfo) < 0) {
slouken@1950
   136
            continue;
slouken@1950
   137
        }
slouken@1950
   138
slouken@2870
   139
        mode.format = X11_GetPixelFormatFromVisualInfo(data->display, &vinfo);
slouken@1950
   140
        mode.w = DisplayWidth(data->display, screen);
slouken@1950
   141
        mode.h = DisplayHeight(data->display, screen);
slouken@1950
   142
        mode.refresh_rate = 0;
slouken@1950
   143
        mode.driverdata = NULL;
slouken@1950
   144
slouken@1950
   145
        displaydata = (SDL_DisplayData *) SDL_malloc(sizeof(*displaydata));
slouken@1950
   146
        if (!displaydata) {
slouken@1950
   147
            continue;
slouken@1950
   148
        }
slouken@1950
   149
        displaydata->screen = screen;
slouken@1950
   150
        displaydata->visual = vinfo.visual;
slouken@1951
   151
        displaydata->depth = vinfo.depth;
slouken@1950
   152
slouken@2920
   153
        displaydata->scanline_pad = SDL_BYTESPERPIXEL(mode.format) * 8;
slouken@2916
   154
        pixmapFormats = XListPixmapFormats(data->display, &n);
slouken@2916
   155
        if (pixmapFormats) {
slouken@2916
   156
            for (i = 0; i < n; ++i) {
slouken@2916
   157
                if (pixmapFormats[i].depth == displaydata->depth) {
slouken@2916
   158
                    displaydata->scanline_pad = pixmapFormats[i].scanline_pad;
slouken@2916
   159
                    break;
slouken@2916
   160
                }
slouken@2916
   161
            }
slouken@2916
   162
            XFree(pixmapFormats);
slouken@2916
   163
        }
slouken@2916
   164
slouken@1950
   165
        SDL_zero(display);
slouken@1950
   166
        display.desktop_mode = mode;
slouken@1950
   167
        display.current_mode = mode;
slouken@1950
   168
        display.driverdata = displaydata;
slouken@1950
   169
        SDL_AddVideoDisplay(&display);
slouken@1950
   170
    }
slouken@1950
   171
}
slouken@1950
   172
slouken@2873
   173
/* Global for the error handler */
slouken@2873
   174
int vm_event, vm_error = -1;
slouken@2873
   175
slouken@2873
   176
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   177
static SDL_bool
slouken@2874
   178
CheckXinerama(Display * display, int *major, int *minor)
slouken@2873
   179
{
slouken@2873
   180
    const char *env;
slouken@2873
   181
slouken@2873
   182
    /* Default the extension not available */
slouken@2873
   183
    *major = *minor = 0;
slouken@2873
   184
slouken@2873
   185
    /* Allow environment override */
slouken@2873
   186
    env = getenv("SDL_VIDEO_X11_XINERAMA");
slouken@2873
   187
    if (env && !SDL_atoi(env)) {
slouken@2873
   188
        return SDL_FALSE;
slouken@2873
   189
    }
slouken@2873
   190
slouken@2873
   191
    /* Query the extension version */
slouken@2874
   192
    if (!SDL_NAME(XineramaQueryExtension) (display, major, minor) ||
slouken@2874
   193
        !SDL_NAME(XineramaIsActive) (display)) {
slouken@2873
   194
        return SDL_FALSE;
slouken@2873
   195
    }
slouken@2873
   196
    return SDL_TRUE;
slouken@2873
   197
}
slouken@2873
   198
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   199
slouken@2873
   200
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   201
static SDL_bool
slouken@2874
   202
CheckXRandR(Display * display, int *major, int *minor)
slouken@2873
   203
{
slouken@2873
   204
    const char *env;
slouken@2873
   205
slouken@2873
   206
    /* Default the extension not available */
slouken@2873
   207
    *major = *minor = 0;
slouken@2873
   208
slouken@2873
   209
    /* Allow environment override */
slouken@2873
   210
    env = getenv("SDL_VIDEO_X11_XRANDR");
slouken@2873
   211
    if (env && !SDL_atoi(env)) {
slouken@2873
   212
        return SDL_FALSE;
slouken@2873
   213
    }
slouken@2873
   214
slouken@2873
   215
    if (!SDL_X11_HAVE_XRANDR) {
slouken@2873
   216
        return SDL_FALSE;
slouken@2873
   217
    }
slouken@2873
   218
slouken@2873
   219
    /* Query the extension version */
slouken@2873
   220
    if (!XRRQueryVersion(display, major, minor)) {
slouken@2873
   221
        return SDL_FALSE;
slouken@2873
   222
    }
slouken@2873
   223
    return SDL_TRUE;
slouken@2873
   224
}
slouken@2873
   225
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   226
slouken@2873
   227
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@2873
   228
static SDL_bool
slouken@2874
   229
CheckVidMode(Display * display, int *major, int *minor)
slouken@2873
   230
{
slouken@2873
   231
    const char *env;
slouken@2873
   232
slouken@2873
   233
    /* Default the extension not available */
slouken@2873
   234
    *major = *minor = 0;
slouken@2873
   235
slouken@2873
   236
    /* Allow environment override */
slouken@2873
   237
    env = getenv("SDL_VIDEO_X11_VIDMODE");
slouken@2873
   238
    if (env && !SDL_atoi(env)) {
slouken@2873
   239
        return SDL_FALSE;
slouken@2873
   240
    }
slouken@2874
   241
slouken@2873
   242
    /* Query the extension version */
slouken@2873
   243
    vm_error = -1;
slouken@2874
   244
    if (!SDL_NAME(XF86VidModeQueryExtension) (display, &vm_event, &vm_error)
slouken@2874
   245
        || !SDL_NAME(XF86VidModeQueryVersion) (display, major, minor)) {
slouken@2873
   246
        return SDL_FALSE;
slouken@2873
   247
    }
slouken@2873
   248
    return SDL_TRUE;
slouken@2873
   249
}
slouken@2873
   250
slouken@2874
   251
Bool SDL_NAME(XF86VidModeGetModeInfo) (Display * dpy, int scr,
slouken@2874
   252
                                       SDL_NAME(XF86VidModeModeInfo) * info)
slouken@2873
   253
{
slouken@2873
   254
    Bool retval;
slouken@2873
   255
    int dotclock;
slouken@2873
   256
    SDL_NAME(XF86VidModeModeLine) l;
slouken@2873
   257
    SDL_zerop(info);
slouken@2873
   258
    SDL_zero(l);
slouken@2874
   259
    retval = SDL_NAME(XF86VidModeGetModeLine) (dpy, scr, &dotclock, &l);
slouken@2873
   260
    info->dotclock = dotclock;
slouken@2873
   261
    info->hdisplay = l.hdisplay;
slouken@2873
   262
    info->hsyncstart = l.hsyncstart;
slouken@2873
   263
    info->hsyncend = l.hsyncend;
slouken@2873
   264
    info->htotal = l.htotal;
slouken@2873
   265
    info->hskew = l.hskew;
slouken@2873
   266
    info->vdisplay = l.vdisplay;
slouken@2873
   267
    info->vsyncstart = l.vsyncstart;
slouken@2873
   268
    info->vsyncend = l.vsyncend;
slouken@2873
   269
    info->vtotal = l.vtotal;
slouken@2873
   270
    info->flags = l.flags;
slouken@2873
   271
    info->privsize = l.privsize;
slouken@2873
   272
    info->private = l.private;
slouken@2873
   273
    return retval;
slouken@2873
   274
}
slouken@2873
   275
slouken@2873
   276
static int
slouken@2874
   277
calculate_rate(SDL_NAME(XF86VidModeModeInfo) * info)
slouken@2873
   278
{
slouken@2874
   279
    return (info->htotal
slouken@2874
   280
            && info->vtotal) ? (1000 * info->dotclock / (info->htotal *
slouken@2874
   281
                                                         info->vtotal)) : 0;
slouken@2873
   282
}
slouken@2873
   283
slouken@2873
   284
static void
slouken@2874
   285
save_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   286
{
slouken@2874
   287
    SDL_NAME(XF86VidModeGetModeInfo) (display, data->screen,
slouken@2874
   288
                                      &data->saved_mode);
slouken@2874
   289
    SDL_NAME(XF86VidModeGetViewPort) (display, data->screen,
slouken@2874
   290
                                      &data->saved_view.x,
slouken@2874
   291
                                      &data->saved_view.y);
slouken@2873
   292
}
slouken@2873
   293
slouken@2873
   294
static void
slouken@2874
   295
restore_mode(Display * display, SDL_DisplayData * data)
slouken@2873
   296
{
slouken@2873
   297
    SDL_NAME(XF86VidModeModeInfo) mode;
slouken@2873
   298
slouken@2874
   299
    if (SDL_NAME(XF86VidModeGetModeInfo) (display, data->screen, &mode)) {
slouken@2873
   300
        if (SDL_memcmp(&mode, &data->saved_mode, sizeof(mode)) != 0) {
slouken@2874
   301
            SDL_NAME(XF86VidModeSwitchToMode) (display, data->screen,
slouken@2874
   302
                                               &data->saved_mode);
slouken@2873
   303
        }
slouken@2873
   304
    }
slouken@2873
   305
    if ((data->saved_view.x != 0) || (data->saved_view.y != 0)) {
slouken@2874
   306
        SDL_NAME(XF86VidModeSetViewPort) (display, data->screen,
slouken@2874
   307
                                          data->saved_view.x,
slouken@2874
   308
                                          data->saved_view.y);
slouken@2873
   309
    }
slouken@2873
   310
}
slouken@2873
   311
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
slouken@2873
   312
slouken@1950
   313
void
slouken@1950
   314
X11_GetDisplayModes(_THIS)
slouken@1950
   315
{
slouken@2870
   316
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@1950
   317
    SDL_DisplayData *data = (SDL_DisplayData *) SDL_CurrentDisplay.driverdata;
slouken@2873
   318
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   319
    int xinerama_major, xinerama_minor;
slouken@2873
   320
    int screens;
slouken@2874
   321
    SDL_NAME(XineramaScreenInfo) * xinerama;
slouken@2873
   322
#endif
slouken@2873
   323
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   324
    int xrandr_major, xrandr_minor;
slouken@2873
   325
    int nsizes, nrates;
slouken@2873
   326
    XRRScreenSize *sizes;
slouken@2873
   327
    short *rates;
slouken@2873
   328
#endif
slouken@2873
   329
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@2873
   330
    int vm_major, vm_minor;
slouken@2873
   331
    int nmodes;
slouken@2874
   332
    SDL_NAME(XF86VidModeModeInfo) ** modes;
slouken@2873
   333
#endif
slouken@2873
   334
    int screen_w;
slouken@2873
   335
    int screen_h;
slouken@1950
   336
    SDL_DisplayMode mode;
slouken@2870
   337
slouken@2873
   338
    /* Unfortunately X11 requires the window to be created with the correct
slouken@2873
   339
     * visual and depth ahead of time, but the SDL API allows you to create
slouken@2873
   340
     * a window before setting the fullscreen display mode.  This means that
slouken@2873
   341
     * we have to use the same format for all windows and all display modes.
slouken@2873
   342
     * (or support recreating the window with a new visual behind the scenes)
slouken@2873
   343
     */
slouken@2873
   344
    mode.format = SDL_CurrentDisplay.current_mode.format;
slouken@2873
   345
    mode.driverdata = NULL;
slouken@2873
   346
slouken@2873
   347
    data->use_xinerama = 0;
slouken@2873
   348
    data->use_xrandr = 0;
slouken@2873
   349
    data->use_vidmode = 0;
slouken@2873
   350
    screen_w = DisplayWidth(display, data->screen);
slouken@2873
   351
    screen_h = DisplayHeight(display, data->screen);
slouken@2873
   352
slouken@2873
   353
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   354
    /* Query Xinerama extention */
slouken@2873
   355
    if (CheckXinerama(display, &xinerama_major, &xinerama_minor)) {
slouken@2873
   356
#ifdef X11MODES_DEBUG
slouken@2873
   357
        printf("X11 detected Xinerama:\n");
slouken@2873
   358
#endif
slouken@2874
   359
        xinerama = SDL_NAME(XineramaQueryScreens) (display, &screens);
slouken@2873
   360
        if (xinerama) {
slouken@2873
   361
            int i;
slouken@2873
   362
            for (i = 0; i < screens; i++) {
slouken@2873
   363
#ifdef X11MODES_DEBUG
slouken@2873
   364
                printf("xinerama %d: %dx%d+%d+%d\n",
slouken@2874
   365
                       xinerama[i].screen_number,
slouken@2874
   366
                       xinerama[i].width, xinerama[i].height,
slouken@2874
   367
                       xinerama[i].x_org, xinerama[i].y_org);
slouken@2873
   368
#endif
slouken@2873
   369
                if (xinerama[i].screen_number == data->screen) {
slouken@2874
   370
                    data->use_xinerama =
slouken@2874
   371
                        xinerama_major * 100 + xinerama_minor;
slouken@2873
   372
                    data->xinerama_info = xinerama[i];
slouken@2873
   373
                }
slouken@2873
   374
            }
slouken@2873
   375
            XFree(xinerama);
slouken@2873
   376
        }
slouken@2873
   377
slouken@2873
   378
        if (data->use_xinerama) {
slouken@2873
   379
            /* Add the full xinerama mode */
slouken@2873
   380
            if (screen_w > data->xinerama_info.width ||
slouken@2873
   381
                screen_h > data->xinerama_info.height) {
slouken@2873
   382
                mode.w = screen_w;
slouken@2873
   383
                mode.h = screen_h;
slouken@2873
   384
                mode.refresh_rate = 0;
slouken@2873
   385
                SDL_AddDisplayMode(_this->current_display, &mode);
slouken@2873
   386
            }
slouken@2873
   387
slouken@2873
   388
            /* Add the head xinerama mode */
slouken@2873
   389
            mode.w = data->xinerama_info.width;
slouken@2873
   390
            mode.h = data->xinerama_info.height;
slouken@2873
   391
            mode.refresh_rate = 0;
slouken@2873
   392
            SDL_AddDisplayMode(_this->current_display, &mode);
slouken@2873
   393
        }
slouken@2873
   394
    }
slouken@2873
   395
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   396
slouken@2873
   397
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   398
    /* XRandR */
slouken@2873
   399
    /* require at least XRandR v1.0 (arbitrary) */
slouken@2874
   400
    if (CheckXRandR(display, &xrandr_major, &xrandr_minor)
slouken@2874
   401
        && xrandr_major >= 1) {
slouken@2873
   402
#ifdef X11MODES_DEBUG
slouken@2873
   403
        fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
slouken@2873
   404
                xrandr_major, xrandr_minor);
slouken@2873
   405
#endif
slouken@2873
   406
slouken@2873
   407
        /* save the screen configuration since we must reference it
slouken@2873
   408
           each time we toggle modes.
slouken@2874
   409
         */
slouken@2874
   410
        data->screen_config =
slouken@2874
   411
            XRRGetScreenInfo(display, RootWindow(display, data->screen));
slouken@2873
   412
slouken@2873
   413
        /* retrieve the list of resolution */
slouken@2873
   414
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   415
        if (nsizes > 0) {
slouken@2873
   416
            int i, j;
slouken@2874
   417
            for (i = 0; i < nsizes; i++) {
slouken@2873
   418
                mode.w = sizes[i].width;
slouken@2873
   419
                mode.h = sizes[i].height;
slouken@2873
   420
slouken@2873
   421
                rates = XRRConfigRates(data->screen_config, i, &nrates);
slouken@2873
   422
                for (j = 0; j < nrates; ++j) {
slouken@2873
   423
                    mode.refresh_rate = rates[j];
slouken@2873
   424
#ifdef X11MODES_DEBUG
slouken@2874
   425
                    fprintf(stderr,
slouken@2874
   426
                            "XRANDR: mode = %4d[%d], w = %4d, h = %4d, rate = %4d\n",
slouken@2873
   427
                            i, j, mode.w, mode.h, mode.refresh_rate);
slouken@2873
   428
#endif
slouken@2873
   429
                    SDL_AddDisplayMode(_this->current_display, &mode);
slouken@2873
   430
                }
slouken@2873
   431
            }
slouken@2873
   432
slouken@2873
   433
            data->use_xrandr = xrandr_major * 100 + xrandr_minor;
slouken@2874
   434
            data->saved_size =
slouken@2874
   435
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   436
                                              &data->saved_rotation);
slouken@2873
   437
            data->saved_rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   438
        }
slouken@2873
   439
    }
slouken@2873
   440
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   441
slouken@2873
   442
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@2873
   443
    /* XVidMode */
slouken@2873
   444
    if (!data->use_xrandr &&
slouken@2873
   445
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   446
        (!data->use_xinerama || data->xinerama_info.screen_number == 0) &&
slouken@2873
   447
#endif
slouken@2873
   448
        CheckVidMode(display, &vm_major, &vm_minor) &&
slouken@2874
   449
        SDL_NAME(XF86VidModeGetAllModeLines) (display, data->screen, &nmodes,
slouken@2874
   450
                                              &modes)) {
slouken@2873
   451
        int i;
slouken@2873
   452
slouken@2873
   453
#ifdef X11MODES_DEBUG
slouken@2873
   454
        printf("VidMode modes: (unsorted)\n");
slouken@2873
   455
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   456
            printf("Mode %d: %d x %d @ %d\n", i,
slouken@2874
   457
                   modes[i]->hdisplay, modes[i]->vdisplay,
slouken@2874
   458
                   calculate_rate(modes[i]));
slouken@2873
   459
        }
slouken@2873
   460
#endif
slouken@2873
   461
        for (i = 0; i < nmodes; ++i) {
slouken@2873
   462
            mode.w = modes[i]->hdisplay;
slouken@2873
   463
            mode.h = modes[i]->vdisplay;
slouken@2873
   464
            mode.refresh_rate = calculate_rate(modes[i]);
slouken@2873
   465
            SDL_AddDisplayMode(_this->current_display, &mode);
slouken@2873
   466
        }
slouken@2873
   467
        XFree(modes);
slouken@2873
   468
slouken@2873
   469
        data->use_vidmode = vm_major * 100 + vm_minor;
slouken@2873
   470
        save_mode(display, data);
slouken@2873
   471
    }
slouken@2873
   472
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
slouken@2873
   473
slouken@2873
   474
    if (!data->use_xrandr && !data->use_vidmode) {
slouken@2873
   475
        mode.w = screen_w;
slouken@2873
   476
        mode.h = screen_h;
slouken@2873
   477
        mode.refresh_rate = 0;
slouken@2873
   478
        SDL_AddDisplayMode(_this->current_display, &mode);
slouken@2874
   479
    }
slouken@2873
   480
#ifdef X11MODES_DEBUG
slouken@2873
   481
    if (data->use_xinerama) {
slouken@2873
   482
        printf("Xinerama is enabled\n");
slouken@2873
   483
    }
slouken@2873
   484
slouken@2873
   485
    if (data->use_xrandr) {
slouken@2873
   486
        printf("XRandR is enabled\n");
slouken@2873
   487
    }
slouken@2873
   488
slouken@2873
   489
    if (data->use_vidmode) {
slouken@2873
   490
        printf("VidMode is enabled\n");
slouken@2873
   491
    }
slouken@2873
   492
#endif /* X11MODES_DEBUG */
slouken@2873
   493
}
slouken@2873
   494
slouken@2873
   495
static void
slouken@2874
   496
get_real_resolution(Display * display, SDL_DisplayData * data, int *w, int *h,
slouken@2874
   497
                    int *rate)
slouken@2873
   498
{
slouken@2873
   499
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   500
    if (data->use_xrandr) {
slouken@2873
   501
        int nsizes;
slouken@2873
   502
        XRRScreenSize *sizes;
slouken@2874
   503
slouken@2873
   504
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   505
        if (nsizes > 0) {
slouken@2873
   506
            int cur_size;
slouken@2873
   507
            Rotation cur_rotation;
slouken@2873
   508
slouken@2874
   509
            cur_size =
slouken@2874
   510
                XRRConfigCurrentConfiguration(data->screen_config,
slouken@2874
   511
                                              &cur_rotation);
slouken@2873
   512
            *w = sizes[cur_size].width;
slouken@2873
   513
            *h = sizes[cur_size].height;
slouken@2873
   514
            *rate = XRRConfigCurrentRate(data->screen_config);
slouken@2873
   515
#ifdef X11MODES_DEBUG
slouken@2874
   516
            fprintf(stderr,
slouken@2874
   517
                    "XRANDR: get_real_resolution: w = %d, h = %d, rate = %d\n",
slouken@2874
   518
                    *w, *h, *rate);
slouken@2873
   519
#endif
slouken@2873
   520
            return;
slouken@2873
   521
        }
slouken@2873
   522
    }
slouken@2873
   523
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   524
slouken@2873
   525
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@2873
   526
    if (data->use_vidmode) {
slouken@2873
   527
        SDL_NAME(XF86VidModeModeInfo) mode;
slouken@2873
   528
slouken@2874
   529
        if (SDL_NAME(XF86VidModeGetModeInfo) (display, data->screen, &mode)) {
slouken@2873
   530
            *w = mode.hdisplay;
slouken@2873
   531
            *h = mode.vdisplay;
slouken@2873
   532
            *rate = calculate_rate(&mode);
slouken@2873
   533
            return;
slouken@2873
   534
        }
slouken@2873
   535
    }
slouken@2873
   536
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
slouken@2873
   537
slouken@2873
   538
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@2873
   539
    if (data->use_xinerama) {
slouken@2873
   540
        *w = data->xinerama_info.width;
slouken@2873
   541
        *h = data->xinerama_info.height;
slouken@2873
   542
        *rate = 0;
slouken@2873
   543
        return;
slouken@2873
   544
    }
slouken@2873
   545
#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
slouken@2873
   546
slouken@2873
   547
    *w = DisplayWidth(display, data->screen);
slouken@2873
   548
    *h = DisplayHeight(display, data->screen);
slouken@2873
   549
    *rate = 0;
slouken@2873
   550
}
slouken@2873
   551
slouken@2873
   552
static void
slouken@2874
   553
set_best_resolution(Display * display, SDL_DisplayData * data, int w, int h,
slouken@2874
   554
                    int rate)
slouken@2873
   555
{
slouken@2873
   556
    int real_w, real_h, real_rate;
slouken@2873
   557
slouken@2873
   558
    /* check current mode so we can avoid uneccessary mode changes */
slouken@2873
   559
    get_real_resolution(display, data, &real_w, &real_h, &real_rate);
slouken@2873
   560
    if (w == real_w && h == real_h && (!rate || rate == real_rate)) {
slouken@2873
   561
        return;
slouken@2873
   562
    }
slouken@2873
   563
#if SDL_VIDEO_DRIVER_X11_XRANDR
slouken@2873
   564
    if (data->use_xrandr) {
slouken@2873
   565
#ifdef X11MODES_DEBUG
slouken@2873
   566
        fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
slouken@2873
   567
                w, h);
slouken@2873
   568
#endif
slouken@2873
   569
        int i, nsizes, nrates;
slouken@2873
   570
        int best;
slouken@2873
   571
        int best_rate;
slouken@2873
   572
        XRRScreenSize *sizes;
slouken@2873
   573
        short *rates;
slouken@2873
   574
slouken@2873
   575
        /* find the smallest resolution that is at least as big as the user requested */
slouken@2873
   576
        best = -1;
slouken@2873
   577
        sizes = XRRConfigSizes(data->screen_config, &nsizes);
slouken@2873
   578
        for (i = 0; i < nsizes; ++i) {
slouken@2873
   579
            if (sizes[i].width < w || sizes[i].height < h) {
slouken@2873
   580
                continue;
slouken@2873
   581
            }
slouken@2873
   582
            if (sizes[i].width == w && sizes[i].height == h) {
slouken@2873
   583
                best = i;
slouken@2873
   584
                break;
slouken@2873
   585
            }
slouken@2873
   586
            if (best == -1 ||
slouken@2873
   587
                (sizes[i].width < sizes[best].width) ||
slouken@2874
   588
                (sizes[i].width == sizes[best].width
slouken@2874
   589
                 && sizes[i].height < sizes[best].height)) {
slouken@2873
   590
                best = i;
slouken@2873
   591
            }
slouken@2873
   592
        }
slouken@2873
   593
slouken@2873
   594
        if (best >= 0) {
slouken@2873
   595
            best_rate = 0;
slouken@2873
   596
            rates = XRRConfigRates(data->screen_config, best, &nrates);
slouken@2873
   597
            for (i = 0; i < nrates; ++i) {
slouken@2873
   598
                if (rates[i] == rate) {
slouken@2873
   599
                    best_rate = rate;
slouken@2873
   600
                    break;
slouken@2873
   601
                }
slouken@2873
   602
                if (!rate) {
slouken@2873
   603
                    /* Higher is better, right? */
slouken@2873
   604
                    if (rates[i] > best_rate) {
slouken@2873
   605
                        best_rate = rates[i];
slouken@2873
   606
                    }
slouken@2873
   607
                } else {
slouken@2874
   608
                    if (SDL_abs(rates[i] - rate) < SDL_abs(best_rate - rate)) {
slouken@2873
   609
                        best_rate = rates[i];
slouken@2873
   610
                    }
slouken@2873
   611
                }
slouken@2873
   612
            }
slouken@2874
   613
            XRRSetScreenConfigAndRate(display, data->screen_config,
slouken@2874
   614
                                      RootWindow(display, data->screen), best,
slouken@2874
   615
                                      data->saved_rotation, best_rate,
slouken@2874
   616
                                      CurrentTime);
slouken@2873
   617
        }
slouken@2873
   618
        return;
slouken@2873
   619
    }
slouken@2873
   620
#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
slouken@2873
   621
slouken@2873
   622
#if SDL_VIDEO_DRIVER_X11_VIDMODE
slouken@2873
   623
    if (data->use_vidmode) {
slouken@2874
   624
        SDL_NAME(XF86VidModeModeInfo) ** modes;
slouken@2873
   625
        int i, nmodes;
slouken@2873
   626
        int best;
slouken@2873
   627
slouken@2874
   628
        if (SDL_NAME(XF86VidModeGetAllModeLines)
slouken@2874
   629
            (display, data->screen, &nmodes, &modes)) {
slouken@2873
   630
            best = -1;
slouken@2873
   631
            for (i = 0; i < nmodes; ++i) {
slouken@2873
   632
                if (modes[i]->hdisplay < w || modes[i]->vdisplay < h) {
slouken@2873
   633
                    continue;
slouken@2873
   634
                }
slouken@2873
   635
                if (best == -1 ||
slouken@2873
   636
                    (modes[i]->hdisplay < modes[best]->hdisplay) ||
slouken@2874
   637
                    (modes[i]->hdisplay == modes[best]->hdisplay
slouken@2874
   638
                     && modes[i]->vdisplay < modes[best]->vdisplay)) {
slouken@2873
   639
                    best = i;
slouken@2873
   640
                    continue;
slouken@2873
   641
                }
slouken@2873
   642
                if ((modes[i]->hdisplay == modes[best]->hdisplay) &&
slouken@2873
   643
                    (modes[i]->vdisplay == modes[best]->vdisplay)) {
slouken@2873
   644
                    if (!rate) {
slouken@2873
   645
                        /* Higher is better, right? */
slouken@2874
   646
                        if (calculate_rate(modes[i]) >
slouken@2874
   647
                            calculate_rate(modes[best])) {
slouken@2873
   648
                            best = i;
slouken@2873
   649
                        }
slouken@2873
   650
                    } else {
slouken@2874
   651
                        if (SDL_abs(calculate_rate(modes[i]) - rate) <
slouken@2874
   652
                            SDL_abs(calculate_rate(modes[best]) - rate)) {
slouken@2873
   653
                            best = i;
slouken@2873
   654
                        }
slouken@2873
   655
                    }
slouken@2873
   656
                }
slouken@2873
   657
            }
slouken@2873
   658
            if (best >= 0) {
slouken@2873
   659
#ifdef X11MODES_DEBUG
slouken@2873
   660
                printf("Best Mode %d: %d x %d @ %d\n", best,
slouken@2874
   661
                       modes[best]->hdisplay, modes[best]->vdisplay,
slouken@2874
   662
                       calculate_rate(modes[best]));
slouken@2873
   663
#endif
slouken@2874
   664
                SDL_NAME(XF86VidModeSwitchToMode) (display, data->screen,
slouken@2874
   665
                                                   modes[best]);
slouken@2873
   666
            }
slouken@2873
   667
            XFree(modes);
slouken@2873
   668
        }
slouken@2873
   669
        return;
slouken@2873
   670
    }
slouken@2873
   671
#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
slouken@1950
   672
}
slouken@1950
   673
slouken@1950
   674
int
slouken@1950
   675
X11_SetDisplayMode(_THIS, SDL_DisplayMode * mode)
slouken@1950
   676
{
slouken@2873
   677
    Display *display = ((SDL_VideoData *) _this->driverdata)->display;
slouken@2873
   678
    SDL_DisplayData *data = (SDL_DisplayData *) SDL_CurrentDisplay.driverdata;
slouken@2873
   679
slouken@2873
   680
    set_best_resolution(display, data, mode->w, mode->h, mode->refresh_rate);
slouken@2873
   681
    return 0;
slouken@1950
   682
}
slouken@1950
   683
slouken@1950
   684
void
slouken@1950
   685
X11_QuitModes(_THIS)
slouken@1950
   686
{
slouken@1950
   687
}
slouken@1950
   688
slouken@1950
   689
/* vi: set ts=4 sw=4 expandtab: */