diff src/video/x11/SDL_x11modes.c @ 1659:14717b52abc0 SDL-1.3

Merge trunk-1.3-3
author Sam Lantinga <slouken@libsdl.org>
date Wed, 17 May 2006 08:18:28 +0000
parents e49147870aac
children 782fd950bd46
line wrap: on
line diff
--- a/src/video/x11/SDL_x11modes.c	Mon May 01 06:58:33 2006 +0000
+++ b/src/video/x11/SDL_x11modes.c	Wed May 17 08:18:28 2006 +0000
@@ -33,12 +33,22 @@
 #include "SDL_x11modes_c.h"
 #include "SDL_x11image_c.h"
 
-#if SDL_VIDEO_DRIVER_X11_XINERAMA
-#include "../Xext/extensions/Xinerama.h"
-#endif 
+/*#define X11MODES_DEBUG*/
 
 #define MAX(a, b)        (a > b ? a : b)
 
+#if SDL_VIDEO_DRIVER_X11_XRANDR
+static int cmpmodelist(const void *va, const void *vb)
+{
+    const SDL_Rect *a = *(const SDL_Rect **)va;
+    const SDL_Rect *b = *(const SDL_Rect **)vb;
+    if ( a->w == b->w )
+        return b->h - a->h;
+    else
+        return b->w - a->w;
+}
+#endif
+
 #if SDL_VIDEO_DRIVER_X11_VIDMODE
 Bool SDL_NAME(XF86VidModeGetModeInfo)(Display *dpy, int scr, SDL_NAME(XF86VidModeModeInfo) *info)
 {
@@ -86,18 +96,6 @@
 }
 #endif
 
-#if SDL_VIDEO_DRIVER_X11_XRANDR
-static int cmpmodelist(const void *va, const void *vb)
-{
-    const SDL_Rect *a = *(const SDL_Rect **)va;
-    const SDL_Rect *b = *(const SDL_Rect **)vb;
-    if ( a->w == b->w )
-        return b->h - a->h;
-    else
-        return b->w - a->w;
-}
-#endif
-
 static void get_real_resolution(_THIS, int* w, int* h);
 
 static void set_best_resolution(_THIS, int width, int height)
@@ -107,45 +105,37 @@
         SDL_NAME(XF86VidModeModeLine) mode;
         SDL_NAME(XF86VidModeModeInfo) **modes;
         int i;
-        int best_width = 0, best_height = 0;
         int nmodes;
+        int best = -1;
 
         if ( SDL_NAME(XF86VidModeGetModeLine)(SDL_Display, SDL_Screen, &i, &mode) &&
-             SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display,SDL_Screen,&nmodes,&modes)){
-#ifdef XFREE86_DEBUG
-            printf("Available modes (unsorted):\n");
-            for ( i = 0; i < nmodes; ++i ) {
-                printf("Mode %d: %d x %d @ %d\n", i,
-                        modes[i]->hdisplay, modes[i]->vdisplay,
-                        1000 * modes[i]->dotclock / (modes[i]->htotal *
-                        modes[i]->vtotal) );
-            }
-#endif
+             SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display,SDL_Screen,&nmodes,&modes) ) {
             for ( i = 0; i < nmodes ; i++ ) {
                 if ( (modes[i]->hdisplay == width) &&
-                     (modes[i]->vdisplay == height) )
-                    goto match;
-            }
-            qsort(modes, nmodes, sizeof *modes, cmpmodes);
-            for ( i = nmodes-1; i > 0 ; i-- ) {
-		if ( ! best_width ) {
-                    if ( (modes[i]->hdisplay >= width) &&
-                         (modes[i]->vdisplay >= height) ) {
-                        best_width = modes[i]->hdisplay;
-                        best_height = modes[i]->vdisplay;
-                    }
-                } else {
-                    if ( (modes[i]->hdisplay != best_width) ||
-                         (modes[i]->vdisplay != best_height) ) {
-                        i++;
-                        break;
+                     (modes[i]->vdisplay == height) ) {
+                    best = i;
+                    break;
+                }
+                if ( modes[i]->hdisplay >= width &&
+                     modes[i]->vdisplay >= height ) {
+                    if ( best < 0 ||
+                         (modes[i]->hdisplay < modes[best]->hdisplay &&
+                          modes[i]->vdisplay <= modes[best]->vdisplay) ||
+                         (modes[i]->vdisplay < modes[best]->vdisplay &&
+                          modes[i]->hdisplay <= modes[best]->hdisplay) ) {
+                        best = i;
                     }
                 }
             }
-       match:
-            if ( (modes[i]->hdisplay != mode.hdisplay) ||
-                 (modes[i]->vdisplay != mode.vdisplay) ) {
-                SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, modes[i]);
+            if ( best >= 0 &&
+                 ((modes[best]->hdisplay != mode.hdisplay) ||
+                  (modes[best]->vdisplay != mode.vdisplay)) ) {
+#ifdef X11MODES_DEBUG
+                printf("Best Mode %d: %d x %d @ %d\n", best,
+                        modes[best]->hdisplay, modes[best]->vdisplay,
+                        (modes[best]->htotal && modes[best]->vtotal) ? (1000 * modes[best]->dotclock / (modes[best]->htotal * modes[best]->vtotal)) : 0 );
+#endif
+                SDL_NAME(XF86VidModeSwitchToMode)(SDL_Display, SDL_Screen, modes[best]);
             }
             XFree(modes);
         }
@@ -154,13 +144,13 @@
 
                                 /* XiG */
 #if SDL_VIDEO_DRIVER_X11_XME
-#ifdef XIG_DEBUG
-    fprintf(stderr, "XME: set_best_resolution(): w = %d, h = %d\n",
-            width, height);
-#endif
-    if ( SDL_modelist ) {
+    if ( use_xme && SDL_modelist ) {
         int i;
 
+#ifdef X11MODES_DEBUG
+        fprintf(stderr, "XME: set_best_resolution(): w = %d, h = %d\n",
+                width, height);
+#endif
         for ( i=0; SDL_modelist[i]; ++i ) {
             if ( (SDL_modelist[i]->w >= width) &&
                  (SDL_modelist[i]->h >= height) ) {
@@ -175,11 +165,11 @@
             get_real_resolution(this, &w, &h);
 
             if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
-# ifdef XIG_DEBUG
+#ifdef X11MODES_DEBUG
                 fprintf(stderr, "XME: set_best_resolution: "
                         "XiGMiscChangeResolution: %d %d\n",
-                        SDL_modelist[s]->w, SDL_modelist[s]->h);
-# endif
+                        SDL_modelist[i]->w, SDL_modelist[i]->h);
+#endif
                 XiGMiscChangeResolution(SDL_Display, 
                                         SDL_Screen,
                                         0, /* view */
@@ -193,58 +183,73 @@
 #endif /* SDL_VIDEO_DRIVER_X11_XME */
 
 #if SDL_VIDEO_DRIVER_X11_XRANDR
-    if ( use_xrandr ) {
-#ifdef XRANDR_DEBUG
+    if ( use_xrandr && SDL_modelist ) {
+#ifdef X11MODES_DEBUG
         fprintf(stderr, "XRANDR: set_best_resolution(): w = %d, h = %d\n",
                 width, height);
 #endif
-        if ( SDL_modelist ) {
-            int i, nsizes;
-            XRRScreenSize *sizes;
+        int i, nsizes;
+        XRRScreenSize *sizes;
 
-            /* find the smallest resolution that is at least as big as the user requested */
-            sizes = XRRConfigSizes(screen_config, &nsizes);
-            for ( i = (nsizes-1); i >= 0; i-- ) {
-                if ( (SDL_modelist[i]->w >= width) &&
-                     (SDL_modelist[i]->h >= height) ) {
-                    break;
-                }
+        /* find the smallest resolution that is at least as big as the user requested */
+        sizes = XRRConfigSizes(screen_config, &nsizes);
+        for ( i = (nsizes-1); i >= 0; i-- ) {
+            if ( (SDL_modelist[i]->w >= width) &&
+                 (SDL_modelist[i]->h >= height) ) {
+                break;
             }
+        }
 
-            if ( i >= 0 && SDL_modelist[i] ) { /* found one, lets try it */
-                int w, h;
+        if ( i >= 0 && SDL_modelist[i] ) { /* found one, lets try it */
+            int w, h;
 
-                /* check current mode so we can avoid uneccessary mode changes */
-                get_real_resolution(this, &w, &h);
+            /* check current mode so we can avoid uneccessary mode changes */
+            get_real_resolution(this, &w, &h);
 
-                if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
-                    int size_id;
+            if ( (SDL_modelist[i]->w != w) || (SDL_modelist[i]->h != h) ) {
+                int size_id;
 
-#ifdef XRANDR_DEBUG
-                    fprintf(stderr, "XRANDR: set_best_resolution: "
-                            "XXRSetScreenConfig: %d %d\n",
-                            SDL_modelist[i]->w, SDL_modelist[i]->h);
+#ifdef X11MODES_DEBUG
+                fprintf(stderr, "XRANDR: set_best_resolution: "
+                        "XXRSetScreenConfig: %d %d\n",
+                        SDL_modelist[i]->w, SDL_modelist[i]->h);
 #endif
 
-                    /* find the matching size entry index */
-                    for ( size_id = 0; size_id < nsizes; ++size_id ) {
-                        if ( (sizes[size_id].width == SDL_modelist[i]->w) &&
-                             (sizes[size_id].height == SDL_modelist[i]->h) )
-                            break;
-                    }
+                /* find the matching size entry index */
+                for ( size_id = 0; size_id < nsizes; ++size_id ) {
+                    if ( (sizes[size_id].width == SDL_modelist[i]->w) &&
+                         (sizes[size_id].height == SDL_modelist[i]->h) )
+                        break;
+                }
 
-                    XRRSetScreenConfig(SDL_Display, screen_config, SDL_Root,
-                                       size_id, saved_rotation, CurrentTime);
-                }
+                XRRSetScreenConfig(SDL_Display, screen_config, SDL_Root,
+                                   size_id, saved_rotation, CurrentTime);
             }
         }
     }
 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
-
 }
 
 static void get_real_resolution(_THIS, int* w, int* h)
 {
+#if SDL_VIDEO_DRIVER_X11_XME
+    if ( use_xme ) {
+        int ractive;
+        XiGMiscResolutionInfo *modelist;
+
+        XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
+                                0, /* view */
+                                &ractive, &modelist);
+        *w = modelist[ractive].width;
+        *h = modelist[ractive].height;
+#ifdef X11MODES_DEBUG
+        fprintf(stderr, "XME: get_real_resolution: w = %d h = %d\n", *w, *h);
+#endif
+        XFree(modelist);
+        return;
+    }
+#endif /* SDL_VIDEO_DRIVER_X11_XME */
+
 #if SDL_VIDEO_DRIVER_X11_VIDMODE
     if ( use_vidmode ) {
         SDL_NAME(XF86VidModeModeLine) mode;
@@ -258,24 +263,6 @@
     }
 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
 
-#if SDL_VIDEO_DRIVER_X11_XME
-    if ( use_xme ) {
-        int ractive;
-        XiGMiscResolutionInfo *modelist;
-
-        XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
-                                0, /* view */
-                                &ractive, &modelist);
-        *w = modelist[ractive].width;
-        *h = modelist[ractive].height;
-#ifdef XIG_DEBUG
-        fprintf(stderr, "XME: get_real_resolution: w = %d h = %d\n", *w, *h);
-#endif
-        XFree(modelist);
-        return;
-    }
-#endif /* SDL_VIDEO_DRIVER_X11_XME */
-
 #if SDL_VIDEO_DRIVER_X11_XRANDR
     if ( use_xrandr ) {
         int nsizes;
@@ -291,7 +278,7 @@
                 *w = sizes[cur_size].width;
                 *h = sizes[cur_size].height;
             }
-#ifdef XRANDR_DEBUG
+#ifdef X11MODES_DEBUG
             fprintf(stderr, "XRANDR: get_real_resolution: w = %d h = %d\n", *w, *h);
 #endif
             return;
@@ -299,6 +286,14 @@
     }
 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
 
+#if SDL_VIDEO_DRIVER_X11_XINERAMA
+    if ( use_xinerama ) {
+        *w = xinerama_info.width;
+        *h = xinerama_info.height;
+        return;
+    }
+#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
+
     *w = DisplayWidth(SDL_Display, SDL_Screen);
     *h = DisplayHeight(SDL_Display, SDL_Screen);
 }
@@ -360,10 +355,136 @@
 /* Global for the error handler */
 int vm_event, vm_error = -1;
 
+#if SDL_VIDEO_DRIVER_X11_XINERAMA
+static int CheckXinerama(_THIS, int *major, int *minor)
+{
+    const char *env;
+
+    /* Default the extension not available */
+    *major = *minor = 0;
+
+    /* Allow environment override */
+    env = getenv("SDL_VIDEO_X11_XINERAMA");
+    if ( env && !SDL_atoi(env) ) {
+        return 0;
+    }
+
+    /* Query the extension version */
+    if ( !SDL_NAME(XineramaQueryExtension)(SDL_Display, major, minor) ||
+         !SDL_NAME(XineramaIsActive)(SDL_Display) ) {
+        return 0;
+    }
+    return 1;
+}
+#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
+
+#if SDL_VIDEO_DRIVER_X11_XRANDR
+static int CheckXRandR(_THIS, int *major, int *minor)
+{
+    const char *env;
+
+    /* Default the extension not available */
+    *major = *minor = 0;
+
+    /* Allow environment override */
+    env = getenv("SDL_VIDEO_X11_XRANDR");
+    if ( env && !SDL_atoi(env) ) {
+        return 0;
+    }
+
+    /* This defaults off now, due to KDE window maximize problems */
+    if ( !env ) {
+        return 0;
+    }
+
+    if ( !SDL_X11_HAVE_XRANDR ) {
+        return 0;
+    }
+
+    /* Query the extension version */
+    if ( !XRRQueryVersion(SDL_Display, major, minor) ) {
+        return 0;
+    }
+    return 1;
+}
+#endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
+
+#if SDL_VIDEO_DRIVER_X11_VIDMODE
+static int CheckVidMode(_THIS, int *major, int *minor)
+{
+    const char *env;
+
+    /* Default the extension not available */
+    *major = *minor = 0;
+
+    /* Allow environment override */
+    env = getenv("SDL_VIDEO_X11_VIDMODE");
+    if ( env && !SDL_atoi(env) ) {
+        return 0;
+    }
+    
+    /* Metro-X 4.3.0 and earlier has a broken implementation of
+       XF86VidModeGetAllModeLines() - it hangs the client.
+     */
+    if ( SDL_strcmp(ServerVendor(SDL_Display), "Metro Link Incorporated") == 0 ) {
+        FILE *metro_fp;
+
+        metro_fp = fopen("/usr/X11R6/lib/X11/Metro/.version", "r");
+        if ( metro_fp != NULL ) {
+            int major, minor, patch, version;
+            major = 0; minor = 0; patch = 0;
+            fscanf(metro_fp, "%d.%d.%d", &major, &minor, &patch);
+            fclose(metro_fp);
+            version = major*100+minor*10+patch;
+            if ( version < 431 ) {
+                return 0;
+            }
+        }
+    }
+
+    /* Query the extension version */
+    vm_error = -1;
+    if ( !SDL_NAME(XF86VidModeQueryExtension)(SDL_Display, &vm_event, &vm_error) ||
+         !SDL_NAME(XF86VidModeQueryVersion)(SDL_Display, major, minor) ) {
+        return 0;
+    }
+    return 1;
+}
+#endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
+
+#if SDL_VIDEO_DRIVER_X11_XME
+static int CheckXME(_THIS, int *major, int *minor)
+{
+    const char *env;
+
+    /* Default the extension not available */
+    *major = *minor = 0;
+
+    /* Allow environment override */
+    env = getenv("SDL_VIDEO_X11_VIDMODE");
+    if ( env && !SDL_atoi(env) ) {
+        return 0;
+    }
+    
+    /* Query the extension version */
+    if ( !XiGMiscQueryVersion(SDL_Display, major, minor) ) {
+        return 0;
+    }
+    return 1;
+}
+#endif /* SDL_VIDEO_DRIVER_X11_XME */
+
 int X11_GetVideoModes(_THIS)
 {
+#if SDL_VIDEO_DRIVER_X11_XINERAMA
+    int xinerama_major, xinerama_minor;
+#endif
+#if SDL_VIDEO_DRIVER_X11_XRANDR
+    int xrandr_major, xrandr_minor;
+    int nsizes;
+    XRRScreenSize *sizes;
+#endif
 #if SDL_VIDEO_DRIVER_X11_VIDMODE
-    int buggy_X11;
     int vm_major, vm_minor;
     int nmodes;
     SDL_NAME(XF86VidModeModeInfo) **modes;
@@ -373,30 +494,90 @@
     int ractive, nummodes;
     XiGMiscResolutionInfo *modelist;
 #endif
-#if SDL_VIDEO_DRIVER_X11_XRANDR
-    int xrandr_major, xrandr_minor;
-    int nsizes;
-    XRRScreenSize *sizes;
-#endif
     int i, n;
     int screen_w;
     int screen_h;
 
-    vm_error = -1;
+    use_xinerama = 0;
+    use_xrandr = 0;
     use_vidmode = 0;
-    use_xrandr = 0;
+    use_xme = 0;
     screen_w = DisplayWidth(SDL_Display, SDL_Screen);
     screen_h = DisplayHeight(SDL_Display, SDL_Screen);
 
+#if SDL_VIDEO_DRIVER_X11_XINERAMA
+    /* Query Xinerama extention */
+    if ( CheckXinerama(this, &xinerama_major, &xinerama_minor) ) {
+        /* Find out which screen is the desired one */
+        int desired = 0;
+        int screens;
+        int w, h;
+        SDL_NAME(XineramaScreenInfo) *xinerama;
+
+        const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
+        if ( variable ) {
+                desired = SDL_atoi(variable);
+        }
+#ifdef X11MODES_DEBUG
+        printf("X11 detected Xinerama:\n");
+#endif
+        xinerama = SDL_NAME(XineramaQueryScreens)(SDL_Display, &screens);
+        for ( i = 0; i < screens; i++ ) {
+#ifdef X11MODES_DEBUG
+            printf("xinerama %d: %dx%d+%d+%d\n",
+                xinerama[i].screen_number,
+                xinerama[i].width, xinerama[i].height,
+                xinerama[i].x_org, xinerama[i].y_org);
+#endif
+            if ( xinerama[i].screen_number == desired ) {
+                use_xinerama = 1;
+                xinerama_info = xinerama[i];
+            }
+        }
+        XFree(xinerama);
+
+        if ( use_xinerama ) {
+            SDL_modelist = (SDL_Rect **)SDL_malloc(3*sizeof(SDL_Rect *));
+            if ( !SDL_modelist ) {
+                SDL_OutOfMemory();
+                return -1;
+            }
+
+            /* Add the full xinerama mode */
+            n = 0;
+            w = xinerama_info.width;
+            h = xinerama_info.height;
+            if ( screen_w > w || screen_h > h) {
+                SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
+                if ( SDL_modelist[n] ) {
+                    SDL_modelist[n]->x = 0;
+                    SDL_modelist[n]->y = 0;
+                    SDL_modelist[n]->w = screen_w;
+                    SDL_modelist[n]->h = screen_h;
+                    ++n;
+                }
+            }
+
+            /* Add the head xinerama mode */
+            SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
+            if ( SDL_modelist[n] ) {
+                SDL_modelist[n]->x = 0;
+                SDL_modelist[n]->y = 0;
+                SDL_modelist[n]->w = w;
+                SDL_modelist[n]->h = h;
+                ++n;
+            }
+            SDL_modelist[n] = NULL;
+        }
+    }
+#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
+
+#if SDL_VIDEO_DRIVER_X11_XRANDR
     /* XRandR */
-#if SDL_VIDEO_DRIVER_X11_XRANDR
     /* require at least XRandR v1.0 (arbitrary) */
-    if ( ( SDL_X11_HAVE_XRANDR ) &&
-         ( getenv("SDL_VIDEO_X11_NO_XRANDR") == NULL ) &&
-         ( XRRQueryVersion(SDL_Display, &xrandr_major, &xrandr_minor) ) &&
-         ( xrandr_major >= 1 ) ) {
-
-#ifdef XRANDR_DEBUG
+    if ( CheckXRandR(this, &xrandr_major, &xrandr_minor) && (xrandr_major >= 1) )
+    {
+#ifdef X11MODES_DEBUG
         fprintf(stderr, "XRANDR: XRRQueryVersion: V%d.%d\n",
                 xrandr_major, xrandr_minor);
 #endif
@@ -409,27 +590,36 @@
         /* retrieve the list of resolution */
         sizes = XRRConfigSizes(screen_config, &nsizes);
         if (nsizes > 0) {
+            if ( SDL_modelist ) {
+                for ( i = 0; SDL_modelist[i]; ++i ) {
+                    SDL_free(SDL_modelist[i]);
+                }
+                SDL_free(SDL_modelist);
+            }
             SDL_modelist = (SDL_Rect **)malloc((nsizes+1)*sizeof(SDL_Rect *));
-            if (SDL_modelist) {
-                for ( i=0; i < nsizes; i++ ) {
-                    if ((SDL_modelist[i] =
-                         (SDL_Rect *)malloc(sizeof(SDL_Rect))) == NULL)
-                        break;
-#ifdef XRANDR_DEBUG
-                    fprintf(stderr, "XRANDR: mode = %4d, w = %4d, h = %4d\n",
-                            i, sizes[i].width, sizes[i].height);
+            if ( !SDL_modelist ) {
+                SDL_OutOfMemory();
+                return -1;
+            }
+            for ( i=0; i < nsizes; i++ ) {
+                if ((SDL_modelist[i] =
+                     (SDL_Rect *)malloc(sizeof(SDL_Rect))) == NULL)
+                    break;
+#ifdef X11MODES_DEBUG
+                fprintf(stderr, "XRANDR: mode = %4d, w = %4d, h = %4d\n",
+                        i, sizes[i].width, sizes[i].height);
 #endif
 
-                    SDL_modelist[i]->x = 0;
-                    SDL_modelist[i]->y = 0;
-                    SDL_modelist[i]->w = sizes[i].width;
-                    SDL_modelist[i]->h = sizes[i].height;
+                SDL_modelist[i]->x = 0;
+                SDL_modelist[i]->y = 0;
+                SDL_modelist[i]->w = sizes[i].width;
+                SDL_modelist[i]->h = sizes[i].height;
 
-                }
-                /* sort the mode list descending as SDL expects */
-                qsort(SDL_modelist, nsizes, sizeof *SDL_modelist, cmpmodelist);
-                SDL_modelist[i] = NULL; /* terminator */
             }
+            /* sort the mode list descending as SDL expects */
+            SDL_qsort(SDL_modelist, nsizes, sizeof *SDL_modelist, cmpmodelist);
+            SDL_modelist[i] = NULL; /* terminator */
+
             use_xrandr = xrandr_major * 100 + xrandr_minor;
             saved_size_id = XRRConfigCurrentConfiguration(screen_config, &saved_rotation);
         }
@@ -437,110 +627,73 @@
 #endif /* SDL_VIDEO_DRIVER_X11_XRANDR */
 
 #if SDL_VIDEO_DRIVER_X11_VIDMODE
-    /* Metro-X 4.3.0 and earlier has a broken implementation of
-       XF86VidModeGetAllModeLines() - it hangs the client.
-     */
-    buggy_X11 = 0;
-    if ( SDL_strcmp(ServerVendor(SDL_Display), "Metro Link Incorporated") == 0 ) {
-        FILE *metro_fp;
-
-        metro_fp = fopen("/usr/X11R6/lib/X11/Metro/.version", "r");
-        if ( metro_fp != NULL ) {
-            int major, minor, patch, version;
-            major = 0; minor = 0; patch = 0;
-            fscanf(metro_fp, "%d.%d.%d", &major, &minor, &patch);
-            version = major*100+minor*10+patch;
-            if ( version < 431 ) {
-                buggy_X11 = 1;
-            }
-            fclose(metro_fp);
-        }
-    }
-#if 0 /* Let's try this again... hopefully X servers have improved... */
-#if defined(__alpha__) || defined(__sparc64__) || defined(__powerpc__)
-    /* The alpha, sparc64 and PPC XFree86 servers are also buggy */
-    buggy_X11 = 1;
-#endif
-#endif
-    /* Enumerate the available fullscreen modes */
-    if ( ! buggy_X11 ) {
-        if ( SDL_NAME(XF86VidModeQueryExtension)(SDL_Display, &vm_event, &vm_error) &&
-              SDL_NAME(XF86VidModeQueryVersion)(SDL_Display, &vm_major, &vm_minor) ) {
-#ifdef BROKEN_XFREE86_4001
-#ifdef X_XF86VidModeGetDotClocks  /* Compiled under XFree86 4.0 */
-                /* Earlier X servers hang when doing vidmode */
-                if ( vm_major < 2 ) {
-#ifdef XFREE86_DEBUG
-                    printf("Compiled under XFree86 4.0, server is XFree86 3.X\n");
-#endif
-                    buggy_X11 = 1;
-                }
-#else
-                /* XFree86 3.X code works with XFree86 4.0 servers */;
-#endif /* XFree86 4.0 */
-#endif /* XFree86 4.02 and newer are fixed wrt backwards compatibility */
-        } else {
-            buggy_X11 = 1;
-        }
-    }
-    if ( ! buggy_X11 && ! use_xrandr &&
-         SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display, SDL_Screen,&nmodes,&modes) ) {
-
-#ifdef XFREE86_DEBUG
-        printf("Available modes: (sorted)\n");
+    /* XVidMode */
+    if ( !use_xrandr &&
+         (!use_xinerama || xinerama_info.screen_number == 0) &&
+         CheckVidMode(this, &vm_major, &vm_minor) &&
+         SDL_NAME(XF86VidModeGetAllModeLines)(SDL_Display, SDL_Screen,&nmodes,&modes) )
+    {
+#ifdef X11MODES_DEBUG
+        printf("VidMode modes: (unsorted)\n");
         for ( i = 0; i < nmodes; ++i ) {
             printf("Mode %d: %d x %d @ %d\n", i,
                     modes[i]->hdisplay, modes[i]->vdisplay,
-                    1000 * modes[i]->dotclock / (modes[i]->htotal *
-                    modes[i]->vtotal) );
+                    (modes[i]->htotal && modes[i]->vtotal) ? (1000 * modes[i]->dotclock / (modes[i]->htotal * modes[i]->vtotal)) : 0 );
         }
 #endif
-
-        SDL_qsort(modes, nmodes, sizeof *modes, cmpmodes);
+        if ( SDL_modelist ) {
+            for ( i = 0; SDL_modelist[i]; ++i ) {
+                SDL_free(SDL_modelist[i]);
+            }
+            SDL_free(SDL_modelist);
+        }
         SDL_modelist = (SDL_Rect **)SDL_malloc((nmodes+2)*sizeof(SDL_Rect *));
-        if ( SDL_modelist ) {
-            n = 0;
-            for ( i=0; i<nmodes; ++i ) {
-                int w, h;
+        if ( !SDL_modelist ) {
+            SDL_OutOfMemory();
+            return -1;
+        }
+        SDL_qsort(modes, nmodes, sizeof *modes, cmpmodes);
+        n = 0;
+        for ( i=0; i<nmodes; ++i ) {
+            int w, h;
 
-		/* Eliminate duplicate modes with different refresh rates */
-		if ( i > 0 &&
-		     modes[i]->hdisplay == modes[i-1]->hdisplay &&
-		     modes[i]->vdisplay == modes[i-1]->vdisplay ) {
-			continue;
-		}
+            /* Eliminate duplicate modes with different refresh rates */
+            if ( i > 0 &&
+                 modes[i]->hdisplay == modes[i-1]->hdisplay &&
+                 modes[i]->vdisplay == modes[i-1]->vdisplay ) {
+                    continue;
+            }
 
-                /* Check to see if we should add the screen size (Xinerama) */
-                w = modes[i]->hdisplay;
-                h = modes[i]->vdisplay;
-                if ( (screen_w * screen_h) >= (w * h) ) {
-                    if ( (screen_w != w) || (screen_h != h) ) {
-                        SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
-                        if ( SDL_modelist[n] ) {
-                            SDL_modelist[n]->x = 0;
-                            SDL_modelist[n]->y = 0;
-                            SDL_modelist[n]->w = screen_w;
-                            SDL_modelist[n]->h = screen_h;
-                            ++n;
-                        }
+            /* Check to see if we should add the screen size (Xinerama) */
+            w = modes[i]->hdisplay;
+            h = modes[i]->vdisplay;
+            if ( (screen_w * screen_h) >= (w * h) ) {
+                if ( (screen_w != w) || (screen_h != h) ) {
+                    SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
+                    if ( SDL_modelist[n] ) {
+                        SDL_modelist[n]->x = 0;
+                        SDL_modelist[n]->y = 0;
+                        SDL_modelist[n]->w = screen_w;
+                        SDL_modelist[n]->h = screen_h;
+                        ++n;
                     }
-                    screen_w = 0;
-                    screen_h = 0;
                 }
+                screen_w = 0;
+                screen_h = 0;
+            }
 
-                /* Add the size from the video mode list */
-                SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
-                if ( SDL_modelist[n] == NULL ) {
-                    break;
-                }
-                SDL_modelist[n]->x = 0;
-                SDL_modelist[n]->y = 0;
-                SDL_modelist[n]->w = w;
-                SDL_modelist[n]->h = h;
-                ++n;
+            /* Add the size from the video mode list */
+            SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
+            if ( SDL_modelist[n] == NULL ) {
+                break;
             }
-            SDL_modelist[n] = NULL;
+            SDL_modelist[n]->x = 0;
+            SDL_modelist[n]->y = 0;
+            SDL_modelist[n]->w = w;
+            SDL_modelist[n]->h = h;
+            ++n;
         }
+        SDL_modelist[n] = NULL;
         XFree(modes);
 
         use_vidmode = vm_major * 100 + vm_minor;
@@ -548,71 +701,53 @@
     }
 #endif /* SDL_VIDEO_DRIVER_X11_VIDMODE */
 
-                                /* XiG */
 #if SDL_VIDEO_DRIVER_X11_XME
+    /* XiG */
+    modelist = NULL;
     /* first lets make sure we have the extension, and it's at least v2.0 */
-    if (XiGMiscQueryVersion(SDL_Display, &xme_major, &xme_minor)) {
-#ifdef XIG_DEBUG
-        fprintf(stderr, "XME: XiGMiscQueryVersion: V%d.%d\n",
-                xme_major, xme_minor);
-#endif
-        /* work around a XiGMisc bogosity in our version of libXext */
-        if (xme_major == 0 && xme_major == 0) {
-            /* Ideally libxme would spit this out, but the problem is that
-               the right Query func will never be called if using the bogus
-               libXext version.
-             */
-            fprintf(stderr, 
-"XME: If you are using Xi Graphics CDE and a Summit server, you need to\n"
-"XME: get the libXext update from Xi's ftp site before fullscreen switching\n"
-"XME: will work.  Fullscreen switching is only supported on Summit Servers\n");
-          }
-    } else {
-        /* not there. Bummer. */
-        xme_major = xme_minor = 0;
-    }
-
-    modelist = NULL;
-    if (xme_major >= 2 && (nummodes = XiGMiscQueryResolutions(SDL_Display, 
-                                            SDL_Screen,
-                                            0, /* view */
-                                            &ractive, 
-                                            &modelist)) > 1)
+    if ( CheckXME(this, &xme_major, &xme_minor) && xme_major >= 2 &&
+         (nummodes = XiGMiscQueryResolutions(SDL_Display, SDL_Screen,
+                                             0, /* view */
+                                             &ractive, &modelist)) > 1 )
     {                                /* then we actually have some */
         int j;
 
-#ifdef XIG_DEBUG
+        /* We get the list already sorted in descending order.
+           We'll copy it in reverse order so SDL is happy */
+#ifdef X11MODES_DEBUG
         fprintf(stderr, "XME: nummodes = %d, active mode = %d\n",
                 nummodes, ractive);
 #endif
-
+        if ( SDL_modelist ) {
+            for ( i = 0; SDL_modelist[i]; ++i ) {
+                SDL_free(SDL_modelist[i]);
+            }
+            SDL_free(SDL_modelist);
+        }
         SDL_modelist = (SDL_Rect **)SDL_malloc((nummodes+1)*sizeof(SDL_Rect *));
+        if ( !SDL_modelist ) {
+            SDL_OutOfMemory();
+            return -1;
+        }
+        for ( i=0, j=nummodes-1; j>=0; i++, j-- ) {
+            if ((SDL_modelist[i] = 
+                 (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect))) == NULL)
+              break;
+#ifdef X11MODES_DEBUG
+            fprintf(stderr, "XME: mode = %4d, w = %4d, h = %4d\n",
+                   i, modelist[i].width, modelist[i].height);
+#endif
+            
+            SDL_modelist[i]->x = 0;
+            SDL_modelist[i]->y = 0;
+            SDL_modelist[i]->w = modelist[j].width;
+            SDL_modelist[i]->h = modelist[j].height;
+            
+        }
+        SDL_modelist[i] = NULL; /* terminator */
 
-                                /* we get the list already sorted in */
-                                /* descending order.  We'll copy it in */
-                                /* reverse order so SDL is happy */
-        if (SDL_modelist) {
-            for ( i=0, j=nummodes-1; j>=0; i++, j-- ) {
-                if ((SDL_modelist[i] = 
-                     (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect))) == NULL)
-                  break;
-#ifdef XIG_DEBUG
-                fprintf(stderr, "XME: mode = %4d, w = %4d, h = %4d\n",
-                       i, modelist[i].width, modelist[i].height);
-#endif
-                
-                SDL_modelist[i]->x = 0;
-                SDL_modelist[i]->y = 0;
-                SDL_modelist[i]->w = modelist[j].width;
-                SDL_modelist[i]->h = modelist[j].height;
-                
-            }
-            SDL_modelist[i] = NULL; /* terminator */
-        }
-        use_xme = 1;
+        use_xme = xme_major * 100 + xme_minor;
         saved_res = modelist[ractive]; /* save the current resolution */
-    } else {
-        use_xme = 0;
     }
     if ( modelist ) {
         XFree(modelist);
@@ -668,31 +803,38 @@
 
     if ( SDL_modelist == NULL ) {
         SDL_modelist = (SDL_Rect **)SDL_malloc((1+1)*sizeof(SDL_Rect *));
-        if ( SDL_modelist ) {
-            n = 0;
-            SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
-            if ( SDL_modelist[n] ) {
-                SDL_modelist[n]->x = 0;
-                SDL_modelist[n]->y = 0;
-                SDL_modelist[n]->w = screen_w;
-                SDL_modelist[n]->h = screen_h;
-                ++n;
-            }
-            SDL_modelist[n] = NULL;
+        if ( !SDL_modelist ) {
+            SDL_OutOfMemory();
+            return -1;
         }
+        n = 0;
+        SDL_modelist[n] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
+        if ( SDL_modelist[n] ) {
+            SDL_modelist[n]->x = 0;
+            SDL_modelist[n]->y = 0;
+            SDL_modelist[n]->w = screen_w;
+            SDL_modelist[n]->h = screen_h;
+            ++n;
+        }
+        SDL_modelist[n] = NULL;
     }
 
-#if defined(XFREE86_DEBUG) || defined(XIG_DEBUG)
-    if ( use_vidmode ) {
-        printf("XFree86 VidMode is enabled\n");
+#ifdef X11MODES_DEBUG
+    if ( use_xinerama ) {
+        printf("Xinerama is enabled\n");
     }
 
-#if SDL_VIDEO_DRIVER_X11_XME
-    if ( use_xme )
-      printf("Xi Graphics XME fullscreen is enabled\n");
-    else
-      printf("Xi Graphics XME fullscreen is not available\n");
-#endif 
+    if ( use_xrandr ) {
+        printf("XRandR is enabled\n");
+    }
+
+    if ( use_vidmode ) {
+        printf("VidMode is enabled\n");
+    }
+
+    if ( use_xme ) {
+        printf("Xi Graphics XME fullscreen is enabled\n");
+    }
 
     if ( SDL_modelist ) {
         printf("X11 video mode list:\n");
@@ -700,46 +842,7 @@
             printf("\t%dx%d\n", SDL_modelist[i]->w, SDL_modelist[i]->h);
         }
     }
-#endif /* XFREE86_DEBUG || XIG_DEBUG */
-
-    /* The default X/Y fullscreen offset is 0/0 */
-    xinerama_x = 0;
-    xinerama_y = 0;
-
-#if SDL_VIDEO_DRIVER_X11_XINERAMA
-    /* Query Xinerama extention */
-    if ( SDL_NAME(XineramaQueryExtension)(SDL_Display, &i, &i) &&
-         SDL_NAME(XineramaIsActive)(SDL_Display) ) {
-        /* Find out which screen is the desired one */
-        int desired = 0;
-        int screens;
-        SDL_NAME(XineramaScreenInfo) *xinerama;
-
-#ifdef XINERAMA_DEBUG
-        printf("X11 detected Xinerama:\n");
-#endif
-#if 0 /* Apparently the vidmode extension doesn't work with Xinerama */
-        const char *variable = SDL_getenv("SDL_VIDEO_X11_XINERAMA_SCREEN");
-        if ( variable ) {
-                desired = atoi(variable);
-        }
-#endif
-        xinerama = SDL_NAME(XineramaQueryScreens)(SDL_Display, &screens);
-        for ( i = 0; i < screens; i++ ) {
-#ifdef XINERAMA_DEBUG
-            printf("xinerama %d: %dx%d+%d+%d\n",
-                xinerama[i].screen_number,
-                xinerama[i].width, xinerama[i].height,
-                xinerama[i].x_org, xinerama[i].y_org);
-#endif
-            if ( xinerama[i].screen_number == desired ) {
-                xinerama_x = xinerama[i].x_org;
-                xinerama_y = xinerama[i].y_org;
-            }
-        }
-        XFree(xinerama);
-    }
-#endif /* SDL_VIDEO_DRIVER_X11_XINERAMA */
+#endif /* X11MODES_DEBUG */
 
     return 0;
 }
@@ -789,7 +892,7 @@
 
 int X11_ResizeFullScreen(_THIS)
 {
-    int x, y;
+    int x = 0, y = 0;
     int real_w, real_h;
     int screen_w;
     int screen_h;
@@ -797,8 +900,14 @@
     screen_w = DisplayWidth(SDL_Display, SDL_Screen);
     screen_h = DisplayHeight(SDL_Display, SDL_Screen);
 
-    x = xinerama_x;
-    y = xinerama_y;
+#if SDL_VIDEO_DRIVER_X11_VIDMODE
+    if ( use_xinerama &&
+         window_w <= xinerama_info.width &&
+         window_h <= xinerama_info.height ) {
+        x = xinerama_info.x_org;
+        y = xinerama_info.y_org;
+    }
+#endif
     if ( currently_fullscreen ) {
         /* Switch resolution and cover it with the FSwindow */
         move_cursor_to(this, x, y);
@@ -840,6 +949,7 @@
     Window tmpwin, *windows;
     int i, nwindows;
 #endif
+    int x = 0, y = 0;
     int real_w, real_h;
     int screen_w;
     int screen_h;
@@ -852,6 +962,14 @@
     /* Ungrab the input so that we can move the mouse around */
     X11_GrabInputNoLock(this, SDL_GRAB_OFF);
 
+#if SDL_VIDEO_DRIVER_X11_VIDMODE
+    if ( use_xinerama &&
+         window_w <= xinerama_info.width &&
+         window_h <= xinerama_info.height ) {
+        x = xinerama_info.x_org;
+        y = xinerama_info.y_org;
+    }
+#endif
     /* Map the fullscreen window to blank the screen */
     screen_w = DisplayWidth(SDL_Display, SDL_Screen);
     screen_h = DisplayHeight(SDL_Display, SDL_Screen);
@@ -863,7 +981,7 @@
         real_h = MAX(real_h, screen_h);
     }
     XMoveResizeWindow(SDL_Display, FSwindow,
-                      xinerama_x, xinerama_y, real_w, real_h);
+                      x, y, real_w, real_h);
     XMapRaised(SDL_Display, FSwindow);
     X11_WaitMapped(this, FSwindow);
 
@@ -908,8 +1026,9 @@
     if ( SDL_XColorMap ) {
         XInstallColormap(SDL_Display, SDL_XColorMap);
     }
-    if ( okay )
+    if ( okay ) {
         X11_GrabInputNoLock(this, this->input_grab | SDL_GRAB_FULLSCREEN);
+    }
 
     /* We may need to refresh the screen at this point (no backing store)
        We also don't get an event, which is why we explicitly refresh. */