/* workbench.c */

/* appwindow interface */

/*
 * $Author: Espie $
 * $Date: 91/05/10 00:03:16 $
 * $Revision: 1.2 $
 * $Log:	workbench.c,v $
 * Revision 1.2  91/05/10  00:03:16  Espie
 * Corrected a memory leak. We now close the appwindow correctly.
 * 
 * Revision 1.1  91/05/09  17:38:28  Espie
 * Initial revision
 * 
 *
 */

#include <exec/types.h>
#include <workbench/workbench.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <proto/wb.h>
#include <custom/cleanup.h>

#include "player.h"
#include "proto.h"

LOCAL struct MsgPort *port;
LOCAL struct AppWindow *app;

LOCAL void doremovewindow(void)
	{
	struct AppMessage *msg;
		while((msg = GetMsg(port)) || !RemoveAppWindow(app))
			if (msg)
				ReplyMsg(msg);
	}
	
ULONG init_appwindow(struct Window *w)
	{
		if (!wb_around())
			return NULL;
		port = CreatePort(NULL, 0);
		if (port)
			ToClean(DeletePort, port);
		else
			return 0;
		app = AddAppWindow(1, 0, w, port, TAG_END);
		if (app)
			ToClean0(doremovewindow);
		else
			return 0;
		return 1L <<port->mp_SigBit;
	}
	
	
void handle_app(void)
	{
	struct AppMessage *msg;
		if (!port)
			return;
		while(msg = GetMsg(port))
			{
				if (msg->am_Type != MTYPE_APPWINDOW)
					{
						ReplyMsg(msg);
						continue;
					}
				add_some_songs(msg);
			}
			/* kludge to get song to start... */
		handle_interface();
	}	
	
