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, see <http://www.gnu.org/licenses/>.
21 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 #error "Only <glib.h> can be included directly."
25 #include <glib/gpoll.h>
26 #include <glib/gslist.h>
27 #include <glib/gthread.h>
31 typedef enum /*< flags >*/
33 G_IO_IN GLIB_SYSDEF_POLLIN
,
34 G_IO_OUT GLIB_SYSDEF_POLLOUT
,
35 G_IO_PRI GLIB_SYSDEF_POLLPRI
,
36 G_IO_ERR GLIB_SYSDEF_POLLERR
,
37 G_IO_HUP GLIB_SYSDEF_POLLHUP
,
38 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
45 * The `GMainContext` struct is an opaque data
46 * type representing a set of sources to be handled in a main loop.
48 typedef struct _GMainContext GMainContext
;
53 * The `GMainLoop` struct is an opaque data type
54 * representing the main event loop of a GLib or GTK+ application.
56 typedef struct _GMainLoop GMainLoop
;
61 * The `GSource` struct is an opaque data type
62 * representing an event source.
64 typedef struct _GSource GSource
;
65 typedef struct _GSourcePrivate GSourcePrivate
;
68 * GSourceCallbackFuncs:
69 * @ref: Called when a reference is added to the callback object
70 * @unref: Called when a reference to the callback object is dropped
71 * @get: Called to extract the callback function and data from the
74 * The `GSourceCallbackFuncs` struct contains
75 * functions for managing callback objects.
77 typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs
;
81 * @prepare: Called before all the file descriptors are polled. If the
82 * source can determine that it is ready here (without waiting for the
83 * results of the poll() call) it should return %TRUE. It can also return
84 * a @timeout_ value which should be the maximum timeout (in milliseconds)
85 * which should be passed to the poll() call. The actual timeout used will
86 * be -1 if all sources returned -1, or it will be the minimum of all
87 * the @timeout_ values returned which were >= 0. Since 2.36 this may
88 * be %NULL, in which case the effect is as if the function always returns
89 * %FALSE with a timeout of -1. If @prepare returns a
90 * timeout and the source also has a 'ready time' set then the
91 * nearer of the two will be used.
92 * @check: Called after all the file descriptors are polled. The source
93 * should return %TRUE if it is ready to be dispatched. Note that some
94 * time may have passed since the previous prepare function was called,
95 * so the source should be checked again here. Since 2.36 this may
96 * be %NULL, in which case the effect is as if the function always returns
98 * @dispatch: Called to dispatch the event source, after it has returned
99 * %TRUE in either its @prepare or its @check function. The @dispatch
100 * function is passed in a callback function and data. The callback
101 * function may be %NULL if the source was never connected to a callback
102 * using g_source_set_callback(). The @dispatch function should call the
103 * callback function with @user_data and whatever additional parameters
104 * are needed for this type of event source. The return value of the
105 * @dispatch function should be #G_SOURCE_REMOVE if the source should be
106 * removed or #G_SOURCE_CONTINUE to keep it.
107 * @finalize: Called when the source is finalized.
109 * The `GSourceFuncs` struct contains a table of
110 * functions used to handle event sources in a generic manner.
112 * For idle sources, the prepare and check functions always return %TRUE
113 * to indicate that the source is always ready to be processed. The prepare
114 * function also returns a timeout value of 0 to ensure that the poll() call
115 * doesn't block (since that would be time wasted which could have been spent
116 * running the idle function).
118 * For timeout sources, the prepare and check functions both return %TRUE
119 * if the timeout interval has expired. The prepare function also returns
120 * a timeout value to ensure that the poll() call doesn't block too long
121 * and miss the next timeout.
123 * For file descriptor sources, the prepare function typically returns %FALSE,
124 * since it must wait until poll() has been called before it knows whether
125 * any events need to be processed. It sets the returned timeout to -1 to
126 * indicate that it doesn't mind how long the poll() call blocks. In the
127 * check function, it tests the results of the poll() call to see if the
128 * required condition has been met, and returns %TRUE if so.
130 typedef struct _GSourceFuncs GSourceFuncs
;
135 * A type which is used to hold a process identification.
137 * On UNIX, processes are identified by a process id (an integer),
138 * while Windows uses process handles (which are pointers).
140 * GPid is used in GLib only for descendant processes spawned with
141 * the g_spawn functions.
143 /* defined in glibconfig.h */
148 * A format specifier that can be used in printf()-style format strings
149 * when printing a #GPid.
153 /* defined in glibconfig.h */
157 * @user_data: data passed to the function, set when the source was
158 * created with one of the above functions
160 * Specifies the type of function passed to g_timeout_add(),
161 * g_timeout_add_full(), g_idle_add(), and g_idle_add_full().
163 * Returns: %FALSE if the source should be removed. #G_SOURCE_CONTINUE and
164 * #G_SOURCE_REMOVE are more memorable names for the return value.
166 typedef gboolean (*GSourceFunc
) (gpointer user_data
);
170 * @pid: the process id of the child process
171 * @status: Status information about the child process, encoded
172 * in a platform-specific manner
173 * @user_data: user data passed to g_child_watch_add()
175 * Prototype of a #GChildWatchSource callback, called when a child
176 * process has exited. To interpret @status, see the documentation
177 * for g_spawn_check_exit_status().
179 typedef void (*GChildWatchFunc
) (GPid pid
,
185 gpointer callback_data
;
186 GSourceCallbackFuncs
*callback_funcs
;
188 const GSourceFuncs
*source_funcs
;
191 GMainContext
*context
;
204 GSourcePrivate
*priv
;
207 struct _GSourceCallbackFuncs
209 void (*ref
) (gpointer cb_data
);
210 void (*unref
) (gpointer cb_data
);
211 void (*get
) (gpointer cb_data
,
218 * GSourceDummyMarshal:
220 * This is just a placeholder for #GClosureMarshal,
221 * which cannot be used here for dependency reasons.
223 typedef void (*GSourceDummyMarshal
) (void);
227 gboolean (*prepare
) (GSource
*source
,
229 gboolean (*check
) (GSource
*source
);
230 gboolean (*dispatch
) (GSource
*source
,
231 GSourceFunc callback
,
233 void (*finalize
) (GSource
*source
); /* Can be NULL */
236 /* For use by g_source_set_closure */
237 GSourceFunc closure_callback
;
238 GSourceDummyMarshal closure_marshal
; /* Really is of type GClosureMarshal */
241 /* Standard priorities */
246 * Use this for high priority event sources.
248 * It is not used within GLib or GTK+.
250 #define G_PRIORITY_HIGH -100
253 * G_PRIORITY_DEFAULT:
255 * Use this for default priority event sources.
257 * In GLib this priority is used when adding timeout functions
258 * with g_timeout_add(). In GDK this priority is used for events
261 #define G_PRIORITY_DEFAULT 0
264 * G_PRIORITY_HIGH_IDLE:
266 * Use this for high priority idle functions.
268 * GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
269 * and #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
270 * done to ensure that any pending resizes are processed before any
271 * pending redraws, so that widgets are not redrawn twice unnecessarily.)
273 #define G_PRIORITY_HIGH_IDLE 100
276 * G_PRIORITY_DEFAULT_IDLE:
278 * Use this for default priority idle functions.
280 * In GLib this priority is used when adding idle functions with
283 #define G_PRIORITY_DEFAULT_IDLE 200
288 * Use this for very low priority background tasks.
290 * It is not used within GLib or GTK+.
292 #define G_PRIORITY_LOW 300
297 * Use this macro as the return value of a #GSourceFunc to remove
298 * the #GSource from the main loop.
302 #define G_SOURCE_REMOVE FALSE
307 * Use this macro as the return value of a #GSourceFunc to leave
308 * the #GSource in the main loop.
312 #define G_SOURCE_CONTINUE TRUE
316 GLIB_AVAILABLE_IN_ALL
317 GMainContext
*g_main_context_new (void);
318 GLIB_AVAILABLE_IN_ALL
319 GMainContext
*g_main_context_ref (GMainContext
*context
);
320 GLIB_AVAILABLE_IN_ALL
321 void g_main_context_unref (GMainContext
*context
);
322 GLIB_AVAILABLE_IN_ALL
323 GMainContext
*g_main_context_default (void);
325 GLIB_AVAILABLE_IN_ALL
326 gboolean
g_main_context_iteration (GMainContext
*context
,
328 GLIB_AVAILABLE_IN_ALL
329 gboolean
g_main_context_pending (GMainContext
*context
);
331 /* For implementation of legacy interfaces
333 GLIB_AVAILABLE_IN_ALL
334 GSource
*g_main_context_find_source_by_id (GMainContext
*context
,
336 GLIB_AVAILABLE_IN_ALL
337 GSource
*g_main_context_find_source_by_user_data (GMainContext
*context
,
339 GLIB_AVAILABLE_IN_ALL
340 GSource
*g_main_context_find_source_by_funcs_user_data (GMainContext
*context
,
344 /* Low level functions for implementing custom main loops.
346 GLIB_AVAILABLE_IN_ALL
347 void g_main_context_wakeup (GMainContext
*context
);
348 GLIB_AVAILABLE_IN_ALL
349 gboolean
g_main_context_acquire (GMainContext
*context
);
350 GLIB_AVAILABLE_IN_ALL
351 void g_main_context_release (GMainContext
*context
);
352 GLIB_AVAILABLE_IN_ALL
353 gboolean
g_main_context_is_owner (GMainContext
*context
);
354 GLIB_AVAILABLE_IN_ALL
355 gboolean
g_main_context_wait (GMainContext
*context
,
359 GLIB_AVAILABLE_IN_ALL
360 gboolean
g_main_context_prepare (GMainContext
*context
,
362 GLIB_AVAILABLE_IN_ALL
363 gint
g_main_context_query (GMainContext
*context
,
368 GLIB_AVAILABLE_IN_ALL
369 gboolean
g_main_context_check (GMainContext
*context
,
373 GLIB_AVAILABLE_IN_ALL
374 void g_main_context_dispatch (GMainContext
*context
);
376 GLIB_AVAILABLE_IN_ALL
377 void g_main_context_set_poll_func (GMainContext
*context
,
379 GLIB_AVAILABLE_IN_ALL
380 GPollFunc
g_main_context_get_poll_func (GMainContext
*context
);
382 /* Low level functions for use by source implementations
384 GLIB_AVAILABLE_IN_ALL
385 void g_main_context_add_poll (GMainContext
*context
,
388 GLIB_AVAILABLE_IN_ALL
389 void g_main_context_remove_poll (GMainContext
*context
,
392 GLIB_AVAILABLE_IN_ALL
393 gint
g_main_depth (void);
394 GLIB_AVAILABLE_IN_ALL
395 GSource
*g_main_current_source (void);
397 /* GMainContexts for other threads
399 GLIB_AVAILABLE_IN_ALL
400 void g_main_context_push_thread_default (GMainContext
*context
);
401 GLIB_AVAILABLE_IN_ALL
402 void g_main_context_pop_thread_default (GMainContext
*context
);
403 GLIB_AVAILABLE_IN_ALL
404 GMainContext
*g_main_context_get_thread_default (void);
405 GLIB_AVAILABLE_IN_ALL
406 GMainContext
*g_main_context_ref_thread_default (void);
410 GLIB_AVAILABLE_IN_ALL
411 GMainLoop
*g_main_loop_new (GMainContext
*context
,
412 gboolean is_running
);
413 GLIB_AVAILABLE_IN_ALL
414 void g_main_loop_run (GMainLoop
*loop
);
415 GLIB_AVAILABLE_IN_ALL
416 void g_main_loop_quit (GMainLoop
*loop
);
417 GLIB_AVAILABLE_IN_ALL
418 GMainLoop
*g_main_loop_ref (GMainLoop
*loop
);
419 GLIB_AVAILABLE_IN_ALL
420 void g_main_loop_unref (GMainLoop
*loop
);
421 GLIB_AVAILABLE_IN_ALL
422 gboolean
g_main_loop_is_running (GMainLoop
*loop
);
423 GLIB_AVAILABLE_IN_ALL
424 GMainContext
*g_main_loop_get_context (GMainLoop
*loop
);
428 GLIB_AVAILABLE_IN_ALL
429 GSource
*g_source_new (GSourceFuncs
*source_funcs
,
431 GLIB_AVAILABLE_IN_ALL
432 GSource
*g_source_ref (GSource
*source
);
433 GLIB_AVAILABLE_IN_ALL
434 void g_source_unref (GSource
*source
);
436 GLIB_AVAILABLE_IN_ALL
437 guint
g_source_attach (GSource
*source
,
438 GMainContext
*context
);
439 GLIB_AVAILABLE_IN_ALL
440 void g_source_destroy (GSource
*source
);
442 GLIB_AVAILABLE_IN_ALL
443 void g_source_set_priority (GSource
*source
,
445 GLIB_AVAILABLE_IN_ALL
446 gint
g_source_get_priority (GSource
*source
);
447 GLIB_AVAILABLE_IN_ALL
448 void g_source_set_can_recurse (GSource
*source
,
449 gboolean can_recurse
);
450 GLIB_AVAILABLE_IN_ALL
451 gboolean
g_source_get_can_recurse (GSource
*source
);
452 GLIB_AVAILABLE_IN_ALL
453 guint
g_source_get_id (GSource
*source
);
455 GLIB_AVAILABLE_IN_ALL
456 GMainContext
*g_source_get_context (GSource
*source
);
458 GLIB_AVAILABLE_IN_ALL
459 void g_source_set_callback (GSource
*source
,
462 GDestroyNotify notify
);
464 GLIB_AVAILABLE_IN_ALL
465 void g_source_set_funcs (GSource
*source
,
466 GSourceFuncs
*funcs
);
467 GLIB_AVAILABLE_IN_ALL
468 gboolean
g_source_is_destroyed (GSource
*source
);
470 GLIB_AVAILABLE_IN_ALL
471 void g_source_set_name (GSource
*source
,
473 GLIB_AVAILABLE_IN_ALL
474 const char * g_source_get_name (GSource
*source
);
475 GLIB_AVAILABLE_IN_ALL
476 void g_source_set_name_by_id (guint tag
,
479 GLIB_AVAILABLE_IN_2_36
480 void g_source_set_ready_time (GSource
*source
,
482 GLIB_AVAILABLE_IN_2_36
483 gint64
g_source_get_ready_time (GSource
*source
);
486 GLIB_AVAILABLE_IN_2_36
487 gpointer
g_source_add_unix_fd (GSource
*source
,
489 GIOCondition events
);
490 GLIB_AVAILABLE_IN_2_36
491 void g_source_modify_unix_fd (GSource
*source
,
493 GIOCondition new_events
);
494 GLIB_AVAILABLE_IN_2_36
495 void g_source_remove_unix_fd (GSource
*source
,
497 GLIB_AVAILABLE_IN_2_36
498 GIOCondition
g_source_query_unix_fd (GSource
*source
,
502 /* Used to implement g_source_connect_closure and internally*/
503 GLIB_AVAILABLE_IN_ALL
504 void g_source_set_callback_indirect (GSource
*source
,
505 gpointer callback_data
,
506 GSourceCallbackFuncs
*callback_funcs
);
508 GLIB_AVAILABLE_IN_ALL
509 void g_source_add_poll (GSource
*source
,
511 GLIB_AVAILABLE_IN_ALL
512 void g_source_remove_poll (GSource
*source
,
515 GLIB_AVAILABLE_IN_ALL
516 void g_source_add_child_source (GSource
*source
,
517 GSource
*child_source
);
518 GLIB_AVAILABLE_IN_ALL
519 void g_source_remove_child_source (GSource
*source
,
520 GSource
*child_source
);
522 GLIB_DEPRECATED_IN_2_28_FOR(g_source_get_time
)
523 void g_source_get_current_time (GSource
*source
,
526 GLIB_AVAILABLE_IN_ALL
527 gint64
g_source_get_time (GSource
*source
);
529 /* void g_source_connect_closure (GSource *source,
533 /* Specific source types
535 GLIB_AVAILABLE_IN_ALL
536 GSource
*g_idle_source_new (void);
537 GLIB_AVAILABLE_IN_ALL
538 GSource
*g_child_watch_source_new (GPid pid
);
539 GLIB_AVAILABLE_IN_ALL
540 GSource
*g_timeout_source_new (guint interval
);
541 GLIB_AVAILABLE_IN_ALL
542 GSource
*g_timeout_source_new_seconds (guint interval
);
544 /* Miscellaneous functions
546 GLIB_AVAILABLE_IN_ALL
547 void g_get_current_time (GTimeVal
*result
);
548 GLIB_AVAILABLE_IN_ALL
549 gint64
g_get_monotonic_time (void);
550 GLIB_AVAILABLE_IN_ALL
551 gint64
g_get_real_time (void);
554 /* Source manipulation by ID */
555 GLIB_AVAILABLE_IN_ALL
556 gboolean
g_source_remove (guint tag
);
557 GLIB_AVAILABLE_IN_ALL
558 gboolean
g_source_remove_by_user_data (gpointer user_data
);
559 GLIB_AVAILABLE_IN_ALL
560 gboolean
g_source_remove_by_funcs_user_data (GSourceFuncs
*funcs
,
563 /* Idles, child watchers and timeouts */
564 GLIB_AVAILABLE_IN_ALL
565 guint
g_timeout_add_full (gint priority
,
567 GSourceFunc function
,
569 GDestroyNotify notify
);
570 GLIB_AVAILABLE_IN_ALL
571 guint
g_timeout_add (guint interval
,
572 GSourceFunc function
,
574 GLIB_AVAILABLE_IN_ALL
575 guint
g_timeout_add_seconds_full (gint priority
,
577 GSourceFunc function
,
579 GDestroyNotify notify
);
580 GLIB_AVAILABLE_IN_ALL
581 guint
g_timeout_add_seconds (guint interval
,
582 GSourceFunc function
,
584 GLIB_AVAILABLE_IN_ALL
585 guint
g_child_watch_add_full (gint priority
,
587 GChildWatchFunc function
,
589 GDestroyNotify notify
);
590 GLIB_AVAILABLE_IN_ALL
591 guint
g_child_watch_add (GPid pid
,
592 GChildWatchFunc function
,
594 GLIB_AVAILABLE_IN_ALL
595 guint
g_idle_add (GSourceFunc function
,
597 GLIB_AVAILABLE_IN_ALL
598 guint
g_idle_add_full (gint priority
,
599 GSourceFunc function
,
601 GDestroyNotify notify
);
602 GLIB_AVAILABLE_IN_ALL
603 gboolean
g_idle_remove_by_data (gpointer data
);
605 GLIB_AVAILABLE_IN_ALL
606 void g_main_context_invoke_full (GMainContext
*context
,
608 GSourceFunc function
,
610 GDestroyNotify notify
);
611 GLIB_AVAILABLE_IN_ALL
612 void g_main_context_invoke (GMainContext
*context
,
613 GSourceFunc function
,
616 /* Hook for GClosure / GSource integration. Don't touch */
617 GLIB_VAR GSourceFuncs g_timeout_funcs
;
618 GLIB_VAR GSourceFuncs g_child_watch_funcs
;
619 GLIB_VAR GSourceFuncs g_idle_funcs
;
621 GLIB_VAR GSourceFuncs g_unix_signal_funcs
;
622 GLIB_VAR GSourceFuncs g_unix_fd_source_funcs
;
627 #endif /* __G_MAIN_H__ */