/*
 * mpeg.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] =
  "$Id: mpeg.c,v 1.10 2001/10/12 13:51:16 root Exp root $";

/*
 * $Log: mpeg.c,v $
 * Revision 1.10  2001/10/12 13:51:16  root
 * #
 *
 * Revision 1.9  2001/01/30 06:16:26  root
 * #
 *
 * Revision 1.8  2000/10/07 18:30:06  root
 * #
 *
 * Revision 1.7  2000/10/07 17:57:01  root
 * #
 *
 * Revision 1.6  2000/10/06 19:52:20  root
 * #
 *
 * Revision 1.5  2000/09/24 01:37:11  root
 * #
 *
 * Revision 1.4  2000/09/24 01:12:47  root
 * #
 *
 * Revision 1.3  2000/09/19 06:09:00  root
 * #
 *
 * Revision 1.2  2000/09/19 05:39:42  root
 * #
 *
 * Revision 1.1  2000/09/19 05:08:18  root
 * #
 *
 */

#include "project.h"

extern void *player;
extern void *msglib;


void
mpeg_cmdst_message (Byte dunit, Word cmd, Byte * data, Word len)
{

  if ((dunit != mpegunit) && (dunit != HIFI_UNIT_ALL))
    return;


  switch (cmd)
    {
    case HIFI_CMD_VOL:
      {
        float voll = 0.01 * (float) data[0];
        float volr = 0.01 * (float) data[1];
//        printf ("Vol %d %d\n", data[0], data[1]);
        mpegp_setvol (player, voll, volr);
      }
      break;
    case HIFI_CMD_MPEG_RESCAN:
      printf ("Rescan\n");
      rotary_rescan ();
      break;
    case HIFI_CMD_RANDOM:
      rotary_choose_random ();
      break;
    case HIFI_CMD_MPEG_PLAY:
      printf ("Play\n");
      rotary_resolve (data, 0);
      break;
    case HIFI_CMD_MPEG_SEARCH:
      printf ("Search\n");
      rotary_search (data);
      break;
    case HIFI_CMD_MPEG_SEEK:
      {
        Word where;
        hifipkt_dword (data, &where);
        mpegp_seek (player, (int) where);
      }
      printf ("Seek\n");
      break;
    case HIFI_CMD_STOP:
      printf ("Stop\n");
      mpegp_stop (player);
      break;
    }


}


void
mpeg_status (void)
{
  Byte *buf = hifi_getbuf ();
  Byte *ptr = buf, *cptr;

  struct mpegp_status s;



  s = mpegp_status (player);


  ptr = hifipkt_new_brd (ptr, HIFI_CLASS_MPEG, mpegunit);


  ptr = hifipkt_est (cptr = ptr, HIFI_ST_MPEG_STATE);



  switch (s.state)
    {
    case MPEGP_STOPPED:
      ptr = hifipkt_ebyte (ptr, HIFI_MPEG_State_Stopped);
      break;
    case MPEGP_PAUSED:
      ptr = hifipkt_ebyte (ptr, HIFI_MPEG_State_Paused);
      break;
    case MPEGP_PLAYING:
      ptr = hifipkt_ebyte (ptr, HIFI_MPEG_State_Playing);
      break;
    case MPEGP_ERROR:
      ptr = hifipkt_ebyte (ptr, HIFI_MPEG_State_Error);
      break;
    default:
      ptr = hifipkt_ebyte (ptr, HIFI_MPEG_State_Unknown);
    }

  ptr = hifipkt_ebyte (ptr, s.position.bmins);
  ptr = hifipkt_ebyte (ptr, s.position.bsecs);

  ptr = hifipkt_eword (ptr, s.length.secs);


  if (s.e)
    {
      if (s.e)
        {
          bcopy (s.e->e, ptr, sizeof (s.e->e));
        }
      else
        {
          bzero (ptr, sizeof (s.e->e));
        }
      ptr += 8;
    }
  else
    {
      bzero (ptr, 8);
      ptr += 8;
    }


  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));


  {
    int l, r;

    l = (int) (s.voll * 100.0);
    r = (int) (s.volr * 100.0);

    ptr = hifipkt_est (cptr = ptr, HIFI_ST_VOL);
    ptr = hifipkt_ebyte (ptr, l);
    ptr = hifipkt_ebyte (ptr, r);
    hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));

  }

  {
    int i;

    i = mpegp_cfhstatus (player);

    ptr = hifipkt_est (cptr = ptr, HIFI_ST_MPEG_BUFFER);
    ptr = hifipkt_ebyte (ptr, i);
    hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));

  }
  ptr = hifipkt_eeof (ptr);

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

}
