/*

File: handler.h
Author: Neil Cafferkey
Copyright (C) 2001-2002 Neil Cafferkey

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 (at your option) 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., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

*/

#ifndef HANDLER_H
#define HANDLER_H


#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/filehandler.h>
#include <dos/notify.h>
#include <exec/memory.h>
#include <utility/utility.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/alib.h>
#include <proto/locale.h>
#include <proto/utility.h>


typedef ULONG UPINT;
typedef LONG PINT;

#ifndef MEM_BLOCKSHIFT
#define MEM_BLOCKSHIFT 3
#endif

#define MIN(A,B) (((A)<(B))?(A):(B))
#define MEMBLOCKS(A) (((A)-1>>(MEM_BLOCKSHIFT))+1);
#define HARDLINK(A) (struct Object *)(((BYTE *)(A))-(UPINT)&((struct Object *)NULL)->hard_link)
#define DOSBOOL(A) ((A)?DOSTRUE:DOSFALSE)

#define MIN_BLOCK_SIZE 64
#define MAX_BLOCK_SIZE 0x8000
#define DOS_VERSION 39
#define UTILITY_VERSION 37
#define LOCALE_VERSION 38


struct Block
{
   struct MinNode node;
   UWORD length;            /* number of data bytes */
};


struct Lock
{
   struct FileLock lock;
   struct MinList openings;
   UPINT lock_count;
   BOOL changed;
};


struct Object
{
   struct Node node;
   UWORD end_length;        /* number of valid bytes in last data block */
   struct MinList elements;
   UPINT length;            /* logical length in bytes for a file */
   UPINT block_count;       /* number of memory blocks */
   struct Lock *lock;
   struct Object *parent;
   struct DateStamp date;
   ULONG protection;
   STRPTR comment;
   struct MinNode hard_link;
};


struct Opening
{
   struct MinNode node;
   struct Object *file;
   UPINT pos;
   struct Block *block;
   UPINT block_pos;
};


struct Notification
{
   struct MinNode node;
   struct NotifyRequest *request;
   struct Object *object;
};


struct Handler
{
   struct Object *root_dir;
   struct DosList *volume;
   struct MsgPort *proc_port;
   struct MsgPort *notify_port;
   UPINT block_count;
   UPINT lock_count;
   struct Locale *locale;
   ULONG min_block_size;
   ULONG max_block_size;
   BOOL locked;
   struct MinList notifications;
};



/* Global variables */

GLOBAL struct ExecBase *SysBase;
GLOBAL struct DosLibrary *DOSBase;
GLOBAL struct UtilityBase *UtilityBase;
GLOBAL struct LocaleBase *LocaleBase;


#endif


