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 module glib.Timeout;
26 
27 private import glib.Source;
28 private import gtkc.glib;
29 public  import gtkc.glibtypes;
30 
31 
32 public class Timeout
33 {
34 	/** Holds all timeout delegates */
35 	bool delegate()[] timeoutListeners;
36 	/** our gtk timeout ID */
37 	uint timeoutID;
38 	
39 	
40 	/**
41 	 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
42 	 *
43 	 * Note that timeout functions may be delayed, due to the processing of other
44 	 * event sources. Thus they should not be relied on for precise timing.
45 	 * After each call to the timeout function, the time of the next timeout is
46 	 * recalculated based on the current time and the given interval
47 	 * (it does not try to 'catch up' time lost in delays).
48 	 * Params:
49 	 *    	interval = 	the timeout in milieconds
50 	 *    	delegate() = 	the delegate to be executed
51 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
52 	 */
53 	this(uint interval, bool delegate() dlg, bool fireNow=false)
54 	{
55 		timeoutListeners ~= dlg;
56 		timeoutID = g_timeout_add(interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
57 		if ( fireNow )
58 		{
59 			if ( !dlg() )
60 			{
61 				timeoutListeners.length = 0;
62 			}
63 		}
64 	}
65 	
66 	/**
67 	 * Creates a new timeout cycle.
68 	 * Params:
69 	 *    	interval = 	the timeout in milieconds
70 	 *    	delegate() = 	the delegate to be executed
71 	 *      priority = Priority for the timeout function
72 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
73 	 */
74 	this(uint interval, bool delegate() dlg, GPriority priority, bool fireNow=false)
75 	{
76 		timeoutListeners ~= dlg;
77 		timeoutID = g_timeout_add_full(priority, interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
78 		if ( fireNow )
79 		{
80 			if ( !dlg() )
81 			{
82 				timeoutListeners.length = 0;
83 			}
84 		}
85 	}
86 	
87 	/**
88 	 * Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
89 	 * Params:
90 	 *    	delegate() = 	the delegate to be executed
91 	 *      seconds = interval in seconds.
92 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
93 	 */
94 	this(bool delegate() dlg, uint seconds, bool fireNow=false)
95 	{
96 		timeoutListeners ~= dlg;
97 		timeoutID = g_timeout_add_seconds(seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
98 		if ( fireNow )
99 		{
100 			if ( !dlg() )
101 			{
102 				timeoutListeners.length = 0;
103 			}
104 		}
105 	}
106 	
107 	/**
108 	 * Creates a new timeout cycle.
109 	 * Params:
110 	 *    	delegate() = 	the delegate to be executed
111 	 *      seconds = interval in seconds.
112 	 *      priority = Priority for the timeout function
113 	 *    	fireNow = 	When true the delegate will be executed emmidiatly
114 	 */
115 	this(bool delegate() dlg, uint seconds, GPriority priority, bool fireNow=false)
116 	{
117 		timeoutListeners ~= dlg;
118 		timeoutID = g_timeout_add_seconds_full(priority, seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
119 		if ( fireNow )
120 		{
121 			if ( !dlg() )
122 			{
123 				timeoutListeners.length = 0;
124 			}
125 		}
126 	}
127 	
128 	/** */
129 	public void stop()
130 	{
131 		if ( timeoutID > 0 )
132 		{
133 			g_source_remove(timeoutID);
134 		}
135 		timeoutID = 0;
136 		timeoutListeners.length = 0;
137 	}
138 	
139 	/**
140 	 * Removes the timeout from gtk
141 	 */
142 	~this()
143 	{
144 		stop();
145 	}
146 	
147 	/**
148 	 * Adds a new delegate to this timeout cycle
149 	 * Params:
150 	 *    	dlg =
151 	 *    	fireNow =
152 	 */
153 	public void addListener(bool delegate() dlg, bool fireNow=false)
154 	{
155 		timeoutListeners ~= dlg;
156 		if ( fireNow )
157 		{
158 			if ( !dlg() )
159 			{
160 				timeoutListeners.length = timeoutListeners.length - 1;
161 			}
162 		}
163 	}
164 	
165 	/**
166 	 * The callback execution from glib
167 	 * Params:
168 	 *    	timeout =
169 	 * Returns:
170 	 */
171 	extern(C) static bool timeoutCallback(Timeout timeout)
172 	{
173 		return timeout.callAllListeners();
174 	}
175 	
176 	/**
177 	 * Executes all delegates on the execution list
178 	 * Returns:
179 	 */
180 	private bool callAllListeners()
181 	{
182 		bool runAgain = false;
183 		
184 		int i = 0;
185 		
186 		while ( i<timeoutListeners.length )
187 		{
188 			if ( !timeoutListeners[i]() )
189 			{
190 				timeoutListeners = timeoutListeners[0..i] ~ timeoutListeners[i+1..timeoutListeners.length];
191 			}
192 			else
193 			{
194 				runAgain = true;
195 				++i;
196 			}
197 		}
198 		return runAgain;
199 	}
200 
201 	/**
202 	 */
203 
204 	/**
205 	 * Sets a function to be called at regular intervals, with the default
206 	 * priority, #G_PRIORITY_DEFAULT.  The function is called repeatedly
207 	 * until it returns %FALSE, at which point the timeout is automatically
208 	 * destroyed and the function will not be called again.  The first call
209 	 * to the function will be at the end of the first @interval.
210 	 *
211 	 * Note that timeout functions may be delayed, due to the processing of other
212 	 * event sources. Thus they should not be relied on for precise timing.
213 	 * After each call to the timeout function, the time of the next
214 	 * timeout is recalculated based on the current time and the given interval
215 	 * (it does not try to 'catch up' time lost in delays).
216 	 *
217 	 * If you want to have a timer in the "seconds" range and do not care
218 	 * about the exact time of the first call of the timer, use the
219 	 * g_timeout_add_seconds() function; this function allows for more
220 	 * optimizations and more efficient system power usage.
221 	 *
222 	 * This internally creates a main loop source using g_timeout_source_new()
223 	 * and attaches it to the global #GMainContext using g_source_attach(), so
224 	 * the callback will be invoked in whichever thread is running that main
225 	 * context. You can do these steps manually if you need greater control or to
226 	 * use a custom main context.
227 	 *
228 	 * The interval given is in terms of monotonic time, not wall clock
229 	 * time.  See g_get_monotonic_time().
230 	 *
231 	 * Params:
232 	 *     interval = the time between calls to the function, in milliseconds
233 	 *         (1/1000ths of a second)
234 	 *     funct = function to call
235 	 *     data = data to pass to @function
236 	 *
237 	 * Return: the ID (greater than 0) of the event source.
238 	 */
239 	public static uint add(uint interval, GSourceFunc funct, void* data)
240 	{
241 		return g_timeout_add(interval, funct, data);
242 	}
243 
244 	/**
245 	 * Sets a function to be called at regular intervals, with the given
246 	 * priority.  The function is called repeatedly until it returns
247 	 * %FALSE, at which point the timeout is automatically destroyed and
248 	 * the function will not be called again.  The @notify function is
249 	 * called when the timeout is destroyed.  The first call to the
250 	 * function will be at the end of the first @interval.
251 	 *
252 	 * Note that timeout functions may be delayed, due to the processing of other
253 	 * event sources. Thus they should not be relied on for precise timing.
254 	 * After each call to the timeout function, the time of the next
255 	 * timeout is recalculated based on the current time and the given interval
256 	 * (it does not try to 'catch up' time lost in delays).
257 	 *
258 	 * This internally creates a main loop source using g_timeout_source_new()
259 	 * and attaches it to the global #GMainContext using g_source_attach(), so
260 	 * the callback will be invoked in whichever thread is running that main
261 	 * context. You can do these steps manually if you need greater control or to
262 	 * use a custom main context.
263 	 *
264 	 * The interval given in terms of monotonic time, not wall clock time.
265 	 * See g_get_monotonic_time().
266 	 *
267 	 * Params:
268 	 *     priority = the priority of the timeout source. Typically this will be in
269 	 *         the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
270 	 *     interval = the time between calls to the function, in milliseconds
271 	 *         (1/1000ths of a second)
272 	 *     funct = function to call
273 	 *     data = data to pass to @function
274 	 *     notify = function to call when the timeout is removed, or %NULL
275 	 *
276 	 * Return: the ID (greater than 0) of the event source.
277 	 */
278 	public static uint addFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify)
279 	{
280 		return g_timeout_add_full(priority, interval, funct, data, notify);
281 	}
282 
283 	/**
284 	 * Sets a function to be called at regular intervals with the default
285 	 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
286 	 * it returns %FALSE, at which point the timeout is automatically destroyed
287 	 * and the function will not be called again.
288 	 *
289 	 * This internally creates a main loop source using
290 	 * g_timeout_source_new_seconds() and attaches it to the main loop context
291 	 * using g_source_attach(). You can do these steps manually if you need
292 	 * greater control. Also see g_timeout_add_seconds_full().
293 	 *
294 	 * Note that the first call of the timer may not be precise for timeouts
295 	 * of one second. If you need finer precision and have such a timeout,
296 	 * you may want to use g_timeout_add() instead.
297 	 *
298 	 * The interval given is in terms of monotonic time, not wall clock
299 	 * time.  See g_get_monotonic_time().
300 	 *
301 	 * Params:
302 	 *     interval = the time between calls to the function, in seconds
303 	 *     funct = function to call
304 	 *     data = data to pass to @function
305 	 *
306 	 * Return: the ID (greater than 0) of the event source.
307 	 *
308 	 * Since: 2.14
309 	 */
310 	public static uint addSeconds(uint interval, GSourceFunc funct, void* data)
311 	{
312 		return g_timeout_add_seconds(interval, funct, data);
313 	}
314 
315 	/**
316 	 * Sets a function to be called at regular intervals, with @priority.
317 	 * The function is called repeatedly until it returns %FALSE, at which
318 	 * point the timeout is automatically destroyed and the function will
319 	 * not be called again.
320 	 *
321 	 * Unlike g_timeout_add(), this function operates at whole second granularity.
322 	 * The initial starting point of the timer is determined by the implementation
323 	 * and the implementation is expected to group multiple timers together so that
324 	 * they fire all at the same time.
325 	 * To allow this grouping, the @interval to the first timer is rounded
326 	 * and can deviate up to one second from the specified interval.
327 	 * Subsequent timer iterations will generally run at the specified interval.
328 	 *
329 	 * Note that timeout functions may be delayed, due to the processing of other
330 	 * event sources. Thus they should not be relied on for precise timing.
331 	 * After each call to the timeout function, the time of the next
332 	 * timeout is recalculated based on the current time and the given @interval
333 	 *
334 	 * If you want timing more precise than whole seconds, use g_timeout_add()
335 	 * instead.
336 	 *
337 	 * The grouping of timers to fire at the same time results in a more power
338 	 * and CPU efficient behavior so if your timer is in multiples of seconds
339 	 * and you don't require the first timer exactly one second from now, the
340 	 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
341 	 *
342 	 * This internally creates a main loop source using
343 	 * g_timeout_source_new_seconds() and attaches it to the main loop context
344 	 * using g_source_attach(). You can do these steps manually if you need
345 	 * greater control.
346 	 *
347 	 * The interval given is in terms of monotonic time, not wall clock
348 	 * time.  See g_get_monotonic_time().
349 	 *
350 	 * Params:
351 	 *     priority = the priority of the timeout source. Typically this will be in
352 	 *         the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
353 	 *     interval = the time between calls to the function, in seconds
354 	 *     funct = function to call
355 	 *     data = data to pass to @function
356 	 *     notify = function to call when the timeout is removed, or %NULL
357 	 *
358 	 * Return: the ID (greater than 0) of the event source.
359 	 *
360 	 * Since: 2.14
361 	 */
362 	public static uint addSecondsFull(int priority, uint interval, GSourceFunc funct, void* data, GDestroyNotify notify)
363 	{
364 		return g_timeout_add_seconds_full(priority, interval, funct, data, notify);
365 	}
366 
367 	/**
368 	 * Creates a new timeout source.
369 	 *
370 	 * The source will not initially be associated with any #GMainContext
371 	 * and must be added to one with g_source_attach() before it will be
372 	 * executed.
373 	 *
374 	 * The interval given is in terms of monotonic time, not wall clock
375 	 * time.  See g_get_monotonic_time().
376 	 *
377 	 * Params:
378 	 *     interval = the timeout interval in milliseconds.
379 	 *
380 	 * Return: the newly-created timeout source
381 	 */
382 	public static Source sourceNew(uint interval)
383 	{
384 		auto p = g_timeout_source_new(interval);
385 		
386 		if(p is null)
387 		{
388 			return null;
389 		}
390 		
391 		return new Source(cast(GSource*) p);
392 	}
393 
394 	/**
395 	 * Creates a new timeout source.
396 	 *
397 	 * The source will not initially be associated with any #GMainContext
398 	 * and must be added to one with g_source_attach() before it will be
399 	 * executed.
400 	 *
401 	 * The scheduling granularity/accuracy of this timeout source will be
402 	 * in seconds.
403 	 *
404 	 * The interval given in terms of monotonic time, not wall clock time.
405 	 * See g_get_monotonic_time().
406 	 *
407 	 * Params:
408 	 *     interval = the timeout interval in seconds
409 	 *
410 	 * Return: the newly-created timeout source
411 	 *
412 	 * Since: 2.14
413 	 */
414 	public static Source sourceNewSeconds(uint interval)
415 	{
416 		auto p = g_timeout_source_new_seconds(interval);
417 		
418 		if(p is null)
419 		{
420 			return null;
421 		}
422 		
423 		return new Source(cast(GSource*) p);
424 	}
425 }