/*
** Copyright (C) 2000 by Kevin L. Mitchell <klmitch@mit.edu>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**
** @(#)$Id: timer_run.c,v 1.2 2000/12/10 15:00:20 klmitch Exp $
*/
#include "timer.h"
#include "timer_int.h"
#include "rcstag.h"

RCSTAG("@(#)$Id: timer_run.c,v 1.2 2000/12/10 15:00:20 klmitch Exp $");

timer *timers = 0;

_s_ul
timer_run(void)
{
  _s_ul retval;
  timer *ptr;
  timer_val time;

  /* start by getting current time */
  if ((retval = timer_time(&time)))
    return retval;

  /* go through queue */
  for (ptr = timers; ptr; ptr = ptr->next) {
    if (time.t_sec < ptr->call.t_sec ||
	(time.t_sec == ptr->call.t_sec &&
	 time.t_nsec <= ptr->call.t_nsec))
      break; /* we're done */

    _timer_del(ptr); /* start by removing timer from linked list */
    if (!(ptr->t_flag & TIMER_REPEAT))
      ptr->t_flag = 0; /* timer is no longer active */

    (*ptr->t_callback)(ptr, &time); /* call callback */

    if (ptr->t_flag & TIMER_REPEAT) { /* readd to timer queue */
      _timer_set_call(atimer, &time); /* we give it a time already */
      _timer_add(atimer);
    }
  }

  return 0;
}
