/*
 * main.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: main.c,v $
 * Revision 1.3  2000/12/01 04:42:25  root
 * #
 *
 * Revision 1.2  2000/12/01 04:13:56  root
 * #
 *
 * Revision 1.1  2000/11/23 23:45:36  root
 * #
 *
 * Revision 1.6  2000/09/24 01:37:11  root
 * #
 *
 * Revision 1.5  2000/09/24 01:12:47  root
 * #
 *
 * Revision 1.4  2000/09/19 06:09:00  root
 * #
 *
 * Revision 1.3  2000/09/19 05:39:42  root
 * #
 *
 * Revision 1.2  2000/09/19 05:08:18  root
 * #
 *
 * Revision 1.1  2000/09/08 00:35:27  root
 * #
 *
 * Revision 1.6  2000/02/21 04:11:27  root
 * #
 *
 * Revision 1.5  2000/02/21 04:01:00  root
 * #
 *
 * Revision 1.4  2000/02/20 03:03:53  root
 * #
 *
 * Revision 1.3  2000/02/20 02:52:13  root
 * #
 *
 * Revision 1.2  2000/02/16 13:51:59  root
 * #
 *
 * Revision 1.1  2000/02/16 13:50:11  root
 * Initial revision
 *
 *
 */

static char rcsid[] = "$Id: main.c,v 1.3 2000/12/01 04:42:25 root Exp $";

#include "project.h"

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <signal.h>

#include <endian.h>


void *msglib;
void *player;


void
timeout (void)
{
  wave_status ();
  return;
}

/*
 * Function main (argc, )
 *
 *    Initialize and try to receive test frames
 *
 */
int waveunit;

int
main (int argc, char *argv[])
{
  fd_set readfds, writefds;
  int mfd, hfd, w;
  struct timeval tv, rlast, mlast, sep;

/* Should be first as may do a fork() */
  waveunit = hifi_getunit ("wave");
  msglib = hifi_open (HIFI_CLASS_WAVE, waveunit);


  mixer_open ();

  mixer_set_source (HIFI_WAVE_Source_Tuner);

  hifi_send (msglib, "fish", 4);

  player = wavep_create ();


  rotary_init ();

//  mpegp_play (player, f, e);
//   mixer_open();


  timerclear (&mlast);
  timerclear (&rlast);

  while (1)
    {

      FD_ZERO (&readfds);
      FD_ZERO (&writefds);

      mfd = wavep_fd (player);
      hfd = hifimsg_fd (msglib);

      w = mfd > hfd ? (mfd + 1) : (hfd + 1);

      FD_SET (hfd, &readfds);
      if (mfd > 0)
        {
          FD_SET (mfd, &writefds);
          FD_SET (mfd, &readfds);
        }

      tv.tv_sec = 1;
      tv.tv_usec = 0;
      select (w, &readfds, &writefds, NULL, &tv);


      if ((mfd > 0)
          && ((FD_ISSET (mfd, &writefds)) || FD_ISSET (mfd, &readfds)))
        {
          wavep_dispatch (player);
        }

      if (FD_ISSET (hfd, &readfds))
        {
          hifi_dispatch ();
        }

      {
        struct timeval d;
        gettimeofday (&tv, NULL);

        timersub (&tv, &mlast, &d);

        if (d.tv_sec > 1)
          {
            timeout ();
            mlast = tv;
          }

        timersub (&tv, &rlast, &d);


        if (timercmp (&d, &rotary_sep, >))
          {
            rotary_dispatch ();
            rlast = tv;
          }
      }



    }


}
