/*
    XHWIF.C

Started by DavidW, 8/11/91

Later additions to h layer of hwif

*/

#include <p_std.h>
#include <p_file.h>
#include <p_sys.h>
#include <p_object.h>
#include <epoc.h>

GLREF_D TEXT *DatUsedPathNamePtr;
GLREF_D TEXT *DatCommandPtr;
GLREF_D TEXT *DatProcessNamePtr;
GLREF_D TEXT *DatStatusNamePtr;


GLDEF_C VOID hEnsurePath(TEXT *fname)
/*
Ensure that the path of the specified path exists.
All errors are ignored.
*/
    {
    P_FPARSE crk;
    TEXT buf[P_FNAMESIZE];

    if (!p_fparse(fname,NULL,&buf[0],&crk))
        {
        buf[crk.system+crk.device+crk.path]=0;
        p_mkdir(&buf[0]);
        }
    }

GLDEF_C VOID hSetUpStatusNames(TEXT *pb)
    {
    TEXT buf[P_FNAMESIZE];
    P_FPARSE crk;

    DatUsedPathNamePtr=pb;
    p_fparse(pb,0,&buf[0],&crk);
    DatStatusNamePtr=pb+P_FSYSNAMESIZE+crk.device+crk.path;
    }

GLDEF_C INT hCrackCommandLine(VOID)
    {
    INT ret;
    TEXT *pb;

    pb=DatCommandPtr;
    pb+=p_slen(pb)+1;
    if (!*pb++)
        ret=0;
    else
        {
        ret=(*pb++);
        DatProcessNamePtr=pb;
        pb+=p_slen(pb)+1;
        pb+=p_slen(pb)+1;
        if (*pb)
            hSetUpStatusNames(pb);
        else
            DatStatusNamePtr=DatProcessNamePtr;
        }
    return(ret);
    }

GLDEF_C INT hIsDbfCompressible(VOID *dH)
    {
    return(*((UWORD *)dH+19)&P_FMEDIA_COMPRESSIBLE);
    }

typedef struct
    {
    UWORD toneLengthTicks;
    UWORD delayLengthTicks;
    UWORD pauseLengthTicks;
    UBYTE dialOutCode[6];
    } DTMF_ENVAR;

LOCAL_C VOID ReadDTMFEnvar(DTMF_ENVAR *penv)
    {
    INT len;
    UBYTE buf[P_ENVMAX];

    len=p_getenviron("D$X",3,&buf[0]);
    if (len==sizeof(DTMF_ENVAR))
        p_bcpy(penv,&buf[0],sizeof(DTMF_ENVAR));
    else
        {
        penv->toneLengthTicks=8;
        penv->delayLengthTicks=8;
        penv->pauseLengthTicks=48;
        }
    }

GLDEF_C INT hDTMFString(TEXT *zts)
    {
    E_DIAL c;
    DTMF_ENVAR env;
    INT oldsnd_state;
    INT ret;
    VOID *snd;
    INT i;

    ReadDTMFEnvar(&env);
	c.toneLengthTicks=env.toneLengthTicks;
	c.delayLengthTicks=env.delayLengthTicks;
    c.pauseLengthTicks=env.pauseLengthTicks;
    oldsnd_state=p_getsnd();
    p_setsnd((oldsnd_state&~E_SOUND_DISABLE)|E_SOUND_DEVICE);
    for (i=3;i;i--)
        {
        if (!(ret=p_open(&snd,"SND:",0)))
            break;
        p_sleep(1);
        }
    if (!ret)
        {
        p_iow4(snd,E_FDIAL,zts,&c);
        p_close(snd);
        }
    p_setsnd(oldsnd_state);
    return(ret);
    }

GLDEF_C INT hLoadOwnDyl(INT index)
    {
    INT DylHandle;
    VOID *fcb;
    INT ret;

    DylHandle=0;
    ret=p_openlib(&fcb,DatCommandPtr);
    if (!ret)
        {
        p_loadfilelib(fcb,index,&DylHandle,TRUE);
        p_close(fcb);
        }
    return(DylHandle);
    }
