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