src/video/macrom/SDL_romvideo.c
changeset 0 74212992fb08
child 47 45b1c4303f87
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/macrom/SDL_romvideo.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,730 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +#include <stdio.h>
    1.32 +#include <stdlib.h>
    1.33 +
    1.34 +#if TARGET_API_MAC_CARBON
    1.35 +#include <Carbon.h>
    1.36 +/* The fullscreen code requires the QuickTime framework, and the window
    1.37 +   is still at the back on MacOS X, which is where this code is needed.
    1.38 + */
    1.39 +#if USE_QUICKTIME
    1.40 +#include <Movies.h>
    1.41 +#endif
    1.42 +#else
    1.43 +#include <LowMem.h>
    1.44 +#include <Gestalt.h>
    1.45 +#include <Devices.h>
    1.46 +#include <DiskInit.h>
    1.47 +#include <QDOffscreen.h>
    1.48 +#endif
    1.49 +
    1.50 +#include "SDL_video.h"
    1.51 +#include "SDL_error.h"
    1.52 +#include "SDL_syswm.h"
    1.53 +#include "SDL_sysvideo.h"
    1.54 +#include "SDL_romvideo.h"
    1.55 +#include "SDL_macgl_c.h"
    1.56 +#include "SDL_macwm_c.h"
    1.57 +#include "SDL_macmouse_c.h"
    1.58 +#include "SDL_macevents_c.h"
    1.59 +
    1.60 +/* Initialization/Query functions */
    1.61 +static int ROM_VideoInit(_THIS, SDL_PixelFormat *vformat);
    1.62 +static SDL_Rect **ROM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    1.63 +static SDL_Surface *ROM_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    1.64 +static int ROM_SetColors(_THIS, int firstcolor, int ncolors,
    1.65 +			 SDL_Color *colors);
    1.66 +static void ROM_VideoQuit(_THIS);
    1.67 +
    1.68 +/* Hardware surface functions */
    1.69 +static int ROM_AllocHWSurface(_THIS, SDL_Surface *surface);
    1.70 +static int ROM_LockHWSurface(_THIS, SDL_Surface *surface);
    1.71 +static void ROM_UnlockHWSurface(_THIS, SDL_Surface *surface);
    1.72 +static void ROM_FreeHWSurface(_THIS, SDL_Surface *surface);
    1.73 +
    1.74 +#if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
    1.75 +/* Saved state for the menu bar */
    1.76 +static RgnHandle	gSaveGrayRgn = nil;
    1.77 +static short		gSaveMenuBar = 0;
    1.78 +static Boolean		gSaveCSVis = true;
    1.79 +
    1.80 +#if powerc
    1.81 +/* Mixed mode glue to activate the 68K emulator and twiddle a register */
    1.82 +#define ONEWORDSTUB(p1) \
    1.83 +		{ 0x41FA, 0x0010, 0x209F, (p1), 0x41FA, \
    1.84 +		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
    1.85 +
    1.86 +#define TWOWORDSTUB(p1,p2) \
    1.87 +		{ 0x41FA, 0x0012, 0x209F, (p1), (p2), 0x41FA, \
    1.88 +		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
    1.89 +
    1.90 +#define THREEWORDSTUB(p1,p2,p3) \
    1.91 +		{ 0x41FA, 0x0014, 0x209F, (p1), (p2), (p3), 0x41FA, \
    1.92 +		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
    1.93 +
    1.94 +/* ControlStrip inline glue for PowerPC */
    1.95 +static pascal Boolean SBIsControlStripVisible(void)
    1.96 +{
    1.97 +	static short procData[] = TWOWORDSTUB(0x7000, 0xAAF2);
    1.98 +	ProcInfoType procInfo = kD0DispatchedPascalStackBased
    1.99 +				| RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
   1.100 +            	| DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode);
   1.101 +            				
   1.102 +	return((Boolean) CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x00));
   1.103 +}
   1.104 +
   1.105 +static pascal void SBShowHideControlStrip(Boolean showIt)
   1.106 +{
   1.107 +	static short procData[] = THREEWORDSTUB(0x303C, 0x0101, 0xAAF2);
   1.108 +	ProcInfoType procInfo = kD0DispatchedPascalStackBased
   1.109 +				| DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode)
   1.110 +				| DISPATCHED_STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(Boolean)));
   1.111 +
   1.112 +	CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x01, showIt);
   1.113 +}
   1.114 +#endif /* powerc */
   1.115 +#endif /* !TARGET_API_MAC_CARBON */
   1.116 +
   1.117 +/* Macintosh toolbox driver bootstrap functions */
   1.118 +
   1.119 +static int ROM_Available(void)
   1.120 +{
   1.121 +	return(1);
   1.122 +}
   1.123 +
   1.124 +static void ROM_DeleteDevice(SDL_VideoDevice *device)
   1.125 +{
   1.126 +	free(device->hidden);
   1.127 +	free(device);
   1.128 +}
   1.129 +
   1.130 +static SDL_VideoDevice *ROM_CreateDevice(int devindex)
   1.131 +{
   1.132 +	SDL_VideoDevice *device;
   1.133 +
   1.134 +	/* Initialize all variables that we clean on shutdown */
   1.135 +	device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
   1.136 +	if ( device ) {
   1.137 +		memset(device, 0, (sizeof *device));
   1.138 +		device->hidden = (struct SDL_PrivateVideoData *)
   1.139 +				malloc((sizeof *device->hidden));
   1.140 +	}
   1.141 +	if ( (device == NULL) || (device->hidden == NULL) ) {
   1.142 +		SDL_OutOfMemory();
   1.143 +		if ( device ) {
   1.144 +			free(device);
   1.145 +		}
   1.146 +		return(0);
   1.147 +	}
   1.148 +	memset(device->hidden, 0, (sizeof *device->hidden));
   1.149 +
   1.150 +	/* Set the function pointers */
   1.151 +	device->VideoInit = ROM_VideoInit;
   1.152 +	device->ListModes = ROM_ListModes;
   1.153 +	device->SetVideoMode = ROM_SetVideoMode;
   1.154 +	device->SetColors = ROM_SetColors;
   1.155 +	device->UpdateRects = NULL;
   1.156 +	device->VideoQuit = ROM_VideoQuit;
   1.157 +	device->AllocHWSurface = ROM_AllocHWSurface;
   1.158 +	device->CheckHWBlit = NULL;
   1.159 +	device->FillHWRect = NULL;
   1.160 +	device->SetHWColorKey = NULL;
   1.161 +	device->SetHWAlpha = NULL;
   1.162 +	device->LockHWSurface = ROM_LockHWSurface;
   1.163 +	device->UnlockHWSurface = ROM_UnlockHWSurface;
   1.164 +	device->FlipHWSurface = NULL;
   1.165 +	device->FreeHWSurface = ROM_FreeHWSurface;
   1.166 +#ifdef HAVE_OPENGL
   1.167 +	device->GL_MakeCurrent = Mac_GL_MakeCurrent;
   1.168 +	device->GL_SwapBuffers = Mac_GL_SwapBuffers;
   1.169 +#endif
   1.170 +	device->SetCaption = Mac_SetCaption;
   1.171 +	device->SetIcon = NULL;
   1.172 +	device->IconifyWindow = NULL;
   1.173 +	device->GrabInput = NULL;
   1.174 +	device->GetWMInfo = NULL;
   1.175 +	device->FreeWMCursor = Mac_FreeWMCursor;
   1.176 +	device->CreateWMCursor = Mac_CreateWMCursor;
   1.177 +	device->ShowWMCursor = Mac_ShowWMCursor;
   1.178 +	device->WarpWMCursor = Mac_WarpWMCursor;
   1.179 +	device->InitOSKeymap = Mac_InitOSKeymap;
   1.180 +	device->PumpEvents = Mac_PumpEvents;
   1.181 +
   1.182 +	device->free = ROM_DeleteDevice;
   1.183 +
   1.184 +	return device;
   1.185 +}
   1.186 +
   1.187 +VideoBootStrap TOOLBOX_bootstrap = {
   1.188 +	"toolbox", "MacOS ROM Toolbox",
   1.189 +	ROM_Available, ROM_CreateDevice
   1.190 +};
   1.191 +
   1.192 +
   1.193 +static int ROM_VideoInit(_THIS, SDL_PixelFormat *vformat)
   1.194 +{
   1.195 +	long info;
   1.196 +	
   1.197 +	/* Check out some things about the system */
   1.198 +	Gestalt(gestaltQuickdrawVersion, &info);
   1.199 +	if ( info == gestaltOriginalQD ) {
   1.200 +		SDL_SetError("Color Quickdraw not available");
   1.201 +		return(-1);
   1.202 +	}
   1.203 +
   1.204 +	/* Start ROMintosh events */
   1.205 +	Mac_InitEvents(this);
   1.206 +
   1.207 +	/* Get a handle to the main monitor */
   1.208 +	SDL_Display = GetMainDevice();
   1.209 +
   1.210 +	/* Determine pixel format */
   1.211 +	vformat->BitsPerPixel = (**(**SDL_Display).gdPMap).pixelSize;
   1.212 +	switch (vformat->BitsPerPixel) {
   1.213 +		case 16:	/* 5-5-5 RGB */
   1.214 +			vformat->Rmask = 0x00007c00;
   1.215 +			vformat->Gmask = 0x000003e0;
   1.216 +			vformat->Bmask = 0x0000001f;
   1.217 +			break;
   1.218 +		default:
   1.219 +			break;
   1.220 +	}
   1.221 +
   1.222 +	/* Create our palette */
   1.223 +	SDL_CTab = (CTabHandle)NewHandle(sizeof(ColorSpec)*256 + 8);
   1.224 +	if ( SDL_CTab == nil ) {
   1.225 +		SDL_OutOfMemory();
   1.226 +		return(-1);
   1.227 +	}
   1.228 +	(**SDL_CTab).ctSeed = GetCTSeed();
   1.229 +	(**SDL_CTab).ctFlags = 0;
   1.230 +	(**SDL_CTab).ctSize = 255;
   1.231 +	CTabChanged(SDL_CTab);
   1.232 +	SDL_CPal = NewPalette(256, SDL_CTab, pmExplicit+pmTolerant, 0);
   1.233 +
   1.234 +	/* Get a list of available fullscreen modes */
   1.235 +	SDL_modelist = (SDL_Rect **)malloc((1+1)*sizeof(SDL_Rect *));
   1.236 +	if ( SDL_modelist ) {
   1.237 +		SDL_modelist[0] = (SDL_Rect *)malloc(sizeof(SDL_Rect));
   1.238 +		if ( SDL_modelist[0] ) {
   1.239 +			SDL_modelist[0]->x = 0;
   1.240 +			SDL_modelist[0]->y = 0;
   1.241 +			SDL_modelist[0]->w = (**SDL_Display).gdRect.right;
   1.242 +			SDL_modelist[0]->h = (**SDL_Display).gdRect.bottom;
   1.243 +		}
   1.244 +		SDL_modelist[1] = NULL;
   1.245 +	}
   1.246 +
   1.247 +	/* Fill in some window manager capabilities */
   1.248 +	this->info.wm_available = 1;
   1.249 +
   1.250 +	/* We're done! */
   1.251 +	return(0);
   1.252 +}
   1.253 +
   1.254 +static SDL_Rect **ROM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   1.255 +{
   1.256 +	if ( this->screen->format->BitsPerPixel == format->BitsPerPixel ) {
   1.257 +		if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   1.258 +			return(SDL_modelist);
   1.259 +		} else {
   1.260 +			return((SDL_Rect **)-1);
   1.261 +		}
   1.262 +	} else {
   1.263 +		return((SDL_Rect **)0);
   1.264 +	}
   1.265 +}
   1.266 +
   1.267 +static void ROM_HideMenuBar(_THIS)
   1.268 +{
   1.269 +#if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
   1.270 +	RgnHandle		drawRgn = nil;
   1.271 +	RgnHandle		tempRgn = nil;
   1.272 +	RgnHandle		grayRgn = nil;
   1.273 +	WindowPtr		window = nil;
   1.274 +	GDHandle		gd = nil;
   1.275 +	GrafPtr			savePort;
   1.276 +	long			response;
   1.277 +	short			height;
   1.278 +	EventRecord		theEvent;
   1.279 +
   1.280 +	height = GetMBarHeight();
   1.281 +	
   1.282 +	if ( height > 0 ) {
   1.283 +		tempRgn = NewRgn();
   1.284 +		drawRgn = NewRgn();
   1.285 +		gSaveGrayRgn = NewRgn();
   1.286 +		if ( ! tempRgn || ! drawRgn || ! gSaveGrayRgn ) {
   1.287 +			goto CLEANUP;
   1.288 +		}
   1.289 +		grayRgn = GetGrayRgn(); /* No need to check for this */
   1.290 +	
   1.291 +		GetPort(&savePort);
   1.292 +
   1.293 +		/* Hide the control strip if it's present, and record its 
   1.294 +		   previous position into the dirty region for redrawing. 
   1.295 +		   This isn't necessary, but may help catch stray bits. */
   1.296 +		CopyRgn(grayRgn, tempRgn);
   1.297 +		if (!Gestalt(gestaltControlStripAttr, &response) && 
   1.298 +			(response & (1L << gestaltControlStripExists))) {
   1.299 +			gSaveCSVis = SBIsControlStripVisible();
   1.300 +			if (gSaveCSVis)
   1.301 +				SBShowHideControlStrip(false);
   1.302 +		}
   1.303 +		DiffRgn(grayRgn, tempRgn, drawRgn);
   1.304 +
   1.305 +		/* Save the gray region once the control strip is hidden*/
   1.306 +		CopyRgn(grayRgn, gSaveGrayRgn);
   1.307 +
   1.308 +		/* Change the menu height in lowmem */
   1.309 +		gSaveMenuBar = height;
   1.310 +		LMSetMBarHeight(0);
   1.311 +		
   1.312 +		/* Walk the monitor rectangles, and combine any pieces that
   1.313 +		   aren't in GrayRgn: menubar, round corners, fake floaters. */
   1.314 +		for(gd = GetDeviceList(); gd; gd = GetNextDevice(gd)) 
   1.315 +			{
   1.316 +			if (!TestDeviceAttribute(gd, screenDevice)) continue;
   1.317 +			if (!TestDeviceAttribute(gd, screenActive)) continue;
   1.318 +
   1.319 +			RectRgn(tempRgn, &(*gd)->gdRect);	/* Get the whole screen */
   1.320 +			DiffRgn(tempRgn, grayRgn, tempRgn); /* Subtract out GrayRgn */
   1.321 +			UnionRgn(tempRgn, drawRgn, drawRgn);/* Combine all the bits */
   1.322 +			}
   1.323 +			
   1.324 +		/* Add the bits into the GrayRgn */
   1.325 +		UnionRgn(drawRgn, grayRgn, grayRgn);
   1.326 +
   1.327 +		/* Modify the vis regions of exposed windows */
   1.328 +		window = (FrontWindow()) ? FrontWindow() : (WindowPtr) -1L;
   1.329 +		PaintBehind(window, drawRgn);
   1.330 +		CalcVisBehind(window, drawRgn);
   1.331 +
   1.332 +		SetPort(savePort);
   1.333 +		
   1.334 +		/* Yield time so that floaters can catch up */
   1.335 +		EventAvail(0, &theEvent);
   1.336 +		EventAvail(0, &theEvent);
   1.337 +		EventAvail(0, &theEvent);
   1.338 +		EventAvail(0, &theEvent);
   1.339 +		}
   1.340 +
   1.341 +CLEANUP:
   1.342 +
   1.343 +	if (tempRgn) DisposeRgn(tempRgn);
   1.344 +	if (drawRgn) DisposeRgn(drawRgn);
   1.345 +#endif /* !TARGET_API_MAC_CARBON */
   1.346 +}
   1.347 +	
   1.348 +static void ROM_ShowMenuBar(_THIS)
   1.349 +{
   1.350 +#if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
   1.351 +	RgnHandle		drawRgn = nil;
   1.352 +	RgnHandle		menuRgn = nil;
   1.353 +	RgnHandle		tempRgn = nil;
   1.354 +	RgnHandle		grayRgn = nil;
   1.355 +	WindowPtr		window = nil;
   1.356 +	GrafPtr			wMgrPort;
   1.357 +	GrafPtr			savePort;
   1.358 +	Rect			menuRect;
   1.359 +	long			response;
   1.360 +	short			height;
   1.361 +	EventRecord		theEvent;
   1.362 +	RGBColor		saveRGB;
   1.363 +	RGBColor		blackRGB = { 0, 0, 0 };
   1.364 +
   1.365 +	height = GetMBarHeight();
   1.366 +	
   1.367 +	if ((height <= 0) && (gSaveMenuBar > 0)) {
   1.368 +		drawRgn = NewRgn();
   1.369 +		menuRgn = NewRgn();
   1.370 +		tempRgn = NewRgn();
   1.371 +		if ( ! tempRgn || ! drawRgn || ! gSaveGrayRgn ) {
   1.372 +			goto CLEANUP;
   1.373 +		}
   1.374 +		grayRgn = GetGrayRgn(); /* No need to check for this */
   1.375 +	
   1.376 +		GetPort(&savePort);
   1.377 +		GetWMgrPort(&wMgrPort);
   1.378 +
   1.379 +		/* Set the height properly */
   1.380 +		LMSetMBarHeight(gSaveMenuBar);
   1.381 +
   1.382 +		/* Restore the old GrayRgn: rounded corners, etc, but not
   1.383 +		   the menubar -- subtract that out first! */
   1.384 +		if (gSaveGrayRgn)
   1.385 +			{
   1.386 +			menuRect = (*GetMainDevice())->gdRect;
   1.387 +			menuRect.bottom = menuRect.top + gSaveMenuBar;
   1.388 +			RectRgn(menuRgn, &menuRect);
   1.389 +
   1.390 +			DiffRgn(grayRgn, gSaveGrayRgn, drawRgn); 	/* What do we inval? */
   1.391 +			DiffRgn(drawRgn, menuRgn, drawRgn);			/* Clip out the menu */
   1.392 +			
   1.393 +			/* Now redraw the corners and other bits black */
   1.394 +			SetPort(wMgrPort);
   1.395 +			GetClip(tempRgn);
   1.396 +			SetClip(drawRgn);
   1.397 +			GetForeColor(&saveRGB);
   1.398 +			RGBForeColor(&blackRGB);
   1.399 +			PaintRgn(drawRgn);
   1.400 +			RGBForeColor(&saveRGB);
   1.401 +			SetClip(tempRgn);
   1.402 +			SetPort(savePort);
   1.403 +			
   1.404 +			UnionRgn(drawRgn, menuRgn, drawRgn);		/* Put back the menu */
   1.405 +
   1.406 +			/* Now actually restore the GrayRgn */
   1.407 +			CopyRgn(gSaveGrayRgn, grayRgn);
   1.408 +			DisposeRgn(gSaveGrayRgn);
   1.409 +			gSaveGrayRgn = nil;
   1.410 +			}
   1.411 +
   1.412 +		/* Modify the vis regions of exposed windows and draw menubar */
   1.413 +		window = (FrontWindow()) ? FrontWindow() : (WindowPtr) -1L;
   1.414 +		PaintBehind(window, drawRgn);
   1.415 +		CalcVisBehind(window, drawRgn);
   1.416 +		DrawMenuBar();
   1.417 +
   1.418 +		SetPort(savePort);
   1.419 +		gSaveMenuBar = 0;
   1.420 +
   1.421 +		/* Now show the control strip if it's present */
   1.422 +		if (!Gestalt(gestaltControlStripAttr, &response) && 
   1.423 +				(response & (1L << gestaltControlStripExists)))
   1.424 +			{
   1.425 +			if (gSaveCSVis && !SBIsControlStripVisible())
   1.426 +				SBShowHideControlStrip(true);
   1.427 +			gSaveCSVis = true;
   1.428 +			}
   1.429 +
   1.430 +		/* Yield time so that floaters can catch up */
   1.431 +		EventAvail(0, &theEvent);
   1.432 +		EventAvail(0, &theEvent);
   1.433 +		EventAvail(0, &theEvent);
   1.434 +		EventAvail(0, &theEvent);
   1.435 +		}
   1.436 +
   1.437 +CLEANUP:
   1.438 +
   1.439 +	if (drawRgn) DisposeRgn(drawRgn);
   1.440 +	if (menuRgn) DisposeRgn(menuRgn);
   1.441 +	if (tempRgn) DisposeRgn(tempRgn);
   1.442 +#endif /* !TARGET_API_MAC_CARBON */
   1.443 +}
   1.444 +
   1.445 +/* Various screen update functions available */
   1.446 +static void ROM_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
   1.447 +static void ROM_WindowUpdate(_THIS, int numrects, SDL_Rect *rects);
   1.448 +
   1.449 +static void ROM_UnsetVideoMode(_THIS, SDL_Surface *current)
   1.450 +{
   1.451 +	/* Free the current window, if any */
   1.452 +	if ( SDL_Window != nil ) {
   1.453 +		GWorldPtr memworld;
   1.454 +		
   1.455 +		/* Handle OpenGL support */
   1.456 +		Mac_GL_Quit(this);
   1.457 +
   1.458 +		memworld = (GWorldPtr)GetWRefCon(SDL_Window);
   1.459 +		if ( memworld != nil ) {
   1.460 +			UnlockPixels(GetGWorldPixMap(memworld));
   1.461 +			DisposeGWorld(memworld);
   1.462 +		}
   1.463 +		if ( (current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   1.464 +#if USE_QUICKTIME
   1.465 +			EndFullScreen(fullscreen_ctx, nil);
   1.466 +			SDL_Window = nil;
   1.467 +#else
   1.468 +			ROM_ShowMenuBar(this);
   1.469 +#endif
   1.470 +		}
   1.471 +	}
   1.472 +	current->pixels = NULL;
   1.473 +	current->flags &= ~(SDL_HWSURFACE|SDL_FULLSCREEN);
   1.474 +}
   1.475 +
   1.476 +static SDL_Surface *ROM_SetVideoMode(_THIS, SDL_Surface *current,
   1.477 +				int width, int height, int bpp, Uint32 flags)
   1.478 +{
   1.479 +	Rect wrect, orect;
   1.480 +#if TARGET_API_MAC_CARBON
   1.481 +	Rect tmprect;
   1.482 +#endif
   1.483 +
   1.484 +	/* Free any previous video mode */
   1.485 +	ROM_UnsetVideoMode(this, current);
   1.486 +
   1.487 +	/* Create the ROM window and SDL video surface */
   1.488 +	current->flags = 0;		/* Clear flags */
   1.489 +	current->w = width;
   1.490 +	current->h = height;
   1.491 +	SetRect(&wrect, 0, 0, width, height);
   1.492 +	if ( SDL_Window ) {
   1.493 +		/* If we recreate the window, don't move it around */
   1.494 +#if TARGET_API_MAC_CARBON
   1.495 +		orect = *GetWindowPortBounds(SDL_Window, &tmprect);
   1.496 +#else
   1.497 +		orect = SDL_Window->portRect;
   1.498 +#endif
   1.499 +		OffsetRect(&wrect, orect.left, orect.top);
   1.500 +	} else {
   1.501 +		/* Center the window the first time we show it */
   1.502 +		OffsetRect(&wrect,
   1.503 +		(SDL_modelist[0]->w-width)/2, (SDL_modelist[0]->h-height)/2);
   1.504 +	}
   1.505 +
   1.506 +#if MACOSX && !USE_QUICKTIME
   1.507 +	/* Hum.. fullscreen mode is broken */
   1.508 +	flags &= ~SDL_FULLSCREEN;
   1.509 +#endif
   1.510 +	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   1.511 +		/* Create the fullscreen window and use screen bits */
   1.512 +		current->flags |= SDL_HWSURFACE|SDL_FULLSCREEN;
   1.513 +		if ( SDL_Window ) {
   1.514 +			DisposeWindow(SDL_Window);
   1.515 +		}
   1.516 +#if USE_QUICKTIME
   1.517 +		BeginFullScreen(&fullscreen_ctx, nil, 0,0, &SDL_Window, nil, 0);
   1.518 +#else
   1.519 +		SDL_Window = NewCWindow(nil, &wrect, "\p", true, plainDBox,
   1.520 +						(WindowPtr)-1, false, 0);
   1.521 +		ROM_HideMenuBar(this);
   1.522 +#endif
   1.523 +		current->pitch = (**(**SDL_Display).gdPMap).rowBytes & 0x3FFF;
   1.524 +		current->pixels = (**(**SDL_Display).gdPMap).baseAddr;
   1.525 +		this->UpdateRects = ROM_DirectUpdate;
   1.526 +	} else {
   1.527 +		GWorldPtr memworld;
   1.528 +		PixMapHandle pixmap;
   1.529 +		int style;
   1.530 +
   1.531 +		style = noGrowDocProc;
   1.532 +		if ( flags & SDL_NOFRAME ) {
   1.533 +			style = plainDBox;
   1.534 +			current->flags |= SDL_NOFRAME;
   1.535 +		} else
   1.536 +		if ( flags & SDL_RESIZABLE ) {
   1.537 +			style = zoomDocProc;
   1.538 +			current->flags |= SDL_RESIZABLE;
   1.539 +		}
   1.540 +		if ( SDL_Window && (style == current_style) ) {
   1.541 +			/* Resize existing window, if necessary */
   1.542 +			if ( ((orect.right-orect.left) != width) ||
   1.543 +			     ((orect.bottom-orect.top) != height) ) {
   1.544 +				SizeWindow(SDL_Window, width, height, false);
   1.545 +			}
   1.546 +		} else {
   1.547 +			/* Recreate the window in the new style */
   1.548 +			if ( SDL_Window ) {
   1.549 +				DisposeWindow(SDL_Window);
   1.550 +			}
   1.551 +			SDL_Window = NewCWindow(nil, &wrect, "\p", true,
   1.552 +			                        style, (WindowPtr)-1, true, 0);
   1.553 +
   1.554 +			/* Set the window title, if any */
   1.555 +			{ char *title;
   1.556 +				SDL_WM_GetCaption(&title, NULL);
   1.557 +				if ( title ) {
   1.558 +					Mac_SetCaption(this, title, NULL);
   1.559 +				}
   1.560 +			}
   1.561 +		}
   1.562 +		current_style = style;
   1.563 +		SetPalette(SDL_Window, SDL_CPal, false);
   1.564 +		ActivatePalette(SDL_Window);
   1.565 +		if ( NewGWorld(&memworld, 0,
   1.566 +#if TARGET_API_MAC_CARBON
   1.567 +			       GetWindowPortBounds(SDL_Window, &tmprect),
   1.568 +#else
   1.569 +			       &SDL_Window->portRect,
   1.570 +#endif
   1.571 +			       SDL_CTab, nil, 0) != noErr ) {
   1.572 +			SDL_SetError("NewGWorld() failed");
   1.573 +			return(NULL);
   1.574 +		}
   1.575 +		SetWRefCon(SDL_Window, (long)memworld);
   1.576 +		pixmap = GetGWorldPixMap(memworld);
   1.577 +		LockPixels(pixmap);
   1.578 +		current->pitch = (**pixmap).rowBytes & 0x3FFF;
   1.579 +		current->pixels = GetPixBaseAddr(pixmap);
   1.580 +		this->UpdateRects = ROM_WindowUpdate;
   1.581 +	}
   1.582 +	SetPortWindowPort(SDL_Window);
   1.583 +	SelectWindow(SDL_Window);
   1.584 +
   1.585 +	/* Handle OpenGL support */
   1.586 +	if ( flags & SDL_OPENGL ) {
   1.587 +		if ( Mac_GL_Init(this) == 0 ) {
   1.588 +			current->flags |= SDL_OPENGL;
   1.589 +		} else {
   1.590 +			current = NULL;
   1.591 +		}
   1.592 +	}
   1.593 +	
   1.594 +	if ( (flags & SDL_HWPALETTE) && (flags & SDL_FULLSCREEN) )
   1.595 +	   current->flags |= SDL_HWPALETTE;
   1.596 +	   
   1.597 +	/* We're live! */
   1.598 +	return(current);
   1.599 +}
   1.600 +
   1.601 +/* We don't actually allow hardware surfaces other than the main one */
   1.602 +static int ROM_AllocHWSurface(_THIS, SDL_Surface *surface)
   1.603 +{
   1.604 +	return(-1);
   1.605 +}
   1.606 +static void ROM_FreeHWSurface(_THIS, SDL_Surface *surface)
   1.607 +{
   1.608 +	return;
   1.609 +}
   1.610 +static int ROM_LockHWSurface(_THIS, SDL_Surface *surface)
   1.611 +{
   1.612 +	return(0);
   1.613 +}
   1.614 +static void ROM_UnlockHWSurface(_THIS, SDL_Surface *surface)
   1.615 +{
   1.616 +	return;
   1.617 +}
   1.618 +
   1.619 +static void ROM_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
   1.620 +{
   1.621 +	/* The application is already updating the visible video memory */
   1.622 +	return;
   1.623 +}
   1.624 +
   1.625 +static void ROM_WindowUpdate(_THIS, int numrects, SDL_Rect *rects)
   1.626 +{
   1.627 +	GWorldPtr memworld;
   1.628 +	GrafPtr saveport;
   1.629 +	CGrafPtr thePort;
   1.630 +	const BitMap *memBits;
   1.631 +	const BitMap *winBits;
   1.632 +	int i;
   1.633 +	Rect update;
   1.634 +	
   1.635 +	/* Copy from the offscreen GWorld to the window port */
   1.636 +	GetPort(&saveport);
   1.637 +	SetPortWindowPort(SDL_Window);
   1.638 +	thePort = GetWindowPort(SDL_Window);
   1.639 +	memworld = (GWorldPtr)GetWRefCon(SDL_Window);
   1.640 +#if TARGET_API_MAC_CARBON
   1.641 +	memBits = GetPortBitMapForCopyBits((CGrafPtr) memworld);
   1.642 +#else
   1.643 +	memBits = &((GrafPtr)memworld)->portBits;
   1.644 +#endif
   1.645 +#if TARGET_API_MAC_CARBON
   1.646 +	winBits = GetPortBitMapForCopyBits(thePort);
   1.647 +#else
   1.648 +	winBits = &SDL_Window->portBits;
   1.649 +#endif
   1.650 +	for ( i=0; i<numrects; ++i ) {
   1.651 +		update.left = rects[i].x;
   1.652 +		update.right = rects[i].x+rects[i].w;
   1.653 +		update.top = rects[i].y;
   1.654 +		update.bottom = rects[i].y+rects[i].h;
   1.655 +		CopyBits(memBits, winBits,
   1.656 +			 &update, &update, srcCopy, nil);
   1.657 +	}
   1.658 +#if TARGET_API_MAC_CARBON
   1.659 +	if ( QDIsPortBuffered(thePort) ) {
   1.660 +		QDFlushPortBuffer(thePort, NULL);
   1.661 +	}
   1.662 +#endif
   1.663 +	SetPort(saveport);
   1.664 +}
   1.665 +
   1.666 +static int ROM_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   1.667 +{
   1.668 +	CTabHandle cTab;
   1.669 +	int i;
   1.670 +
   1.671 +	/* Get the colortable from the either the display or window */
   1.672 +	if ( (this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
   1.673 +		cTab = (**(**SDL_Display).gdPMap).pmTable;
   1.674 +	} else {
   1.675 +		cTab = SDL_CTab;
   1.676 +	}
   1.677 +
   1.678 +	/* Verify the range of colors */
   1.679 +	if ( (firstcolor+ncolors) > ((**cTab).ctSize+1) ) {
   1.680 +		return(0);
   1.681 +	}
   1.682 +	
   1.683 +	/* Set the screen palette and update the display */
   1.684 +	for ( i=0; i< ncolors; ++i ) {
   1.685 +	        int j = firstcolor + i;
   1.686 +		(**cTab).ctTable[j].value = j;
   1.687 +		(**cTab).ctTable[j].rgb.red = colors[i].r << 8 | colors[i].r;
   1.688 +		(**cTab).ctTable[j].rgb.green = colors[i].g << 8 | colors[i].g;
   1.689 +		(**cTab).ctTable[j].rgb.blue = colors[i].b << 8 | colors[i].b;
   1.690 +	}
   1.691 +//	if ( (this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) 
   1.692 +{
   1.693 +		GDevice **odisplay;
   1.694 +		odisplay = GetGDevice();
   1.695 +		SetGDevice(SDL_Display);
   1.696 +		SetEntries(0, (**cTab).ctSize, (ColorSpec *)&(**cTab).ctTable);
   1.697 +		SetGDevice(odisplay);
   1.698 +	}
   1.699 +	return(1);
   1.700 +}
   1.701 +
   1.702 +void ROM_VideoQuit(_THIS)
   1.703 +{
   1.704 +	int i;
   1.705 +
   1.706 +	/* Free current video mode */
   1.707 +	ROM_UnsetVideoMode(this, this->screen);
   1.708 +	if ( SDL_Window ) {
   1.709 +		DisposeWindow(SDL_Window);
   1.710 +		SDL_Window = nil;
   1.711 +	}
   1.712 +
   1.713 +	/* Free palette and restore original one */
   1.714 +	if ( SDL_CTab != nil ) {
   1.715 +		DisposeHandle((Handle)SDL_CTab);
   1.716 +		SDL_CTab = nil;
   1.717 +	}
   1.718 +	if ( SDL_CPal != nil ) {
   1.719 +		DisposePalette(SDL_CPal);
   1.720 +		SDL_CPal = nil;
   1.721 +	}
   1.722 +	RestoreDeviceClut(GetMainDevice());
   1.723 +
   1.724 +	/* Free list of video modes */
   1.725 +	if ( SDL_modelist != NULL ) {
   1.726 +		for ( i=0; SDL_modelist[i]; ++i ) {
   1.727 +			free(SDL_modelist[i]);
   1.728 +		}
   1.729 +		free(SDL_modelist);
   1.730 +		SDL_modelist = NULL;
   1.731 +	}
   1.732 +}
   1.733 +