gmain.h File Reference

__G_MAIN_H__

Typedef GMainContext

typedef typedefG_BEGIN_DECLS struct _GMainContextGMainContext

Typedef GMainLoop

typedef struct _GMainLoopGMainLoop

Typedef GSource

typedef struct _GSourceGSource

Typedef GSourceCallbackFuncs

typedef struct _GSourceCallbackFuncsGSourceCallbackFuncs

Typedef GSourceFuncs

typedef struct _GSourceFuncsGSourceFuncs

Typedef GSourceFunc

typedef gboolean(*GSourceFunc

Typedef GChildWatchFunc

typedef void(*GChildWatchFunc

Typedef GSourceDummyMarshal

typedef void(*GSourceDummyMarshal

G_PRIORITY_HIGH

G_PRIORITY_DEFAULT

G_PRIORITY_HIGH_IDLE

G_PRIORITY_DEFAULT_IDLE

G_PRIORITY_LOW

g_main_context_new ( void )

IMPORT_C GMainContext *g_main_context_new(void)

g_main_context_new:

Creates a new GMainContext structure.

Return value: the new GMainContext

g_main_context_ref ( GMainContext * )

IMPORT_C GMainContext *g_main_context_ref(GMainContext *context)

g_main_context_ref: : a GMainContext

Increases the reference count on a GMainContext object by one.

Returns: the that was passed in (since 2.6)

g_main_context_unref ( GMainContext * )

IMPORT_C voidg_main_context_unref(GMainContext *context)

g_main_context_unref: : a GMainContext

Decreases the reference count on a GMainContext object by one. If the result is zero, free the context and free all associated memory.

g_main_context_default ( void )

IMPORT_C GMainContext *g_main_context_default(void)

g_main_context_default:

Returns the default main context. This is the main context used for main loop functions when a main loop is not explicitly specified.

Return value: the default main context.

g_main_context_iteration ( GMainContext *, gboolean )

IMPORT_C gbooleang_main_context_iteration(GMainContext *context,
gbooleanmay_block
)

g_main_context_pending ( GMainContext * )

IMPORT_C gbooleang_main_context_pending(GMainContext *context)

g_main_context_find_source_by_id ( GMainContext *, guint )

IMPORT_C GSource *g_main_context_find_source_by_id(GMainContext *context,
guintsource_id
)

g_main_context_find_source_by_id: : a GMainContext (if NULL, the default context will be used) : the source ID, as returned by g_source_get_id().

Finds a GSource given a pair of context and ID.

Return value: the GSource if found, otherwise, NULL

g_main_context_find_source_by_user_data ( GMainContext *, gpointer )

IMPORT_C GSource *g_main_context_find_source_by_user_data(GMainContext *context,
gpointeruser_data
)

g_main_context_find_source_by_user_data: : a GMainContext : the user_data for the callback.

Finds a source with the given user data for the callback. If multiple sources exist with the same user data, the first one found will be returned.

Return value: the source, if one was found, otherwise NULL

g_main_context_find_source_by_funcs_user_data ( GMainContext *, GSourceFuncs *, gpointer )

IMPORT_C GSource *g_main_context_find_source_by_funcs_user_data(GMainContext *context,
GSourceFuncs *funcs,
gpointeruser_data
)

g_main_context_find_source_by_funcs_user_data: : a GMainContext (if NULL, the default context will be used). : the passed to g_source_new(). : the user data from the callback.

Finds a source with the given source functions and user data. If multiple sources exist with the same source function and user data, the first one found will be returned.

Return value: the source, if one was found, otherwise NULL

g_main_context_wakeup ( GMainContext * )

IMPORT_C voidg_main_context_wakeup(GMainContext *context)

g_main_context_acquire ( GMainContext * )

IMPORT_C gbooleang_main_context_acquire(GMainContext *context)

g_main_context_acquire: : a GMainContext

Tries to become the owner of the specified context. If some other thread is the owner of the context, returns FALSE immediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when g_main_context_release() is called as many times as g_main_context_acquire().

You must be the owner of a context before you can call g_main_context_prepare(), g_main_context_query(), g_main_context_check(), g_main_context_dispatch().

Return value: TRUE if the operation succeeded, and this thread is now the owner of .

g_main_context_release ( GMainContext * )

IMPORT_C voidg_main_context_release(GMainContext *context)

g_main_context_release: : a GMainContext

Releases ownership of a context previously acquired by this thread with g_main_context_acquire(). If the context was acquired multiple times, the ownership will be released only when g_main_context_release() is called as many times as it was acquired.

g_main_context_is_owner ( GMainContext * )

IMPORT_C gbooleang_main_context_is_owner(GMainContext *context)

g_main_context_wait ( GMainContext *, GCond *, GMutex * )

IMPORT_C gbooleang_main_context_wait(GMainContext *context,
GCond *cond,
GMutex *mutex
)

g_main_context_prepare ( GMainContext *, gint * )

IMPORT_C gbooleang_main_context_prepare(GMainContext *context,
gint *priority
)

g_main_context_query ( GMainContext *, gint, gint *, GPollFD *, gint )

IMPORT_C gintg_main_context_query(GMainContext *context,
gintmax_priority,
gint *timeout_,
GPollFD *fds,
gintn_fds
)

g_main_context_check ( GMainContext *, gint, GPollFD *, gint )

IMPORT_C gintg_main_context_check(GMainContext *context,
gintmax_priority,
GPollFD *fds,
gintn_fds
)

g_main_context_dispatch ( GMainContext * )

IMPORT_C voidg_main_context_dispatch(GMainContext *context)

g_main_context_set_poll_func ( GMainContext *, GPollFunc )

IMPORT_C voidg_main_context_set_poll_func(GMainContext *context,
GPollFuncfunc
)

g_main_context_get_poll_func ( GMainContext * )

IMPORT_C GPollFuncg_main_context_get_poll_func(GMainContext *context)

g_main_context_add_poll ( GMainContext *, GPollFD *, gint )

IMPORT_C voidg_main_context_add_poll(GMainContext *context,
GPollFD *fd,
gintpriority
)

g_main_context_remove_poll ( GMainContext *, GPollFD * )

IMPORT_C voidg_main_context_remove_poll(GMainContext *context,
GPollFD *fd
)

g_main_depth ( void )

IMPORT_C gintg_main_depth(void)

g_main_depth:

Returns the depth of the stack of calls to g_main_context_dispatch() on any GMainContext in the current thread. That is, when called from the toplevel, it gives 0. When called from within a callback from g_main_context_iteration() (or g_main_loop_run(), etc.) it returns 1. When called from within a callback to a recursive call to g_main_context_iterate(), it returns 2. And so forth.

This function is useful in a situation like the following: Imagine an extremely simple "garbage collected" system.

|[ static GList *free_list;

gpointer allocate_memory (gsize size) { gpointer result = g_malloc (size); free_list = g_list_prepend (free_list, result); return result; }

void free_allocated_memory (void) { GList *l; for (l = free_list; l; l = l->next); g_free (l->data); g_list_free (free_list); free_list = NULL; }

[...]

while (TRUE); { g_main_context_iteration (NULL, TRUE); free_allocated_memory(); } ]|

This works from an application, however, if you want to do the same thing from a library, it gets more difficult, since you no longer control the main loop. You might think you can simply use an idle function to make the call to free_allocated_memory(), but that doesn't work, since the idle function could be called from a recursive callback. This can be fixed by using g_main_depth()

|[ gpointer allocate_memory (gsize size) { FreeListBlock *block = g_new (FreeListBlock, 1); block->mem = g_malloc (size); block->depth = g_main_depth (); free_list = g_list_prepend (free_list, block); return block->mem; }

void free_allocated_memory (void) { GList *l;

int depth = g_main_depth (); for (l = free_list; l; ); { GList *next = l->next; FreeListBlock *block = l->data; if (block->depth > depth) { g_free (block->mem); g_free (block); free_list = g_list_delete_link (free_list, l); }

l = next; } } ]|

There is a temptation to use g_main_depth() to solve problems with reentrancy. For instance, while waiting for data to be received from the network in response to a menu item, the menu item might be selected again. It might seem that one could make the menu item's callback return immediately and do nothing if g_main_depth() returns a value greater than 1. However, this should be avoided since the user then sees selecting the menu item do nothing. Furthermore, you'll find yourself adding these checks all over your code, since there are doubtless many, many things that the user could do. Instead, you can use the following techniques:

<orderedlist> <listitem>

Use gtk_widget_set_sensitive() or modal dialogs to prevent the user from interacting with elements while the main loop is recursing.

</listitem> <listitem>

Avoid main loop recursion in situations where you can't handle arbitrary callbacks. Instead, structure your code so that you simply return to the main loop and then get called again when there is more work to do.

</listitem> </orderedlist>

Return value: The main loop recursion level in the current thread

g_main_current_source ( void )

IMPORT_C GSource *g_main_current_source(void)

g_main_current_source:

Returns the currently firing source for this thread.

Return value: The currently firing source or NULL.

Since: 2.12

g_main_loop_new ( GMainContext *, gboolean )

IMPORT_C GMainLoop *g_main_loop_new(GMainContext *context,
gbooleanis_running
)

g_main_loop_run ( GMainLoop * )

IMPORT_C voidg_main_loop_run(GMainLoop *loop)

g_main_loop_quit ( GMainLoop * )

IMPORT_C voidg_main_loop_quit(GMainLoop *loop)

g_main_loop_ref ( GMainLoop * )

IMPORT_C GMainLoop *g_main_loop_ref(GMainLoop *loop)

g_main_loop_unref ( GMainLoop * )

IMPORT_C voidg_main_loop_unref(GMainLoop *loop)

g_main_loop_is_running ( GMainLoop * )

IMPORT_C gbooleang_main_loop_is_running(GMainLoop *loop)

g_main_loop_get_context ( GMainLoop * )

IMPORT_C GMainContext *g_main_loop_get_context(GMainLoop *loop)

g_source_new ( GSourceFuncs *, guint )

IMPORT_C GSource *g_source_new(GSourceFuncs *source_funcs,
guintstruct_size
)

g_source_new: : structure containing functions that implement the sources behavior. : size of the GSource structure to create.

Creates a new GSource structure. The size is specified to allow creating structures derived from GSource that contain additional data. The size passed in must be at least <literal>sizeof (GSource)</literal>.

The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.

Return value: the newly-created GSource.

g_source_ref ( GSource * )

IMPORT_C GSource *g_source_ref(GSource *source)

g_source_ref: : a GSource

Increases the reference count on a source by one.

Return value:

g_source_unref ( GSource * )

IMPORT_C voidg_source_unref(GSource *source)

g_source_unref: : a GSource

Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed.

g_source_attach ( GSource *, GMainContext * )

IMPORT_C guintg_source_attach(GSource *source,
GMainContext *context
)

g_source_attach: : a GSource : a GMainContext (if NULL, the default context will be used)

Adds a GSource to a so that it will be executed within that context. Remove it by calling g_source_destroy().

Return value: the ID (greater than 0) for the source within the GMainContext.

g_source_destroy ( GSource * )

IMPORT_C voidg_source_destroy(GSource *source)

g_source_destroy: : a GSource

Removes a source from its GMainContext, if any, and mark it as destroyed. The source cannot be subsequently added to another context.

g_source_set_priority ( GSource *, gint )

IMPORT_C voidg_source_set_priority(GSource *source,
gintpriority
)

g_source_set_priority: : a GSource : the new priority.

Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.

g_source_get_priority ( GSource * )

IMPORT_C gintg_source_get_priority(GSource *source)

g_source_get_priority: : a GSource

Gets the priority of a source.

Return value: the priority of the source

g_source_set_can_recurse ( GSource *, gboolean )

IMPORT_C voidg_source_set_can_recurse(GSource *source,
gbooleancan_recurse
)

g_source_set_can_recurse: : a GSource : whether recursion is allowed for this source

Sets whether a source can be called recursively. If is TRUE, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.

g_source_get_can_recurse ( GSource * )

IMPORT_C gbooleang_source_get_can_recurse(GSource *source)

g_source_get_can_recurse: : a GSource

Checks whether a source is allowed to be called recursively. see g_source_set_can_recurse().

Return value: whether recursion is allowed.

g_source_get_id ( GSource * )

IMPORT_C guintg_source_get_id(GSource *source)

g_source_get_id: : a GSource

Returns the numeric ID for a particular source. The ID of a source is a positive integer which is unique within a particular main loop context. The reverse mapping from ID to source is done by g_main_context_find_source_by_id().

Return value: the ID (greater than 0) for the source

g_source_get_context ( GSource * )

IMPORT_C GMainContext *g_source_get_context(GSource *source)

g_source_get_context: : a GSource

Gets the GMainContext with which the source is associated. Calling this function on a destroyed source is an error.

Return value: the GMainContext with which the source is associated, or NULL if the context has not yet been added to a source.

g_source_set_callback ( GSource *, GSourceFunc, gpointer, GDestroyNotify )

IMPORT_C voidg_source_set_callback(GSource *source,
GSourceFuncfunc,
gpointerdata,
GDestroyNotifynotify
)

g_source_set_callback: : the source : a callback function : the data to pass to callback function : a function to call when is no longer in use, or NULL.

Sets the callback function for a source. The callback for a source is called from the source's dispatch function.

The exact type of depends on the type of source; ie. you should not count on being called with as its first parameter.

Typically, you won't use this function. Instead use functions specific to the type of source you are using.

g_source_set_funcs ( GSource *, GSourceFuncs * )

IMPORT_C voidg_source_set_funcs(GSource *source,
GSourceFuncs *funcs
)

g_source_set_funcs: : a GSource : the new GSourceFuncs

Sets the source functions (can be used to override default implementations) of an unattached source.

Since: 2.12

g_source_is_destroyed ( GSource * )

IMPORT_C gbooleang_source_is_destroyed(GSource *source)

g_source_is_destroyed: : a GSource

Returns whether has been destroyed.

This is important when you operate upon your objects from within idle handlers, but may have freed the object before the dispatch of your idle handler.

|[ static gboolean idle_callback (gpointer data) { SomeWidget *self = data;

GDK_THREADS_ENTER (); /* do stuff with self */ self->idle_id = 0; GDK_THREADS_LEAVE ();

return FALSE; }

static void some_widget_do_stuff_later (SomeWidget *self) { self->idle_id = g_idle_add (idle_callback, self); }

static void some_widget_finalize (GObject *object) { SomeWidget *self = SOME_WIDGET (object);

if (self->idle_id) g_source_remove (self->idle_id);

G_OBJECT_CLASS (parent_class)->finalize (object); } ]|

This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires due to the use after free in the callback. A solution, to this particular problem, is to check to if the source has already been destroy within the callback.

|[ static gboolean idle_callback (gpointer data) { SomeWidget *self = data;

GDK_THREADS_ENTER (); if (!g_source_is_destroyed (g_main_current_source ())) { /* do stuff with self */ } GDK_THREADS_LEAVE ();

return FALSE; } ]|

Return value: TRUE if the source has been destroyed

Since: 2.12

g_source_set_callback_indirect ( GSource *, gpointer, GSourceCallbackFuncs * )

IMPORT_C voidg_source_set_callback_indirect(GSource *source,
gpointercallback_data,
GSourceCallbackFuncs *callback_funcs
)

g_source_set_callback_indirect: : the source : pointer to callback data "object" : functions for reference counting and getting the callback and data

Sets the callback function storing the data as a refcounted callback "object". This is used internally. Note that calling g_source_set_callback_indirect() assumes an initial reference count on , and thus ->unref will eventually be called once more than ->ref.

g_source_add_poll ( GSource *, GPollFD * )

IMPORT_C voidg_source_add_poll(GSource *source,
GPollFD *fd
)

g_source_add_poll: :a GSource : a GPollFD structure holding information about a file descriptor to watch.

Adds a file descriptor to the set of file descriptors polled for this source. This is usually combined with g_source_new() to add an event source. The event source's check function will typically test the field in the GPollFD struct and return TRUE if events need to be processed.

g_source_remove_poll ( GSource *, GPollFD * )

IMPORT_C voidg_source_remove_poll(GSource *source,
GPollFD *fd
)

g_source_remove_poll: :a GSource : a GPollFD structure previously passed to g_source_add_poll().

Removes a file descriptor from the set of file descriptors polled for this source.

g_source_get_current_time ( GSource *, GTimeVal * )

IMPORT_C voidg_source_get_current_time(GSource *source,
GTimeVal *timeval
)

g_idle_source_new ( void )

IMPORT_C GSource *g_idle_source_new(void)

g_child_watch_source_new ( GPid )

IMPORT_C GSource *g_child_watch_source_new(GPidpid)

g_timeout_source_new ( guint )

IMPORT_C GSource *g_timeout_source_new(guintinterval)

g_timeout_source_new_seconds ( guint )

IMPORT_C GSource *g_timeout_source_new_seconds(guintinterval)

g_get_current_time ( GTimeVal * )

IMPORT_C voidg_get_current_time(GTimeVal *result)

g_get_current_time: Equivalent to the UNIX gettimeofday() function, but portable.

Return Value
: GTimeVal structure in which to store current time.

g_main_new

g_main_run

g_main_quit

g_main_destroy

g_main_is_running

g_main_iteration

g_main_pending

g_main_set_poll_func

g_source_remove ( guint )

IMPORT_C gbooleang_source_remove(guinttag)

g_source_remove: : the ID of the source to remove.

Removes the source with the given id from the default main context. The id of a GSource is given by g_source_get_id(), or will be returned by the functions g_source_attach(), g_idle_add(), g_idle_add_full(), g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().

See also g_source_destroy(). You must use g_source_destroy() for sources added to a non-default main context.

Return value: TRUE if the source was found and removed.

g_source_remove_by_user_data ( gpointer )

IMPORT_C gbooleang_source_remove_by_user_data(gpointeruser_data)

g_source_remove_by_user_data: : the user_data for the callback.

Removes a source from the default main loop context given the user data for the callback. If multiple sources exist with the same user data, only one will be destroyed.

Return value: TRUE if a source was found and removed.

g_source_remove_by_funcs_user_data ( GSourceFuncs *, gpointer )

IMPORT_C gbooleang_source_remove_by_funcs_user_data(GSourceFuncs *funcs,
gpointeruser_data
)

g_source_remove_by_funcs_user_data: : The passed to g_source_new() : the user data for the callback

Removes a source from the default main loop context given the source functions and user data. If multiple sources exist with the same source functions and user data, only one will be destroyed.

Return value: TRUE if a source was found and removed.

g_timeout_add_full ( gint, guint, GSourceFunc, gpointer, GDestroyNotify )

IMPORT_C guintg_timeout_add_full(gintpriority,
guintinterval,
GSourceFuncfunction,
gpointerdata,
GDestroyNotifynotify
)

g_timeout_add ( guint, GSourceFunc, gpointer )

IMPORT_C guintg_timeout_add(guintinterval,
GSourceFuncfunction,
gpointerdata
)

g_timeout_add_seconds_full ( gint, guint, GSourceFunc, gpointer, GDestroyNotify )

IMPORT_C guintg_timeout_add_seconds_full(gintpriority,
guintinterval,
GSourceFuncfunction,
gpointerdata,
GDestroyNotifynotify
)

g_timeout_add_seconds ( guint, GSourceFunc, gpointer )

IMPORT_C guintg_timeout_add_seconds(guintinterval,
GSourceFuncfunction,
gpointerdata
)

g_child_watch_add_full ( gint, GPid, GChildWatchFunc, gpointer, GDestroyNotify )

IMPORT_C guintg_child_watch_add_full(gintpriority,
GPidpid,
GChildWatchFuncfunction,
gpointerdata,
GDestroyNotifynotify
)

g_child_watch_add ( GPid, GChildWatchFunc, gpointer )

IMPORT_C guintg_child_watch_add(GPidpid,
GChildWatchFuncfunction,
gpointerdata
)

g_idle_add ( GSourceFunc, gpointer )

IMPORT_C guintg_idle_add(GSourceFuncfunction,
gpointerdata
)

g_idle_add_full ( gint, GSourceFunc, gpointer, GDestroyNotify )

IMPORT_C guintg_idle_add_full(gintpriority,
GSourceFuncfunction,
gpointerdata,
GDestroyNotifynotify
)

g_idle_remove_by_data ( gpointer )

IMPORT_C gbooleang_idle_remove_by_data(gpointerdata)

_g_timeout_funcs ( void )

IMPORT_C GSourceFuncs *_g_timeout_funcs(void)

g_timeout_funcs

GLIB_VAR GSourceFuncsg_timeout_funcs

_g_child_watch_funcs ( void )

IMPORT_C GSourceFuncs *_g_child_watch_funcs(void)

g_child_watch_funcs

GLIB_VAR GSourceFuncsg_child_watch_funcs

_g_idle_funcs ( void )

IMPORT_C GSourceFuncs *_g_idle_funcs(void)

g_idle_funcs

GLIB_VAR GSourceFuncsg_idle_funcs