/*
**
** $VER: screen.c 0.5 (05.11.02)
** Description
**
** (C) Copyright 1999 Paul Hill
** (C) Copyright 2002 Alexandre Balaban
**
** $HISTORY :
**		- 0.5, 05.11.02 : o Added TRACE debug informations
**						  o Added pack pragma directives for PPC compilation
**		- 0.4, 04.11.02 : Improved PPC includes
**		- 0.3, 03.10.02 : Add loop setting menu
**		- 0.2, 27.07.02 : Clean-up
**		- 0.1, 27.02.99 : Original version by Paul Hill
**
*/

#pragma pack(2)

#ifndef __PPC__
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/asl.h>
#include <proto/gadtools.h>
#else
#include </ADE/os-includeppc/proto/intuition.h>
#include </ADE/os-includeppc/proto/graphics.h>
#include </ADE/os-includeppc/proto/asl.h>
#include </ADE/os-includeppc/proto/gadtools.h>
#endif

#pragma pack()

#include <stdio.h>
#include "swfplayer.h"

static APTR VisualInfo = NULL;
static struct RastPort video_temprp;
static struct BitMap video_tmp_bm = { 0, 0, 0, 0, 0, {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL} };

//static void swf_about(struct MenuItem *menuitem);
//static void swf_seperatescreen(struct MenuItem *menuitem);
void swf_quit(struct MenuItem *menuitem);

static struct NewMenu SWFPlayerNewMenu[] =
{
	{NM_TITLE,	(STRPTR)"Project",				NULL,	0,			NULL,	NULL},
//	{NM_ITEM,	(STRPTR)"Open", 				"O",	0,			0L,		(APTR) MENU_OPEN},
//	{NM_ITEM,	(STRPTR)NM_BARLABEL,			NULL,	0,			0L,		NULL},
	{NM_ITEM,	(STRPTR)"Properties",			"I",	0,			0L,		(APTR) MENU_INFO},
	{NM_ITEM,	(STRPTR)"About",				"?",	0,			0L,		(APTR) MENU_ABOUT},
	{NM_ITEM,	(STRPTR)NM_BARLABEL,			NULL,	0,			0L,		NULL},
	{NM_ITEM,	(STRPTR)"Quit",					"Q",	0,			0L,		(APTR) MENU_QUIT},

	{NM_TITLE,	(STRPTR)"Control",				NULL,	0,			NULL,	NULL},
	{NM_ITEM,	(STRPTR)"Pause",				"P",	0,			0L,		(APTR) MENU_PAUSE},
	{NM_ITEM,	(STRPTR)"Continue",				"C",	0,			0L,		(APTR) MENU_CONTINUE},
	{NM_ITEM,	(STRPTR)"Rewind",				"R",	0,			0L,		(APTR) MENU_REWIND},
	{NM_ITEM,	(STRPTR)"Zoom In",				"+",	0,			0L,		(APTR) MENU_ZOOMIN},
	{NM_ITEM,	(STRPTR)"Zoom Out",				"-",	0,			0L,		(APTR) MENU_ZOOMOUT},
//	{NM_ITEM,	(STRPTR)"Step",					NULL,	0,			0L,		(APTR) MENU_STEP},

  	{NM_TITLE,	(STRPTR)"Settings",				NULL,	0,			NULL,	NULL},
//	{NM_ITEM,	(STRPTR)"Use Seperate Screen",	NULL,	CHECKIT,	0L,		MENU_SEPERATE},
//	{NM_ITEM,	(STRPTR)NM_BARLABEL,			NULL,	0,			0L,		NULL},
//	{NM_ITEM,	(STRPTR)"Volume",				NULL,	0,			NULL,	NULL},
//	{NM_SUB,	(STRPTR)"Off",					NULL,	0,			0L,		NULL},
//	{NM_SUB,	(STRPTR)"Quiet",				NULL,	0,			0L,		NULL},
//	{NM_SUB,	(STRPTR)"Normal",				NULL,	0,			0L,		NULL},
//	{NM_SUB,	(STRPTR)"Loud",					NULL,	0,			0L,		NULL,		//(APTR)Project0Item15},
    {NM_ITEM,	(STRPTR)"Loop play",			"L",	CHECKIT|CHECKED,	0L,		(APTR) MENU_LOOPPLAY},
//	{NM_ITEM,	(STRPTR)NM_BARLABEL,			NULL,	0,			0L,		NULL},
//	{NM_ITEM,	(STRPTR)"Save As Defaults",		"D",	0,			0L,		NULL},
	{NM_END,	NULL,							NULL,	0,			0L,		NULL}
};


int open_swf_window()
{
	/* Adjust the width & height if MAXWIDTH or MAXHEIGHT were specified on the command line */
	if (args[OPT_MAXWIDTH])
		if (swfinfo.width > (* (ULONG *) args[OPT_MAXWIDTH])) swfinfo.width = * (ULONG*) args[OPT_MAXWIDTH];

	if (args[OPT_MAXHEIGHT])
		if (swfinfo.height > (* (ULONG *) args[OPT_MAXHEIGHT])) swfinfo.height = * (ULONG*) args[OPT_MAXHEIGHT];



#ifdef FULLSCREEN_CODE
	if (swfinfo.fullscreen)
	{
		/* We're running fullscreen, not in a window */

		if ((swfinfo.screen = OpenScreenTags(NULL,
					SA_Left,			0,
					SA_Top,			0,
					SA_Width,		swfinfo.width,
					SA_Height,		swfinfo.height,
					SA_DisplayID,	swfinfo.modeid,
					SA_Depth,		swfinfo.depth,
					SA_Title,		(ULONG) "SWFPlayer",
					TAG_DONE)))
		{
			swfinfo.window = OpenWindowTags(NULL,
										WA_InnerWidth,				(ULONG) swfinfo.width,
										WA_InnerHeight,			(ULONG) swfinfo.height,
										WA_CloseGadget,	FALSE,
										WA_DepthGadget,	FALSE,
										WA_DragBar,			FALSE,
										WA_SizeGadget,		FALSE,
										WA_RMBTrap,			FALSE,
										WA_Activate,		TRUE,
										WA_Borderless,		TRUE,
										WA_IDCMP,			IDCMP_MENUVERIFY | IDCMP_MENUPICK | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | IDCMP_VANILLAKEY | IDCMP_RAWKEY | IDCMP_INTUITICKS | IDCMP_MOUSEBUTTONS,
										WA_Activate,		TRUE,
										WA_CustomScreen,	(ULONG) swfinfo.screen,
										TAG_END);
		}
	}
	else
#endif
	{
		/* We're running in a window on a pubscreen */
		swfinfo.screen = LockPubScreen((char *) swfinfo.pubscreenname);
		if (!swfinfo.screen)
		{
			fprintf(stderr,"Can't lock pubscreen\n");
			return 0;
		}

		swfinfo.window = OpenWindowTags(NULL,
									WA_InnerWidth,				(ULONG) swfinfo.width,
									WA_InnerHeight,			(ULONG) swfinfo.height,
									WA_CloseGadget,			TRUE,
									WA_DepthGadget,			TRUE,
									WA_DragBar,					TRUE,
									WA_Title,					(ULONG) "swfplayer",
									WA_SizeGadget,				FALSE,
									WA_IDCMP,					IDCMP_MENUPICK |
																	IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW |
																	IDCMP_VANILLAKEY | IDCMP_RAWKEY |
																	IDCMP_INTUITICKS | IDCMP_MOUSEBUTTONS |
																	IDCMP_ACTIVEWINDOW | IDCMP_INACTIVEWINDOW,
									WA_Activate,				TRUE,
									WA_PubScreen,				(ULONG) swfinfo.screen,
									WA_PubScreenFallBack,	TRUE,
									TAG_END);
	}

	if (!swfinfo.window)
	{
		fprintf(stderr,"no window\n");
		return 0;
	}

	swfinfo.depth = GetBitMapAttr(swfinfo.screen->RastPort.BitMap,BMA_DEPTH);
    TRACE( "*** GetBitMapAttr passed !\n" );

	/* Cache some variables for later */
	swfinfo.winbordertop  = swfinfo.window->BorderTop;
	swfinfo.winborderleft = swfinfo.window->BorderLeft;
	swfinfo.rport         = swfinfo.window->RPort;

	/* There's a chance that the desired window width & height couldn't be
		met, so update the width & height variables with the real width &
		height */
	swfinfo.width  = swfinfo.window->Width  - swfinfo.window->BorderLeft - swfinfo.window->BorderRight;
	swfinfo.height = swfinfo.window->Height - swfinfo.window->BorderTop  - swfinfo.window->BorderBottom;

	/* Layout the gadtools menus */
	if ((VisualInfo = GetVisualInfo(swfinfo.screen, TAG_DONE )))
	{
		if ((swfinfo.SWFPlayerMenus = CreateMenus(SWFPlayerNewMenu, GTMN_FrontPen, 0L, TAG_DONE)))
		{
			LayoutMenus(swfinfo.SWFPlayerMenus, VisualInfo, TAG_DONE);

			SetMenuStrip(swfinfo.window, swfinfo.SWFPlayerMenus);
			GT_RefreshWindow(swfinfo.window, NULL);
		}
	}

	/* Setup the temporary rastport required for kickstart 3.0 */
	if (!swfinfo.kick31)
	{
		int depth;
		TRACE( "*** kickstart 3.0 or earlier detected, setting up temporary rastport...\n" );
        InitBitMap (&video_tmp_bm, swfinfo.depth, swfinfo.width, 1);

		for (depth = 0; depth < swfinfo.depth; depth++)
		{
			if ((video_tmp_bm.Planes[depth] = (PLANEPTR)AllocRaster (swfinfo.width, 1)) == NULL)
			{
				fprintf(stderr,"AllocRaster() failed");
				return 0;
			}
		}

		video_temprp = *swfinfo.rport;
		video_temprp.Layer = NULL;
		video_temprp.BitMap = &video_tmp_bm;

		swfinfo.tmprport = &video_temprp;
	}

	TRACE( "*** open_swf_window() exit normally !\n" );
	return 1;
}


void close_swf_window()
{
	if (swfinfo.window)
	{
		if (!swfinfo.kick31)
		{
			int depth;
			for (depth = 0; depth < swfinfo.depth; depth++)
			{
				if (video_tmp_bm.Planes[depth] != NULL)
				{
					FreeRaster (video_tmp_bm.Planes[depth], swfinfo.width, 1);
					video_tmp_bm.Planes[depth] = NULL;
				}
			}
			swfinfo.tmprport = NULL;
		}

		if (VisualInfo)
		{
			if (swfinfo.SWFPlayerMenus)
			{
				ClearMenuStrip(swfinfo.window);
				FreeMenus(swfinfo.SWFPlayerMenus);
				swfinfo.SWFPlayerMenus = NULL;
			}
			FreeVisualInfo(VisualInfo);
		}

		CloseWindow(swfinfo.window);
		swfinfo.window = NULL;
	}
	if (swfinfo.screen)
	{
#ifdef FULLSCREEN_CODE
		if (swfinfo.fullscreen)
			CloseScreen(swfinfo.screen);
		else
#endif
			UnlockPubScreen(NULL,swfinfo.screen);

		swfinfo.screen = NULL;
	}
}

