-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
172 lines (114 loc) · 3.18 KB
/
timer.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Gravit - A gravity simulator
Copyright 2003-2005 Gerald Kaszuba
Gravit 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.
Gravit 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 Gravit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "gravit.h"
#define TIMER_NAME_LENGTH 255
#define TIMER_COMMAND_LENGTH CONSOLE_LENGTH
#define MAX_TIMERS 50
typedef struct {
char name[TIMER_NAME_LENGTH];
float interval; // how often to execute (seconds)
int repetitions; // how many times to execute
int executed; // number of executions
Uint32 lastExec; // timestamp of last execution
char command[TIMER_COMMAND_LENGTH];
struct mytimer_t *next;
} mytimer_t;
mytimer_t *timers[50]; // array of pointers...
void timerInit() {
memset(timers, 0, sizeof(timers));
}
void timerFree() {
int i;
mytimer_t *t;
for (i = 0; i < MAX_TIMERS; i++) {
t = timers[i];
if (!t) continue;
free(timers[i]);
timers[i] = 0;
return;
}
}
mytimer_t *timerNew() {
int i;
for (i = 0; i < MAX_TIMERS; i++) {
if (timers[i])
continue;
timers[i] = (mytimer_t*)malloc(sizeof(mytimer_t));
return timers[i];
}
return NULL;
}
void timerAdd(char *name, float interval, int reps, char *command) {
mytimer_t *t = timerNew();
if (!t) {
conAdd(LERR, "Sorry, Maximum timers reached");
return;
}
strncpy(t->name, name, TIMER_NAME_LENGTH);
strncpy(t->command, command, TIMER_COMMAND_LENGTH);
t->interval = interval * 1000; // seconds to milliseconds
t->repetitions = reps;
t->executed = 0;
t->lastExec = getMS();
conAdd(LNORM, "Added timer '%s'", name);
}
void timerDelbyID(int i) {
if (!timers[i]) return;
conAdd(LNORM, "Deleting timer '%s'", timers[i]->name);
free(timers[i]);
timers[i] = 0;
}
void timerDel(char *name) {
int i;
mytimer_t *t;
for (i = 0; i < MAX_TIMERS; i++) {
t = timers[i];
if (!t) continue;
if (strncmp(t->name, name, TIMER_NAME_LENGTH)) continue;
timerDelbyID(i);
return;
}
}
void timerUpdate() {
int i;
mytimer_t *t;
Uint32 ms;
ms = getMS();
for (i = 0; i < MAX_TIMERS; i++) {
t = timers[i];
if (!t) continue;
if (ms < t->lastExec + t->interval) continue;
t->lastExec = ms;
cmdExecute(t->command);
t->executed++;
if (t->repetitions == 0) continue;
if (t->executed == t->repetitions)
timerDelbyID(i);
return;
}
}
void timerList() {
int i;
mytimer_t *t;
for (i = 0; i < MAX_TIMERS; i++) {
t = timers[i];
if (!t) continue;
if (t->repetitions == 0)
conAdd(LNORM, "%s: Executes \"%s\" every %.3fs lots of times", t->name, t->command, t->interval/1000);
else
conAdd(LNORM, "%s: Executes \"%s\" every %.3fs %i times (%i left)", t->name, t->command, t->interval/1000, t->repetitions, t->repetitions-t->executed);
}
}