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  = glib-The-Main-Event-Loop.html
27  * outPack = glib
28  * outFile = MainLoop
29  * strct   = GMainLoop
30  * realStrct=
31  * ctorStrct=
32  * clss    = MainLoop
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_main_loop_
41  * 	- g_
42  * omit structs:
43  * omit prefixes:
44  * 	- g_main_context_
45  * 	- g_timeout_
46  * 	- g_idle_
47  * 	- g_child_
48  * 	- g_source_
49  * omit code:
50  * omit signals:
51  * imports:
52  * 	- gtkc.paths
53  * 	- gtkc.Loader
54  * 	- glib.MainContext
55  * 	- glib.Source
56  * structWrap:
57  * 	- GMainContext* -> MainContext
58  * 	- GMainLoop* -> MainLoop
59  * 	- GSource* -> Source
60  * module aliases:
61  * local aliases:
62  * overrides:
63  */
64 
65 module glib.MainLoop;
66 
67 public  import gtkc.glibtypes;
68 
69 private import gtkc.glib;
70 private import glib.ConstructionException;
71 
72 private import gtkc.paths;
73 private import gtkc.Loader;
74 private import glib.MainContext;
75 private import glib.Source;
76 
77 
78 
79 /**
80  * The main event loop manages all the available sources of events for
81  * GLib and GTK+ applications. These events can come from any number of
82  * different types of sources such as file descriptors (plain files,
83  * pipes or sockets) and timeouts. New types of event sources can also
84  * be added using g_source_attach().
85  *
86  * To allow multiple independent sets of sources to be handled in
87  * different threads, each source is associated with a GMainContext.
88  * A GMainContext can only be running in a single thread, but
89  * sources can be added to it and removed from it from other threads.
90  *
91  * Each event source is assigned a priority. The default priority,
92  * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
93  * Values greater than 0 denote lower priorities. Events from high priority
94  * sources are always processed before events from lower priority sources.
95  *
96  * Idle functions can also be added, and assigned a priority. These will
97  * be run whenever no events with a higher priority are ready to be processed.
98  *
99  * The GMainLoop data type represents a main event loop. A GMainLoop is
100  * created with g_main_loop_new(). After adding the initial event sources,
101  * g_main_loop_run() is called. This continuously checks for new events from
102  * each of the event sources and dispatches them. Finally, the processing of
103  * an event from one of the sources leads to a call to g_main_loop_quit() to
104  * exit the main loop, and g_main_loop_run() returns.
105  *
106  * It is possible to create new instances of GMainLoop recursively.
107  * This is often used in GTK+ applications when showing modal dialog
108  * boxes. Note that event sources are associated with a particular
109  * GMainContext, and will be checked and dispatched for all main
110  * loops associated with that GMainContext.
111  *
112  * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
113  * gtk_main_quit() and gtk_events_pending().
114  *
115  * Creating new source types
116  *
117  * One of the unusual features of the GMainLoop functionality
118  * is that new types of event source can be created and used in
119  * addition to the builtin type of event source. A new event source
120  * type is used for handling GDK events. A new source type is created
121  * by deriving from the GSource structure.
122  * The derived type of source is represented by a structure that has
123  * the GSource structure as a first element, and other elements specific
124  * to the new source type. To create an instance of the new source type,
125  * call g_source_new() passing in the size of the derived structure and
126  * a table of functions. These GSourceFuncs determine the behavior of
127  * the new source type.
128  *
129  * New source types basically interact with the main context
130  * in two ways. Their prepare function in GSourceFuncs can set a timeout
131  * to determine the maximum amount of time that the main loop will sleep
132  * before checking the source again. In addition, or as well, the source
133  * can add file descriptors to the set that the main context checks using
134  * g_source_add_poll().
135  *
136  * <hr>
137  *
138  * Customizing the main loop iteration
139  *
140  * Single iterations of a GMainContext can be run with
141  * g_main_context_iteration(). In some cases, more detailed control
142  * of exactly how the details of the main loop work is desired, for
143  * instance, when integrating the GMainLoop with an external main loop.
144  * In such cases, you can call the component functions of
145  * g_main_context_iteration() directly. These functions are
146  * g_main_context_prepare(), g_main_context_query(),
147  * g_main_context_check() and g_main_context_dispatch().
148  *
149  * The operation of these functions can best be seen in terms
150  * of a state diagram, as shown in Figure 1, “States of a Main Context”.
151  *
152  * Figure 1. States of a Main Context
153  *
154  * On Unix, the GLib mainloop is incompatible with fork(). Any program
155  * using the mainloop must either exec() or exit() from the child
156  * without returning to the mainloop.
157  */
158 public class MainLoop
159 {
160 	
161 	/** the main Gtk struct */
162 	protected GMainLoop* gMainLoop;
163 	
164 	
165 	/** Get the main Gtk struct */
166 	public GMainLoop* getMainLoopStruct()
167 	{
168 		return gMainLoop;
169 	}
170 	
171 	
172 	/** the main Gtk struct as a void* */
173 	protected void* getStruct()
174 	{
175 		return cast(void*)gMainLoop;
176 	}
177 	
178 	/**
179 	 * Sets our main struct and passes it to the parent class
180 	 */
181 	public this (GMainLoop* gMainLoop)
182 	{
183 		this.gMainLoop = gMainLoop;
184 	}
185 	
186 	~this()
187 	{
188 		if ( Linker.isLoaded(LIBRARY.GLIB) && gMainLoop != null)
189 		{
190 			g_main_loop_unref(gMainLoop);
191 		}
192 	}
193 	
194 	/**
195 	 */
196 	
197 	/**
198 	 * Creates a new GMainLoop structure.
199 	 * Params:
200 	 * context = a GMainContext (if NULL, the default context will be used). [allow-none]
201 	 * isRunning = set to TRUE to indicate that the loop is running. This
202 	 * is not very important since calling g_main_loop_run() will set this to
203 	 * TRUE anyway.
204 	 * Throws: ConstructionException GTK+ fails to create the object.
205 	 */
206 	public this (MainContext context, int isRunning)
207 	{
208 		// GMainLoop * g_main_loop_new (GMainContext *context,  gboolean is_running);
209 		auto p = g_main_loop_new((context is null) ? null : context.getMainContextStruct(), isRunning);
210 		if(p is null)
211 		{
212 			throw new ConstructionException("null returned by g_main_loop_new((context is null) ? null : context.getMainContextStruct(), isRunning)");
213 		}
214 		this(cast(GMainLoop*) p);
215 	}
216 	
217 	/**
218 	 * Increases the reference count on a GMainLoop object by one.
219 	 * Returns: loop
220 	 */
221 	public MainLoop doref()
222 	{
223 		// GMainLoop * g_main_loop_ref (GMainLoop *loop);
224 		auto p = g_main_loop_ref(gMainLoop);
225 		
226 		if(p is null)
227 		{
228 			return null;
229 		}
230 		
231 		return new MainLoop(cast(GMainLoop*) p);
232 	}
233 	
234 	/**
235 	 * Decreases the reference count on a GMainLoop object by one. If
236 	 * the result is zero, free the loop and free all associated memory.
237 	 */
238 	public void unref()
239 	{
240 		// void g_main_loop_unref (GMainLoop *loop);
241 		g_main_loop_unref(gMainLoop);
242 	}
243 	
244 	/**
245 	 * Runs a main loop until g_main_loop_quit() is called on the loop.
246 	 * If this is called for the thread of the loop's GMainContext,
247 	 * it will process events from the loop, otherwise it will
248 	 * simply wait.
249 	 */
250 	public void run()
251 	{
252 		// void g_main_loop_run (GMainLoop *loop);
253 		g_main_loop_run(gMainLoop);
254 	}
255 	
256 	/**
257 	 * Stops a GMainLoop from running. Any calls to g_main_loop_run()
258 	 * for the loop will return.
259 	 * Note that sources that have already been dispatched when
260 	 * g_main_loop_quit() is called will still be executed.
261 	 */
262 	public void quit()
263 	{
264 		// void g_main_loop_quit (GMainLoop *loop);
265 		g_main_loop_quit(gMainLoop);
266 	}
267 	
268 	/**
269 	 * Checks to see if the main loop is currently being run via g_main_loop_run().
270 	 * Returns: TRUE if the mainloop is currently being run.
271 	 */
272 	public int isRunning()
273 	{
274 		// gboolean g_main_loop_is_running (GMainLoop *loop);
275 		return g_main_loop_is_running(gMainLoop);
276 	}
277 	
278 	/**
279 	 * Returns the GMainContext of loop.
280 	 * Returns: the GMainContext of loop. [transfer none]
281 	 */
282 	public MainContext getContext()
283 	{
284 		// GMainContext * g_main_loop_get_context (GMainLoop *loop);
285 		auto p = g_main_loop_get_context(gMainLoop);
286 		
287 		if(p is null)
288 		{
289 			return null;
290 		}
291 		
292 		return new MainContext(cast(GMainContext*) p);
293 	}
294 	
295 	/**
296 	 * Returns the depth of the stack of calls to
297 	 * g_main_context_dispatch() on any GMainContext in the current thread.
298 	 *  That is, when called from the toplevel, it gives 0. When
299 	 * called from within a callback from g_main_context_iteration()
300 	 * (or g_main_loop_run(), etc.) it returns 1. When called from within
301 	 * a callback to a recursive call to g_main_context_iteration(),
302 	 * it returns 2. And so forth.
303 	 * Returns: The main loop recursion level in the current thread
304 	 */
305 	public static int mainDepth()
306 	{
307 		// gint g_main_depth (void);
308 		return g_main_depth();
309 	}
310 	
311 	/**
312 	 * Returns the currently firing source for this thread.
313 	 * Since 2.12
314 	 * Returns: The currently firing source or NULL. [transfer none]
315 	 */
316 	public static Source mainCurrentSource()
317 	{
318 		// GSource * g_main_current_source (void);
319 		auto p = g_main_current_source();
320 		
321 		if(p is null)
322 		{
323 			return null;
324 		}
325 		
326 		return new Source(cast(GSource*) p);
327 	}
328 	
329 	/**
330 	 * Polls fds, as with the poll() system call, but portably. (On
331 	 * systems that don't have poll(), it is emulated using select().)
332 	 * This is used internally by GMainContext, but it can be called
333 	 * directly if you need to block until a file descriptor is ready, but
334 	 * don't want to run the full main loop.
335 	 * Each element of fds is a GPollFD describing a single file
336 	 * descriptor to poll. The fd field indicates the file descriptor,
337 	 * and the events field indicates the events to poll for. On return,
338 	 * the revents fields will be filled with the events that actually
339 	 * occurred.
340 	 * On POSIX systems, the file descriptors in fds can be any sort of
341 	 * file descriptor, but the situation is much more complicated on
342 	 * Windows. If you need to use g_poll() in code that has to run on
343 	 * Windows, the easiest solution is to construct all of your
344 	 * GPollFDs with g_io_channel_win32_make_pollfd().
345 	 * Since 2.20
346 	 * Params:
347 	 * fds = file descriptors to poll
348 	 * timeout = amount of time to wait, in milliseconds, or -1 to wait forever
349 	 * Returns: the number of entries in fds whose revents fields were filled in, or 0 if the operation timed out, or -1 on error or if the call was interrupted.
350 	 */
351 	public static int poll(GPollFD[] fds, int timeout)
352 	{
353 		// gint g_poll (GPollFD *fds,  guint nfds,  gint timeout);
354 		return g_poll(fds.ptr, cast(int) fds.length, timeout);
355 	}
356 }