Use new C2P routine + corrections for iconification window
authorPatrice Mandin <patmandin@gmail.com>
Wed, 12 Nov 2003 18:49:29 +0000
changeset 736 028e03e273c8
parent 735 abec2a842d11
child 737 7e4347dd2f9c
Use new C2P routine + corrections for iconification window
src/video/gem/SDL_gemevents.c
src/video/gem/SDL_gemmouse.c
src/video/gem/SDL_gemvideo.c
src/video/gem/SDL_gemvideo.h
src/video/gem/SDL_gemwm.c
--- a/src/video/gem/SDL_gemevents.c	Wed Nov 12 18:46:48 2003 +0000
+++ b/src/video/gem/SDL_gemevents.c	Wed Nov 12 18:49:29 2003 +0000
@@ -219,6 +219,11 @@
 				/* Send an internal deactivate event */
 				SDL_PrivateAppActive(0, SDL_APPACTIVE|SDL_APPINPUTFOCUS);
 			}
+			/* Update window title */
+			if (GEM_refresh_name && GEM_icon_name) {
+				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
+				GEM_refresh_name = SDL_FALSE;
+			}
 			break;
 		case WM_UNICONIFY:
 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
@@ -227,6 +232,10 @@
 				/* Send an internal activate event */
 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
 			}
+			if (GEM_refresh_name && GEM_title_name) {
+				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
+				GEM_refresh_name = SDL_FALSE;
+			}
 			break;
 		case WM_SIZED:
 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
--- a/src/video/gem/SDL_gemmouse.c	Wed Nov 12 18:46:48 2003 +0000
+++ b/src/video/gem/SDL_gemmouse.c	Wed Nov 12 18:49:29 2003 +0000
@@ -43,6 +43,8 @@
 
 /* Defines */
 
+/*#define DEBUG_VIDEO_GEM 1*/
+
 #define MAXCURWIDTH 16
 #define MAXCURHEIGHT 16
 
@@ -102,36 +104,46 @@
 	new_mform->mf_fg = 0;
 	new_mform->mf_bg = 1;
 
-	for (i=0;i<MAXCURHEIGHT;i++)
-	{
+	for (i=0;i<MAXCURHEIGHT;i++) {
 		new_mform->mf_mask[i]=0;
 		new_mform->mf_data[i]=0;
 	}
 
 	if (w<=8) {
-		for (i=0;i<h;i++)
-		{
+		for (i=0;i<h;i++) {
 			new_mform->mf_mask[i]= mask[i]<<8;
 			new_mform->mf_data[i]= data[i]<<8;
 		}
 	} else {
-		for (i=0;i<h;i++)
-		{
+		for (i=0;i<h;i++) {
 			new_mform->mf_mask[i]= mask[i<<1]<<8 | mask[(i<<1)+1];
 			new_mform->mf_data[i]= data[i<<1]<<8 | data[(i<<1)+1];
 		}
 	}
 
+#ifdef DEBUG_VIDEO_GEM
+	for (i=0; i<h ;i++) {
+		printf("sdl:video:gem: cursor, line %d = 0x%04x\n", i, new_mform->mf_mask[i]);
+	}
+
+	printf("sdl:video:gem: CreateWMCursor(): done\n");
+#endif
+
 	return cursor;
 }
 
 int GEM_ShowWMCursor(_THIS, WMcursor *cursor)
 {
+/*
 	if (cursor == NULL) {
 		graf_mouse(M_OFF, NULL);
 	} else if (cursor->mform_p) {
 		graf_mouse(USER_DEF, cursor->mform_p);
 	}
+*/
+#ifdef DEBUG_VIDEO_GEM
+	printf("sdl:video:gem: ShowWMCursor(0x%08x)\n", (long) cursor);
+#endif
 
 	return 1;
 }
--- a/src/video/gem/SDL_gemvideo.c	Wed Nov 12 18:46:48 2003 +0000
+++ b/src/video/gem/SDL_gemvideo.c	Wed Nov 12 18:49:29 2003 +0000
@@ -26,13 +26,12 @@
 #endif
 
 /*
- * GEM SDL video driver implementation
- * inspired from the Dummy SDL driver
- * 
- * Patrice Mandin
- * and work from
- * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
- */
+	GEM video driver
+
+	Patrice Mandin
+	and work from
+	Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
+*/
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -54,7 +53,6 @@
 #include "SDL_cursor_c.h"
 
 #include "SDL_ataric2p_s.h"
-#include "SDL_ataric2p060_c.h"
 #include "SDL_atarieddi_s.h"
 #include "SDL_atarimxalloc_c.h"
 #include "SDL_gemvideo.h"
@@ -65,6 +63,8 @@
 
 /* Defines */
 
+/*#define DEBUG_VIDEO_GEM	1*/
+
 #define GEM_VID_DRIVER_NAME "gem"
 
 /* Variables */
@@ -97,6 +97,7 @@
 /* Internal functions */
 static void GEM_FreeBuffers(_THIS);
 static void GEM_ClearScreen(_THIS);
+static void GEM_ClearRect(_THIS, short *rect);
 static void GEM_LockScreen(_THIS);
 static void GEM_UnlockScreen(_THIS);
 static void refresh_window(_THIS, int winhandle, short *rect);
@@ -139,8 +140,6 @@
 	}
 	memset(device->hidden, 0, (sizeof *device->hidden));
 
-	atari_test_cpu060_present();
-
 	/* Set the function pointers */
 	device->VideoInit = GEM_VideoInit;
 	device->ListModes = GEM_ListModes;
@@ -157,7 +156,7 @@
 
 	/* Window manager */
 	device->SetCaption = GEM_SetCaption;
-	device->SetIcon = NULL /*GEM_SetIcon*/;
+	device->SetIcon = GEM_SetIcon;
 	device->IconifyWindow = GEM_IconifyWindow;
 	device->GrabInput = GEM_GrabInput;
 
@@ -218,48 +217,26 @@
 				if (work_out[14] & (1<<7)) {
 					/* Little endian */
 					if (work_out[14] & (1<<1)) {
-						/* Falcon */
-						VDI_alphamask = 1 << 13;
-						VDI_redmask = 31 << 3;
-						VDI_greenmask = (3 << 14) | 7;
-						VDI_bluemask = 31 << 8;
+						VDI_FBMASK(1<<13, 31<<3, (3<<14)|7, 31<<8);
 					} else {
-						/* Others */
-						VDI_alphamask = 1 << 7;
-						VDI_redmask = 31 << 2;
-						VDI_greenmask = (7 << 13) | 3;
-						VDI_bluemask = 31 << 8;
+						VDI_FBMASK(1<<7, 31<<2, (7<<13)|3, 31<<8);
 					}
 				} else {
 					/* Big endian */
 					if (work_out[14] & (1<<1)) {
-						/* Falcon */
-						VDI_alphamask = 1 << 5;
-						VDI_redmask = 31 << 11;
-						VDI_greenmask = 31 << 6;
-						VDI_bluemask = 31;
+						VDI_FBMASK(1<<5, 31<<11, 31<<6, 31);
 					} else {
-						/* Others */
-						VDI_alphamask = 1 << 15;
-						VDI_redmask = 31 << 10;
-						VDI_greenmask = 31 << 5;
-						VDI_bluemask = 31;
+						VDI_FBMASK(1<<15, 31<<10, 31<<5, 31);
 					}
 				}
 				break;
 			case 65536UL:
 				if (work_out[14] & (1<<7)) {
 					/* Little endian */
-					VDI_alphamask = 0;
-					VDI_redmask = 31 << 3;
-					VDI_greenmask = (7 << 13) | 7;
-					VDI_bluemask = 31 << 8;
+					VDI_FBMASK(0, 31<<3, (7<<13)|7, 31<<8);
 				} else {
 					/* Big endian */
-					VDI_alphamask = 0;
-					VDI_redmask = 31 << 11;
-					VDI_greenmask = 63 << 5;
-					VDI_bluemask = 31;
+					VDI_FBMASK(0, 31<<11, 63<<5, 31);
 				}
 				break;
 			case 16777216UL:
@@ -267,32 +244,20 @@
 					/* Little endian */
 					switch(num_bits) {
 						case 24:
-							VDI_alphamask = 0;
-							VDI_redmask = 255;
-							VDI_greenmask = 255 << 8;
-							VDI_bluemask = 255 << 16;
+							VDI_FBMASK(0, 255, 255<<8, 255<<16);
 							break;
 						case 32:
-							VDI_alphamask = 255;
-							VDI_redmask = 255 << 8;
-							VDI_greenmask = 255 << 16;
-							VDI_bluemask = 255 << 24;
+							VDI_FBMASK(255, 255<<8, 255<<16, 255<<24);
 							break;
 					}
 				} else {
 					/* Big endian */
 					switch(num_bits) {
 						case 24:
-							VDI_alphamask = 0;
-							VDI_redmask = 255 << 16;
-							VDI_greenmask = 255 << 8;
-							VDI_bluemask = 255;
+							VDI_FBMASK(0, 255<<16, 255<<8, 255);
 							break;
 						case 32:
-							VDI_alphamask = 255 << 24;
-							VDI_redmask = 255 << 16;
-							VDI_greenmask = 255 << 8;
-							VDI_bluemask = 255;
+							VDI_FBMASK(255<<24, 255<<16, 255<<8, 255);
 							break;
 					}
 				}
@@ -371,13 +336,31 @@
 	}
 
 	/* Read version and features */
-	GEM_version = aes_params.global[0];
-	if (GEM_version >= 0x0400) {
-		short ap_gout[4];
+	GEM_version = aes_global[0];
+	if (GEM_version >= 0x0410) {
+		short ap_gout[4], errorcode;
 		
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem: AES %02x.%02x\n", (GEM_version>>8) & 0xff, GEM_version & 0xff);
+#endif
+
 		GEM_wfeatures=0;
-		if (appl_getinfo(AES_WINDOW, &ap_gout[0], &ap_gout[1], &ap_gout[2], &ap_gout[3])==0) {
+		errorcode=appl_getinfo(AES_WINDOW, &ap_gout[0], &ap_gout[1], &ap_gout[2], &ap_gout[3]);
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem: appl_getinfo() returned 0x%04x\n", errorcode);
+#endif
+
+		if (errorcode==0) {
 			GEM_wfeatures=ap_gout[0];			
+
+#ifdef DEBUG_VIDEO_GEM
+			printf("sdl:video:gem: AES wind_*() modes: 0x%04x\n", GEM_wfeatures);
+			printf("sdl:video:gem: AES window behaviours: 0x%04x\n", ap_gout[3]);
+		} else {
+			printf("sdl:video:gem: apgout[]={0x%04x,0x%04x,0x%04x,0x%04x}\n",
+				ap_gout[0], ap_gout[1], ap_gout[1], ap_gout[3]
+			);
+#endif
 		}
 	}	
 
@@ -469,7 +452,10 @@
 	VDI_ReadExtInfo(this, work_out);
 	if (VDI_screen == NULL) {
 		VDI_pitch = VDI_w * VDI_pixelsize;
-		VDI_format = VDI_FORMAT_UNKNOWN;
+		VDI_format = VDI_FORMAT_PACK;
+		if (VDI_bpp <= 8) {
+			VDI_format = VDI_FORMAT_INTER;
+		}
 		VDI_redmask = VDI_greenmask = VDI_bluemask = VDI_alphamask = 0;
 	}
 
@@ -480,14 +466,6 @@
 	this->info.hw_available = 0;
 	this->info.video_mem = 0;
 
-	/*	TC, screen : no shadow (direct)
-     *  8P, screen: no shadow (direct)
-     *  8I, screen: shadow, c2p (shadow -> c2p)
-	 *  TC, no screen: shadow (vro_cpyfm)
-	 *  8P, no screen: shadow (vro_cpyfm)
-	 *  8I/U, no screen: shadow, shadow_c2p, c2p (shadow -> c2p -> vro_cpyfm)
-	 */
-
 	/* Determine the screen depth */
 	/* we change this during the SDL_SetVideoMode implementation... */
 	vformat->BitsPerPixel = VDI_bpp;
@@ -496,13 +474,16 @@
 	graf_mouse(ARROW, NULL);
 
 	/* Init chunky to planar routine */
-	Atari_C2pInit = Atari_C2pInit8;
-	if (atari_cpu060_avail) {
-		Atari_C2pConvert = Atari_C2pConvert8_060;
-	} else {
-		Atari_C2pConvert = Atari_C2pConvert8;
-	}
-	Atari_C2pInit();
+	SDL_Atari_C2pConvert = SDL_Atari_C2pConvert8;
+
+	/* Setup VDI fill functions */
+	vsf_color(VDI_handle,0);
+	vsf_interior(VDI_handle,1);
+	vsf_perimeter(VDI_handle,0);
+
+#ifdef DEBUG_VIDEO_GEM
+	printf("sdl:video:gem: VideoInit(): done\n");
+#endif
 
 	/* We're done! */
 	return(0);
@@ -520,9 +501,14 @@
 static void GEM_FreeBuffers(_THIS)
 {
 	/* Release buffer */
-	if ( GEM_buffer ) {
-		free( GEM_buffer );
-		GEM_buffer=NULL;
+	if ( GEM_buffer2 ) {
+		free( GEM_buffer2 );
+		GEM_buffer2=NULL;
+	}
+
+	if ( GEM_buffer1 ) {
+		free( GEM_buffer1 );
+		GEM_buffer1=NULL;
 	}
 
 	/* Destroy window */
@@ -533,26 +519,31 @@
 	}
 }
 
+static void GEM_ClearRect(_THIS, short *rect)
+{
+	short oldrgb[3], rgb[3]={0,0,0};
+
+	vq_color(VDI_handle, vdi_index[0], 0, oldrgb);
+	vs_color(VDI_handle, vdi_index[0], rgb);
+
+	vsf_color(VDI_handle,0);
+	vsf_interior(VDI_handle,1);
+	vsf_perimeter(VDI_handle,0);
+	v_bar(VDI_handle, rect);
+
+	vs_color(VDI_handle, vdi_index[0], oldrgb);
+}
+
 static void GEM_ClearScreen(_THIS)
 {
-	short rgb[3]={0,0,0};
-	short oldrgb[3];
 	short pxy[4];
 
 	v_hide_c(VDI_handle);
 
-	vq_color(VDI_handle, vdi_index[0], 0, oldrgb);
-	vs_color(VDI_handle, vdi_index[0], rgb);
-
 	pxy[0] = pxy[1] = 0;
 	pxy[2] = VDI_w - 1;
 	pxy[3] = VDI_h - 1;
-	vsf_color(VDI_handle,0);
-	vsf_interior(VDI_handle,1);
-	vsf_perimeter(VDI_handle,0);
-	v_bar(VDI_handle,pxy);
-
-	vs_color(VDI_handle, vdi_index[0], oldrgb);
+	GEM_ClearRect(this, pxy);
 
 	v_show_c(VDI_handle, 1);
 }
@@ -588,7 +579,7 @@
 {
 	int maxwidth, maxheight;
 	Uint32 modeflags, screensize;
-	SDL_bool use_shadow;
+	SDL_bool use_shadow1, use_shadow2;
 
 	GEM_FreeBuffers(this);
 
@@ -613,38 +604,48 @@
 		return(NULL);
 	}
 
-	/*--- Allocate shadow buffer if needed ---*/
-	use_shadow=SDL_FALSE;
-	if (flags & SDL_FULLSCREEN) {
-		if (!VDI_screen) {
-			/* No access to real framebuffer, use shadow surface */
-			use_shadow=SDL_TRUE;
-		} else {
-			if (VDI_format==VDI_FORMAT_INTER) {
-				/* Real framebuffer, interleaved bitplanes,
-				  use shadow surface */
-				use_shadow=SDL_TRUE;
-			} else if (flags & SDL_DOUBLEBUF) {
-				/* Real framebuffer, double-buffered,
-				  use shadow surface */
-				use_shadow=SDL_TRUE;
-				modeflags |= SDL_DOUBLEBUF;
-			}
+	screensize = width * height * VDI_pixelsize;
+
+	/*--- Allocate shadow buffers if needed, and conversion operations ---*/
+	GEM_bufops=0;
+	use_shadow1=use_shadow2=SDL_FALSE;
+	if (VDI_screen && (flags & SDL_FULLSCREEN)) {
+		if (VDI_format==VDI_FORMAT_INTER) {
+			use_shadow1=SDL_TRUE;
+			GEM_bufops = B2S_C2P_1TOS;
 		}
 	} else {
-		/* Windowed mode, always with shadow surface */
-		use_shadow=SDL_TRUE;
+		use_shadow1=SDL_TRUE;
+		if (VDI_format==VDI_FORMAT_PACK) {
+			GEM_bufops = B2S_VROCPYFM_1TOS;
+		} else {
+			use_shadow2=SDL_TRUE;
+			GEM_bufops = B2S_C2P_1TO2|B2S_VROCPYFM_2TOS;
+		}
 	}
 
-	if (use_shadow) {
-		screensize = width * height * VDI_pixelsize;
-
-		GEM_buffer = Atari_SysMalloc(screensize, MX_PREFTTRAM);
-		if (GEM_buffer==NULL) {
+	if (use_shadow1) {
+		GEM_buffer1 = Atari_SysMalloc(screensize, MX_PREFTTRAM);
+		if (GEM_buffer1==NULL) {
 			fprintf(stderr,"Unable to allocate shadow buffer\n");
 			return NULL;
 		}
-		memset(GEM_buffer, 0, screensize);
+		memset(GEM_buffer1, 0, screensize);
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem: setvideomode(): allocated buffer 1\n");
+#endif
+	}
+
+	if (use_shadow2) {
+		GEM_buffer2 = Atari_SysMalloc(screensize, MX_PREFTTRAM);
+		if (GEM_buffer2==NULL) {
+			fprintf(stderr,"Unable to allocate shadow buffer\n");
+			return NULL;
+		}
+		memset(GEM_buffer2, 0, screensize);
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem: setvideomode(): allocated buffer 2\n");
+#endif
 	}
 
 	/*--- Initialize screen ---*/
@@ -659,7 +660,7 @@
 		GEM_ClearScreen(this);
 
 		modeflags |= SDL_FULLSCREEN;
-		if (VDI_screen && (VDI_format==VDI_FORMAT_PACK) && !use_shadow) {
+		if (VDI_screen && (VDI_format==VDI_FORMAT_PACK) && !use_shadow1) {
 			modeflags |= SDL_HWSURFACE;
 		} else {
 			modeflags |= SDL_SWSURFACE;
@@ -704,8 +705,13 @@
 			return NULL;
 		}
 
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem: handle=%d\n", GEM_handle);
+#endif
+
 		/* Setup window name */
 		wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
+		GEM_refresh_name = SDL_FALSE;
 	
 		/* Open the window */
 		wind_open(GEM_handle,x2,y2,w2,h2);
@@ -717,8 +723,8 @@
 	current->flags = modeflags;
 	current->w = width;
 	current->h = height;
-	if (use_shadow) {
-		current->pixels = GEM_buffer;
+	if (use_shadow1) {
+		current->pixels = GEM_buffer1;
 		current->pitch = width * (VDI_bpp >> 3);
 	} else {
 		current->pixels = VDI_screen;
@@ -733,7 +739,6 @@
 	return(current);
 }
 
-/* We don't actually allow hardware surfaces other than the main one */
 static int GEM_AllocHWSurface(_THIS, SDL_Surface *surface)
 {
 	return -1;
@@ -743,7 +748,6 @@
 	return;
 }
 
-/* We need to wait for vertical retrace on page flipped displays */
 static int GEM_LockHWSurface(_THIS, SDL_Surface *surface)
 {
 	return(0);
@@ -757,93 +761,96 @@
 static void GEM_UpdateRectsFullscreen(_THIS, int numrects, SDL_Rect *rects)
 {
 	SDL_Surface *surface;
-	MFDB mfdb_src;
-	short blitcoords[8];
 	int i;
 
 	surface = this->screen;
 
-	if (VDI_screen) {
-		if (VDI_format==VDI_FORMAT_INTER) {
-			void *destscr;
+	if (GEM_bufops & (B2S_C2P_1TO2|B2S_C2P_1TOS)) {
+		void *destscr;
+		int destpitch;
+
+		if (GEM_bufops & B2S_C2P_1TOS) {
 			int destx;
-			
+
 			destscr = VDI_screen;
 			destscr += VDI_pitch * ((VDI_h - surface->h) >> 1);
 			destx = (VDI_w - surface->w) >> 1;
 			destx &= ~15;
 			destscr += destx;
-
-			for (i=0;i<numrects;i++) {
-				void *source,*destination;
-				int x1,x2;
-
-				x1 = rects[i].x & ~15;
-				x2 = rects[i].x+rects[i].w;
-				if (x2 & 15) {
-					x2 = (x2 | 15) +1;
-				}
-
-				source = surface->pixels;
-				source += surface->pitch * rects[i].y;
-				source += x1;
+			destpitch = VDI_pitch;
+		} else {
+			destscr = GEM_buffer2;
+			destpitch = surface->pitch;
+		}
 
-				destination = destscr;
-				destination += VDI_pitch * rects[i].y;
-				destination += x1;
+		for (i=0;i<numrects;i++) {
+			void *source,*destination;
+			int x1,x2;
 
-				/* Convert chunky to planar screen */
-				Atari_C2pConvert(
-					source,
-					destination,
-					x2-x1,
-					rects[i].h,
-					SDL_FALSE,
-					surface->pitch,
-					VDI_pitch
-				);
-
+			x1 = rects[i].x & ~15;
+			x2 = rects[i].x+rects[i].w;
+			if (x2 & 15) {
+				x2 = (x2 | 15) +1;
 			}
 
-			return;
-		}
+			source = surface->pixels;
+			source += surface->pitch * rects[i].y;
+			source += x1;
 
-		if (!(surface->flags & SDL_DOUBLEBUF)) {
-			return;
+			destination = destscr;
+			destination += destpitch * rects[i].y;
+			destination += x1;
+
+			SDL_Atari_C2pConvert(
+				source, destination,
+				x2-x1, rects[i].h,
+				SDL_FALSE,
+				surface->pitch, destpitch
+			);
 		}
 	}
 
-	mfdb_src.fd_addr=surface->pixels;
-	mfdb_src.fd_w=surface->w;
-	mfdb_src.fd_h=surface->h;
-	mfdb_src.fd_wdwidth=(surface->w) >> 4;
-	mfdb_src.fd_stand=0;
-	mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
-	mfdb_src.fd_r1=0;
-	mfdb_src.fd_r2=0;
-	mfdb_src.fd_r3=0;
+	if (GEM_bufops & (B2S_VROCPYFM_1TOS|B2S_VROCPYFM_2TOS)) {
+		MFDB mfdb_src;
+		short blitcoords[8];
 
-	for ( i=0; i<numrects; ++i ) {
-		blitcoords[0] = rects[i].x;
-		blitcoords[1] = rects[i].y;
-		blitcoords[2] = blitcoords[0] + rects[i].w - 1;
-		blitcoords[3] = blitcoords[1] + rects[i].h - 1;
+		mfdb_src.fd_addr=surface->pixels;
+		mfdb_src.fd_w=surface->w;
+		mfdb_src.fd_h=surface->h;
+		mfdb_src.fd_wdwidth=(surface->w) >> 4;
+		mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
+		mfdb_src.fd_stand=
+			mfdb_src.fd_r1=
+			mfdb_src.fd_r2=
+			mfdb_src.fd_r3= 0;
+		if (GEM_bufops & B2S_VROCPYFM_2TOS) {
+			mfdb_src.fd_addr=GEM_buffer2;
+		}
 
-		blitcoords[4] = rects[i].x + ((VDI_w - surface->w) >> 1);
-		blitcoords[5] = rects[i].y + ((VDI_h - surface->h) >> 1);
-		blitcoords[6] = blitcoords[4] + rects[i].w - 1;
-		blitcoords[7] = blitcoords[5] + rects[i].h - 1;
+		for ( i=0; i<numrects; ++i ) {
+			blitcoords[0] = rects[i].x;
+			blitcoords[1] = rects[i].y;
+			blitcoords[2] = blitcoords[0] + rects[i].w - 1;
+			blitcoords[3] = blitcoords[1] + rects[i].h - 1;
 
-		vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &mfdb_src, &VDI_dst_mfdb);
+			blitcoords[4] = rects[i].x + ((VDI_w - surface->w) >> 1);
+			blitcoords[5] = rects[i].y + ((VDI_h - surface->h) >> 1);
+			blitcoords[6] = blitcoords[4] + rects[i].w - 1;
+			blitcoords[7] = blitcoords[5] + rects[i].h - 1;
+
+			vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &mfdb_src, &VDI_dst_mfdb);
+		}
 	}
 }
 
 static void GEM_UpdateRectsWindowed(_THIS, int numrects, SDL_Rect *rects)
 {
-	short pxy[8], wind_pxy[8];
+	short pxy[4], wind_pxy[4];
 	int i;
 
-	wind_get(GEM_handle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3]);
+	if (wind_get(GEM_handle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3])==0) {
+		return;
+	}
 
 	for ( i=0; i<numrects; ++i ) {
 		pxy[0] = wind_pxy[0] + rects[i].x;
@@ -870,60 +877,62 @@
 
 static int GEM_FlipHWSurfaceFullscreen(_THIS, SDL_Surface *surface)
 {
-	MFDB mfdb_src;
-	short blitcoords[8];
+	if (GEM_bufops & (B2S_C2P_1TO2|B2S_C2P_1TOS)) {
+		void *destscr;
+		int destpitch;
 
-	if (VDI_screen) {
-		if (VDI_format==VDI_FORMAT_INTER) {
-			void *destscr;
+		if (GEM_bufops & B2S_C2P_1TOS) {
 			int destx;
-			
-			/* Center on destination screen */
+
 			destscr = VDI_screen;
 			destscr += VDI_pitch * ((VDI_h - surface->h) >> 1);
 			destx = (VDI_w - surface->w) >> 1;
 			destx &= ~15;
 			destscr += destx;
-
-			/* Convert chunky to planar screen */
-			Atari_C2pConvert(
-				surface->pixels,
-				destscr,
-				surface->w,
-				surface->h,
-				SDL_FALSE,
-				surface->pitch,
-				VDI_pitch
-			);
-
-			return(0);
+			destpitch = VDI_pitch;
+		} else {
+			destscr = GEM_buffer2;
+			destpitch = surface->pitch;
 		}
 
-		if (!(surface->flags & SDL_DOUBLEBUF)) {
-			return(0);
-		}
+		SDL_Atari_C2pConvert(
+			surface->pixels, destscr,
+			surface->w, surface->h,
+			SDL_FALSE,
+			surface->pitch, destpitch
+		);
 	}
-	
-	mfdb_src.fd_addr=surface->pixels;
-	mfdb_src.fd_w=surface->w;
-	mfdb_src.fd_h=surface->h;
-	mfdb_src.fd_wdwidth=(surface->w) >> 4;
-	mfdb_src.fd_stand=0;
-	mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
-	mfdb_src.fd_r1=0;
-	mfdb_src.fd_r2=0;
-	mfdb_src.fd_r3=0;
+
+	if (GEM_bufops & (B2S_VROCPYFM_1TOS|B2S_VROCPYFM_2TOS)) {
+		MFDB mfdb_src;
+		short blitcoords[8];
 
-	blitcoords[0] = 0;
-	blitcoords[1] = 0;
-	blitcoords[2] = surface->w - 1;
-	blitcoords[3] = surface->h - 1;
-	blitcoords[4] = (VDI_w - surface->w) >> 1;
-	blitcoords[5] = (VDI_h - surface->h) >> 1;
-	blitcoords[6] = blitcoords[4] + surface->w - 1;
-	blitcoords[7] = blitcoords[5] + surface->h - 1;
+		mfdb_src.fd_w=surface->w;
+		mfdb_src.fd_h=surface->h;
+		mfdb_src.fd_wdwidth=(surface->w) >> 4;
+		mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
+		mfdb_src.fd_stand=
+			mfdb_src.fd_r1=
+			mfdb_src.fd_r2=
+			mfdb_src.fd_r3= 0;
+		if (GEM_bufops & B2S_VROCPYFM_1TOS) {
+			mfdb_src.fd_addr=surface->pixels;
+		} else {
+			mfdb_src.fd_addr=GEM_buffer2;
+		}
 
-	vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &mfdb_src, &VDI_dst_mfdb);
+		blitcoords[0] = 0;
+		blitcoords[1] = 0;
+		blitcoords[2] = surface->w - 1;
+		blitcoords[3] = surface->h - 1;
+
+		blitcoords[4] = (VDI_w - surface->w) >> 1;
+		blitcoords[5] = (VDI_h - surface->h) >> 1;
+		blitcoords[6] = blitcoords[4] + surface->w - 1;
+		blitcoords[7] = blitcoords[5] + surface->h - 1;
+
+		vro_cpyfm(VDI_handle, S_ONLY, blitcoords, &mfdb_src, &VDI_dst_mfdb);
+	}
 
 	return(0);
 }
@@ -954,6 +963,10 @@
 	int i;
 	SDL_Surface *surface;
 
+#ifdef DEBUG_VIDEO_GEM
+	printf("sdl:video:gem: setcolors()\n");
+#endif
+
 	/* Do not change palette in True Colour */
 	surface = this->screen;
 	if (surface->format->BitsPerPixel > 8) {
@@ -1044,76 +1057,193 @@
 	v_hide_c(VDI_handle);
 
 	/* Browse the rectangle list to redraw */
-	wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3]);
+	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
 
-	while (todo[2] && todo[3]) {
+		while (todo[2] && todo[3]) {
 
-		if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
-			todo[2] += todo[0]-1;
-			todo[3] += todo[1]-1;
-			refresh_window(this, winhandle, todo);
+			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
+				todo[2] += todo[0]-1;
+				todo[3] += todo[1]-1;
+				refresh_window(this, winhandle, todo);
+			}
+
+			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
+				break;
+			}
 		}
 
-		wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3]);
 	}
 
 	/* Update finished */
 	wind_update(END_UPDATE);
 
 	v_show_c(VDI_handle,1);
+
+#if DEBUG_VIDEO_GEM
+	fflush(stdout);
+#endif
 }
 
 static void refresh_window(_THIS, int winhandle, short *rect)
 {
-	MFDB	mfdb_src;
-	short	pxy[8], wind_pxy[8];
+	MFDB mfdb_src;
+	short pxy[8],wind_pxy[8];
+	SDL_Surface *surface;
 	int iconified;
-	SDL_Surface *surface;
-
-	surface=this->screen;
 
 	/* Is window iconified ? */
 	iconified = 0;
-	if (GEM_wfeatures & (1<<WF_ICONIFY)) {
-		wind_get(winhandle, WF_ICONIFY, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3]);
-		iconified = pxy[0];
+/*	if (GEM_wfeatures & (1<<WF_ICONIFY))*/ {
+		if (wind_get(winhandle, WF_ICONIFY, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3])!=0) {
+			iconified = wind_pxy[0];
+		}
+	}
+
+	if (wind_get(winhandle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3])==0) {
+		return;
 	}
 
-	wind_get(winhandle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3]);
+	if (iconified && GEM_icon) {
+		short icon_rect[4], dst_rect[4];
+		
+		surface = GEM_icon;
+
+		GEM_ClearRect(this, rect);
+
+		/* Calculate centered icon(x,y,w,h) relative to window */
+		icon_rect[0] = (wind_pxy[2]-surface->w)>>1;
+		icon_rect[1] = (wind_pxy[3]-surface->h)>>1;
+		icon_rect[2] = surface->w;
+		icon_rect[3] = surface->h;
+
+		/* Calculate redraw rectangle(x,y,w,h) relative to window */
+		dst_rect[0] = rect[0]-wind_pxy[0];
+		dst_rect[1] = rect[1]-wind_pxy[1];
+		dst_rect[2] = rect[2]-rect[0]+1;
+		dst_rect[3] = rect[3]-rect[1]+1;
+
+		/* Does the icon rectangle must be redrawn ? */
+		if (!rc_intersect((GRECT *)icon_rect, (GRECT *)dst_rect)) {
+			return;
+		}
+
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem:  clip(0,0,%d,%d) to (%d,%d,%d,%d)\n",
+			surface->w-1,surface->h-1, dst_rect[0],dst_rect[1],dst_rect[2],dst_rect[3]);
+		printf("sdl:video:gem:  icon(%d,%d,%d,%d)\n",
+			icon_rect[0], icon_rect[1], icon_rect[2], icon_rect[3]);
+		printf("sdl:video:gem: refresh_window(): draw icon\n");
+#endif
+
+		/* Calculate icon(x1,y1,x2,y2) relative to screen */
+		icon_rect[0] += wind_pxy[0];
+		icon_rect[1] += wind_pxy[1];
+		icon_rect[2] += icon_rect[0]-1;
+		icon_rect[3] += icon_rect[1]-1;
 
-	if (iconified) {
-		/* Refresh icon */
-		mfdb_src.fd_addr=surface->pixels;	/* Should be icon image */
-		mfdb_src.fd_w=surface->w;
-		mfdb_src.fd_h=surface->h;
-		mfdb_src.fd_wdwidth=mfdb_src.fd_w>>4;
-	  	mfdb_src.fd_stand=0;
-  		mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
-		mfdb_src.fd_r1=0;
-  		mfdb_src.fd_r2=0;
-	  	mfdb_src.fd_r3=0;
+		/* Calculate intersection rectangle to redraw */
+		pxy[0]=0;
+		pxy[1]=0;
+ 		pxy[2]=surface->w - 1;
+	 	pxy[3]=surface->h - 1;
+		pxy[4]=rect[0];
+		pxy[5]=rect[1];
+		pxy[6]=rect[2];
+		pxy[7]=rect[3];
+
+		if (icon_rect[0]>rect[0]) {
+			pxy[4]=icon_rect[0];
+		} else if (icon_rect[0]<rect[0]) {
+			pxy[0]=rect[0]-icon_rect[0];
+		}
+
+		if (icon_rect[1]>rect[1]) {
+			pxy[5]=icon_rect[1];
+		} else if (icon_rect[1]<rect[1]) {
+			pxy[1]=rect[1]-icon_rect[1];
+		}
+
+		if (icon_rect[2]>rect[2]) {
+			pxy[2]=rect[2]-icon_rect[0];
+			pxy[6]=rect[2];
+		} else if (icon_rect[2]<rect[2]) {
+			pxy[2]=icon_rect[2]-rect[0];
+			pxy[6]=icon_rect[2];
+		}
+
+		if (icon_rect[3]>rect[3]) {
+			pxy[3]=rect[3]-icon_rect[1];
+			pxy[7]=rect[3];
+		} else if (icon_rect[3]<rect[3]) {
+			pxy[3]=icon_rect[3]-rect[1];
+			pxy[7]=icon_rect[3];
+		}
+
 	} else {
-		/* Refresh window */
-		mfdb_src.fd_addr=surface->pixels;
-		mfdb_src.fd_w=surface->w;
-		mfdb_src.fd_h=surface->h;
-		mfdb_src.fd_wdwidth=mfdb_src.fd_w>>4;
-	  	mfdb_src.fd_stand=0;
-  		mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
-		mfdb_src.fd_r1=0;
-  		mfdb_src.fd_r2=0;
-	  	mfdb_src.fd_r3=0;
+		surface = this->screen;
+
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem: refresh_window(): draw frame buffer\n");
+#endif
+
+		/* Redraw all window content */
+		pxy[0] = rect[0]-wind_pxy[0];
+		pxy[1] = rect[1]-wind_pxy[1];
+	 	pxy[2] = rect[2]-wind_pxy[0];   
+	 	pxy[3] = rect[3]-wind_pxy[1];  
+
+		pxy[4] = rect[0];
+		pxy[5] = rect[1];
+		pxy[6] = rect[2];  
+		pxy[7] = rect[3];
 	}
 
-	pxy[0] = rect[0] - wind_pxy[0];
-	pxy[1] = rect[1] - wind_pxy[1];
- 	pxy[2] = pxy[0] + rect[2] - rect[0];   
- 	pxy[3] = pxy[1] + rect[3] - rect[1];  
+	if (GEM_bufops & B2S_C2P_1TO2) {
+		void *src, *dest;
+		int x1,x2;
+
+		x1 = (rect[0]-wind_pxy[0]) & ~15;
+		x2 = rect[2]-wind_pxy[0];
+		if (x2 & 15) {
+			x2 = (x2 | 15) +1;
+		}
+
+		src = surface->pixels;
+		src += surface->pitch * (rect[1]-wind_pxy[1]);
+		src += x1;
+
+		dest = GEM_buffer2;
+		dest += surface->pitch * (rect[1]-wind_pxy[1]);
+		dest += x1;
 
-	pxy[4] = rect[0];
-	pxy[5] = rect[1];
-	pxy[6] = rect[2];  
-	pxy[7] = rect[3];
+		SDL_Atari_C2pConvert(
+			src, dest,
+			x2-x1, rect[3]-rect[1]+1,
+			SDL_FALSE,
+			surface->pitch, surface->pitch
+		);
+	}
+
+	mfdb_src.fd_addr=surface->pixels;
+	mfdb_src.fd_w=surface->w;
+	mfdb_src.fd_h=surface->h;
+  	mfdb_src.fd_nplanes=surface->format->BitsPerPixel;
+	mfdb_src.fd_wdwidth=mfdb_src.fd_w>>4;
+	mfdb_src.fd_stand=
+		mfdb_src.fd_r1=
+  		mfdb_src.fd_r2=
+	  	mfdb_src.fd_r3= 0;
+
+	if (GEM_bufops & B2S_VROCPYFM_2TOS) {
+		mfdb_src.fd_addr=GEM_buffer2;
+	}
+
+#ifdef DEBUG_VIDEO_GEM
+	printf("sdl:video:gem: redraw (%d,%d,%d,%d) to (%d,%d,%d,%d)\n",
+		pxy[0],pxy[1],pxy[2],pxy[3],
+		pxy[4],pxy[5],pxy[6],pxy[7]
+	);
+#endif
 
 	vro_cpyfm( VDI_handle, S_ONLY, pxy, &mfdb_src, &VDI_dst_mfdb);
 }
--- a/src/video/gem/SDL_gemvideo.h	Wed Nov 12 18:46:48 2003 +0000
+++ b/src/video/gem/SDL_gemvideo.h	Wed Nov 12 18:49:29 2003 +0000
@@ -39,37 +39,49 @@
 
 /* Private display data */
 
+#define B2S_C2P_1TO2		(1<<0)	/* C2P convert buffer 1 to buffer 2 */
+#define B2S_C2P_1TOS		(1<<1)	/* C2P convert buffer 1 to screen */
+#define B2S_VROCPYFM_1TOS	(1<<2)	/* vro_cpyfm() buffer 1 to screen */
+#define B2S_VROCPYFM_2TOS	(1<<3)	/* vro_cpyfm() buffer 2 to screen */
+
 #define SDL_NUMMODES	1		/* Fullscreen */
 
 struct SDL_PrivateVideoData {
+	Uint16	buf2scr_ops;		/* Operations to get buffer to screen */
+    void *buffer1;				/* Our shadow buffers */
+	void *buffer2;
+
 	/* VDI infos */
 	short vdi_handle;			/* VDI handle */
 	short full_w, full_h;		/* Fullscreen size */
-    int bpp;					/* Colour depth */
-	int pixelsize;				/* Bytes per pixel */
-	Uint16 old_numcolors;		/* Number of colors in saved palette */
-	Uint16 old_palette[256][3];	/* Saved current palette */
+    short bpp;					/* Colour depth */
+	short pixelsize;			/* Bytes per pixel */
+	short old_numcolors;		/* Number of colors in saved palette */
 	Uint16 pitch;				/* Line length */
-	int format;					/* Screen format */
+	Uint16 format;				/* Screen format */
 	void *screen;				/* Screen address */
 	Uint32 red, green, blue, alpha;	/* Screen components */
 	Uint32 screensize;
+	short	blit_coords[8];		/* Coordinates for bitblt */
 	MFDB	src_mfdb, dst_mfdb;	/* VDI MFDB for bitblt */
-	short	blit_coords[8];		/* Coordinates for bitblt */
+	Uint16 old_palette[256][3];	/* Saved current palette */
+
 	/* GEM infos */
 	short desk_x, desk_y;		/* Desktop properties */
 	short desk_w, desk_h;
 	short win_handle;			/* Our window handle */
-    void *buffer;				/* Our shadow buffer */
 	int window_type;			/* Window type */
 	const char *title_name;		/* Window title */
 	const char *icon_name;		/* Icon title */
 	short version;				/* AES version */
 	short wfeatures;			/* AES window features */
+	SDL_bool refresh_name;		/* Change window title ? */
 	SDL_bool window_fulled;		/* Window maximized ? */
 	SDL_bool mouse_relative;	/* Report relative mouse movement */
 	SDL_bool locked;			/* AES locked for fullscreen ? */
+	short message[8];			/* To self-send an AES message */
 	SDL_Rect *SDL_modelist[SDL_NUMMODES+1];	/* Mode list */
+	SDL_Surface *icon;			/* The icon */
 };
 
 /* Hidden structure -> variables names */
@@ -97,15 +109,66 @@
 #define GEM_desk_w			(this->hidden->desk_w)
 #define GEM_desk_h			(this->hidden->desk_h)
 #define GEM_handle			(this->hidden->win_handle)
-#define GEM_buffer			(this->hidden->buffer)
 #define GEM_win_type		(this->hidden->window_type)
 #define GEM_title_name		(this->hidden->title_name)
 #define GEM_icon_name		(this->hidden->icon_name)
+#define GEM_refresh_name	(this->hidden->refresh_name)
 #define GEM_version			(this->hidden->version)
 #define GEM_wfeatures		(this->hidden->wfeatures)
 #define GEM_win_fulled		(this->hidden->window_fulled)
 #define GEM_mouse_relative	(this->hidden->mouse_relative)
 #define GEM_locked			(this->hidden->locked)
+#define GEM_message			(this->hidden->message)
 #define SDL_modelist		(this->hidden->SDL_modelist)
+#define GEM_icon			(this->hidden->icon)
+
+#define GEM_buffer1			(this->hidden->buffer1)
+#define GEM_buffer2			(this->hidden->buffer2)
+#define GEM_bufops			(this->hidden->buf2scr_ops)
+
+#define VDI_FBMASK(amask, rmask, gmask, bmask) \
+	VDI_alphamask = (amask); \
+	VDI_redmask = (rmask); \
+	VDI_greenmask = (gmask); \
+	VDI_bluemask = (bmask);
+
+/*
+	Possible buffer to screen operations:
+
+	TC: 8 (chunky),15,16,24,32 bpp
+	8I: 8 bpp planes
+	FB: screen framebuffer address available
+	FS: fullscreen
+
+	TC, FB, FS:
+		- draw to screen
+	8I, FB, FS:
+		- draw to buffer 1
+		- C2P from buffer 1 to screen
+
+	TC, !FB, FS:
+		- draw to buffer 1
+		- vro_cpyfm() from buffer 1 to screen
+	8I, !FB, FS:
+		- draw to buffer 1
+		- C2P from buffer 1 to buffer 2
+		- vro_cpyfm() from buffer 2 to screen
+
+	TC, FB, !FS:
+		- draw to buffer 1
+		- vro_cpyfm() from buffer 1 to screen
+	8I, FB, !FS:
+		- draw to buffer 1
+		- C2P from buffer 1 to buffer 2
+		- vro_cpyfm() from buffer 2 to screen
+
+	TC, !FB, !FS:
+		- draw to buffer 1
+		- vro_cpyfm() from buffer 1 to screen
+	8I, !FB, !FS:
+		- draw to buffer 1
+		- C2P from buffer 1 to buffer 2
+		- vro_cpyfm() from buffer 2 to screen
+*/
 
 #endif /* _SDL_gemvideo_h */
--- a/src/video/gem/SDL_gemwm.c	Wed Nov 12 18:46:48 2003 +0000
+++ b/src/video/gem/SDL_gemwm.c	Wed Nov 12 18:49:29 2003 +0000
@@ -26,11 +26,11 @@
 #endif
 
 /*
- *	GEM SDL video driver implementation
- *	Window manager functions
- * 
- *	Patrice Mandin
- */
+	GEM SDL video driver
+	Window manager functions
+
+	Patrice Mandin
+*/
 
 /* Mint includes */
 #include <gem.h>
@@ -39,6 +39,8 @@
 
 /* Defines */
 
+#define DEBUG_VIDEO_GEM 0
+
 #define ICONWIDTH 64
 #define ICONHEIGHT 64
 
@@ -46,65 +48,77 @@
 
 void GEM_SetCaption(_THIS, const char *title, const char *icon)
 {
-	short parm[4];
-	const char *new_name;
-
-	new_name = NULL;
-
-	if (title)
+	if (title) {
 		GEM_title_name = title;
-
-	if (icon)
-		GEM_icon_name = icon;
-
-	/* Is window iconified ? */
-	parm[0]=0;
-	if (GEM_wfeatures & (1<<WF_ICONIFY))
-		wind_get(GEM_handle, WF_ICONIFY, &parm[0], &parm[1], &parm[2], &parm[3]);
-
-	if (parm[0]==0) {
-		/* Change window name */
-		if (title)
-			new_name = title;
-	} else {
-		/* Change icon name */
-		if (icon)
-			new_name = icon;
+		GEM_refresh_name = SDL_TRUE;
 	}
 
-	parm[0]= ((unsigned long) new_name)>>16;
-	parm[1]= ((unsigned long) new_name) & 65535;
-
-	if (new_name) {
-		wind_set(GEM_handle, WF_NAME, parm[0], parm[1], 0, 0);
+	if (icon) {
+		GEM_icon_name = icon;
+		GEM_refresh_name = SDL_TRUE;
 	}
 }
 
 void GEM_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
 {
-	if ((GEM_wfeatures & (1<<WF_ICONIFY))==0)
+	SDL_Surface *sicon;
+	SDL_Rect bounds;
+
+#ifdef DEBUG_VIDEO_GEM
+	printf("sdl:video:gem: SetIcon(0x%08x)\n", (long) icon);
+#endif
+
+#if 0
+	if ((GEM_wfeatures & (1<<WF_ICONIFY))==0) {
+#ifdef DEBUG_VIDEO_GEM
+		printf("sdl:video:gem: AES can not iconify windows\n");
+#endif
+		return;
+	}
+#endif
+
+	if (icon == NULL) {
 		return;
+	}
+	
+	/* Convert icon to the screen format */
+	sicon = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
+		VDI_bpp, VDI_redmask, VDI_greenmask, VDI_bluemask, 0);
+	if ( sicon == NULL ) {
+		return;
+	}
 
-	/* Todo */
+	bounds.x = 0;
+	bounds.y = 0;
+	bounds.w = icon->w;
+	bounds.h = icon->h;
+	if ( SDL_LowerBlit(icon, &bounds, sicon, &bounds) < 0 ) {
+		SDL_FreeSurface(sicon);
+		return;
+	}
+
+	GEM_icon = sicon;
+
+#ifdef DEBUG_VIDEO_GEM
+	printf("sdl:video:gem: SetIcon(): done\n");
+#endif
 }
 
 int GEM_IconifyWindow(_THIS)
 {
-	short message[8];
-	
 	if ((GEM_wfeatures & (1<<WF_ICONIFY))==0)
 		return 0;
 
-	message[0] = WM_ICONIFY;
-	message[1] = gl_apid;
-	message[2] = 0;
-	message[3] = GEM_handle;
-	message[4] = 0;
-	message[5] = GEM_desk_h-ICONHEIGHT;
-	message[6] = ICONWIDTH;
-	message[7] = ICONHEIGHT;
+	GEM_message[0] = WM_ICONIFY;
+	GEM_message[1] = gl_apid;
+	GEM_message[2] = 0;
+	GEM_message[3] = GEM_handle;
+	GEM_message[4] = 0;
+	GEM_message[5] = GEM_desk_h-ICONHEIGHT;
+	GEM_message[6] = ICONWIDTH;
+	GEM_message[7] = ICONHEIGHT;
 
-	appl_write(gl_apid, sizeof(message), message);
+	appl_write(gl_apid, sizeof(GEM_message), GEM_message);
 
 	return 1;
 }