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 module glib.MainContext;
26 
27 private import glib.Cond;
28 private import glib.ConstructionException;
29 private import glib.Mutex;
30 private import glib.Source;
31 private import glib.c.functions;
32 public  import glib.c.types;
33 private import gtkd.Loader;
34 
35 
36 /**
37  * The `GMainContext` struct is an opaque data
38  * type representing a set of sources to be handled in a main loop.
39  */
40 public class MainContext
41 {
42 	/** the main Gtk struct */
43 	protected GMainContext* gMainContext;
44 	protected bool ownedRef;
45 
46 	/** Get the main Gtk struct */
47 	public GMainContext* getMainContextStruct(bool transferOwnership = false)
48 	{
49 		if (transferOwnership)
50 			ownedRef = false;
51 		return gMainContext;
52 	}
53 
54 	/** the main Gtk struct as a void* */
55 	protected void* getStruct()
56 	{
57 		return cast(void*)gMainContext;
58 	}
59 
60 	/**
61 	 * Sets our main struct and passes it to the parent class.
62 	 */
63 	public this (GMainContext* gMainContext, bool ownedRef = false)
64 	{
65 		this.gMainContext = gMainContext;
66 		this.ownedRef = ownedRef;
67 	}
68 
69 	~this ()
70 	{
71 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
72 			g_main_context_unref(gMainContext);
73 	}
74 
75 
76 	/**
77 	 * Creates a new #GMainContext structure.
78 	 *
79 	 * Returns: the new #GMainContext
80 	 *
81 	 * Throws: ConstructionException GTK+ fails to create the object.
82 	 */
83 	public this()
84 	{
85 		auto __p = g_main_context_new();
86 
87 		if(__p is null)
88 		{
89 			throw new ConstructionException("null returned by new");
90 		}
91 
92 		this(cast(GMainContext*) __p);
93 	}
94 
95 	/**
96 	 * Tries to become the owner of the specified context.
97 	 * If some other thread is the owner of the context,
98 	 * returns %FALSE immediately. Ownership is properly
99 	 * recursive: the owner can require ownership again
100 	 * and will release ownership when g_main_context_release()
101 	 * is called as many times as g_main_context_acquire().
102 	 *
103 	 * You must be the owner of a context before you
104 	 * can call g_main_context_prepare(), g_main_context_query(),
105 	 * g_main_context_check(), g_main_context_dispatch().
106 	 *
107 	 * Returns: %TRUE if the operation succeeded, and
108 	 *     this thread is now the owner of @context.
109 	 */
110 	public bool acquire()
111 	{
112 		return g_main_context_acquire(gMainContext) != 0;
113 	}
114 
115 	/**
116 	 * Adds a file descriptor to the set of file descriptors polled for
117 	 * this context. This will very seldom be used directly. Instead
118 	 * a typical event source will use g_source_add_unix_fd() instead.
119 	 *
120 	 * Params:
121 	 *     fd = a #GPollFD structure holding information about a file
122 	 *         descriptor to watch.
123 	 *     priority = the priority for this file descriptor which should be
124 	 *         the same as the priority used for g_source_attach() to ensure that the
125 	 *         file descriptor is polled whenever the results may be needed.
126 	 */
127 	public void addPoll(GPollFD* fd, int priority)
128 	{
129 		g_main_context_add_poll(gMainContext, fd, priority);
130 	}
131 
132 	/**
133 	 * Passes the results of polling back to the main loop. You should be
134 	 * careful to pass @fds and its length @n_fds as received from
135 	 * g_main_context_query(), as this functions relies on assumptions
136 	 * on how @fds is filled.
137 	 *
138 	 * You must have successfully acquired the context with
139 	 * g_main_context_acquire() before you may call this function.
140 	 *
141 	 * Params:
142 	 *     maxPriority = the maximum numerical priority of sources to check
143 	 *     fds = array of #GPollFD's that was passed to
144 	 *         the last call to g_main_context_query()
145 	 *
146 	 * Returns: %TRUE if some sources are ready to be dispatched.
147 	 */
148 	public bool check(int maxPriority, GPollFD[] fds)
149 	{
150 		return g_main_context_check(gMainContext, maxPriority, fds.ptr, cast(int)fds.length) != 0;
151 	}
152 
153 	/**
154 	 * Dispatches all pending sources.
155 	 *
156 	 * You must have successfully acquired the context with
157 	 * g_main_context_acquire() before you may call this function.
158 	 */
159 	public void dispatch()
160 	{
161 		g_main_context_dispatch(gMainContext);
162 	}
163 
164 	/**
165 	 * Finds a source with the given source functions and user data.  If
166 	 * multiple sources exist with the same source function and user data,
167 	 * the first one found will be returned.
168 	 *
169 	 * Params:
170 	 *     funcs = the @source_funcs passed to g_source_new().
171 	 *     userData = the user data from the callback.
172 	 *
173 	 * Returns: the source, if one was found, otherwise %NULL
174 	 */
175 	public Source findSourceByFuncsUserData(GSourceFuncs* funcs, void* userData)
176 	{
177 		auto __p = g_main_context_find_source_by_funcs_user_data(gMainContext, funcs, userData);
178 
179 		if(__p is null)
180 		{
181 			return null;
182 		}
183 
184 		return new Source(cast(GSource*) __p);
185 	}
186 
187 	/**
188 	 * Finds a #GSource given a pair of context and ID.
189 	 *
190 	 * It is a programmer error to attempt to look up a non-existent source.
191 	 *
192 	 * More specifically: source IDs can be reissued after a source has been
193 	 * destroyed and therefore it is never valid to use this function with a
194 	 * source ID which may have already been removed.  An example is when
195 	 * scheduling an idle to run in another thread with g_idle_add(): the
196 	 * idle may already have run and been removed by the time this function
197 	 * is called on its (now invalid) source ID.  This source ID may have
198 	 * been reissued, leading to the operation being performed against the
199 	 * wrong source.
200 	 *
201 	 * Params:
202 	 *     sourceId = the source ID, as returned by g_source_get_id().
203 	 *
204 	 * Returns: the #GSource
205 	 */
206 	public Source findSourceById(uint sourceId)
207 	{
208 		auto __p = g_main_context_find_source_by_id(gMainContext, sourceId);
209 
210 		if(__p is null)
211 		{
212 			return null;
213 		}
214 
215 		return new Source(cast(GSource*) __p);
216 	}
217 
218 	/**
219 	 * Finds a source with the given user data for the callback.  If
220 	 * multiple sources exist with the same user data, the first
221 	 * one found will be returned.
222 	 *
223 	 * Params:
224 	 *     userData = the user_data for the callback.
225 	 *
226 	 * Returns: the source, if one was found, otherwise %NULL
227 	 */
228 	public Source findSourceByUserData(void* userData)
229 	{
230 		auto __p = g_main_context_find_source_by_user_data(gMainContext, userData);
231 
232 		if(__p is null)
233 		{
234 			return null;
235 		}
236 
237 		return new Source(cast(GSource*) __p);
238 	}
239 
240 	/**
241 	 * Gets the poll function set by g_main_context_set_poll_func().
242 	 *
243 	 * Returns: the poll function
244 	 */
245 	public GPollFunc getPollFunc()
246 	{
247 		return g_main_context_get_poll_func(gMainContext);
248 	}
249 
250 	/**
251 	 * Invokes a function in such a way that @context is owned during the
252 	 * invocation of @function.
253 	 *
254 	 * If @context is %NULL then the global default main context — as
255 	 * returned by g_main_context_default() — is used.
256 	 *
257 	 * If @context is owned by the current thread, @function is called
258 	 * directly.  Otherwise, if @context is the thread-default main context
259 	 * of the current thread and g_main_context_acquire() succeeds, then
260 	 * @function is called and g_main_context_release() is called
261 	 * afterwards.
262 	 *
263 	 * In any other case, an idle source is created to call @function and
264 	 * that source is attached to @context (presumably to be run in another
265 	 * thread).  The idle source is attached with #G_PRIORITY_DEFAULT
266 	 * priority.  If you want a different priority, use
267 	 * g_main_context_invoke_full().
268 	 *
269 	 * Note that, as with normal idle functions, @function should probably
270 	 * return %FALSE.  If it returns %TRUE, it will be continuously run in a
271 	 * loop (and may prevent this call from returning).
272 	 *
273 	 * Params:
274 	 *     function_ = function to call
275 	 *     data = data to pass to @function
276 	 *
277 	 * Since: 2.28
278 	 */
279 	public void invoke(GSourceFunc function_, void* data)
280 	{
281 		g_main_context_invoke(gMainContext, function_, data);
282 	}
283 
284 	/**
285 	 * Invokes a function in such a way that @context is owned during the
286 	 * invocation of @function.
287 	 *
288 	 * This function is the same as g_main_context_invoke() except that it
289 	 * lets you specify the priority in case @function ends up being
290 	 * scheduled as an idle and also lets you give a #GDestroyNotify for @data.
291 	 *
292 	 * @notify should not assume that it is called from any particular
293 	 * thread or with any particular context acquired.
294 	 *
295 	 * Params:
296 	 *     priority = the priority at which to run @function
297 	 *     function_ = function to call
298 	 *     data = data to pass to @function
299 	 *     notify = a function to call when @data is no longer in use, or %NULL.
300 	 *
301 	 * Since: 2.28
302 	 */
303 	public void invokeFull(int priority, GSourceFunc function_, void* data, GDestroyNotify notify)
304 	{
305 		g_main_context_invoke_full(gMainContext, priority, function_, data, notify);
306 	}
307 
308 	/**
309 	 * Determines whether this thread holds the (recursive)
310 	 * ownership of this #GMainContext. This is useful to
311 	 * know before waiting on another thread that may be
312 	 * blocking to get ownership of @context.
313 	 *
314 	 * Returns: %TRUE if current thread is owner of @context.
315 	 *
316 	 * Since: 2.10
317 	 */
318 	public bool isOwner()
319 	{
320 		return g_main_context_is_owner(gMainContext) != 0;
321 	}
322 
323 	/**
324 	 * Runs a single iteration for the given main loop. This involves
325 	 * checking to see if any event sources are ready to be processed,
326 	 * then if no events sources are ready and @may_block is %TRUE, waiting
327 	 * for a source to become ready, then dispatching the highest priority
328 	 * events sources that are ready. Otherwise, if @may_block is %FALSE
329 	 * sources are not waited to become ready, only those highest priority
330 	 * events sources will be dispatched (if any), that are ready at this
331 	 * given moment without further waiting.
332 	 *
333 	 * Note that even when @may_block is %TRUE, it is still possible for
334 	 * g_main_context_iteration() to return %FALSE, since the wait may
335 	 * be interrupted for other reasons than an event source becoming ready.
336 	 *
337 	 * Params:
338 	 *     mayBlock = whether the call may block.
339 	 *
340 	 * Returns: %TRUE if events were dispatched.
341 	 */
342 	public bool iteration(bool mayBlock)
343 	{
344 		return g_main_context_iteration(gMainContext, mayBlock) != 0;
345 	}
346 
347 	/**
348 	 * Checks if any sources have pending events for the given context.
349 	 *
350 	 * Returns: %TRUE if events are pending.
351 	 */
352 	public bool pending()
353 	{
354 		return g_main_context_pending(gMainContext) != 0;
355 	}
356 
357 	/**
358 	 * Pops @context off the thread-default context stack (verifying that
359 	 * it was on the top of the stack).
360 	 *
361 	 * Since: 2.22
362 	 */
363 	public void popThreadDefault()
364 	{
365 		g_main_context_pop_thread_default(gMainContext);
366 	}
367 
368 	/**
369 	 * Prepares to poll sources within a main loop. The resulting information
370 	 * for polling is determined by calling g_main_context_query ().
371 	 *
372 	 * You must have successfully acquired the context with
373 	 * g_main_context_acquire() before you may call this function.
374 	 *
375 	 * Params:
376 	 *     priority = location to store priority of highest priority
377 	 *         source already ready.
378 	 *
379 	 * Returns: %TRUE if some source is ready to be dispatched
380 	 *     prior to polling.
381 	 */
382 	public bool prepare(out int priority)
383 	{
384 		return g_main_context_prepare(gMainContext, &priority) != 0;
385 	}
386 
387 	/**
388 	 * Acquires @context and sets it as the thread-default context for the
389 	 * current thread. This will cause certain asynchronous operations
390 	 * (such as most [gio][gio]-based I/O) which are
391 	 * started in this thread to run under @context and deliver their
392 	 * results to its main loop, rather than running under the global
393 	 * default context in the main thread. Note that calling this function
394 	 * changes the context returned by g_main_context_get_thread_default(),
395 	 * not the one returned by g_main_context_default(), so it does not affect
396 	 * the context used by functions like g_idle_add().
397 	 *
398 	 * Normally you would call this function shortly after creating a new
399 	 * thread, passing it a #GMainContext which will be run by a
400 	 * #GMainLoop in that thread, to set a new default context for all
401 	 * async operations in that thread. In this case you may not need to
402 	 * ever call g_main_context_pop_thread_default(), assuming you want the
403 	 * new #GMainContext to be the default for the whole lifecycle of the
404 	 * thread.
405 	 *
406 	 * If you don't have control over how the new thread was created (e.g.
407 	 * in the new thread isn't newly created, or if the thread life
408 	 * cycle is managed by a #GThreadPool), it is always suggested to wrap
409 	 * the logic that needs to use the new #GMainContext inside a
410 	 * g_main_context_push_thread_default() / g_main_context_pop_thread_default()
411 	 * pair, otherwise threads that are re-used will end up never explicitly
412 	 * releasing the #GMainContext reference they hold.
413 	 *
414 	 * In some cases you may want to schedule a single operation in a
415 	 * non-default context, or temporarily use a non-default context in
416 	 * the main thread. In that case, you can wrap the call to the
417 	 * asynchronous operation inside a
418 	 * g_main_context_push_thread_default() /
419 	 * g_main_context_pop_thread_default() pair, but it is up to you to
420 	 * ensure that no other asynchronous operations accidentally get
421 	 * started while the non-default context is active.
422 	 *
423 	 * Beware that libraries that predate this function may not correctly
424 	 * handle being used from a thread with a thread-default context. Eg,
425 	 * see g_file_supports_thread_contexts().
426 	 *
427 	 * Since: 2.22
428 	 */
429 	public void pushThreadDefault()
430 	{
431 		g_main_context_push_thread_default(gMainContext);
432 	}
433 
434 	/**
435 	 * Determines information necessary to poll this main loop. You should
436 	 * be careful to pass the resulting @fds array and its length @n_fds
437 	 * as is when calling g_main_context_check(), as this function relies
438 	 * on assumptions made when the array is filled.
439 	 *
440 	 * You must have successfully acquired the context with
441 	 * g_main_context_acquire() before you may call this function.
442 	 *
443 	 * Params:
444 	 *     maxPriority = maximum priority source to check
445 	 *     timeout = location to store timeout to be used in polling
446 	 *     fds = location to
447 	 *         store #GPollFD records that need to be polled.
448 	 *
449 	 * Returns: the number of records actually stored in @fds,
450 	 *     or, if more than @n_fds records need to be stored, the number
451 	 *     of records that need to be stored.
452 	 */
453 	public int query(int maxPriority, out int timeout, GPollFD[] fds)
454 	{
455 		return g_main_context_query(gMainContext, maxPriority, &timeout, fds.ptr, cast(int)fds.length);
456 	}
457 
458 	alias doref = ref_;
459 	/**
460 	 * Increases the reference count on a #GMainContext object by one.
461 	 *
462 	 * Returns: the @context that was passed in (since 2.6)
463 	 */
464 	public MainContext ref_()
465 	{
466 		auto __p = g_main_context_ref(gMainContext);
467 
468 		if(__p is null)
469 		{
470 			return null;
471 		}
472 
473 		return new MainContext(cast(GMainContext*) __p, true);
474 	}
475 
476 	/**
477 	 * Releases ownership of a context previously acquired by this thread
478 	 * with g_main_context_acquire(). If the context was acquired multiple
479 	 * times, the ownership will be released only when g_main_context_release()
480 	 * is called as many times as it was acquired.
481 	 */
482 	public void release()
483 	{
484 		g_main_context_release(gMainContext);
485 	}
486 
487 	/**
488 	 * Removes file descriptor from the set of file descriptors to be
489 	 * polled for a particular context.
490 	 *
491 	 * Params:
492 	 *     fd = a #GPollFD descriptor previously added with g_main_context_add_poll()
493 	 */
494 	public void removePoll(GPollFD* fd)
495 	{
496 		g_main_context_remove_poll(gMainContext, fd);
497 	}
498 
499 	/**
500 	 * Sets the function to use to handle polling of file descriptors. It
501 	 * will be used instead of the poll() system call
502 	 * (or GLib's replacement function, which is used where
503 	 * poll() isn't available).
504 	 *
505 	 * This function could possibly be used to integrate the GLib event
506 	 * loop with an external event loop.
507 	 *
508 	 * Params:
509 	 *     func = the function to call to poll all file descriptors
510 	 */
511 	public void setPollFunc(GPollFunc func)
512 	{
513 		g_main_context_set_poll_func(gMainContext, func);
514 	}
515 
516 	/**
517 	 * Decreases the reference count on a #GMainContext object by one. If
518 	 * the result is zero, free the context and free all associated memory.
519 	 */
520 	public void unref()
521 	{
522 		g_main_context_unref(gMainContext);
523 	}
524 
525 	/**
526 	 * Tries to become the owner of the specified context,
527 	 * as with g_main_context_acquire(). But if another thread
528 	 * is the owner, atomically drop @mutex and wait on @cond until
529 	 * that owner releases ownership or until @cond is signaled, then
530 	 * try again (once) to become the owner.
531 	 *
532 	 * Deprecated: Use g_main_context_is_owner() and separate locking instead.
533 	 *
534 	 * Params:
535 	 *     cond = a condition variable
536 	 *     mutex = a mutex, currently held
537 	 *
538 	 * Returns: %TRUE if the operation succeeded, and
539 	 *     this thread is now the owner of @context.
540 	 */
541 	public bool wait(Cond cond, Mutex mutex)
542 	{
543 		return g_main_context_wait(gMainContext, (cond is null) ? null : cond.getCondStruct(), (mutex is null) ? null : mutex.getMutexStruct()) != 0;
544 	}
545 
546 	/**
547 	 * If @context is currently blocking in g_main_context_iteration()
548 	 * waiting for a source to become ready, cause it to stop blocking
549 	 * and return.  Otherwise, cause the next invocation of
550 	 * g_main_context_iteration() to return without blocking.
551 	 *
552 	 * This API is useful for low-level control over #GMainContext; for
553 	 * example, integrating it with main loop implementations such as
554 	 * #GMainLoop.
555 	 *
556 	 * Another related use for this function is when implementing a main
557 	 * loop with a termination condition, computed from multiple threads:
558 	 *
559 	 * |[<!-- language="C" -->
560 	 * #define NUM_TASKS 10
561 	 * static gint tasks_remaining = NUM_TASKS;  // (atomic)
562 	 * ...
563 	 *
564 	 * while (g_atomic_int_get (&tasks_remaining) != 0)
565 	 * g_main_context_iteration (NULL, TRUE);
566 	 * ]|
567 	 *
568 	 * Then in a thread:
569 	 * |[<!-- language="C" -->
570 	 * perform_work();
571 	 *
572 	 * if (g_atomic_int_dec_and_test (&tasks_remaining))
573 	 * g_main_context_wakeup (NULL);
574 	 * ]|
575 	 */
576 	public void wakeup()
577 	{
578 		g_main_context_wakeup(gMainContext);
579 	}
580 
581 	/**
582 	 * Returns the global default main context. This is the main context
583 	 * used for main loop functions when a main loop is not explicitly
584 	 * specified, and corresponds to the "main" main loop. See also
585 	 * g_main_context_get_thread_default().
586 	 *
587 	 * Returns: the global default main context.
588 	 */
589 	public static MainContext default_()
590 	{
591 		auto __p = g_main_context_default();
592 
593 		if(__p is null)
594 		{
595 			return null;
596 		}
597 
598 		return new MainContext(cast(GMainContext*) __p);
599 	}
600 
601 	/**
602 	 * Gets the thread-default #GMainContext for this thread. Asynchronous
603 	 * operations that want to be able to be run in contexts other than
604 	 * the default one should call this method or
605 	 * g_main_context_ref_thread_default() to get a #GMainContext to add
606 	 * their #GSources to. (Note that even in single-threaded
607 	 * programs applications may sometimes want to temporarily push a
608 	 * non-default context, so it is not safe to assume that this will
609 	 * always return %NULL if you are running in the default thread.)
610 	 *
611 	 * If you need to hold a reference on the context, use
612 	 * g_main_context_ref_thread_default() instead.
613 	 *
614 	 * Returns: the thread-default #GMainContext, or
615 	 *     %NULL if the thread-default context is the global default context.
616 	 *
617 	 * Since: 2.22
618 	 */
619 	public static MainContext getThreadDefault()
620 	{
621 		auto __p = g_main_context_get_thread_default();
622 
623 		if(__p is null)
624 		{
625 			return null;
626 		}
627 
628 		return new MainContext(cast(GMainContext*) __p);
629 	}
630 
631 	/**
632 	 * Gets the thread-default #GMainContext for this thread, as with
633 	 * g_main_context_get_thread_default(), but also adds a reference to
634 	 * it with g_main_context_ref(). In addition, unlike
635 	 * g_main_context_get_thread_default(), if the thread-default context
636 	 * is the global default context, this will return that #GMainContext
637 	 * (with a ref added to it) rather than returning %NULL.
638 	 *
639 	 * Returns: the thread-default #GMainContext. Unref
640 	 *     with g_main_context_unref() when you are done with it.
641 	 *
642 	 * Since: 2.32
643 	 */
644 	public static MainContext refThreadDefault()
645 	{
646 		auto __p = g_main_context_ref_thread_default();
647 
648 		if(__p is null)
649 		{
650 			return null;
651 		}
652 
653 		return new MainContext(cast(GMainContext*) __p, true);
654 	}
655 }