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