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 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 Timeout
145 {
146 	
147 	/** Holds all timeout delegates */
148 	bool delegate()[] timeoutListeners;
149 	/** our gtk timeout ID */
150 	uint timeoutID;
151 	
152 	
153 	/**
154 	 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
155 	 *
156 	 * Note that timeout functions may be delayed, due to the processing of other
157 	 * event sources. Thus they should not be relied on for precise timing.
158 	 * After each call to the timeout function, the time of the next timeout is
159 	 * recalculated based on the current time and the given interval
160 	 * (it does not try to 'catch up' time lost in delays).
161 	 * Params:
162 	 *    	interval = 	the timeout in milieconds
163 	 *    	delegate() = 	the delegate to be executed
164 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
165 	 */
166 	this(uint interval, bool delegate() dlg, bool fireNow=false)
167 	{
168 		timeoutListeners ~= dlg;
169 		timeoutID = g_timeout_add(interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
170 		if ( fireNow )
171 		{
172 			if ( !dlg() )
173 			{
174 				timeoutListeners.length = 0;
175 			}
176 		}
177 	}
178 	
179 	/**
180 	 * Creates a new timeout cycle.
181 	 * Params:
182 	 *    	interval = 	the timeout in milieconds
183 	 *    	delegate() = 	the delegate to be executed
184 	 *      priority = Priority for the timeout function
185 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
186 	 */
187 	this(uint interval, bool delegate() dlg, GPriority priority, bool fireNow=false)
188 	{
189 		timeoutListeners ~= dlg;
190 		timeoutID = g_timeout_add_full(priority, interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
191 		if ( fireNow )
192 		{
193 			if ( !dlg() )
194 			{
195 				timeoutListeners.length = 0;
196 			}
197 		}
198 	}
199 	
200 	/**
201 	 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
202 	 * Params:
203 	 *    	delegate() = 	the delegate to be executed
204 	 *      seconds = interval in seconds.
205 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
206 	 */
207 	this(bool delegate() dlg, uint seconds, bool fireNow=false)
208 	{
209 		timeoutListeners ~= dlg;
210 		timeoutID = g_timeout_add_seconds(seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
211 		if ( fireNow )
212 		{
213 			if ( !dlg() )
214 			{
215 				timeoutListeners.length = 0;
216 			}
217 		}
218 	}
219 	
220 	/**
221 	 * Creates a new timeout cycle.
222 	 * Params:
223 	 *    	delegate() = 	the delegate to be executed
224 	 *      seconds = interval in seconds.
225 	 *      priority = Priority for the timeout function
226 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
227 	 */
228 	this(bool delegate() dlg, uint seconds, GPriority priority, bool fireNow=false)
229 	{
230 		timeoutListeners ~= dlg;
231 		timeoutID = g_timeout_add_seconds_full(priority, seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
232 		if ( fireNow )
233 		{
234 			if ( !dlg() )
235 			{
236 				timeoutListeners.length = 0;
237 			}
238 		}
239 	}
240 	
241 	/** */
242 	public void stop()
243 	{
244 		if ( timeoutID > 0 )
245 		{
246 			g_source_remove(timeoutID);
247 		}
248 		timeoutListeners.length = 0;
249 	}
250 	
251 	/**
252 	 * Removes the timeout from gtk
253 	 */
254 	~this()
255 	{
256 		stop();
257 	}
258 	
259 	/**
260 	 * Adds a new delegate to this timeout cycle
261 	 * Params:
262 	 *    	dlg =
263 	 *    	fireNow =
264 	 */
265 	public void addListener(bool delegate() dlg, bool fireNow=false)
266 	{
267 		timeoutListeners ~= dlg;
268 		if ( fireNow )
269 		{
270 			if ( !dlg() )
271 			{
272 				timeoutListeners.length = timeoutListeners.length - 1;
273 			}
274 		}
275 	}
276 	
277 	/**
278 	 * The callback execution from glib
279 	 * Params:
280 	 *    	timeout =
281 	 * Returns:
282 	 */
283 	extern(C) static bool timeoutCallback(Timeout timeout)
284 	{
285 		return timeout.callAllListeners();
286 	}
287 	
288 	/**
289 	 * Executes all delegates on the execution list
290 	 * Returns:
291 	 */
292 	private bool callAllListeners()
293 	{
294 		bool runAgain = false;
295 		
296 		int i = 0;
297 		
298 		while ( i<timeoutListeners.length )
299 		{
300 			if ( !timeoutListeners[i]() )
301 			{
302 				timeoutListeners = timeoutListeners[0..i] ~ timeoutListeners[i+1..timeoutListeners.length];
303 			}
304 			else
305 			{
306 				runAgain = true;
307 				++i;
308 			}
309 		}
310 		return runAgain;
311 	}
312 	
313 	/**
314 	 */
315 	
316 	/**
317 	 * Creates a new timeout source.
318 	 * The source will not initially be associated with any GMainContext
319 	 * and must be added to one with g_source_attach() before it will be
320 	 * executed.
321 	 * The interval given is in terms of monotonic time, not wall clock
322 	 * time. See g_get_monotonic_time().
323 	 * Params:
324 	 * interval = the timeout interval in milliseconds.
325 	 * Returns: the newly-created timeout source
326 	 */
327 	public static Source sourceNew(uint interval)
328 	{
329 		// GSource * g_timeout_source_new (guint interval);
330 		auto p = g_timeout_source_new(interval);
331 		
332 		if(p is null)
333 		{
334 			return null;
335 		}
336 		
337 		return new Source(cast(GSource*) p);
338 	}
339 	
340 	/**
341 	 * Creates a new timeout source.
342 	 * The source will not initially be associated with any GMainContext
343 	 * and must be added to one with g_source_attach() before it will be
344 	 * executed.
345 	 * The scheduling granularity/accuracy of this timeout source will be
346 	 * in seconds.
347 	 * The interval given in terms of monotonic time, not wall clock time.
348 	 * See g_get_monotonic_time().
349 	 * Since 2.14
350 	 * Params:
351 	 * interval = the timeout interval in seconds
352 	 * Returns: the newly-created timeout source
353 	 */
354 	public static Source sourceNewSeconds(uint interval)
355 	{
356 		// GSource * g_timeout_source_new_seconds (guint interval);
357 		auto p = g_timeout_source_new_seconds(interval);
358 		
359 		if(p is null)
360 		{
361 			return null;
362 		}
363 		
364 		return new Source(cast(GSource*) p);
365 	}
366 	
367 	/**
368 	 * Sets a function to be called at regular intervals, with the default
369 	 * priority, G_PRIORITY_DEFAULT. The function is called repeatedly
370 	 * until it returns FALSE, at which point the timeout is automatically
371 	 * destroyed and the function will not be called again. The first call
372 	 * to the function will be at the end of the first interval.
373 	 * Note that timeout functions may be delayed, due to the processing of other
374 	 * event sources. Thus they should not be relied on for precise timing.
375 	 * After each call to the timeout function, the time of the next
376 	 * timeout is recalculated based on the current time and the given interval
377 	 * (it does not try to 'catch up' time lost in delays).
378 	 * If you want to have a timer in the "seconds" range and do not care
379 	 * about the exact time of the first call of the timer, use the
380 	 * g_timeout_add_seconds() function; this function allows for more
381 	 * optimizations and more efficient system power usage.
382 	 * This internally creates a main loop source using g_timeout_source_new()
383 	 * and attaches it to the main loop context using g_source_attach(). You can
384 	 * do these steps manually if you need greater control.
385 	 * The interval given is in terms of monotonic time, not wall clock
386 	 * time. See g_get_monotonic_time().
387 	 * Params:
388 	 * interval = the time between calls to the function, in milliseconds
389 	 * (1/1000ths of a second)
390 	 * data = data to pass to function
391 	 * Returns: the ID (greater than 0) of the event source.
392 	 */
393 	public static uint add(uint interval, GSourceFunc funct, void* data)
394 	{
395 		// guint g_timeout_add (guint interval,  GSourceFunc function,  gpointer data);
396 		return g_timeout_add(interval, funct, data);
397 	}
398 	
399 	/**
400 	 * Sets a function to be called at regular intervals, with the given
401 	 * priority. The function is called repeatedly until it returns
402 	 * FALSE, at which point the timeout is automatically destroyed and
403 	 * the function will not be called again. The notify function is
404 	 * called when the timeout is destroyed. The first call to the
405 	 * function will be at the end of the first interval.
406 	 * Note that timeout functions may be delayed, due to the processing of other
407 	 * event sources. Thus they should not be relied on for precise timing.
408 	 * After each call to the timeout function, the time of the next
409 	 * timeout is recalculated based on the current time and the given interval
410 	 * (it does not try to 'catch up' time lost in delays).
411 	 * This internally creates a main loop source using g_timeout_source_new()
412 	 * and attaches it to the main loop context using g_source_attach(). You can
413 	 * do these steps manually if you need greater control.
414 	 * The interval given in terms of monotonic time, not wall clock time.
415 	 * See g_get_monotonic_time().
416 	 * Params:
417 	 * priority = the priority of the timeout source. Typically this will be in
418 	 * the range between G_PRIORITY_DEFAULT and G_PRIORITY_HIGH.
419 	 * interval = the time between calls to the function, in milliseconds
420 	 * (1/1000ths of a second)
421 	 * data = data to pass to function
422 	 * notify = function to call when the timeout is removed, or NULL. [allow-none]
423 	 * Returns: the ID (greater than 0) of the event source. Rename to: g_timeout_add
424 	 */
425 	public static uint addFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify)
426 	{
427 		// guint g_timeout_add_full (gint priority,  guint interval,  GSourceFunc function,  gpointer data,  GDestroyNotify notify);
428 		return g_timeout_add_full(priority, interval, funct, data, notify);
429 	}
430 	
431 	/**
432 	 * Sets a function to be called at regular intervals with the default
433 	 * priority, G_PRIORITY_DEFAULT. The function is called repeatedly until
434 	 * it returns FALSE, at which point the timeout is automatically destroyed
435 	 * and the function will not be called again.
436 	 * This internally creates a main loop source using
437 	 * g_timeout_source_new_seconds() and attaches it to the main loop context
438 	 * using g_source_attach(). You can do these steps manually if you need
439 	 * greater control. Also see g_timeout_add_seconds_full().
440 	 * Note that the first call of the timer may not be precise for timeouts
441 	 * of one second. If you need finer precision and have such a timeout,
442 	 * you may want to use g_timeout_add() instead.
443 	 * The interval given is in terms of monotonic time, not wall clock
444 	 * time. See g_get_monotonic_time().
445 	 * Since 2.14
446 	 * Params:
447 	 * interval = the time between calls to the function, in seconds
448 	 * data = data to pass to function
449 	 * Returns: the ID (greater than 0) of the event source.
450 	 */
451 	public static uint addSeconds(uint interval, GSourceFunc funct, void* data)
452 	{
453 		// guint g_timeout_add_seconds (guint interval,  GSourceFunc function,  gpointer data);
454 		return g_timeout_add_seconds(interval, funct, data);
455 	}
456 	
457 	/**
458 	 * Sets a function to be called at regular intervals, with priority.
459 	 * The function is called repeatedly until it returns FALSE, at which
460 	 * point the timeout is automatically destroyed and the function will
461 	 * not be called again.
462 	 * Unlike g_timeout_add(), this function operates at whole second granularity.
463 	 * The initial starting point of the timer is determined by the implementation
464 	 * and the implementation is expected to group multiple timers together so that
465 	 * they fire all at the same time.
466 	 * To allow this grouping, the interval to the first timer is rounded
467 	 * and can deviate up to one second from the specified interval.
468 	 * Subsequent timer iterations will generally run at the specified interval.
469 	 * Note that timeout functions may be delayed, due to the processing of other
470 	 * event sources. Thus they should not be relied on for precise timing.
471 	 * After each call to the timeout function, the time of the next
472 	 * timeout is recalculated based on the current time and the given interval
473 	 * If you want timing more precise than whole seconds, use g_timeout_add()
474 	 * instead.
475 	 * The grouping of timers to fire at the same time results in a more power
476 	 * and CPU efficient behavior so if your timer is in multiples of seconds
477 	 * and you don't require the first timer exactly one second from now, the
478 	 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
479 	 * This internally creates a main loop source using
480 	 * g_timeout_source_new_seconds() and attaches it to the main loop context
481 	 * using g_source_attach(). You can do these steps manually if you need
482 	 * greater control.
483 	 * The interval given is in terms of monotonic time, not wall clock
484 	 * time. See g_get_monotonic_time().
485 	 * Since 2.14
486 	 * Params:
487 	 * priority = the priority of the timeout source. Typically this will be in
488 	 * the range between G_PRIORITY_DEFAULT and G_PRIORITY_HIGH.
489 	 * interval = the time between calls to the function, in seconds
490 	 * data = data to pass to function
491 	 * notify = function to call when the timeout is removed, or NULL. [allow-none]
492 	 * Returns: the ID (greater than 0) of the event source. Rename to: g_timeout_add_seconds
493 	 */
494 	public static uint addSecondsFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify)
495 	{
496 		// guint g_timeout_add_seconds_full (gint priority,  guint interval,  GSourceFunc function,  gpointer data,  GDestroyNotify notify);
497 		return g_timeout_add_seconds_full(priority, interval, funct, data, notify);
498 	}
499 }