1 /* gmain.h - the GLib Main loop
2 * Copyright (C) 1998-2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
21 #error "Only <glib.h> can be included directly."
27 #include <glib/gpoll.h>
28 #include <glib/gslist.h>
29 #include <glib/gthread.h>
36 * The <structname>GMainContext</structname> struct is an opaque data
37 * type representing a set of sources to be handled in a main loop.
39 typedef struct _GMainContext GMainContext;
44 * The <structname>GMainLoop</structname> struct is an opaque data type
45 * representing the main event loop of a GLib or GTK+ application.
47 typedef struct _GMainLoop GMainLoop;
52 * The <structname>GSource</structname> struct is an opaque data type
53 * representing an event source.
55 typedef struct _GSource GSource;
56 typedef struct _GSourcePrivate GSourcePrivate;
59 * GSourceCallbackFuncs:
60 * @ref: Called when a reference is added to the callback object
61 * @unref: Called when a reference to the callback object is dropped
62 * @get: Called to extract the callback function and data from the
65 * The <structname>GSourceCallbackFuncs</structname> struct contains
66 * functions for managing callback objects.
68 typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs;
72 * @prepare: Called before all the file descriptors are polled. If the
73 * source can determine that it is ready here (without waiting for the
74 * results of the poll() call) it should return %TRUE. It can also return
75 * a @timeout_ value which should be the maximum timeout (in milliseconds)
76 * which should be passed to the poll() call. The actual timeout used will
77 * be -1 if all sources returned -1, or it will be the minimum of all the
78 * @timeout_ values returned which were >= 0.
79 * @check: Called after all the file descriptors are polled. The source
80 * should return %TRUE if it is ready to be dispatched. Note that some
81 * time may have passed since the previous prepare function was called,
82 * so the source should be checked again here.
83 * @dispatch: Called to dispatch the event source, after it has returned
84 * %TRUE in either its @prepare or its @check function. The @dispatch
85 * function is passed in a callback function and data. The callback
86 * function may be %NULL if the source was never connected to a callback
87 * using g_source_set_callback(). The @dispatch function should call the
88 * callback function with @user_data and whatever additional parameters
89 * are needed for this type of event source.
90 * @finalize: Called when the source is finalized.
92 * The <structname>GSourceFuncs</structname> struct contains a table of
93 * functions used to handle event sources in a generic manner.
95 * For idle sources, the prepare and check functions always return %TRUE
96 * to indicate that the source is always ready to be processed. The prepare
97 * function also returns a timeout value of 0 to ensure that the poll() call
98 * doesn't block (since that would be time wasted which could have been spent
99 * running the idle function).
101 * For timeout sources, the prepare and check functions both return %TRUE
102 * if the timeout interval has expired. The prepare function also returns
103 * a timeout value to ensure that the poll() call doesn't block too long
104 * and miss the next timeout.
106 * For file descriptor sources, the prepare function typically returns %FALSE,
107 * since it must wait until poll() has been called before it knows whether
108 * any events need to be processed. It sets the returned timeout to -1 to
109 * indicate that it doesn't mind how long the poll() call blocks. In the
110 * check function, it tests the results of the poll() call to see if the
111 * required condition has been met, and returns %TRUE if so.
113 typedef struct _GSourceFuncs GSourceFuncs;
118 * A type which is used to hold a process identification.
120 * On UNIX, processes are identified by a process id (an integer),
121 * while Windows uses process handles (which are pointers).
123 * GPid is used in GLib only for descendant processes spawned with
124 * the g_spawn functions.
129 * @user_data: data passed to the function, set when the source was
130 * created with one of the above functions
132 * Specifies the type of function passed to g_timeout_add(),
133 * g_timeout_add_full(), g_idle_add(), and g_idle_add_full().
135 * Returns: %FALSE if the source should be removed
137 typedef gboolean (*GSourceFunc) (gpointer user_data);
141 * @pid: the process id of the child process
142 * @status: Status information about the child process,
143 * see waitpid(2) for more information about this field
144 * @user_data: user data passed to g_child_watch_add()
146 * The type of functions to be called when a child exists.
148 typedef void (*GChildWatchFunc) (GPid pid,
154 gpointer callback_data;
155 GSourceCallbackFuncs *callback_funcs;
157 GSourceFuncs *source_funcs;
160 GMainContext *context;
173 GSourcePrivate *priv;
176 struct _GSourceCallbackFuncs
178 void (*ref) (gpointer cb_data);
179 void (*unref) (gpointer cb_data);
180 void (*get) (gpointer cb_data,
187 * GSourceDummyMarshal:
189 * This is just a placeholder for #GClosureMarshal,
190 * which cannot be used here for dependency reasons.
192 typedef void (*GSourceDummyMarshal) (void);
196 gboolean (*prepare) (GSource *source,
198 gboolean (*check) (GSource *source);
199 gboolean (*dispatch) (GSource *source,
200 GSourceFunc callback,
202 void (*finalize) (GSource *source); /* Can be NULL */
205 /* For use by g_source_set_closure */
206 GSourceFunc closure_callback;
207 GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
210 /* Standard priorities */
215 * Use this for high priority event sources.
217 * It is not used within GLib or GTK+.
219 #define G_PRIORITY_HIGH -100
222 * G_PRIORITY_DEFAULT:
224 * Use this for default priority event sources.
226 * In GLib this priority is used when adding timeout functions
227 * with g_timeout_add(). In GDK this priority is used for events
230 #define G_PRIORITY_DEFAULT 0
233 * G_PRIORITY_HIGH_IDLE:
235 * Use this for high priority idle functions.
237 * GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
238 * and #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
239 * done to ensure that any pending resizes are processed before any
240 * pending redraws, so that widgets are not redrawn twice unnecessarily.)
242 #define G_PRIORITY_HIGH_IDLE 100
245 * G_PRIORITY_DEFAULT_IDLE:
247 * Use this for default priority idle functions.
249 * In GLib this priority is used when adding idle functions with
252 #define G_PRIORITY_DEFAULT_IDLE 200
257 * Use this for very low priority background tasks.
259 * It is not used within GLib or GTK+.
261 #define G_PRIORITY_LOW 300
266 * Use this macro as the return value of a #GSourceFunc to remove
267 * the #GSource from the main loop.
271 #define G_SOURCE_REMOVE FALSE
276 * Use this macro as the return value of a #GSourceFunc to leave
277 * the #GSource in the main loop.
281 #define G_SOURCE_CONTINUE TRUE
285 GMainContext *g_main_context_new (void);
286 GMainContext *g_main_context_ref (GMainContext *context);
287 void g_main_context_unref (GMainContext *context);
288 GMainContext *g_main_context_default (void);
290 gboolean g_main_context_iteration (GMainContext *context,
292 gboolean g_main_context_pending (GMainContext *context);
294 /* For implementation of legacy interfaces
296 GSource *g_main_context_find_source_by_id (GMainContext *context,
298 GSource *g_main_context_find_source_by_user_data (GMainContext *context,
300 GSource *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
304 /* Low level functions for implementing custom main loops.
306 void g_main_context_wakeup (GMainContext *context);
307 gboolean g_main_context_acquire (GMainContext *context);
308 void g_main_context_release (GMainContext *context);
309 gboolean g_main_context_is_owner (GMainContext *context);
310 gboolean g_main_context_wait (GMainContext *context,
314 gboolean g_main_context_prepare (GMainContext *context,
316 gint g_main_context_query (GMainContext *context,
321 gint g_main_context_check (GMainContext *context,
325 void g_main_context_dispatch (GMainContext *context);
327 void g_main_context_set_poll_func (GMainContext *context,
329 GPollFunc g_main_context_get_poll_func (GMainContext *context);
331 /* Low level functions for use by source implementations
333 void g_main_context_add_poll (GMainContext *context,
336 void g_main_context_remove_poll (GMainContext *context,
339 gint g_main_depth (void);
340 GSource *g_main_current_source (void);
342 /* GMainContexts for other threads
344 void g_main_context_push_thread_default (GMainContext *context);
345 void g_main_context_pop_thread_default (GMainContext *context);
346 GMainContext *g_main_context_get_thread_default (void);
347 GMainContext *g_main_context_ref_thread_default (void);
351 GMainLoop *g_main_loop_new (GMainContext *context,
352 gboolean is_running);
353 void g_main_loop_run (GMainLoop *loop);
354 void g_main_loop_quit (GMainLoop *loop);
355 GMainLoop *g_main_loop_ref (GMainLoop *loop);
356 void g_main_loop_unref (GMainLoop *loop);
357 gboolean g_main_loop_is_running (GMainLoop *loop);
358 GMainContext *g_main_loop_get_context (GMainLoop *loop);
362 GSource *g_source_new (GSourceFuncs *source_funcs,
364 GSource *g_source_ref (GSource *source);
365 void g_source_unref (GSource *source);
367 guint g_source_attach (GSource *source,
368 GMainContext *context);
369 void g_source_destroy (GSource *source);
371 void g_source_set_priority (GSource *source,
373 gint g_source_get_priority (GSource *source);
374 void g_source_set_can_recurse (GSource *source,
375 gboolean can_recurse);
376 gboolean g_source_get_can_recurse (GSource *source);
377 guint g_source_get_id (GSource *source);
379 GMainContext *g_source_get_context (GSource *source);
381 void g_source_set_callback (GSource *source,
384 GDestroyNotify notify);
386 void g_source_set_funcs (GSource *source,
387 GSourceFuncs *funcs);
388 gboolean g_source_is_destroyed (GSource *source);
390 void g_source_set_name (GSource *source,
392 const char * g_source_get_name (GSource *source);
393 void g_source_set_name_by_id (guint tag,
397 /* Used to implement g_source_connect_closure and internally*/
398 void g_source_set_callback_indirect (GSource *source,
399 gpointer callback_data,
400 GSourceCallbackFuncs *callback_funcs);
402 void g_source_add_poll (GSource *source,
404 void g_source_remove_poll (GSource *source,
407 void g_source_add_child_source (GSource *source,
408 GSource *child_source);
409 void g_source_remove_child_source (GSource *source,
410 GSource *child_source);
412 GLIB_DEPRECATED_IN_2_28_FOR(g_source_get_time)
413 void g_source_get_current_time (GSource *source,
416 gint64 g_source_get_time (GSource *source);
418 /* void g_source_connect_closure (GSource *source,
422 /* Specific source types
424 GSource *g_idle_source_new (void);
425 GSource *g_child_watch_source_new (GPid pid);
426 GSource *g_timeout_source_new (guint interval);
427 GSource *g_timeout_source_new_seconds (guint interval);
429 /* Miscellaneous functions
431 void g_get_current_time (GTimeVal *result);
432 gint64 g_get_monotonic_time (void);
433 gint64 g_get_real_time (void);
436 /* Source manipulation by ID */
437 gboolean g_source_remove (guint tag);
438 gboolean g_source_remove_by_user_data (gpointer user_data);
439 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
442 /* Idles, child watchers and timeouts */
443 guint g_timeout_add_full (gint priority,
445 GSourceFunc function,
447 GDestroyNotify notify);
448 guint g_timeout_add (guint interval,
449 GSourceFunc function,
451 guint g_timeout_add_seconds_full (gint priority,
453 GSourceFunc function,
455 GDestroyNotify notify);
456 guint g_timeout_add_seconds (guint interval,
457 GSourceFunc function,
459 guint g_child_watch_add_full (gint priority,
461 GChildWatchFunc function,
463 GDestroyNotify notify);
464 guint g_child_watch_add (GPid pid,
465 GChildWatchFunc function,
467 guint g_idle_add (GSourceFunc function,
469 guint g_idle_add_full (gint priority,
470 GSourceFunc function,
472 GDestroyNotify notify);
473 gboolean g_idle_remove_by_data (gpointer data);
475 void g_main_context_invoke_full (GMainContext *context,
477 GSourceFunc function,
479 GDestroyNotify notify);
480 void g_main_context_invoke (GMainContext *context,
481 GSourceFunc function,
484 /* Hook for GClosure / GSource integration. Don't touch */
485 GLIB_VAR GSourceFuncs g_timeout_funcs;
486 GLIB_VAR GSourceFuncs g_child_watch_funcs;
487 GLIB_VAR GSourceFuncs g_idle_funcs;
491 #endif /* __G_MAIN_H__ */