// MPImage - Amiga Image Conversion
// Copyright (C) © 1996 Mark John Paddock
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.

// This program 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 General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

// mark@topic.demon.co.uk
// mpaddock@cix.compulink.co.uk

#define DOSBase mydosbase;
#include <pragmas/dos_pragmas.h>
#include <clib/dos_protos.h>
#include <proto/exec.h>

// initial value
#define DJPEG_INIT	0
// Waiting to read
#define DJPEG_WAIT	1
// Have read
#define DJPEG_HAVE	2
// Abort
#define DJPEG_ABORT	3

extern far ULONG djpegflag = DJPEG_INIT;
extern far char jpegpip[32] = "";

void
djpegWaitTask(void) {
	int i;
	BPTR fh;
	char tow[] = "??";
	struct Library *mydosbase;
	if (mydosbase = OpenLibrary("dos.library",0)) {
		for (i = 0;
			  (i < 40);
			  ++i) {
			if (djpegflag == DJPEG_HAVE) {
				CloseLibrary(mydosbase);
				return;
			}
			// otherwise wait 1/2 second
			Delay(25);
		}
		// Try to write to file - if this fails then give up
		if (fh = Open(jpegpip,MODE_NEWFILE)) {
			// write flags and set abort
			djpegflag = DJPEG_ABORT;
			Write(fh,tow,2);
			Close(fh);
		}
		CloseLibrary(mydosbase);
	}
	else {
		djpegflag = DJPEG_ABORT;
	}
}
