From 2f58ef08ec4003124b4071903ee6e1c73a4092c4 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <slouken@libsdl.org>
Date: Wed, 22 Mar 2006 07:48:22 +0000
Subject: [PATCH] Date: Tue, 4 Mar 2003 15:05:31 -0800 From: "Jim" Subject:
 [SDL] Frame Buffer patches...

 Okay I'm new at patch generation - so please tell me if there's a better way
 I could have done this.

 Attached are two patch files generated with 'cvs diff -u'

 SDL-fb-open-lock.patch applies to SDL_fbvideo.c
    Modifies the open loop to check /dev/fb/0 found on devfs...

    Modifies the lock code to return failure if the current virtual terminal
 is not the one opened for frame buffer writing...
   Lock would hang forever if switched away (ctrl-alt-F1) ...

 SDL-fb-mousedrv-screensave.patch applies to SDL_fbevents.c
    Switches default mouse mode based on SDL_MOUSEDRV - currently only
 accepts PS2 - otherwise default is MS Mouse.

    When the screen is switched - exisiting code (wrapped in ifdef
 SAVE_SCREEN_COTENTS) would save the wrong bit of the screen....
     ( I run frame buffer 1600x1200, the size I requested was 800x600 - the
 save would save the top 800 lines (non biased) and restore them... Adding
 screen->offset fixed that )

    However, if that option is not set, then a call to SDL_UpdateRect (full
 screen) is made. (which may have had it's contents changed since the screen
 is not entirely locked because of lock-failure patch)

 Jim

[patches slightly tweaked for SDL 1.2.10]

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%401590
---
 src/video/fbcon/SDL_fbevents.c | 33 ++++++++++++++++---------
 src/video/fbcon/SDL_fbvideo.c  | 45 +++++++++++++++++++++++++++-------
 2 files changed, 58 insertions(+), 20 deletions(-)

diff --git a/src/video/fbcon/SDL_fbevents.c b/src/video/fbcon/SDL_fbevents.c
index 55a93844..48b63d07 100644
--- a/src/video/fbcon/SDL_fbevents.c
+++ b/src/video/fbcon/SDL_fbevents.c
@@ -505,7 +505,7 @@ int FB_OpenMouse(_THIS)
 	mouse_fd = -1;
 
 #if SDL_INPUT_TSLIB
-	if ((mousedrv != NULL) && (SDL_strcmp(mousedrv, "TSLIB") == 0)) {
+	if ( mousedrv && (SDL_strcmp(mousedrv, "TSLIB") == 0) ) {
 		if (mousedev == NULL) mousedev = SDL_getenv("TSLIB_TSDEVICE");
 		if (mousedev != NULL) {
 			ts_dev = ts_open(mousedev, 1);
@@ -525,7 +525,7 @@ int FB_OpenMouse(_THIS)
 
 	/* ELO TOUCHSCREEN SUPPORT */
 
-	if( (mousedrv != NULL) && (SDL_strcmp(mousedrv, "ELO") == 0) ) {
+	if ( mousedrv && (SDL_strcmp(mousedrv, "ELO") == 0) ) {
 		mouse_fd = open(mousedev, O_RDWR);
 		if ( mouse_fd >= 0 ) {
 			if(eloInitController(mouse_fd)) {
@@ -616,10 +616,17 @@ fprintf(stderr, "Using ADB mouse\n");
 			mouse_termios.c_cflag |= CS8;
 			mouse_termios.c_cflag |= B1200;
 			tcsetattr(mouse_fd, TCSAFLUSH, &mouse_termios);
+			if ( mousedrv && (SDL_strcmp(mousedrv, "PS2") == 0) ) {
 #ifdef DEBUG_MOUSE
-fprintf(stderr, "Using Microsoft mouse on %s\n", mousedev);
+fprintf(stderr, "Using (user specified) PS2 mouse on %s\n", mousedev);
 #endif
-			mouse_drv = MOUSE_MS;
+				mouse_drv = MOUSE_PS2;
+			} else {
+#ifdef DEBUG_MOUSE
+fprintf(stderr, "Using (default) MS mouse on %s\n", mousedev);
+#endif
+				mouse_drv = MOUSE_MS;
+			}
 		}
 	}
 	if ( mouse_fd < 0 ) {
@@ -856,10 +863,10 @@ static void switch_vt(_THIS, unsigned short which)
 	struct fb_var_screeninfo vinfo;
 	struct vt_stat vtstate;
 	unsigned short v_active;
-	SDL_Surface *screen;
 	__u16 saved_pal[3*256];
+	SDL_Surface *screen;
 	Uint32 screen_arealen;
-	Uint8 *screen_contents;
+	Uint8 *screen_contents = NULL;
 
 	/* Figure out whether or not we're switching to a new console */
 	if ( (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) < 0) ||
@@ -872,10 +879,12 @@ static void switch_vt(_THIS, unsigned short which)
 	SDL_mutexP(hw_lock);
 	wait_idle(this);
 	screen = SDL_VideoSurface;
-	screen_arealen = (screen->h*screen->pitch);
-	screen_contents = (Uint8 *)SDL_malloc(screen_arealen);
-	if ( screen_contents ) {
-		SDL_memcpy(screen_contents, screen->pixels, screen_arealen);
+	if ( !SDL_ShadowSurface ) {
+		screen_arealen = (screen->h*screen->pitch);
+		screen_contents = (Uint8 *)SDL_malloc(screen_arealen);
+		if ( screen_contents ) {
+			SDL_memcpy(screen_contents, (Uint8 *)screen->pixels + screen->offset, screen_arealen);
+		}
 	}
 	FB_SavePaletteTo(this, 256, saved_pal);
 	ioctl(console_fd, FBIOGET_VSCREENINFO, &vinfo);
@@ -899,8 +908,10 @@ static void switch_vt(_THIS, unsigned short which)
 	ioctl(console_fd, FBIOPUT_VSCREENINFO, &vinfo);
 	FB_RestorePaletteFrom(this, 256, saved_pal);
 	if ( screen_contents ) {
-		SDL_memcpy(screen->pixels, screen_contents, screen_arealen);
+		SDL_memcpy((Uint8 *)screen->pixels + screen->offset, screen_contents, screen_arealen);
 		SDL_free(screen_contents);
+	} else {
+		SDL_UpdateRect(screen, 0, 0, 0, 0);
 	}
 	SDL_mutexV(hw_lock);
 }
diff --git a/src/video/fbcon/SDL_fbvideo.c b/src/video/fbcon/SDL_fbvideo.c
index 412317d5..098ae89e 100644
--- a/src/video/fbcon/SDL_fbvideo.c
+++ b/src/video/fbcon/SDL_fbvideo.c
@@ -30,6 +30,7 @@
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <asm/page.h>		/* For definition of PAGE_SIZE */
+#include <linux/vt.h>
 
 #include "SDL_video.h"
 #include "SDL_mouse.h"
@@ -153,15 +154,21 @@ static void FB_RestorePalette(_THIS);
 static int FB_Available(void)
 {
 	int console;
-	const char *SDL_fbdev;
-
-	SDL_fbdev = SDL_getenv("SDL_FBDEV");
-	if ( SDL_fbdev == NULL ) {
-		SDL_fbdev = "/dev/fb0";
-	}
-	console = open(SDL_fbdev, O_RDWR, 0);
-	if ( console >= 0 ) {
-		close(console);
+	/* Added check for /fb/0 (devfs) */
+	/* but - use environment variable first... if it fails, still check defaults */
+	int idx = 0;
+	const char *SDL_fbdevs[4] = { NULL, "/dev/fb0", "/dev/fb/0", NULL };
+
+	SDL_fbdevs[0] = SDL_getenv("SDL_FBDEV");
+	if( !SDL_fbdevs[0] )
+		idx++;
+	for( ; SDL_fbdevs[idx]; idx++ )
+	{
+		console = open(SDL_fbdevs[idx], O_RDWR, 0);
+		if ( console >= 0 ) {
+			close(console);
+			break;
+		}
 	}
 	return(console >= 0);
 }
@@ -1230,8 +1237,28 @@ static void FB_FreeHWSurface(_THIS, SDL_Surface *surface)
 	surface->pixels = NULL;
 	surface->hwdata = NULL;
 }
+
+/* Routine to check to see if the frame buffer virtual terminal */
+/* is the current(active) one.  If it is not, result will cause */
+/* Lock to fail.  (would have waited forever, since the fbevent */
+/* keyboard handler maintains a lock when switched away from    */
+/* current) */
+static __inline__ int FB_IsFrameBufferActive(_THIS)
+{
+	struct vt_stat vtstate;
+	if ( (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) < 0) ||
+	     (current_vt != vtstate.v_active) ) {
+		return 0;
+	}
+	return 1;
+}
+
+
 static int FB_LockHWSurface(_THIS, SDL_Surface *surface)
 {
+	if ( !FB_IsFrameBufferActive(this) ) {
+		return -1; /* fail locking. */
+	}
 	if ( surface == this->screen ) {
 		SDL_mutexP(hw_lock);
 		if ( FB_IsSurfaceBusy(surface) ) {
-- 
2.18.1