

/*
 * foo.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: system.c,v $
 * Revision 1.14  2001/10/12 13:51:17  root
 * #
 *
 * Revision 1.13  2001/01/30 06:16:31  root
 * #
 *
 * Revision 1.12  2000/12/01 04:42:19  root
 * #
 *
 * Revision 1.11  2000/11/23 23:45:28  root
 * #
 *
 * Revision 1.10  2000/10/07 18:30:10  root
 * #
 *
 * Revision 1.9  2000/10/06 19:52:45  root
 * #
 *
 * Revision 1.8  2000/09/29 07:53:48  root
 * #
 *
 * Revision 1.7  2000/09/29 07:53:02  root
 * #
 *
 * Revision 1.6  2000/09/24 01:48:14  root
 * #
 *
 * Revision 1.5  2000/09/24 01:12:53  root
 * #
 *
 * Revision 1.4  2000/09/19 09:36:01  root
 * #
 *
 * Revision 1.3  2000/02/20 03:17:22  root
 * #
 *
 * Revision 1.2  2000/02/20 03:16:39  root
 * #
 *
 * Revision 1.1  2000/02/20 02:51:44  root
 * #
 *
 * Revision 1.22  2000/02/16 11:34:56  root
 * #
 *
 * Revision 1.21  2000/02/16 11:34:44  root
 * #
 *
 * Revision 1.20  2000/02/16 11:34:21  root
 * #
 *
 * Revision 1.19  2000/02/15 22:14:16  root
 * #
 *
 * Revision 1.18  2000/02/15 22:13:52  root
 * #
 *
 * Revision 1.17  2000/02/15 22:13:40  root
 * #
 *
 * Revision 1.16  2000/02/15 16:58:01  root
 * #
 *
 * Revision 1.15  2000/02/15 04:15:06  root
 * #
 *
 * Revision 1.14  2000/02/15 04:14:52  root
 * #
 *
 * Revision 1.13  2000/02/14 22:17:05  root
 * #
 *
 * Revision 1.12  2000/02/14 22:04:58  root
 * #
 *
 * Revision 1.11  2000/02/14 22:04:24  root
 * #
 *
 * Revision 1.10  2000/02/14 21:44:59  root
 * #
 *
 * Revision 1.9  2000/02/14 18:50:14  root
 * #
 *
 * Revision 1.8  2000/02/14 18:36:28  root
 * #
 *
 * Revision 1.7  2000/02/14 03:46:11  root
 * #
 *
 * Revision 1.6  2000/02/14 03:45:03  root
 * #
 *
 * Revision 1.5  2000/02/13 21:13:27  root
 * #
 *
 * Revision 1.4  2000/02/13 21:13:14  root
 * #
 *
 * Revision 1.3  2000/02/13 19:15:34  root
 * #
 *
 * Revision 1.2  2000/02/13 18:57:48  root
 * #
 *
 * Revision 1.1  2000/02/13 17:44:10  root
 * Initial revision
 *
 *
 */

static char rcsid[] =
  "$Id: system.c,v 1.14 2001/10/12 13:51:17 root Exp root $";

#include "project.h"


static void
dosleep (Word n)
{

  Byte *cptr, *ptr, *buf;

  buf = ptr = hifi_getbuf ();
  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, displayunit, HIFI_CLASS_SYSTEM,
                     systemunit);
  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_SYSTEM_SLEEP);
  ptr = hifipkt_eword (ptr, n);
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

  hifi_send (NULL, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);
}

static void
volume_callback (Word formid, Word sliderid, int v)
{

  Byte *cptr, *ptr, *buf;
  Byte class, unit;
  Word cmd = HIFI_CMD_VOL;

  switch (sliderid)
    {
    case SystemTunVolume:
      class = HIFI_CLASS_TUNER;
      unit = tunerunit;
      break;
    case SystemCdVolume:
      class = HIFI_CLASS_CD;
      unit = cdunit;
      break;
    case SystemMpgVolume:
      class = HIFI_CLASS_MPEG;
      unit = mpegunit;
      break;
    case SystemWavVolume:
      class = HIFI_CLASS_WAVE;
      unit = waveunit;
      break;
    case SystemOggVolume:
      class = HIFI_CLASS_OGG;
      unit = waveunit;
      break;
    case SystemPcmVolume:
      cmd = HIFI_CMD_SYSTEM_PCM_VOL;
      class = HIFI_CLASS_SYSTEM;
      unit = systemunit;
      break;
    case SystemMasVolume:
      class = HIFI_CLASS_SYSTEM;
      unit = systemunit;
      break;
    }

  buf = ptr = hifi_getbuf ();
  ptr = hifipkt_new (ptr, HIFI_CLASS_DISPLAY, displayunit, 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 (NULL, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);
}

void
system_cmdst_message (Byte class, Byte unit, Word cmd, Byte * data, Word len)
{
  Word id = FrmGetActiveFormID ();



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

      switch (cmd)
        {
        case HIFI_ST_SYSTEM_STATE:
          if (id != SystemForm)
            return;
          WinDrawChars (data, len - 1, SystemStLeft, SystemStTop);
          break;
        case HIFI_CMD_SYSTEM_OFF:
          SysSleep (0, 0);
          break;
        }

    }

  if (id != SystemForm)
    return;

  if (class == HIFI_CLASS_CLOCK)
    {
      if (unit != clockunit)
        return;

      switch (cmd)
        {
        case HIFI_ST_CLOCK_TIME:
          WinDrawChars (data, len - 1, SystemTimeLeft, SystemTimeTop);
          break;
        }

    }

  if ((cmd != HIFI_ST_VOL) && (cmd != HIFI_ST_SYSTEM_PCM_VOL))
    return;

  id = 0;
  switch (class)
    {
    case HIFI_CLASS_SYSTEM:
      if (unit != systemunit)
        return;
      switch (cmd)
        {
        case HIFI_ST_VOL:
          id = SystemMasVolume;
          break;
        case HIFI_ST_SYSTEM_PCM_VOL:
          id = SystemPcmVolume;
          break;
        }
      break;
    case HIFI_CLASS_TUNER:
      if (unit != tunerunit)
        return;
      id = SystemTunVolume;
      break;
    case HIFI_CLASS_CD:
      if (unit != cdunit)
        return;
      id = SystemCdVolume;
      break;
    case HIFI_CLASS_MPEG:
      if (unit != mpegunit)
        return;
      id = SystemMpgVolume;
      break;
    case HIFI_CLASS_WAVE:
      if (unit != waveunit)
        return;
      id = SystemWavVolume;
      break;
    case HIFI_CLASS_OGG:
      if (unit != oggunit)
        return;
      id = SystemOggVolume;
      break;
    }
  if (!id)
    return;


  {
    Word v;
    v = data[0] + data[1];
    v = v / 2;
    SliderSet (SystemForm, id, v);
  }



}

void
system_dispatch (EventType * e)
{
  Byte *cptr, *ptr;

  switch (e->eType)
    {
    case penDownEvent:
      slider_dispatch (e, SystemForm, SystemTunVolume, volume_callback);
      slider_dispatch (e, SystemForm, SystemCdVolume, volume_callback);
      slider_dispatch (e, SystemForm, SystemMpgVolume, volume_callback);
      slider_dispatch (e, SystemForm, SystemWavVolume, volume_callback);
      slider_dispatch (e, SystemForm, SystemOggVolume, volume_callback);
      slider_dispatch (e, SystemForm, SystemPcmVolume, volume_callback);
      slider_dispatch (e, SystemForm, SystemMasVolume, volume_callback);
      break;

    case ctlSelectEvent:

      switch (e->data.ctlEnter.controlID)
        {
        case SystemPowerButton:

          hifi_cmd_rel (NULL, HIFI_CLASS_DISPLAY, displayunit,
                        HIFI_CLASS_SYSTEM, systemunit, HIFI_CMD_SYSTEM_OFF);
        case SystemSleepOff:
          dosleep (0);
          break;
        case SystemSleep30:
          dosleep (30);
          break;
        case SystemSleep45:
          dosleep (45);
          break;
        case SystemSleep60:
          dosleep (60);
          break;
        case SystemSleep90:
          dosleep (90);
          break;
          break;
        }
      break;
    case menuEvent:
      break;

    case frmOpenEvent:
      SliderSet (SystemForm, SystemTunVolume, 0);
      SliderSet (SystemForm, SystemCdVolume, 0);
      SliderSet (SystemForm, SystemMpgVolume, 0);
      SliderSet (SystemForm, SystemWavVolume, 0);
      SliderSet (SystemForm, SystemOggVolume, 0);
      SliderSet (SystemForm, SystemPcmVolume, 0);
      SliderSet (SystemForm, SystemMasVolume, 0);
      break;

    }
}
