src/video/macrom/SDL_romvideo.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 252 e8157fcb3114
child 769 b8d311d90021
permissions -rw-r--r--
Updated copyright information for 2002
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 
    31 #if TARGET_API_MAC_CARBON
    32 #include <Carbon.h>
    33 /* The fullscreen code requires the QuickTime framework, and the window
    34    is still at the back on MacOS X, which is where this code is needed.
    35  */
    36 #if USE_QUICKTIME
    37 #include <QuickTime/Movies.h>
    38 #endif
    39 #else
    40 #include <LowMem.h>
    41 #include <Gestalt.h>
    42 #include <Devices.h>
    43 #include <DiskInit.h>
    44 #include <QDOffscreen.h>
    45 #endif
    46 
    47 #include "SDL_video.h"
    48 #include "SDL_error.h"
    49 #include "SDL_syswm.h"
    50 #include "SDL_sysvideo.h"
    51 #include "SDL_romvideo.h"
    52 #include "SDL_macgl_c.h"
    53 #include "SDL_macwm_c.h"
    54 #include "SDL_macmouse_c.h"
    55 #include "SDL_macevents_c.h"
    56 
    57 /* Initialization/Query functions */
    58 static int ROM_VideoInit(_THIS, SDL_PixelFormat *vformat);
    59 static SDL_Rect **ROM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    60 static SDL_Surface *ROM_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    61 static int ROM_SetColors(_THIS, int firstcolor, int ncolors,
    62 			 SDL_Color *colors);
    63 static void ROM_VideoQuit(_THIS);
    64 
    65 /* Hardware surface functions */
    66 static int ROM_AllocHWSurface(_THIS, SDL_Surface *surface);
    67 static int ROM_LockHWSurface(_THIS, SDL_Surface *surface);
    68 static void ROM_UnlockHWSurface(_THIS, SDL_Surface *surface);
    69 static void ROM_FreeHWSurface(_THIS, SDL_Surface *surface);
    70 
    71 #if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
    72 /* Saved state for the menu bar */
    73 static RgnHandle	gSaveGrayRgn = nil;
    74 static short		gSaveMenuBar = 0;
    75 static Boolean		gSaveCSVis = true;
    76 
    77 #if powerc
    78 /* Mixed mode glue to activate the 68K emulator and twiddle a register */
    79 #define ONEWORDSTUB(p1) \
    80 		{ 0x41FA, 0x0010, 0x209F, (p1), 0x41FA, \
    81 		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
    82 
    83 #define TWOWORDSTUB(p1,p2) \
    84 		{ 0x41FA, 0x0012, 0x209F, (p1), (p2), 0x41FA, \
    85 		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
    86 
    87 #define THREEWORDSTUB(p1,p2,p3) \
    88 		{ 0x41FA, 0x0014, 0x209F, (p1), (p2), (p3), 0x41FA, \
    89 		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
    90 
    91 /* ControlStrip inline glue for PowerPC */
    92 static pascal Boolean SBIsControlStripVisible(void)
    93 {
    94 	static short procData[] = TWOWORDSTUB(0x7000, 0xAAF2);
    95 	ProcInfoType procInfo = kD0DispatchedPascalStackBased
    96 				| RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
    97             	| DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode);
    98             				
    99 	return((Boolean) CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x00));
   100 }
   101 
   102 static pascal void SBShowHideControlStrip(Boolean showIt)
   103 {
   104 	static short procData[] = THREEWORDSTUB(0x303C, 0x0101, 0xAAF2);
   105 	ProcInfoType procInfo = kD0DispatchedPascalStackBased
   106 				| DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode)
   107 				| DISPATCHED_STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(Boolean)));
   108 
   109 	CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x01, showIt);
   110 }
   111 #endif /* powerc */
   112 #endif /* !TARGET_API_MAC_CARBON */
   113 
   114 /* Macintosh toolbox driver bootstrap functions */
   115 
   116 static int ROM_Available(void)
   117 {
   118 	return(1);
   119 }
   120 
   121 static void ROM_DeleteDevice(SDL_VideoDevice *device)
   122 {
   123 	free(device->hidden);
   124 	free(device);
   125 }
   126 
   127 static SDL_VideoDevice *ROM_CreateDevice(int devindex)
   128 {
   129 	SDL_VideoDevice *device;
   130 
   131 	/* Initialize all variables that we clean on shutdown */
   132 	device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
   133 	if ( device ) {
   134 		memset(device, 0, (sizeof *device));
   135 		device->hidden = (struct SDL_PrivateVideoData *)
   136 				malloc((sizeof *device->hidden));
   137 	}
   138 	if ( (device == NULL) || (device->hidden == NULL) ) {
   139 		SDL_OutOfMemory();
   140 		if ( device ) {
   141 			free(device);
   142 		}
   143 		return(0);
   144 	}
   145 	memset(device->hidden, 0, (sizeof *device->hidden));
   146 
   147 	/* Set the function pointers */
   148 	device->VideoInit = ROM_VideoInit;
   149 	device->ListModes = ROM_ListModes;
   150 	device->SetVideoMode = ROM_SetVideoMode;
   151 	device->SetColors = ROM_SetColors;
   152 	device->UpdateRects = NULL;
   153 	device->VideoQuit = ROM_VideoQuit;
   154 	device->AllocHWSurface = ROM_AllocHWSurface;
   155 	device->CheckHWBlit = NULL;
   156 	device->FillHWRect = NULL;
   157 	device->SetHWColorKey = NULL;
   158 	device->SetHWAlpha = NULL;
   159 	device->LockHWSurface = ROM_LockHWSurface;
   160 	device->UnlockHWSurface = ROM_UnlockHWSurface;
   161 	device->FlipHWSurface = NULL;
   162 	device->FreeHWSurface = ROM_FreeHWSurface;
   163 #ifdef HAVE_OPENGL
   164 	device->GL_MakeCurrent = Mac_GL_MakeCurrent;
   165 	device->GL_SwapBuffers = Mac_GL_SwapBuffers;
   166 #endif
   167 	device->SetCaption = Mac_SetCaption;
   168 	device->SetIcon = NULL;
   169 	device->IconifyWindow = NULL;
   170 	device->GrabInput = NULL;
   171 	device->GetWMInfo = NULL;
   172 	device->FreeWMCursor = Mac_FreeWMCursor;
   173 	device->CreateWMCursor = Mac_CreateWMCursor;
   174 	device->ShowWMCursor = Mac_ShowWMCursor;
   175 	device->WarpWMCursor = Mac_WarpWMCursor;
   176 	device->InitOSKeymap = Mac_InitOSKeymap;
   177 	device->PumpEvents = Mac_PumpEvents;
   178 
   179 	device->free = ROM_DeleteDevice;
   180 
   181 	return device;
   182 }
   183 
   184 VideoBootStrap TOOLBOX_bootstrap = {
   185 	"toolbox", "MacOS ROM Toolbox",
   186 	ROM_Available, ROM_CreateDevice
   187 };
   188 
   189 
   190 static int ROM_VideoInit(_THIS, SDL_PixelFormat *vformat)
   191 {
   192 	long info;
   193 	
   194 	/* Check out some things about the system */
   195 	Gestalt(gestaltQuickdrawVersion, &info);
   196 	if ( info == gestaltOriginalQD ) {
   197 		SDL_SetError("Color Quickdraw not available");
   198 		return(-1);
   199 	}
   200 
   201 	/* Start ROMintosh events */
   202 	Mac_InitEvents(this);
   203 
   204 	/* Get a handle to the main monitor */
   205 	SDL_Display = GetMainDevice();
   206 
   207 	/* Determine pixel format */
   208 	vformat->BitsPerPixel = (**(**SDL_Display).gdPMap).pixelSize;
   209 	switch (vformat->BitsPerPixel) {
   210 		case 16:	/* 5-5-5 RGB */
   211 			vformat->Rmask = 0x00007c00;
   212 			vformat->Gmask = 0x000003e0;
   213 			vformat->Bmask = 0x0000001f;
   214 			break;
   215 		default:
   216 			break;
   217 	}
   218 
   219 	/* Create our palette */
   220 	SDL_CTab = (CTabHandle)NewHandle(sizeof(ColorSpec)*256 + 8);
   221 	if ( SDL_CTab == nil ) {
   222 		SDL_OutOfMemory();
   223 		return(-1);
   224 	}
   225 	(**SDL_CTab).ctSeed = GetCTSeed();
   226 	(**SDL_CTab).ctFlags = 0;
   227 	(**SDL_CTab).ctSize = 255;
   228 	CTabChanged(SDL_CTab);
   229 	SDL_CPal = NewPalette(256, SDL_CTab, pmExplicit+pmTolerant, 0);
   230 
   231 	/* Get a list of available fullscreen modes */
   232 	SDL_modelist = (SDL_Rect **)malloc((1+1)*sizeof(SDL_Rect *));
   233 	if ( SDL_modelist ) {
   234 		SDL_modelist[0] = (SDL_Rect *)malloc(sizeof(SDL_Rect));
   235 		if ( SDL_modelist[0] ) {
   236 			SDL_modelist[0]->x = 0;
   237 			SDL_modelist[0]->y = 0;
   238 			SDL_modelist[0]->w = (**SDL_Display).gdRect.right;
   239 			SDL_modelist[0]->h = (**SDL_Display).gdRect.bottom;
   240 		}
   241 		SDL_modelist[1] = NULL;
   242 	}
   243 
   244 	/* Fill in some window manager capabilities */
   245 	this->info.wm_available = 1;
   246 
   247 	/* We're done! */
   248 	return(0);
   249 }
   250 
   251 static SDL_Rect **ROM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   252 {
   253 	if ( this->screen->format->BitsPerPixel == format->BitsPerPixel ) {
   254 		if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   255 			return(SDL_modelist);
   256 		} else {
   257 			return((SDL_Rect **)-1);
   258 		}
   259 	} else {
   260 		return((SDL_Rect **)0);
   261 	}
   262 }
   263 
   264 static void ROM_HideMenuBar(_THIS)
   265 {
   266 #if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
   267 	RgnHandle		drawRgn = nil;
   268 	RgnHandle		tempRgn = nil;
   269 	RgnHandle		grayRgn = nil;
   270 	WindowPtr		window = nil;
   271 	GDHandle		gd = nil;
   272 	GrafPtr			savePort;
   273 	long			response;
   274 	short			height;
   275 	EventRecord		theEvent;
   276 
   277 	height = GetMBarHeight();
   278 	
   279 	if ( height > 0 ) {
   280 		tempRgn = NewRgn();
   281 		drawRgn = NewRgn();
   282 		gSaveGrayRgn = NewRgn();
   283 		if ( ! tempRgn || ! drawRgn || ! gSaveGrayRgn ) {
   284 			goto CLEANUP;
   285 		}
   286 		grayRgn = GetGrayRgn(); /* No need to check for this */
   287 	
   288 		GetPort(&savePort);
   289 
   290 		/* Hide the control strip if it's present, and record its 
   291 		   previous position into the dirty region for redrawing. 
   292 		   This isn't necessary, but may help catch stray bits. */
   293 		CopyRgn(grayRgn, tempRgn);
   294 		if (!Gestalt(gestaltControlStripAttr, &response) && 
   295 			(response & (1L << gestaltControlStripExists))) {
   296 			gSaveCSVis = SBIsControlStripVisible();
   297 			if (gSaveCSVis)
   298 				SBShowHideControlStrip(false);
   299 		}
   300 		DiffRgn(grayRgn, tempRgn, drawRgn);
   301 
   302 		/* Save the gray region once the control strip is hidden*/
   303 		CopyRgn(grayRgn, gSaveGrayRgn);
   304 
   305 		/* Change the menu height in lowmem */
   306 		gSaveMenuBar = height;
   307 		LMSetMBarHeight(0);
   308 		
   309 		/* Walk the monitor rectangles, and combine any pieces that
   310 		   aren't in GrayRgn: menubar, round corners, fake floaters. */
   311 		for(gd = GetDeviceList(); gd; gd = GetNextDevice(gd)) 
   312 			{
   313 			if (!TestDeviceAttribute(gd, screenDevice)) continue;
   314 			if (!TestDeviceAttribute(gd, screenActive)) continue;
   315 
   316 			RectRgn(tempRgn, &(*gd)->gdRect);	/* Get the whole screen */
   317 			DiffRgn(tempRgn, grayRgn, tempRgn); /* Subtract out GrayRgn */
   318 			UnionRgn(tempRgn, drawRgn, drawRgn);/* Combine all the bits */
   319 			}
   320 			
   321 		/* Add the bits into the GrayRgn */
   322 		UnionRgn(drawRgn, grayRgn, grayRgn);
   323 
   324 		/* Modify the vis regions of exposed windows */
   325 		window = (FrontWindow()) ? FrontWindow() : (WindowPtr) -1L;
   326 		PaintBehind(window, drawRgn);
   327 		CalcVisBehind(window, drawRgn);
   328 
   329 		SetPort(savePort);
   330 		
   331 		/* Yield time so that floaters can catch up */
   332 		EventAvail(0, &theEvent);
   333 		EventAvail(0, &theEvent);
   334 		EventAvail(0, &theEvent);
   335 		EventAvail(0, &theEvent);
   336 		}
   337 
   338 CLEANUP:
   339 
   340 	if (tempRgn) DisposeRgn(tempRgn);
   341 	if (drawRgn) DisposeRgn(drawRgn);
   342 #endif /* !TARGET_API_MAC_CARBON */
   343 }
   344 	
   345 static void ROM_ShowMenuBar(_THIS)
   346 {
   347 #if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
   348 	RgnHandle		drawRgn = nil;
   349 	RgnHandle		menuRgn = nil;
   350 	RgnHandle		tempRgn = nil;
   351 	RgnHandle		grayRgn = nil;
   352 	WindowPtr		window = nil;
   353 	GrafPtr			wMgrPort;
   354 	GrafPtr			savePort;
   355 	Rect			menuRect;
   356 	long			response;
   357 	short			height;
   358 	EventRecord		theEvent;
   359 	RGBColor		saveRGB;
   360 	RGBColor		blackRGB = { 0, 0, 0 };
   361 
   362 	height = GetMBarHeight();
   363 	
   364 	if ((height <= 0) && (gSaveMenuBar > 0)) {
   365 		drawRgn = NewRgn();
   366 		menuRgn = NewRgn();
   367 		tempRgn = NewRgn();
   368 		if ( ! tempRgn || ! drawRgn || ! gSaveGrayRgn ) {
   369 			goto CLEANUP;
   370 		}
   371 		grayRgn = GetGrayRgn(); /* No need to check for this */
   372 	
   373 		GetPort(&savePort);
   374 		GetWMgrPort(&wMgrPort);
   375 
   376 		/* Set the height properly */
   377 		LMSetMBarHeight(gSaveMenuBar);
   378 
   379 		/* Restore the old GrayRgn: rounded corners, etc, but not
   380 		   the menubar -- subtract that out first! */
   381 		if (gSaveGrayRgn)
   382 			{
   383 			menuRect = (*GetMainDevice())->gdRect;
   384 			menuRect.bottom = menuRect.top + gSaveMenuBar;
   385 			RectRgn(menuRgn, &menuRect);
   386 
   387 			DiffRgn(grayRgn, gSaveGrayRgn, drawRgn); 	/* What do we inval? */
   388 			DiffRgn(drawRgn, menuRgn, drawRgn);			/* Clip out the menu */
   389 			
   390 			/* Now redraw the corners and other bits black */
   391 			SetPort(wMgrPort);
   392 			GetClip(tempRgn);
   393 			SetClip(drawRgn);
   394 			GetForeColor(&saveRGB);
   395 			RGBForeColor(&blackRGB);
   396 			PaintRgn(drawRgn);
   397 			RGBForeColor(&saveRGB);
   398 			SetClip(tempRgn);
   399 			SetPort(savePort);
   400 			
   401 			UnionRgn(drawRgn, menuRgn, drawRgn);		/* Put back the menu */
   402 
   403 			/* Now actually restore the GrayRgn */
   404 			CopyRgn(gSaveGrayRgn, grayRgn);
   405 			DisposeRgn(gSaveGrayRgn);
   406 			gSaveGrayRgn = nil;
   407 			}
   408 
   409 		/* Modify the vis regions of exposed windows and draw menubar */
   410 		window = (FrontWindow()) ? FrontWindow() : (WindowPtr) -1L;
   411 		PaintBehind(window, drawRgn);
   412 		CalcVisBehind(window, drawRgn);
   413 		DrawMenuBar();
   414 
   415 		SetPort(savePort);
   416 		gSaveMenuBar = 0;
   417 
   418 		/* Now show the control strip if it's present */
   419 		if (!Gestalt(gestaltControlStripAttr, &response) && 
   420 				(response & (1L << gestaltControlStripExists)))
   421 			{
   422 			if (gSaveCSVis && !SBIsControlStripVisible())
   423 				SBShowHideControlStrip(true);
   424 			gSaveCSVis = true;
   425 			}
   426 
   427 		/* Yield time so that floaters can catch up */
   428 		EventAvail(0, &theEvent);
   429 		EventAvail(0, &theEvent);
   430 		EventAvail(0, &theEvent);
   431 		EventAvail(0, &theEvent);
   432 		}
   433 
   434 CLEANUP:
   435 
   436 	if (drawRgn) DisposeRgn(drawRgn);
   437 	if (menuRgn) DisposeRgn(menuRgn);
   438 	if (tempRgn) DisposeRgn(tempRgn);
   439 #endif /* !TARGET_API_MAC_CARBON */
   440 }
   441 
   442 /* Various screen update functions available */
   443 static void ROM_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
   444 static void ROM_WindowUpdate(_THIS, int numrects, SDL_Rect *rects);
   445 
   446 static void ROM_UnsetVideoMode(_THIS, SDL_Surface *current)
   447 {
   448 	/* Free the current window, if any */
   449 	if ( SDL_Window != nil ) {
   450 		GWorldPtr memworld;
   451 		
   452 		/* Handle OpenGL support */
   453 		Mac_GL_Quit(this);
   454 
   455 		memworld = (GWorldPtr)GetWRefCon(SDL_Window);
   456 		if ( memworld != nil ) {
   457 			UnlockPixels(GetGWorldPixMap(memworld));
   458 			DisposeGWorld(memworld);
   459 		}
   460 		if ( (current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   461 #if USE_QUICKTIME
   462 			EndFullScreen(fullscreen_ctx, nil);
   463 			SDL_Window = nil;
   464 #else
   465 			ROM_ShowMenuBar(this);
   466 #endif
   467 		}
   468 	}
   469 	current->pixels = NULL;
   470 	current->flags &= ~(SDL_HWSURFACE|SDL_FULLSCREEN);
   471 }
   472 
   473 static SDL_Surface *ROM_SetVideoMode(_THIS, SDL_Surface *current,
   474 				int width, int height, int bpp, Uint32 flags)
   475 {
   476 	Rect wrect, orect;
   477 #if TARGET_API_MAC_CARBON
   478 	Rect tmprect;
   479 #endif
   480 
   481 	/* Free any previous video mode */
   482 	ROM_UnsetVideoMode(this, current);
   483 
   484 	/* Create the ROM window and SDL video surface */
   485 	current->flags = 0;		/* Clear flags */
   486 	current->w = width;
   487 	current->h = height;
   488 	SetRect(&wrect, 0, 0, width, height);
   489 	if ( SDL_Window ) {
   490 		/* If we recreate the window, don't move it around */
   491 #if TARGET_API_MAC_CARBON
   492 		orect = *GetWindowPortBounds(SDL_Window, &tmprect);
   493 #else
   494 		orect = SDL_Window->portRect;
   495 #endif
   496 		OffsetRect(&wrect, orect.left, orect.top);
   497 	} else {
   498 		/* Center the window the first time we show it */
   499 		OffsetRect(&wrect,
   500 		(SDL_modelist[0]->w-width)/2, (SDL_modelist[0]->h-height)/2);
   501 	}
   502 
   503 #if MACOSX && !USE_QUICKTIME
   504 	/* Hum.. fullscreen mode is broken */
   505 	flags &= ~SDL_FULLSCREEN;
   506 #endif
   507 	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   508 		/* Create the fullscreen window and use screen bits */
   509 		current->flags |= SDL_HWSURFACE|SDL_FULLSCREEN;
   510 		if ( SDL_Window ) {
   511 			DisposeWindow(SDL_Window);
   512 		}
   513 #if USE_QUICKTIME
   514 		BeginFullScreen(&fullscreen_ctx, nil, 0,0, &SDL_Window, nil, 0);
   515 #else
   516 		SDL_Window = NewCWindow(nil, &wrect, "\p", true, plainDBox,
   517 						(WindowPtr)-1, false, 0);
   518 		ROM_HideMenuBar(this);
   519 #endif
   520 		current->pitch = (**(**SDL_Display).gdPMap).rowBytes & 0x3FFF;
   521 		current->pixels = (**(**SDL_Display).gdPMap).baseAddr;
   522 		this->UpdateRects = ROM_DirectUpdate;
   523 	} else {
   524 		GWorldPtr memworld;
   525 		PixMapHandle pixmap;
   526 		int style;
   527 
   528 		style = noGrowDocProc;
   529 		if ( flags & SDL_NOFRAME ) {
   530 			style = plainDBox;
   531 			current->flags |= SDL_NOFRAME;
   532 		} else
   533 		if ( flags & SDL_RESIZABLE ) {
   534 			style = zoomDocProc;
   535 			current->flags |= SDL_RESIZABLE;
   536 		}
   537 		if ( SDL_Window && (style == current_style) ) {
   538 			/* Resize existing window, if necessary */
   539 			if ( ((orect.right-orect.left) != width) ||
   540 			     ((orect.bottom-orect.top) != height) ) {
   541 				SizeWindow(SDL_Window, width, height, false);
   542 			}
   543 		} else {
   544 			/* Recreate the window in the new style */
   545 			if ( SDL_Window ) {
   546 				DisposeWindow(SDL_Window);
   547 			}
   548 			SDL_Window = NewCWindow(nil, &wrect, "\p", true,
   549 			                        style, (WindowPtr)-1, true, 0);
   550 
   551 			/* Set the window title, if any */
   552 			{ char *title;
   553 				SDL_WM_GetCaption(&title, NULL);
   554 				if ( title ) {
   555 					Mac_SetCaption(this, title, NULL);
   556 				}
   557 			}
   558 		}
   559 		current_style = style;
   560 		SetPalette(SDL_Window, SDL_CPal, false);
   561 		ActivatePalette(SDL_Window);
   562 		if ( NewGWorld(&memworld, 0,
   563 #if TARGET_API_MAC_CARBON
   564 			       GetWindowPortBounds(SDL_Window, &tmprect),
   565 #else
   566 			       &SDL_Window->portRect,
   567 #endif
   568 			       SDL_CTab, nil, 0) != noErr ) {
   569 			SDL_SetError("NewGWorld() failed");
   570 			return(NULL);
   571 		}
   572 		SetWRefCon(SDL_Window, (long)memworld);
   573 		pixmap = GetGWorldPixMap(memworld);
   574 		LockPixels(pixmap);
   575 		current->pitch = (**pixmap).rowBytes & 0x3FFF;
   576 		current->pixels = GetPixBaseAddr(pixmap);
   577 		this->UpdateRects = ROM_WindowUpdate;
   578 	}
   579 	SetPortWindowPort(SDL_Window);
   580 	SelectWindow(SDL_Window);
   581 
   582 	/* Handle OpenGL support */
   583 	if ( flags & SDL_OPENGL ) {
   584 		if ( Mac_GL_Init(this) == 0 ) {
   585 			current->flags |= SDL_OPENGL;
   586 		} else {
   587 			current = NULL;
   588 		}
   589 	}
   590 	
   591 	if ( (flags & SDL_HWPALETTE) && (flags & SDL_FULLSCREEN) )
   592 	   current->flags |= SDL_HWPALETTE;
   593 	   
   594 	/* We're live! */
   595 	return(current);
   596 }
   597 
   598 /* We don't actually allow hardware surfaces other than the main one */
   599 static int ROM_AllocHWSurface(_THIS, SDL_Surface *surface)
   600 {
   601 	return(-1);
   602 }
   603 static void ROM_FreeHWSurface(_THIS, SDL_Surface *surface)
   604 {
   605 	return;
   606 }
   607 static int ROM_LockHWSurface(_THIS, SDL_Surface *surface)
   608 {
   609 	return(0);
   610 }
   611 static void ROM_UnlockHWSurface(_THIS, SDL_Surface *surface)
   612 {
   613 	return;
   614 }
   615 
   616 static void ROM_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
   617 {
   618 	/* The application is already updating the visible video memory */
   619 	return;
   620 }
   621 
   622 static void ROM_WindowUpdate(_THIS, int numrects, SDL_Rect *rects)
   623 {
   624 	GWorldPtr memworld;
   625 	GrafPtr saveport;
   626 	CGrafPtr thePort;
   627 	const BitMap *memBits;
   628 	const BitMap *winBits;
   629 	int i;
   630 	Rect update;
   631 	
   632 	/* Copy from the offscreen GWorld to the window port */
   633 	GetPort(&saveport);
   634 	SetPortWindowPort(SDL_Window);
   635 	thePort = GetWindowPort(SDL_Window);
   636 	memworld = (GWorldPtr)GetWRefCon(SDL_Window);
   637 #if TARGET_API_MAC_CARBON
   638 	memBits = GetPortBitMapForCopyBits((CGrafPtr) memworld);
   639 #else
   640 	memBits = &((GrafPtr)memworld)->portBits;
   641 #endif
   642 #if TARGET_API_MAC_CARBON
   643 	winBits = GetPortBitMapForCopyBits(thePort);
   644 #else
   645 	winBits = &SDL_Window->portBits;
   646 #endif
   647 	for ( i=0; i<numrects; ++i ) {
   648 		update.left = rects[i].x;
   649 		update.right = rects[i].x+rects[i].w;
   650 		update.top = rects[i].y;
   651 		update.bottom = rects[i].y+rects[i].h;
   652 		CopyBits(memBits, winBits,
   653 			 &update, &update, srcCopy, nil);
   654 	}
   655 #if TARGET_API_MAC_CARBON
   656 	if ( QDIsPortBuffered(thePort) ) {
   657 		QDFlushPortBuffer(thePort, NULL);
   658 	}
   659 #endif
   660 	SetPort(saveport);
   661 }
   662 
   663 static int ROM_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   664 {
   665 	CTabHandle cTab;
   666 	int i;
   667 
   668 	/* Get the colortable from the either the display or window */
   669 	if ( (this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   670 		cTab = (**(**SDL_Display).gdPMap).pmTable;
   671 	} else {
   672 		cTab = SDL_CTab;
   673 	}
   674 
   675 	/* Verify the range of colors */
   676 	if ( (firstcolor+ncolors) > ((**cTab).ctSize+1) ) {
   677 		return(0);
   678 	}
   679 	
   680 	/* Set the screen palette and update the display */
   681 	for ( i=0; i< ncolors; ++i ) {
   682 	        int j = firstcolor + i;
   683 		(**cTab).ctTable[j].value = j;
   684 		(**cTab).ctTable[j].rgb.red = colors[i].r << 8 | colors[i].r;
   685 		(**cTab).ctTable[j].rgb.green = colors[i].g << 8 | colors[i].g;
   686 		(**cTab).ctTable[j].rgb.blue = colors[i].b << 8 | colors[i].b;
   687 	}
   688 //	if ( (this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) 
   689 {
   690 		GDevice **odisplay;
   691 		odisplay = GetGDevice();
   692 		SetGDevice(SDL_Display);
   693 		SetEntries(0, (**cTab).ctSize, (ColorSpec *)&(**cTab).ctTable);
   694 		SetGDevice(odisplay);
   695 	}
   696 	return(1);
   697 }
   698 
   699 void ROM_VideoQuit(_THIS)
   700 {
   701 	int i;
   702 
   703 	/* Free current video mode */
   704 	ROM_UnsetVideoMode(this, this->screen);
   705 	if ( SDL_Window ) {
   706 		DisposeWindow(SDL_Window);
   707 		SDL_Window = nil;
   708 	}
   709 
   710 	/* Free palette and restore original one */
   711 	if ( SDL_CTab != nil ) {
   712 		DisposeHandle((Handle)SDL_CTab);
   713 		SDL_CTab = nil;
   714 	}
   715 	if ( SDL_CPal != nil ) {
   716 		DisposePalette(SDL_CPal);
   717 		SDL_CPal = nil;
   718 	}
   719 	RestoreDeviceClut(GetMainDevice());
   720 
   721 	/* Free list of video modes */
   722 	if ( SDL_modelist != NULL ) {
   723 		for ( i=0; SDL_modelist[i]; ++i ) {
   724 			free(SDL_modelist[i]);
   725 		}
   726 		free(SDL_modelist);
   727 		SDL_modelist = NULL;
   728 	}
   729 }
   730