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