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

static char rcsid[] = "$Id: wave.c,v 1.4 2000/11/23 23:45:30 root Exp $";

/*
 * $Log: wave.c,v $
 * Revision 1.4  2000/11/23 23:45:30  root
 * #
 *
 * Revision 1.2  2000/10/07 16:41:53  root
 * #
 *
 * Revision 1.1  2000/10/06 19:52:46  root
 * #
 *
 * Revision 1.1  2000/10/06 06:21:36  root
 * #
 *
 */

#include "project.h"

static Panel_item volumeitem, listitem, statusitem, progitem, spdifitem;

static Word current_length;
static int num = -1;

static int selected = -1;

static void
list_lookup (int j)
{

  if (j == 255)
    j = -1;


  if (j != -1)
    {
/*      if (xv_get (listitem, PANEL_LIST_SELECTED, j) == FALSE) */
      if (j != selected)
        {
          xv_set (listitem, PANEL_LIST_SELECT, j, TRUE, 0);
          selected = j;
        }
    }
  else
    {
      if (j != selected)
        {
          xv_set (listitem, PANEL_LIST_SELECT, 0, FALSE, 0);
          selected = -1;
        }
    }

}

static int
lnp (Panel_item z, char *s, char *cd, Panel_list_op op, Event * event, int i)
{
  char *ptr, *buf, *cptr;
  char *a;

  selected = -1;

/*  if (op != PANEL_LIST_OP_SELECT)
    return XV_OK;*/


  if (i >= num)
    return XV_OK;

  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, clientunit,
                     HIFI_CLASS_WAVE, waveunit);
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_WAVE_PLAY);

  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);

  return XV_OK;
}



static void
listnew (void)
{
  int x, y, h, w;
  int i;


  h = xv_get (listitem, PANEL_LIST_DISPLAY_ROWS);
  w = xv_get (listitem, PANEL_LIST_WIDTH);
  x = xv_get (listitem, XV_X);
  y = xv_get (listitem, XV_Y);



  xv_destroy (listitem);
  selected = -1;

  listitem = xv_create (panels[TAB_WAVE], PANEL_LIST,
                        XV_X, x,
                        XV_Y, y,
                        PANEL_LIST_WIDTH, w,
                        PANEL_LIST_DISPLAY_ROWS, h,
                        PANEL_LIST_NROWS, num,
                        PANEL_READ_ONLY, TRUE,
                        PANEL_NOTIFY_PROC, lnp,
                        PANEL_CHOOSE_ONE, TRUE, PANEL_CHOOSE_NONE, TRUE, 0);

  for (i = 0; i < num; ++i)
    {
      xv_set (listitem, PANEL_LIST_STRING, i, "Please wait...", 0);
    }

}



static void
maintain_list (int j, int n, char *name)
{


  if (n != num)
    {
      int i;

      num = n;
      listnew ();


    }



  if (j < num)
    {


      xv_set (listitem, PANEL_LIST_STRING, j, name, 0);

    }





}


static void
digiproc (Panel_item p, int i)
{
  char *buf, *ptr, *cptr;

  buf = ptr = hifi_getbuf ();

  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_WAVE,
                     waveunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_WAVE_SOURCE);
  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);
}

static void
wrec (void)
{
  hifi_cmd_rel (msglib, HIFI_CLASS_DISPLAY, clientunit,
                HIFI_CLASS_WAVE, waveunit, HIFI_CMD_RECORD);
}

static void
wstop (void)
{
  hifi_cmd_rel (msglib, HIFI_CLASS_DISPLAY, clientunit,
                HIFI_CLASS_WAVE, waveunit, HIFI_CMD_STOP);
}

static void
doprog (Panel_item p, int i)
{
  Byte *buf, *ptr, *cptr;

  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_WAVE,
                 waveunit);
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_WAVE_SEEK);
  ptr = hifipkt_eword (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);


}


static void
dovol (Panel_item p, int i)
{
  char *buf, *ptr, *cptr;


  buf = ptr = hifi_getbuf ();

  ptr =
    hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_WAVE,
                 waveunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_VOL);
  ptr = hifipkt_ebyte (ptr, i);
  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);
}


void
wave_cmdst (Byte class, Byte unit, Word cmd, unsigned char *data, int len)
{
  if (unit != waveunit)
    return;



  switch (cmd)
    {
    case HIFI_ST_WAVE_LIST:
      maintain_list (data[0], data[1], &data[2]);
      break;
    case HIFI_ST_VOL:
      xv_set (volumeitem, PANEL_VALUE, (int) *data, 0);
      break;

    case HIFI_ST_WAVE_SOURCE:
      xv_set (spdifitem, PANEL_VALUE, data[0], 0);
      break;

    case HIFI_ST_WAVE_STATE:
      {
        int current_state;
        int fl = 0;

        DWord d;
        Word length;

        char buf[80], *ptr;

        ptr = buf;


        current_state = data[0];

        switch (current_state)
          {
          case HIFI_WAVE_State_Stopped:
            ptr += sprintf (ptr, "Stop: ");
            break;
          case HIFI_WAVE_State_Playing:
            ptr += sprintf (ptr, "Play: ");
            fl++;
            break;
          case HIFI_WAVE_State_Recording:
            ptr += sprintf (ptr, "Record: ");
            fl++;
            break;
          case HIFI_WAVE_State_Error:
            ptr += sprintf (ptr, "Error: ");
            break;
          case HIFI_WAVE_State_Unknown:
            ptr += sprintf (ptr, "???: ");
            break;
          }

        hifipkt_dword (&data[3], &current_length);


        if (fl)
          {
            sprintf (ptr, "%02d:%02d/%d:%02d", data[1], data[2],
                     current_length / 60, current_length % 60);

          }
        d = 0;
        if (current_length > 0)
          {
            d = data[1];
            d *= 60;
            d += data[2];
          }

        xv_set (progitem, PANEL_VALUE, d, PANEL_MAX_VALUE, current_length, 0);

        list_lookup (data[5]);


        xv_set (statusitem, PANEL_VALUE, buf, 0);
      }

      break;

    }




}


void
wave_makepanel (Panel p)
{
  int y = 5;


  listitem = xv_create (p, PANEL_LIST,
                        XV_X, 5,
                        XV_Y, 5,
                        PANEL_NOTIFY_PROC, lnp,
                        PANEL_READ_ONLY, TRUE,
                        PANEL_CHOOSE_ONE, TRUE,
                        PANEL_LIST_WIDTH, WIDTH - 30,
                        PANEL_LIST_DISPLAY_ROWS, 1, 0);


  xv_set (listitem, PANEL_LIST_DISPLAY_ROWS,
          110 / (int) xv_get (listitem, PANEL_LIST_ROW_HEIGHT), 0);


  y += 120 + xv_get (listitem, PANEL_LIST_ROW_HEIGHT);



  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Stop", XV_X, 5, XV_Y, y,
             PANEL_NOTIFY_PROC, wstop, 0);

  xv_create (p, PANEL_BUTTON, PANEL_LABEL_STRING, "Record",
             PANEL_NOTIFY_PROC, wrec, 0);
  y += 25;

  statusitem = xv_create (p, PANEL_TEXT,
                          PANEL_VALUE_DISPLAY_LENGTH, 40,
                          PANEL_READ_ONLY, TRUE,
                          PANEL_LABEL_STRING, "Status:", XV_X, 5, XV_Y, y, 0);
  y += 25;

  progitem = xv_create (p, PANEL_SLIDER,
                        PANEL_MIN_VALUE, 0,
                        PANEL_MAX_VALUE, 0, PANEL_SLIDER_WIDTH, SLW,
                        PANEL_SHOW_RANGE, FALSE,
                        PANEL_SHOW_VALUE, FALSE, PANEL_SLIDER_END_BOXES, TRUE,
                        PANEL_NOTIFY_PROC, doprog,
                        PANEL_NOTIFY_LEVEL, PANEL_ALL, XV_X, 5, XV_Y, y, 0);
  y += SLSEP;
  y += SLSEP;

  volumeitem = xv_create (p, PANEL_SLIDER,
                          PANEL_MIN_VALUE, 0,
                          PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                          PANEL_LABEL_STRING, "Vol:",
                          PANEL_SHOW_VALUE, FALSE,
                          PANEL_SHOW_RANGE, FALSE,
                          PANEL_SLIDER_END_BOXES, TRUE,
                          PANEL_NOTIFY_PROC, dovol,
                          PANEL_NOTIFY_LEVEL, PANEL_ALL, XV_X, 5, XV_Y, y, 0);

  y += SLSEP;

  spdifitem = xv_create (p, PANEL_TOGGLE,
                         PANEL_FEEDBACK, PANEL_MARKED,
                         PANEL_LABEL_STRING, "Sources:",
                         PANEL_CHOICE_STRINGS,
                         "CD", "Tuner", "SPDIF", 0,
                         PANEL_NOTIFY_PROC, digiproc, XV_X, 5, XV_Y, y, 0);


}
