all repos — tint2 @ 6c40536d1a0a9935e63c7414384839883a0d4077

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/util/timer.c (raw)

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
/**************************************************************************
*
* Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/

#include <sys/timerfd.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include "timer.h"

GSList* timer_list = 0;

int install_timer(int value_sec, int value_nsec, int interval_sec, int interval_nsec, void (*_callback)())
{
	if ( value_sec < 0 || interval_sec < 0 || _callback == 0 )
		return -1;

	int timer_fd;
	struct itimerspec timer;
	timer.it_value = (struct timespec){ .tv_sec=value_sec, .tv_nsec=value_nsec };
	timer.it_interval = (struct timespec){ .tv_sec=interval_sec, .tv_nsec=interval_nsec };
	timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
	timerfd_settime(timer_fd, 0, &timer, 0);
	struct timer* t = malloc(sizeof(struct timer));
	t->id=timer_fd;
	t->_callback = _callback;
	timer_list = g_slist_prepend(timer_list, t);

	int flags = fcntl( timer_fd, F_GETFL, 0 );
	if( flags != -1 )
		fcntl( timer_fd, F_SETFL, flags | O_NONBLOCK );

	return timer_fd;
}


void reset_timer(int id, int value_sec, int value_nsec, int interval_sec, int interval_nsec)
{
	struct itimerspec timer;
	timer.it_value = (struct timespec){ .tv_sec=value_sec, .tv_nsec=value_nsec };
	timer.it_interval = (struct timespec){ .tv_sec=interval_sec, .tv_nsec=interval_nsec };
	timerfd_settime(id, 0, &timer, 0);
}


void uninstall_timer(int id)
{
	close(id);
	GSList* timer_iter = timer_list;
	while (timer_iter) {
		struct timer* t = timer_iter->data;
		if (t->id == id) {
			timer_list = g_slist_remove(timer_list, t);
			free(t);
			return;
		}
		timer_iter = timer_iter->next;
	}
}