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