//========================================================================
//
// AComm.h
//
// Copyright 1999-2000 Emmanuel Lesueur
//
//========================================================================

#ifndef ACOMM_H
#define ACOMM_H

#define VMAGIC       202

#define st_init      0
#define st_newdoc    1
#define st_url       2
#define st_run       3
#define st_goto      4
#define st_newdocnw  5
#define st_goback    6
#define st_prevpage  7
#define st_nextpage  8

struct OutlinesInfo {
    const char **titles;
    struct LinkAction **actions;
    char* buf;
};

struct PDFDoc;
struct AOutputDev;
struct OutputDev;
struct FontOutputDev;

struct StartupData {
    void* port;
    int in;
    int out;
    int err;
    int status;
    int flags;
    int dir;
};

struct msg_base {
    int success;
};

struct msg_init {
    struct msg_base base;
    int version;
    int errors;
};

struct msg_prefs {
    struct msg_base base;
    int colors;
    size_t cachesz;
    size_t cacheblocsz;
    char gzipcmd[256];
};

struct msg_version {
    struct msg_base base;
    char version[1024];
};

struct msg_create_output_dev {
    struct msg_base base;
    struct AOutputDev *out;
    void* port;
    int depth;
};

struct msg_delete_output_dev {
    struct msg_base base;
    struct AOutputDev *out;
};

struct msg_setaalevels {
    struct msg_base base;
    struct AOutputDev *out;
    int text;
    int stroke;
    int fill;
};

struct msg_pagesize {
    struct msg_base base;
    struct PDFDoc* doc;
    int page;
    int width;
    int height;
    int rotate;
};

struct msg_page {
    struct msg_base base;
    struct AOutputDev* aout;
    struct PDFDoc* doc;
    int page_num;
    int zoom;
    int rotate;
    int xoffset;
    int yoffset;
    int width;
    int height;
    int modulo;
    int bpp;
};

struct msg_createdoc {
    struct msg_base base;
    struct PDFDoc* doc;
    int num_pages;
    long dir;
    char filename[256];
};

struct msg_deletedoc {
    struct msg_base base;
    struct PDFDoc* doc;
};

struct msg_find {
    struct msg_base base;
    struct PDFDoc *doc;
    struct AOutputDev *aout;
    int xmin;
    int xmax;
    int ymin;
    int ymax;
    int top;
    int bottom;
    int page;
    char str[100];
};

struct msg_chklink {
    struct msg_base base;
    struct PDFDoc *doc;
    struct AOutputDev *aout;
    int x;
    int y;
    struct LinkAction *action;
    char str[256];
};

struct msg_doaction {
    struct msg_base base;
    struct PDFDoc *doc;
    struct LinkAction *action;
    struct AOutputDev *aout;
    int state;
    int page;
    int x;
    int y;
    char str[256];
};

struct msg_write {
    struct msg_base base;
    struct PDFDoc *doc;
    struct OutputDev *savedev;
    int type;
    int first_page;
    int last_page;
    int zoom;
    int rotate;
    char filename[256];
};

struct msg_gettext {
    struct msg_base base;
    struct AOutputDev *aout;
    int xmin;
    int xmax;
    int ymin;
    int ymax;
    int size;
};

struct msg_getdocinfo {
    struct msg_base base;
    struct PDFDoc *doc;
    int size;
    int encrypted;
    int oktoprint;
    int oktocopy;

};

struct msg_showfonts {
    struct msg_base base;
    int flag;
};

struct msg_fontmap {
    struct msg_base base;
    struct FontOutputDev *out;
    int m;
    int type;
    int flags;
    char pdffont[128];
    char font[128];
};

struct msg_addfontmap {
    struct msg_base base;
    struct PDFDoc *pdfdoc;
    int type;
    char pdffont[128];
    char font[128];
};

struct msg_scan_fonts {
    struct msg_base base;
    struct PDFDoc *doc;
    struct FontOutputDev *out;
    int first_page;
    int last_page;
};

struct msg_get_outlines_info {
    struct msg_base base;
    struct PDFDoc *doc;
    int num;
    size_t bufsize;
};

struct msg_get_outlines {
    struct msg_base base;
    struct PDFDoc *doc;
};


struct msg_startdoc {
    struct msg_base base;
    struct AOutputDev *aout;
};

struct msg_addfontpath {
    struct msg_base base;
    char path[256];
};

struct msg_pagelabel {
    struct msg_base base;
    struct PDFDoc *doc;
    int index;
    char label[32];
};

struct msg_error {
    struct msg_base base;
    char msg[256];
};

union msg_max {
    struct msg_init m0;
    struct msg_create_output_dev m1;
    struct msg_delete_output_dev m2;
    struct msg_page m3;
    struct msg_createdoc m4;
    struct msg_find m5;
    struct msg_doaction m6;
    struct msg_chklink m7;
    struct msg_write m8;
    struct msg_gettext m9;
    struct msg_showfonts m10;
    struct msg_fontmap m11;
    struct msg_scan_fonts m12;
    struct msg_deletedoc m13;
    struct msg_get_outlines m14;
    struct msg_get_outlines_info m15;
    struct msg_getdocinfo m16;
    struct msg_startdoc m17;
    struct msg_addfontmap m18;
    struct msg_version m19;
    struct msg_addfontpath m20;
    struct msg_pagelabel m21;
    struct msg_error m22;
};


struct msg_getcolor {
    struct msg_base base;
    unsigned long r;
    unsigned long g;
    unsigned long b;
};

struct msg_getcolors {
    struct msg_base base;
    int num;
    unsigned long table[1];
};

union ppcmsg_max {
    struct msg_getcolor getcolor;
    struct msg_getcolors getcolors;
};

#define MSGID_INIT            0
#define MSGID_QUIT            1
#define MSGID_GFX             2
#define MSGID_INFO            3
#define MSGID_PAGE            4
#define MSGID_CREATEOUTPUTDEV 5
#define MSGID_DELETEOUTPUTDEV 6
#define MSGID_CREATEDOC       7
#define MSGID_DELETEDOC       8
#define MSGID_FIND            9
#define MSGID_CHKLINK         10
#define MSGID_DOACTION        11
#define MSGID_SIMPLEFIND      12
#define MSGID_WRITE           13
#define MSGID_VERSION         14
#define MSGID_SETAALEVELS     15
#define MSGID_GETTEXT         16
#define MSGID_SHOWFONTS       17
#define MSGID_ADDFONTMAP      18
#define MSGID_CLEARFONTMAP    19
#define MSGID_CLEARFONTPATH   20
#define MSGID_ADDFONTPATH     21
#define MSGID_SCANFONTS       22
#define MSGID_ENDSCANFONTS    23
#define MSGID_GETFONT         24
#define MSGID_DEFAULTFONTS    25
#define MSGID_PAGESIZE        26
#define MSGID_INITFIND        27
#define MSGID_ENDFIND         28
#define MSGID_INITWRITE       29
#define MSGID_ENDWRITE        30
#define MSGID_INITSCAN        31
#define MSGID_GETOUTLINESINFO 32
#define MSGID_GETOUTLINES     33
#define MSGID_GETDOCINFO      34
#define MSGID_STARTDOC        35
#define MSGID_USEPREFS        36
#define MSGID_GETPAGELABEL    37
#define MSGID_GETPAGEINDEX    38
#define MSGID_GETERROR        39
#define MSGID_HASOUTLINES     40

#define MSGID_GETCOLOR        100
#define MSGID_GETCOLORS       101

struct comm_info;

struct server_info {
    void *port;
    void *reply_port;
    void *process;
    void *pool;
    void *startupmsg;
    void *extra;

    void (*destroy)(struct server_info *);
    void *(*MyAllocSharedMem)(struct server_info *,size_t);
    void (*MyFreeSharedMem)(struct server_info *,void *);
    struct comm_info *(*create_comm_info)(struct server_info*,size_t);
    void (*delete_comm_info)(struct comm_info *);
    int (*exchange_msg)(struct comm_info *,struct msg_base *,size_t,int);
    void (*exchange_raw_msg)(struct comm_info *,void *,size_t,int);
    //extern void (*init_exchange)(struct comm_info *);
    void (*send_msg)(struct comm_info *,void *,size_t,int);
    void *(*get_msg)(struct comm_info *,void **,size_t *,int *);
    void (*reply_msg)(struct comm_info *,void *);
};

struct comm_info {
    struct server_info *sv;
    void *msg;
    void *cmddata;
};

void server(struct comm_info *);

//extern void (*end_exchange)(struct comm_info *);
//extern void (*delete_server)(struct server_info*);

extern struct comm_info *(*create_comm_info)(size_t);
extern void (*delete_comm_info)(struct comm_info *);
extern int (*exchange_msg)(struct comm_info *,struct msg_base *,size_t,int);
extern void (*send_msg)(struct comm_info *,void *,size_t,int);
extern void *(*get_msg)(struct comm_info *,void **,size_t *,int *);
extern void (*reply_msg)(struct comm_info *,void *);

#define LOCK
#define UNLOCK

#endif

