summaryrefslogtreecommitdiff
path: root/gtk/gtk_schedule.c
blob: 73568fa934addd2d6c4ea47c942a2f831b106f64 (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
/*
 * This file is part of NetSurf, http://netsurf-browser.org/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2006 Daniel Silverstone <dsilvers@digital-scurf.org>
 */

#include <glib.h>
#include <stdlib.h>

#include "netsurf/desktop/browser.h"

typedef struct {
	void (*callback)(void *);
	void *p;
	int die;
} _nsgtkcallback;

static GList *callbacks;

static gboolean ns_generic_gtk_callback(gpointer data)
{
	_nsgtkcallback *cb = (_nsgtkcallback*)(data);
	if(cb->die) {
		/* We got removed before we got fired off */
		free(cb);
		return FALSE;
	}
	cb->callback(cb->p);
	callbacks = g_list_remove(callbacks, cb);
	free(cb);
	return FALSE;
}

void schedule_remove(void (*callback)(void *p), void *p)
{
	_nsgtkcallback *cb;
        GList *l;
	l = callbacks;
	while(l) {
		cb = (_nsgtkcallback*)(l->data);
		if(cb->callback == callback && cb->p == p) {
			l = callbacks = g_list_remove(callbacks, cb);
			cb->die = 1;
		} else
			l = g_list_next(l);
	}
}

void schedule(int t, void (*callback)(void *p), void *p)
{
	_nsgtkcallback *cb = (_nsgtkcallback*)malloc(sizeof(_nsgtkcallback));
	schedule_remove(callback, p);
	cb->callback = callback;
	cb->p = p;
	cb->die = 0;
	callbacks = g_list_prepend(callbacks, cb);
	g_timeout_add(t * 10, ns_generic_gtk_callback, cb);
}

void schedule_run(void)
{
	/* Nothing to do, the running is done via the gtk mainloop of joy */
}