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 
62 private import glib.Source;
63 
64 
65 
66 
67 /**
68  * Description
69  * The main event loop manages all the available sources of events for
70  * GLib and GTK+ applications. These events can come from any number of
71  * different types of sources such as file descriptors (plain files,
72  * pipes or sockets) and timeouts. New types of event sources can also
73  * be added using g_source_attach().
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  * Each event source is assigned a priority. The default priority,
79  * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
80  * Values greater than 0 denote lower priorities. Events from high priority
81  * sources are always processed before events from lower priority sources.
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  * The GMainLoop data type represents a main event loop. A GMainLoop is
85  * created with g_main_loop_new(). After adding the initial event sources,
86  * g_main_loop_run() is called. This continuously checks for new events from
87  * each of the event sources and dispatches them. Finally, the processing of
88  * an event from one of the sources leads to a call to g_main_loop_quit() to
89  * exit the main loop, and g_main_loop_run() returns.
90  * It is possible to create new instances of GMainLoop recursively.
91  * This is often used in GTK+ applications when showing modal dialog
92  * boxes. Note that event sources are associated with a particular
93  * GMainContext, and will be checked and dispatched for all main
94  * loops associated with that GMainContext.
95  * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
96  * gtk_main_quit() and gtk_events_pending().
97  * Creating new source types
98  * One of the unusual features of the GMainLoop functionality
99  * is that new types of event source can be created and used in
100  * addition to the builtin type of event source. A new event source
101  * type is used for handling GDK events. A new source type is created
102  * by deriving from the GSource structure.
103  * The derived type of source is represented by a structure that has
104  * the GSource structure as a first element, and other elements specific
105  * to the new source type. To create an instance of the new source type,
106  * call g_source_new() passing in the size of the derived structure and
107  * a table of functions. These GSourceFuncs determine the behavior of
108  * the new source type.
109  * New source types basically interact with the main context
110  * in two ways. Their prepare function in GSourceFuncs can set a timeout
111  * to determine the maximum amount of time that the main loop will sleep
112  * before checking the source again. In addition, or as well, the source
113  * can add file descriptors to the set that the main context checks using
114  * g_source_add_poll().
115  * <hr>
116  * Customizing the main loop iteration
117  * Single iterations of a GMainContext can be run with
118  * g_main_context_iteration(). In some cases, more detailed control
119  * of exactly how the details of the main loop work is desired, for
120  * instance, when integrating the GMainLoop with an external main loop.
121  * In such cases, you can call the component functions of
122  * g_main_context_iteration() directly. These functions are
123  * g_main_context_prepare(), g_main_context_query(),
124  * g_main_context_check() and g_main_context_dispatch().
125  * The operation of these functions can best be seen in terms
126  * of a state diagram, as shown in Figure 1, “States of a Main Context”.
127  * Figure 1. States of a Main Context
128  */
129 public class Idle
130 {
131 	
132 	/** Holds all idle delegates */
133 	bool delegate()[] idleListeners;
134 	/** our idle ID */
135 	uint idleID;
136 	
137 	/**
138 	 * Creates a new idle cycle.
139 	 * Params:
140 	 *    	interval = the idle in milieconds
141 	 *    	dlg = the delegate to be executed
142 	 *    	fireNow = When true the delegate will be executed emmidiatly
143 	 */
144 	this(bool delegate() dlg, bool fireNow=false)
145 	{
146 		idleListeners ~= dlg;
147 		idleID = g_idle_add(cast(GSourceFunc)&idleCallback, cast(void*)this);
148 		if ( fireNow )
149 		{
150 			if ( !dlg() )
151 			{
152 				idleListeners.length = 0;
153 			}
154 		}
155 	}
156 	
157 	/**
158 	 * Creates a new idle cycle.
159 	 * Params:
160 	 *    	dlg = the delegate to be executed
161 	 *      priority = Priority for the idle function
162 	 *    	fireNow = When true the delegate will be executed emmidiatly
163 	 */
164 	this(bool delegate() dlg, GPriority priority, bool fireNow=false)
165 	{
166 		idleListeners ~= dlg;
167 		idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, null);
168 		if ( fireNow )
169 		{
170 			if ( !dlg() )
171 			{
172 				idleListeners.length = 0;
173 			}
174 		}
175 	}
176 	
177 	/** */
178 	public void stop()
179 	{
180 		if ( idleID > 0 )
181 		{
182 			g_source_remove(idleID);
183 		}
184 		idleListeners.length = 0;
185 	}
186 	
187 	/**
188 	 * Removes the idle from gtk
189 	 */
190 	~this()
191 	{
192 		stop();
193 	}
194 	
195 	/**
196 	 * Adds a new delegate to this idle cycle
197 	 * Params:
198 	 *    	dlg =
199 	 *    	fireNow =
200 	 */
201 	public void addListener(bool delegate() dlg, bool fireNow=false)
202 	{
203 		idleListeners ~= dlg;
204 		if ( fireNow )
205 		{
206 			if ( !dlg() )
207 			{
208 				idleListeners.length = idleListeners.length - 1;
209 			}
210 		}
211 	}
212 	
213 	/**
214 	 * The callback execution from glib
215 	 * Params:
216 	 *    	idle =
217 	 * Returns:
218 	 */
219 	extern(C) static bool idleCallback(Idle idle)
220 	{
221 		return idle.callAllListeners();
222 	}
223 	
224 	/**
225 	 * Executes all delegates on the execution list
226 	 * Returns:
227 	 */
228 	private bool callAllListeners()
229 	{
230 		bool runAgain = false;
231 		
232 		int i = 0;
233 		
234 		while ( i<idleListeners.length )
235 		{
236 			if ( !idleListeners[i]() )
237 			{
238 				idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length];
239 			}
240 			else
241 			{
242 				runAgain = true;
243 				++i;
244 			}
245 		}
246 		return runAgain;
247 	}
248 	
249 	/**
250 	 */
251 	
252 	/**
253 	 * Creates a new idle source.
254 	 * The source will not initially be associated with any GMainContext
255 	 * and must be added to one with g_source_attach() before it will be
256 	 * executed. Note that the default priority for idle sources is
257 	 * G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
258 	 * have a default priority of G_PRIORITY_DEFAULT.
259 	 * Returns: the newly-created idle source
260 	 */
261 	public static Source sourceNew()
262 	{
263 		// GSource * g_idle_source_new (void);
264 		auto p = g_idle_source_new();
265 		
266 		if(p is null)
267 		{
268 			return null;
269 		}
270 		
271 		return new Source(cast(GSource*) p);
272 	}
273 	
274 	/**
275 	 * Adds a function to be called whenever there are no higher priority
276 	 * events pending to the default main loop. The function is given the
277 	 * default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function
278 	 * returns FALSE it is automatically removed from the list of event
279 	 * sources and will not be called again.
280 	 * This internally creates a main loop source using g_idle_source_new()
281 	 * and attaches it to the main loop context using g_source_attach().
282 	 * You can do these steps manually if you need greater control.
283 	 * Params:
284 	 * data = data to pass to function.
285 	 * Returns: the ID (greater than 0) of the event source.
286 	 */
287 	public static uint add(GSourceFunc funct, void* data)
288 	{
289 		// guint g_idle_add (GSourceFunc function,  gpointer data);
290 		return g_idle_add(funct, data);
291 	}
292 	
293 	/**
294 	 * Adds a function to be called whenever there are no higher priority
295 	 * events pending. If the function returns FALSE it is automatically
296 	 * removed from the list of event sources and will not be called again.
297 	 * This internally creates a main loop source using g_idle_source_new()
298 	 * and attaches it to the main loop context using g_source_attach().
299 	 * You can do these steps manually if you need greater control.
300 	 * Params:
301 	 * priority = the priority of the idle source. Typically this will be in the
302 	 * range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
303 	 * data = data to pass to function
304 	 * notify = function to call when the idle is removed, or NULL
305 	 * Returns: the ID (greater than 0) of the event source.
306 	 */
307 	public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
308 	{
309 		// guint g_idle_add_full (gint priority,  GSourceFunc function,  gpointer data,  GDestroyNotify notify);
310 		return g_idle_add_full(priority, funct, data, notify);
311 	}
312 	
313 	/**
314 	 * Removes the idle function with the given data.
315 	 * Params:
316 	 * data = the data for the idle source's callback.
317 	 * Returns: TRUE if an idle source was found and removed.
318 	 */
319 	public static int removeByData(void* data)
320 	{
321 		// gboolean g_idle_remove_by_data (gpointer data);
322 		return g_idle_remove_by_data(data);
323 	}
324 }