#ifndef _CHANNELS_H
#define _CHANNELS_H
/*
 *          < c h a n n e l s . h >
 *
 *  Definitions used internally by the various QDOS
 *  device drivers.
 *
 *  NOTE.   Much of this information could change on future
 *          variants of QDOS compatible systems, so use
 *          this header file with great care!
 *
 *  AMENDMENT HISTORY
 *  ~~~~~~~~~~~~~~~~~
 *  16 Sep 94   DJW   - Added definition of getsddata() function.
 *                    - Updated defintions of SCR/CON channel blocks, and
 *                      changed data types where appropriate.  In particular
 *                      set the unsigned qualifer where approriate as it tends
 *                      to help the compiler generate more effecient code.
 */

#ifndef _SYS_TYPES_H
#include <sys/types.h>
#endif

/*
 *  Structure of a QL queue
 */
struct qdos_queue {
    struct qdos_queue *q_nextq; /* Next queue on list */
    char *  q_end;        /* Pointer to end of queue */
    char *  q_nextin;     /* Pointer to back of queue (next location to fill)*/
    char *  q_nxtout;     /* Pointer to front of queue (next location go get byte from */
    char    q_queue[2];    /* Actual start of queue */
};

/*
 *  The base channel definition block that all channels have
 */
struct chan_defb {
    unsigned long   ch_len;     /* Length of this definition block */
    char *          ch_drivr;   /* Pointer to linkage block of device driver*/
    jobid_t         ch_owner;   /* ID of owner job */
    char *          ch_rflag;   /* Location to be set when chan released */
    unsigned short  ch_tag;     /* Channel tag */
    char            ch_stat;    /* Channel status 0 = OK */
    char            ch_actn;    /* Action required by waiting job(value of d0)*/
    jobid_t         ch_jobwt;   /* ID of waiting job */
};

/*
 *  The serial port channel definition block
 */
struct ser_cdefb {
    struct chan_defb    ser_cdef;       /* The serial chan block */
    unsigned short      ser_chno;       /* Port number, 1 or 2 */
    unsigned char       ser_par;        /* Parity, 0 none, 1 odd, 2 even, 3 mark, 4 space*/
    char                ser_txhs;       /* Transmit handshake $FF ignore, 0 handshake */
    char                ser_prot;       /* Protocol flag, FF to raw, 0 for ^Z, 1 for C */
    char                filler;         /* 1 byte padding */
    struct qdos_queue   ser_rxq;        /* Receive queue 18 bytes here */
    char                ser_dum1[80];   /* Actual space used for receive queue */
    struct qdos_queue   ser_txq;        /* transmit queue 18 bytes here */
    char                ser_dum2[80];   /* Actual space used for receive queue */
};

#define SER_CDEFBSIZE 0xE4  /* Correct size of a serial channel defn */

/*
 *  The network device channel definition block
 */
struct net_cdefb {
    struct chan_defb net_cdef;      /* The default network block */
    unsigned char   net_hedr;       /* Destination station number */
    unsigned char   net_self;       /* Our station number */
    unsigned char   net_blkl;       /* LSB of block number */
    unsigned char   net_blkh;       /* MSB of block number */
    unsigned char   net_type;       /* Packet type 0 = data, 1 = EOF */
    unsigned char   net_nbyt;       /* Number of bytes in data block */
    unsigned char   net_dchk;       /* Data checksum */
    unsigned char   net_hchk;       /* Header checksum */
    char            net_data[255];  /* Data block */
    char            net_rpnt;       /* Pointer to current position in data block */
};

#define NET_CDEFBSIZE 0x120

/*
 *  The pipe device channel definition block
 */
struct pipe_cdefb {
    struct chan_defb    ch_cdef;    /* Normal channel defn block */
    struct qdos_queue * ch_qin;     /* Pointer to input queue */
    struct qdos_queue * ch_qout;    /* Pointer to output queue */
};

#define PIPE_CDEFBSIZE 0x20

struct scrn_info {
    /* Info common to both scr_ and con_ channels */
    unsigned short  sd_xmin;        /* Top LH of window */
    unsigned short  sd_ymin;        /* Top of window */
    unsigned short  sd_xsize;       /* Window width */
    unsigned short  sd_ysize;       /* Window height */
    unsigned short  sd_borwd;       /* Border width */
    unsigned short  sd_xpos;        /* Cursor position in window */
    unsigned short  sd_ypos;
    unsigned short  sd_xinc;        /* Cursor size */
    unsigned short  sd_yinc;
    char *          sd_font[2];     /* Font addresses */
    char *          sd_scrb;        /* Base of screen */
    unsigned long   sd_pmask;       /* Paper colour mask */
    unsigned long   sd_smask;       /* Strip colour mask */
    unsigned long   sd_imask;       /* Ink colour mask */
    char            sd_cattr;       /* Character attributes - see #defines */
    char            sd_curf;        /* Cursor status, 0 suppressed, >0 visible */
    colour_t        sd_pcolr;       /* Paper colour */
    colour_t        sd_icolr;       /* Ink colour */
    colour_t        sd_bcolr;       /* Border color */
    unsigned char   sd_nlsta;       /* Newline status, 0 none pending, 1 pending */
    unsigned char   sd_fmod;        /* Fill mode 0 off 1 on */
    struct QLFLOAT  sd_yorg;
    struct QLFLOAT  sd_xorg;        /* Graphics window origin */
    struct QLFLOAT  sd_scal;        /* Scale factor */
    char *          sd_fbuf;        /* Fill buffer location */
    char *          sd_fuse;        /* User defined fill vectors ? */
    short           sd_linel;       /* Line length in bytes */
    short           sd_dummy;       /* word of zero */
};

/*
 *  Extended Channel block information
 *  (used in Pointer environemnt)
 *  This precedes the standard block in memory)
 */
struct scrn_xinfo {
    unsigned short  sd_xhits;   /* x hit size       \                    */
    unsigned short  sd_yhits;   /* y hit size        \  Equivalent to    */
    unsigned short  sd_xhito;   /* x hit origin      /  'struct QLRECT'  */
    unsigned short  sd_yhito;   /* y hit origin     /                    */

    unsigned short  sd_xouts;   /* x outline size   \                    */
    unsigned short  sd_youts;   /* y outline size    \  Equivalent to    */
    unsigned short  sd_xouto;   /* x outline origin  /  'struct QLRECT'  */
    unsigned short  sd_youto;   /* y out;ine origin /                    */

    char *          sd_prwlb;   /* Primary linked list bottom up (primary window) */
#define sd_pprwn    sd_prwlb;   /* Pointer to primary window (secondary window) */
    char *          sd_prwlt;   /* Primary linked list top down */
    char *          sd_sewlt;   /* Secondary window linked list pointer */
    char *          sd_wsave;   /* Window save area base */
    unsigned long   sd_wssiz;   /* Size of window save area */
    char *          sd_wwdef;   /* Pointer to window working defn. */
    char            sd_wlstt;   /* Window lock status (-1 locked, 0 unlocked, 1 no lock )*/
    unsigned char   sd_prwin;   /* Bit 7 set if primary window, 0 set if managed window */
    char            sd_wmode;   /* Mode of this window */
    unsigned char   sd_mysav;   /* 1 if save area is mine */
    char            sd_wmove;   /* Move/query flag (d2 from iop_rptr ) */
    char            filler[3];
    };


/*
 *  Extended Channel block information used in Pointer environemnt.
 *  (This precedes the standard block in memory)
 *
 *  N.B.    This definition is just kept for backwards compatibility
 *          with earlier releases of C68.  You should instead use the
 *          structure above which uses the documented names for members.
 */
struct qram_xcdef {
    struct QLRECT   qx_hitrect;     /* Hit rectangle (width,heightx,y) */
    struct QLRECT   qx_outrect;     /* Outline rectangle */
    char *          qx_pllbu;       /* Primary linked list bottom up */
    char *          qx_pwind;       /* Pointer to primary window */
    char *          qx_plltd;       /* Primary linked list top down */
    char *          qx_swind;       /* Secondary window linked list pointer */
    char *          qx_wsave;       /* Window save area base */
    unsigned long   qx_wssize;      /* Size of window save area */
    char *          qx_pwwdef;      /* Pointer to window working defn. */
    char            qx_lock;        /* Window lock status (-1 locked, 0 unlocked, 1 no lock )*/
    unsigned char   qx_ppwin;       /* Bit 7 set if primary window, 0 set if managed window */
    char            qx_smode;       /* Screen mode of this window */
    unsigned char   qx_mysave;      /* 1 if save area is mine */
    char            qx_wmove;       /* Move/query flag (d2 from iop_rptr ) */
    };
/*
 * Screen SCR_ channel definition block
 */
struct scr_cdefb {
    struct chan_defb scr_cdef;  /* Default defn block for scr channels*/
    struct scrn_info scr_info;  /* Common to scr and con */
};

/*
 * Screen SCR_ channel definition block
 * (Pointer environment extended version)
 */
struct scr_xcdefb {
    struct chan_defb  scr_cdef;   /* Default defn block for scr channels*/
    struct scrn_xinfo scr_xinfo;  /* Extended information Block */
    struct scrn_info  scr_info;   /* Standard Information Block */
};
/*
 *  Screen SCR_ channel definition block
 *  (with Pointer Environment installed)
 *
 *  N.B.    This definition is just kept for backward compatibility
 *          with earlier releases of C68.  You should instead use the
 *          structure above which uses the documented member names.
 */
struct scr_qram_cdefb {
    struct chan_defb    scr_cdef;  /* Default defn block for scr channels*/
    struct qram_xcdef   scr_xcdef;/* Extra for QRAM */
    struct scrn_info    scr_info;  /* Common to scr and con */
};

#define SCR_CDEFBSIZE 0x70   /* Length of a standard JS/JM channel defn block */
#define SCR_QRCDEFBSIZE 0xA0 /* Length with QRAM installed */

/* Defines for character attributes */
#define CA_UNDERLINE 0x1
#define CA_FLASH 0x2
#define CA_TRANS 0x4
#define CA_XOR 0x8
#define CA_DOUBLE_HEIGHT 0x10
#define CA_EXT_WIDTH 0x20
#define CA_DBLE_WIDTH 0x40
#define CA_GRAF_POS_CHAR 0x80 /* Graphics positioned characters */

/*
 *  The CON_ channel defn block
 */
struct con_cdefb {
    struct chan_defb    con_cdef;
    struct scrn_info    con_info;
    union {
        struct {
            /* Only for JS and above */
            long              sdu_linel;    /* Shold be 0x800000L on JS machines */
            struct qdos_queue sdu_kbd;      /* Keyboard queue */
        } sd_js;
        /*Keyboard queue on JM machines */
        struct qdos_queue   sd_jm;
    } con_end;
};

/*
 *  The CON_ channel defn block
 *  (with Pointer Environment installed)
 */
struct con_qram_cdefb {
    struct chan_defb    con_cdef;
    struct qram_xcdef   scr_xcdef;          /* Extra for QRAM */
    struct scrn_info    con_info;
    union {
        struct {
            /* Only for JS and above */
            long              sdu_linel;    /* Shold be 0x800000L on JS machines */
            struct qdos_queue sdu_kbd;      /* Keyboard queue */
        } sd_js;
        /*Keyboard queue on JM machines */
        struct qdos_queue   sd_jm;
    } con_end;
};

/*
 * Structure of a directory device driver
 * channel definition block
 */
struct fs_cdefb {
    struct chan_defb fs_cdef;   /* Base channel definition block */
    struct fs_cdefb *fs_next;   /* Next channel for this file system */
    unsigned char    fs_access; /* Access mode when file opened (in d3) */
    unsigned char    fs_drive;  /* Index into physical block table 0-15 */
    unsigned short   fs_filnr;  /* File number on drive */
    unsigned short   fs_nblok;  /* Block number containing next byte */
    unsigned short   fs_nbyte;  /* Next byte from block */
    unsigned short   fs_eblok;  /* Block number containing byte after EOF*/
    unsigned short   fs_ebyte;  /* Byte after EOF */
    char *           fs_cblock; /* Pointer to current slave block table */
    unsigned char    fs_updt;   /* Set when file updated */
    char             fs_res1;   /* Reserved */
    long             fs_res2;   /* Reserved */
    struct QLSTR     fs_name;   /* File name */
    char fs_pad[106];           /* Padding */
};

#define FSCDEF_SIZE 0xA0

/*
 *  Structure of a physical definition block
 *  for directory devices
 */
struct physdef_block {
    unsigned long   fd_len;
    long            fd_spare;
    long            fd_owner;
    char *          fd_rflag;       /* Same as fields of channel block */
    char *          fs_drivr;
    unsigned char   fs_drivn;       /* Drive number */
    unsigned short  fs_random;      /* Random number identifying drive */
    char            fs_name[10];    /* 10 bytes of medium name */
    unsigned char   fs_files;       /* Number of files open */
};

/**
 *
 *      Function definitions
 *
**/

#ifdef __STDC__
#define _P_(params) params
#else
#define _P_(params) ()
#endif

/*  Name Hiding definition */


/*  Protototypes */


#ifdef __LIBRARY__
/*************************************************************************
 *
 *                          C A U T I O N
 *                          ~~~~~~~~~~~~~
 *
 *  The remainder of this header contains definitions that are internal
 *  to the way that the C68 libraries have been implemented on QDOS/SMS.
 *  These definitions should not be used by any user program as they are
 *  subject to change without notice.
 *
 *************************************************************************/

struct chan_defb *  _getcdb     _P_((chanid_t));
int                 _Getsddata  _P_((chanid_t, struct scrn_info **));
struct QLDDEV_LINK *_isfscdb    _P_((struct chan_defb *));

#endif  /* __LIBRARY__ */

#undef _P_

#endif /* _CHANNELS_H */

