/*
 * Copyright (c) 1994 Malcolm Beattie, all rights reserved.
 */
#include "tkperl.h"
#include <sys/ioctl.h>

struct tkpasyncio
{
    IO *io;
    int mask;
    struct tkpcallback callback;
};

static AV *asynciolist;

void TkpAsyncIoHandler P((ClientData, int));

int TkpAddasyncioCmd(ix, sp, items)
int ix;
int sp;
int items;
{
    SV **st = stack_base + sp;
    GV *gv;
    IO *io;
    int fd;
    int mask;
    char *maskstr;
    char *method;
    STRLEN mlen;
    struct tkpasyncio *asynciop;
    SV **svp;
    SV *sv;
    
    if (items < 3 || items > 4)
	croak("Usage: addasyncio(FILEHANDLE, \"rwe\", $method [, $slave])");
    
    if (!asynciolist)
	asynciolist = newAV();

    if (!(gv = gv_fetchpv(SvPV(st[1], na), FALSE, SVt_PVIO))
	|| !(io = GvIO(gv))
	|| !IoIFP(io))
	croak("bad filehandle in addasyncio");
    
    fd = fileno(IoIFP(io));
    mask = 0;
    maskstr = SvPV(st[2], na);
    if (strchr(maskstr, 'r'))
	mask |= TK_READABLE;
    if (strchr(maskstr, 'w'))
	mask |= TK_WRITABLE;
    if (strchr(maskstr, 'e'))
	mask |= TK_EXCEPTION;
    if (!mask)
	croak("bad mask in addasyncio");
    
    svp = av_fetch(asynciolist, fd, FALSE);
    if (svp)
    {
	sv = *svp;
	asynciop = (struct tkpasyncio *) SvPV(sv, na);
	if (asynciop->callback.method)
	    ckfree(asynciop->callback.method);
	if (asynciop->callback.slave)
	    SvREFCNT_dec(asynciop->callback.slave);
    }
    else
    {
	struct tkpasyncio asyncio;
	
	Zero(&asyncio, 1, struct tkpasyncio);
	sv = newSVpv((char *) &asyncio, sizeof(struct tkpasyncio));
	asynciop = (struct tkpasyncio *) SvPV(sv, na);
    }
    
    asynciop->io = io;
    asynciop->mask = mask;
    asynciop->callback.slave = (items == 4) ? newSVsv(st[4]) : Nullsv;
    method = SvPV(st[3], mlen);
    asynciop->callback.method = (char *) ckalloc(mlen + 1);
    strcpy(asynciop->callback.method, method);
    av_store(asynciolist, fd, sv);
    Tk_CreateFileHandler(fd, mask, TkpAsyncIoHandler, (ClientData) sv);
    st[0] = sv_2mortal(newSViv(fd));
    return sp;
}

int TkpDelasyncioCmd(ix, sp, items)
int ix;
int sp;
int items;
{
    SV **st = stack_base + sp;
    GV *gv;
    IO *io;
    SV **svp;
    int fd;
    struct tkpasyncio *asynciop;
    
    if (items != 1)
	croak("Usage: delasyncio(FILEHANDLE)");
    
    if (!(gv = gv_fetchpv(SvPV(st[1], na), FALSE, SVt_PVIO))
	|| !(io = GvIO(gv))
	|| !IoIFP(io))
	croak("bad filehandle in delasyncio");
    
    fd = fileno(IoIFP(io));
    if (!asynciolist || !(svp = av_fetch(asynciolist, fd, FALSE)))
	croak("no async file handler to delete in delasyncio");
    Tk_DeleteFileHandler(fd);
    asynciop = (struct tkpasyncio *) SvPV(*svp, na);
    if (asynciop->callback.method)
	ckfree(asynciop->callback.method);
    if (asynciop->callback.slave)
	sv_free(asynciop->callback.slave);
    av_store(asynciolist, fd, Nullsv);
    st[0] = sv_2mortal(newSViv(fd));
    return sp;
}


void TkpAsyncIoHandler(clientData, mask)
ClientData clientData;
int mask;
{
    SV *sv = (SV *) clientData;
    int sp = stack_sp - stack_base;
    struct tkpasyncio *asynciop;
    struct tkpcallback callback;
    int fd;
    int toread, towrite;


    asynciop = (struct tkpasyncio *) SvPV(sv, na);
    fd = fileno(IoIFP(asynciop->io));

    toread = -1;
    if ((mask & TK_READABLE) && ioctl(fd, FIONREAD, &toread) == -1)
	croak("ioctl FIONREAD failed in TkpAsyncIoHandler");

    towrite = 0;
    if ((mask & TK_WRITABLE) && ioctl(fd, TIOCOUTQ, &towrite) == -1)
	croak("ioctl TIOCOUTQ failed in TkpAsyncIoHandler");
    
    av_store(stack, sp + 2, sv_2mortal(newSViv(toread)));
    av_store(stack, sp + 3, sv_2mortal(newSViv(towrite)));
    av_store(stack, sp + 4,
	     sv_mortalcopy((mask & TK_EXCEPTION) ? &sv_yes : &sv_no));
    RECOVERSTACK;
    TkpCallback(&asynciop->callback, 3);
}

    

