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

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

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

#include "project.h"


#define NTRACKS 24
#define TWID 6

static Panel_item volumeitem, progitem, statusitem, choiceitem;

static Byte current_track = 0;
static Word current_length = 0;
static Word current_numtrack = NTRACKS;
static Byte current_state = 255;


static void
cd_play_track (int i)
{
  Byte *buf, *ptr, *cptr;

  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, clientunit,
                     HIFI_CLASS_CD, cdunit);
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_CD_PLAY);
  ptr = hifipkt_ebyte (ptr, i); //Track

  ptr = hifipkt_ebyte (ptr, 0); //Index (Ignored)

  ptr = hifipkt_ebyte (ptr, 0); //Min

  ptr = hifipkt_ebyte (ptr, 0); //Sec

  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
tbut (Panel_item i, int j)
{
  cd_play_track (j + 1);
}
static void
tcmd (Panel_item i)
{
  Word c = (Word) (int) xv_get (i, PANEL_CLIENT_DATA);
  hifi_cmd_rel (msglib, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_CD,
                tunerunit, c);
}


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

  min = (Byte) (i / 60);
  sec = (Byte) (i % 60);


  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_CD, cdunit);
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_CD_PLAY);
  ptr = hifipkt_ebyte (ptr, current_track);
  ptr = hifipkt_ebyte (ptr, 0); //Index (Ignored)

  ptr = hifipkt_ebyte (ptr, min); //Min

  ptr = hifipkt_ebyte (ptr, sec); //Sec

  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 char **numbered_buttons = NULL;


static void
do_hide_tracks ()
{
  Xv_opaque t = choiceitem;
  int i;
  char temp[20];

  xv_set (t, XV_SHOW, FALSE, NULL);

  if (numbered_buttons != NULL)
    {
      for (i = 0; numbered_buttons[i]; i++)
        free (numbered_buttons[i]);
      free (numbered_buttons);
    }

  numbered_buttons = (char **) calloc (current_numtrack + 1, sizeof (char *));

  xv_set (t, PANEL_CHOICE_STRINGS, " ", NULL, NULL);

  for (i = 0; i < current_numtrack; i++)
    {
      sprintf (temp, "%d", i + 1);

      numbered_buttons[i] = strdup (temp);

      xv_set (t, PANEL_CHOICE_STRING, i, numbered_buttons[i], NULL);
    }

  numbered_buttons[i] = NULL;

  i = 1;
  while (xv_get (t, XV_WIDTH) + 5 >= xv_get (panels[TAB_CD], XV_WIDTH))
    xv_set (t, PANEL_CHOICE_NROWS, ++i, NULL);

  xv_set (t, XV_SHOW, TRUE, NULL);
}



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_CD, cdunit);

  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
cd_cmdst (Byte class, Byte unit, Word cmd, unsigned char *data, int len)
{
  if (unit != cdunit)
    return;

  switch (cmd)
    {
    case HIFI_ST_VOL:
      xv_set (volumeitem, PANEL_VALUE, (int) *data, 0);
      break;

    case HIFI_ST_CD_STATE:
      {
        Byte st;
        DWord d;
        char buf[80], *ptr;
        int fl = 0;


        ptr = buf;
/*
   if (current_state != data[4])
   {
   if (current_state == HIFI_CD_State_Playing)
   }
 */
        current_state = data[4];

        current_track = data[0];

        switch (current_state)
          {
          case HIFI_CD_State_Stopped:
            xv_set (choiceitem, XV_SHOW, TRUE, 0);
            xv_set (choiceitem, PANEL_VALUE, -1, 0);
            ptr += sprintf (ptr, "Stop: ");
            break;
          case HIFI_CD_State_Playing:
            xv_set (choiceitem, XV_SHOW, TRUE, 0);
            xv_set (choiceitem, PANEL_VALUE, current_track - 1, 0);
            ptr += sprintf (ptr, "Play: ");
            fl++;
            break;
          case HIFI_CD_State_Paused:
            xv_set (choiceitem, XV_SHOW, TRUE, 0);
            xv_set (choiceitem, PANEL_VALUE, current_track - 1, 0);
            ptr += sprintf (ptr, "Paused: ");
            fl++;
            break;
          case HIFI_CD_State_NoDisc:
            xv_set (choiceitem, XV_SHOW, FALSE, 0);
            xv_set (choiceitem, PANEL_VALUE, -1, 0);
            ptr += sprintf (ptr, "NoDisc: ");
            break;
          case HIFI_CD_State_Unknown:
            ptr += sprintf (ptr, "???: ");
            xv_set (choiceitem, PANEL_VALUE, -1, 0);
            break;
          }

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

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

          }

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


        xv_set (statusitem, PANEL_VALUE, buf, 0);



      }


      break;

    case HIFI_ST_CD_TKSTATE:
      {

        if (current_numtrack != data[1])
          {
            current_numtrack = data[1];

            do_hide_tracks ();

            current_track = 0;

          }

        hifipkt_dword (data + 2, &current_length);
      }
      break;
    }

}

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

  int i = 0;
  char buf[80];

  choiceitem = xv_create (p, PANEL_CHOICE,
                          XV_X, 5, XV_Y, y,
                          PANEL_LAYOUT, PANEL_HORIZONTAL, PANEL_CHOOSE_NONE,
                          FALSE, PANEL_NOTIFY_PROC, tbut, NULL);


  y += 100;
  y += 25;

  current_numtrack = NTRACKS;

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Stop",
             PANEL_CLIENT_DATA, HIFI_CMD_STOP,
             PANEL_NOTIFY_PROC, tcmd, XV_X, 5, XV_Y, y, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Eject",
             PANEL_CLIENT_DATA, HIFI_CMD_EJECT, PANEL_NOTIFY_PROC, tcmd, 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;

}
