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

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

/*
 * $Log: sleep.c,v $
 * Revision 1.4  2001/10/12 13:51:17  root
 * #
 *
 * Revision 1.3  2001/01/30 06:16:29  root
 * #
 *
 * Revision 1.2  2000/12/01 04:13:51  root
 * #
 *
 * Revision 1.1  2000/11/23 23:45:27  root
 * #
 *
 */


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

extern void *msglib;

static time_t soff = 0;

void
sleep_turn_off (void)
{
  int i;

//FIXME

  for (i = 0; i < 20; ++i)
    {
      hifi_cmd (msglib, HIFI_CLASS_SYSTEM, systemunit, HIFI_CLASS_DISPLAY,
                HIFI_UNIT_ALL, HIFI_CMD_SYSTEM_OFF);
      usleep (500000);
    }



}


void
sleep_set (int mins)
{

  if (mins)
    {
      time (&soff);
      soff += (mins * 60);
    }
  else
    {
      soff = 0;
    }


}

int
sleep_left (void)
{
  double dt;
  time_t now;
  int ret;
  time (&now);

  if (soff)
    {
      dt = difftime (soff, now);
      ret = (int) (dt / 60.0);
    }
  else
    {
      return 0;
    }

  return ret;
}

void
sleep_dispatch (void)
{
  if (sleep_left () < 0)
    sleep_turn_off ();
}
