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  = 
27  * outPack = glib
28  * outFile = Source
29  * strct   = GSource
30  * realStrct=
31  * ctorStrct=
32  * clss    = Source
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_source_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * 	- glib.MainContext
48  * 	- glib.TimeVal
49  * structWrap:
50  * 	- GMainContext* -> MainContext
51  * 	- GSource* -> Source
52  * 	- GTimeVal* -> TimeVal
53  * module aliases:
54  * local aliases:
55  * overrides:
56  */
57 
58 module glib.Source;
59 
60 public  import gtkc.glibtypes;
61 
62 private import gtkc.glib;
63 private import glib.ConstructionException;
64 
65 
66 private import glib.Str;
67 private import glib.MainContext;
68 private import glib.TimeVal;
69 
70 
71 
72 
73 /**
74  * Description
75  * The main event loop manages all the available sources of events for
76  * GLib and GTK+ applications. These events can come from any number of
77  * different types of sources such as file descriptors (plain files,
78  * pipes or sockets) and timeouts. New types of event sources can also
79  * be added using g_source_attach().
80  * To allow multiple independent sets of sources to be handled in
81  * different threads, each source is associated with a GMainContext.
82  * A GMainContext can only be running in a single thread, but
83  * sources can be added to it and removed from it from other threads.
84  * Each event source is assigned a priority. The default priority,
85  * G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
86  * Values greater than 0 denote lower priorities. Events from high priority
87  * sources are always processed before events from lower priority sources.
88  * Idle functions can also be added, and assigned a priority. These will
89  * be run whenever no events with a higher priority are ready to be processed.
90  * The GMainLoop data type represents a main event loop. A GMainLoop is
91  * created with g_main_loop_new(). After adding the initial event sources,
92  * g_main_loop_run() is called. This continuously checks for new events from
93  * each of the event sources and dispatches them. Finally, the processing of
94  * an event from one of the sources leads to a call to g_main_loop_quit() to
95  * exit the main loop, and g_main_loop_run() returns.
96  * It is possible to create new instances of GMainLoop recursively.
97  * This is often used in GTK+ applications when showing modal dialog
98  * boxes. Note that event sources are associated with a particular
99  * GMainContext, and will be checked and dispatched for all main
100  * loops associated with that GMainContext.
101  * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
102  * gtk_main_quit() and gtk_events_pending().
103  * Creating new source types
104  * One of the unusual features of the GMainLoop functionality
105  * is that new types of event source can be created and used in
106  * addition to the builtin type of event source. A new event source
107  * type is used for handling GDK events. A new source type is created
108  * by deriving from the GSource structure.
109  * The derived type of source is represented by a structure that has
110  * the GSource structure as a first element, and other elements specific
111  * to the new source type. To create an instance of the new source type,
112  * call g_source_new() passing in the size of the derived structure and
113  * a table of functions. These GSourceFuncs determine the behavior of
114  * the new source type.
115  * New source types basically interact with the main context
116  * in two ways. Their prepare function in GSourceFuncs can set a timeout
117  * to determine the maximum amount of time that the main loop will sleep
118  * before checking the source again. In addition, or as well, the source
119  * can add file descriptors to the set that the main context checks using
120  * g_source_add_poll().
121  * <hr>
122  * Customizing the main loop iteration
123  * Single iterations of a GMainContext can be run with
124  * g_main_context_iteration(). In some cases, more detailed control
125  * of exactly how the details of the main loop work is desired, for
126  * instance, when integrating the GMainLoop with an external main loop.
127  * In such cases, you can call the component functions of
128  * g_main_context_iteration() directly. These functions are
129  * g_main_context_prepare(), g_main_context_query(),
130  * g_main_context_check() and g_main_context_dispatch().
131  * The operation of these functions can best be seen in terms
132  * of a state diagram, as shown in Figure 1, “States of a Main Context”.
133  * Figure 1. States of a Main Context
134  */
135 public class Source
136 {
137 	
138 	/** the main Gtk struct */
139 	protected GSource* gSource;
140 	
141 	
142 	public GSource* getSourceStruct()
143 	{
144 		return gSource;
145 	}
146 	
147 	
148 	/** the main Gtk struct as a void* */
149 	protected void* getStruct()
150 	{
151 		return cast(void*)gSource;
152 	}
153 	
154 	/**
155 	 * Sets our main struct and passes it to the parent class
156 	 */
157 	public this (GSource* gSource)
158 	{
159 		this.gSource = gSource;
160 	}
161 	
162 	/**
163 	 */
164 	
165 	/**
166 	 * Creates a new GSource structure. The size is specified to
167 	 * allow creating structures derived from GSource that contain
168 	 * additional data. The size passed in must be at least
169 	 * sizeof (GSource).
170 	 * The source will not initially be associated with any GMainContext
171 	 * and must be added to one with g_source_attach() before it will be
172 	 * executed.
173 	 * Params:
174 	 * sourceFuncs = structure containing functions that implement
175 	 * the sources behavior.
176 	 * structSize = size of the GSource structure to create.
177 	 * Throws: ConstructionException GTK+ fails to create the object.
178 	 */
179 	public this (GSourceFuncs* sourceFuncs, uint structSize)
180 	{
181 		// GSource * g_source_new (GSourceFuncs *source_funcs,  guint struct_size);
182 		auto p = g_source_new(sourceFuncs, structSize);
183 		if(p is null)
184 		{
185 			throw new ConstructionException("null returned by g_source_new(sourceFuncs, structSize)");
186 		}
187 		this(cast(GSource*) p);
188 	}
189 	
190 	/**
191 	 * Increases the reference count on a source by one.
192 	 * Returns: source
193 	 */
194 	public Source doref()
195 	{
196 		// GSource * g_source_ref (GSource *source);
197 		auto p = g_source_ref(gSource);
198 		
199 		if(p is null)
200 		{
201 			return null;
202 		}
203 		
204 		return new Source(cast(GSource*) p);
205 	}
206 	
207 	/**
208 	 * Decreases the reference count of a source by one. If the
209 	 * resulting reference count is zero the source and associated
210 	 * memory will be destroyed.
211 	 */
212 	public void unref()
213 	{
214 		// void g_source_unref (GSource *source);
215 		g_source_unref(gSource);
216 	}
217 	
218 	/**
219 	 * Sets the source functions (can be used to override
220 	 * default implementations) of an unattached source.
221 	 * Since 2.12
222 	 * Params:
223 	 * funcs = the new GSourceFuncs
224 	 */
225 	public void setFuncs(GSourceFuncs* funcs)
226 	{
227 		// void g_source_set_funcs (GSource *source,  GSourceFuncs *funcs);
228 		g_source_set_funcs(gSource, funcs);
229 	}
230 	
231 	/**
232 	 * Adds a GSource to a context so that it will be executed within
233 	 * that context. Remove it by calling g_source_destroy().
234 	 * Params:
235 	 * context = a GMainContext (if NULL, the default context will be used)
236 	 * Returns: the ID (greater than 0) for the source within the GMainContext.
237 	 */
238 	public uint attach(MainContext context)
239 	{
240 		// guint g_source_attach (GSource *source,  GMainContext *context);
241 		return g_source_attach(gSource, (context is null) ? null : context.getMainContextStruct());
242 	}
243 	
244 	/**
245 	 * Removes a source from its GMainContext, if any, and mark it as
246 	 * destroyed. The source cannot be subsequently added to another
247 	 * context.
248 	 */
249 	public void destroy()
250 	{
251 		// void g_source_destroy (GSource *source);
252 		g_source_destroy(gSource);
253 	}
254 	
255 	/**
256 	 * Returns whether source has been destroyed.
257 	 * This is important when you operate upon your objects
258 	 * from within idle handlers, but may have freed the object
259 	 * before the dispatch of your idle handler.
260 	 * $(DDOC_COMMENT example)
261 	 * This will fail in a multi-threaded application if the
262 	 * widget is destroyed before the idle handler fires due
263 	 * to the use after free in the callback. A solution, to
264 	 * this particular problem, is to check to if the source
265 	 * has already been destroy within the callback.
266 	 * $(DDOC_COMMENT example)
267 	 * Since 2.12
268 	 * Returns: TRUE if the source has been destroyed
269 	 */
270 	public int isDestroyed()
271 	{
272 		// gboolean g_source_is_destroyed (GSource *source);
273 		return g_source_is_destroyed(gSource);
274 	}
275 	
276 	/**
277 	 * Sets the priority of a source. While the main loop is being run, a
278 	 * source will be dispatched if it is ready to be dispatched and no
279 	 * sources at a higher (numerically smaller) priority are ready to be
280 	 * dispatched.
281 	 * Params:
282 	 * priority = the new priority.
283 	 */
284 	public void setPriority(int priority)
285 	{
286 		// void g_source_set_priority (GSource *source,  gint priority);
287 		g_source_set_priority(gSource, priority);
288 	}
289 	
290 	/**
291 	 * Gets the priority of a source.
292 	 * Returns: the priority of the source
293 	 */
294 	public int getPriority()
295 	{
296 		// gint g_source_get_priority (GSource *source);
297 		return g_source_get_priority(gSource);
298 	}
299 	
300 	/**
301 	 * Sets whether a source can be called recursively. If can_recurse is
302 	 * TRUE, then while the source is being dispatched then this source
303 	 * will be processed normally. Otherwise, all processing of this
304 	 * source is blocked until the dispatch function returns.
305 	 * Params:
306 	 * canRecurse = whether recursion is allowed for this source
307 	 */
308 	public void setCanRecurse(int canRecurse)
309 	{
310 		// void g_source_set_can_recurse (GSource *source,  gboolean can_recurse);
311 		g_source_set_can_recurse(gSource, canRecurse);
312 	}
313 	
314 	/**
315 	 * Checks whether a source is allowed to be called recursively.
316 	 * see g_source_set_can_recurse().
317 	 * Returns: whether recursion is allowed.
318 	 */
319 	public int getCanRecurse()
320 	{
321 		// gboolean g_source_get_can_recurse (GSource *source);
322 		return g_source_get_can_recurse(gSource);
323 	}
324 	
325 	/**
326 	 * Returns the numeric ID for a particular source. The ID of a source
327 	 * is a positive integer which is unique within a particular main loop
328 	 * context. The reverse
329 	 * mapping from ID to source is done by g_main_context_find_source_by_id().
330 	 * Returns: the ID (greater than 0) for the source
331 	 */
332 	public uint getId()
333 	{
334 		// guint g_source_get_id (GSource *source);
335 		return g_source_get_id(gSource);
336 	}
337 	
338 	/**
339 	 * Gets a name for the source, used in debugging and profiling.
340 	 * The name may be NULL if it has never been set with
341 	 * g_source_set_name().
342 	 * Since 2.26
343 	 * Returns: the name of the source
344 	 */
345 	public string getName()
346 	{
347 		// const char * g_source_get_name (GSource *source);
348 		return Str.toString(g_source_get_name(gSource));
349 	}
350 	
351 	/**
352 	 * Sets a name for the source, used in debugging and profiling.
353 	 * The name defaults to NULL.
354 	 * The source name should describe in a human-readable way
355 	 * what the source does. For example, "X11 event queue"
356 	 * or "GTK+ repaint idle handler" or whatever it is.
357 	 * It is permitted to call this function multiple times, but is not
358 	 * recommended due to the potential performance impact. For example,
359 	 * one could change the name in the "check" function of a GSourceFuncs
360 	 * to include details like the event type in the source name.
361 	 * Since 2.26
362 	 * Params:
363 	 * name = debug name for the source
364 	 */
365 	public void setName(string name)
366 	{
367 		// void g_source_set_name (GSource *source,  const char *name);
368 		g_source_set_name(gSource, Str.toStringz(name));
369 	}
370 	
371 	/**
372 	 * Sets the name of a source using its ID.
373 	 * This is a convenience utility to set source names from the return
374 	 * value of g_idle_add(), g_timeout_add(), etc.
375 	 * Since 2.26
376 	 * Params:
377 	 * tag = a GSource ID
378 	 * name = debug name for the source
379 	 */
380 	public static void setNameById(uint tag, string name)
381 	{
382 		// void g_source_set_name_by_id (guint tag,  const char *name);
383 		g_source_set_name_by_id(tag, Str.toStringz(name));
384 	}
385 	
386 	/**
387 	 * Gets the GMainContext with which the source is associated.
388 	 * Calling this function on a destroyed source is an error.
389 	 * Returns: the GMainContext with which the source is associated, or NULL if the context has not yet been added to a source.
390 	 */
391 	public MainContext getContext()
392 	{
393 		// GMainContext * g_source_get_context (GSource *source);
394 		auto p = g_source_get_context(gSource);
395 		
396 		if(p is null)
397 		{
398 			return null;
399 		}
400 		
401 		return new MainContext(cast(GMainContext*) p);
402 	}
403 	
404 	/**
405 	 * Sets the callback function for a source. The callback for a source is
406 	 * called from the source's dispatch function.
407 	 * The exact type of func depends on the type of source; ie. you
408 	 * should not count on func being called with data as its first
409 	 * parameter.
410 	 * Typically, you won't use this function. Instead use functions specific
411 	 * to the type of source you are using.
412 	 * Params:
413 	 * func = a callback function
414 	 * data = the data to pass to callback function
415 	 * notify = a function to call when data is no longer in use, or NULL.
416 	 */
417 	public void setCallback(GSourceFunc func, void* data, GDestroyNotify notify)
418 	{
419 		// void g_source_set_callback (GSource *source,  GSourceFunc func,  gpointer data,  GDestroyNotify notify);
420 		g_source_set_callback(gSource, func, data, notify);
421 	}
422 	
423 	/**
424 	 * Sets the callback function storing the data as a refcounted callback
425 	 * "object". This is used internally. Note that calling
426 	 * g_source_set_callback_indirect() assumes
427 	 * an initial reference count on callback_data, and thus
428 	 * callback_funcs->unref will eventually be called once more
429 	 * than callback_funcs->ref.
430 	 * Params:
431 	 * callbackData = pointer to callback data "object"
432 	 * callbackFuncs = functions for reference counting callback_data
433 	 * and getting the callback and data
434 	 */
435 	public void setCallbackIndirect(void* callbackData, GSourceCallbackFuncs* callbackFuncs)
436 	{
437 		// void g_source_set_callback_indirect (GSource *source,  gpointer callback_data,  GSourceCallbackFuncs *callback_funcs);
438 		g_source_set_callback_indirect(gSource, callbackData, callbackFuncs);
439 	}
440 	
441 	/**
442 	 * Adds a file descriptor to the set of file descriptors polled for
443 	 * this source. This is usually combined with g_source_new() to add an
444 	 * event source. The event source's check function will typically test
445 	 * the revents field in the GPollFD struct and return TRUE if events need
446 	 * to be processed.
447 	 * Params:
448 	 * fd = a GPollFD structure holding information about a file
449 	 * descriptor to watch.
450 	 */
451 	public void addPoll(GPollFD* fd)
452 	{
453 		// void g_source_add_poll (GSource *source,  GPollFD *fd);
454 		g_source_add_poll(gSource, fd);
455 	}
456 	
457 	/**
458 	 * Removes a file descriptor from the set of file descriptors polled for
459 	 * this source.
460 	 * Params:
461 	 * fd = a GPollFD structure previously passed to g_source_add_poll().
462 	 */
463 	public void removePoll(GPollFD* fd)
464 	{
465 		// void g_source_remove_poll (GSource *source,  GPollFD *fd);
466 		g_source_remove_poll(gSource, fd);
467 	}
468 	
469 	/**
470 	 * Adds child_source to source as a "polled" source; when source is
471 	 * added to a GMainContext, child_source will be automatically added
472 	 * with the same priority, when child_source is triggered, it will
473 	 * cause source to dispatch (in addition to calling its own
474 	 * callback), and when source is destroyed, it will destroy
475 	 * child_source as well. (source will also still be dispatched if
476 	 * its own prepare/check functions indicate that it is ready.)
477 	 * If you don't need child_source to do anything on its own when it
478 	 * triggers, you can call g_source_set_dummy_callback() on it to set a
479 	 * callback that does nothing (except return TRUE if appropriate).
480 	 * source will hold a reference on child_source while child_source
481 	 * is attached to it.
482 	 * Since 2.28
483 	 * Params:
484 	 * childSource = a second GSource that source should "poll"
485 	 */
486 	public void addChildSource(Source childSource)
487 	{
488 		// void g_source_add_child_source (GSource *source,  GSource *child_source);
489 		g_source_add_child_source(gSource, (childSource is null) ? null : childSource.getSourceStruct());
490 	}
491 	
492 	/**
493 	 * Detaches child_source from source and destroys it.
494 	 * Since 2.28
495 	 * Params:
496 	 * childSource = a GSource previously passed to
497 	 * g_source_add_child_source().
498 	 */
499 	public void removeChildSource(Source childSource)
500 	{
501 		// void g_source_remove_child_source (GSource *source,  GSource *child_source);
502 		g_source_remove_child_source(gSource, (childSource is null) ? null : childSource.getSourceStruct());
503 	}
504 	
505 	/**
506 	 * Gets the time to be used when checking this source. The advantage of
507 	 * calling this function over calling g_get_monotonic_time() directly is
508 	 * that when checking multiple sources, GLib can cache a single value
509 	 * instead of having to repeatedly get the system monotonic time.
510 	 * The time here is the system monotonic time, if available, or some
511 	 * other reasonable alternative otherwise. See g_get_monotonic_time().
512 	 * Since 2.28
513 	 * Returns: the monotonic time in microseconds
514 	 */
515 	public long getTime()
516 	{
517 		// gint64 g_source_get_time (GSource *source);
518 		return g_source_get_time(gSource);
519 	}
520 	
521 	/**
522 	 * Warning
523 	 * g_source_get_current_time has been deprecated since version 2.28 and should not be used in newly-written code. use g_source_get_time() instead
524 	 * Gets the "current time" to be used when checking
525 	 * this source. The advantage of calling this function over
526 	 * calling g_get_current_time() directly is that when
527 	 * checking multiple sources, GLib can cache a single value
528 	 * instead of having to repeatedly get the system time.
529 	 * Params:
530 	 * timeval = GTimeVal structure in which to store current time.
531 	 */
532 	public void getCurrentTime(TimeVal timeval)
533 	{
534 		// void g_source_get_current_time (GSource *source,  GTimeVal *timeval);
535 		g_source_get_current_time(gSource, (timeval is null) ? null : timeval.getTimeValStruct());
536 	}
537 	
538 	/**
539 	 * Removes the source with the given id from the default main context.
540 	 * The id of
541 	 * a GSource is given by g_source_get_id(), or will be returned by the
542 	 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
543 	 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
544 	 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
545 	 * See also g_source_destroy(). You must use g_source_destroy() for sources
546 	 * added to a non-default main context.
547 	 * Params:
548 	 * tag = the ID of the source to remove.
549 	 * Returns: TRUE if the source was found and removed.
550 	 */
551 	public static int remove(uint tag)
552 	{
553 		// gboolean g_source_remove (guint tag);
554 		return g_source_remove(tag);
555 	}
556 	
557 	/**
558 	 * Removes a source from the default main loop context given the
559 	 * source functions and user data. If multiple sources exist with the
560 	 * same source functions and user data, only one will be destroyed.
561 	 * Params:
562 	 * funcs = The source_funcs passed to g_source_new()
563 	 * userData = the user data for the callback
564 	 * Returns: TRUE if a source was found and removed.
565 	 */
566 	public static int removeByFuncsUserData(GSourceFuncs* funcs, void* userData)
567 	{
568 		// gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,  gpointer user_data);
569 		return g_source_remove_by_funcs_user_data(funcs, userData);
570 	}
571 	
572 	/**
573 	 * Removes a source from the default main loop context given the user
574 	 * data for the callback. If multiple sources exist with the same user
575 	 * data, only one will be destroyed.
576 	 * Params:
577 	 * userData = the user_data for the callback.
578 	 * Returns: TRUE if a source was found and removed.
579 	 */
580 	public static int removeByUserData(void* userData)
581 	{
582 		// gboolean g_source_remove_by_user_data (gpointer user_data);
583 		return g_source_remove_by_user_data(userData);
584 	}
585 }