Date: Sat, 23 Mar 2002 13:53:37 +0200
authorSam Lantinga <slouken@libsdl.org>
Sat, 23 Mar 2002 20:19:44 +0000
changeset 315 3333b6e68289
parent 314 bff64eba7721
child 316 d85fc19bf840
Date: Sat, 23 Mar 2002 13:53:37 +0200 From: "Mike Gorchak" <mike@malva.ua> Subject: Big QNX patch again. Added 8bit palette emulation code for window mode with bpp>=15. Added store/restore original palette for 8bit modes. Added more information about photon API call fails. Rewroten change palette code, slow but works. Fixed bug with set caption before window was inited. Fixed bugs with some initial state of variables. Fixed bug with storing old video mode settings. Fixed bug with switching to fullscreen mode and back. Fixed few double SEGFAULTS during parachute mode. Removed compilation warning with no PgWaitHWIdle prototype. Removed pack of dead unusable code. Cleanups SDL_PrivateVideoData structure, some headers. Some code formatting.
src/video/photon/SDL_ph_image.c
src/video/photon/SDL_ph_modes.c
src/video/photon/SDL_ph_modes_c.h
src/video/photon/SDL_ph_mouse.c
src/video/photon/SDL_ph_video.c
src/video/photon/SDL_ph_video.h
src/video/photon/SDL_ph_wm.c
--- a/src/video/photon/SDL_ph_image.c	Fri Mar 22 18:11:56 2002 +0000
+++ b/src/video/photon/SDL_ph_image.c	Sat Mar 23 20:19:44 2002 +0000
@@ -33,9 +33,13 @@
 #include "SDL_endian.h"
 #include "SDL_ph_image_c.h"
 
+/* remove this line, if photon headers updates */
+int PgWaitHWIdle(void);
+
 int ph_SetupImage(_THIS, SDL_Surface *screen)
 {
-    int type = 0;
+    int type=0;
+    PgColor_t* palette=NULL;
 
     /* Determine image type */
     switch(screen->format->BitsPerPixel)
@@ -69,11 +73,28 @@
         break;
     }
 
-    /* using shared memory for speed (set last param to 1) */
-    if ((SDL_Image = PhCreateImage(NULL, screen->w, screen->h, type, NULL, 0, 1)) == NULL)
+    /* palette emulation code */
+    if ((screen->format->BitsPerPixel==8) && (desktoppal==SDLPH_PAL_EMULATE))
     {
-        fprintf(stderr,"error: PhCreateImage failed.\n");
-        return -1;
+        /* creating image palette */
+        palette=malloc(_Pg_MAX_PALETTE*sizeof(PgColor_t));
+        PgGetPalette(palette);
+
+        /* using shared memory for speed (set last param to 1) */
+        if ((SDL_Image = PhCreateImage(NULL, screen->w, screen->h, type, palette, _Pg_MAX_PALETTE, 1)) == NULL)
+        {
+            fprintf(stderr,"ph_SetupImage: PhCreateImage failed for bpp=8.\n");
+            return -1;
+        }
+    }
+    else
+    {
+        /* using shared memory for speed (set last param to 1) */
+        if ((SDL_Image = PhCreateImage(NULL, screen->w, screen->h, type, NULL, 0, 1)) == NULL)
+        {
+            fprintf(stderr,"ph_SetupImage: PhCreateImage failed.\n");
+            return -1;
+        }
     }
 
     screen->pixels = SDL_Image->image;
@@ -83,7 +104,7 @@
     return 0;
 }
 
-int ph_SetupOCImage(_THIS, SDL_Surface *screen) //Offscreen context
+int ph_SetupOCImage(_THIS, SDL_Surface *screen)
 {
 	int type = 0;
 
@@ -185,11 +206,18 @@
 
     if (SDL_Image)
     {
+        /* if palette allocated, free it */
+        if (SDL_Image->palette)
+        {
+            free(SDL_Image->palette);
+        }
         PgShmemDestroy(SDL_Image->image);
         free(SDL_Image);
-        SDL_Image = NULL;
     }
 
+    /* Must be zeroed everytime */
+    SDL_Image = NULL;
+
     if (screen)
     {
         screen->pixels = NULL;
@@ -200,14 +228,14 @@
 {
     ph_DestroyImage(this, screen);
     
-    if(  flags & SDL_HWSURFACE)
+    if (flags & SDL_HWSURFACE)
     {
         OCImage.flags = flags;  /* needed for SDL_DOUBLEBUF check */
         return ph_SetupOCImage(this, screen);
     }
-    else if(flags & SDL_OPENGL)
+    else if (flags & SDL_OPENGL)
     {
-       return ph_SetupOpenGLImage(this, screen);
+        return ph_SetupOpenGLImage(this, screen);
     } 
     else
     {
@@ -217,32 +245,32 @@
 
 int ph_AllocHWSurface(_THIS, SDL_Surface *surface)
 {
-        return(-1);
+    return(-1);
 }
 
 void ph_FreeHWSurface(_THIS, SDL_Surface *surface)
 {
-        return;
+    return;
 }
 
 int ph_FlipHWSurface(_THIS, SDL_Surface *surface)
 {
-        return(0);
+    return(0);
 }
 
 int ph_LockHWSurface(_THIS, SDL_Surface *surface)
 {
-        if ( (surface == SDL_VideoSurface) && blit_queued ) {
-//                XSync(GFX_Display, False);
-				PgFlush();
-                blit_queued = 0;
-        }
-        return(0);
+    if ((surface == SDL_VideoSurface) && blit_queued) {
+	PgFlush();
+        blit_queued = 0;
+    }
+
+    return(0);
 }
 
 void ph_UnlockHWSurface(_THIS, SDL_Surface *surface)
 {
-        return;
+    return;
 }
 
 static PhPoint_t ph_pos;
@@ -258,80 +286,80 @@
 
 void ph_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
 {
-
     for ( i=0; i<numrects; ++i ) 
-	{
-    	if ( rects[i].w == 0 ) { /* Clipped? */
-                continue;
+    {
+    	if (rects[i].w==0) /* Clipped? */
+        { 
+            continue;
         }
 
-		ph_pos.x = rects[i].x;
-		ph_pos.y = rects[i].y;
-		ph_rect.ul.x = rects[i].x;
-		ph_rect.ul.y = rects[i].y;
-		ph_rect.lr.x = rects[i].x + rects[i].w;
-		ph_rect.lr.y = rects[i].y + rects[i].h;
+        ph_pos.x = rects[i].x;
+        ph_pos.y = rects[i].y;
+        ph_rect.ul.x = rects[i].x;
+        ph_rect.ul.y = rects[i].y;
+        ph_rect.lr.x = rects[i].x + rects[i].w;
+        ph_rect.lr.y = rects[i].y + rects[i].h;
 
-		if (PgDrawPhImageRectmx( &ph_pos, SDL_Image, &ph_rect, 0 ) < 0)
-		{
-			fprintf(stderr,"error: PgDrawPhImageRectmx failed.\n");
-		}
-	}
+        if (PgDrawPhImageRectmx(&ph_pos, SDL_Image, &ph_rect, 0) < 0)
+        {
+            fprintf(stderr,"ph_NormalUpdate: PgDrawPhImageRectmx failed.\n");
+        }
+    }
+
     if (PgFlush() < 0)
     {
-    	fprintf(stderr,"error: PgFlush failed.\n");
+    	fprintf(stderr,"ph_NormalUpdate: PgFlush failed.\n");
     }
 }
 void ph_OCUpdate(_THIS, int numrects, SDL_Rect *rects)
 {
-	PhPoint_t zero = {0};
-	PhRect_t src_rect;
-	PhRect_t dest_rect;
+    PhPoint_t zero = {0};
+    PhRect_t src_rect;
+    PhRect_t dest_rect;
+
+    if(OCImage.direct_context == NULL)
+    {
+        return;
+    }
 
-	if(OCImage.direct_context == NULL)
-	 {
-	    return;
-	 }   
-	   		
-	  PgSetRegion(PtWidgetRid(window));
-	  PgSetClipping(0,NULL);
-	  PgWaitHWIdle();
- 		
+    PgSetRegion(PtWidgetRid(window));
+    PgSetClipping(0,NULL);
+    PgWaitHWIdle();
 
-    for ( i=0; i<numrects; ++i ) 
-	{
-    	if ( rects[i].w == 0 ) { /* Clipped? */
-                continue;
+    for (i=0; i<numrects; ++i)
+    {
+        if (rects[i].w == 0)  /* Clipped? */
+        {
+            continue;
         }
 
-			src_rect.ul.x=rects[i].x;
-			src_rect.ul.y=rects[i].y;
-			dest_rect.ul.x=rects[i].x;
-			dest_rect.ul.y=rects[i].y;
-			
-			dest_rect.lr.x=src_rect.lr.x= rects[i].x +rects[i].w;
-			dest_rect.lr.y=src_rect.lr.y= rects[i].y +rects[i].h;
-		
-			zero.x = zero.y = 0;
-	   		PgSetTranslation (&zero, 0);
-	                PgSetRegion(PtWidgetRid(window));
-			PgSetClipping(0,NULL);
- 			PgContextBlitArea(OCImage.offscreen_context, (PhArea_t *)(&src_rect), NULL, (PhArea_t *)(&dest_rect));
- 			
-	}
+        src_rect.ul.x=rects[i].x;
+        src_rect.ul.y=rects[i].y;
+        dest_rect.ul.x=rects[i].x;
+        dest_rect.ul.y=rects[i].y;
+
+        dest_rect.lr.x=src_rect.lr.x= rects[i].x +rects[i].w;
+        dest_rect.lr.y=src_rect.lr.y= rects[i].y +rects[i].h;
+
+        zero.x = zero.y = 0;
+        PgSetTranslation (&zero, 0);
+        PgSetRegion(PtWidgetRid(window));
+        PgSetClipping(0,NULL);
+        PgContextBlitArea(OCImage.offscreen_context, (PhArea_t *)(&src_rect), NULL, (PhArea_t *)(&dest_rect));
+
+    }
     if (PgFlush() < 0)
     {
-    	fprintf(stderr,"error: PgFlush failed.\n");
+        fprintf(stderr,"ph_OCUpdate: PgFlush failed.\n");
     }
     
- //later used to toggling double buffer   
-    if(OCImage.current == 0)
-	{
-		OCImage.CurrentFrameData = OCImage.FrameData0;
-	}
-	else
-	{
-		OCImage.CurrentFrameData = OCImage.FrameData1;
-	}
+    /* later used to toggling double buffer */
+    if (OCImage.current == 0)
+    {
+        OCImage.CurrentFrameData = OCImage.FrameData0;
+    }
+    else
+    {
+        OCImage.CurrentFrameData = OCImage.FrameData1;
+    }
 }
-
--- a/src/video/photon/SDL_ph_modes.c	Fri Mar 22 18:11:56 2002 +0000
+++ b/src/video/photon/SDL_ph_modes.c	Sat Mar 23 20:19:44 2002 +0000
@@ -62,97 +62,6 @@
 		return -1;
 }
 
-/*
-static int compare_modes_by_bpp(const void* mode1, const void* mode2)
-{
-
-    if (PgGetVideoModeInfo(*(unsigned short*)mode1, &mode_info) < 0)
-    {
-        fprintf(stderr,"error: In compare_modes_by_bpp PgGetVideoModeInfo failed on mode: 0x%x\n",
-                *(unsigned short*)mode1);
-        return 0;
-    }
-    key1 = mode_info.bits_per_pixel;
-
-    if (PgGetVideoModeInfo(*(unsigned short*)mode2, &mode_info) < 0)
-    {
-        fprintf(stderr,"error: In compare_modes_by_bpp PgGetVideoModeInfo failed on mode: 0x%x\n",
-                *(unsigned short*)mode2);
-        return 0;
-    }
-    key2 = mode_info.bits_per_pixel;
-
-    if (key1 > key2)
-        return 1;
-    else if (key1 == key2)
-        return 0;
-    else
-        return -1;
-}
-*/
-
-/*
-int ph_GetVideoModes(_THIS)
-{
-	unsigned short *front;
-	int i, bpp_group_size;
-
-	// TODO: add mode_list member to _THIS
-	if (PgGetVideoModeList( &mode_list ) < 0)
-	{
-		fprintf(stderr,"error: PgGetVideoModeList failed\n");
-		return -1;
-	}
-	
-	// sort list first by bits per pixel (bpp), 
-	// then sort groups with same bpp by resolution.
-	qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short), compare_modes_by_bpp);
-	bpp_group_size = 1;
-	front = &mode_list.modes[0];
-	for(i=0;i<mode_list.num_modes-2;i++)
-	{
-		if (compare_modes_by_bpp(&mode_list.modes[i],&mode_list.modes[i+1]))
-		{
-			qsort(front, bpp_group_size, sizeof(unsigned short), compare_modes_by_res);
-			front = &mode_list.modes[i+1];
-			bpp_group_size = 1;
-		}
-		else
-		{
-			bpp_group_size++;
-		}
-	}
-
-	//SDL_modelist = (SDL_Rect **)malloc((mode_list.num_modes+1)*sizeof(SDL_Rect *));
-	if ( SDL_modelist ) {
-		for (i=0;i<mode_list.num_modes;i++) {
-        //	SDL_modelist[i] = (SDL_Rect *)malloc(sizeof(SDL_Rect));
-	     //   if ( SDL_modelist[i] == NULL ) {
-    	  //      break;
-	     //   }
-		    if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
-		    {
-        		fprintf(stderr,"error: PgGetVideoModeInfo failed on mode: 0x%x\n",
-                		mode_list.modes[i]);
-		        return -1;
-    		}
-    	    SDL_modelist[i].x = 0;
-	        SDL_modelist[i].y = 0;
-    	    SDL_modelist[i].w = mode_info.height;
-        	SDL_modelist[i].h = mode_info.width;
-	    }
-    	//SDL_modelist[i] = NULL;
-	}
-	else
-	{
-		fprintf(stderr,"error: malloc failed on SDL_modelist\n");
-		return -1;
-	}
-	
-	return 0;
-}
-*/
-
 SDL_Rect **ph_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
 {
     int i = 0;
@@ -208,9 +117,10 @@
    return;
 }
 
+#if 0
 static void set_best_resolution(_THIS, int width, int height)
 {
-
+    /* warning ! dead variable use_vidmode ! */
     if ( use_vidmode ) {
 		PgDisplaySettings_t 	settings;
 		PgVideoModeInfo_t		current_mode_info;
@@ -281,11 +191,14 @@
 
 int ph_ResizeFullScreen(_THIS)
 {
-    if (currently_fullscreen) {
+    if (currently_fullscreen)
+    {
         set_best_resolution(this, current_w, current_h);
     }
+
     return (1);
 }
+#endif /* 0 */
 
 /* return the mode associated with width, height and bpp */
 /* if there is no mode then zero is returned             */
@@ -354,7 +267,7 @@
 	}
 	if (i<mode_list.num_modes)
 	{
-		// get closest bpp
+		/* get closest bpp */
 		closest = i++;
 		if (mode_info.bits_per_pixel == bpp)
 			return mode_list.modes[ closest ];
@@ -415,40 +328,35 @@
 {
     if (!currently_fullscreen)
     {
-        if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
+        if (this->screen)
         {
+            if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
+            {
 #ifdef HAVE_OPENGL
 #endif /* HAVE_OPENGL */
-           return 0;
+                return 0;
+            }
         }
-        else
+
+        if (OCImage.direct_context == NULL)
         {
-            if (old_video_mode==-1)
-            {
-                PgGetGraphicsHWCaps(&graphics_card_caps);
-                old_video_mode=graphics_card_caps.current_video_mode;
-                old_refresh_rate=graphics_card_caps.current_rrate;
-            }
+            OCImage.direct_context=(PdDirectContext_t*)PdCreateDirectContext();
+        }
 
-            if(OCImage.direct_context == NULL)
-            {
-                OCImage.direct_context=(PdDirectContext_t*)PdCreateDirectContext();
-            }
-            if(!OCImage.direct_context)
-            {
-                fprintf(stderr, "error: Can't create direct context\n" );
-            }
+        if (!OCImage.direct_context)
+        {
+            fprintf(stderr, "ph_EnterFullScreen: Can't create direct context\n" );
+        }
 
-            PdDirectStart(OCImage.direct_context);
+        PdDirectStart(OCImage.direct_context);
 
-            currently_fullscreen = 1;
-        }
+        currently_fullscreen = 1;
     }
 
     return 1;
 }
 
-int ph_LeaveFullScreen(_THIS )
+int ph_LeaveFullScreen(_THIS)
 {
     PgDisplaySettings_t mymode_settings;
        
@@ -464,13 +372,16 @@
         {
             PdDirectStop(OCImage.direct_context);
             PdReleaseDirectContext(OCImage.direct_context);
+            
+            currently_fullscreen=0;
 
             /* Restore old video mode */
             if (old_video_mode != -1)
             {
                 mymode_settings.mode= (unsigned short) old_video_mode;
                 mymode_settings.refresh= (unsigned short) old_refresh_rate;
-                mymode_settings.flags = 0;
+                mymode_settings.flags= 0;
+                
                 if (PgSetVideoMode(&mymode_settings) < 0)
                 {
                     fprintf(stderr,"error: PgSetVideoMode failed\n");
@@ -478,7 +389,7 @@
             }
 
             old_video_mode=-1;
-            old_refresh_rate=-1;	
+            old_refresh_rate=-1;
         }
 
     }
--- a/src/video/photon/SDL_ph_modes_c.h	Fri Mar 22 18:11:56 2002 +0000
+++ b/src/video/photon/SDL_ph_modes_c.h	Sat Mar 23 20:19:44 2002 +0000
@@ -33,7 +33,6 @@
 
 #define PH_MAX_VIDEOMODES 127
 
-//extern int ph_GetVideoModes(_THIS);
 extern SDL_Rect **ph_ListModes(_THIS,SDL_PixelFormat *format, Uint32 flags);
 extern void ph_FreeVideoModes(_THIS);
 extern int ph_ResizeFullScreen(_THIS);
--- a/src/video/photon/SDL_ph_mouse.c	Fri Mar 22 18:11:56 2002 +0000
+++ b/src/video/photon/SDL_ph_mouse.c	Sat Mar 23 20:19:44 2002 +0000
@@ -36,25 +36,25 @@
 #include "SDL_ph_mouse_c.h"
 
 struct  WMcursor {
-	PhCursorDef_t *ph_cursor ;
+    PhCursorDef_t *ph_cursor ;
 };
 
 
 void ph_FreeWMCursor(_THIS, WMcursor *cursor)
 {
+    if (window != NULL)
+    {
+        SDL_Lock_EventThread();
 
-    if ( window != NULL ) {
-		SDL_Lock_EventThread();
-		
-		if (PtSetResource( window, Pt_ARG_CURSOR_TYPE, Ph_CURSOR_INHERIT, 0 ) < 0)
-		{
-			//TODO: output error msg
-		}
-		
-		SDL_Unlock_EventThread();
-	}	
-	//free(cursor->ph_cursor.images);
-	free(cursor);
+        if (PtSetResource( window, Pt_ARG_CURSOR_TYPE, Ph_CURSOR_INHERIT, 0 ) < 0)
+        {
+            /* TODO: output error msg */
+        }
+
+        SDL_Unlock_EventThread();
+    }	
+    /* free(cursor->ph_cursor.images); */
+    free(cursor);
 }
 
 WMcursor *ph_CreateWMCursor(_THIS,
@@ -120,7 +120,7 @@
 
 PhCursorDef_t ph_GetWMPhCursor(WMcursor *cursor)
 {
-	return(*cursor->ph_cursor);
+    return(*cursor->ph_cursor);
 }
 
 int ph_ShowWMCursor(_THIS, WMcursor *cursor)
@@ -170,22 +170,24 @@
 
 void ph_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
 {
-	short abs_x, abs_y;
+    short abs_x, abs_y;
 
-	SDL_Lock_EventThread();
-	PtGetAbsPosition( window, &abs_x, &abs_y );
-	PhMoveCursorAbs( PhInputGroup(NULL), x + abs_x, y + abs_y );
-	SDL_Unlock_EventThread();
+    SDL_Lock_EventThread();
+    PtGetAbsPosition( window, &abs_x, &abs_y );
+    PhMoveCursorAbs( PhInputGroup(NULL), x + abs_x, y + abs_y );
+    SDL_Unlock_EventThread();
 }
 
 
 void ph_CheckMouseMode(_THIS)
 {
-        /* If the mouse is hidden and input is grabbed, we use relative mode */
-        if ( !(SDL_cursorstate & CURSOR_VISIBLE) &&
-             (this->input_grab != SDL_GRAB_OFF) ) {
-                mouse_relative = 1;
-        } else {
-                mouse_relative = 0;
-        }
+    /* If the mouse is hidden and input is grabbed, we use relative mode */
+    if ( !(SDL_cursorstate & CURSOR_VISIBLE) && (this->input_grab != SDL_GRAB_OFF))
+    {
+        mouse_relative = 1;
+    }
+    else
+    {
+        mouse_relative = 0;
+    }
 }
--- a/src/video/photon/SDL_ph_video.c	Fri Mar 22 18:11:56 2002 +0000
+++ b/src/video/photon/SDL_ph_video.c	Sat Mar 23 20:19:44 2002 +0000
@@ -150,8 +150,8 @@
 }
 
 VideoBootStrap ph_bootstrap = {
-        "photon", "QNX Photon video output",
-	ph_Available, ph_CreateDevice
+    "photon", "QNX Photon video output",
+    ph_Available, ph_CreateDevice
 };
 
 static void ph_DeleteDevice(SDL_VideoDevice *device)
@@ -175,7 +175,6 @@
 
 static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat)
 {
-    PgColor_t ph_palette[_Pg_MAX_PALETTE];
     int i;
     unsigned long *tempptr;
     int rtnval;
@@ -184,6 +183,11 @@
 
     window=NULL;
     oglctx=NULL;
+    desktoppal=SDLPH_PAL_NONE;
+    
+    captionflag=0;
+    old_video_mode=-1;
+    old_refresh_rate=-1;
 	
     if (NULL == (event = malloc(EVENT_SIZE)))
     {
@@ -213,6 +217,7 @@
     /* We need to return BytesPerPixel as it in used by CreateRGBsurface */
     vformat->BitsPerPixel = my_mode_info.bits_per_pixel;
     vformat->BytesPerPixel = my_mode_info.bytes_per_scanline/my_mode_info.width;
+    desktopbpp = my_mode_info.bits_per_pixel;
          
     /* return a palette if we are in 256 color mode */
     if (vformat->BitsPerPixel == 8)
@@ -253,7 +258,6 @@
     PtArg_t arg[32];
     PhDim_t dim;
     int rtnval;
-    PgColor_t ph_palette[_Pg_MAX_PALETTE];
     int i;
     unsigned long *tempptr;
     int pargc;
@@ -281,7 +285,7 @@
 
         window=PtCreateWidget(PtWindow, NULL, pargc-1, arg);
         PtRealizeWidget(window);
-
+        
         PtFlush();
     }
 
@@ -330,21 +334,30 @@
                     exit(1);
                 }
             }
+            
+            if (bpp==8)
+            {
+               desktoppal=SDLPH_PAL_SYSTEM;
+            }
+            
+            /* save old video mode caps */
+            PgGetVideoMode(&settings);
+            old_video_mode=settings.mode;
+            old_refresh_rate=settings.refresh;
+
+            /* setup new video mode */
             settings.mode = mode;
             settings.refresh = 0;
-            settings.flags  = 0;       
+            settings.flags = 0;
 
-            if (PgSetVideoMode( &settings ) < 0)
+            if (PgSetVideoMode(&settings) < 0)
             {
                 fprintf(stderr,"error: PgSetVideoMode failed\n");
             }
 
-            /* Get the true height and width */
-
             current->flags = (flags & (~SDL_RESIZABLE)); /* no resize for Direct Context */
 
             /* Begin direct mode */
-
             ph_EnterFullScreen(this);
 
         } /* end fullscreen flag */
@@ -359,6 +372,22 @@
             {
                 current->flags = (flags | SDL_RESIZABLE); /* yes we can resize as this is a software surface */
             }
+            /* using palette emulation code in window mode */
+            if (bpp==8)
+            {
+                if (desktopbpp>=15)
+                {
+                    desktoppal=SDLPH_PAL_EMULATE;
+                }
+                else
+                {
+                    desktoppal=SDLPH_PAL_SYSTEM;
+                }
+            }
+            else
+            {
+               desktoppal=SDLPH_PAL_NONE;
+            }
         }
 
 	/* If we are setting video to use the palette make sure we have allocated memory for it */
@@ -390,10 +419,16 @@
     /* Must call at least once it setup image planes */
     ph_ResizeImage(this, current, flags);
 
+    /* delayed set caption call */
+    if (captionflag)
+    {
+        ph_SetCaption(this, this->wm_title, NULL);
+    }
+
     SDL_Unlock_EventThread();
 
     /* We're done! */
-    return(current);
+    return (current);
 }
 
 static void ph_VideoQuit(_THIS)
@@ -404,19 +439,20 @@
 
     if (currently_fullscreen)
     {
-        PdDirectStop(directContext);
-        PdReleaseDirectContext(directContext);
-        directContext=0;	
-        currently_fullscreen=0;
+        ph_LeaveFullScreen(this);
     }
 
 #ifdef HAVE_OPENGL
-    if (((this->screen->flags & SDL_FULLSCREEN)==SDL_FULLSCREEN) &&
-        ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
+    /* prevent double SEGFAULT with parachute mode */
+    if (this->screen)
     {
-        region_info.cursor_type=Ph_CURSOR_POINTER;
-        region_info.rid=PtWidgetRid(window);
-        PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
+        if (((this->screen->flags & SDL_FULLSCREEN)==SDL_FULLSCREEN) &&
+            ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
+        {
+            region_info.cursor_type=Ph_CURSOR_POINTER;
+            region_info.rid=PtWidgetRid(window);
+            PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
+        }
     }
 
     PtFlush();
@@ -428,6 +464,12 @@
         PtDestroyWidget(window);
         window=NULL;
     }
+    
+    /* restore palette */
+    if (desktoppal!=SDLPH_PAL_NONE)
+    {
+        PgSetPalette(ph_palette, 0, 0, _Pg_MAX_PALETTE, Pg_PALSET_GLOBAL | Pg_PALSET_FORCE_EXPOSE, 0);
+    }
 
 #ifdef HAVE_OPENGL
     if (oglctx)
@@ -441,60 +483,58 @@
 
 static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
 {
-	PgColor_t *in, *out;
-	int i, j;
-	int alloct_all = 1;
-
-    	colors = this->screen->format->palette->colors;
-
-	in = alloca( ncolors*sizeof(PgColor_t)  );
-	if ( in == NULL  ) {
-		return 0;
-	}
-	memset(in,0,ncolors*sizeof(PgColor_t));
-
-    out = alloca( ncolors*sizeof(PgColor_t)  );
-    if ( out == NULL  ) {
-		return 0;
-    }
-	
-	for (i=0,j=firstcolor;i<ncolors;i++,j++)
-	{
-		in[i] |= colors[j].r<<16 ;
-		in[i] |= colors[j].g<<8 ;
-		in[i] |= colors[j].b ; 
-	}
+    int i;
+    PhPoint_t point={0, 0};
+    PgColor_t syspalph[_Pg_MAX_PALETTE];
 
-	if ( (this->screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) 
-	{
-		if( PgSetPalette( in, 0, 0, ncolors, Pg_PALSET_HARD, 0) < 0 )
-		{
-			fprintf(stderr,"error: PgSetPalette(..,Pg_PALSET_HARD)  failed\n");
-			return 0;
-		}  
-	}
-	else 
-	{
-		if ( PgColorMatch(ncolors, in, out) < 0 )
+    /* palette emulation code, using palette of the PhImage_t struct */
+    if (desktoppal==SDLPH_PAL_EMULATE)
+    {
+        if ((SDL_Image) && (SDL_Image->palette))
         {
-            fprintf(stderr,"error: PgColorMatch failed\n");
-            return 0;
+            for (i=firstcolor; i<firstcolor+ncolors; i++)
+            {
+                SDL_Image->palette[i]  = 0x00000000UL;
+                SDL_Image->palette[i] |= colors[i-firstcolor].r<<16;
+                SDL_Image->palette[i] |= colors[i-firstcolor].g<<8;
+                SDL_Image->palette[i] |= colors[i-firstcolor].b;
+            }
         }
-		for (i=0;i<ncolors;i++)
-		{
-			if (memcmp(&in[i],&out[i],sizeof(PgColor_t)))
-			{
-				alloct_all = 0;
-				break;
-			}
-		}
-        if( PgSetPalette( out, 0, 0, ncolors, Pg_PALSET_SOFT, 0) < 0 )
+        /* image needs to be redrawed, very slow method */
+        PgDrawPhImage(&point, SDL_Image, 0);
+    }
+    else
+    {
+        if (desktoppal==SDLPH_PAL_SYSTEM)
         {
-            fprintf(stderr,"error: PgSetPalette(..,Pg_PALSET_SOFT) failed\n");
-            return 0;
+            for (i=firstcolor; i<firstcolor+ncolors; i++)
+            {
+                syspalph[i]  = 0x00000000UL;
+                syspalph[i] |= colors[i-firstcolor].r<<16;
+                syspalph[i] |= colors[i-firstcolor].g<<8;
+                syspalph[i] |= colors[i-firstcolor].b;
+            }
+
+            if ((this->screen->flags & SDL_FULLSCREEN) != SDL_FULLSCREEN)
+            {
+                /* window mode must use soft palette */
+                PgSetPalette((PgColor_t*)&syspalph, 0, firstcolor, ncolors, Pg_PALSET_SOFT, 0);
+                /* image needs to be redrawed, very slow method */
+                PgDrawPhImage(&point, SDL_Image, 0);
+            }
+            else
+            {
+                /* fullscreen mode must use hardware palette */
+                PgSetPalette((PgColor_t*)&syspalph, 0, firstcolor, ncolors, Pg_PALSET_GLOBAL, 0);
+            }
         }
-	}
-	return alloct_all;
+        else
+        {
+            /* SDLPH_PAL_NONE do nothing */
+        }
+    }
+    
+    return 1;
 }
 
 #ifdef HAVE_OPENGL
--- a/src/video/photon/SDL_ph_video.h	Fri Mar 22 18:11:56 2002 +0000
+++ b/src/video/photon/SDL_ph_video.h	Sat Mar 23 20:19:44 2002 +0000
@@ -39,6 +39,10 @@
 
 #define PH_OGL_MAX_ATTRIBS 32
 
+#define SDLPH_PAL_NONE    0x00000000L
+#define SDLPH_PAL_EMULATE 0x00000001L
+#define SDLPH_PAL_SYSTEM  0x00000002L
+
 typedef union vidptr{
   uint8_t  *volatile ptr8;
   uint16_t *volatile ptr16;
@@ -55,14 +59,13 @@
 
 /* Private display data */
 struct SDL_PrivateVideoData {
-    int local_ph;		/* Flag: true if local display */
-    PtAppContext_t app;
     PgDisplaySettings_t mode_settings;	
-    PtWidget_t *Window;    /* used to handle input events */
-    PhImage_t *image;	   /* used to display image */
+    PtWidget_t *Window;                  /* used to handle input events */
+    PhImage_t *image;	                 /* used to display image       */
 #ifdef HAVE_OPENGL
-    PdOpenGLContext_t* OGLContext;
+    PdOpenGLContext_t* OGLContext;       /* OpenGL context              */
 #endif /* HAVE_OPENGL */
+    PgColor_t ph_palette[_Pg_MAX_PALETTE];
 
     struct {
         PdDirectContext_t *direct_context;
@@ -76,16 +79,9 @@
         long flags;
     } ocimage;
 
-    PhDrawContext_t *ScreenDC; //=NULL;
-    signed short old_video_mode; //=-1;
-    signed short old_refresh_rate; //=-1;
-    PgHWCaps_t graphics_card_caps;
-	
-    PdDirectContext_t *directContext;
-    PdOffscreenContext_t *Buff[2];
-    struct _Ph_ctrl* ctrl_channel;
-
-    /* The variables used for displaying graphics */
+    PgHWCaps_t graphics_card_caps;  /* Graphics card caps at the moment of start   */
+    int old_video_mode;             /* Stored mode before fullscreen switch        */
+    int old_refresh_rate;           /* Stored refresh rate befor fullscreen switch */
 
     /* The current width and height of the fullscreen mode */
     int current_w;
@@ -106,7 +102,10 @@
     int mouse_relative;
     WMcursor* BlankCursor;
 
-    int depth;			/* current visual depth (not bpp) */
+    int depth;			/* current visual depth (not bpp)        */
+    int desktopbpp;             /* bpp of desktop at the moment of start */
+    int desktoppal;             /* palette mode emulation or system      */
+    int captionflag;            /* caption setting flag                  */
 
     int use_vidmode;
     int currently_fullscreen;
@@ -118,26 +117,22 @@
     /* Prevent too many XSync() calls */
     int blit_queued;
 
-    short *iconcolors;		/* List of colors used by the icon */
     PhEvent_t* event;
 };
 
-#define local_ph             (this->hidden->local_ph)
-#define app                  (this->hidden->app)
 #define mode_settings        (this->hidden->mode_settings)
 #define window	             (this->hidden->Window)
 #define oglctx               (this->hidden->OGLContext)
-#define directContext	     (this->hidden->directContext)
-#define Buff                 (this->hidden->Buff)
-#define ctrl_channel         (this->hidden->ctrl_channel)
 #define SDL_Image            (this->hidden->image)
 #define OCImage              (this->hidden->ocimage)
 #define old_video_mode       (this->hidden->old_video_mode)
 #define old_refresh_rate     (this->hidden->old_refresh_rate)
 #define graphics_card_caps   (this->hidden->graphics_card_caps)
+#define desktopbpp           (this->hidden->desktopbpp)
+#define desktoppal           (this->hidden->desktoppal)
+#define ph_palette           (this->hidden->ph_palette)
 
 /* Old variable names */
-#define swap_pixels          (this->hidden->swap_pixels)
 #define current_w            (this->hidden->current_w)
 #define current_h            (this->hidden->current_h)
 #define mouse_last           (this->hidden->mouse_last)
@@ -150,8 +145,8 @@
 #define switch_waiting       (this->hidden->switch_waiting)
 #define switch_time          (this->hidden->switch_time)
 #define blit_queued          (this->hidden->blit_queued)
-#define SDL_iconcolorIs      (this->hidden->iconcolors)
 #define event                (this->hidden->event)
 #define SDL_BlankCursor      (this->hidden->BlankCursor)
+#define captionflag          (this->hidden->captionflag)
 
 #endif /* _SDL_x11video_h */
--- a/src/video/photon/SDL_ph_wm.c	Fri Mar 22 18:11:56 2002 +0000
+++ b/src/video/photon/SDL_ph_wm.c	Sat Mar 23 20:19:44 2002 +0000
@@ -48,207 +48,47 @@
 
 void ph_SetIcon(_THIS, SDL_Surface *icon, Uint8 *mask)
 {
-
-#if 0 /*big*/
-	int ncolors;
-	PhImage_t *image;
-	PgColor_t* palette;
-
-	image = PhCreateImage( image,
-                          	icon->w,
-                          	icon->h,
-                          	Pg_IMAGE_DIRECT_888,
-                          	NULL, 0, 0 );
-
-/* ---------------------------------------- */
-	SDL_Surface *sicon;
-//	XWMHints *wmhints;
-//	XImage *icon_image;
-//	Pixmap icon_pixmap;
-//	Pixmap mask_pixmap;
-//	GC GC;
-//	XGCValues GCvalues;
-	int i, b, dbpp;
-	SDL_Rect bounds;
-	Uint8 *LSBmask, *color_tried;
-	Visual *dvis;
-
-	/* Lock the event thread, in multi-threading environments */
-	SDL_Lock_EventThread();
-
-	/* The icon must use the default visual, depth and colormap of the
-	   screen, so it might need a conversion */
-// ?	dbpp = DefaultDepth(SDL_Display, SDL_Screen);
-	switch(dbpp) {
-	case 15:
-	    dbpp = 16; break;
-	case 24:
-	    dbpp = 32; break;
-	}
-	dvis = DefaultVisual(SDL_Display, SDL_Screen);
-
-	/* The Visual struct is supposed to be opaque but we cheat a little */
-	sicon = SDL_CreateRGBSurface(SDL_SWSURFACE, icon->w, icon->h,
-				     dbpp,
-				     dvis->red_mask, dvis->green_mask,
-				     dvis->blue_mask, 0);
-
-	if ( sicon == NULL ) {
-		goto done;
-	}
-	/* If we already have allocated colours from the default colormap,
-	   copy them */
-	if(SDL_Visual == dvis && SDL_XColorMap == SDL_DisplayColormap
-	   && this->screen->format->palette && sicon->format->palette) {
-	    memcpy(sicon->format->palette->colors,
-		   this->screen->format->palette->colors,
-		   this->screen->format->palette->ncolors * sizeof(SDL_Color));
-	}
-
-	bounds.x = 0;
-	bounds.y = 0;
-	bounds.w = icon->w;
-	bounds.h = icon->h;
-	if ( SDL_LowerBlit(icon, &bounds, sicon, &bounds) < 0 )
-		goto done;
-
-	/* Lock down the colors used in the colormap */
-	color_tried = NULL;
-	if ( sicon->format->BitsPerPixel == 8 ) {
-		SDL_Palette *palette;
-		Uint8 *p;
-		XColor wanted;
-
-		palette = sicon->format->palette;
-		color_tried = malloc(palette->ncolors);
-		if ( color_tried == NULL ) {
-			goto done;
-		}
-		if ( SDL_iconcolors != NULL ) {
-			free(SDL_iconcolors);
-		}
-		SDL_iconcolors = malloc(palette->ncolors
-					* sizeof(*SDL_iconcolors));
-		if ( SDL_iconcolors == NULL ) {
-			free(color_tried);
-			goto done;
-		}
-		memset(color_tried, 0, palette->ncolors);
-		memset(SDL_iconcolors, 0,
-		       palette->ncolors * sizeof(*SDL_iconcolors));
-
-		p = (Uint8 *)sicon->pixels; 
-		for ( i = sicon->w*sicon->h; i > 0; --i, ++p ) {
-			if ( ! color_tried[*p] ) {
-				wanted.pixel = *p;
-				wanted.red   = (palette->colors[*p].r<<8);
-				wanted.green = (palette->colors[*p].g<<8);
-				wanted.blue  = (palette->colors[*p].b<<8);
-				wanted.flags = (DoRed|DoGreen|DoBlue);
-				if (XAllocColor(SDL_Display,
-						SDL_DisplayColormap, &wanted)) {
-					++SDL_iconcolors[wanted.pixel];
-				}
-				color_tried[*p] = 1;
-			}
-		}
-	}
-	if ( color_tried != NULL ) {
-		free(color_tried);
-	}
-
-	/* Translate mask data to LSB order and set the icon mask */
-	i = (sicon->w/8)*sicon->h;
-	LSBmask = (Uint8 *)malloc(i);
-	if ( LSBmask == NULL ) {
-		goto done;
-	}
-	memset(LSBmask, 0, i);
-	while ( --i >= 0 ) {
-		for ( b=0; b<8; ++b )
-			LSBmask[i] |= (((mask[i]>>b)&0x01)<<(7-b));
-	}
-	mask_pixmap = XCreatePixmapFromBitmapData(SDL_Display, WMwindow,
-					LSBmask, sicon->w, sicon->h, 1L, 0L, 1);
-
-	/* Transfer the image to an X11 pixmap */
-	icon_image = XCreateImage(SDL_Display,
-			DefaultVisual(SDL_Display, SDL_Screen),
-			DefaultDepth(SDL_Display, SDL_Screen),
-			ZPixmap, 0, (char *)sicon->pixels, sicon->w, sicon->h,
-			((sicon->format)->BytesPerPixel == 3) ? 32 :
-				(sicon->format)->BytesPerPixel*8, 0);
-	icon_pixmap = XCreatePixmap(SDL_Display, SDL_Root, sicon->w, sicon->h,
-			DefaultDepth(SDL_Display, SDL_Screen));
-	GC = XCreateGC(SDL_Display, icon_pixmap, 0, &GCvalues);
-	XPutImage(SDL_Display, icon_pixmap, GC, icon_image,
-					0, 0, 0, 0, sicon->w, sicon->h);
-	XFreeGC(SDL_Display, GC);
-	XDestroyImage(icon_image);
-	free(LSBmask);
-	sicon->pixels = NULL;
-
-#ifdef USE_ICON_WINDOW
-	/* Create an icon window and set the pixmap as its background */
-	icon_window = XCreateSimpleWindow(SDL_Display, SDL_Root,
-					0, 0, sicon->w, sicon->h, 0,
-					CopyFromParent, CopyFromParent);
-	XSetWindowBackgroundPixmap(SDL_Display, icon_window, icon_pixmap);
-	XClearWindow(SDL_Display, icon_window);
-#endif
-
-	/* Set the window icon to the icon pixmap (and icon window) */
-	wmhints = XAllocWMHints();
-	wmhints->flags = (IconPixmapHint | IconMaskHint);
-	wmhints->icon_pixmap = icon_pixmap;
-	wmhints->icon_mask = mask_pixmap;
-#ifdef USE_ICON_WINDOW
-	wmhints->flags |= IconWindowHint;
-	wmhints->icon_window = icon_window;
-#endif
-	XSetWMHints(SDL_Display, WMwindow, wmhints);
-	XFree(wmhints);
-	XSync(SDL_Display, False);
-
-  done:
-	SDL_Unlock_EventThread();
-	if ( sicon != NULL ) {
-		SDL_FreeSurface(sicon);
-	}
-	
-#endif /*big*/
-	return;
+    return;
 }
 
 /* Set window caption */
 void ph_SetCaption(_THIS, const char *title, const char *icon)
 {
-	SDL_Lock_EventThread();
-	if ( title != NULL ) {
-		PtSetResource(window, Pt_ARG_WINDOW_TITLE, title, 0);
-	}
-	SDL_Unlock_EventThread();
+    SDL_Lock_EventThread();
+
+    /* check for set caption call before window init */
+    if (window!=NULL)
+    {
+        PtSetResource(window, Pt_ARG_WINDOW_TITLE, title, 0);
+    }
+    else
+    {
+        captionflag=1;
+    }
+
+    SDL_Unlock_EventThread();
 }
 
 /* Iconify current window */
 int ph_IconifyWindow(_THIS)
 {
-	PhWindowEvent_t windowevent;
+    PhWindowEvent_t windowevent;
+
+    SDL_Lock_EventThread();
 
-	SDL_Lock_EventThread();
-	memset( &windowevent, 0, sizeof (event) );
-	windowevent.event_f = Ph_WM_HIDE;
-	windowevent.event_state = Ph_WM_EVSTATE_HIDE;
-	windowevent.rid = PtWidgetRid( window );
-	PtForwardWindowEvent( &windowevent );
-	SDL_Unlock_EventThread();
-        
-        return 0;
+    memset( &windowevent, 0, sizeof (event) );
+    windowevent.event_f = Ph_WM_HIDE;
+    windowevent.event_state = Ph_WM_EVSTATE_HIDE;
+    windowevent.rid = PtWidgetRid( window );
+    PtForwardWindowEvent( &windowevent );
+    SDL_Unlock_EventThread();
+
+    return 0;
 }
 
 SDL_GrabMode ph_GrabInputNoLock(_THIS, SDL_GrabMode mode)
 {
-   return(mode);
+    return(mode);
 }
 
 SDL_GrabMode ph_GrabInput(_THIS, SDL_GrabMode mode)
@@ -280,14 +120,14 @@
 
 int ph_GetWMInfo(_THIS, SDL_SysWMinfo *info)
 {
-   if (info->version.major <= SDL_MAJOR_VERSION)
-   {
-      return 1;
-   }
-   else
-   {
-      SDL_SetError("Application not compiled with SDL %d.%d\n",
-                    SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
-      return -1;
-   }
+    if (info->version.major <= SDL_MAJOR_VERSION)
+    {
+        return 1;
+    }
+    else
+    {
+        SDL_SetError("Application not compiled with SDL %d.%d\n",
+                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
+        return -1;
+    }
 }