19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Utilities for getting and setting the X display mode */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <stdio.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11video.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11wm_c.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11modes_c.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11image_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*#define X11MODES_DEBUG*/
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX(a, b)        (a > b ? a : b)
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int cmpmodelist(const void *va, const void *vb)
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const SDL_Rect *a = *(const SDL_Rect **)va;
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const SDL_Rect *b = *(const SDL_Rect **)vb;
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( a->w == b->w )
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->h - a->h;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->w - a->w;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallBool SDL_NAME(XF86VidModeGetModeInfo)(Display *dpy, int scr, SDL_NAME(XF86VidModeModeInfo) *info)
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Bool retval;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int dotclock;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_NAME(XF86VidModeModeLine) l;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(&l, 0, sizeof(l));
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    retval = SDL_NAME(XF86VidModeGetModeLine)(dpy, scr, &dotclock, &l);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->dotclock = dotclock;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->hdisplay = l.hdisplay;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->hsyncstart = l.hsyncstart;
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->hsyncend = l.hsyncend;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->htotal = l.htotal;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->hskew = l.hskew;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->vdisplay = l.vdisplay;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->vsyncstart = l.vsyncstart;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->vsyncend = l.vsyncend;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->vtotal = l.vtotal;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->flags = l.flags;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->privsize = l.privsize;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    info->private = l.private;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return retval;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void save_mode(_THIS)
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(&saved_mode, 0, sizeof(saved_mode));
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_NAME(XF86VidModeGetModeInfo)(SDL_Display,SDL_Screen,&saved_mode);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_NAME(XF86VidModeGetViewPort)(SDL_Display,SDL_Screen,&saved_view.x,&saved_view.y);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void restore_mode(_THIS)
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_NAME(XF86VidModeModeLine) mode;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int unused;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &unused, &mode) ) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( (saved_mode.hdisplay != mode.hdisplay) ||
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             (saved_mode.vdisplay != mode.vdisplay) ) {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, &saved_mode);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (saved_view.x != 0) || (saved_view.y != 0) ) {
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XF86VidModeSetViewPort)(SDL_Display, SDL_Screen, saved_view.x, saved_view.y);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int cmpmodes(const void *va, const void *vb)
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const SDL_NAME(XF86VidModeModeInfo) *a = *(const SDL_NAME(XF86VidModeModeInfo)**)va;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const SDL_NAME(XF86VidModeModeInfo) *b = *(const SDL_NAME(XF86VidModeModeInfo)**)vb;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( a->hdisplay == b->hdisplay )
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->vdisplay - a->vdisplay;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return b->hdisplay - a->hdisplay;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void get_real_resolution(_THIS, int* w, int* h);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void set_best_resolution(_THIS, int width, int height)
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_vidmode ) {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XF86VidModeModeLine) mode;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XF86VidModeModeInfo) **modes;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int i;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int nmodes;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int best = -1;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &i, &mode) &&
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display,SDL_Screen,&nmodes,&modes) ) {
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for ( i = 0; i < nmodes ; i++ ) {
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( (modes[i]->hdisplay == width) &&
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (modes[i]->vdisplay == height) ) {
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    best = i;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    break;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( modes[i]->hdisplay >= width &&
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     modes[i]->vdisplay >= height ) {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    if ( best < 0 ||
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         (modes[i]->hdisplay < modes[best]->hdisplay &&
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                          modes[i]->vdisplay <= modes[best]->vdisplay) ||
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         (modes[i]->vdisplay < modes[best]->vdisplay &&
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                          modes[i]->hdisplay <= modes[best]->hdisplay) ) {
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        best = i;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    }
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( best >= 0 &&
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 ((modes[best]->hdisplay != mode.hdisplay) ||
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                  (modes[best]->vdisplay != mode.vdisplay)) ) {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                printf("Best Mode %d: %d x %d @ %d\n", best,
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        modes[best]->hdisplay, modes[best]->vdisplay,
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        (modes[best]->htotal && modes[best]->vtotal) ? (1000 * modes[best]->dotclock / (modes[best]->htotal * modes[best]->vtotal)) : 0 );
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, modes[best]);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            XFree(modes);
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                /* XiG */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XME
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xme && SDL_modelist ) {
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int i;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "XME: set_best_resolution(): w = %d, h = %d\n",
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                width, height);
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i=0; SDL_modelist[i]; ++i ) {
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (SDL_modelist[i]->w >= width) &&
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 (SDL_modelist[i]->h >= height) ) {
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_modelist[i] ) { /* found one, lets try it */
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int w, h;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* check current mode so we can avoid uneccessary mode changes */
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            get_real_resolution(this, &w, &h);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                fprintf(stderr, "XME: set_best_resolution: "
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        "XiGMiscChangeResolution: %d %d\n",
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_modelist[i]->w, SDL_modelist[i]->h);
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                XiGMiscChangeResolution(SDL_Display,
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        SDL_Screen,
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        0, /* view */
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        SDL_modelist[i]->w,
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        SDL_modelist[i]->h,
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        0);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                XSync(SDL_Display, False);
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XME */
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xrandr && SDL_modelist ) {
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                width, height);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int i, nsizes;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XRRScreenSize *sizes;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* find the smallest resolution that is at least as big as the user requested */
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sizes = XRRConfigSizes(screen_config, &nsizes);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i = (nsizes-1); i >= 0; i-- ) {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (SDL_modelist[i]->w >= width) &&
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 (SDL_modelist[i]->h >= height) ) {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( i >= 0 && SDL_modelist[i] ) { /* found one, lets try it */
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int w, h;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* check current mode so we can avoid uneccessary mode changes */
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            get_real_resolution(this, &w, &h);
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                int size_id;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                fprintf(stderr, "XRANDR: set_best_resolution: "
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        "XXRSetScreenConfig: %d %d\n",
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_modelist[i]->w, SDL_modelist[i]->h);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                /* find the matching size entry index */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                for ( size_id = 0; size_id < nsizes; ++size_id ) {
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    if ( (sizes[size_id].width == SDL_modelist[i]->w) &&
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         (sizes[size_id].height == SDL_modelist[i]->h) )
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        break;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                XRRSetScreenConfig(SDL_Display, screen_config, SDL_Root,
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                   size_id, saved_rotation, CurrentTime);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void get_real_resolution(_THIS, int* w, int* h)
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XME
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xme ) {
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int ractive;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XiGMiscResolutionInfo *modelist;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                0, /* view */
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                &ractive, &modelist);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *w = modelist[ractive].width;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *h = modelist[ractive].height;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "XME: get_real_resolution: w = %d h = %d\n", *w, *h);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XFree(modelist);
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XME */
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_vidmode ) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XF86VidModeModeLine) mode;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int unused;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &unused, &mode) ) {
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *w = mode.hdisplay;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            *h = mode.vdisplay;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xrandr ) {
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int nsizes;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XRRScreenSize* sizes;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sizes = XRRConfigSizes(screen_config, &nsizes);
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( nsizes > 0 ) {
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int cur_size;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            Rotation cur_rotation;
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            cur_size = XRRConfigCurrentConfiguration(screen_config, &cur_rotation);
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( cur_size >= 0 && cur_size < nsizes ) {
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                *w = sizes[cur_size].width;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                *h = sizes[cur_size].height;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "XRANDR: get_real_resolution: w = %d h = %d\n", *w, *h);
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XINERAMA
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xinerama ) {
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *w = xinerama_info.width;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *h = xinerama_info.height;
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *w = DisplayWidth(SDL_Display, SDL_Screen);
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *h = DisplayHeight(SDL_Display, SDL_Screen);
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Called after mapping a window - waits until the window is mapped */
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_WaitMapped(_THIS, Window win)
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XEvent event;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    do {
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XMaskEvent(SDL_Display, StructureNotifyMask, &event);
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } while ( (event.type != MapNotify) || (event.xmap.event != win) );
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Called after unmapping a window - waits until the window is unmapped */
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_WaitUnmapped(_THIS, Window win)
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XEvent event;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    do {
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XMaskEvent(SDL_Display, StructureNotifyMask, &event);
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } while ( (event.type != UnmapNotify) || (event.xunmap.event != win) );
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void move_cursor_to(_THIS, int x, int y)
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XWarpPointer(SDL_Display, None, SDL_Root, 0, 0, 0, 0, x, y);
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int add_default_visual(_THIS)
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int n = this->hidden->nvisuals;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i=0; i<n; i++) {
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (this->hidden->visuals[i].visual == DefaultVisual(SDL_Display, SDL_Screen)) return n;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->hidden->visuals[n].depth = DefaultDepth(SDL_Display, SDL_Screen);;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->hidden->visuals[n].visual = DefaultVisual(SDL_Display, SDL_Screen);;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->hidden->nvisuals++;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(this->hidden->nvisuals);
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int add_visual(_THIS, int depth, int class)
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XVisualInfo vi;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if(XMatchVisualInfo(SDL_Display, SDL_Screen, depth, class, &vi)) {
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int n = this->hidden->nvisuals;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->hidden->visuals[n].depth = vi.depth;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->hidden->visuals[n].visual = vi.visual;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->hidden->nvisuals++;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(this->hidden->nvisuals);
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int add_visual_byid(_THIS, const char *visual_id)
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XVisualInfo *vi, template;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int nvis;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( visual_id ) {
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_memset(&template, 0, (sizeof template));
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        template.visualid = SDL_strtol(visual_id, NULL, 0);
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        vi = XGetVisualInfo(SDL_Display, VisualIDMask, &template, &nvis);
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( vi ) {
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int n = this->hidden->nvisuals;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            this->hidden->visuals[n].depth = vi->depth;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            this->hidden->visuals[n].visual = vi->visual;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            this->hidden->nvisuals++;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            XFree(vi);
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(this->hidden->nvisuals);
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Global for the error handler */
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint vm_event, vm_error = -1;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XINERAMA
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckXinerama(_THIS, int *major, int *minor)
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *env;
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Default the extension not available */
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *major = *minor = 0;
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allow environment override */
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    env = getenv("SDL_VIDEO_X11_XINERAMA");
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( env && !SDL_atoi(env) ) {
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Query the extension version */
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !SDL_NAME(XineramaQueryExtension)(SDL_Display, major, minor) ||
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         !SDL_NAME(XineramaIsActive)(SDL_Display) ) {
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 1;
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckXRandR(_THIS, int *major, int *minor)
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *env;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Default the extension not available */
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *major = *minor = 0;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allow environment override */
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    env = getenv("SDL_VIDEO_X11_XRANDR");
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( env && !SDL_atoi(env) ) {
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* This defaults off now, due to KDE window maximize problems */
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !env ) {
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !SDL_X11_HAVE_XRANDR ) {
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Query the extension version */
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !XRRQueryVersion(SDL_Display, major, minor) ) {
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 1;
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckVidMode(_THIS, int *major, int *minor)
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *env;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Default the extension not available */
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *major = *minor = 0;
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allow environment override */
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    env = getenv("SDL_VIDEO_X11_VIDMODE");
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( env && !SDL_atoi(env) ) {
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Metro-X 4.3.0 and earlier has a broken implementation of
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       XF86VidModeGetAllModeLines() - it hangs the client.
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_strcmp(ServerVendor(SDL_Display), "Metro Link Incorporated") == 0 ) {
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FILE *metro_fp;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        metro_fp = fopen("/usr/X11R6/lib/X11/Metro/.version", "r");
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( metro_fp != NULL ) {
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int major, minor, patch, version, scannum;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            major = 0; minor = 0; patch = 0;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            scannum = fscanf(metro_fp, "%d.%d.%d", &major, &minor, &patch);
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fclose(metro_fp);
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (scannum < 0) || (scannum > 3) ) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return 0;  /* we need _something_ useful from fscanf(). */
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            version = major*100+minor*10+patch;
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( version < 431 ) {
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return 0;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Query the extension version */
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    vm_error = -1;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !SDL_NAME(XF86VidModeQueryExtension)(SDL_Display, &vm_event, &vm_error) ||
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         !SDL_NAME(XF86VidModeQueryVersion)(SDL_Display, major, minor) ) {
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 1;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XME
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckXME(_THIS, int *major, int *minor)
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    const char *env;
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Default the extension not available */
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *major = *minor = 0;
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allow environment override */
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    env = getenv("SDL_VIDEO_X11_VIDMODE");
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( env && !SDL_atoi(env) ) {
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Query the extension version */
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !XiGMiscQueryVersion(SDL_Display, major, minor) ) {
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 1;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XME */
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_GetVideoModes(_THIS)
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XINERAMA
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int xinerama_major, xinerama_minor;
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int xrandr_major, xrandr_minor;
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int nsizes;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XRRScreenSize *sizes;
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int vm_major, vm_minor;
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int nmodes;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_NAME(XF86VidModeModeInfo) **modes;
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XME
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int xme_major, xme_minor;
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int ractive, nummodes;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XiGMiscResolutionInfo *modelist;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i, n;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int screen_w;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int screen_h;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    use_xinerama = 0;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    use_xrandr = 0;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    use_vidmode = 0;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    use_xme = 0;
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    screen_w = DisplayWidth(SDL_Display, SDL_Screen);
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    screen_h = DisplayHeight(SDL_Display, SDL_Screen);
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XINERAMA
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Query Xinerama extention */
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( CheckXinerama(this, &xinerama_major, &xinerama_minor) ) {
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Find out which screen is the desired one */
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int desired = -1;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int screens;
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int w, h;
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XineramaScreenInfo) *xinerama;
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( !variable ) {
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( variable ) {
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                desired = SDL_atoi(variable);
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("X11 detected Xinerama:\n");
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        xinerama = SDL_NAME(XineramaQueryScreens)(SDL_Display, &screens);
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i = 0; i < screens; i++ ) {
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("xinerama %d: %dx%d+%d+%d\n",
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                xinerama[i].screen_number,
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                xinerama[i].width, xinerama[i].height,
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                xinerama[i].x_org, xinerama[i].y_org);
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( xinerama[i].screen_number == desired ) {
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                use_xinerama = 1;
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                xinerama_info = xinerama[i];
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XFree(xinerama);
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( use_xinerama ) {
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist = (SDL_Rect **)SDL_malloc(3*sizeof(SDL_Rect *));
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( !SDL_modelist ) {
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_OutOfMemory();
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return -1;
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* Add the full xinerama mode */
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            n = 0;
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            w = xinerama_info.width;
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            h = xinerama_info.height;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( screen_w > w || screen_h > h) {
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( SDL_modelist[n] ) {
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_modelist[n]->x = 0;
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_modelist[n]->y = 0;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_modelist[n]->w = screen_w;
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_modelist[n]->h = screen_h;
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    ++n;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* Add the head xinerama mode */
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( SDL_modelist[n] ) {
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[n]->x = 0;
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[n]->y = 0;
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[n]->w = w;
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[n]->h = h;
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                ++n;
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n] = NULL;
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* XRandR */
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* require at least XRandR v1.0 (arbitrary) */
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( CheckXRandR(this, &xrandr_major, &xrandr_minor) && (xrandr_major >= 1) )
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                xrandr_major, xrandr_minor);
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* save the screen configuration since we must reference it
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           each time we toggle modes.
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        */
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        screen_config = XRRGetScreenInfo(SDL_Display, SDL_Root);
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* retrieve the list of resolution */
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        sizes = XRRConfigSizes(screen_config, &nsizes);
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (nsizes > 0) {
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( SDL_modelist ) {
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                for ( i = 0; SDL_modelist[i]; ++i ) {
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_free(SDL_modelist[i]);
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_free(SDL_modelist);
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist = (SDL_Rect **)malloc((nsizes+1)*sizeof(SDL_Rect *));
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( !SDL_modelist ) {
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_OutOfMemory();
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return -1;
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for ( i=0; i < nsizes; i++ ) {
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ((SDL_modelist[i] =
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (SDL_Rect *)malloc(sizeof(SDL_Rect))) == NULL)
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    break;
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                fprintf(stderr, "XRANDR: mode = %4d, w = %4d, h = %4d\n",
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        i, sizes[i].width, sizes[i].height);
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[i]->x = 0;
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[i]->y = 0;
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[i]->w = sizes[i].width;
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_modelist[i]->h = sizes[i].height;
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* sort the mode list descending as SDL expects */
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_qsort(SDL_modelist, nsizes, sizeof *SDL_modelist, cmpmodelist);
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[i] = NULL; /* terminator */
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            use_xrandr = xrandr_major * 100 + xrandr_minor;
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            saved_size_id = XRRConfigCurrentConfiguration(screen_config, &saved_rotation);
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* XVidMode */
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( !use_xrandr &&
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XINERAMA
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         (!use_xinerama || xinerama_info.screen_number == -1) &&
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         CheckVidMode(this, &vm_major, &vm_minor) &&
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display, SDL_Screen,&nmodes,&modes) )
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("VidMode modes: (unsorted)\n");
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i = 0; i < nmodes; ++i ) {
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("Mode %d: %d x %d @ %d\n", i,
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    modes[i]->hdisplay, modes[i]->vdisplay,
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    (modes[i]->htotal && modes[i]->vtotal) ? (1000 * modes[i]->dotclock / (modes[i]->htotal * modes[i]->vtotal)) : 0 );
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_modelist ) {
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for ( i = 0; SDL_modelist[i]; ++i ) {
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_free(SDL_modelist[i]);
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(SDL_modelist);
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist = (SDL_Rect **)SDL_malloc((nmodes+2)*sizeof(SDL_Rect *));
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( !SDL_modelist ) {
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_OutOfMemory();
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return -1;
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_qsort(modes, nmodes, sizeof *modes, cmpmodes);
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        n = 0;
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i=0; i<nmodes; ++i ) {
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int w, h;
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* Eliminate duplicate modes with different refresh rates */
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( i > 0 &&
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 modes[i]->hdisplay == modes[i-1]->hdisplay &&
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 modes[i]->vdisplay == modes[i-1]->vdisplay ) {
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    continue;
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* Check to see if we should add the screen size (Xinerama) */
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            w = modes[i]->hdisplay;
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            h = modes[i]->vdisplay;
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( (screen_w * screen_h) >= (w * h) ) {
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( (screen_w != w) || (screen_h != h) ) {
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    if ( SDL_modelist[n] ) {
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_modelist[n]->x = 0;
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_modelist[n]->y = 0;
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_modelist[n]->w = screen_w;
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_modelist[n]->h = screen_h;
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        ++n;
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    }
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                screen_w = 0;
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                screen_h = 0;
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* Add the size from the video mode list */
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ( SDL_modelist[n] == NULL ) {
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                break;
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->x = 0;
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->y = 0;
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->w = w;
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->h = h;
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++n;
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist[n] = NULL;
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XFree(modes);
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        use_vidmode = vm_major * 100 + vm_minor;
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        save_mode(this);
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XME
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* XiG */
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modelist = NULL;
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* first lets make sure we have the extension, and it's at least v2.0 */
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( CheckXME(this, &xme_major, &xme_minor) && xme_major >= 2 &&
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         (nummodes = XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                             0, /* view */
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                             &ractive, &modelist)) > 1 )
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {                                /* then we actually have some */
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int j;
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* We get the list already sorted in descending order.
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           We'll copy it in reverse order so SDL is happy */
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "XME: nummodes = %d, active mode = %d\n",
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                nummodes, ractive);
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_modelist ) {
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for ( i = 0; SDL_modelist[i]; ++i ) {
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_free(SDL_modelist[i]);
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(SDL_modelist);
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist = (SDL_Rect **)SDL_malloc((nummodes+1)*sizeof(SDL_Rect *));
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( !SDL_modelist ) {
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_OutOfMemory();
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return -1;
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i=0, j=nummodes-1; j>=0; i++, j-- ) {
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ((SDL_modelist[i] =
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect))) == NULL)
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              break;
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "XME: mode = %4d, w = %4d, h = %4d\n",
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   i, modelist[i].width, modelist[i].height);
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[i]->x = 0;
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[i]->y = 0;
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[i]->w = modelist[j].width;
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[i]->h = modelist[j].height;
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist[i] = NULL; /* terminator */
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        use_xme = xme_major * 100 + xme_minor;
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        saved_res = modelist[ractive]; /* save the current resolution */
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( modelist ) {
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XFree(modelist);
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XME */
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* It's interesting to note that if we allow 32 bit depths,
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   we get a visual with an alpha mask on composite servers.
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        static int depth_list[] = { 32, 24, 16, 15, 8 };
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        static int depth_list[] = { 24, 16, 15, 8 };
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int j, np;
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int use_directcolor = 1;
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XPixmapFormatValues *pf;
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Search for the visuals in deepest-first order, so that the first
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           will be the richest one */
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ) {
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                use_directcolor = 0;
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->hidden->nvisuals = 0;
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( ! add_visual_byid(this, SDL_getenv("SDL_VIDEO_X11_VISUALID")) ) {
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                for ( i=0; i<SDL_arraysize(depth_list); ++i ) {
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ( depth_list[i] > 8 ) {
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                if ( use_directcolor ) {
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        add_visual(this, depth_list[i], DirectColor);
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                }
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                add_visual(this, depth_list[i], TrueColor);
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        } else {
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                add_visual(this, depth_list[i], PseudoColor);
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                add_visual(this, depth_list[i], StaticColor);
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                add_default_visual(this);
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( this->hidden->nvisuals == 0 ) {
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_SetError("Found no sufficiently capable X11 visuals");
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return -1;
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* look up the pixel quantum for each depth */
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pf = XListPixmapFormats(SDL_Display, &np);
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for(i = 0; i < this->hidden->nvisuals; i++) {
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int d = this->hidden->visuals[i].depth;
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for(j = 0; j < np; j++)
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if(pf[j].depth == d)
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    break;
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            this->hidden->visuals[i].bpp = j < np ? pf[j].bits_per_pixel : d;
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XFree(pf);
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_modelist == NULL ) {
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist = (SDL_Rect **)SDL_malloc((1+1)*sizeof(SDL_Rect *));
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( !SDL_modelist ) {
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_OutOfMemory();
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return -1;
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        n = 0;
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( SDL_modelist[n] ) {
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->x = 0;
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->y = 0;
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->w = screen_w;
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_modelist[n]->h = screen_h;
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            ++n;
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist[n] = NULL;
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef X11MODES_DEBUG
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xinerama ) {
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("Xinerama is enabled\n");
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xrandr ) {
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("XRandR is enabled\n");
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_vidmode ) {
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("VidMode is enabled\n");
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xme ) {
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("Xi Graphics XME fullscreen is enabled\n");
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_modelist ) {
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("X11 video mode list:\n");
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i=0; SDL_modelist[i]; ++i ) {
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("\t%dx%d\n", SDL_modelist[i]->w, SDL_modelist[i]->h);
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* X11MODES_DEBUG */
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_SupportedVisual(_THIS, SDL_PixelFormat *format)
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for(i = 0; i < this->hidden->nvisuals; i++)
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if(this->hidden->visuals[i].bpp == format->BitsPerPixel)
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return 1;
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **X11_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( X11_SupportedVisual(this, format) ) {
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( flags & SDL_FULLSCREEN ) {
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return(SDL_modelist);
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return((SDL_Rect **)-1);
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return((SDL_Rect **)0);
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_FreeVideoModes(_THIS)
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_modelist ) {
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for ( i=0; SDL_modelist[i]; ++i ) {
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(SDL_modelist[i]);
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_free(SDL_modelist);
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_modelist = NULL;
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Free the Xrandr screen configuration */
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( screen_config ) {
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XRRFreeScreenConfigInfo(screen_config);
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        screen_config = NULL;
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_ResizeFullScreen(_THIS)
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int x = 0, y = 0;
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int real_w, real_h;
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int screen_w;
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int screen_h;
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    screen_w = DisplayWidth(SDL_Display, SDL_Screen);
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    screen_h = DisplayHeight(SDL_Display, SDL_Screen);
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XINERAMA
9429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xinerama &&
9439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         window_w <= xinerama_info.width &&
9449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         window_h <= xinerama_info.height ) {
9459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        x = xinerama_info.x_org;
9469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        y = xinerama_info.y_org;
9479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
9499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( currently_fullscreen ) {
9519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Switch resolution and cover it with the FSwindow */
9529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        move_cursor_to(this, x, y);
9539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        set_best_resolution(this, window_w, window_h);
9549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        move_cursor_to(this, x, y);
9559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        get_real_resolution(this, &real_w, &real_h);
9569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( window_w > real_w ) {
9579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            real_w = MAX(real_w, screen_w);
9589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( window_h > real_h ) {
9609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            real_h = MAX(real_h, screen_h);
9619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
9629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XMoveResizeWindow(SDL_Display, FSwindow, x, y, real_w, real_h);
9639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        move_cursor_to(this, real_w/2, real_h/2);
9649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Center and reparent the drawing window */
9669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        x = (real_w - window_w)/2;
9679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        y = (real_h - window_h)/2;
9689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XReparentWindow(SDL_Display, SDL_Window, FSwindow, x, y);
9699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* FIXME: move the mouse to the old relative location */
9709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XSync(SDL_Display, True);   /* Flush spurious mode change events */
9719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(1);
9739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_QueueEnterFullScreen(_THIS)
9769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch_waiting = 0x01 | SDL_FULLSCREEN;
9789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch_time = SDL_GetTicks() + 1500;
9799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 /* This causes a BadMatch error if the window is iconified (not needed) */
9809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XSetInputFocus(SDL_Display, WMwindow, RevertToNone, CurrentTime);
9819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_EnterFullScreen(_THIS)
9859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int okay;
9879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
9889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Window tmpwin, *windows;
9899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i, nwindows;
9909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int x = 0, y = 0;
9929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int real_w, real_h;
9939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int screen_w;
9949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int screen_h;
9959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    okay = 1;
9979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( currently_fullscreen ) {
9989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(okay);
9999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Ungrab the input so that we can move the mouse around */
10029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    X11_GrabInputNoLock(this, SDL_GRAB_OFF);
10039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XINERAMA
10059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_xinerama &&
10069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         window_w <= xinerama_info.width &&
10079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         window_h <= xinerama_info.height ) {
10089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        x = xinerama_info.x_org;
10099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        y = xinerama_info.y_org;
10109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
10129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Map the fullscreen window to blank the screen */
10149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    screen_w = DisplayWidth(SDL_Display, SDL_Screen);
10159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    screen_h = DisplayHeight(SDL_Display, SDL_Screen);
10169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    get_real_resolution(this, &real_w, &real_h);
10179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    real_w = MAX(window_w, MAX(real_w, screen_w));
10189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    real_h = MAX(window_h, MAX(real_h, screen_h));
10199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XMoveResizeWindow(SDL_Display, FSwindow,
10209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      x, y, real_w, real_h);
10219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XMapRaised(SDL_Display, FSwindow);
10229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    X11_WaitMapped(this, FSwindow);
10239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 /* This seems to break WindowMaker in focus-follows-mouse mode */
10259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Make sure we got to the top of the window stack */
10269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( XQueryTree(SDL_Display, SDL_Root, &tmpwin, &tmpwin,
10279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            &windows, &nwindows) && windows ) {
10289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* If not, try to put us there - if fail... oh well */
10299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( windows[nwindows-1] != FSwindow ) {
10309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            tmpwin = windows[nwindows-1];
10319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            for ( i=0; i<nwindows; ++i ) {
10329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( windows[i] == FSwindow ) {
10339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_memcpy(&windows[i], &windows[i+1],
10349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           (nwindows-i-1)*sizeof(windows[i]));
10359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    break;
10369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
10379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
10389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            windows[nwindows-1] = FSwindow;
10399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            XRestackWindows(SDL_Display, windows, nwindows);
10409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            XSync(SDL_Display, False);
10419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XFree(windows);
10439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
10459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    XRaiseWindow(SDL_Display, FSwindow);
10469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
10499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Save the current video mode */
10509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( use_vidmode ) {
10519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_NAME(XF86VidModeLockModeSwitch)(SDL_Display, SDL_Screen, True);
10529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        save_mode(this);
10539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    currently_fullscreen = 1;
10569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the new resolution */
10589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    okay = X11_ResizeFullScreen(this);
10599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( ! okay ) {
10609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        X11_LeaveFullScreen(this);
10619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the colormap */
10639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_XColorMap ) {
10649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XInstallColormap(SDL_Display, SDL_XColorMap);
10659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( okay ) {
10679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        X11_GrabInputNoLock(this, this->input_grab | SDL_GRAB_FULLSCREEN);
10689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* We may need to refresh the screen at this point (no backing store)
10719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       We also don't get an event, which is why we explicitly refresh. */
10729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->screen ) {
10739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( this->screen->flags & SDL_OPENGL ) {
10749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateExpose();
10759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
10769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            X11_RefreshDisplay(this);
10779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(okay);
10819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_LeaveFullScreen(_THIS)
10849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( currently_fullscreen ) {
10869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XReparentWindow(SDL_Display, SDL_Window, WMwindow, 0, 0);
10879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_VIDMODE
10889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( use_vidmode ) {
10899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            restore_mode(this);
10909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_NAME(XF86VidModeLockModeSwitch)(SDL_Display, SDL_Screen, False);
10919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
10939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XME
10959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( use_xme ) {
10969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            int rw, rh;
10979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* check current mode so we can avoid uneccessary mode changes */
10999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            get_real_resolution(this, &rw, &rh);
11009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (rw != saved_res.width || rh != saved_res.height) {
11029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                XiGMiscChangeResolution(SDL_Display,
11039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        SDL_Screen,
11049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        0, /* view */
11059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        saved_res.width,
11069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        saved_res.height,
11079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        0);
11089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                XSync(SDL_Display, False);
11099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
11109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
11119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_VIDEO_DRIVER_X11_XRANDR
11149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( use_xrandr ) {
11159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            XRRSetScreenConfig(SDL_Display, screen_config, SDL_Root,
11169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               saved_size_id, saved_rotation, CurrentTime);
11179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
11189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
11199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XUnmapWindow(SDL_Display, FSwindow);
11219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        X11_WaitUnmapped(this, FSwindow);
11229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        XSync(SDL_Display, True);   /* Flush spurious mode change events */
11239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        currently_fullscreen = 0;
11249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* If we get popped out of fullscreen mode for some reason, input_grab
11269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       will still have the SDL_GRAB_FULLSCREEN flag set, since this is only
11279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       temporary.  In this case, release the grab unless the input has been
11289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       explicitly grabbed.
11299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
11309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    X11_GrabInputNoLock(this, this->input_grab & ~SDL_GRAB_FULLSCREEN);
11319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* We may need to refresh the screen at this point (no backing store)
11339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       We also don't get an event, which is why we explicitly refresh. */
11349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this->screen ) {
11359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( this->screen->flags & SDL_OPENGL ) {
11369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_PrivateExpose();
11379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
11389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            X11_RefreshDisplay(this);
11399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
11409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(0);
11439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1144