19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Framebuffer console based SDL video driver implementation.
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/mman.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_cursor_c.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gsvideo.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gsmouse_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gsevents_c.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gsyuv_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_VideoInit(_THIS, SDL_PixelFormat *vformat);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **GS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *GS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_VideoQuit(_THIS);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_AllocHWSurface(_THIS, SDL_Surface *surface);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_LockHWSurface(_THIS, SDL_Surface *surface);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_UnlockHWSurface(_THIS, SDL_Surface *surface);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_FreeHWSurface(_THIS, SDL_Surface *surface);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* GS driver bootstrap functions */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_Available(void)
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int console, memory;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	console = open(PS2_DEV_GS, O_RDWR, 0);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( console >= 0 ) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(console);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	memory = open(PS2_DEV_MEM, O_RDWR, 0);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( memory >= 0 ) {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(memory);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return((console >= 0) && (memory >= 0));
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_DeleteDevice(SDL_VideoDevice *device)
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *GS_CreateDevice(int devindex)
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateVideoData *)
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mouse_fd = -1;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	keyboard_fd = -1;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->VideoInit = GS_VideoInit;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->ListModes = GS_ListModes;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->SetVideoMode = GS_SetVideoMode;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CreateYUVOverlay = GS_CreateYUVOverlay;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->SetColors = GS_SetColors;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->UpdateRects = NULL;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->VideoQuit = GS_VideoQuit;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->AllocHWSurface = GS_AllocHWSurface;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CheckHWBlit = NULL;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->FillHWRect = NULL;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->SetHWColorKey = NULL;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->SetHWAlpha = NULL;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->LockHWSurface = GS_LockHWSurface;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->UnlockHWSurface = GS_UnlockHWSurface;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->FlipHWSurface = NULL;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->FreeHWSurface = GS_FreeHWSurface;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->SetIcon = NULL;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->SetCaption = NULL;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetWMInfo = NULL;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->FreeWMCursor = GS_FreeWMCursor;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CreateWMCursor = GS_CreateWMCursor;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->ShowWMCursor = GS_ShowWMCursor;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->MoveWMCursor = GS_MoveWMCursor;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->InitOSKeymap = GS_InitOSKeymap;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PumpEvents = GS_PumpEvents;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = GS_DeleteDevice;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap PS2GS_bootstrap = {
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"ps2gs", "PlayStation 2 Graphics Synthesizer",
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_Available, GS_CreateDevice
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* These are the pixel formats for the 32, 24, and 16 bit video modes */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct {
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bpp;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 r;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 g;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 b;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} GS_pixelmasks[] = {
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ 32, 0x000000FF,	/* RGB little-endian */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      0x0000FF00,
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      0x00FF0000 },
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ 24, 0x000000FF,	/* RGB little-endian */
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      0x0000FF00,
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      0x00FF0000 },
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ 16, 0x0000001f,	/* RGB little-endian */
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      0x000003e0,
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      0x00007c00 },
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is a mapping from SDL bytes-per-pixel to GS pixel format */
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_formatmap[] = {
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	-1,		/* 0 bpp, not a legal value */
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	-1,		/* 8 bpp, not supported (yet?) */
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_PSMCT16,	/* 16 bpp */
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_PSMCT24,	/* 24 bpp */
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_PSMCT32	/* 32 bpp */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned long long head_tags[] __attribute__((aligned(16))) = {
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	4 | (1LL << 60),	/* GIFtag */
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0x0e,			/* A+D */
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,			/* 2 */
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_BITBLTBUF,
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,			/* 4 */
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_TRXPOS,
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,			/* 6 */
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_TRXREG,
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,			/* 8 */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_TRXDIR
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAXIMG		(32767 * 16)
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAXTAGS		8
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic inline int loadimage_nonblock(int fd, struct ps2_image *image, int size,
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                     unsigned long long *hm,
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                     unsigned long long *im)
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_plist plist;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_packet packet[1 + MAXTAGS * 2];
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int isize;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int pnum, it, eop;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *data;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* initialize the variables */
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	data = (char *)image->ptr;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pnum = it = eop = 0;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	plist.packet = packet;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* make BITBLT packet */
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[pnum].ptr = hm;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[pnum].len = sizeof(head_tags);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pnum++;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hm[2] = ((unsigned long long)image->fbp << 32) |
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        ((unsigned long long)image->fbw << 48) |
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        ((unsigned long long)image->psm << 56);
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hm[4] = ((unsigned long long)image->x << 32) |
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        ((unsigned long long)image->y << 48);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hm[6] = (unsigned long long)image->w |
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        ((unsigned long long)image->h << 32);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* make image mode tags */
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (!eop) {
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		isize = size > MAXIMG ? MAXIMG : size;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		size -= isize;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		eop = (size == 0);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		packet[pnum].ptr = &im[it];
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		packet[pnum].len = sizeof(unsigned long long) * 2;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pnum++;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		im[it++] = (isize >> 4) | (eop ? (1 << 15) : 0) | (2LL << 58);
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		im[it++] = 0;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		packet[pnum].ptr = (void *)data;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		packet[pnum].len = isize;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pnum++;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		data += isize;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	plist.num = pnum;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return ioctl(fd, PS2IOC_SENDL, &plist);
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned long long tex_tags[] __attribute__((aligned(16))) = {
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	3 | (1LL << 60),	/* GIFtag */
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0x0e,			/* A+D */
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,			/* 2 */
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_TEX0_1,
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	(1 << 5) + (1 << 6),
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_TEX1_1,
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_TEXFLUSH
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned long long scale_tags[] __attribute__((aligned(16))) = {
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	5 | (1LL << 60),	/* GIFtag */
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0x0e,			/* A+D */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	6 + (1 << 4) + (1 << 8),
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_PRIM,
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	((unsigned long long)0 * 16) + (((unsigned long long)0 * 16) << 16),
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_UV,
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	((unsigned long long)0 * 16) + (((unsigned long long)0 * 16) << 16),
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_XYZ2,
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,			/* 8 */
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_UV,
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	0,			/* 10 */
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	PS2_GS_XYZ2
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint scaleimage_nonblock(int fd, unsigned long long *tm, unsigned long long *sm)
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_plist plist;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_packet packet[2];
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* initialize the variables */
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	plist.num = 2;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	plist.packet = packet;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[0].ptr = tm;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[0].len = sizeof(tex_tags);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[1].ptr = sm;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[1].len = sizeof(scale_tags);
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return ioctl(fd, PS2IOC_SENDL, &plist);
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int power_of_2(int value)
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int shift;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( shift = 0; (1<<shift) < value; ++shift ) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Keep looking */ ;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(shift);
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_VideoInit(_THIS, SDL_PixelFormat *vformat)
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_screeninfo vinfo;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize the library */
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	console_fd = open(PS2_DEV_GS, O_RDWR, 0);
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( console_fd < 0 ) {
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to open %s", PS2_DEV_GS);
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	memory_fd = open(PS2_DEV_MEM, O_RDWR, 0);
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( memory_fd < 0 ) {
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(console_fd);
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		console_fd = -1;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to open %s", PS2_DEV_MEM);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(console_fd, PS2IOC_GSCREENINFO, &vinfo) < 0 ) {
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(memory_fd);
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(console_fd);
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		console_fd = -1;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't get console pixel format");
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the current screen size */
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_w = vinfo.w;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->info.current_h = vinfo.h;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the current screen depth */
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (vinfo.psm) {
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Supported pixel formats */
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case PS2_GS_PSMCT32:
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case PS2_GS_PSMCT24:
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case PS2_GS_PSMCT16:
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GS_VideoQuit(this);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unknown console pixel format: %d", vinfo.psm);
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->BitsPerPixel = GS_pixelmasks[vinfo.psm].bpp;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->Rmask = GS_pixelmasks[vinfo.psm].r;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->Gmask = GS_pixelmasks[vinfo.psm].g;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	vformat->Bmask = GS_pixelmasks[vinfo.psm].b;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	saved_vinfo = vinfo;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Enable mouse and keyboard support */
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( GS_OpenKeyboard(this) < 0 ) {
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		GS_VideoQuit(this);
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to open keyboard");
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( GS_OpenMouse(this) < 0 ) {
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const char *sdl_nomouse;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sdl_nomouse = SDL_getenv("SDL_NOMOUSE");
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! sdl_nomouse ) {
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GS_VideoQuit(this);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unable to open mouse");
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **GS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static SDL_Rect GS_vesa_mode_list[] = {
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{ 0, 0, 1280, 1024 },
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{ 0, 0, 1024, 768 },
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{ 0, 0, 800, 600 },
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{ 0, 0, 640, 480 }
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	};
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static SDL_Rect *GS_vesa_modes[] = {
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&GS_vesa_mode_list[0],
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&GS_vesa_mode_list[1],
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&GS_vesa_mode_list[2],
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		&GS_vesa_mode_list[3],
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		NULL
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	};
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static SDL_Rect GS_tvout_stretch;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static SDL_Rect GS_tvout_mode;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static SDL_Rect *GS_tvout_modes[3];
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect **modes = NULL;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (format->BitsPerPixel) {
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 16:
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 24:
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 32:
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( saved_vinfo.mode == PS2_GS_VESA ) {
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			modes = GS_vesa_modes;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int i, j = 0;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// FIXME - what's wrong with the stretch code at 16 bpp?
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallif ( format->BitsPerPixel != 32 ) break;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Add a mode that we could possibly stretch to */
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; GS_vesa_modes[i]; ++i ) {
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( (GS_vesa_modes[i]->w == saved_vinfo.w) &&
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				     (GS_vesa_modes[i]->h != saved_vinfo.h) ) {
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GS_tvout_stretch.w=GS_vesa_modes[i]->w;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GS_tvout_stretch.h=GS_vesa_modes[i]->h;
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					GS_tvout_modes[j++] = &GS_tvout_stretch;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					break;
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Add the current TV video mode */
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GS_tvout_mode.w = saved_vinfo.w;
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GS_tvout_mode.h = saved_vinfo.h;
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GS_tvout_modes[j++] = &GS_tvout_mode;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			GS_tvout_modes[j++] = NULL;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Return the created list of modes */
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			modes = GS_tvout_modes;
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(modes);
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Various screen update functions available */
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_DMAFullUpdate(_THIS, int numrects, SDL_Rect *rects);
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *GS_SetVideoMode(_THIS, SDL_Surface *current,
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int width, int height, int bpp, Uint32 flags)
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_screeninfo vinfo;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the terminal into graphics mode */
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( GS_EnterGraphicsMode(this) < 0 ) {
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the video mode and get the final screen format */
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(console_fd, PS2IOC_GSCREENINFO, &vinfo) < 0 ) {
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't get console screen info");
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (vinfo.w != width) || (vinfo.h != height) ||
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (GS_pixelmasks[vinfo.psm].bpp != bpp) ) {
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If we're not in VESA mode, we have to scale resolution */
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( saved_vinfo.mode == PS2_GS_VESA ) {
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch (width) {
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    case 640:
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				vinfo.res = PS2_GS_640x480;
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    case 800:
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				vinfo.res = PS2_GS_800x600;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    case 1024:
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				vinfo.res = PS2_GS_1024x768;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    case 1280:
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				vinfo.res = PS2_GS_1280x1024;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    default:
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_SetError("Unsupported resolution: %dx%d\n",
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					     width, height);
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(NULL);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vinfo.res |= (PS2_GS_75Hz << 8);
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vinfo.w = width;
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			vinfo.h = height;
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vinfo.fbp = 0;
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		vinfo.psm = GS_formatmap[bpp/8];
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( vinfo.psm < 0 ) {
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unsupported depth: %d bpp\n", bpp);
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ioctl(console_fd, PS2IOC_SSCREENINFO, &vinfo) < 0 ) {
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Couldn't set console screen info");
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Unmap the previous DMA buffer */
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mapped_mem ) {
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			munmap(mapped_mem, mapped_len);
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mapped_mem = NULL;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! SDL_ReallocFormat(current, GS_pixelmasks[vinfo.psm].bpp,
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                  GS_pixelmasks[vinfo.psm].r,
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                  GS_pixelmasks[vinfo.psm].g,
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                                  GS_pixelmasks[vinfo.psm].b, 0) ) {
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the new mode framebuffer */
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->flags = SDL_FULLSCREEN;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->w = width;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->h = height;
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->pitch = SDL_CalculatePitch(current);
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Memory map the DMA area for block memory transfer */
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! mapped_mem ) {
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pixels_len = height * current->pitch;
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mapped_len = pixels_len +
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		             /* Screen update DMA command area */
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		             sizeof(head_tags) + ((2 * MAXTAGS) * 16);
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( saved_vinfo.mode != PS2_GS_VESA ) {
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mapped_len += sizeof(tex_tags) + sizeof(scale_tags);
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mapped_mem = mmap(0, mapped_len, PROT_READ|PROT_WRITE,
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                  MAP_SHARED, memory_fd, 0);
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mapped_mem == MAP_FAILED ) {
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Unable to map %d bytes for DMA",
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			             mapped_len);
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mapped_mem = NULL;
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Set up the entire screen for DMA transfer */
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image.ptr = mapped_mem;
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image.fbp = 0;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image.fbw = (vinfo.w + 63) / 64;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image.psm = vinfo.psm;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image.x = 0;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( vinfo.h == height ) {
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_image.y = 0;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Put image offscreen and scale to screen height */
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_image.y = vinfo.h;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image.w = current->w;
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image.h = current->h;
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* get screen image data size (qword aligned) */
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image_size = (screen_image.w * screen_image.h);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (screen_image.psm) {
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case PS2_GS_PSMCT32:
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_image_size *= 4;
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case PS2_GS_PSMCT24:
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_image_size *= 3;
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case PS2_GS_PSMCT16:
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_image_size *= 2;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_image_size = (screen_image_size + 15) & ~15;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Set up the memory for screen update DMA commands */
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		head_tags_mem = (unsigned long long *)
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                (mapped_mem + pixels_len);
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		image_tags_mem = (unsigned long long *)
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                 ((caddr_t)head_tags_mem + sizeof(head_tags));
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(head_tags_mem, head_tags, sizeof(head_tags));
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( saved_vinfo.mode != PS2_GS_VESA ) {
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tex_tags_mem = (unsigned long long *)
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                 ((caddr_t)image_tags_mem + ((2*MAXTAGS)*16));
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			scale_tags_mem = (unsigned long long *)
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                 ((caddr_t)tex_tags_mem + sizeof(tex_tags));
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memcpy(tex_tags_mem, tex_tags, sizeof(tex_tags));
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tex_tags_mem[2] =
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(vinfo.h * vinfo.w) / 64 +
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          		((unsigned long long)screen_image.fbw << 14) +
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          		((unsigned long long)screen_image.psm << 20) +
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          		((unsigned long long)power_of_2(screen_image.w) << 26) +
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          		((unsigned long long)power_of_2(screen_image.h) << 30) +
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          		((unsigned long long)1 << 34) +
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          		((unsigned long long)1 << 35);
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memcpy(scale_tags_mem, scale_tags, sizeof(scale_tags));
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			scale_tags_mem[8] =
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				((unsigned long long)screen_image.w * 16) +
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			         (((unsigned long long)screen_image.h * 16) << 16);
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			scale_tags_mem[10] =
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				((unsigned long long)vinfo.w * 16) +
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			         (((unsigned long long)vinfo.h * 16) << 16);
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	current->pixels = NULL;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_getenv("SDL_FULLSCREEN_UPDATE") ) {
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Correct semantics */
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->flags |= SDL_ASYNCBLIT;
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* We lie here - the screen memory isn't really the visible
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   display memory and still requires an update, but this
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   has the desired effect for most applications.
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		current->flags |= SDL_HWSURFACE;
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the update rectangle function */
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->UpdateRects = GS_DMAFullUpdate;
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done */
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(current);
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We don't support hardware surfaces yet */
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_AllocHWSurface(_THIS, SDL_Surface *surface)
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_FreeHWSurface(_THIS, SDL_Surface *surface)
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_LockHWSurface(_THIS, SDL_Surface *surface)
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface == this->screen ) {
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Since mouse motion affects 'pixels', lock it */
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_LockCursor();
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Make sure any pending DMA has completed */
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( dma_pending ) {
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ioctl(console_fd, PS2IOC_SENDQCT, 1);
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dma_pending = 0;
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If the cursor is drawn on the DMA area, remove it */
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( cursor_drawn ) {
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface->pixels = mapped_mem + surface->offset;
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_EraseCursorNoLock(this->screen);
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cursor_drawn = 0;
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Set the surface pixels to the base of the DMA area */
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->pixels = mapped_mem;
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* We're finished! */
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnlockCursor();
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_UnlockHWSurface(_THIS, SDL_Surface *surface)
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface == this->screen ) {
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Since mouse motion affects 'pixels', lock it */
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_LockCursor();
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->pixels = NULL;
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnlockCursor();
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_DMAFullUpdate(_THIS, int numrects, SDL_Rect *rects)
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Lock so we aren't interrupted by a mouse update */
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockCursor();
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Make sure any pending DMA has completed */
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dma_pending ) {
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ioctl(console_fd, PS2IOC_SENDQCT, 1);
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dma_pending = 0;
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If the mouse is visible, draw it on the DMA area */
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (SDL_cursorstate & CURSOR_VISIBLE) && !cursor_drawn ) {
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->screen->pixels = mapped_mem + this->screen->offset;
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_DrawCursorNoLock(this->screen);
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->screen->pixels = NULL;
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cursor_drawn = 1;
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Put the image onto the screen */
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	loadimage_nonblock(console_fd,
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                   &screen_image, screen_image_size,
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                   head_tags_mem, image_tags_mem);
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_image.y > 0 ) {
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Need to scale offscreen image to TV output */
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ioctl(console_fd, PS2IOC_SENDQCT, 1);
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dma_pending = 0;
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		scaleimage_nonblock(console_fd, tex_tags_mem, scale_tags_mem);
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dma_pending = 1;
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're finished! */
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockCursor();
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int GS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_VideoQuit(_THIS)
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Close console and input file descriptors */
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( console_fd > 0 ) {
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Unmap the video framebuffer */
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mapped_mem ) {
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Unmap the video framebuffer */
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			munmap(mapped_mem, mapped_len);
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mapped_mem = NULL;
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(memory_fd);
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Restore the original video mode */
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( GS_InGraphicsMode(this) ) {
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			ioctl(console_fd, PS2IOC_SSCREENINFO, &saved_vinfo);
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* We're all done with the graphics device */
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(console_fd);
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		console_fd = -1;
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_CloseMouse(this);
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_CloseKeyboard(this);
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
690