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 = Timeout
29  * strct   = 
30  * realStrct=
31  * ctorStrct=
32  * clss    = Timeout
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_timeout_
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.Timeout;
55 
56 public  import gtkc.glibtypes;
57 
58 private import gtkc.glib;
59 private import glib.ConstructionException;
60 
61 
62 private import glib.Source;
63 
64 
65 
66 
67 /**
68  * The main event loop manages all the available sources of events for
69  * GLib and GTK+ applications. These events can come from any number of
70  * different types of sources such as file descriptors (plain files,
71  * pipes or sockets) and timeouts. New types of event sources can also
72  * be added using g_source_attach().
73  *
74  * To allow multiple independent sets of sources to be handled in
75  * different threads, each source is associated with a GMainContext.
76  * A GMainContext can only be running in a single thread, but
77  * sources can be added to it and removed from it from other threads.
78  *
79  * Each event source is assigned a priority. The default priority,
80  * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
81  * Values greater than 0 denote lower priorities. Events from high priority
82  * sources are always processed before events from lower priority sources.
83  *
84  * Idle functions can also be added, and assigned a priority. These will
85  * be run whenever no events with a higher priority are ready to be processed.
86  *
87  * The GMainLoop data type represents a main event loop. A GMainLoop is
88  * created with g_main_loop_new(). After adding the initial event sources,
89  * g_main_loop_run() is called. This continuously checks for new events from
90  * each of the event sources and dispatches them. Finally, the processing of
91  * an event from one of the sources leads to a call to g_main_loop_quit() to
92  * exit the main loop, and g_main_loop_run() returns.
93  *
94  * It is possible to create new instances of GMainLoop recursively.
95  * This is often used in GTK+ applications when showing modal dialog
96  * boxes. Note that event sources are associated with a particular
97  * GMainContext, and will be checked and dispatched for all main
98  * loops associated with that GMainContext.
99  *
100  * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
101  * gtk_main_quit() and gtk_events_pending().
102  *
103  * Creating new source types
104  *
105  * One of the unusual features of the GMainLoop functionality
106  * is that new types of event source can be created and used in
107  * addition to the builtin type of event source. A new event source
108  * type is used for handling GDK events. A new source type is created
109  * by deriving from the GSource structure.
110  * The derived type of source is represented by a structure that has
111  * the GSource structure as a first element, and other elements specific
112  * to the new source type. To create an instance of the new source type,
113  * call g_source_new() passing in the size of the derived structure and
114  * a table of functions. These GSourceFuncs determine the behavior of
115  * the new source type.
116  *
117  * New source types basically interact with the main context
118  * in two ways. Their prepare function in GSourceFuncs can set a timeout
119  * to determine the maximum amount of time that the main loop will sleep
120  * before checking the source again. In addition, or as well, the source
121  * can add file descriptors to the set that the main context checks using
122  * g_source_add_poll().
123  *
124  * <hr>
125  *
126  * Customizing the main loop iteration
127  *
128  * Single iterations of a GMainContext can be run with
129  * g_main_context_iteration(). In some cases, more detailed control
130  * of exactly how the details of the main loop work is desired, for
131  * instance, when integrating the GMainLoop with an external main loop.
132  * In such cases, you can call the component functions of
133  * g_main_context_iteration() directly. These functions are
134  * g_main_context_prepare(), g_main_context_query(),
135  * g_main_context_check() and g_main_context_dispatch().
136  *
137  * The operation of these functions can best be seen in terms
138  * of a state diagram, as shown in Figure 1, “States of a Main Context”.
139  *
140  * Figure 1. States of a Main Context
141  *
142  * On Unix, the GLib mainloop is incompatible with fork(). Any program
143  * using the mainloop must either exec() or exit() from the child
144  * without returning to the mainloop.
145  */
146 public class Timeout
147 {
148 	
149 	/** Holds all timeout delegates */
150 	bool delegate()[] timeoutListeners;
151 	/** our gtk timeout ID */
152 	uint timeoutID;
153 	
154 	
155 	/**
156 	 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
157 	 *
158 	 * Note that timeout functions may be delayed, due to the processing of other
159 	 * event sources. Thus they should not be relied on for precise timing.
160 	 * After each call to the timeout function, the time of the next timeout is
161 	 * recalculated based on the current time and the given interval
162 	 * (it does not try to 'catch up' time lost in delays).
163 	 * Params:
164 	 *    	interval = 	the timeout in milieconds
165 	 *    	delegate() = 	the delegate to be executed
166 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
167 	 */
168 	this(uint interval, bool delegate() dlg, bool fireNow=false)
169 	{
170 		timeoutListeners ~= dlg;
171 		timeoutID = g_timeout_add(interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
172 		if ( fireNow )
173 		{
174 			if ( !dlg() )
175 			{
176 				timeoutListeners.length = 0;
177 			}
178 		}
179 	}
180 	
181 	/**
182 	 * Creates a new timeout cycle.
183 	 * Params:
184 	 *    	interval = 	the timeout in milieconds
185 	 *    	delegate() = 	the delegate to be executed
186 	 *      priority = Priority for the timeout function
187 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
188 	 */
189 	this(uint interval, bool delegate() dlg, GPriority priority, bool fireNow=false)
190 	{
191 		timeoutListeners ~= dlg;
192 		timeoutID = g_timeout_add_full(priority, interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
193 		if ( fireNow )
194 		{
195 			if ( !dlg() )
196 			{
197 				timeoutListeners.length = 0;
198 			}
199 		}
200 	}
201 	
202 	/**
203 	 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
204 	 * Params:
205 	 *    	delegate() = 	the delegate to be executed
206 	 *      seconds = interval in seconds.
207 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
208 	 */
209 	this(bool delegate() dlg, uint seconds, bool fireNow=false)
210 	{
211 		timeoutListeners ~= dlg;
212 		timeoutID = g_timeout_add_seconds(seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
213 		if ( fireNow )
214 		{
215 			if ( !dlg() )
216 			{
217 				timeoutListeners.length = 0;
218 			}
219 		}
220 	}
221 	
222 	/**
223 	 * Creates a new timeout cycle.
224 	 * Params:
225 	 *    	delegate() = 	the delegate to be executed
226 	 *      seconds = interval in seconds.
227 	 *      priority = Priority for the timeout function
228 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
229 	 */
230 	this(bool delegate() dlg, uint seconds, GPriority priority, bool fireNow=false)
231 	{
232 		timeoutListeners ~= dlg;
233 		timeoutID = g_timeout_add_seconds_full(priority, seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
234 		if ( fireNow )
235 		{
236 			if ( !dlg() )
237 			{
238 				timeoutListeners.length = 0;
239 			}
240 		}
241 	}
242 	
243 	/** */
244 	public void stop()
245 	{
246 		if ( timeoutID > 0 )
247 		{
248 			g_source_remove(timeoutID);
249 		}
250 		timeoutListeners.length = 0;
251 	}
252 	
253 	/**
254 	 * Removes the timeout from gtk
255 	 */
256 	~this()
257 	{
258 		stop();
259 	}
260 	
261 	/**
262 	 * Adds a new delegate to this timeout cycle
263 	 * Params:
264 	 *    	dlg =
265 	 *    	fireNow =
266 	 */
267 	public void addListener(bool delegate() dlg, bool fireNow=false)
268 	{
269 		timeoutListeners ~= dlg;
270 		if ( fireNow )
271 		{
272 			if ( !dlg() )
273 			{
274 				timeoutListeners.length = timeoutListeners.length - 1;
275 			}
276 		}
277 	}
278 	
279 	/**
280 	 * The callback execution from glib
281 	 * Params:
282 	 *    	timeout =
283 	 * Returns:
284 	 */
285 	extern(C) static bool timeoutCallback(Timeout timeout)
286 	{
287 		return timeout.callAllListeners();
288 	}
289 	
290 	/**
291 	 * Executes all delegates on the execution list
292 	 * Returns:
293 	 */
294 	private bool callAllListeners()
295 	{
296 		bool runAgain = false;
297 		
298 		int i = 0;
299 		
300 		while ( i<timeoutListeners.length )
301 		{
302 			if ( !timeoutListeners[i]() )
303 			{
304 				timeoutListeners = timeoutListeners[0..i] ~ timeoutListeners[i+1..timeoutListeners.length];
305 			}
306 			else
307 			{
308 				runAgain = true;
309 				++i;
310 			}
311 		}
312 		return runAgain;
313 	}
314 	
315 	/**
316 	 */
317 	
318 	/**
319 	 * Creates a new timeout source.
320 	 * The source will not initially be associated with any GMainContext
321 	 * and must be added to one with g_source_attach() before it will be
322 	 * executed.
323 	 * The interval given is in terms of monotonic time, not wall clock
324 	 * time. See g_get_monotonic_time().
325 	 * Params:
326 	 * interval = the timeout interval in milliseconds.
327 	 * Returns: the newly-created timeout source
328 	 */
329 	public static Source sourceNew(uint interval)
330 	{
331 		// GSource * g_timeout_source_new (guint interval);
332 		auto p = g_timeout_source_new(interval);
333 		
334 		if(p is null)
335 		{
336 			return null;
337 		}
338 		
339 		return new Source(cast(GSource*) p);
340 	}
341 	
342 	/**
343 	 * Creates a new timeout source.
344 	 * The source will not initially be associated with any GMainContext
345 	 * and must be added to one with g_source_attach() before it will be
346 	 * executed.
347 	 * The scheduling granularity/accuracy of this timeout source will be
348 	 * in seconds.
349 	 * The interval given in terms of monotonic time, not wall clock time.
350 	 * See g_get_monotonic_time().
351 	 * Since 2.14
352 	 * Params:
353 	 * interval = the timeout interval in seconds
354 	 * Returns: the newly-created timeout source
355 	 */
356 	public static Source sourceNewSeconds(uint interval)
357 	{
358 		// GSource * g_timeout_source_new_seconds (guint interval);
359 		auto p = g_timeout_source_new_seconds(interval);
360 		
361 		if(p is null)
362 		{
363 			return null;
364 		}
365 		
366 		return new Source(cast(GSource*) p);
367 	}
368 	
369 	/**
370 	 * Sets a function to be called at regular intervals, with the default
371 	 * priority, G_PRIORITY_DEFAULT. The function is called repeatedly
372 	 * until it returns FALSE, at which point the timeout is automatically
373 	 * destroyed and the function will not be called again. The first call
374 	 * to the function will be at the end of the first interval.
375 	 * Note that timeout functions may be delayed, due to the processing of other
376 	 * event sources. Thus they should not be relied on for precise timing.
377 	 * After each call to the timeout function, the time of the next
378 	 * timeout is recalculated based on the current time and the given interval
379 	 * (it does not try to 'catch up' time lost in delays).
380 	 * If you want to have a timer in the "seconds" range and do not care
381 	 * about the exact time of the first call of the timer, use the
382 	 * g_timeout_add_seconds() function; this function allows for more
383 	 * optimizations and more efficient system power usage.
384 	 * This internally creates a main loop source using g_timeout_source_new()
385 	 * and attaches it to the main loop context using g_source_attach(). You can
386 	 * do these steps manually if you need greater control.
387 	 * The interval given is in terms of monotonic time, not wall clock
388 	 * time. See g_get_monotonic_time().
389 	 * Params:
390 	 * interval = the time between calls to the function, in milliseconds
391 	 * (1/1000ths of a second)
392 	 * data = data to pass to function
393 	 * Returns: the ID (greater than 0) of the event source.
394 	 */
395 	public static uint add(uint interval, GSourceFunc funct, void* data)
396 	{
397 		// guint g_timeout_add (guint interval,  GSourceFunc function,  gpointer data);
398 		return g_timeout_add(interval, funct, data);
399 	}
400 	
401 	/**
402 	 * Sets a function to be called at regular intervals, with the given
403 	 * priority. The function is called repeatedly until it returns
404 	 * FALSE, at which point the timeout is automatically destroyed and
405 	 * the function will not be called again. The notify function is
406 	 * called when the timeout is destroyed. The first call to the
407 	 * function will be at the end of the first interval.
408 	 * Note that timeout functions may be delayed, due to the processing of other
409 	 * event sources. Thus they should not be relied on for precise timing.
410 	 * After each call to the timeout function, the time of the next
411 	 * timeout is recalculated based on the current time and the given interval
412 	 * (it does not try to 'catch up' time lost in delays).
413 	 * This internally creates a main loop source using g_timeout_source_new()
414 	 * and attaches it to the main loop context using g_source_attach(). You can
415 	 * do these steps manually if you need greater control.
416 	 * The interval given in terms of monotonic time, not wall clock time.
417 	 * See g_get_monotonic_time().
418 	 * Params:
419 	 * priority = the priority of the timeout source. Typically this will be in
420 	 * the range between G_PRIORITY_DEFAULT and G_PRIORITY_HIGH.
421 	 * interval = the time between calls to the function, in milliseconds
422 	 * (1/1000ths of a second)
423 	 * data = data to pass to function
424 	 * notify = function to call when the timeout is removed, or NULL. [allow-none]
425 	 * Returns: the ID (greater than 0) of the event source. Rename to: g_timeout_add
426 	 */
427 	public static uint addFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify)
428 	{
429 		// guint g_timeout_add_full (gint priority,  guint interval,  GSourceFunc function,  gpointer data,  GDestroyNotify notify);
430 		return g_timeout_add_full(priority, interval, funct, data, notify);
431 	}
432 	
433 	/**
434 	 * Sets a function to be called at regular intervals with the default
435 	 * priority, G_PRIORITY_DEFAULT. The function is called repeatedly until
436 	 * it returns FALSE, at which point the timeout is automatically destroyed
437 	 * and the function will not be called again.
438 	 * This internally creates a main loop source using
439 	 * g_timeout_source_new_seconds() and attaches it to the main loop context
440 	 * using g_source_attach(). You can do these steps manually if you need
441 	 * greater control. Also see g_timeout_add_seconds_full().
442 	 * Note that the first call of the timer may not be precise for timeouts
443 	 * of one second. If you need finer precision and have such a timeout,
444 	 * you may want to use g_timeout_add() instead.
445 	 * The interval given is in terms of monotonic time, not wall clock
446 	 * time. See g_get_monotonic_time().
447 	 * Since 2.14
448 	 * Params:
449 	 * interval = the time between calls to the function, in seconds
450 	 * data = data to pass to function
451 	 * Returns: the ID (greater than 0) of the event source.
452 	 */
453 	public static uint addSeconds(uint interval, GSourceFunc funct, void* data)
454 	{
455 		// guint g_timeout_add_seconds (guint interval,  GSourceFunc function,  gpointer data);
456 		return g_timeout_add_seconds(interval, funct, data);
457 	}
458 	
459 	/**
460 	 * Sets a function to be called at regular intervals, with priority.
461 	 * The function is called repeatedly until it returns FALSE, at which
462 	 * point the timeout is automatically destroyed and the function will
463 	 * not be called again.
464 	 * Unlike g_timeout_add(), this function operates at whole second granularity.
465 	 * The initial starting point of the timer is determined by the implementation
466 	 * and the implementation is expected to group multiple timers together so that
467 	 * they fire all at the same time.
468 	 * To allow this grouping, the interval to the first timer is rounded
469 	 * and can deviate up to one second from the specified interval.
470 	 * Subsequent timer iterations will generally run at the specified interval.
471 	 * Note that timeout functions may be delayed, due to the processing of other
472 	 * event sources. Thus they should not be relied on for precise timing.
473 	 * After each call to the timeout function, the time of the next
474 	 * timeout is recalculated based on the current time and the given interval
475 	 * If you want timing more precise than whole seconds, use g_timeout_add()
476 	 * instead.
477 	 * The grouping of timers to fire at the same time results in a more power
478 	 * and CPU efficient behavior so if your timer is in multiples of seconds
479 	 * and you don't require the first timer exactly one second from now, the
480 	 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
481 	 * This internally creates a main loop source using
482 	 * g_timeout_source_new_seconds() and attaches it to the main loop context
483 	 * using g_source_attach(). You can do these steps manually if you need
484 	 * greater control.
485 	 * The interval given is in terms of monotonic time, not wall clock
486 	 * time. See g_get_monotonic_time().
487 	 * Since 2.14
488 	 * Params:
489 	 * priority = the priority of the timeout source. Typically this will be in
490 	 * the range between G_PRIORITY_DEFAULT and G_PRIORITY_HIGH.
491 	 * interval = the time between calls to the function, in seconds
492 	 * data = data to pass to function
493 	 * notify = function to call when the timeout is removed, or NULL. [allow-none]
494 	 * Returns: the ID (greater than 0) of the event source. Rename to: g_timeout_add_seconds
495 	 */
496 	public static uint addSecondsFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify)
497 	{
498 		// guint g_timeout_add_seconds_full (gint priority,  guint interval,  GSourceFunc function,  gpointer data,  GDestroyNotify notify);
499 		return g_timeout_add_seconds_full(priority, interval, funct, data, notify);
500 	}
501 }