Wrote the README_GIT file to be a proper tutorial on git, repo.or.cz and gtkD.
[gtkD.git] / gtkD / src / glib / MainContext.d
blob9485e2c55587b4e9cffb30008a7d4127fa2d22ca
1 /*
2 * This file is part of gtkD.
4 * gtkD 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 * gtkD 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 gtkD; 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 =
26 * outPack = glib
27 * outFile = MainContext
28 * strct = GMainContext
29 * realStrct=
30 * ctorStrct=
31 * clss = MainContext
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_main_context_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * - glib.MainLoop
45 * - glib.Source
46 * - gthread.Cond
47 * - gthread.Mutex
48 * - glib.ThreadPool
49 * - glib.MainContext
50 * structWrap:
51 * - GCond* -> Cond
52 * - GMainContext* -> MainContext
53 * - GMainLLoop* -> MainLoop
54 * - GMutex* -> Mutex
55 * - GSource* -> Source
56 * - GThreadPoll* -> ThreadPoll
57 * module aliases:
58 * local aliases:
61 module glib.MainContext;
63 version(noAssert)
65 version(Tango)
67 import tango.io.Stdout; // use the tango loging?
71 private import gtkc.glibtypes;
73 private import gtkc.glib;
76 private import glib.MainLoop;
77 private import glib.Source;
78 private import gthread.Cond;
79 private import gthread.Mutex;
80 private import glib.ThreadPool;
81 private import glib.MainContext;
86 /**
87 * Description
88 * The main event loop manages all the available sources of events for
89 * GLib and GTK+ applications. These events can come from any number of
90 * different types of sources such as file descriptors (plain files,
91 * pipes or sockets) and timeouts. New types of event sources can also
92 * be added using g_source_attach().
93 * To allow multiple independent sets of sources to be handled in
94 * different threads, each source is associated with a GMainContext.
95 * A GMainContext can only be running in a single thread, but
96 * sources can be added to it and removed from it from other threads.
97 * Each event source is assigned a priority. The default priority,
98 * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher
99 * priorities. Values greater than 0 denote lower priorities. Events
100 * from high priority sources are always processed before events from
101 * lower priority sources.
102 * Idle functions can also be added, and assigned a priority. These will
103 * be run whenever no events with a higher priority are ready to be
104 * processed.
105 * The GMainLoop data type represents a main event loop. A GMainLoop
106 * is created with g_main_loop_new(). After adding the initial event sources,
107 * g_main_loop_run() is called. This continuously checks for new events from
108 * each of the event sources and dispatches them. Finally, the
109 * processing of an event from one of the sources leads to a call to
110 * g_main_loop_quit() to exit the main loop, and g_main_loop_run() returns.
111 * It is possible to create new instances of GMainLoop recursively.
112 * This is often used in GTK+ applications when showing modal dialog
113 * boxes. Note that event sources are associated with a particular
114 * GMainContext, and will be checked and dispatched for all main
115 * loops associated with that GMainContext.
116 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
117 * gtk_main_quit() and gtk_events_pending().
118 * Creating new sources types
119 * One of the unusual features of the GTK+ main loop 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
123 * created by deriving from the GSource
124 * structure. The derived type of source is represented by a
125 * structure that has the GSource structure as a first element,
126 * and other elements specific to the new source type. To create
127 * an instance of the new source type, call g_source_new() passing
128 * in the size of the derived structure and a table of functions.
129 * These GSourceFuncs determine the behavior of the new source
130 * types.
131 * New source types basically interact with with the main context
132 * in two ways. Their prepare function in GSourceFuncs can set
133 * a timeout to determine the maximum amount of time that the
134 * main loop will sleep before checking the source again. In
135 * addition, or as well, the source can add file descriptors to
136 * the set that the main context checks using g_source_add_poll().
137 * <hr>
138 * Customizing the main loop iteration
139 * Single iterations of a GMainContext can be run with
140 * g_main_context_iteration(). In some cases, more detailed control
141 * of exactly how the details of the main loop work is desired,
142 * for instance, when integrating the GMainLoop with an external
143 * main loop. In such cases, you can call the component functions
144 * of g_main_context_iteration() directly. These functions
145 * are g_main_context_prepare(), g_main_context_query(),
146 * g_main_context_check() and g_main_context_dispatch().
147 * The operation of these functions can best be seen in terms
148 * of a state diagram, as shown in Figure1, States of a Main Context.
149 * Figure1.States of a Main Context
151 public class MainContext
154 /** the main Gtk struct */
155 protected GMainContext* gMainContext;
158 public GMainContext* getMainContextStruct()
160 return gMainContext;
164 /** the main Gtk struct as a void* */
165 protected void* getStruct()
167 return cast(void*)gMainContext;
171 * Sets our main struct and passes it to the parent class
173 public this (GMainContext* gMainContext)
175 version(noAssert)
177 if ( gMainContext is null )
179 int zero = 0;
180 version(Tango)
182 Stdout("struct gMainContext is null on constructor").newline;
184 else
186 printf("struct gMainContext is null on constructor");
188 zero = zero / zero;
191 else
193 assert(gMainContext !is null, "struct gMainContext is null on constructor");
195 this.gMainContext = gMainContext;
221 * Creates a new GMainContext strcuture
222 * Returns:
223 * the new GMainContext
225 public this ()
227 // GMainContext* g_main_context_new (void);
228 this(cast(GMainContext*)g_main_context_new() );
232 * Increases the reference count on a GMainContext object by one.
233 * context:
234 * a GMainContext
235 * Returns:
236 * the context that was passed in (since 2.6)
238 public MainContext doref()
240 // GMainContext* g_main_context_ref (GMainContext *context);
241 return new MainContext( g_main_context_ref(gMainContext) );
245 * Decreases the reference count on a GMainContext object by one. If
246 * the result is zero, free the context and free all associated memory.
247 * context:
248 * a GMainContext
250 public void unref()
252 // void g_main_context_unref (GMainContext *context);
253 g_main_context_unref(gMainContext);
257 * Returns the default main context. This is the main context used
258 * for main loop functions when a main loop is not explicitly
259 * specified.
260 * Returns:
261 * the default main context.
263 public static MainContext defaulx()
265 // GMainContext* g_main_context_default (void);
266 return new MainContext( g_main_context_default() );
270 * Runs a single iteration for the given main loop. This involves
271 * checking to see if any event sources are ready to be processed,
272 * then if no events sources are ready and may_block is TRUE, waiting
273 * for a source to become ready, then dispatching the highest priority
274 * events sources that are ready. Note that even when may_block is TRUE,
275 * it is still possible for g_main_context_iteration() to return
276 * FALSE, since the the wait may be interrupted for other
277 * reasons than an event source becoming ready.
278 * context:
279 * a GMainContext (if NULL, the default context will be used)
280 * may_block:
281 * whether the call may block.
282 * Returns:
283 * TRUE if events were dispatched.
285 public int iteration(int mayBlock)
287 // gboolean g_main_context_iteration (GMainContext *context, gboolean may_block);
288 return g_main_context_iteration(gMainContext, mayBlock);
293 * Checks if any sources have pending events for the given context.
294 * context:
295 * a GMainContext (if NULL, the default context will be used)
296 * Returns:
297 * TRUE if events are pending.
299 public int pending()
301 // gboolean g_main_context_pending (GMainContext *context);
302 return g_main_context_pending(gMainContext);
307 * Finds a GSource given a pair of context and ID.
308 * context:
309 * a GMainContext (if NULL, the default context will be used)
310 * source_id:
311 * the source ID, as returned by g_source_get_id().
312 * Returns:
313 * the GSource if found, otherwise, NULL
315 public Source findSourceById(uint sourceId)
317 // GSource* g_main_context_find_source_by_id (GMainContext *context, guint source_id);
318 return new Source( g_main_context_find_source_by_id(gMainContext, sourceId) );
322 * Finds a source with the given user data for the callback. If
323 * multiple sources exist with the same user data, the first
324 * one found will be returned.
325 * context:
326 * a GMainContext
327 * user_data:
328 * the user_data for the callback.
329 * Returns:
330 * the source, if one was found, otherwise NULL
332 public Source findSourceByUserData(void* userData)
334 // GSource* g_main_context_find_source_by_user_data (GMainContext *context, gpointer user_data);
335 return new Source( g_main_context_find_source_by_user_data(gMainContext, userData) );
339 * Finds a source with the given source functions and user data. If
340 * multiple sources exist with the same source function and user data,
341 * the first one found will be returned.
342 * context:
343 * a GMainContext (if NULL, the default context will be used).
344 * funcs:
345 * the source_funcs passed to g_source_new().
346 * user_data:
347 * the user data from the callback.
348 * Returns:
349 * the source, if one was found, otherwise NULL
351 public Source findSourceByFuncsUserData(GSourceFuncs* funcs, void* userData)
353 // GSource* g_main_context_find_source_by_funcs_user_data (GMainContext *context, GSourceFuncs *funcs, gpointer user_data);
354 return new Source( g_main_context_find_source_by_funcs_user_data(gMainContext, funcs, userData) );
358 * If context is currently waiting in a poll(), interrupt
359 * the poll(), and continue the iteration process.
360 * context:
361 * a GMainContext
363 public void wakeup()
365 // void g_main_context_wakeup (GMainContext *context);
366 g_main_context_wakeup(gMainContext);
370 * Tries to become the owner of the specified context.
371 * If some other context is the owner of the context,
372 * returns FALSE immediately. Ownership is properly
373 * recursive: the owner can require ownership again
374 * and will release ownership when g_main_context_release()
375 * is called as many times as g_main_context_acquire().
376 * You must be the owner of a context before you
377 * can call g_main_context_prepare(), g_main_context_query(),
378 * g_main_context_check(), g_main_context_dispatch().
379 * context:
380 * a GMainContext
381 * Returns:
382 * TRUE if the operation succeeded, and
383 * this thread is now the owner of context.
385 public int acquire()
387 // gboolean g_main_context_acquire (GMainContext *context);
388 return g_main_context_acquire(gMainContext);
392 * Releases ownership of a context previously acquired by this thread
393 * with g_main_context_acquire(). If the context was acquired multiple
394 * times, the only release ownership when g_main_context_release()
395 * is called as many times as it was acquired.
396 * context:
397 * a GMainContext
399 public void release()
401 // void g_main_context_release (GMainContext *context);
402 g_main_context_release(gMainContext);
406 * Determines whether this thread holds the (recursive)
407 * ownership of this GMaincontext. This is useful to
408 * know before waiting on another thread that may be
409 * blocking to get ownership of context.
410 * context:
411 * a GMainContext
412 * Returns:
413 * TRUE if current thread is owner of context.
414 * Since 2.10
416 public int isOwner()
418 // gboolean g_main_context_is_owner (GMainContext *context);
419 return g_main_context_is_owner(gMainContext);
423 * Tries to become the owner of the specified context,
424 * as with g_main_context_acquire(). But if another thread
425 * is the owner, atomically drop mutex and wait on cond until
426 * that owner releases ownership or until cond is signaled, then
427 * try again (once) to become the owner.
428 * context:
429 * a GMainContext
430 * cond:
431 * a condition variable
432 * mutex:
433 * a mutex, currently held
434 * Returns:
435 * TRUE if the operation succeeded, and
436 * this thread is now the owner of context.
438 public int wait(Cond cond, Mutex mutex)
440 // gboolean g_main_context_wait (GMainContext *context, GCond *cond, GMutex *mutex);
441 return g_main_context_wait(gMainContext, (cond is null) ? null : cond.getCondStruct(), (mutex is null) ? null : mutex.getMutexStruct());
445 * Prepares to poll sources within a main loop. The resulting information
446 * for polling is determined by calling g_main_context_query().
447 * context:
448 * a GMainContext
449 * priority:
450 * location to store priority of highest priority
451 * source already ready.
452 * Returns:
453 * TRUE if some source is ready to be dispatched
454 * prior to polling.
456 public int prepare(int* priority)
458 // gboolean g_main_context_prepare (GMainContext *context, gint *priority);
459 return g_main_context_prepare(gMainContext, priority);
463 * Determines information necessary to poll this main loop.
464 * context:
465 * a GMainContext
466 * max_priority:
467 * maximum priority source to check
468 * timeout_:
469 * location to store timeout to be used in polling
470 * fds:
471 * location to store GPollFD records that need to be polled.
472 * n_fds:
473 * length of fds.
474 * Returns:
475 * the number of records actually stored in fds,
476 * or, if more than n_fds records need to be stored, the number
477 * of records that need to be stored.
479 public int query(int maxPriority, int* timeout, GPollFD* fds, int nFds)
481 // gint g_main_context_query (GMainContext *context, gint max_priority, gint *timeout_, GPollFD *fds, gint n_fds);
482 return g_main_context_query(gMainContext, maxPriority, timeout, fds, nFds);
486 * Passes the results of polling back to the main loop.
487 * context:
488 * a GMainContext
489 * max_priority:
490 * the maximum numerical priority of sources to check
491 * fds:
492 * array of GPollFD's that was passed to the last call to
493 * g_main_context_query()
494 * n_fds:
495 * return value of g_main_context_query()
496 * Returns:
497 * TRUE if some sources are ready to be dispatched.
499 public int check(int maxPriority, GPollFD* fds, int nFds)
501 // gint g_main_context_check (GMainContext *context, gint max_priority, GPollFD *fds, gint n_fds);
502 return g_main_context_check(gMainContext, maxPriority, fds, nFds);
506 * Dispatches all pending sources.
507 * context:
508 * a GMainContext
510 public void dispatch()
512 // void g_main_context_dispatch (GMainContext *context);
513 g_main_context_dispatch(gMainContext);
517 * Sets the function to use to handle polling of file descriptors. It
518 * will be used instead of the poll() system call
519 * (or GLib's replacement function, which is used where
520 * poll() isn't available).
521 * This function could possibly be used to integrate the GLib event
522 * loop with an external event loop.
523 * context:
524 * a GMainContext
525 * func:
526 * the function to call to poll all file descriptors
528 public void setPollFunc(GPollFunc func)
530 // void g_main_context_set_poll_func (GMainContext *context, GPollFunc func);
531 g_main_context_set_poll_func(gMainContext, func);
535 * Gets the poll function set by g_main_context_set_poll_func().
536 * context:
537 * a GMainContext
538 * Returns:
539 * the poll function
541 public GPollFunc getPollFunc()
543 // GPollFunc g_main_context_get_poll_func (GMainContext *context);
544 return g_main_context_get_poll_func(gMainContext);
549 * Adds a file descriptor to the set of file descriptors polled for
550 * this context. This will very seldomly be used directly. Instead
551 * a typical event source will use g_source_add_poll() instead.
552 * context:
553 * a GMainContext (or NULL for the default context)
554 * fd:
555 * a GPollFD structure holding information about a file
556 * descriptor to watch.
557 * priority:
558 * the priority for this file descriptor which should be
559 * the same as the priority used for g_source_attach() to ensure that the
560 * file descriptor is polled whenever the results may be needed.
562 public void addPoll(GPollFD* fd, int priority)
564 // void g_main_context_add_poll (GMainContext *context, GPollFD *fd, gint priority);
565 g_main_context_add_poll(gMainContext, fd, priority);
569 * Removes file descriptor from the set of file descriptors to be
570 * polled for a particular context.
571 * context:
572 * a GMainContext
573 * fd:
574 * a GPollFD descriptor previously added with g_main_context_add_poll()
576 public void removePoll(GPollFD* fd)
578 // void g_main_context_remove_poll (GMainContext *context, GPollFD *fd);
579 g_main_context_remove_poll(gMainContext, fd);