1 /*
2  * Copyright (C) 2008 2009 2010, Magnus Hjorth
3  *
4  * This file is part of mhWaveEdit.
5  *
6  * mhWaveEdit is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * mhWaveEdit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mhWaveEdit; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #ifndef MAINLOOP_H_INCLUDED
22 #define MAINLOOP_H_INCLUDED
23 
24 #include <gtk/gtk.h>
25 
26 void mainloop(void);
27 void mainloop_recurse_on(gpointer *sources, int n_sources);
28 
29 typedef void (*iosource_cb)(gpointer iosource, int fd, gushort revents,
30 			    gpointer user_data);
31 
32 /* fd is -1 if timeout occurred
33  * Return value: <0=Disable, 0=Wait for additional events, >0=Re-enable all events
34  * For timeout,  <0=Disable watchdog, 0=Restart watchdog and wait for same events, >0=Re-enable all events */
35 typedef int (*iogroup_cb)(gpointer iogroup, int fd, gushort revents, gpointer user_data);
36 
37 /* Return value: 0=disable, >0=call again in X millis from current_time
38  * <0=call again in X millis from nominal time */
39 typedef gint (*timesource_cb)(gpointer timesource, GTimeVal *current_time,
40 			      gpointer user_data);
41 /* Return value:
42  * <0: May sleep
43  * 0:  Execute other event sources but do not sleep
44  * >0: Do not execute any other event sources
45  */
46 typedef int (*constsource_cb)(gpointer csource, gpointer user_data);
47 
48 gpointer mainloop_io_source_add(int fd, gushort events, iosource_cb cb,
49 				gpointer user_data);
50 void mainloop_io_source_set_events(gpointer iosource, gushort new_events);
51 void mainloop_io_source_enable(gpointer iosource, gboolean enable);
52 void mainloop_io_source_free(gpointer iosource);
53 
54 gpointer mainloop_io_group_add(int nfds, GPollFD *pfds, int wdtime_ms,
55 			       iogroup_cb cb, gpointer user_data);
56 void mainloop_io_group_enable(gpointer iogroup, gboolean enable);
57 void mainloop_io_group_free(gpointer iogroup);
58 
59 gpointer mainloop_time_source_add(GTimeVal *tv, timesource_cb cb,
60 				  gpointer user_data);
61 void mainloop_time_source_restart(gpointer timesource, GTimeVal *new_tv);
62 void mainloop_time_source_free(gpointer timesource);
63 gboolean mainloop_time_source_enabled(gpointer timesource);
64 
65 gpointer mainloop_constant_source_add(constsource_cb cb, gpointer user_data,
66 				      gboolean lowprio);
67 void mainloop_constant_source_enable(gpointer constsource, gboolean enable);
68 void mainloop_constant_source_free(gpointer constsource);
69 
70 /* Convenience wrappers */
71 
72 typedef void (*defer_once_cb)(gpointer user_data);
73 void mainloop_defer_once(defer_once_cb cb, gint reltime, gpointer user_data);
74 
75 #endif
76