/*
 * Copyright (c) 1993 Michael D. Bayne.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software must display the following
 *    acknowledgement:
 *
 *       This product includes software developed by Michael D. Bayne.
 *
 * 4. My name may not be used to endorse or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY MICHAEL D. BAYNE ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL MICHAEL D. BAYNE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <exec/tasks.h>
#include <exec/execbase.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <dos/dos.h>

#include "TaskE.h"
#include "defs.h"

extern	struct	ExecBase *SysBase;
extern	struct	IntuitionBase *IBase;
extern	struct	List *scrList;

void sendSigAbrt( struct TaskNode *t )
{
	APTR Lock;

	if( TaskExists( t->tn_TaskAddress )) Signal(( struct Task * )t->tn_TaskAddress, SIGBREAKF_CTRL_C );

	Lock = rtLockWindow( TaskEWnd );
	Delay( 50 );
	rtUnlockWindow( TaskEWnd, Lock );
}

void popScreen( int item )
{
	struct	Screen	*Scr;
		ULONG	lock, num;
	struct	Node	*t;

	for( t = scrList->lh_Head, num = 0; num != item; num++, t = t->ln_Succ );

	lock = LockIBase( 0 );

	for( Scr = IBase->FirstScreen; Scr; Scr = Scr->NextScreen )
		if(!( Strnicmp( Scr->DefaultTitle, t->ln_Name, 30 ))) break;
	
	UnlockIBase( lock );

	if( Scr ) {
		ScreenToFront( Scr );
		if( Scr->FirstWindow ) ActivateWindow( Scr->FirstWindow );
	}
}

LONG TaskExists( ULONG Addr )
{
	struct Node		*execNode;
	int retval = 0;

        Disable();
	    
	for( execNode = (SysBase->TaskWait).lh_Head; execNode; execNode = execNode->ln_Succ )
		if( retval = ( Addr == (ULONG)execNode )) break;

	if( !retval )
		for( execNode = (SysBase->TaskReady).lh_Head; execNode; execNode = execNode->ln_Succ )
			retval = ( Addr == (ULONG)execNode );
	Enable();
	return( retval );
}

