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

static char rcsid[] = "$Id: ogg.c,v 1.1 2002/04/02 14:13:06 root Exp root $";

/*
 * $Log: ogg.c,v $
 * Revision 1.1  2002/04/02 14:13:06  root
 * Initial revision
 *
 * 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"


#include <sys/time.h>
#include <time.h>

static Panel_item volumeitem, statusitem,buffitem;
static Panel_item pistream,piyear,pimon,piday,pihour,pimin,pisec;

static time_t last_rectime;

static void
stop (void)
{

  hifi_cmd_rel (msglib, HIFI_CLASS_DISPLAY, clientunit, HIFI_CLASS_OGG,
                tunerunit, HIFI_CMD_STOP);
}

static time_t panelt()
{
struct tm tm;
time_t t;

tm.tm_isdst=-1;

tm.tm_year=atoi((char*) xv_get(piyear,PANEL_VALUE))-1900;
tm.tm_mon=atoi((char*) xv_get(pimon,PANEL_VALUE))-1;
tm.tm_mday=atoi((char*) xv_get(piday,PANEL_VALUE));
tm.tm_hour=atoi((char*) xv_get(pihour,PANEL_VALUE));
tm.tm_min=atoi((char*) xv_get(pimin,PANEL_VALUE));
tm.tm_sec=atoi((char*) xv_get(pisec,PANEL_VALUE));

return mktime(&tm);
}

static void tpanel(time_t t)
{
struct tm *tm=localtime(&t);
char buf[100];

sprintf(buf,"%d",tm->tm_year+1900);
xv_set(piyear,PANEL_VALUE,buf,0);

sprintf(buf,"%d",tm->tm_mon+1);
xv_set(pimon,PANEL_VALUE,buf,0);

sprintf(buf,"%d",tm->tm_mday);
xv_set(piday,PANEL_VALUE,buf,0);

sprintf(buf,"%02d",tm->tm_hour);
xv_set(pihour,PANEL_VALUE,buf,0);

sprintf(buf,"%02d",tm->tm_min);
xv_set(pimin,PANEL_VALUE,buf,0);

sprintf(buf,"%02d",tm->tm_sec);
xv_set(pisec,PANEL_VALUE,buf,0);
}


static void date_proc()
{
Byte *cptr, *ptr, *buf;
struct tm *tm;
time_t t;
int s;

t=panelt();

tm=localtime(&t);

s=atoi((char *) xv_get(pistream,PANEL_VALUE));

  buf = ptr = hifi_getbuf ();
      ptr =
        hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, clientunit,
                         HIFI_CLASS_OGG, oggunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_OGG_PLAY);
  ptr = hifipkt_ebyte(ptr,s);
  ptr = hifipkt_eword(ptr,tm->tm_year+1900);
  ptr = hifipkt_ebyte(ptr,tm->tm_mon+1);
  ptr = hifipkt_ebyte(ptr,tm->tm_mday);
  ptr = hifipkt_ebyte(ptr,tm->tm_hour);
  ptr = hifipkt_ebyte(ptr,tm->tm_min);
  ptr = hifipkt_ebyte(ptr,tm->tm_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 do_ogg_cmd(Word cmd,SDWord sd,int rel)
{
Byte *cptr, *ptr, *buf;
struct tm *tm;
time_t t;
int s;

t=panelt();

tm=localtime(&t);

s=atoi((char *) xv_get(pistream,PANEL_VALUE));

  buf = ptr = hifi_getbuf ();
if (rel) {
      ptr =
        hifipkt_new_rel (ptr, HIFI_CLASS_DISPLAY, clientunit,
                         HIFI_CLASS_OGG, oggunit);
} else {
      ptr =
        hifipkt_new (ptr, HIFI_CLASS_DISPLAY, clientunit,
                         HIFI_CLASS_OGG, oggunit);
}


  ptr = hifipkt_ecmd (cptr = ptr, cmd);
  ptr = hifipkt_esdword(ptr,sd);

  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

  hifi_send (msglib, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);

}

#if 0
static void now()
{
char buf[100];
time_t t;
struct tm *tm;

time(&t);
tpanel(t);
date_proc();
}
#endif


static void play_slew(Panel_item it)
{
time_t t;
int i;
#if 0

t=last_rectime;
t+=(int) xv_get(it,PANEL_CLIENT_DATA);
tpanel(t);
date_proc();
#else
i=(int) xv_get(it,PANEL_CLIENT_DATA);
do_ogg_cmd(HIFI_CMD_OGG_SEEK,i,0);
#endif
}

static void play_hms(Panel_item it)
{
time_t t;
struct tm *tm;
int i;

time(&t);

tm=localtime(&t);
tm->tm_hour=0;
tm->tm_min=0;
tm->tm_sec=0;
t=mktime(tm);

t+=(int) xv_get(it,PANEL_CLIENT_DATA);
tpanel(t);
//date_proc();

i=(int) xv_get(it,PANEL_CLIENT_DATA);
do_ogg_cmd(HIFI_CMD_OGG_PLAY_HMS,i,1);


}

static void play_ms(Panel_item it)
{
time_t t;
struct tm *tm;
int i;

time(&t);
tm=localtime(&t);
tm->tm_min=0;
tm->tm_sec=0;
t=mktime(tm);

t+=(int) xv_get(it,PANEL_CLIENT_DATA);
tpanel(t);
//date_proc();
i=(int) xv_get(it,PANEL_CLIENT_DATA);
do_ogg_cmd(HIFI_CMD_OGG_PLAY_MS,i,1);
}

static void play_s(Panel_item it)
{
time_t t;
struct tm *tm;
int i;

time(&t);
tm=localtime(&t);
tm->tm_sec=0;
t=mktime(tm);

t+=(int) xv_get(it,PANEL_CLIENT_DATA);
tpanel(t);
//date_proc();
i=(int) xv_get(it,PANEL_CLIENT_DATA);
do_ogg_cmd(HIFI_CMD_OGG_PLAY_S,i,1);
}

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_OGG,
                 oggunit);

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

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

    case HIFI_ST_OGG_STATE:
      {
        char buf[80], *ptr = buf;
 	Word w;


        switch(byte[0]) {
	case HIFI_OGG_State_Playing:
	   ptr+=sprintf(ptr,"Playing");
	   break;
	case HIFI_OGG_State_Stopped:
	   ptr+=sprintf(ptr,"Stopped");
	   break;
	case HIFI_OGG_State_Error:
	   ptr+=sprintf(ptr,"Error");
	   break;
 	default:
	   ptr+=sprintf(ptr,"Unknown");
	}

        ptr+=sprintf (ptr, " id:%d ",byte[1]);

	hifipkt_dword(byte+2,&w);
	
        sprintf (ptr, "%d/%d/%d %02d:%02d:%02d",
		w,byte[4],byte[5],byte[6],byte[7],byte[8]);


        xv_set (statusitem, PANEL_VALUE, buf, 0);
	
 	{
	time_t t;
	struct tm tm;


	tm.tm_year=w-1900;
	tm.tm_mon=byte[4]-1;
	tm.tm_mday=byte[5];
	tm.tm_hour=byte[6];
	tm.tm_min=byte[7];
	tm.tm_sec=byte[8];

	tm.tm_isdst=-1;

	last_rectime=mktime(&tm);

	}

	xv_set(buffitem,PANEL_VALUE,byte[9],0);

      }
      break;

    }

}

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


  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;

  pistream = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 8,
                         PANEL_LABEL_STRING, "Stream:",
			 PANEL_VALUE,"0",
                         PANEL_NOTIFY_PROC, date_proc, XV_X, 5, XV_Y, y, 0);

  y+=25;

  pihour = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 3,
                         PANEL_LABEL_STRING, "Time:",
                         PANEL_NOTIFY_PROC, date_proc, XV_X, 5, XV_Y, y, 0);

  pimin = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 3,
                         PANEL_LABEL_STRING, ":",
                         PANEL_NOTIFY_PROC, date_proc,0);

  pisec = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 3,
                         PANEL_LABEL_STRING, ":",
                         PANEL_NOTIFY_PROC, date_proc,0);
  y += 25;


  piyear = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 5,
                         PANEL_LABEL_STRING, "Date:",
                         PANEL_NOTIFY_PROC, date_proc, XV_X, 5, XV_Y, y, 0);

  pimon = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 3,
                         PANEL_LABEL_STRING, "/",
                         PANEL_NOTIFY_PROC, date_proc,0);

  piday = xv_create (p, PANEL_TEXT,
                         PANEL_VALUE_DISPLAY_LENGTH, 3,
                         PANEL_LABEL_STRING, "/",
                         PANEL_NOTIFY_PROC, date_proc,0);

  y+=25;

  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Now",
	     PANEL_CLIENT_DATA,0,
             PANEL_NOTIFY_PROC, play_s, XV_X, 5, XV_Y, y, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "top of hour",
	     PANEL_CLIENT_DATA,0,
             PANEL_NOTIFY_PROC, play_ms, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "-5min",
	     PANEL_CLIENT_DATA,-5*60,
             PANEL_NOTIFY_PROC, play_slew, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "+5min",
	     PANEL_CLIENT_DATA,5*60,
             PANEL_NOTIFY_PROC, play_slew,
	     0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "-1hour",
	     PANEL_CLIENT_DATA,-3600,
             PANEL_NOTIFY_PROC, play_slew, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "+1hour",
	     PANEL_CLIENT_DATA,3600,
             PANEL_NOTIFY_PROC, play_slew,
	     0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "-day",
	     PANEL_CLIENT_DATA,-3600*24,
             PANEL_NOTIFY_PROC, play_slew, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "+day",
	     PANEL_CLIENT_DATA,3600*24,
             PANEL_NOTIFY_PROC, play_slew, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "7am",
	     PANEL_CLIENT_DATA,7*3600,
             PANEL_NOTIFY_PROC, play_hms, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "1pm",
	     PANEL_CLIENT_DATA,13*3600,
             PANEL_NOTIFY_PROC, play_hms, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "2:15pm",
	     PANEL_CLIENT_DATA,((14*60)+15)*60,
             PANEL_NOTIFY_PROC, play_hms, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "10:45pm",
	     PANEL_CLIENT_DATA,((22*60)+45)*60,
             PANEL_NOTIFY_PROC, play_hms, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "00:30am",
	     PANEL_CLIENT_DATA,(30*60),
             PANEL_NOTIFY_PROC, play_hms, 0);
  xv_create (p, PANEL_BUTTON,
             PANEL_LABEL_STRING, "Stop",
             PANEL_NOTIFY_PROC, stop, 0);

  y += SLSEP;
  y += SLSEP;
  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;
  y += SLSEP;

  buffitem = xv_create (p, PANEL_GAUGE,
                        PANEL_MIN_VALUE, 0,
                        PANEL_MAX_VALUE, 100, PANEL_GAUGE_WIDTH, SLW,
                        PANEL_LABEL_STRING, "Buffer:",
                        PANEL_SHOW_VALUE, FALSE,
                        PANEL_SHOW_RANGE, FALSE, XV_X, 5, XV_Y, y, 0);
  y += SLSEP;
}
