--- SDL_sysvideo.cc.org	2003-10-12 01:25:48.000000000 +0900
+++ SDL_sysvideo.cc	2003-10-13 19:49:06.000000000 +0900
@@ -31,15 +31,19 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
 
 #include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <linux/fb.h>
 
 #include <qapplication.h>
 #include <qpe/qpeapplication.h>
 #include <qpe/qcopenvelope_qws.h>
+#include <qgfx_qws.h>
 
 #include "SDL.h"
 #include "SDL_timer.h"
@@ -132,6 +136,9 @@
   static int QT_IconifyWindow(_THIS);
   static SDL_GrabMode QT_GrabInput(_THIS, SDL_GrabMode mode);
 
+  static int console_fd;
+  struct fb_var_screeninfo saved_vinfo;
+ 
   /* FB driver bootstrap functions */
 
   static int QT_Available(void)
@@ -276,6 +283,9 @@
 
   int QT_VideoInit(_THIS, SDL_PixelFormat *vformat)
   {
+    const char *SDL_fbdev;
+    struct fb_var_screeninfo vinfo;
+
     /* Initialize the QPE Application  */
      /* Determine the screen depth */
     vformat->BitsPerPixel = QPixmap::defaultDepth();
@@ -283,6 +293,23 @@
     // For now we hardcode the current depth because anything else
     // might as well be emulated by SDL rather than by Qtopia.
 
+    SDL_fbdev = getenv("SDL_FBDEV");
+    if ( SDL_fbdev == NULL ) {
+    	SDL_fbdev = "/dev/fb0";
+    }
+    console_fd = open(SDL_fbdev, O_RDWR, 0);
+    if ( console_fd < 0 ) {
+    	SDL_SetError("Unable to open %s", SDL_fbdev);
+    	return(-1);
+    }
+
+    if ( ioctl(console_fd, FBIOGET_VSCREENINFO, &vinfo) < 0 ) {
+    	SDL_SetError("Couldn't get console pixel format");
+    	QT_VideoQuit(_this);
+    	return(-1);
+    }
+    saved_vinfo = vinfo;
+
     QSize desktop_size = qApp->desktop()->size();
     QT_AddMode(_this, ((vformat->BitsPerPixel+7)/8)-1,
 	       desktop_size.width(), desktop_size.height());
@@ -425,6 +452,9 @@
     QSize qteSize = qApp->desktop()->size();
     QSize fbSize;
     QSize userSize;
+    int fb_xres;
+    int fb_yres;
+    struct fb_var_screeninfo vinfo;
 
     machine_t machine = QT_GetMachine(_this);
     machine_spec_t machineSpec = st_machine_spec[machine];
@@ -435,6 +465,36 @@
     // qte での回転角度を取得
     QT_GetQteServerSpec(_this, &qteRotation, &isQteQvga);
 
+    const char *envFb_Size = getenv("SDL_FB_SIZE");
+    if (envFb_Size !=NULL){
+        if (sscanf(envFb_Size,"%dx%d",&fb_xres,&fb_yres) ==2) {
+
+            LOG("QT_FBVideoMode: argSize=%dx%d\n", fb_xres, fb_yres);
+
+            if((fb_xres == 640) && (fb_yres == 480) && (qteSize.width() == 640) && (qteSize.height() == 480)){
+                if ( ioctl(console_fd, FBIOGET_VSCREENINFO, &vinfo) < 0 ) {
+                    SDL_SetError("Couldn't get console pixel format");
+                    QT_VideoQuit(_this);
+                    return(NULL);
+                }
+        
+                vinfo.xres = fb_xres;
+                vinfo.xres_virtual = fb_xres;
+                vinfo.yres = fb_yres;
+                vinfo.yres_virtual = fb_yres;
+    
+                if ( ioctl(console_fd, FBIOPUT_VSCREENINFO, &vinfo) < 0 ) {
+                    SDL_SetError("Couldn't set console screen info");
+                    return(NULL);
+                }
+
+                qteSize.setWidth(fb_xres);
+                qteSize.setHeight(fb_yres);
+                qteRotation=0;
+            }
+        }
+    }
+
     // hack for SL-5500
     if (machine == MACHINE_SHARP_SL5500)
       qteRotation = 3;
@@ -644,6 +704,18 @@
     //    -- David Hedbor
     //    delete SDL_Win; 
     //    SDL_Win = 0;
+
+    if ( console_fd > 0 ) {
+    	/* Restore the original video mode and palette */
+    	ioctl(console_fd, FBIOPUT_VSCREENINFO, &saved_vinfo);
+
+    	/* We're all done with the framebuffer */
+    	close(console_fd);
+    	console_fd = -1;
+    }
+
+
+
     _this->screen->pixels = NULL;
     QT_GrabInput(_this, SDL_GRAB_OFF);
 
