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

static char rcsid[] = "$Id: system.c,v 1.6 2001/01/30 06:16:33 root Exp $";

/*
 * $Log: system.c,v $
 * Revision 1.6  2001/01/30 06:16:33  root
 * #
 *
 * Revision 1.5  2000/12/01 04:42:23  root
 * #
 *
 * 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"

static Panel_item tunervolitem, cdvolitem, mpegvolitem, wavevolitem,oggvolitem,
  pcmvolitem, mastervolitem, statusitem, clockitem, balitem, spdifitem;


static void
off (void)
{
  hifi_cmd_rel (msglib, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_SYSTEM,
                systemunit, HIFI_CMD_SYSTEM_OFF);
}


static void
dosleep (Panel_item p)
{
  char *buf, *ptr, *cptr;
  int i = (int) panel_get (p, PANEL_CLIENT_DATA);

  buf = ptr = hifi_getbuf ();

  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_SYSTEM,
                     systemunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_SYSTEM_SLEEP);
  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
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_SYSTEM,
                     systemunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_SYSTEM_SPDIF);
  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
dovol (Byte class, Byte unit, Word cmd, int v)
{
  char *buf, *ptr, *cptr;

  buf = ptr = hifi_getbuf ();

  ptr = hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit, class, unit);

  ptr = hifipkt_ecmd (cptr = ptr, cmd);
  ptr = hifipkt_ebyte (ptr, v);
  ptr = hifipkt_ebyte (ptr, v);
  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
balance (Panel_item p, int i)
{
  char *buf, *ptr, *cptr;

  buf = ptr = hifi_getbuf ();

  ptr =
    hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_SYSTEM,
                 systemunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_BAL);
  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
master_vol (Panel_item p, int i)
{
  dovol (HIFI_CLASS_SYSTEM, systemunit, HIFI_CMD_VOL, i);
}


static void
pcm_vol (Panel_item p, int i)
{
  dovol (HIFI_CLASS_SYSTEM, systemunit, HIFI_CMD_SYSTEM_PCM_VOL, i);
}


static void
tuner_vol (Panel_item p, int i)
{
  dovol (HIFI_CLASS_TUNER, tunerunit, HIFI_CMD_VOL, i);
}

static void
cd_vol (Panel_item p, int i)
{
  dovol (HIFI_CLASS_CD, cdunit, HIFI_CMD_VOL, i);
}

static void
mpeg_vol (Panel_item p, int i)
{
  dovol (HIFI_CLASS_MPEG, mpegunit, HIFI_CMD_VOL, i);
}

static void
wave_vol (Panel_item p, int i)
{
  dovol (HIFI_CLASS_WAVE, waveunit, HIFI_CMD_VOL, i);
}


static void
ogg_vol (Panel_item p, int i)
{
  dovol (HIFI_CLASS_OGG, oggunit, HIFI_CMD_VOL, i);
}


void
clock_cmdst (Byte class, Byte unit, Word cmd, unsigned char *byte, int len)
{
  if (unit != clockunit)
    return;

  switch (cmd)
    {
    case HIFI_ST_CLOCK_TIME:
      xv_set (clockitem, PANEL_VALUE, byte, 0);
      break;

    }

}

void
system_cmdst (Byte class, Byte unit, Word cmd, unsigned char *byte, int len)
{

  if (class == HIFI_CLASS_SYSTEM)
    {
      if (unit != systemunit)
        return;

      switch (cmd)
        {
        case HIFI_ST_BAL:
          xv_set (balitem, PANEL_VALUE, (int) *byte, 0);
          break;
        case HIFI_ST_SYSTEM_STATE:
          xv_set (statusitem, PANEL_VALUE, byte, 0);
          break;
        case HIFI_ST_SYSTEM_PCM_VOL:
          xv_set (pcmvolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        case HIFI_ST_SYSTEM_SPDIF:
          xv_set (pcmvolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        }
    }


  if (cmd == HIFI_ST_VOL)
    {
      switch (class)
        {
        case HIFI_CLASS_TUNER:
          if (unit != tunerunit)
            return;
          xv_set (tunervolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        case HIFI_CLASS_CD:
          if (unit != cdunit)
            return;
          xv_set (cdvolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        case HIFI_CLASS_MPEG:
          if (unit != mpegunit)
            return;
          xv_set (mpegvolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        case HIFI_CLASS_WAVE:
          if (unit != waveunit)
            return;
          xv_set (wavevolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        case HIFI_CLASS_OGG:
          if (unit != oggunit)
            return;
          xv_set (oggvolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        case HIFI_CLASS_SYSTEM:
          xv_set (mastervolitem, PANEL_VALUE, (int) *byte, 0);
          break;
        }
    }


}

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

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Power Off",
             PANEL_NOTIFY_PROC, off, XV_X, 5, XV_Y, y, 0);

  xv_create (p, PANEL_MESSAGE, PANEL_LABEL_STRING, "Sleep:", 0);

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Off",
             PANEL_NOTIFY_PROC, dosleep, PANEL_CLIENT_DATA, 0, 0);

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "30",
             PANEL_NOTIFY_PROC, dosleep, PANEL_CLIENT_DATA, 30, 0);

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "45",
             PANEL_NOTIFY_PROC, dosleep, PANEL_CLIENT_DATA, 45, 0);

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "60",
             PANEL_NOTIFY_PROC, dosleep, PANEL_CLIENT_DATA, 60, 0);

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "90",
             PANEL_NOTIFY_PROC, dosleep, PANEL_CLIENT_DATA, 90, 0);
  y += 30;
  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;
  clockitem = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 40,
                         PANEL_READ_ONLY, TRUE,
                         PANEL_LABEL_STRING, "Clock:", XV_X, 5, XV_Y, y, 0);
  y += 25;


  tunervolitem = xv_create (p, PANEL_SLIDER,
                            PANEL_MIN_VALUE, 0,
                            PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                            PANEL_LABEL_STRING, "Tuner:",
                            PANEL_SHOW_VALUE, FALSE,
                            PANEL_SHOW_RANGE, FALSE,
                            PANEL_SLIDER_END_BOXES, TRUE,
                            PANEL_NOTIFY_PROC, tuner_vol,
                            PANEL_NOTIFY_LEVEL, PANEL_ALL,
                            XV_X, 5, XV_Y, y, 0);
  y += SLSEP;

  cdvolitem = xv_create (p, PANEL_SLIDER,
                         PANEL_MIN_VALUE, 0,
                         PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                         PANEL_LABEL_STRING, "CD:",
                         PANEL_SHOW_VALUE, FALSE,
                         PANEL_SHOW_RANGE, FALSE,
                         PANEL_SLIDER_END_BOXES, TRUE,
                         PANEL_NOTIFY_PROC, cd_vol,
                         PANEL_NOTIFY_LEVEL, PANEL_ALL, XV_X, 5, XV_Y, y, 0);
  y += SLSEP;


  mpegvolitem = xv_create (p, PANEL_SLIDER,
                           PANEL_MIN_VALUE, 0,
                           PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                           PANEL_LABEL_STRING, "MPEG:",
                           PANEL_SHOW_VALUE, FALSE,
                           PANEL_SHOW_RANGE, FALSE,
                           PANEL_SLIDER_END_BOXES, TRUE,
                           PANEL_NOTIFY_PROC, mpeg_vol,
                           PANEL_NOTIFY_LEVEL, PANEL_ALL,
                           XV_X, 5, XV_Y, y, 0);
  y += SLSEP;

  wavevolitem = xv_create (p, PANEL_SLIDER,
                           PANEL_MIN_VALUE, 0,
                           PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                           PANEL_LABEL_STRING, "Wave:",
                           PANEL_SHOW_VALUE, FALSE,
                           PANEL_SHOW_RANGE, FALSE,
                           PANEL_SLIDER_END_BOXES, TRUE,
                           PANEL_NOTIFY_PROC, wave_vol,
                           PANEL_NOTIFY_LEVEL, PANEL_ALL,
                           XV_X, 5, XV_Y, y, 0);
  y += SLSEP;
  oggvolitem = xv_create (p, PANEL_SLIDER,
                          PANEL_MIN_VALUE, 0,
                          PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                          PANEL_LABEL_STRING, "Ogg:",
                          PANEL_SHOW_VALUE, FALSE,
                          PANEL_SHOW_RANGE, FALSE,
                          PANEL_SLIDER_END_BOXES, TRUE,
                          PANEL_NOTIFY_PROC, ogg_vol,
                          PANEL_NOTIFY_LEVEL, PANEL_ALL, XV_X, 5, XV_Y, y, 0);
  y += SLSEP;


  pcmvolitem = xv_create (p, PANEL_SLIDER,
                          PANEL_MIN_VALUE, 0,
                          PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                          PANEL_LABEL_STRING, "PCM:",
                          PANEL_SHOW_VALUE, FALSE,
                          PANEL_SHOW_RANGE, FALSE,
                          PANEL_SLIDER_END_BOXES, TRUE,
                          PANEL_NOTIFY_PROC, pcm_vol,
                          PANEL_NOTIFY_LEVEL, PANEL_ALL, XV_X, 5, XV_Y, y, 0);
  y += SLSEP;


  mastervolitem = xv_create (p, PANEL_SLIDER,
                             PANEL_MIN_VALUE, 0,
                             PANEL_MAX_VALUE, 100, PANEL_SLIDER_WIDTH, SLW,
                             PANEL_LABEL_STRING, "Master:",
                             PANEL_SHOW_VALUE, FALSE,
                             PANEL_SHOW_RANGE, FALSE,
                             PANEL_SLIDER_END_BOXES, TRUE,
                             PANEL_NOTIFY_PROC, master_vol,
                             PANEL_NOTIFY_LEVEL, PANEL_ALL,
                             XV_X, 5, XV_Y, y, 0);
  y += SLSEP;

  balitem = xv_create (p, PANEL_SLIDER,
                       PANEL_MIN_VALUE, 0,
                       PANEL_MAX_VALUE, 255, PANEL_SLIDER_WIDTH, SLW,
                       PANEL_LABEL_STRING, "Balance:",
                       PANEL_SHOW_VALUE, FALSE,
                       PANEL_SHOW_RANGE, FALSE,
                       PANEL_SLIDER_END_BOXES, TRUE,
                       PANEL_NOTIFY_PROC, balance,
                       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, "Digital:",
                         PANEL_CHOICE_STRINGS,
                         "IN->Amp", 0,
                         PANEL_NOTIFY_PROC, digiproc, XV_X, 5, XV_Y, y, 0);





}
