1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = 
27  * outPack = glib
28  * outFile = Idle
29  * strct   = 
30  * realStrct=
31  * ctorStrct=
32  * clss    = Idle
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_idle_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Source
47  * structWrap:
48  * 	- GSource* -> Source
49  * module aliases:
50  * local aliases:
51  * overrides:
52  */
53 
54 module glib.Idle;
55 
56 public  import gtkc.glibtypes;
57 
58 private import gtkc.glib;
59 private import glib.ConstructionException;
60 
61 private import glib.Source;
62 
63 
64 
65 /**
66  * The main event loop manages all the available sources of events for
67  * GLib and GTK+ applications. These events can come from any number of
68  * different types of sources such as file descriptors (plain files,
69  * pipes or sockets) and timeouts. New types of event sources can also
70  * be added using g_source_attach().
71  *
72  * To allow multiple independent sets of sources to be handled in
73  * different threads, each source is associated with a GMainContext.
74  * A GMainContext can only be running in a single thread, but
75  * sources can be added to it and removed from it from other threads.
76  *
77  * Each event source is assigned a priority. The default priority,
78  * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
79  * Values greater than 0 denote lower priorities. Events from high priority
80  * sources are always processed before events from lower priority sources.
81  *
82  * Idle functions can also be added, and assigned a priority. These will
83  * be run whenever no events with a higher priority are ready to be processed.
84  *
85  * The GMainLoop data type represents a main event loop. A GMainLoop is
86  * created with g_main_loop_new(). After adding the initial event sources,
87  * g_main_loop_run() is called. This continuously checks for new events from
88  * each of the event sources and dispatches them. Finally, the processing of
89  * an event from one of the sources leads to a call to g_main_loop_quit() to
90  * exit the main loop, and g_main_loop_run() returns.
91  *
92  * It is possible to create new instances of GMainLoop recursively.
93  * This is often used in GTK+ applications when showing modal dialog
94  * boxes. Note that event sources are associated with a particular
95  * GMainContext, and will be checked and dispatched for all main
96  * loops associated with that GMainContext.
97  *
98  * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
99  * gtk_main_quit() and gtk_events_pending().
100  *
101  * Creating new source types
102  *
103  * One of the unusual features of the GMainLoop functionality
104  * is that new types of event source can be created and used in
105  * addition to the builtin type of event source. A new event source
106  * type is used for handling GDK events. A new source type is created
107  * by deriving from the GSource structure.
108  * The derived type of source is represented by a structure that has
109  * the GSource structure as a first element, and other elements specific
110  * to the new source type. To create an instance of the new source type,
111  * call g_source_new() passing in the size of the derived structure and
112  * a table of functions. These GSourceFuncs determine the behavior of
113  * the new source type.
114  *
115  * New source types basically interact with the main context
116  * in two ways. Their prepare function in GSourceFuncs can set a timeout
117  * to determine the maximum amount of time that the main loop will sleep
118  * before checking the source again. In addition, or as well, the source
119  * can add file descriptors to the set that the main context checks using
120  * g_source_add_poll().
121  *
122  * <hr>
123  *
124  * Customizing the main loop iteration
125  *
126  * Single iterations of a GMainContext can be run with
127  * g_main_context_iteration(). In some cases, more detailed control
128  * of exactly how the details of the main loop work is desired, for
129  * instance, when integrating the GMainLoop with an external main loop.
130  * In such cases, you can call the component functions of
131  * g_main_context_iteration() directly. These functions are
132  * g_main_context_prepare(), g_main_context_query(),
133  * g_main_context_check() and g_main_context_dispatch().
134  *
135  * The operation of these functions can best be seen in terms
136  * of a state diagram, as shown in Figure 1, “States of a Main Context”.
137  *
138  * Figure 1. States of a Main Context
139  *
140  * On Unix, the GLib mainloop is incompatible with fork(). Any program
141  * using the mainloop must either exec() or exit() from the child
142  * without returning to the mainloop.
143  */
144 public class Idle
145 {
146 	
147 	/** Holds all idle delegates */
148 	bool delegate()[] idleListeners;
149 	/** our idle ID */
150 	uint idleID;
151 	
152 	/**
153 	 * Creates a new idle cycle.
154 	 * Params:
155 	 *    	interval = the idle in milieconds
156 	 *    	dlg = the delegate to be executed
157 	 *    	fireNow = When true the delegate will be executed emmidiatly
158 	 */
159 	this(bool delegate() dlg, bool fireNow=false)
160 	{
161 		idleListeners ~= dlg;
162 		idleID = g_idle_add(cast(GSourceFunc)&idleCallback, cast(void*)this);
163 		if ( fireNow )
164 		{
165 			if ( !dlg() )
166 			{
167 				idleListeners.length = 0;
168 			}
169 		}
170 	}
171 	
172 	/**
173 	 * Creates a new idle cycle.
174 	 * Params:
175 	 *    	dlg = the delegate to be executed
176 	 *      priority = Priority for the idle function
177 	 *    	fireNow = When true the delegate will be executed emmidiatly
178 	 */
179 	this(bool delegate() dlg, GPriority priority, bool fireNow=false)
180 	{
181 		idleListeners ~= dlg;
182 		idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, null);
183 		if ( fireNow )
184 		{
185 			if ( !dlg() )
186 			{
187 				idleListeners.length = 0;
188 			}
189 		}
190 	}
191 	
192 	/** */
193 	public void stop()
194 	{
195 		if ( idleID > 0 )
196 		{
197 			g_source_remove(idleID);
198 		}
199 		idleListeners.length = 0;
200 	}
201 	
202 	/**
203 	 * Removes the idle from gtk
204 	 */
205 	~this()
206 	{
207 		stop();
208 	}
209 	
210 	/**
211 	 * Adds a new delegate to this idle cycle
212 	 * Params:
213 	 *    	dlg =
214 	 *    	fireNow =
215 	 */
216 	public void addListener(bool delegate() dlg, bool fireNow=false)
217 	{
218 		idleListeners ~= dlg;
219 		if ( fireNow )
220 		{
221 			if ( !dlg() )
222 			{
223 				idleListeners.length = idleListeners.length - 1;
224 			}
225 		}
226 	}
227 	
228 	/**
229 	 * The callback execution from glib
230 	 * Params:
231 	 *    	idle =
232 	 * Returns:
233 	 */
234 	extern(C) static bool idleCallback(Idle idle)
235 	{
236 		return idle.callAllListeners();
237 	}
238 	
239 	/**
240 	 * Executes all delegates on the execution list
241 	 * Returns:
242 	 */
243 	private bool callAllListeners()
244 	{
245 		bool runAgain = false;
246 		
247 		int i = 0;
248 		
249 		while ( i<idleListeners.length )
250 		{
251 			if ( !idleListeners[i]() )
252 			{
253 				idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length];
254 			}
255 			else
256 			{
257 				runAgain = true;
258 				++i;
259 			}
260 		}
261 		return runAgain;
262 	}
263 	
264 	/**
265 	 */
266 	
267 	/**
268 	 * Creates a new idle source.
269 	 * The source will not initially be associated with any GMainContext
270 	 * and must be added to one with g_source_attach() before it will be
271 	 * executed. Note that the default priority for idle sources is
272 	 * G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
273 	 * have a default priority of G_PRIORITY_DEFAULT.
274 	 * Returns: the newly-created idle source
275 	 */
276 	public static Source sourceNew()
277 	{
278 		// GSource * g_idle_source_new (void);
279 		auto p = g_idle_source_new();
280 		
281 		if(p is null)
282 		{
283 			return null;
284 		}
285 		
286 		return new Source(cast(GSource*) p);
287 	}
288 	
289 	/**
290 	 * Adds a function to be called whenever there are no higher priority
291 	 * events pending to the default main loop. The function is given the
292 	 * default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function
293 	 * returns FALSE it is automatically removed from the list of event
294 	 * sources and will not be called again.
295 	 * This internally creates a main loop source using g_idle_source_new()
296 	 * and attaches it to the main loop context using g_source_attach().
297 	 * You can do these steps manually if you need greater control.
298 	 * Params:
299 	 * data = data to pass to function.
300 	 * Returns: the ID (greater than 0) of the event source.
301 	 */
302 	public static uint add(GSourceFunc funct, void* data)
303 	{
304 		// guint g_idle_add (GSourceFunc function,  gpointer data);
305 		return g_idle_add(funct, data);
306 	}
307 	
308 	/**
309 	 * Adds a function to be called whenever there are no higher priority
310 	 * events pending. If the function returns FALSE it is automatically
311 	 * removed from the list of event sources and will not be called again.
312 	 * This internally creates a main loop source using g_idle_source_new()
313 	 * and attaches it to the main loop context using g_source_attach().
314 	 * You can do these steps manually if you need greater control.
315 	 * Params:
316 	 * priority = the priority of the idle source. Typically this will be in the
317 	 * range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
318 	 * data = data to pass to function
319 	 * notify = function to call when the idle is removed, or NULL. [allow-none]
320 	 * Returns: the ID (greater than 0) of the event source. Rename to: g_idle_add
321 	 */
322 	public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
323 	{
324 		// guint g_idle_add_full (gint priority,  GSourceFunc function,  gpointer data,  GDestroyNotify notify);
325 		return g_idle_add_full(priority, funct, data, notify);
326 	}
327 	
328 	/**
329 	 * Removes the idle function with the given data.
330 	 * Params:
331 	 * data = the data for the idle source's callback.
332 	 * Returns: TRUE if an idle source was found and removed.
333 	 */
334 	public static int removeByData(void* data)
335 	{
336 		// gboolean g_idle_remove_by_data (gpointer data);
337 		return g_idle_remove_by_data(data);
338 	}
339 }