The Main Event Loop manages all available sources of events. The main event loop manages all the available sources of events for GLib and GTK+ applications. These events can come from any number of different types of sources such as file descriptors (plain files, pipes or sockets) and timeouts. New types of event sources can also be added using g_source_attach(). To allow multiple independent sets of sources to be handled in different threads, each source is associated with a #GMainContext. A #GMainContext can only be running in a single thread, but sources can be added to it and removed from it from other threads. Each event source is assigned a priority. The default priority, #G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. Values greater than 0 denote lower priorities. Events from high priority sources are always processed before events from lower priority sources. Idle functions can also be added, and assigned a priority. These will be run whenever no events with a higher priority are ready to be processed. The #GMainLoop data type represents a main event loop. A #GMainLoop is created with g_main_loop_new(). After adding the initial event sources, g_main_loop_run() is called. This continuously checks for new events from each of the event sources and dispatches them. Finally, the processing of an event from one of the sources leads to a call to g_main_loop_quit() to exit the main loop, and g_main_loop_run() returns. It is possible to create new instances of #GMainLoop recursively. This is often used in GTK+ applications when showing modal dialog boxes. Note that event sources are associated with a particular #GMainContext, and will be checked and dispatched for all main loops associated with that #GMainContext. GTK+ contains wrappers of some of these functions, e.g. gtk_main(), gtk_main_quit() and gtk_events_pending(). Creating new sources types One of the unusual features of the GTK+ main loop functionality is that new types of event source can be created and used in addition to the builtin type of event source. A new event source type is used for handling GDK events. A new source type is created by deriving from the #GSource structure. The derived type of source is represented by a structure that has the #GSource structure as a first element, and other elements specific to the new source type. To create an instance of the new source type, call g_source_new() passing in the size of the derived structure and a table of functions. These #GSourceFuncs determine the behavior of the new source types. New source types basically interact with with the main context in two ways. Their prepare function in #GSourceFuncs can set a timeout to determine the maximum amount of time that the main loop will sleep before checking the source again. In addition, or as well, the source can add file descriptors to the set that the main context checks using g_source_add_poll(). Customizing the main loop iteration Single iterations of a #GMainContext can be run with g_main_context_iteration(). In some cases, more detailed control of exactly how the details of the main loop work is desired, for instance, when integrating the #GMainLoop with an external main loop. In such cases, you can call the component functions of g_main_context_iteration() directly. These functions are g_main_context_prepare(), g_main_context_query(), g_main_context_check() and g_main_context_dispatch(). The operation of these functions can best be seen in terms of a state diagram, as shown in .
States of a Main Context
The GMainLoop struct is an opaque data type representing the main event loop of a GLib or GTK+ application. @context: @is_running: @Returns: @loop: @Returns: @loop: @loop: @loop: @loop: @Returns: @loop: @Returns: Creates a new #GMainLoop for the default main loop. @is_running: set to %TRUE to indicate that the loop is running. This is not very important since calling g_main_run() will set this to %TRUE anyway. @Returns: a new #GMainLoop. @Deprecated: Use g_main_loop_new() instead. Frees the memory allocated for the #GMainLoop. @loop: a #GMainLoop. @Deprecated: Use g_main_loop_unref() instead. Runs a main loop until it stops running. @loop: a #GMainLoop. @Deprecated: Use g_main_loop_run() instead. Stops the #GMainLoop. If g_main_run() was called to run the #GMainLoop, it will now return. @loop: a #GMainLoop. @Deprecated: Use g_main_loop_quit() instead. Checks if the main loop is running. @loop: a #GMainLoop. @Returns: %TRUE if the main loop is running. @Deprecated: USe g_main_loop_is_running() instead. Use this for high priority event sources. It is not used within GLib or GTK+. Use this for default priority event sources. In GLib this priority is used when adding timeout functions with g_timeout_add(). In GDK this priority is used for events from the X server. Use this for high priority idle functions. GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations, and #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is done to ensure that any pending resizes are processed before any pending redraws, so that widgets are not redrawn twice unnecessarily.) Use this for default priority idle functions. In GLib this priority is used when adding idle functions with g_idle_add(). Use this for very low priority background tasks. It is not used within GLib or GTK+. The GMainContext struct is an opaque data type representing a set of sources to be handled in a main loop. @Returns: @context: @context: @Returns: @context: @may_block: @Returns: Runs a single iteration for the default #GMainContext. @may_block: set to %TRUE if it should block (i.e. wait) until an event source becomes ready. It will return after an event source has been processed. If set to %FALSE it will return immediately if no event source is ready to be processed. @Returns: %TRUE if more events are pending. @Deprecated: Use g_main_context_iteration() instead. @context: @Returns: Checks if any events are pending for the default #GMainContext (i.e. ready to be processed). @Returns: %TRUE if any events are pending. @Deprecated: Use g_main_context_pending() instead. @context: @source_id: @Returns: @id: @context: @user_data: @Returns: @context: @funcs: @user_data: @Returns: @context: @context: @Returns: @context: @context: @cond: @mutex: @Returns: @context: @priority: @Returns: @context: @max_priority: @timeout_: @fds: @n_fds: @Returns: @context: @max_priority: @fds: @n_fds: @Returns: @context: @context: @func: @context: @Returns: Specifies the type of function passed to g_main_context_set_poll_func(). The semantics of the function should match those of the poll() system call. @ufds: an array of #GPollFD elements. @nfsd: the number of elements in @ufds. @timeout_: the maximum time to wait for an event of the file descriptors. A negative value indicates an infinite timeout. @Returns: the number of #GPollFD elements which have events or errors reported, or -1 if an error occurred. @context: @fd: @priority: @context: @fd: @Returns: Sets the function to use for the handle polling of file descriptors for the default main context. @func: the function to call to poll all file descriptors. @Deprecated: Use g_main_context_set_poll_func() instead. @interval: @Returns: @interval: @function: @data: @Returns: @priority: @interval: @function: @data: @notify: @Returns: @Returns: @function: @data: @Returns: @priority: @function: @data: @notify: @Returns: @data: @Returns: @pid: @status: @data: @pid: @Returns: @pid: @function: @data: @Returns: @priority: @pid: @function: @data: @notify: @Returns: #gint fd; the file descriptor to poll (or a HANDLE on Win32 platforms). #gushort events; a bitwise combination of flags from #GIOCondition, specifying which events should be polled for. Typically for reading from a file descriptor you would use %G_IO_IN | %G_IO_HUP | %G_IO_ERR, and for writing you would use %G_IO_OUT | %G_IO_ERR. #gushort revents; a bitwise combination of flags from #GIOCondition, returned from the poll() function to indicate which events occurred. @fd: @events: @revents: The GSource struct is an opaque data type representing an event source. This is just a placeholder for #GClosureMarshal, which cannot be used here for dependency reasons. The #GSourceFuncs struct contains a table of functions used to handle event sources in a generic manner. prepare Called before all the file descriptors are polled. If the source can determine that it is ready here (without waiting for the results of the poll() call) it should return %TRUE. It can also return a @timeout_ value which should be the maximum timeout (in milliseconds) which should be passed to the poll() call. The actual timeout used will be -1 if all sources returned -1, or it will be the minimum of all the @timeout_ values returned which were >= 0. check Called after all the file descriptors are polled. The source should return %TRUE if it is ready to be dispatched. Note that some time may have passed since the previous prepare function was called, so the source should be checked again here. dispatch Called to dispatch the event source, after it has returned %TRUE in either its @prepare or its @check function. The @dispatch function is passed in a callback function and data. The callback function may be %NULL if the source was never connected to a callback using g_source_set_callback(). The @dispatch function should call the callback function with @user_data and whatever additional parameters are needed for this type of event source. finalize Called when the source is finalized. For idle sources, the prepare and check functions always return %TRUE to indicate that the source is always ready to be processed. The prepare function also returns a timeout value of 0 to ensure that the poll() call doesn't block (since that would be time wasted which could have been spent running the idle function). For timeout sources, the prepare and check functions both return %TRUE if the timeout interval has expired. The prepare function also returns a timeout value to ensure that the poll() call doesn't block too long and miss the next timeout. For file descriptor sources, the prepare function typically returns %FALSE, since it must wait until poll() has been called before it knows whether any events need to be processed. It sets the returned timeout to -1 to indicate that it doesn't mind how long the poll() call blocks. In the check function, it tests the results of the poll() call to see if the required condition has been met, and returns %TRUE if so. @prepare: @check: @dispatch: @finalize: @closure_callback: @closure_marshal: The GSourceCallbackFuncs struct contains functions for managing callback objects. @ref: Called when a reference is added to the callback object. @unref: Called when a reference to the callback object is dropped. @get: Called to extract the callback function and data from the callback object. @source_funcs: @struct_size: @Returns: @source: @Returns: @source: @source: @context: @Returns: @source: @source: @priority: @source: @Returns: @source: @can_recurse: @source: @Returns: @source: @Returns: @source: @Returns: @source: @func: @data: @notify: Specifies the type of function passed to g_timeout_add(), g_timeout_add_full(), g_idle_add(), and g_idle_add_full(). @data: data passed to the function, set when the source was created with one of the above functions. @Returns: it should return %FALSE if the source should be removed. @source: @callback_data: @callback_funcs: @source: @fd: @source: @fd: @source: @timeval: @tag: @Returns: @funcs: @user_data: @Returns: @user_data: @Returns: