source: appstream-generator/build/girepo/glib/Timeout.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

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