summaryrefslogtreecommitdiff
path: root/src/timer.c
blob: 291890a0dbb3d2c8c312faaee41357c214e2b5ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 1997-2002, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

static const char cvs_ident[] = "$Id$";

#include "config.h"
#include "feature.h"

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>

#include "startup.h"
#include "command.h"
#include "events.h"
#include "options.h"
#include "pixmap.h"
#include "timer.h"

static etimer_t *timers = NULL;

timerhdl_t
timer_add(unsigned long msec, timer_handler_t handler, void *data) {

  static etimer_t *timer;
  struct timeval tv;
  static struct timezone tz;

  if (!timers) {
    timers = (etimer_t *) MALLOC(sizeof(etimer_t));
    timer = timers;
  } else {
    timer->next = (etimer_t *) MALLOC(sizeof(etimer_t));
    timer = timer->next;
  }
  timer->msec = msec;
  gettimeofday(&tv, &tz);
  timer->time.tv_sec = (msec / 1000) + tv.tv_sec;
  timer->time.tv_usec = ((msec % 1000) * 1000) + tv.tv_usec;
  timer->handler = handler;
  timer->data = data;
  timer->next = NULL;
  D_TIMER(("Added timer.  Timer set to %lu/%lu with handler %8p and data %8p\n", timer->time.tv_sec, timer->time.tv_usec, timer->handler, timer->data));
  return ((timerhdl_t) timer);
}

unsigned char
timer_del(timerhdl_t handle) {

  register etimer_t *current;
  etimer_t *temp;

  if (timers == handle) {
    timers = handle->next;
    FREE(handle);
    return 1;
  }
  for (current = timers; current->next; current = current->next) {
    if (current->next == handle) {
      break;
    }
  }
  if (!(current->next)) {
    return 0;
  }
  temp = current->next;
  current->next = temp->next;
  FREE(temp);
  return 1;
}

unsigned char
timer_change_delay(timerhdl_t handle, unsigned long msec) {

  struct timeval tv;
  static struct timezone tz;

  handle->msec = msec;
  gettimeofday(&tv, &tz);
  handle->time.tv_sec = (msec / 1000) + tv.tv_sec;
  handle->time.tv_usec = ((msec % 1000) * 1000) + tv.tv_usec;
  return 1;
}

void
timer_check(void) {

  register etimer_t *current;
  struct timeval tv;
  static struct timezone tz;

  if (!timers) return;

  gettimeofday(&tv, &tz);
  for (current = timers; current; current = current->next) {
    if ((current->time.tv_sec > tv.tv_sec) || ((current->time.tv_sec == tv.tv_sec) && (current->time.tv_usec >= tv.tv_usec))) {
      if (!((current->handler)(current->data))) {
        timer_del(current);
      } else {
        timer_change_delay(current, current->msec);
      }
    }
  }
}