I've no idea here...
[gtkD.git] / src / glib / MainLoop.d
blob0fedffac2d4583d137fdeabd0e6308f387343c0e
1 /*
2 * This file is part of duit.
4 * duit 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 2.1 of the License, or
7 * (at your option) any later version.
9 * duit 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.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with duit; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile = glib-The-Main-Event-Loop.html
26 * outPack = glib
27 * outFile = MainLoop
28 * strct = GMainLoop
29 * realStrct=
30 * ctorStrct=
31 * clss = MainLoop
32 * interf =
33 * class Code: Yes
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_main_loop_
40 * omit structs:
41 * omit prefixes:
42 * - g_main_context_
43 * - g_timeout_
44 * - g_child_
45 * - g_source_
46 * omit code:
47 * - g_main_loop_ref
48 * imports:
49 * - glib.Dataset
50 * - glib.MainContext
51 * - glib.Source
52 * structWrap:
53 * - GDataset* -> Dataset
54 * - GMainContext* -> MainContext
55 * - GSource* -> Source
56 * local aliases:
59 module glib.MainLoop;
61 private import glib.glibtypes;
63 private import lib.glib;
65 private import glib.Dataset;
66 private import glib.MainContext;
67 private import glib.Source;
69 /**
70 * Description
71 * The main event loop manages all the available sources of events for
72 * GLib and GTK+ applications. These events can come from any number of
73 * different types of sources such as file descriptors (plain files,
74 * pipes or sockets) and timeouts. New types of event sources can also
75 * be added using g_source_attach().
76 * To allow multiple independent sets of sources to be handled in
77 * different threads, each source is associated with a GMainContext.
78 * A GMainContext can only be running in a single thread, but
79 * sources can be added to it and removed from it from other threads.
80 * Each event source is assigned a priority. The default priority,
81 * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher
82 * priorities. Values greater than 0 denote lower priorities. Events
83 * from high priority sources are always processed before events from
84 * lower priority sources.
85 * Idle functions can also be added, and assigned a priority. These will
86 * be run whenever no events with a higher priority are ready to be
87 * processed.
88 * The GMainLoop data type represents a main event loop. A GMainLoop
89 * is created with g_main_loop_new(). After adding the initial event sources,
90 * g_main_loop_run() is called. This continuously checks for new events from
91 * each of the event sources and dispatches them. Finally, the
92 * processing of an event from one of the sources leads to a call to
93 * g_main_loop_quit() to exit the main loop, and g_main_loop_run() returns.
94 * It is possible to create new instances of GMainLoop recursively.
95 * This is often used in GTK+ applications when showing modal dialog
96 * boxes. Note that event sources are associated with a particular
97 * GMainContext, and will be checked and dispatched for all main
98 * loops associated with that GMainContext.
99 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
100 * gtk_main_quit() and gtk_events_pending().
101 * Creating new sources types
102 * One of the unusual features of the GTK+ main loop functionality
103 * is that new types of event source can be created and used in
104 * addition to the builtin type of event source. A new event source
105 * type is used for handling GDK events. A new source type is
106 * created by deriving from the GSource
107 * structure. The derived type of source is represented by a
108 * structure that has the GSource structure as a first element,
109 * and other elements specific to the new source type. To create
110 * an instance of the new source type, call g_source_new() passing
111 * in the size of the derived structure and a table of functions.
112 * These GSourceFuncs determine the behavior of the new source
113 * types.
114 * New source types basically interact with with the main context
115 * in two ways. Their prepare function in GSourceFuncs can set
116 * a timeout to determine the maximum amount of time that the
117 * main loop will sleep before checking the source again. In
118 * addition, or as well, the source can add file descriptors to
119 * the set that the main context checks using g_source_add_poll().
120 * <hr>
121 * Customizing the main loop iteration
122 * Single iterations of a GMainContext can be run with
123 * g_main_context_iteration(). In some cases, more detailed control
124 * of exactly how the details of the main loop work is desired,
125 * for instance, when integrating the GMainLoop with an external
126 * main loop. In such cases, you can call the component functions
127 * of g_main_context_iteration() directly. These functions
128 * are g_main_context_prepare(), g_main_context_query(),
129 * g_main_context_check() and g_main_context_dispatch().
130 * The operation of these functions can best be seen in terms
131 * of a state diagram, as shown in Figure1, States of a Main Context.
132 * Figure1.States of a Main Context
134 public class MainLoop
137 /** the main Gtk struct */
138 protected GMainLoop* gMainLoop;
141 public GMainLoop* getMainLoopStruct()
143 return gMainLoop;
147 /** the main Gtk struct as a void* */
148 protected void* getStruct()
150 return cast(void*)gMainLoop;
154 * Sets our main struct and passes it to the parent class
156 public this (GMainLoop* gMainLoop)
158 this.gMainLoop = gMainLoop;
162 * Increases the reference count on a GMainLoop object by one.
163 * loop:
164 * a GMainLoop
165 * Returns:
166 * loop
168 public MainLoop ref()
170 // GMainLoop* g_main_loop_ref (GMainLoop *loop);
171 return new MainLoop( g_main_loop_ref(gMainLoop) );
179 * Creates a new GMainLoop structure.
180 * context:
181 * a GMainContext (if NULL, the default context will be used).
182 * is_running:
183 * set to TRUE to indicate that the loop is running. This
184 * is not very important since calling g_main_loop_run() will set this to
185 * TRUE anyway.
186 * Returns:
187 * a new GMainLoop.
189 public this (MainContext context, int isRunning)
191 // GMainLoop* g_main_loop_new (GMainContext *context, gboolean is_running);
192 this(cast(GMainLoop*)g_main_loop_new((context is null) ? null : context.getMainContextStruct(), isRunning) );
197 * Decreases the reference count on a GMainLoop object by one. If
198 * the result is zero, free the loop and free all associated memory.
199 * loop:
200 * a GMainLoop
202 public void unref()
204 // void g_main_loop_unref (GMainLoop *loop);
205 g_main_loop_unref(gMainLoop);
209 * Runs a main loop until g_main_loop_quit() is called on the loop.
210 * If this is called for the thread of the loop's GMainContext,
211 * it will process events from the loop, otherwise it will
212 * simply wait.
213 * loop:
214 * a GMainLoop
216 public void run()
218 // void g_main_loop_run (GMainLoop *loop);
219 g_main_loop_run(gMainLoop);
223 * Stops a GMainLoop from running. Any calls to g_main_loop_run()
224 * for the loop will return.
225 * loop:
226 * a GMainLoop
228 public void quit()
230 // void g_main_loop_quit (GMainLoop *loop);
231 g_main_loop_quit(gMainLoop);
235 * Checks to see if the main loop is currently being run via g_main_loop_run().
236 * loop:
237 * a GMainLoop.
238 * Returns:
239 * TRUE if the mainloop is currently being run.
241 public int isRunning()
243 // gboolean g_main_loop_is_running (GMainLoop *loop);
244 return g_main_loop_is_running(gMainLoop);
248 * Returns the GMainContext of loop.
249 * loop:
250 * a GMainLoop.
251 * Returns:
252 * the GMainContext of loop
254 public MainContext getContext()
256 // GMainContext* g_main_loop_get_context (GMainLoop *loop);
257 return new MainContext( g_main_loop_get_context(gMainLoop) );
297 * Return value: The main loop recursion level in the current thread
298 * Returns:
299 * the depth of the stack of calls to
300 * g_main_context_dispatch() on any GMainContext in the current thread.
301 * That is, when called from the toplevel, it gives 0. When
302 * called from within a callback from g_main_context_iteration()
303 * (or g_main_loop_run(), etc.) it returns 1. When called from within
304 * a callback to a recursive call to g_main_context_iterate(),
305 * it returns 2. And so forth.
306 * This function is useful in a situation like the following:
307 * Imagine an extremely simple "garbage collected" system.
308 * Example1.
309 * static GList *free_list;
310 * gpointer
311 * allocate_memory (gsize size)
313 * gpointer result = g_malloc (size);
314 * free_list = g_list_prepend (free_list, result);
315 * return result;
317 * void
318 * free_allocated_memory (void)
320 * GList *l;
321 * for (l = free_list; l; l = l->next);
322 * g_free (l->data);
323 * g_list_free (free_list);
324 * free_list = NULL;
326 * [...]
327 * while (TRUE);
329 * g_main_context_iteration (NULL, TRUE);
330 * free_allocated_memory();
332 * This works from an application, however, if you want to do the same
333 * thing from a library, it gets more difficult, since you no longer
334 * control the main loop. You might think you can simply use an idle
335 * function to make the call to free_allocated_memory(), but that
336 * doesn't work, since the idle function could be called from a
337 * recursive callback. This can be fixed by using g_main_depth()
338 * Example2.
339 * gpointer
340 * allocate_memory (gsize size)
342 * FreeListBlock *block = g_new (FreeListBlock, 1);\
343 * block->mem = g_malloc (size);
344 * block->depth = g_main_depth();
345 * free_list = g_list_prepend (free_list, block);
346 * return block->mem;
348 * void
349 * free_allocated_memory (void)
351 * GList *l;
352 * int depth = g_main_depth();
353 * for (l = free_list; l; );
355 * GList *next = l->next;
356 * FreeListBlock *block = l->data;
357 * if (block->depth > depth)
359 * g_free (block->mem);
360 * g_free (block);
361 * free_list = g_list_delete_link (free_list, l);
363 * l = next;
366 * There is a temptation to use g_main_depth() to solve
367 * problems with reentrancy. For instance, while waiting for data
368 * to be received from the network in response to a menu item,
369 * the menu item might be selected again. It might seem that
370 * one could make the menu item's callback return immediately
371 * and do nothing if g_main_depth() returns a value greater than 1.
372 * However, this should be avoided since the user then sees selecting
373 * the menu item do nothing. Furthermore, you'll find yourself adding
374 * these checks all over your code, since there are doubtless many,
375 * many things that the user could do. Instead, you can use the
376 * following techniques:
377 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
378 * the user from interacting with elements while the main
379 * loop is recursing.
380 * Avoid main loop recursion in situations where you can't handle
381 * arbitrary callbacks. Instead, structure your code so that you
382 * simply return to the main loop and then get called again when
383 * there is more work to do.
385 public static int gMainDepth()
387 // gint g_main_depth (void);
388 return g_main_depth();
392 * Returns the currently firing source for this thread.
393 * Returns:
394 * The currently firing source or NULL.
395 * Since 2.12
397 public static Source gMainCurrentSource()
399 // GSource* g_main_current_source (void);
400 return new Source( g_main_current_source() );
410 * Creates a new idle source.
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. Note that the default priority for idle sources is
414 * G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
415 * have a default priority of G_PRIORITY_DEFAULT.
416 * Returns:
417 * the newly-created idle source
419 public static Source gIdleSourceNew()
421 // GSource* g_idle_source_new (void);
422 return new Source( g_idle_source_new() );
426 * Adds a function to be called whenever there are no higher priority
427 * events pending to the default main loop. The function is given the
428 * default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function
429 * returns FALSE it is automatically removed from the list of event
430 * sources and will not be called again.
431 * function:
432 * function to call
433 * data:
434 * data to pass to function.
435 * Returns:
436 * the ID (greater than 0) of the event source.
438 public static uint gIdleAdd(GSourceFunc funct, void* data)
440 // guint g_idle_add (GSourceFunc function, gpointer data);
441 return g_idle_add(funct, data);
445 * Adds a function to be called whenever there are no higher priority
446 * events pending. If the function returns FALSE it is automatically
447 * removed from the list of event sources and will not be called again.
448 * priority:
449 * the priority of the idle source. Typically this will be in the
450 * range btweeen G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
451 * function:
452 * function to call
453 * data:
454 * data to pass to function
455 * notify:
456 * function to call when the idle is removed, or NULL
457 * Returns:
458 * the ID (greater than 0) of the event source.
460 public static uint gIdleAddFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
462 // guint g_idle_add_full (gint priority, GSourceFunc function, gpointer data, GDestroyNotify notify);
463 return g_idle_add_full(priority, funct, data, notify);
467 * Removes the idle function with the given data.
468 * data:
469 * the data for the idle source's callback.
470 * Returns:
471 * TRUE if an idle source was found and removed.
473 public static int gIdleRemoveByData(void* data)
475 // gboolean g_idle_remove_by_data (gpointer data);
476 return g_idle_remove_by_data(data);