alternative to assert
[gtkD.git] / gtkD / src / glib / Source.d
bloba4c1ab7ee589f72ed001b3676727cbe49a16c40f
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 = Source
28 * strct = GSource
29 * realStrct=
30 * ctorStrct=
31 * clss = Source
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_source_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * - glib.MainLoop
45 * - glib.Dataset
46 * - glib.Date
47 * - glib.Source
48 * - glib.MainContext
49 * structWrap:
50 * - GDataset* -> Dataset
51 * - GDate* -> Date
52 * - GMainContext* -> MainContext
53 * - GMainLoop* -> MainLoop
54 * - GSource* -> Source
55 * module aliases:
56 * local aliases:
59 module glib.Source;
61 version(noAssert)
63 version(Tango)
65 import tango.io.Stdout; // use the tango loging?
69 private import gtkc.glibtypes;
71 private import gtkc.glib;
74 private import glib.MainLoop;
75 private import glib.Dataset;
76 private import glib.Date;
77 private import glib.Source;
78 private import glib.MainContext;
83 /**
84 * Description
85 * The main event loop manages all the available sources of events for
86 * GLib and GTK+ applications. These events can come from any number of
87 * different types of sources such as file descriptors (plain files,
88 * pipes or sockets) and timeouts. New types of event sources can also
89 * be added using g_source_attach().
90 * To allow multiple independent sets of sources to be handled in
91 * different threads, each source is associated with a GMainContext.
92 * A GMainContext can only be running in a single thread, but
93 * sources can be added to it and removed from it from other threads.
94 * Each event source is assigned a priority. The default priority,
95 * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher
96 * priorities. Values greater than 0 denote lower priorities. Events
97 * from high priority sources are always processed before events from
98 * lower priority sources.
99 * Idle functions can also be added, and assigned a priority. These will
100 * be run whenever no events with a higher priority are ready to be
101 * processed.
102 * The GMainLoop data type represents a main event loop. A GMainLoop
103 * is created with g_main_loop_new(). After adding the initial event sources,
104 * g_main_loop_run() is called. This continuously checks for new events from
105 * each of the event sources and dispatches them. Finally, the
106 * processing of an event from one of the sources leads to a call to
107 * g_main_loop_quit() to exit the main loop, and g_main_loop_run() returns.
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 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
114 * gtk_main_quit() and gtk_events_pending().
115 * Creating new sources types
116 * One of the unusual features of the GTK+ main loop functionality
117 * is that new types of event source can be created and used in
118 * addition to the builtin type of event source. A new event source
119 * type is used for handling GDK events. A new source type is
120 * created by deriving from the GSource
121 * structure. The derived type of source is represented by a
122 * structure that has the GSource structure as a first element,
123 * and other elements specific to the new source type. To create
124 * an instance of the new source type, call g_source_new() passing
125 * in the size of the derived structure and a table of functions.
126 * These GSourceFuncs determine the behavior of the new source
127 * types.
128 * New source types basically interact with with the main context
129 * in two ways. Their prepare function in GSourceFuncs can set
130 * a timeout to determine the maximum amount of time that the
131 * main loop will sleep before checking the source again. In
132 * addition, or as well, the source can add file descriptors to
133 * the set that the main context checks using g_source_add_poll().
134 * <hr>
135 * Customizing the main loop iteration
136 * Single iterations of a GMainContext can be run with
137 * g_main_context_iteration(). In some cases, more detailed control
138 * of exactly how the details of the main loop work is desired,
139 * for instance, when integrating the GMainLoop with an external
140 * main loop. In such cases, you can call the component functions
141 * of g_main_context_iteration() directly. These functions
142 * are g_main_context_prepare(), g_main_context_query(),
143 * g_main_context_check() and g_main_context_dispatch().
144 * The operation of these functions can best be seen in terms
145 * of a state diagram, as shown in Figure1, States of a Main Context.
146 * Figure1.States of a Main Context
148 public class Source
151 /** the main Gtk struct */
152 protected GSource* gSource;
155 public GSource* getSourceStruct()
157 return gSource;
161 /** the main Gtk struct as a void* */
162 protected void* getStruct()
164 return cast(void*)gSource;
168 * Sets our main struct and passes it to the parent class
170 public this (GSource* gSource)
172 version(noAssert)
174 if ( gSource is null )
176 int zero = 0;
177 version(Tango)
179 Stdout("struct gSource is null on constructor").newline;
181 else
183 printf("struct gSource is null on constructor");
185 zero = zero / zero;
188 else
190 assert(gSource !is null, "struct gSource is null on constructor");
192 this.gSource = gSource;
265 * Creates a new GSource structure. The size is specified to
266 * allow creating structures derived from GSource that contain
267 * additional data. The size passed in must be at least
268 * sizeof (GSource).
269 * The source will not initially be associated with any GMainContext
270 * and must be added to one with g_source_attach() before it will be
271 * executed.
272 * source_funcs:
273 * structure containing functions that implement
274 * the sources behavior.
275 * struct_size:
276 * size of the GSource structure to create.
277 * Returns:
278 * the newly-created GSource.
280 public this (GSourceFuncs* sourceFuncs, uint structSize)
282 // GSource* g_source_new (GSourceFuncs *source_funcs, guint struct_size);
283 this(cast(GSource*)g_source_new(sourceFuncs, structSize) );
287 * Increases the reference count on a source by one.
288 * source:
289 * a GSource
290 * Returns:
291 * source
293 public Source doref()
295 // GSource* g_source_ref (GSource *source);
296 return new Source( g_source_ref(gSource) );
300 * Decreases the reference count of a source by one. If the
301 * resulting reference count is zero the source and associated
302 * memory will be destroyed.
303 * source:
304 * a GSource
306 public void unref()
308 // void g_source_unref (GSource *source);
309 g_source_unref(gSource);
313 * Sets the source functions (can be used to override
314 * default implementations) of an unattached source.
315 * source:
316 * a GSource
317 * funcs:
318 * the new GSourceFuncs
319 * Since 2.12
321 public void setFuncs(GSourceFuncs* funcs)
323 // void g_source_set_funcs (GSource *source, GSourceFuncs *funcs);
324 g_source_set_funcs(gSource, funcs);
328 * Adds a GSource to a context so that it will be executed within
329 * that context.
330 * source:
331 * a GSource
332 * context:
333 * a GMainContext (if NULL, the default context will be used)
334 * Returns:
335 * the ID (greater than 0) for the source within the
336 * GMainContext.
338 public uint attach(MainContext context)
340 // guint g_source_attach (GSource *source, GMainContext *context);
341 return g_source_attach(gSource, (context is null) ? null : context.getMainContextStruct());
345 * Removes a source from its GMainContext, if any, and mark it as
346 * destroyed. The source cannot be subsequently added to another
347 * context.
348 * source:
349 * a GSource
351 public void destroy()
353 // void g_source_destroy (GSource *source);
354 g_source_destroy(gSource);
358 * Returns whether source has been destroyed.
359 * This is important when you operate upon your objects
360 * from within idle handlers, but may have freed the object
361 * before the dispatch of your idle handler.
362 * static gboolean
363 * idle_callback (gpointer data)
365 * SomeWidget *self = data;
366 * GDK_THREADS_ENTER ();
367 * /+* do stuff with self +/
368 * self->idle_id = 0;
369 * GDK_THREADS_LEAVE ();
370 * return FALSE;
372 * static void
373 * some_widget_do_stuff_later (SomeWidget *self)
375 * self->idle_id = g_idle_add (idle_callback, self);
377 * static void
378 * some_widget_finalize (GObject *object)
380 * SomeWidget *self = SOME_WIDGET (object);
381 * if (self->idle_id)
382 * g_source_remove (self->idle_id);
383 * G_OBJECT_CLASS (parent_class)->finalize (object);
385 * This will fail in a multi-threaded application if the
386 * widget is destroyed before the idle handler fires due
387 * to the use after free in the callback. A solution, to
388 * this particular problem, is to check to if the source
389 * has already been destroy within the callback.
390 * static gboolean
391 * idle_callback (gpointer data)
393 * SomeWidget *self = data;
394 * GDK_THREADS_ENTER ();
395 * if (!g_source_is_destroyed (g_main_current_source ()))
397 * /+* do stuff with self +/
399 * GDK_THREADS_LEAVE ();
400 * return FALSE;
402 * source:
403 * a GSource
404 * Returns:
405 * TRUE if the source has been destroyed
406 * Since 2.12
408 public int isDestroyed()
410 // gboolean g_source_is_destroyed (GSource *source);
411 return g_source_is_destroyed(gSource);
415 * Sets the priority of a source. While the main loop is being
416 * run, a source will be dispatched if it is ready to be dispatched and no sources
417 * at a higher (numerically smaller) priority are ready to be dispatched.
418 * source:
419 * a GSource
420 * priority:
421 * the new priority.
423 public void setPriority(int priority)
425 // void g_source_set_priority (GSource *source, gint priority);
426 g_source_set_priority(gSource, priority);
430 * Gets the priority of a source.
431 * source:
432 * a GSource
433 * Returns:
434 * the priority of the source
436 public int getPriority()
438 // gint g_source_get_priority (GSource *source);
439 return g_source_get_priority(gSource);
443 * Sets whether a source can be called recursively. If can_recurse is
444 * TRUE, then while the source is being dispatched then this source
445 * will be processed normally. Otherwise, all processing of this
446 * source is blocked until the dispatch function returns.
447 * source:
448 * a GSource
449 * can_recurse:
450 * whether recursion is allowed for this source
452 public void setCanRecurse(int canRecurse)
454 // void g_source_set_can_recurse (GSource *source, gboolean can_recurse);
455 g_source_set_can_recurse(gSource, canRecurse);
459 * Checks whether a source is allowed to be called recursively.
460 * see g_source_set_can_recurse().
461 * source:
462 * a GSource
463 * Returns:
464 * whether recursion is allowed.
466 public int getCanRecurse()
468 // gboolean g_source_get_can_recurse (GSource *source);
469 return g_source_get_can_recurse(gSource);
473 * Returns the numeric ID for a particular source. The ID of a source
474 * is a positive integer which is unique within a particular main loop
475 * context. The reverse
476 * mapping from ID to source is done by g_main_context_find_source_by_id().
477 * source:
478 * a GSource
479 * Returns:
480 * the ID (greater than 0) for the source
482 public uint getId()
484 // guint g_source_get_id (GSource *source);
485 return g_source_get_id(gSource);
489 * Gets the GMainContext with which the source is associated.
490 * Calling this function on a destroyed source is an error.
491 * source:
492 * a GSource
493 * Returns:
494 * the GMainContext with which the source is associated,
495 * or NULL if the context has not yet been added
496 * to a source.
498 public MainContext getContext()
500 // GMainContext* g_source_get_context (GSource *source);
501 return new MainContext( g_source_get_context(gSource) );
505 * Sets the callback function for a source. The callback for a source is
506 * called from the source's dispatch function.
507 * The exact type of func depends on the type of source; ie. you
508 * should not count on func being called with data as its first
509 * parameter.
510 * Typically, you won't use this function. Instead use functions specific
511 * to the type of source you are using.
512 * source:
513 * the source
514 * func:
515 * a callback function
516 * data:
517 * the data to pass to callback function
518 * notify:
519 * a function to call when data is no longer in use, or NULL.
521 public void setCallback(GSourceFunc func, void* data, GDestroyNotify notify)
523 // void g_source_set_callback (GSource *source, GSourceFunc func, gpointer data, GDestroyNotify notify);
524 g_source_set_callback(gSource, func, data, notify);
529 * Sets the callback function storing the data as a refcounted callback
530 * "object". This is used internally. Note that calling
531 * g_source_set_callback_indirect() assumes
532 * an initial reference count on callback_data, and thus
533 * callback_funcs->unref will eventually be called once more
534 * than callback_funcs->ref.
535 * source:
536 * the source
537 * callback_data:
538 * pointer to callback data "object"
539 * callback_funcs:
540 * functions for reference counting callback_data
541 * and getting the callback and data
543 public void setCallbackIndirect(void* callbackData, GSourceCallbackFuncs* callbackFuncs)
545 // void g_source_set_callback_indirect (GSource *source, gpointer callback_data, GSourceCallbackFuncs *callback_funcs);
546 g_source_set_callback_indirect(gSource, callbackData, callbackFuncs);
550 * Adds a file descriptor to the set of file descriptors polled for
551 * this source. This is usually combined with g_source_new() to add an
552 * event source. The event source's check function will typically test
553 * the revents field in the GPollFD struct and return TRUE if events need
554 * to be processed.
555 * source:
556 * a GSource
557 * fd:
558 * a GPollFD structure holding information about a file
559 * descriptor to watch.
561 public void addPoll(GPollFD* fd)
563 // void g_source_add_poll (GSource *source, GPollFD *fd);
564 g_source_add_poll(gSource, fd);
568 * Removes a file descriptor from the set of file descriptors polled for
569 * this source.
570 * source:
571 * a GSource
572 * fd:
573 * a GPollFD structure previously passed to g_source_add_poll().
575 public void removePoll(GPollFD* fd)
577 // void g_source_remove_poll (GSource *source, GPollFD *fd);
578 g_source_remove_poll(gSource, fd);
582 * Gets the "current time" to be used when checking
583 * this source. The advantage of calling this function over
584 * calling g_get_current_time() directly is that when
585 * checking multiple sources, GLib can cache a single value
586 * instead of having to repeatedly get the system time.
587 * source:
588 * a GSource
589 * timeval:
590 * GTimeVal structure in which to store current time.
592 public void getCurrentTime(GTimeVal* timeval)
594 // void g_source_get_current_time (GSource *source, GTimeVal *timeval);
595 g_source_get_current_time(gSource, timeval);
599 * Removes the source with the given id from the default main context.
600 * The id of
601 * a GSource is given by g_source_get_id(), or will be returned by the
602 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
603 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
604 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
605 * See also g_source_destroy().
606 * tag:
607 * the ID of the source to remove.
608 * Returns:
609 * TRUE if the source was found and removed.
611 public static int remove(uint tag)
613 // gboolean g_source_remove (guint tag);
614 return g_source_remove(tag);
618 * Removes a source from the default main loop context given the
619 * source functions and user data. If multiple sources exist with the
620 * same source functions and user data, only one will be destroyed.
621 * funcs:
622 * The source_funcs passed to g_source_new()
623 * user_data:
624 * the user data for the callback
625 * Returns:
626 * TRUE if a source was found and removed.
628 public static int removeByFuncsUserData(GSourceFuncs* funcs, void* userData)
630 // gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs, gpointer user_data);
631 return g_source_remove_by_funcs_user_data(funcs, userData);
635 * Removes a source from the default main loop context given the user
636 * data for the callback. If multiple sources exist with the same user
637 * data, only one will be destroyed.
638 * user_data:
639 * the user_data for the callback.
640 * Returns:
641 * TRUE if a source was found and removed.
643 public static int removeByUserData(void* userData)
645 // gboolean g_source_remove_by_user_data (gpointer user_data);
646 return g_source_remove_by_user_data(userData);