mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-01 21:33:09 +02:00
Move main loop docs inline
This commit is contained in:
parent
ab8cb652c8
commit
358b9d6ac7
@ -2,99 +2,12 @@
|
|||||||
The Main Event Loop
|
The Main Event Loop
|
||||||
|
|
||||||
<!-- ##### SECTION Short_Description ##### -->
|
<!-- ##### SECTION Short_Description ##### -->
|
||||||
manages all available sources of events
|
|
||||||
|
|
||||||
<!-- ##### SECTION Long_Description ##### -->
|
<!-- ##### SECTION Long_Description ##### -->
|
||||||
<para>
|
<para>
|
||||||
The main event loop manages all the available sources of events for
|
|
||||||
GLib and GTK+ applications. These events can come from any number of
|
</para>
|
||||||
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().
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
|
|
||||||
gtk_main_quit() and gtk_events_pending().
|
|
||||||
</para>
|
|
||||||
<refsect2>
|
|
||||||
<title>Creating new sources types</title>
|
|
||||||
<para>
|
|
||||||
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 <firstterm>deriving</firstterm> 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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
New source types basically interact 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().
|
|
||||||
</para>
|
|
||||||
</refsect2>
|
|
||||||
<refsect2>
|
|
||||||
<title>Customizing the main loop iteration</title>
|
|
||||||
<para>
|
|
||||||
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().
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
The operation of these functions can best be seen in terms
|
|
||||||
of a state diagram, as shown in <xref linkend="mainloop-states"/>.
|
|
||||||
</para>
|
|
||||||
<figure id="mainloop-states">
|
|
||||||
<title>States of a Main Context</title>
|
|
||||||
<graphic fileref="mainloop-states.gif" format="GIF"></graphic>
|
|
||||||
</figure>
|
|
||||||
</refsect2>
|
|
||||||
|
|
||||||
<!-- ##### SECTION See_Also ##### -->
|
<!-- ##### SECTION See_Also ##### -->
|
||||||
<para>
|
<para>
|
||||||
@ -109,8 +22,7 @@ manages all available sources of events
|
|||||||
|
|
||||||
<!-- ##### STRUCT GMainLoop ##### -->
|
<!-- ##### STRUCT GMainLoop ##### -->
|
||||||
<para>
|
<para>
|
||||||
The <structname>GMainLoop</structname> struct is an opaque data type
|
|
||||||
representing the main event loop of a GLib or GTK+ application.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
@ -177,102 +89,82 @@ representing the main event loop of a GLib or GTK+ application.
|
|||||||
|
|
||||||
<!-- ##### MACRO g_main_new ##### -->
|
<!-- ##### MACRO g_main_new ##### -->
|
||||||
<para>
|
<para>
|
||||||
Creates a new #GMainLoop for the default main loop.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@is_running: set to %TRUE to indicate that the loop is running. This is not
|
@is_running:
|
||||||
very important since calling g_main_run() will set this to %TRUE anyway.
|
|
||||||
@Returns: a new #GMainLoop.
|
|
||||||
@Deprecated: 2.2: Use g_main_loop_new() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO g_main_destroy ##### -->
|
<!-- ##### MACRO g_main_destroy ##### -->
|
||||||
<para>
|
<para>
|
||||||
Frees the memory allocated for the #GMainLoop.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@loop: a #GMainLoop.
|
@loop:
|
||||||
@Deprecated: 2.2: Use g_main_loop_unref() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO g_main_run ##### -->
|
<!-- ##### MACRO g_main_run ##### -->
|
||||||
<para>
|
<para>
|
||||||
Runs a main loop until it stops running.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@loop: a #GMainLoop.
|
@loop:
|
||||||
@Deprecated: 2.2: Use g_main_loop_run() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO g_main_quit ##### -->
|
<!-- ##### MACRO g_main_quit ##### -->
|
||||||
<para>
|
<para>
|
||||||
Stops the #GMainLoop. If g_main_run() was called to run the #GMainLoop,
|
|
||||||
it will now return.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@loop: a #GMainLoop.
|
@loop:
|
||||||
@Deprecated: 2.2: Use g_main_loop_quit() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO g_main_is_running ##### -->
|
<!-- ##### MACRO g_main_is_running ##### -->
|
||||||
<para>
|
<para>
|
||||||
Checks if the main loop is running.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@loop: a #GMainLoop.
|
@loop:
|
||||||
@Returns: %TRUE if the main loop is running.
|
|
||||||
@Deprecated: 2.2: USe g_main_loop_is_running() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO G_PRIORITY_HIGH ##### -->
|
<!-- ##### MACRO G_PRIORITY_HIGH ##### -->
|
||||||
<para>
|
<para>
|
||||||
Use this for high priority event sources.
|
|
||||||
It is not used within GLib or GTK+.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO G_PRIORITY_DEFAULT ##### -->
|
<!-- ##### MACRO G_PRIORITY_DEFAULT ##### -->
|
||||||
<para>
|
<para>
|
||||||
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.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO G_PRIORITY_HIGH_IDLE ##### -->
|
<!-- ##### MACRO G_PRIORITY_HIGH_IDLE ##### -->
|
||||||
<para>
|
<para>
|
||||||
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.)
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO G_PRIORITY_DEFAULT_IDLE ##### -->
|
<!-- ##### MACRO G_PRIORITY_DEFAULT_IDLE ##### -->
|
||||||
<para>
|
<para>
|
||||||
Use this for default priority idle functions.
|
|
||||||
In GLib this priority is used when adding idle functions with g_idle_add().
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### MACRO G_PRIORITY_LOW ##### -->
|
<!-- ##### MACRO G_PRIORITY_LOW ##### -->
|
||||||
<para>
|
<para>
|
||||||
Use this for very low priority background tasks.
|
|
||||||
It is not used within GLib or GTK+.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### STRUCT GMainContext ##### -->
|
<!-- ##### STRUCT GMainContext ##### -->
|
||||||
<para>
|
<para>
|
||||||
The <structname>GMainContext</structname> struct is an opaque data type
|
|
||||||
representing a set of sources to be handled in a main loop.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
@ -323,15 +215,10 @@ representing a set of sources to be handled in a main loop.
|
|||||||
|
|
||||||
<!-- ##### MACRO g_main_iteration ##### -->
|
<!-- ##### MACRO g_main_iteration ##### -->
|
||||||
<para>
|
<para>
|
||||||
Runs a single iteration for the default #GMainContext.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@may_block: set to %TRUE if it should block (i.e. wait) until an event source
|
@may_block:
|
||||||
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: 2.2: Use g_main_context_iteration() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_main_context_pending ##### -->
|
<!-- ##### FUNCTION g_main_context_pending ##### -->
|
||||||
@ -345,12 +232,9 @@ processed.
|
|||||||
|
|
||||||
<!-- ##### MACRO g_main_pending ##### -->
|
<!-- ##### MACRO g_main_pending ##### -->
|
||||||
<para>
|
<para>
|
||||||
Checks if any events are pending for the default #GMainContext
|
|
||||||
(i.e. ready to be processed).
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@Returns: %TRUE if any events are pending.
|
|
||||||
@Deprecated: 2.2: Use g_main_context_pending() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_main_context_find_source_by_id ##### -->
|
<!-- ##### FUNCTION g_main_context_find_source_by_id ##### -->
|
||||||
@ -492,17 +376,13 @@ Checks if any events are pending for the default #GMainContext
|
|||||||
|
|
||||||
<!-- ##### USER_FUNCTION GPollFunc ##### -->
|
<!-- ##### USER_FUNCTION GPollFunc ##### -->
|
||||||
<para>
|
<para>
|
||||||
Specifies the type of function passed to g_main_context_set_poll_func().
|
|
||||||
The semantics of the function should match those of the
|
|
||||||
<function>poll()</function> system call.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@ufds: an array of #GPollFD elements.
|
@ufds:
|
||||||
@nfsd: the number of elements in @ufds.
|
@nfsd:
|
||||||
@timeout_: the maximum time to wait for an event of the file descriptors.
|
@timeout_:
|
||||||
A negative value indicates an infinite timeout.
|
@Returns:
|
||||||
@Returns: the number of #GPollFD elements which have events or errors reported,
|
|
||||||
or -1 if an error occurred.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_main_context_add_poll ##### -->
|
<!-- ##### FUNCTION g_main_context_add_poll ##### -->
|
||||||
@ -544,12 +424,10 @@ or -1 if an error occurred.
|
|||||||
|
|
||||||
<!-- ##### MACRO g_main_set_poll_func ##### -->
|
<!-- ##### MACRO g_main_set_poll_func ##### -->
|
||||||
<para>
|
<para>
|
||||||
Sets the function to use for the handle polling of file descriptors
|
|
||||||
for the default main context.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@func: the function to call to poll all file descriptors.
|
@func:
|
||||||
@Deprecated: 2.2: Use g_main_context_set_poll_func() instead.
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_main_context_get_thread_default ##### -->
|
<!-- ##### FUNCTION g_main_context_get_thread_default ##### -->
|
||||||
@ -680,22 +558,18 @@ for the default main context.
|
|||||||
|
|
||||||
<!-- ##### TYPEDEF GPid ##### -->
|
<!-- ##### TYPEDEF GPid ##### -->
|
||||||
<para>
|
<para>
|
||||||
A type which is used to hold a process identification.
|
|
||||||
On Unix, processes are identified by a process id (an
|
|
||||||
integer), while Windows uses process handles (which are
|
|
||||||
pointers).
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### USER_FUNCTION GChildWatchFunc ##### -->
|
<!-- ##### USER_FUNCTION GChildWatchFunc ##### -->
|
||||||
<para>
|
<para>
|
||||||
The type of functions to be called when a child exists.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@pid: the process id of the child process
|
@pid:
|
||||||
@status: Status information about the child process,
|
@status:
|
||||||
see waitpid(2) for more information about this field
|
@data:
|
||||||
@data: user data passed to g_child_watch_add()
|
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_child_watch_source_new ##### -->
|
<!-- ##### FUNCTION g_child_watch_source_new ##### -->
|
||||||
@ -734,32 +608,6 @@ The type of functions to be called when a child exists.
|
|||||||
<!-- ##### STRUCT GPollFD ##### -->
|
<!-- ##### STRUCT GPollFD ##### -->
|
||||||
<para>
|
<para>
|
||||||
|
|
||||||
<informaltable pgwide="1" frame="none" role="struct">
|
|
||||||
<tgroup cols="2"><colspec colwidth="2*"/><colspec colwidth="8*"/>
|
|
||||||
<tbody>
|
|
||||||
|
|
||||||
<row>
|
|
||||||
<entry>#gint fd;</entry>
|
|
||||||
<entry>the file descriptor to poll (or a <type>HANDLE</type> on Win32 platforms).</entry>
|
|
||||||
</row>
|
|
||||||
|
|
||||||
<row>
|
|
||||||
<entry>#gushort events;</entry>
|
|
||||||
<entry>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.
|
|
||||||
</entry>
|
|
||||||
</row>
|
|
||||||
|
|
||||||
<row>
|
|
||||||
<entry>#gushort revents;</entry>
|
|
||||||
<entry>a bitwise combination of flags from #GIOCondition, returned from the
|
|
||||||
<function>poll()</function> function to indicate which events occurred.
|
|
||||||
</entry>
|
|
||||||
</row>
|
|
||||||
</tbody></tgroup></informaltable>
|
|
||||||
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@fd:
|
@fd:
|
||||||
@ -787,8 +635,7 @@ you would use %G_IO_IN | %G_IO_HUP | %G_IO_ERR, and for writing you would use
|
|||||||
|
|
||||||
<!-- ##### STRUCT GSource ##### -->
|
<!-- ##### STRUCT GSource ##### -->
|
||||||
<para>
|
<para>
|
||||||
The <structname>GSource</structname> struct is an opaque data type representing
|
|
||||||
an event source.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
|
||||||
@ -803,63 +650,24 @@ for dependency reasons.
|
|||||||
|
|
||||||
<!-- ##### STRUCT GSourceFuncs ##### -->
|
<!-- ##### STRUCT GSourceFuncs ##### -->
|
||||||
<para>
|
<para>
|
||||||
The #GSourceFuncs struct contains a table of functions used to handle
|
|
||||||
event sources in a generic manner.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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).
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
|
||||||
<para>
|
|
||||||
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.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@prepare: Called before all the file descriptors are polled.
|
@prepare:
|
||||||
If the source can determine that it is ready here (without waiting for the
|
@check:
|
||||||
results of the poll() call) it should return %TRUE.
|
@dispatch:
|
||||||
It can also return a @timeout_ value which should be the maximum timeout
|
@finalize:
|
||||||
(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.
|
|
||||||
@closure_callback:
|
@closure_callback:
|
||||||
@closure_marshal:
|
@closure_marshal:
|
||||||
|
|
||||||
<!-- ##### STRUCT GSourceCallbackFuncs ##### -->
|
<!-- ##### STRUCT GSourceCallbackFuncs ##### -->
|
||||||
<para>
|
<para>
|
||||||
The <structname>GSourceCallbackFuncs</structname> struct contains
|
|
||||||
functions for managing callback objects.
|
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
@ref: Called when a reference is added to the callback object.
|
@ref:
|
||||||
@unref: Called when a reference to the callback object is dropped.
|
@unref:
|
||||||
@get: Called to extract the callback function and data from the callback object.
|
@get:
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_source_new ##### -->
|
<!-- ##### FUNCTION g_source_new ##### -->
|
||||||
<para>
|
<para>
|
||||||
|
76
glib/gmain.c
76
glib/gmain.c
@ -78,6 +78,82 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SECTION:main
|
||||||
|
* @title: The Main Event Loop
|
||||||
|
* @short_description: 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().
|
||||||
|
*
|
||||||
|
* <refsect2><title>Creating new source types</title>
|
||||||
|
* <para>One of the unusual features of the #GMainLoop 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 <firstterm>deriving</firstterm> 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 type.</para>
|
||||||
|
* <para>New source types basically interact 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().</para>
|
||||||
|
* </refsect2>
|
||||||
|
* <refsect2><title>Customizing the main loop iteration</title>
|
||||||
|
* <para>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().</para>
|
||||||
|
* <para>The operation of these functions can best be seen in terms
|
||||||
|
* of a state diagram, as shown in <xref linkend="mainloop-states"/>.</para>
|
||||||
|
* <figure id="mainloop-states"><title>States of a Main Context</title>
|
||||||
|
* <graphic fileref="mainloop-states.gif" format="GIF"></graphic>
|
||||||
|
* </figure>
|
||||||
|
* </refsect2>
|
||||||
|
*/
|
||||||
|
|
||||||
/* Types */
|
/* Types */
|
||||||
|
|
||||||
typedef struct _GTimeoutSource GTimeoutSource;
|
typedef struct _GTimeoutSource GTimeoutSource;
|
||||||
|
232
glib/gmain.h
232
glib/gmain.h
@ -30,13 +30,109 @@
|
|||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
typedef struct _GMainContext GMainContext; /* Opaque */
|
/**
|
||||||
typedef struct _GMainLoop GMainLoop; /* Opaque */
|
* GMainContext:
|
||||||
|
*
|
||||||
|
* The <structname>GMainContext</structname> struct is an opaque data
|
||||||
|
* type representing a set of sources to be handled in a main loop.
|
||||||
|
*/
|
||||||
|
typedef struct _GMainContext GMainContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GMainLoop:
|
||||||
|
*
|
||||||
|
* The <structname>GMainLoop</structname> struct is an opaque data type
|
||||||
|
* representing the main event loop of a GLib or GTK+ application.
|
||||||
|
*/
|
||||||
|
typedef struct _GMainLoop GMainLoop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GSource:
|
||||||
|
*
|
||||||
|
* The <structname>GSource</structname> struct is an opaque data type
|
||||||
|
* representing an event source.
|
||||||
|
*/
|
||||||
typedef struct _GSource GSource;
|
typedef struct _GSource GSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GSourceCallbackFuncs:
|
||||||
|
* @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.
|
||||||
|
|
||||||
|
* The <structname>GSourceCallbackFuncs</structname> struct contains
|
||||||
|
* functions for managing callback objects.
|
||||||
|
*/
|
||||||
typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs;
|
typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GSourceFuncs:
|
||||||
|
* @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.
|
||||||
|
* @closure_callback:
|
||||||
|
* @closure_marshal:
|
||||||
|
*
|
||||||
|
* The <structname>GSourceFuncs</structname> struct contains a table of
|
||||||
|
* functions used to handle event sources in a generic manner.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
typedef struct _GSourceFuncs GSourceFuncs;
|
typedef struct _GSourceFuncs GSourceFuncs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GPid:
|
||||||
|
*
|
||||||
|
* A type which is used to hold a process identification.
|
||||||
|
*
|
||||||
|
* On UNIX, processes are identified by a process id (an integer),
|
||||||
|
* while Windows uses process handles (which are pointers).
|
||||||
|
*/
|
||||||
|
|
||||||
typedef gboolean (*GSourceFunc) (gpointer data);
|
typedef gboolean (*GSourceFunc) (gpointer data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GChildWatchFunc:
|
||||||
|
* @pid: the process id of the child process
|
||||||
|
* @status: Status information about the child process,
|
||||||
|
* see waitpid(2) for more information about this field
|
||||||
|
* @data: user data passed to g_child_watch_add()
|
||||||
|
*
|
||||||
|
* The type of functions to be called when a child exists.
|
||||||
|
*/
|
||||||
typedef void (*GChildWatchFunc) (GPid pid,
|
typedef void (*GChildWatchFunc) (GPid pid,
|
||||||
gint status,
|
gint status,
|
||||||
gpointer data);
|
gpointer data);
|
||||||
@ -93,10 +189,55 @@ struct _GSourceFuncs
|
|||||||
|
|
||||||
/* Standard priorities */
|
/* Standard priorities */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* G_PRIORITY_HIGH:
|
||||||
|
*
|
||||||
|
* Use this for high priority event sources.
|
||||||
|
*
|
||||||
|
* It is not used within GLib or GTK+.
|
||||||
|
*/
|
||||||
#define G_PRIORITY_HIGH -100
|
#define G_PRIORITY_HIGH -100
|
||||||
|
|
||||||
|
/**
|
||||||
|
* G_PRIORITY_DEFAULT:
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
#define G_PRIORITY_DEFAULT 0
|
#define G_PRIORITY_DEFAULT 0
|
||||||
|
|
||||||
|
/**
|
||||||
|
* G_PRIORITY_HIGH_IDLE:
|
||||||
|
*
|
||||||
|
* 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.)
|
||||||
|
*/
|
||||||
#define G_PRIORITY_HIGH_IDLE 100
|
#define G_PRIORITY_HIGH_IDLE 100
|
||||||
|
|
||||||
|
/**
|
||||||
|
* G_PRIORITY_DEFAULT_IDLE:
|
||||||
|
*
|
||||||
|
* Use this for default priority idle functions.
|
||||||
|
*
|
||||||
|
* In GLib this priority is used when adding idle functions with
|
||||||
|
* g_idle_add().
|
||||||
|
*/
|
||||||
#define G_PRIORITY_DEFAULT_IDLE 200
|
#define G_PRIORITY_DEFAULT_IDLE 200
|
||||||
|
|
||||||
|
/**
|
||||||
|
* G_PRIORITY_LOW:
|
||||||
|
*
|
||||||
|
* Use this for very low priority background tasks.
|
||||||
|
*
|
||||||
|
* It is not used within GLib or GTK+.
|
||||||
|
*/
|
||||||
#define G_PRIORITY_LOW 300
|
#define G_PRIORITY_LOW 300
|
||||||
|
|
||||||
/* GMainContext: */
|
/* GMainContext: */
|
||||||
@ -244,20 +385,99 @@ void g_get_current_time (GTimeVal *result);
|
|||||||
|
|
||||||
#ifndef G_DISABLE_DEPRECATED
|
#ifndef G_DISABLE_DEPRECATED
|
||||||
|
|
||||||
/* Legacy names for GMainLoop functions
|
/**
|
||||||
|
* g_main_new:
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* Creates a new #GMainLoop for th default main context.
|
||||||
|
*
|
||||||
|
* Returns: a new #GMainLoop
|
||||||
|
*
|
||||||
|
* Deprecated: 2.2: Use g_main_loop_new() instead
|
||||||
|
*/
|
||||||
|
#define g_main_new(is_running) g_main_loop_new (NULL, is_running)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_main_run:
|
||||||
|
* @loop: a #GMainLoop
|
||||||
|
*
|
||||||
|
* Runs a main loop until it stops running.
|
||||||
|
*
|
||||||
|
* Deprecated: 2.2: Use g_main_loop_run() instead
|
||||||
*/
|
*/
|
||||||
#define g_main_new(is_running) g_main_loop_new (NULL, is_running);
|
|
||||||
#define g_main_run(loop) g_main_loop_run(loop)
|
#define g_main_run(loop) g_main_loop_run(loop)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_main_quit:
|
||||||
|
* @loop: a #GMainLoop
|
||||||
|
*
|
||||||
|
* Stops the #GMainLoop.
|
||||||
|
* If g_main_run() was called to run the #GMainLoop, it will now return.
|
||||||
|
*
|
||||||
|
* Deprecated: 2.2: Use g_main_loop_quit() instead
|
||||||
|
*/
|
||||||
#define g_main_quit(loop) g_main_loop_quit(loop)
|
#define g_main_quit(loop) g_main_loop_quit(loop)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_main_destroy:
|
||||||
|
* @loop: a #GMainLoop
|
||||||
|
*
|
||||||
|
* Frees the memory allocated for the #GMainLoop.
|
||||||
|
*
|
||||||
|
* Deprecated: 2.2: Use g_main_loop_unref() instead
|
||||||
|
*/
|
||||||
#define g_main_destroy(loop) g_main_loop_unref(loop)
|
#define g_main_destroy(loop) g_main_loop_unref(loop)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_main_is_running:
|
||||||
|
* @loop: a #GMainLoop
|
||||||
|
*
|
||||||
|
* Checks if the main loop is running.
|
||||||
|
*
|
||||||
|
* Returns: %TRUE if the main loop is running
|
||||||
|
*
|
||||||
|
* Deprecated: 2.2: Use g_main_loop_is_running() instead
|
||||||
|
*/
|
||||||
#define g_main_is_running(loop) g_main_loop_is_running(loop)
|
#define g_main_is_running(loop) g_main_loop_is_running(loop)
|
||||||
|
|
||||||
/* Functions to manipulate the default main loop
|
/**
|
||||||
|
* g_main_iteration:
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* Runs a single iteration for the default #GMainContext.
|
||||||
|
*
|
||||||
|
* Returns: %TRUE if more events are pending.
|
||||||
|
*
|
||||||
|
* Deprecated: 2.2: Use g_main_context_iteration() instead.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define g_main_iteration(may_block) g_main_context_iteration (NULL, may_block)
|
#define g_main_iteration(may_block) g_main_context_iteration (NULL, may_block)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_main_pending:
|
||||||
|
*
|
||||||
|
* Checks if any events are pending for the default #GMainContext
|
||||||
|
* (i.e. ready to be processed).
|
||||||
|
*
|
||||||
|
* Returns: %TRUE if any events are pending.
|
||||||
|
*
|
||||||
|
* Deprected: 2.2: Use g_main_context_pending() instead.
|
||||||
|
*/
|
||||||
#define g_main_pending() g_main_context_pending (NULL)
|
#define g_main_pending() g_main_context_pending (NULL)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_main_set_poll_func:
|
||||||
|
* @func: the function to call to poll all file descriptors
|
||||||
|
*
|
||||||
|
* Sets the function to use for the handle polling of file descriptors
|
||||||
|
* for the default main context.
|
||||||
|
*
|
||||||
|
* Deprecated: 2.2: Use g_main_context_set_poll_func() again
|
||||||
|
*/
|
||||||
#define g_main_set_poll_func(func) g_main_context_set_poll_func (NULL, func)
|
#define g_main_set_poll_func(func) g_main_context_set_poll_func (NULL, func)
|
||||||
|
|
||||||
#endif /* G_DISABLE_DEPRECATED */
|
#endif /* G_DISABLE_DEPRECATED */
|
||||||
|
24
glib/gpoll.h
24
glib/gpoll.h
@ -59,10 +59,34 @@ G_BEGIN_DECLS
|
|||||||
* Windows.
|
* Windows.
|
||||||
*/
|
*/
|
||||||
typedef struct _GPollFD GPollFD;
|
typedef struct _GPollFD GPollFD;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GPollFunc:
|
||||||
|
* @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.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* Returns: the number of #GPollFD elements which have events or errors
|
||||||
|
* reported, or -1 if an error occurred.
|
||||||
|
*/
|
||||||
typedef gint (*GPollFunc) (GPollFD *ufds,
|
typedef gint (*GPollFunc) (GPollFD *ufds,
|
||||||
guint nfsd,
|
guint nfsd,
|
||||||
gint timeout_);
|
gint timeout_);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GPollFD:
|
||||||
|
* @fd: the file descriptor to poll (or a <type>HANDLE</type> on Win32)
|
||||||
|
* @events: a bitwise combination 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.
|
||||||
|
* @revents: a bitwise combination of flags from #GIOCondition, returned
|
||||||
|
* from the poll() function to indicate which events occurred.
|
||||||
|
*/
|
||||||
struct _GPollFD
|
struct _GPollFD
|
||||||
{
|
{
|
||||||
#if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
|
#if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
|
||||||
|
Loading…
x
Reference in New Issue
Block a user