
/*
 * ogg.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: ogg.c,v $
 * Revision 1.1  2002/04/02 17:38:59  root
 * Initial revision
 *
 * Revision 1.15  2001/10/12 13:51:17  root
 * #
 *
 * Revision 1.14  2001/01/30 06:16:31  root
 * #
 *
 * Revision 1.13  2000/12/01 04:42:19  root
 * #
 *
 * Revision 1.12  2000/12/01 04:13:52  root
 * #
 *
 * Revision 1.11  2000/11/23 23:45:28  root
 * #
 *
 * Revision 1.10  2000/10/07 18:30:10  root
 * #
 *
 * Revision 1.9  2000/10/06 19:52:45  root
 * #
 *
 * Revision 1.8  2000/09/29 11:07:42  root
 * #
 *
 * Revision 1.7  2000/09/29 07:53:48  root
 * #
 *
 * Revision 1.6  2000/09/29 07:53:02  root
 * #
 *
 * Revision 1.5  2000/09/24 01:48:14  root
 * #
 *
 * Revision 1.4  2000/09/24 01:12:53  root
 * #
 *
 * Revision 1.3  2000/09/19 09:36:01  root
 * #
 *
 * Revision 1.2  2000/02/20 03:16:39  root
 * #
 *
 * Revision 1.1  2000/02/20 02:51:44  root
 * #
 *
 * Revision 1.1  2000/02/16 11:34:56  root
 * #
 *
 *
 */

static char rcsid[] =
  "$Id: ogg.c,v 1.1 2002/04/02 17:38:59 root Exp root $";

#include "project.h"


static void
volume_callback (Word formid, Word sliderid, int v)
{
  Byte *cptr, *ptr, *buf;

  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new (ptr, HIFI_CLASS_DISPLAY, displayunit, HIFI_CLASS_OGG,
                 oggunit);
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_VOL);
  ptr = hifipkt_ebyte (ptr, v);
  ptr = hifipkt_ebyte (ptr, v);
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

  hifi_send (NULL, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);

}

static void
ogg_empty_message (int msg)
{
  Byte *cptr, *ptr, *buf;

  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, displayunit, HIFI_CLASS_OGG,
                     oggunit);
  ptr = hifipkt_ecmd (cptr = ptr, msg);
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

  hifi_send (NULL, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);

}


static void
do_ogg(Word cmd,SDWord sd,int rel)
{
  Hifi_Radio_Frequency rf;
  Byte *cptr, *ptr, *buf;

  buf = ptr = hifi_getbuf ();
  if (rel)
    {
      ptr =
        hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, displayunit,
                         HIFI_CLASS_OGG, oggunit);
    }
  else
    {
      ptr =
        hifipkt_new (ptr, HIFI_CLASS_DISPLAY, displayunit, HIFI_CLASS_OGG,
                     oggunit);

    }
  ptr = hifipkt_ecmd (cptr = ptr, cmd);
  ptr = hifipkt_esdword(ptr,sd);
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);
  hifi_send (NULL, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);

}

void
settextfield (Word form, Word field, const char *str)
{
  FormPtr formptr;
  FieldPtr fieldptr;
  UInt16 index;
  VoidHand mh;
  VoidPtr p;
  int l;

  if (!str)
    return;

  l = StrLen (str);
  l++;


  formptr = FrmGetFormPtr (form);
  if (!formptr)
    return;

  index = FrmGetObjectIndex (formptr, field);
  fieldptr = FrmGetObjectPtr (formptr, index);
  if (!fieldptr)
    return;

  mh = (VoidHand) FldGetTextHandle (fieldptr);
  FldSetTextHandle (fieldptr, NULL);
  if (!mh)
    {
      mh = MemHandleNew (l);
    }

  if (MemHandleSize (mh) < l)
    {
      MemHandleResize (mh, l);
    }

  p = MemHandleLock (mh);

  StrCopy (p, str);

  MemHandleUnlock (mh);

  FldSetTextHandle (fieldptr, (Handle) mh);
  FldDrawField (fieldptr);

}


static void
gettextfield (Word form, Word field, char *str)
{
  FormPtr formptr;
  FieldPtr fieldptr;
  UInt16 index;
  VoidHand ptr;
  char *p;
  int i;

  formptr = FrmGetFormPtr (form);
  if (!formptr)
    return;
  index = FrmGetObjectIndex (formptr, field);
  fieldptr = FrmGetObjectPtr (formptr, index);
  if (!fieldptr)
    return;

  ptr = (VoidHand) FldGetTextHandle (fieldptr);
  i = FldGetTextLength (fieldptr);

  p = MemHandleLock (ptr);

  while (i--)
    *(str++) = *(p++);
  *str = 0;

  MemHandleUnlock (ptr);


}

static void send_date(void)
{
char fbuf[10];
int i;

  Byte *cptr, *ptr, *buf;

  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, displayunit, HIFI_CLASS_OGG,
                 oggunit);
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_OGG_PLAY);


  gettextfield(OggForm,OggStream,fbuf);
  i=StrAToI(fbuf);
  ptr = hifipkt_ebyte(ptr,i);

  gettextfield(OggForm,OggYear,fbuf);
  i=StrAToI(fbuf);
  ptr = hifipkt_eword(ptr,i);

  gettextfield(OggForm,OggMon,fbuf);
  i=StrAToI(fbuf);
  ptr = hifipkt_ebyte(ptr,i);

  gettextfield(OggForm,OggDay,fbuf);
  i=StrAToI(fbuf);
  ptr = hifipkt_ebyte(ptr,i);

  gettextfield(OggForm,OggHour,fbuf);
  i=StrAToI(fbuf);
  ptr = hifipkt_ebyte(ptr,i);

  gettextfield(OggForm,OggMin,fbuf);
  i=StrAToI(fbuf);
  ptr = hifipkt_ebyte(ptr,i);

  gettextfield(OggForm,OggSec,fbuf);
  i=StrAToI(fbuf);
  ptr = hifipkt_ebyte(ptr,i);

  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);
  hifi_send (NULL, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);

}



void
ogg_dispatch (EventType * e)
{
  switch (e->eType)
    {
    case penDownEvent:
      slider_dispatch (e, OggForm, OggVolume, volume_callback);
      break;

    case keyDownEvent:
      if (e->data.keyDown.chr == 10)
        send_date();
      break;

    case ctlSelectEvent:
      switch (e->data.ctlEnter.controlID)
        {
        case OggStopButton:
          ogg_empty_message (HIFI_CMD_STOP);
          break;
	case OggTOHButton:
	do_ogg(HIFI_CMD_OGG_PLAY_MS,0L,1);
	break;
	case OggLess5Button:
	do_ogg(HIFI_CMD_OGG_SEEK,-5L*60L,0);
	break;
	case OggPlus5Button:
	do_ogg(HIFI_CMD_OGG_SEEK,5L*60L,0);
	break;
	case OggLessHButton:
	do_ogg(HIFI_CMD_OGG_SEEK,-3600L,0);
	break;
	case OggPlusHButton:
	do_ogg(HIFI_CMD_OGG_SEEK,3600L,0);
	break;
	case OggLessDButton:
	do_ogg(HIFI_CMD_OGG_SEEK,-24L*60L*60L,0);
	break;
	case OggPlusDButton:
	do_ogg(HIFI_CMD_OGG_SEEK,24L*60L*60L,0);
	break;
	case Ogg0030amButton:
	do_ogg(HIFI_CMD_OGG_PLAY_HMS,30L*60L,1);
	break;
	case Ogg7amButton:
	do_ogg(HIFI_CMD_OGG_PLAY_HMS,7L*60L*60L,1);
	break;
	case Ogg1pmButton:
	do_ogg(HIFI_CMD_OGG_PLAY_HMS,13L*60L*60L,1);
	break;
	case Ogg215pmButton:
	do_ogg(HIFI_CMD_OGG_PLAY_HMS,((14L*60L)+15L)*60L,1);
	break;
	case Ogg1045pmButton:
	do_ogg(HIFI_CMD_OGG_PLAY_HMS, ((22L*60L)+45L)*60L,1);
	break;
	case OggNowButton:
	do_ogg(HIFI_CMD_OGG_PLAY_S,0L,1);
	break;
        }
      break;

    case frmOpenEvent:
{
UInt32 s;
DateTimeType dt;
char fbuf[10];

s=TimGetSeconds();
TimSecondsToDateTime(s,&dt);

sprintf(fbuf,"0");
settextfield(OggForm,OggStream,fbuf);

sprintf(fbuf,"%04d",dt.year);
  settextfield(OggForm,OggYear,fbuf);

sprintf(fbuf,"%02d",dt.month);
  settextfield(OggForm,OggMon,fbuf);

sprintf(fbuf,"%02d",dt.day);
  settextfield(OggForm,OggDay,fbuf);

sprintf(fbuf,"%02d",dt.hour);
  settextfield(OggForm,OggHour,fbuf);

sprintf(fbuf,"%02d",dt.minute);
  settextfield(OggForm,OggMin,fbuf);

sprintf(fbuf,"%02d",dt.second);
  settextfield(OggForm,OggSec,fbuf);

}
      SliderSet (OggForm, OggVolume, 0);
      break;
    }


}

void
ogg_cmdst_message (Byte unit, Word cmd, Byte * data, Word len)
{
  Word id = FrmGetActiveFormID ();
  if (id != OggForm)
    return;
  if (unit != oggunit)
    return;


  switch (cmd)
    {
    case HIFI_ST_VOL:
      {
        Word v;
        v = data[0] + data[1];
        v = v / 2;
        SliderSet (OggForm, OggVolume, v);
      }
      break;
    case HIFI_ST_OGG_STATE:
      {
        RectangleType rt = { {OggStLeft, OggStTop}
        , {OggStWidth, OggStHeight}
        };
        char buf[80], *ptr = buf;
	Word w;

        WinEraseRectangle (&rt, 0);

        switch(data[0]) {
        case HIFI_OGG_State_Playing:
           ptr+=sprintf(ptr,"Playing");
           break;
        case HIFI_OGG_State_Stopped:
           ptr+=sprintf(ptr,"Stopped");
           break;
        case HIFI_OGG_State_Error:
           ptr+=sprintf(ptr,"Error");
           break;
        default:
           ptr+=sprintf(ptr,"Unknown");
        }

        ptr+=sprintf (ptr, " id:%d ",data[1]);

        hifipkt_dword(data+2,&w);

        sprintf (ptr, "%d/%d/%d %02d:%02d:%02d",
                w,data[4],data[5],data[6],data[7],data[8]);


        WinDrawChars (buf, StrLen (buf), OggStLeft, OggStTop);
      }

      break;
    }


}
