/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define KERNEL
#include "ixemul.h"
#undef u
#undef ix
#include <hardware/intbits.h>

#undef DEBUG

#ifdef DEBUG
#define DP(a) kprintf a
#else
#define DP(a)
#endif

#define BASE_EXT_DECL
#define BASE_PAR_DECL	struct ixemul_base *ix,
#define BASE_PAR_DECL0	struct ixemul_base *ix
#define BASE_NAME	ix->ix_intui_base
#include <inline/intuition.h>

void
ix_close (struct ixemul_base *ixbase)
{
  int fd;
  struct ExecBase 	*SysBase =	*(struct ExecBase **)4;
  struct Task 		*me 	 =	SysBase->ThisTask;
  struct user 		*u	 =	(struct user *) me->tc_TrapData;;

DP(("ix_close.\n"));
    
  RemIntServer (INTB_VERTB, & u->u_itimerint);  
#if later
#endif
  
  me->tc_Flags    = u->u_otask_flags;
  me->tc_Launch	  = u->u_olaunch;
/*  me->tc_SPReg	  = u->u_osp_reg;*/
  FreeSignal (u->u_sleep_sig);

  /* already reset the trap vector here. It's better to get an alert, than
   * to loop infinitely if one of the following functions should crash */
  me->tc_TrapCode = u->u_otrap_code;

  /* close all files */
  for (fd = 0; fd < NOFILE; fd++) 
    if (u->u_ofile[fd]) syscall (SYS_close, fd);

  CloseDevice ((struct IORequest *) u->u_time_req);
  syscall (SYS_DeleteExtIO, u->u_time_req);
  
  if (u->u_startup_cd != (BPTR) -1) __unlock (CurrentDir (u->u_startup_cd));
  
  syscall (SYS_DeletePort, u->u_mp);

  /* delay this until here, since the above called functions need access
   * to the user area. */
  me->tc_TrapData = u->u_otrap_data;


DP(("ix_close: flags = $%lx, launch = $%lx, code = $%lx, data = $%lx, spreg = $%lx\n",
   me->tc_Flags, me->tc_Launch, me->tc_TrapCode, me->tc_TrapData, me->tc_SPReg));


DP(("SysBase->TDNestCnt = %ld, SysBase->IDNestCnt = %ld\n",
    SysBase->TDNestCnt, SysBase->IDNestCnt));


  if (u->u_md.md_key)
    {
      DP(("FreeRemember, ixbase = $%lx, intui_base = $%lx\n", ixbase, ixbase->ix_intui_base));
      FreeRemember (ixbase, & u->u_md.md_key, 1);
    }
  FreeMem (u, sizeof (struct user));
}
