]> Dogcows Code - chaz/tint2/blob - src/util/timer.c
*fix* tooltip copys the displayed text
[chaz/tint2] / src / util / timer.c
1 /**************************************************************************
2 *
3 * Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 **************************************************************************/
17
18 #include <sys/timerfd.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include "timer.h"
23
24 GSList* timer_list = 0;
25
26 int install_timer(int value_sec, int value_nsec, int interval_sec, int interval_nsec, void (*_callback)())
27 {
28 if ( value_sec < 0 || interval_sec < 0 || _callback == 0 )
29 return -1;
30
31 int timer_fd;
32 struct itimerspec timer;
33 timer.it_value = (struct timespec){ .tv_sec=value_sec, .tv_nsec=value_nsec };
34 timer.it_interval = (struct timespec){ .tv_sec=interval_sec, .tv_nsec=interval_nsec };
35 timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
36 timerfd_settime(timer_fd, 0, &timer, 0);
37 struct timer* t = malloc(sizeof(struct timer));
38 t->id=timer_fd;
39 t->_callback = _callback;
40 timer_list = g_slist_prepend(timer_list, t);
41 return timer_fd;
42 }
43
44
45 void reset_timer(int id, int value_sec, int value_nsec, int interval_sec, int interval_nsec)
46 {
47 struct itimerspec timer;
48 timer.it_value = (struct timespec){ .tv_sec=value_sec, .tv_nsec=value_nsec };
49 timer.it_interval = (struct timespec){ .tv_sec=interval_sec, .tv_nsec=interval_nsec };
50 timerfd_settime(id, 0, &timer, 0);
51 }
52
53
54 void uninstall_timer(int id)
55 {
56 close(id);
57 GSList* timer_iter = timer_list;
58 while (timer_iter) {
59 struct timer* t = timer_iter->data;
60 if (t->id == id) {
61 timer_list = g_slist_remove(timer_list, t);
62 free(t);
63 return;
64 }
65 timer_iter = timer_iter->next;
66 }
67 }
This page took 0.042338 seconds and 5 git commands to generate.