summaryrefslogtreecommitdiff
path: root/src/timers.c
blob: 33434453f7d3ceb67afa3d23a0fd626335bc1ada (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* timers.c

Copyright (C) 1999-2003 Tom Gilbert.
Copyright (C) 2010-2011 Daniel Friesel.

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 and its documentation and acknowledgment shall be
given in the documentation 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.

*/

#include "feh.h"
#include "options.h"
#include "timers.h"

fehtimer first_timer = NULL;

void feh_handle_timer(void)
{
	fehtimer ft;

	if (!first_timer) {
		D(("No timer to handle, returning\n"));
		return;
	}
	ft = first_timer;
	first_timer = first_timer->next;
	D(("Executing timer function now\n"));
	(*(ft->func)) (ft->data);
	D(("Freeing the timer\n"));
	if (ft && ft->name)
		free(ft->name);
	if (ft)
		free(ft);
	return;
}

double feh_get_time(void)
{
	struct timeval timev;

	gettimeofday(&timev, NULL);
	return((double) timev.tv_sec + (((double) timev.tv_usec) / 1000000));
}

void feh_remove_timer(char *name)
{
	fehtimer ft, ptr, pptr;

	D(("removing %s\n", name));
	pptr = NULL;
	ptr = first_timer;
	while (ptr) {
		D(("Stepping through event list\n"));
		ft = ptr;
		if (!strcmp(ft->name, name)) {
			D(("Found it. Removing\n"));
			if (pptr)
				pptr->next = ft->next;
			else
				first_timer = ft->next;
			if (ft->next)
				ft->next->in += ft->in;
			if (ft->name)
				free(ft->name);
			if (ft)
				free(ft);
			return;
		}
		pptr = ptr;
		ptr = ptr->next;
	}
	return;
}

void feh_add_timer(void (*func) (void *data), void *data, double in, char *name)
{
	fehtimer ft, ptr, pptr;
	double tally;

	D(("adding timer %s for %f seconds time\n", name, in));
	feh_remove_timer(name);
	ft = malloc(sizeof(_fehtimer));
	ft->next = NULL;
	ft->func = func;
	ft->data = data;
	ft->name = estrdup(name);
	ft->just_added = 1;
	ft->in = in;
	D(("ft->in = %f\n", ft->in));
	if (!first_timer) {
		D(("No first timer\n"));
		first_timer = ft;
	} else {
		D(("There is a first timer\n"));
		pptr = NULL;
		ptr = first_timer;
		tally = 0.0;
		while (ptr) {
			tally += ptr->in;
			if (tally > in) {
				tally -= ptr->in;
				ft->next = ptr;
				if (pptr)
					pptr->next = ft;
				else
					first_timer = ft;
				ft->in -= tally;
				if (ft->next)
					ft->next->in -= ft->in;
				return;
			}
			pptr = ptr;
			ptr = ptr->next;
		}
		if (pptr)
			pptr->next = ft;
		else
			first_timer = ft;
		ft->in -= tally;
	}
	D(("ft->in = %f\n", ft->in));
	return;
}

void feh_add_unique_timer(void (*func) (void *data), void *data, double in)
{
	static long i = 0;
	char evname[20];

	snprintf(evname, sizeof(evname), "T_%ld", i);
	D(("adding timer with unique name %s\n", evname));
	feh_add_timer(func, data, in, evname);
	i++;
	/* Mega paranoia ;) */
	if (i > 1000000)
		i = 0;
	return;
}