Home | History | Annotate | Download | only in cybergfx
      1 /*
      2     SDL - Simple DirectMedia Layer
      3     Copyright (C) 1997-2006 Sam Lantinga
      4 
      5     This library is free software; you can redistribute it and/or
      6     modify it under the terms of the GNU Lesser General Public
      7     License as published by the Free Software Foundation; either
      8     version 2.1 of the License, or (at your option) any later version.
      9 
     10     This library is distributed in the hope that it will be useful,
     11     but WITHOUT ANY WARRANTY; without even the implied warranty of
     12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13     Lesser General Public License for more details.
     14 
     15     You should have received a copy of the GNU Lesser General Public
     16     License along with this library; if not, write to the Free Software
     17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     18 
     19     Sam Lantinga
     20     slouken (at) libsdl.org
     21 */
     22 #include "SDL_config.h"
     23 
     24 /*
     25  * CGX based SDL video driver implementation by Gabriele Greco
     26  * gabriele.greco (at) aruba.it
     27  */
     28 
     29 #include "SDL_endian.h"
     30 #include "SDL_timer.h"
     31 #include "SDL_thread.h"
     32 #include "SDL_video.h"
     33 #include "SDL_mouse.h"
     34 #include "../SDL_sysvideo.h"
     35 #include "../SDL_pixels_c.h"
     36 #include "../../events/SDL_events_c.h"
     37 #include "SDL_cgxgl_c.h"
     38 #include "SDL_cgxvideo.h"
     39 #include "SDL_cgxwm_c.h"
     40 #include "SDL_amigamouse_c.h"
     41 #include "SDL_amigaevents_c.h"
     42 #include "SDL_cgxmodes_c.h"
     43 #include "SDL_cgximage_c.h"
     44 
     45 /* Initialization/Query functions */
     46 static int CGX_VideoInit(_THIS, SDL_PixelFormat *vformat);
     47 static SDL_Surface *CGX_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
     48 static int CGX_ToggleFullScreen(_THIS, int on);
     49 static void CGX_UpdateMouse(_THIS);
     50 static int CGX_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
     51 static void CGX_VideoQuit(_THIS);
     52 
     53 /* CGX driver bootstrap functions */
     54 
     55 struct Library *CyberGfxBase=NULL;
     56 struct IntuitionBase *IntuitionBase=NULL;
     57 struct GfxBase *GfxBase=NULL;
     58 
     59 int CGX_SetGamma(_THIS, float red, float green, float blue)
     60 {
     61     SDL_SetError("Gamma correction not supported");
     62     return -1;
     63 }
     64 
     65 int CGX_GetGamma(_THIS, float red, float green, float blue)
     66 {
     67     SDL_SetError("Gamma correction not supported");
     68     return -1;
     69 }
     70 
     71 int CGX_SetGammaRamp(_THIS, Uint16 *ramp)
     72 {
     73 #if 0
     74 	Int i, ncolors;
     75 	XColor xcmap[256];
     76 
     77 	/* See if actually setting the gamma is supported */
     78 	if ( SDL_Visual->class != DirectColor ) {
     79 	    SDL_SetError("Gamma correction not supported on this visual");
     80 	    return(-1);
     81 	}
     82 
     83 	/* Calculate the appropriate palette for the given gamma ramp */
     84 	ncolors = SDL_Visual->map_entries;
     85 	for ( i=0; i<ncolors; ++i ) {
     86 		Uint8 c = (256 * i / ncolors);
     87 		xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c);
     88 		xcmap[i].red   = ramp[0*256+c];
     89 		xcmap[i].green = ramp[1*256+c];
     90 		xcmap[i].blue  = ramp[2*256+c];
     91 		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
     92 	}
     93 	XStoreColors(GFX_Display, SDL_XColorMap, xcmap, ncolors);
     94 	XSync(GFX_Display, False);
     95 
     96 	return(0);
     97 
     98 #else
     99     SDL_SetError("Gamma correction not supported on this visual");
    100     return(-1);
    101 
    102 #endif
    103 }
    104 
    105 static void DestroyScreen(_THIS)
    106 {
    107   	if(currently_fullscreen)
    108 	{
    109 		if(this->hidden->dbuffer)
    110 		{
    111 			extern struct MsgPort *safeport,*dispport;
    112 
    113 			this->hidden->dbuffer=0;
    114 
    115 			if(safeport)
    116 			{
    117 				while(GetMsg(safeport)!=NULL);
    118 				DeleteMsgPort(safeport);
    119 			}
    120 			if(dispport)
    121 			{
    122 				while(GetMsg(dispport)!=NULL);
    123 				DeleteMsgPort(dispport);
    124 			}
    125 
    126 			this->hidden->SB[0]->sb_DBufInfo->dbi_SafeMessage.mn_ReplyPort=this->hidden->SB[0]->sb_DBufInfo->dbi_DispMessage.mn_ReplyPort=NULL;
    127 			this->hidden->SB[1]->sb_DBufInfo->dbi_SafeMessage.mn_ReplyPort=this->hidden->SB[1]->sb_DBufInfo->dbi_DispMessage.mn_ReplyPort=NULL;
    128 
    129 			if(this->hidden->SB[1])
    130 				FreeScreenBuffer(SDL_Display,this->hidden->SB[1]);
    131 			if(this->hidden->SB[0])
    132 				FreeScreenBuffer(SDL_Display,this->hidden->SB[0]);
    133 
    134 
    135 			this->hidden->SB[0]=this->hidden->SB[1]=NULL;
    136 
    137 			if(SDL_RastPort && SDL_RastPort != &SDL_Display->RastPort)
    138 				SDL_free(SDL_RastPort);
    139 
    140 			SDL_RastPort=NULL;
    141 		}
    142 		CloseScreen(GFX_Display);
    143 		currently_fullscreen=0;
    144 	}
    145 	else if(GFX_Display)
    146 		UnlockPubScreen(NULL,GFX_Display);
    147 
    148 	GFX_Display = NULL;
    149 }
    150 
    151 static int CGX_Available(void)
    152 {
    153 	struct Library *l;
    154 
    155 	l = OpenLibrary("cybergraphics.library",0L);
    156 
    157 	if ( l != NULL ) {
    158 		D(bug("CGX video device AVAILABLE\n"));
    159 		CloseLibrary(l);
    160 	}
    161 	D(else bug("**CGX video device UNAVAILABLE\n"));
    162 
    163 	return(l != NULL);
    164 }
    165 
    166 static void CGX_DeleteDevice(SDL_VideoDevice *device)
    167 {
    168 	if ( device ) {
    169 		if ( device->hidden ) {
    170 			SDL_free(device->hidden);
    171 		}
    172 		if ( device->gl_data ) {
    173 			SDL_free(device->gl_data);
    174 		}
    175 		SDL_free(device);
    176 	}
    177 }
    178 
    179 static SDL_VideoDevice *CGX_CreateDevice(int devindex)
    180 {
    181 	SDL_VideoDevice *device;
    182 
    183 	/* Initialize all variables that we clean on shutdown */
    184 	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
    185 	if ( device ) {
    186 		SDL_memset(device, 0, (sizeof *device));
    187 		device->hidden = (struct SDL_PrivateVideoData *)
    188 				SDL_malloc((sizeof *device->hidden));
    189 		device->gl_data = (struct SDL_PrivateGLData *)
    190 				SDL_malloc((sizeof *device->gl_data));
    191 	}
    192 	if ( (device == NULL) || (device->hidden == NULL) ||
    193 	                         (device->gl_data == NULL) ) {
    194 		D(bug("Unable to create video device!\n"));
    195 		SDL_OutOfMemory();
    196 		CGX_DeleteDevice(device);
    197 		return(0);
    198 	}
    199 	SDL_memset(device->hidden, 0, sizeof(*device->hidden));
    200 	SDL_memset(device->gl_data, 0, sizeof(*device->gl_data));
    201 
    202 	/* Set the driver flags */
    203 	device->handles_any_size = 1;
    204 
    205 	/* Set the function pointers */
    206 	device->VideoInit = CGX_VideoInit;
    207 	device->ListModes = CGX_ListModes;
    208 	device->SetVideoMode = CGX_SetVideoMode;
    209 	device->ToggleFullScreen = CGX_ToggleFullScreen;
    210 	device->UpdateMouse = CGX_UpdateMouse;
    211 	device->SetColors = CGX_SetColors;
    212 	device->UpdateRects = NULL;
    213 	device->VideoQuit = CGX_VideoQuit;
    214 	device->AllocHWSurface = CGX_AllocHWSurface;
    215 	device->CheckHWBlit = CGX_CheckHWBlit;
    216 	device->FillHWRect = CGX_FillHWRect;
    217 	device->SetHWColorKey = CGX_SetHWColorKey;
    218 	device->SetHWAlpha = NULL;
    219 	device->LockHWSurface = CGX_LockHWSurface;
    220 	device->UnlockHWSurface = CGX_UnlockHWSurface;
    221 	device->FlipHWSurface = CGX_FlipHWSurface;
    222 	device->FreeHWSurface = CGX_FreeHWSurface;
    223 	device->SetGamma = CGX_SetGamma;
    224 	device->GetGamma = CGX_GetGamma;
    225 	device->SetGammaRamp = CGX_SetGammaRamp;
    226 	device->GetGammaRamp = NULL;
    227 #if SDL_VIDEO_OPENGL
    228 	device->GL_LoadLibrary = CGX_GL_LoadLibrary;
    229 	device->GL_GetProcAddress = CGX_GL_GetProcAddress;
    230 	device->GL_GetAttribute = CGX_GL_GetAttribute;
    231 	device->GL_MakeCurrent = CGX_GL_MakeCurrent;
    232 	device->GL_SwapBuffers = CGX_GL_SwapBuffers;
    233 #endif
    234 	device->SetIcon = CGX_SetIcon;
    235 	device->SetCaption = CGX_SetCaption;
    236 	device->IconifyWindow = NULL; /* CGX_IconifyWindow; */
    237 	device->GrabInput = NULL /* CGX_GrabInput*/;
    238 	device->GetWMInfo = CGX_GetWMInfo;
    239 	device->FreeWMCursor = amiga_FreeWMCursor;
    240 	device->CreateWMCursor = amiga_CreateWMCursor;
    241 	device->ShowWMCursor = amiga_ShowWMCursor;
    242 	device->WarpWMCursor = amiga_WarpWMCursor;
    243 	device->CheckMouseMode = amiga_CheckMouseMode;
    244 	device->InitOSKeymap = amiga_InitOSKeymap;
    245 	device->PumpEvents = amiga_PumpEvents;
    246 
    247 	device->free = CGX_DeleteDevice;
    248 
    249 	return device;
    250 }
    251 
    252 VideoBootStrap CGX_bootstrap = {
    253 	"CGX", "AmigaOS CyberGraphics", CGX_Available, CGX_CreateDevice
    254 };
    255 
    256 Uint32 MakeBitMask(_THIS,int type,int format,int *bpp)
    257 {
    258 	D(if(type==0)bug("REAL pixel format: "));
    259 
    260 	if(this->hidden->depth==*bpp)
    261 	{
    262 
    263 	switch(format)
    264     	{
    265 		case PIXFMT_LUT8:
    266 			D(if(type==0)bug("LUT8\n"));
    267 			return 0;
    268 		case PIXFMT_BGR15:
    269 		case PIXFMT_RGB15PC:
    270 			switch(type)
    271 			{
    272 				case 0:
    273 					D(bug("RGB15PC/BGR15\n"));
    274 					return 31;
    275 				case 1:
    276 					return 992;
    277 				case 2:
    278 					return 31744;
    279 			}
    280 		case PIXFMT_RGB15:
    281 		case PIXFMT_BGR15PC:
    282 			switch(type)
    283 			{
    284 				case 0:
    285 					D(bug("RGB15/BGR15PC\n"));
    286 					return 31744;
    287 				case 1:
    288 					return 992;
    289 				case 2:
    290 					return 31;
    291 			}
    292 		case PIXFMT_BGR16PC:
    293 		case PIXFMT_RGB16:
    294 			switch(type)
    295 			{
    296 				case 0:
    297 					D(bug("RGB16PC\n"));
    298 					return 63488;
    299 				case 1:
    300 					return 2016;
    301 				case 2:
    302 					return 31;
    303 			}
    304 		case PIXFMT_BGR16:
    305 		case PIXFMT_RGB16PC:
    306 			switch(type)
    307 			{
    308 				case 0:
    309 					D(bug("RGB16PC/BGR16\n"));
    310 					return 31;
    311 				case 1:
    312 					return 2016;
    313 				case 2:
    314 					return 63488;
    315 			}
    316 
    317 		case PIXFMT_RGB24:
    318 			switch(type)
    319 			{
    320 				case 0:
    321 					D(bug("RGB24/BGR24\n"));
    322 					return 0xff0000;
    323 				case 1:
    324 					return 0xff00;
    325 				case 2:
    326 					return 0xff;
    327 			}
    328 		case PIXFMT_BGR24:
    329 			switch(type)
    330 			{
    331 				case 0:
    332 					D(bug("BGR24\n"));
    333 					return 0xff;
    334 				case 1:
    335 					return 0xff00;
    336 				case 2:
    337 					return 0xff0000;
    338 			}
    339 		case PIXFMT_ARGB32:
    340 			switch(type)
    341 			{
    342 				case 0:
    343 					D(bug("ARGB32\n"));
    344 					return 0xff0000;
    345 				case 1:
    346 					return 0xff00;
    347 				case 2:
    348 					return 0xff;
    349 			}
    350 		case PIXFMT_BGRA32:
    351 			switch(type)
    352 			{
    353 				case 0:
    354 					D(bug("BGRA32\n"));
    355 					return 0xff00;
    356 				case 1:
    357 					return 0xff0000;
    358 				case 2:
    359 					return 0xff000000;
    360 			}
    361 		case PIXFMT_RGBA32:
    362 			switch(type)
    363 			{
    364 				case 0:
    365 					D(bug("RGBA32\n"));
    366 					return 0xff000000;
    367 				case 1:
    368 					return 0xff0000;
    369 				case 2:
    370 					return 0xff00;
    371 			}
    372 		default:
    373 			D(bug("Unknown pixel format! Default to 24bit\n"));
    374 			return (Uint32) (255<<(type*8));
    375 	}
    376 	}
    377 	else
    378 	{
    379 		D(if(type==0)bug("DIFFERENT from screen.\nAllocated screen format: "));
    380 
    381 		switch(*bpp)
    382 		{
    383 			case 32:
    384 				D(if(type==0) bug("RGBA32\n"));
    385 				switch(type)
    386 				{
    387 					case 0:
    388 						return 0xff000000;
    389 					case 1:
    390 						return 0xff0000;
    391 					case 2:
    392 						return 0xff00;
    393 				}
    394 				break;
    395 			case 24:
    396 use_truecolor:
    397 				switch(type)
    398 				{
    399 					case 0:
    400 						D(bug("RGB24\n"));
    401 						return 0xff0000;
    402 					case 1:
    403 						return 0xff00;
    404 					case 2:
    405 						return 0xff;
    406 				}
    407 			case 16:
    408 			case 15:
    409 				D(if(type==0) bug("Not supported, switching to 24bit!\n"));
    410 				*bpp=24;
    411 				goto use_truecolor;
    412 				break;
    413 			default:
    414 				D(if(type==0)bug("This is a chunky display\n"));
    415 // For chunky display mask is always 0;
    416 				return 0;
    417 		}
    418 	}
    419 	return 0;
    420 }
    421 
    422 static int CGX_VideoInit(_THIS, SDL_PixelFormat *vformat)
    423 {
    424 	int i;
    425 	struct Library *RTGBase;
    426 
    427 	D(bug("VideoInit... Opening libraries\n"));
    428 
    429 	if(!IntuitionBase) {
    430 		if( !(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",39L))) {
    431 			SDL_SetError("Couldn't open intuition V39+");
    432 			return -1;
    433 		}
    434 	}
    435 
    436 	if(!GfxBase) {
    437 		if( !(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",39L))) {
    438 			SDL_SetError("Couldn't open graphics V39+");
    439 			return -1;
    440 		}
    441 	}
    442 
    443 	if(!CyberGfxBase) {
    444 		if( !(CyberGfxBase=OpenLibrary("cybergraphics.library",40L))) {
    445 			SDL_SetError("Couldn't open cybergraphics.");
    446 			return(-1);
    447 		}
    448 	}
    449 
    450 	if(RTGBase=OpenLibrary("libs:picasso96/rtg.library",0L)) {
    451 		extern int use_picasso96;
    452 
    453 		CloseLibrary(RTGBase);
    454 		use_picasso96=1;
    455 	}
    456 
    457 	D(bug("Library intialized, locking screen...\n"));
    458 
    459 	SDL_Display = LockPubScreen(NULL);
    460 
    461 	if ( SDL_Display == NULL ) {
    462 		D(bug("Cannot lock display...\n"));
    463 		SDL_SetError("Couldn't lock the display");
    464 		return(-1);
    465 	}
    466 	this->info.current_w = SDL_Display->Width;
    467 	this->info.current_h = SDL_Display->Height;
    468 
    469 	D(bug("Checking if we are using a CGX native display...\n"));
    470 
    471 	if(!IsCyberModeID(GetVPModeID(&SDL_Display->ViewPort)))
    472 	{
    473 		Uint32 okid=BestCModeIDTags(CYBRBIDTG_NominalWidth,SDL_Display->Width,
    474 				CYBRBIDTG_NominalHeight,SDL_Display->Height,
    475 				CYBRBIDTG_Depth,8,
    476 				TAG_DONE);
    477 
    478 		D(bug("Default visual is not CGX native!\n"));
    479 
    480 		UnlockPubScreen(NULL,SDL_Display);
    481 
    482 		GFX_Display=NULL;
    483 
    484 		if(okid!=INVALID_ID)
    485 		{
    486 			GFX_Display=OpenScreenTags(NULL,
    487 									SA_Width,SDL_Display->Width,
    488 									SA_Height,SDL_Display->Height,
    489 									SA_Depth,8,SA_Quiet,TRUE,
    490 									SA_ShowTitle,FALSE,
    491 									SA_DisplayID,okid,
    492 									TAG_DONE);
    493 		}
    494 
    495 		if(!GFX_Display)
    496 		{
    497 			SDL_SetError("Unable to open a suited CGX display");
    498 			return -1;
    499 		}
    500 		else SDL_Display=GFX_Display;
    501 
    502 	}
    503 	else GFX_Display = SDL_Display;
    504 
    505 
    506 	/* See whether or not we need to swap pixels */
    507 
    508 	swap_pixels = 0;
    509 
    510 // Non e' detto che sia cosi' pero', alcune schede potrebbero gestire i modi in modo differente
    511 
    512 	if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
    513 		swap_pixels = 1;
    514 	}
    515 
    516 	D(bug("Before GetVideoModes....\n"));
    517 
    518 	/* Get the available video modes */
    519 	if(CGX_GetVideoModes(this) < 0)
    520 	    return -1;
    521 
    522 	/* Determine the default screen depth:
    523 	   Use the default visual (or at least one with the same depth) */
    524 
    525 	for(i = 0; i < this->hidden->nvisuals; i++)
    526 	    if(this->hidden->visuals[i].depth == GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_DEPTH))
    527 					break;
    528 	if(i == this->hidden->nvisuals) {
    529 	    /* default visual was useless, take the deepest one instead */
    530 	    i = 0;
    531 	}
    532 	SDL_Visual = this->hidden->visuals[i].visual;
    533 
    534 //	SDL_XColorMap = SDL_DisplayColormap;
    535 
    536 	this->hidden->depth = this->hidden->visuals[i].depth;
    537 	D(bug("Init: Setting screen depth to: %ld\n",this->hidden->depth));
    538 	vformat->BitsPerPixel = this->hidden->visuals[i].depth; /* this->hidden->visuals[i].bpp; */
    539 
    540 	{
    541 		int form;
    542 		APTR handle;
    543 		struct DisplayInfo info;
    544 
    545 		if(!(handle=FindDisplayInfo(this->hidden->visuals[i].visual)))
    546 		{
    547 			D(bug("Unable to get visual info...\n"));
    548 			return -1;
    549 		}
    550 
    551 		if(!GetDisplayInfoData(handle,(char *)&info,sizeof(struct DisplayInfo),DTAG_DISP,NULL)) {
    552 			D(bug("Unable to get visual info data...\n"));
    553 			return -1;
    554 		}
    555 
    556 		form=GetCyberIDAttr(CYBRIDATTR_PIXFMT,SDL_Visual);
    557 
    558 // In this case I use makebitmask in a way that I'm sure I'll get PIXFMT pixel mask
    559 
    560 		if ( vformat->BitsPerPixel > 8 )
    561 		{
    562 			vformat->Rmask = MakeBitMask(this,0,form,&this->hidden->depth);
    563 	  		vformat->Gmask = MakeBitMask(this,1,form,&this->hidden->depth);
    564 		  	vformat->Bmask = MakeBitMask(this,2,form,&this->hidden->depth);
    565 		}
    566 	}
    567 
    568 	/* See if we have been passed a window to use */
    569 /*	SDL_windowid = SDL_getenv("SDL_WINDOWID"); */
    570 	SDL_windowid=NULL;
    571 
    572 	/* Create the blank cursor */
    573 	SDL_BlankCursor = AllocMem(16,MEMF_CHIP|MEMF_CLEAR);
    574 
    575 	/* Fill in some window manager capabilities */
    576 	this->info.wm_available = 1;
    577 	this->info.blit_hw = 1;
    578 	this->info.blit_hw_CC = 1;
    579 	this->info.blit_sw = 1;
    580 	this->info.blit_fill = 1;
    581 	this->info.video_mem=2000000; // Not always true but almost any Amiga card has this memory!
    582 
    583 	this->hidden->same_format=0;
    584 	SDL_RastPort=&SDL_Display->RastPort;
    585 	/* We're done! */
    586 	D(bug("End of CGX_VideoInit\n"));
    587 
    588 	return(0);
    589 }
    590 
    591 void CGX_DestroyWindow(_THIS, SDL_Surface *screen)
    592 {
    593 	D(bug("Destroy Window...\n"));
    594 
    595 	if ( ! SDL_windowid ) {
    596 		/* Hide the managed window */
    597 		int was_fullscreen=0;
    598 
    599 		/* Clean up OpenGL */
    600 		if ( screen ) {
    601 		screen->flags &= ~(SDL_OPENGL|SDL_OPENGLBLIT);
    602 		}
    603 
    604 		if ( screen && (screen->flags & SDL_FULLSCREEN) ) {
    605 			was_fullscreen=1;
    606 			screen->flags &= ~SDL_FULLSCREEN;
    607 //			CGX_LeaveFullScreen(this); tolto x crash
    608 		}
    609 
    610 		/* Destroy the output window */
    611 		if ( SDL_Window ) {
    612 			CloseWindow(SDL_Window);
    613 			SDL_Window=NULL;
    614 		}
    615 
    616 		/* Free the colormap entries */
    617 		if ( SDL_XPixels ) {
    618 			int numcolors;
    619 			unsigned long pixel;
    620 
    621 			if(this->screen->format&&this->hidden->depth==8&&!was_fullscreen)
    622 			{
    623 				numcolors = 1<<this->screen->format->BitsPerPixel;
    624 
    625 				if(numcolors>256)
    626 					numcolors=256;
    627 
    628 				if(!was_fullscreen&&this->hidden->depth==8)
    629 				{
    630 					for ( pixel=0; pixel<numcolors; pixel++ )
    631 					{
    632 						if(SDL_XPixels[pixel]>=0)
    633 							ReleasePen(GFX_Display->ViewPort.ColorMap,SDL_XPixels[pixel]);
    634 					}
    635 				}
    636 			}
    637 			SDL_free(SDL_XPixels);
    638 			SDL_XPixels = NULL;
    639 		}
    640 	}
    641 }
    642 
    643 static void CGX_SetSizeHints(_THIS, int w, int h, Uint32 flags)
    644 {
    645 	if ( flags & SDL_RESIZABLE ) {
    646 		WindowLimits(SDL_Window, 32, 32,4096,4096);
    647 	} else {
    648 		WindowLimits(SDL_Window, w,h,w,h);
    649 	}
    650 	if ( flags & SDL_FULLSCREEN ) {
    651 		flags&=~SDL_RESIZABLE;
    652 	} else if ( SDL_getenv("SDL_VIDEO_CENTERED") ) {
    653 		int display_w, display_h;
    654 
    655 		display_w = SDL_Display->Width;
    656 		display_h = SDL_Display->Height;
    657 		ChangeWindowBox(SDL_Window,(display_w - w - SDL_Window->BorderLeft-SDL_Window->BorderRight)/2,
    658 					(display_h - h - SDL_Window->BorderTop-SDL_Window->BorderBottom)/2,
    659 					w+SDL_Window->BorderLeft+SDL_Window->BorderRight,
    660 					h+SDL_Window->BorderTop+SDL_Window->BorderBottom);
    661 	}
    662 }
    663 
    664 int CGX_CreateWindow(_THIS, SDL_Surface *screen,
    665 			    int w, int h, int bpp, Uint32 flags)
    666 {
    667 #if 0
    668 	int i, depth;
    669 	Uint32 vis;
    670 #endif
    671 	D(bug("CGX_CreateWindow\n"));
    672 
    673 	/* If a window is already present, destroy it and start fresh */
    674 	if ( SDL_Window ) {
    675 		CGX_DestroyWindow(this, screen);
    676 	}
    677 
    678 	/* See if we have been given a window id */
    679 	if ( SDL_windowid ) {
    680 		SDL_Window = (struct Window *)atol(SDL_windowid);
    681 	} else {
    682 		SDL_Window = 0;
    683 	}
    684 
    685 	/* find out which visual we are going to use */
    686 #if 0
    687 /* questo l'ho spostato nell'apertura dello schermo, in quanto su Amiga le finestre
    688    hanno il pixel mode degli schermi.
    689  */
    690 	/*if ( flags & SDL_OPENGL ) {
    691 		SDL_SetError("OpenGL not supported by the Amiga SDL!");
    692 		return -1;
    693 	}
    694 	else {*/
    695 		for ( i = 0; i < this->hidden->nvisuals; i++ ) {
    696 			if ( this->hidden->visuals[i].depth == bpp ) /* era .depth */
    697 				break;
    698 		}
    699 		if ( i == this->hidden->nvisuals ) {
    700 			SDL_SetError("No matching visual for requested depth");
    701 			return -1;	/* should never happen */
    702 		}
    703 		vis = this->hidden->visuals[i].visual;
    704 		depth = this->hidden->visuals[i].depth;
    705 //	}
    706 	SDL_Visual = vis;
    707 	this->hidden->depth = depth;
    708 	D(bug("Setting screen depth to: %ld\n",this->hidden->depth));
    709 #endif
    710 
    711 	/* Allocate the new pixel format for this video mode */
    712 	{
    713 		Uint32 form;
    714 		APTR handle;
    715 		struct DisplayInfo info;
    716 
    717 		if(!(handle=FindDisplayInfo(SDL_Visual)))
    718 			return -1;
    719 
    720 		if(!GetDisplayInfoData(handle,(char *)&info,sizeof(struct DisplayInfo),DTAG_DISP,NULL))
    721 			return -1;
    722 
    723 		form=GetCyberIDAttr(CYBRIDATTR_PIXFMT,SDL_Visual);
    724 
    725 		if(flags&SDL_HWSURFACE)
    726 		{
    727 			if(bpp!=this->hidden->depth)
    728 			{
    729 				bpp=this->hidden->depth;
    730 				D(bug("Accel forces bpp to be equal (%ld)\n",bpp));
    731 			}
    732 		}
    733 
    734 		D(bug("BEFORE screen allocation: bpp:%ld (real:%ld)\n",bpp,this->hidden->depth));
    735 
    736 /* With this call if needed I'll revert the wanted bpp to a bpp best suited for the display, actually occurs
    737    only with requested format 15/16bit and display format != 15/16bit
    738  */
    739 
    740 		if ( ! SDL_ReallocFormat(screen, bpp,
    741 				MakeBitMask(this,0,form,&bpp), MakeBitMask(this,1,form,&bpp), MakeBitMask(this,2,form,&bpp), 0) )
    742 			return -1;
    743 
    744 		D(bug("AFTER screen allocation: bpp:%ld (real:%ld)\n",bpp,this->hidden->depth));
    745 
    746 	}
    747 
    748 	/* Create the appropriate colormap */
    749 /*
    750 	if ( SDL_XColorMap != SDL_DisplayColormap ) {
    751 		XFreeColormap(SDL_Display, SDL_XColorMap);
    752 	}
    753 */
    754 	if ( GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_PIXFMT)==PIXFMT_LUT8 || bpp==8 ) {
    755 	    int ncolors,i;
    756 	    D(bug("XPixels palette allocation...\n"));
    757 
    758 	    /* Allocate the pixel flags */
    759 
    760 	    if(bpp==8)
    761 		ncolors=256;
    762 	    else
    763 		ncolors = 1 << screen->format->BitsPerPixel;
    764 
    765 	    SDL_XPixels = (Sint32 *)SDL_malloc(ncolors * sizeof(Sint32));
    766 
    767 	    if(SDL_XPixels == NULL) {
    768 		SDL_OutOfMemory();
    769 		return -1;
    770 	    }
    771 
    772 
    773 	    for(i=0;i<ncolors;i++)
    774 		    SDL_XPixels[i]=-1;
    775 
    776 	    /* always allocate a private colormap on non-default visuals */
    777 	    if(bpp==8)
    778 		flags |= SDL_HWPALETTE;
    779 
    780 	    if ( flags & SDL_HWPALETTE )
    781 			screen->flags |= SDL_HWPALETTE;
    782 	}
    783 
    784 	/* resize the (possibly new) window manager window */
    785 
    786 	/* Create (or use) the X11 display window */
    787 
    788 	if ( !SDL_windowid ) {
    789 			if( flags & SDL_FULLSCREEN )
    790 			{
    791 				SDL_Window = OpenWindowTags(NULL,WA_Width,w,WA_Height,h,
    792 											WA_Flags,WFLG_ACTIVATE|WFLG_RMBTRAP|WFLG_BORDERLESS|WFLG_BACKDROP|WFLG_REPORTMOUSE,
    793 											WA_IDCMP,IDCMP_RAWKEY|IDCMP_MOUSEBUTTONS|IDCMP_MOUSEMOVE,
    794 											WA_CustomScreen,(ULONG)SDL_Display,
    795 											TAG_DONE);
    796 
    797 				D(bug("Opening backdrop window %ldx%ld on display %lx!\n",w,h,SDL_Display));
    798 			}
    799 			else
    800 			{
    801 				/* Create GimmeZeroZero window when OpenGL is used */
    802 				unsigned long gzz = FALSE;
    803 				if( flags & SDL_OPENGL ) {
    804 					gzz = TRUE;
    805 				}
    806 
    807 				SDL_Window = OpenWindowTags(NULL,WA_InnerWidth,w,WA_InnerHeight,h,
    808 											WA_Flags,WFLG_REPORTMOUSE|WFLG_ACTIVATE|WFLG_RMBTRAP | ((flags&SDL_NOFRAME) ? 0 : (WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_DRAGBAR | ((flags&SDL_RESIZABLE) ? WFLG_SIZEGADGET|WFLG_SIZEBBOTTOM : 0))),
    809 											WA_IDCMP,IDCMP_RAWKEY|IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS|IDCMP_NEWSIZE|IDCMP_MOUSEMOVE,
    810 											WA_PubScreen,(ULONG)SDL_Display,
    811 											WA_GimmeZeroZero, gzz,
    812 														TAG_DONE);
    813 				D(bug("Opening WB window of size: %ldx%ld!\n",w,h));
    814 			}
    815 
    816 		if(!SDL_Window)
    817 			return -1;
    818 	}
    819 
    820 	this->hidden->BytesPerPixel=GetCyberMapAttr(SDL_Window->RPort->BitMap,CYBRMATTR_BPPIX);
    821 
    822 	if(screen->flags & SDL_DOUBLEBUF)
    823 	{
    824 		if(SDL_RastPort=SDL_malloc(sizeof(struct RastPort)))
    825 		{
    826 			InitRastPort(SDL_RastPort);
    827 			SDL_RastPort->BitMap=this->hidden->SB[1]->sb_BitMap;
    828 		}
    829 		else
    830 			return -1;
    831 	}
    832 	else SDL_RastPort=SDL_Window->RPort;
    833 
    834 	if(flags&SDL_HWSURFACE)
    835 		screen->flags|=SDL_HWSURFACE;
    836 
    837 	if( !SDL_windowid ) {
    838 	    CGX_SetSizeHints(this, w, h, flags);
    839 	}
    840 
    841 	/* Set our colormaps when not setting a GL mode */
    842 /*
    843 	if ( ! (flags & SDL_OPENGL) ) {
    844 		XSetWindowColormap(SDL_Display, SDL_Window, SDL_XColorMap);
    845 	}
    846 */
    847 
    848 	/* Map them both and go fullscreen, if requested */
    849 	if ( ! SDL_windowid ) {
    850 		if ( flags & SDL_FULLSCREEN ) {
    851 			screen->flags |= SDL_FULLSCREEN;
    852 			currently_fullscreen=1;
    853 //			CGX_EnterFullScreen(this); Ci siamo gia'!
    854 		} else {
    855 			screen->flags &= ~SDL_FULLSCREEN;
    856 		}
    857 	}
    858 	screen->w = w;
    859 	screen->h = h;
    860 	screen->pitch = SDL_CalculatePitch(screen);
    861 	CGX_ResizeImage(this, screen, flags);
    862 
    863 	/* Make OpenGL Context if needed*/
    864 	if(flags & SDL_OPENGL) {
    865 		if(this->gl_data->gl_active == 0) {
    866 			if(CGX_GL_Init(this) < 0)
    867 				return -1;
    868 			else
    869 				screen->flags |= SDL_OPENGL;
    870 		}
    871 		else {
    872 			if(CGX_GL_Update(this) < 0)
    873 				return -1;
    874 			else
    875 				screen->flags |= SDL_OPENGL;
    876 		}
    877 	}
    878 }
    879 
    880 int CGX_ResizeWindow(_THIS,
    881 			SDL_Surface *screen, int w, int h, Uint32 flags)
    882 {
    883 	D(bug("CGX_ResizeWindow\n"));
    884 
    885 	if ( ! SDL_windowid ) {
    886 		/* Resize the window manager window */
    887 		CGX_SetSizeHints(this, w, h, flags);
    888 
    889 		ChangeWindowBox(SDL_Window,SDL_Window->LeftEdge,SDL_Window->TopEdge, w+SDL_Window->BorderLeft+SDL_Window->BorderRight,
    890 					h+SDL_Window->BorderTop+SDL_Window->BorderBottom);
    891 
    892 		screen->w = w;
    893 		screen->h = h;
    894 		screen->pitch = SDL_CalculatePitch(screen);
    895 		CGX_ResizeImage(this, screen, flags);
    896 	}
    897 	return(0);
    898 }
    899 
    900 static SDL_Surface *CGX_SetVideoMode(_THIS, SDL_Surface *current,
    901 				int width, int height, int bpp, Uint32 flags)
    902 {
    903 	Uint32 saved_flags;
    904 	int needcreate=0;
    905 
    906 	D(bug("CGX_SetVideoMode current:%lx\n",current));
    907 
    908 	/* Lock the event thread, in multi-threading environments */
    909 	SDL_Lock_EventThread();
    910 
    911 // Check if the window needs to be closed or can be resized
    912 
    913 	if( (flags&SDL_FULLSCREEN) || (current && current->flags&SDL_FULLSCREEN && !(flags&SDL_FULLSCREEN)))
    914 		needcreate=1;
    915 
    916 // Check if we need to close an already existing videomode...
    917 
    918 	if(current && current->flags&SDL_FULLSCREEN && !(flags&SDL_FULLSCREEN)) {
    919 		unsigned long i;
    920 		D(bug("Destroying image, window & screen!\n"));
    921 
    922 		CGX_DestroyImage(this,current);
    923 		CGX_DestroyWindow(this,current);
    924 		DestroyScreen(this);
    925 		GFX_Display=SDL_Display=LockPubScreen(NULL);
    926 
    927 		bpp=this->hidden->depth=GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_DEPTH);
    928 
    929 		for ( i = 0; i < this->hidden->nvisuals; i++ ) {
    930 			if ( this->hidden->visuals[i].depth == bpp ) /* era .depth */
    931 				break;
    932 		}
    933 		if ( i == this->hidden->nvisuals ) {
    934 			SDL_SetError("No matching visual for requested depth");
    935 			return NULL;	/* should never happen */
    936 		}
    937 		SDL_Visual = this->hidden->visuals[i].visual;
    938 
    939 		D(bug("Setting screen depth to: %ld\n",this->hidden->depth));
    940 
    941 	}
    942 	/* Check the combination of flags we were passed */
    943 	if ( flags & SDL_FULLSCREEN ) {
    944 		int i;
    945 
    946 		/* Clear fullscreen flag if not supported */
    947 		if ( SDL_windowid ) {
    948 			flags &= ~SDL_FULLSCREEN;
    949 		}
    950 		else if(current && current->flags&SDL_FULLSCREEN ) {
    951 			if(current->w!=width ||
    952 				current->h!=height ||
    953 				(this->hidden && this->hidden->depth!=bpp))
    954 			{
    955 				D(bug("Deleting previous window...\n"));
    956 				CGX_DestroyImage(this,current);
    957 				CGX_DestroyWindow(this,current);
    958 				DestroyScreen(this);
    959 				goto buildnewscreen;
    960 			}
    961 		}
    962 		else
    963 buildnewscreen:
    964 		{
    965 			Uint32 okid=BestCModeIDTags(CYBRBIDTG_NominalWidth,width,
    966 				CYBRBIDTG_NominalHeight,height,
    967 				CYBRBIDTG_Depth,bpp,
    968 				TAG_DONE);
    969 
    970 			GFX_Display=NULL;
    971 
    972 			D(bug("Opening screen...\n"));
    973 
    974 			if(okid!=INVALID_ID)
    975 				GFX_Display=OpenScreenTags(NULL,
    976 								SA_Width,width,
    977 								SA_Height,height,
    978 								SA_Quiet,TRUE,SA_ShowTitle,FALSE,
    979 								SA_Depth,bpp,
    980 								SA_DisplayID,okid,
    981 								TAG_DONE);
    982 
    983 			if(!GFX_Display) {
    984 				GFX_Display=SDL_Display;
    985 				flags &= ~SDL_FULLSCREEN;
    986 				flags &= ~SDL_DOUBLEBUF;
    987 			}
    988 			else {
    989 				UnlockPubScreen(NULL,SDL_Display);
    990 				SDL_Display=GFX_Display;
    991 
    992 				D(bug("Screen opened.\n"));
    993 
    994 				if(flags&SDL_DOUBLEBUF) {
    995 					int ok=0;
    996 					D(bug("Start of DBuffering allocations...\n"));
    997 
    998 					if(this->hidden->SB[0]=AllocScreenBuffer(SDL_Display,NULL,SB_SCREEN_BITMAP)) {
    999 
   1000 						if(this->hidden->SB[1]=AllocScreenBuffer(SDL_Display,NULL,0L)) {
   1001 							extern struct MsgPort *safeport,*dispport;
   1002 
   1003 							safeport=CreateMsgPort();
   1004 							dispport=CreateMsgPort();
   1005 
   1006 							if(!safeport || !dispport) {
   1007 								if(safeport) {
   1008 									DeleteMsgPort(safeport);
   1009 									safeport=NULL;
   1010 								}
   1011 								if(dispport) {
   1012 									DeleteMsgPort(dispport);
   1013 									dispport=NULL;
   1014 								}
   1015 								FreeScreenBuffer(SDL_Display,this->hidden->SB[0]);
   1016 								FreeScreenBuffer(SDL_Display,this->hidden->SB[1]);
   1017 							}
   1018 							else {
   1019 								extern ULONG safe_sigbit,disp_sigbit;
   1020 								int i;
   1021 
   1022 								safe_sigbit=1L<< safeport->mp_SigBit;
   1023 								disp_sigbit=1L<< dispport->mp_SigBit;
   1024 
   1025 								for(i=0;i<2;i++) {
   1026 									this->hidden->SB[i]->sb_DBufInfo->dbi_SafeMessage.mn_ReplyPort=safeport;
   1027 									this->hidden->SB[i]->sb_DBufInfo->dbi_DispMessage.mn_ReplyPort=dispport;
   1028 								}
   1029 
   1030 								ok=1;
   1031 								D(bug("Dbuffering enabled!\n"));
   1032 								this->hidden->dbuffer=1;
   1033 								current->flags|=SDL_DOUBLEBUF;
   1034 							}
   1035 						}
   1036 						else {
   1037 							FreeScreenBuffer(SDL_Display,this->hidden->SB[1]);
   1038 							this->hidden->SB[0]=NULL;
   1039 						}
   1040 					}
   1041 
   1042 					if(!ok)
   1043 						flags&=~SDL_DOUBLEBUF;
   1044 				}
   1045 			}
   1046 
   1047 			if(GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_DEPTH)==bpp)
   1048 				this->hidden->same_format=1;
   1049 		}
   1050 
   1051 		bpp=this->hidden->depth=GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_DEPTH);
   1052 		D(bug("Setting screen depth to: %ld\n",this->hidden->depth));
   1053 
   1054 		for ( i = 0; i < this->hidden->nvisuals; i++ )
   1055 			if ( this->hidden->visuals[i].depth == bpp ) /* era .depth */
   1056 				break;
   1057 
   1058 		if ( i == this->hidden->nvisuals ) {
   1059 			SDL_SetError("No matching visual for requested depth");
   1060 			return NULL;	/* should never happen */
   1061 		}
   1062 		SDL_Visual = this->hidden->visuals[i].visual;
   1063 
   1064 	}
   1065 
   1066 	/* Set up the X11 window */
   1067 	saved_flags = current->flags;
   1068 
   1069 	if (SDL_Window && (saved_flags&SDL_OPENGL) == (flags&SDL_OPENGL)
   1070 	    && bpp == current->format->BitsPerPixel && !needcreate) {
   1071 		if (CGX_ResizeWindow(this, current, width, height, flags) < 0) {
   1072 			current = NULL;
   1073 			goto done;
   1074 		}
   1075 	} else {
   1076 		if (CGX_CreateWindow(this,current,width,height,bpp,flags) < 0) {
   1077 			current = NULL;
   1078 			goto done;
   1079 		}
   1080 	}
   1081 
   1082 #if 0
   1083 	/* Set up the new mode framebuffer */
   1084 	if ( ((current->w != width) || (current->h != height)) ||
   1085              ((saved_flags&SDL_OPENGL) != (flags&SDL_OPENGL)) ) {
   1086 		current->w = width;
   1087 		current->h = height;
   1088 		current->pitch = SDL_CalculatePitch(current);
   1089 		CGX_ResizeImage(this, current, flags);
   1090 	}
   1091 #endif
   1092 
   1093 	current->flags |= (flags&SDL_RESIZABLE); // Resizable only if the user asked it
   1094 
   1095   done:
   1096 	/* Release the event thread */
   1097 	SDL_Unlock_EventThread();
   1098 
   1099 	/* We're done! */
   1100 	return(current);
   1101 }
   1102 
   1103 static int CGX_ToggleFullScreen(_THIS, int on)
   1104 {
   1105 	Uint32 event_thread;
   1106 
   1107 	/* Don't switch if we don't own the window */
   1108 	if ( SDL_windowid ) {
   1109 		return(0);
   1110 	}
   1111 
   1112 	/* Don't lock if we are the event thread */
   1113 	event_thread = SDL_EventThreadID();
   1114 	if ( event_thread && (SDL_ThreadID() == event_thread) ) {
   1115 		event_thread = 0;
   1116 	}
   1117 	if ( event_thread ) {
   1118 		SDL_Lock_EventThread();
   1119 	}
   1120 	if ( on ) {
   1121 		this->screen->flags |= SDL_FULLSCREEN;
   1122 		CGX_EnterFullScreen(this);
   1123 	} else {
   1124 		this->screen->flags &= ~SDL_FULLSCREEN;
   1125 		CGX_LeaveFullScreen(this);
   1126 	}
   1127 
   1128 	CGX_RefreshDisplay(this);
   1129 	if ( event_thread ) {
   1130 		SDL_Unlock_EventThread();
   1131 	}
   1132 
   1133 	SDL_ResetKeyboard();
   1134 
   1135 	return(1);
   1136 }
   1137 
   1138 static void SetSingleColor(Uint32 fmt, unsigned char r, unsigned char g, unsigned char b, unsigned char *c)
   1139 {
   1140 	switch(fmt)
   1141 	{
   1142 		case PIXFMT_BGR15:
   1143 		case PIXFMT_RGB15PC:
   1144 			{
   1145 				Uint16 *t=(Uint16 *)c;
   1146 				*t=(r>>3) | ((g>>3)<<5) | ((b>>3)<<10) ;
   1147 			}
   1148 			break;
   1149 		case PIXFMT_RGB15:
   1150 		case PIXFMT_BGR15PC:
   1151 			{
   1152 				Uint16 *t=(Uint16 *)c;
   1153 				*t=(b>>3) | ((g>>3)<<5) | ((r>>3)<<10) ;
   1154 			}
   1155 			break;
   1156 		case PIXFMT_BGR16PC:
   1157 		case PIXFMT_RGB16:
   1158 			{
   1159 				Uint16 *t=(Uint16 *)c;
   1160 				*t=(b>>3) | ((g>>2)<<5) | ((r>>3)<<11) ;
   1161 			}
   1162 			break;
   1163 		case PIXFMT_BGR16:
   1164 		case PIXFMT_RGB16PC:
   1165 			{
   1166 				Uint16 *t=(Uint16 *)c;
   1167 				*t=(r>>3) | ((g>>2)<<5) | ((b>>3)<<11) ;
   1168 			}
   1169 			break;
   1170 		case PIXFMT_RGB24:
   1171 			c[0]=r;
   1172 			c[1]=g;
   1173 			c[2]=b;
   1174 			c[3]=0;
   1175 			break;
   1176 		case PIXFMT_BGR24:
   1177 			c[0]=b;
   1178 			c[1]=g;
   1179 			c[2]=r;
   1180 			c[3]=0;
   1181 			break;
   1182 		case PIXFMT_ARGB32:
   1183 			c[0]=0;
   1184 			c[1]=r;
   1185 			c[2]=g;
   1186 			c[3]=b;
   1187 			break;
   1188 		case PIXFMT_BGRA32:
   1189 			c[0]=b;
   1190 			c[1]=g;
   1191 			c[2]=r;
   1192 			c[3]=0;
   1193 			break;
   1194 		case PIXFMT_RGBA32:
   1195 			c[0]=r;
   1196 			c[1]=g;
   1197 			c[2]=b;
   1198 			c[3]=0;
   1199 			break;
   1200 
   1201 		default:
   1202 			D(bug("Error, SetSingleColor with PIXFMT %ld!\n",fmt));
   1203 	}
   1204 }
   1205 
   1206 /* Update the current mouse state and position */
   1207 static void CGX_UpdateMouse(_THIS)
   1208 {
   1209 	/* Lock the event thread, in multi-threading environments */
   1210 	SDL_Lock_EventThread();
   1211 
   1212 	if(currently_fullscreen)
   1213 	{
   1214 			SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   1215 			SDL_PrivateMouseMotion(0, 0, SDL_Display->MouseX, SDL_Display->MouseY);
   1216 	}
   1217 	else
   1218 	{
   1219 		if(	SDL_Display->MouseX>=(SDL_Window->LeftEdge+SDL_Window->BorderLeft) && SDL_Display->MouseX<(SDL_Window->LeftEdge+SDL_Window->Width-SDL_Window->BorderRight) &&
   1220 			SDL_Display->MouseY>=(SDL_Window->TopEdge+SDL_Window->BorderLeft) && SDL_Display->MouseY<(SDL_Window->TopEdge+SDL_Window->Height-SDL_Window->BorderBottom)
   1221 			)
   1222 		{
   1223 			SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   1224 			SDL_PrivateMouseMotion(0, 0, SDL_Display->MouseX-SDL_Window->LeftEdge-SDL_Window->BorderLeft,
   1225 										SDL_Display->MouseY-SDL_Window->TopEdge-SDL_Window->BorderTop);
   1226 		}
   1227 		else
   1228 		{
   1229 			SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
   1230 		}
   1231 	}
   1232 	SDL_Unlock_EventThread();
   1233 }
   1234 
   1235 static int CGX_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   1236 {
   1237 	int      i;
   1238 
   1239 	/* Check to make sure we have a colormap allocated */
   1240 
   1241 	/* It's easy if we have a hidden colormap */
   1242 	if ( (this->screen->flags & SDL_HWPALETTE) && currently_fullscreen )
   1243 	{
   1244 		ULONG  xcmap[256*3+2];
   1245 
   1246 		xcmap[0]=(ncolors<<16);
   1247 		xcmap[0]+=firstcolor;
   1248 
   1249 //		D(bug("Setting %ld colors on an HWPALETTE screen\n",ncolors));
   1250 
   1251 		for ( i=0; i<ncolors; i++ ) {
   1252 			xcmap[i*3+1] = colors[i+firstcolor].r<<24;
   1253 			xcmap[i*3+2] = colors[i+firstcolor].g<<24;
   1254 			xcmap[i*3+3] = colors[i+firstcolor].b<<24;
   1255 		}
   1256 		xcmap[ncolors*3+1]=0;
   1257 		LoadRGB32(&GFX_Display->ViewPort,xcmap);
   1258 	} else {
   1259 // XPixels are not needed on 8bit screen with hwpalette
   1260 		unsigned long pixel;
   1261 
   1262 		if ( SDL_XPixels == NULL ) {
   1263 			D(bug("SetColors without colormap!"));
   1264 			return(0);
   1265 		}
   1266 
   1267 		if(this->hidden->depth==8)
   1268 		{
   1269 // In this case I have to unalloc and realloc the full palette
   1270 			D(bug("Obtaining %ld colors on the screen\n",ncolors));
   1271 
   1272 		/* Free existing allocated colors */
   1273 			for ( pixel=0; pixel<this->screen->format->palette->ncolors; ++pixel ) {
   1274 				if(SDL_XPixels[pixel]>=0)
   1275 					ReleasePen(GFX_Display->ViewPort.ColorMap,SDL_XPixels[pixel]);
   1276 			}
   1277 
   1278 		/* Try to allocate all the colors */
   1279 			for ( i=0; i<this->screen->format->palette->ncolors; ++i ) {
   1280 				SDL_XPixels[i]=ObtainBestPenA(GFX_Display->ViewPort.ColorMap,colors[i].r<<24,colors[i].g<<24,colors[i].b<<24,NULL);
   1281 			}
   1282 		}
   1283 		else
   1284 		{
   1285 #ifndef USE_CGX_WRITELUTPIXEL
   1286 			Uint32 fmt;
   1287 			D(bug("Preparing a conversion pixel table...\n"));
   1288 
   1289 			fmt=GetCyberMapAttr(SDL_Display->RastPort.BitMap,CYBRMATTR_PIXFMT);
   1290 
   1291 			for(i=0;i<ncolors;i++)
   1292 			{
   1293 				SetSingleColor(fmt,colors[firstcolor+i].r,colors[firstcolor+i].g,colors[firstcolor+i].b,(unsigned char *)&SDL_XPixels[firstcolor+i]);
   1294 			}
   1295 #else
   1296 //			D(bug("Executing XPixel(%lx) remapping: (from %ld, %ld colors) first: r%ld g%ld b%ld\n",SDL_XPixels,firstcolor,ncolors,colors[firstcolor].r,colors[firstcolor].g,colors[firstcolor].b));
   1297 			for(i=0;i<ncolors;i++)
   1298 				SDL_XPixels[i+firstcolor]=(colors[firstcolor+i].r<<16)+(colors[firstcolor+i].g<<8)+colors[firstcolor+i].b;
   1299 #endif
   1300 		}
   1301 	}
   1302 
   1303 // Actually it cannot fail!
   1304 
   1305 	return 1;
   1306 }
   1307 
   1308 /* Note:  If we are terminated, this could be called in the middle of
   1309    another SDL video routine -- notably UpdateRects.
   1310 */
   1311 static void CGX_VideoQuit(_THIS)
   1312 {
   1313 	/* Shutdown everything that's still up */
   1314 	/* The event thread should be done, so we can touch SDL_Display */
   1315 	D(bug("CGX_VideoQuit\n"));
   1316 
   1317 	if ( SDL_Display != NULL ) {
   1318 		/* Clean up OpenGL */
   1319 		if(this->gl_data->gl_active == 1) {
   1320 			CGX_GL_Quit(this);
   1321 		}
   1322 		/* Start shutting down the windows */
   1323 		D(bug("Destroying image...\n"));
   1324 		CGX_DestroyImage(this, this->screen);
   1325 		D(bug("Destroying window...\n"));
   1326 		CGX_DestroyWindow(this, this->screen);
   1327 // Otherwise SDL_VideoQuit will try to free it!
   1328 		SDL_VideoSurface=NULL;
   1329 
   1330 		CGX_FreeVideoModes(this);
   1331 
   1332 		/* Free that blank cursor */
   1333 		if ( SDL_BlankCursor != NULL ) {
   1334 			FreeMem(SDL_BlankCursor,16);
   1335 			SDL_BlankCursor = NULL;
   1336 		}
   1337 
   1338 		/* Close the X11 graphics connection */
   1339 		this->hidden->same_format=0;
   1340 
   1341 		D(bug("Destroying screen...\n"));
   1342 
   1343 		if ( GFX_Display != NULL )
   1344 			DestroyScreen(this);
   1345 
   1346 		/* Close the X11 display connection */
   1347 		SDL_Display = NULL;
   1348 
   1349 		/* Unload GL library after X11 shuts down */
   1350 	}
   1351 
   1352 	D(bug("Closing libraries...\n"));
   1353 
   1354 	if( CyberGfxBase) {
   1355 		CloseLibrary(CyberGfxBase);
   1356 		CyberGfxBase=NULL;
   1357 	}
   1358 
   1359 	if (IntuitionBase) {
   1360 		CloseLibrary((struct Library *)IntuitionBase);
   1361 		IntuitionBase=NULL;
   1362 	}
   1363 	if (GfxBase) {
   1364 		CloseLibrary((struct Library *)GfxBase);
   1365 		GfxBase=NULL;
   1366 	}
   1367 
   1368 	if ( this->screen && (this->screen->flags & SDL_HWSURFACE) ) {
   1369 		/* Direct screen access, no memory buffer */
   1370 		this->screen->pixels = NULL;
   1371 	}
   1372 	D(bug("End of CGX_VideoQuit.\n"));
   1373 
   1374 }
   1375 
   1376