/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2012 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
#include "SDL_config.h"

/* Dummy SDL video driver implementation; this is just enough to make an
 *  SDL-based application THINK it's got a working video driver, for
 *  applications that call SDL_Init(SDL_INIT_VIDEO) when they don't need it,
 *  and also for use as a collection of stubs when porting SDL to a new
 *  platform for which you haven't yet written a valid video driver.
 *
 * This is also a great way to determine bottlenecks: if you think that SDL
 *  is a performance problem for a given platform, enable this driver, and
 *  then see if your application runs faster without video overhead.
 *
 * Initial work by Ryan C. Gordon (icculus@icculus.org). A good portion
 *  of this was cut-and-pasted from Stephane Peter's work in the AAlib
 *  SDL video driver.  Renamed to "QUINN" by Kevin Lange.
 */

#include "SDL_video.h"
#include "SDL_mouse.h"
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "../../events/SDL_events_c.h"

#include "SDL_quinnvideo.h"
#include "SDL_quinnevents_c.h"
#include "SDL_quinnmouse_c.h"

#include "quinn.h"

#define QUINNVID_DRIVER_NAME "quinn"

extern QUINN_recv_event(struct window_req_t *req);

struct SDL_VideoDevice *__quinn_device;

/* Initialization/Query functions */
static int QUINN_VideoInit(_THIS, SDL_PixelFormat *vformat);
static SDL_Rect **QUINN_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
static SDL_Surface *QUINN_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
static int QUINN_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
static void QUINN_VideoQuit(_THIS);

/* Hardware surface functions */
static int QUINN_AllocHWSurface(_THIS, SDL_Surface *surface);
static int QUINN_LockHWSurface(_THIS, SDL_Surface *surface);
static void QUINN_UnlockHWSurface(_THIS, SDL_Surface *surface);
static void QUINN_FreeHWSurface(_THIS, SDL_Surface *surface);

static void QUINN_SetCaption(_THIS, const char *title, const char *icon);

/* etc. */
static void QUINN_UpdateRects(_THIS, int numrects, SDL_Rect *rects);

/* QUINN driver bootstrap functions */

static int QUINN_Available(void)
{
	return 1;
}

static void QUINN_DeleteDevice(SDL_VideoDevice *device)
{
	SDL_free(device->hidden);
	SDL_free(device);
}

static SDL_VideoDevice *QUINN_CreateDevice(int devindex)
{
	SDL_VideoDevice *device;

	/* Initialize all variables that we clean on shutdown */
	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
	if ( device ) {
		SDL_memset(device, 0, (sizeof *device));
		device->hidden = (struct SDL_PrivateVideoData *)
				SDL_malloc((sizeof *device->hidden));
	}
	if ( (device == NULL) || (device->hidden == NULL) ) {
		SDL_OutOfMemory();
		if ( device ) {
			SDL_free(device);
		}
		return(0);
	}
	SDL_memset(device->hidden, 0, (sizeof *device->hidden));

	/* Set the function pointers */
	device->VideoInit = QUINN_VideoInit;
	device->ListModes = QUINN_ListModes;
	device->SetVideoMode = QUINN_SetVideoMode;
	device->CreateYUVOverlay = NULL;
	device->SetColors = QUINN_SetColors;
	device->UpdateRects = QUINN_UpdateRects;
	device->VideoQuit = QUINN_VideoQuit;
	device->AllocHWSurface = QUINN_AllocHWSurface;
	device->CheckHWBlit = NULL;
	device->FillHWRect = NULL;
	device->SetHWColorKey = NULL;
	device->SetHWAlpha = NULL;
	device->LockHWSurface = QUINN_LockHWSurface;
	device->UnlockHWSurface = QUINN_UnlockHWSurface;
	device->FlipHWSurface = NULL;
	device->FreeHWSurface = QUINN_FreeHWSurface;
	device->SetCaption = NULL;
	device->SetIcon = NULL;
	device->IconifyWindow = NULL;
	device->GrabInput = NULL;
	device->GetWMInfo = NULL;
	device->FreeWMCursor = QUINN_FreeWMCursor;
	device->CreateWMCursor = QUINN_CreateWMCursor;
	device->ShowWMCursor = QUINN_ShowWMCursor;
	device->WarpWMCursor = QUINN_WarpWMCursor;
	device->InitOSKeymap = QUINN_InitOSKeymap;
	device->PumpEvents = QUINN_PumpEvents;
	device->SetCaption = QUINN_SetCaption;
	device->CheckMouseMode = QUINN_CheckMouseMode;

	device->free = QUINN_DeleteDevice;

	return device;
}

VideoBootStrap QUINN_bootstrap = {
	QUINNVID_DRIVER_NAME, "SDL Quinn OS video driver",
	QUINN_Available, QUINN_CreateDevice
};


int QUINN_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
	vformat->BitsPerPixel = 32;
	vformat->BytesPerPixel = 4;

	quinn_init();
	this->hidden->wh = -1;

	__quinn_device = this;

	/* We're done! */
	return(0);
}

SDL_Rect **QUINN_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
{
   	 return (SDL_Rect **) -1;
}

void QUINN_WinExit(int wh) {
	fprintf(stderr, "I Should Quit\n");
	__quinn_device->hidden->doexit = 1;
}

SDL_Surface *QUINN_SetVideoMode(_THIS, SDL_Surface *current,
				int width, int height, int bpp, Uint32 flags)
{
	if ( this->hidden->wh != -1) {
		fprintf(stderr, "Resize request to %d x %d.\n", width, height);
		// close window and open a new one the requested sizeo
		quinn_remove_window(this->hidden->wh);
	}

	struct window_req_t *req;

	req = (struct window_req_t *)malloc(sizeof(struct window_req_t));
	if (flags & SDL_NOFRAME) {
			fprintf(stderr, "Initializing without borders.\n");
			req->flags = WIN_REQ_FLAG_NO_TITLE;
			req->x = 0;
			req->y = 0;
	} else {
			fprintf(stderr, "Initializing with borders.\n");
			req->flags = 0;
			req->x = 0;
			req->y = 25;
	}

	fprintf(stderr, "Initializing window %dx%d (%d bpp)\n", width, height, bpp);

	sprintf(req->name, "SDL Window");

	req->width = width;
	req->height = height;
	req->icon = NULL;

	this->hidden->bpp = bpp;
	this->hidden->doexit = 0;
	this->hidden->wh = quinn_req_window(req, QUINN_WinExit);

	this->hidden->surface = quinn_add_sdl_surface(this->hidden->wh, 0, 0, width, height, QUINN_recv_event);

	fprintf(stderr, "Window output initialized...\n");

	/* Set up the new mode framebuffer */
	current->flags = flags;
	current->w = width;
	current->h = height;
	current->pitch = current->w * (4);
	current->pixels = this->hidden->surface;

	/* We're done */
	return(current);
}

/* We don't actually allow hardware surfaces other than the main one */
static int QUINN_AllocHWSurface(_THIS, SDL_Surface *surface)
{
	return(-1);
}
static void QUINN_FreeHWSurface(_THIS, SDL_Surface *surface)
{
	return;
}

/* We need to wait for vertical retrace on page flipped displays */
static int QUINN_LockHWSurface(_THIS, SDL_Surface *surface)
{
	return(0);
}

static void QUINN_UnlockHWSurface(_THIS, SDL_Surface *surface)
{
	return;
}

static void QUINN_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
{
	quinn_process();
	quinn_yield();
}

int QUINN_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
{
	/* We don't support palettes... */
	return(1);
}

/* Note:  If we are terminated, this could be called in the middle of
   another SDL video routine -- notably UpdateRects.
*/
void QUINN_VideoQuit(_THIS)
{
	/* XXX close windows */
}

static void QUINN_SetCaption(_THIS, const char *title, const char *icon) {
	// TODO...
}
