1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 manages all available sources of events.
7 <!-- ##### SECTION Long_Description ##### -->
9 The main event loop manages all the available sources of events for
10 GLib and GTK+ applications. These events can come from any number of
11 different types of sources such as file descriptors (plain files,
12 pipes or sockets) and timeouts. New types of event sources can also
13 be added using g_source_add().
16 To allow multiple independent sets of sources to be handled in
17 different threads, each source is associated with a #GMainContext.
18 A #GMainContext can only be running in a single thread, but
19 sources can be added to it and removed from it from other threads.
22 Each event source is assigned a priority. The default priority,
23 #G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher
24 priorities. Values greater than 0 denote lower priorities. Events
25 from high priority sources are always processed before events from
26 lower priority sources.
29 Idle functions can also be added, and assigned a priority. These will
30 be run whenever no events with a higher priority are ready to be
34 The #GMainLoop data type represents a main event loop. A #GMainLoop
35 is created with g_main_loop_new(). After adding the initial event sources,
36 g_main_run() is called. This continuously checks for new events from
37 each of the event sources and dispatches them. Finally, the
38 processing of an event from one of the sources leads to a call to
39 g_main_quit() to exit the main loop, and g_main_run() returns.
42 It is possible to create new instances of #GMainLoop recursively.
43 This is often used in GTK+ applications when showing modal dialog
44 boxes. Note that event sources are associated with a particular
45 #GMainContext, and will be checked and dispatched for all main
46 loops associated with that #GMainContext.
49 GTK+ contains wrappers of many of these functions, e.g. gtk_main(),
50 gtk_main_quit(), gtk_events_pending(), gtk_idle_add(),
51 gtk_timeout_add() and gtk_input_add_full().
54 <title>Creating new sources types</title>
56 One of the unusual features of the GTK+ main loop functionality
57 is that new types of event source can be created and used in
58 addition to the builtin type of event source. A new event source
59 type is used for handling GDK events. A new source type is
60 created by <firstterm>deriving</firstterm> from the #GSource
61 structure. The derived type of source is represented by a
62 structure that has the #GSource structure as a first element,
63 and other elements specific to the new source type. To create
64 an instance of the new source type, call g_source_new() passing
65 in the size of the derived structure and a table of functions.
66 These #GSourceFuncs determine the behavior of the new source
70 New source types basically interact with with the main context
71 in two ways. Their prepare function in #GSourceFuncs can set
72 a timeout to determine the maximum amount of time that the
73 main loop will sleep before checking the source again. In
74 addition, or as well, the source can add file descriptors to
75 the set that the main context checks using g_source_add_poll().
79 <title>Customizing the main loop iteration</title>
81 Single iterations of a #GMainContext can be run with
82 g_main_context_iteration(). In some cases, more detailed control
83 of exactly how the details of the main loop work is desired,
84 for instance, when integrating the #GMainLoop with an external
85 main loop. In such cases, you can call the component functions
86 of g_main_context_iteration() directly. These functions
87 are g_main_context_prepare(), g_main_context_query(),
88 g_main_context_check() and g_main_context_dispatch().
91 The operation of these functions can best be seen in terms
92 of a state diagram, as shown in <xref linkend="mainloop-states">.
94 <figure id="mainloop-states">
95 <title>States of a Main Context</title>
96 <graphic fileref="mainloop-states.gif" format="gif"></graphic>
100 <!-- ##### SECTION See_Also ##### -->
105 <!-- ##### STRUCT GMainLoop ##### -->
107 The <structname>GMainLoop</structname> struct is an opaque data type
108 representing the main event loop of a GLib or GTK+ application.
112 <!-- ##### FUNCTION g_main_loop_new ##### -->
122 <!-- ##### FUNCTION g_main_loop_ref ##### -->
131 <!-- ##### FUNCTION g_main_loop_unref ##### -->
139 <!-- ##### FUNCTION g_main_loop_run ##### -->
147 <!-- ##### FUNCTION g_main_loop_quit ##### -->
155 <!-- ##### FUNCTION g_main_loop_is_running ##### -->
164 <!-- ##### FUNCTION g_main_loop_get_context ##### -->
173 <!-- ##### MACRO g_main_new ##### -->
175 Creates a new #GMainLoop for the default main loop. A compatibility
176 macro, see g_main_loop_new().
179 @is_running: set to %TRUE to indicate that the loop is running. This is not
180 very important since calling g_main_run() will set this to %TRUE anyway.
181 @Returns: a new #GMainLoop.
184 <!-- ##### MACRO g_main_destroy ##### -->
186 Frees the memory allocated for the #GMainLoop. A compatibility macro, see
187 g_main_loop_destroy().
193 <!-- ##### MACRO g_main_run ##### -->
195 Runs a main loop until it stops running. A compatibility macro, see g_main_loop_run().
201 <!-- ##### MACRO g_main_quit ##### -->
203 Stops the #GMainLoop. If g_main_run() was called to run the #GMainLoop,
204 it will now return. A compatibility macro, see g_main_loop_quit().
210 <!-- ##### MACRO g_main_is_running ##### -->
212 Checks if the main loop is running. A compatibility macro, see
213 g_main_loop_is_running().
217 @Returns: %TRUE if the main loop is running.
220 <!-- ##### MACRO G_PRIORITY_HIGH ##### -->
222 Use this for high priority event sources.
223 It is not used within GLib or GTK+.
228 <!-- ##### MACRO G_PRIORITY_DEFAULT ##### -->
230 Use this for default priority event sources.
231 In GLib this priority is used when adding timeout functions with
233 In GDK this priority is used for events from the X Windows server.
238 <!-- ##### MACRO G_PRIORITY_HIGH_IDLE ##### -->
240 Use this for high priority idle functions.
241 GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations, and
242 #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is done to
243 ensure that any pending resizes are processed before any pending redraws,
244 so that widgets are not redrawn twice unnecessarily.)
249 <!-- ##### MACRO G_PRIORITY_DEFAULT_IDLE ##### -->
251 Use this for default priority idle functions.
252 In GLib this priority is used when adding idle functions with g_idle_add().
257 <!-- ##### MACRO G_PRIORITY_LOW ##### -->
259 Use this for very low priority background tasks.
260 It is not used within GLib or GTK+.
265 <!-- ##### STRUCT GMainContext ##### -->
267 The <structname>GMainContext</structname> struct is an opaque data type
268 representing a set of sources to be handled in a main loop.
272 <!-- ##### FUNCTION g_main_context_new ##### -->
280 <!-- ##### FUNCTION g_main_context_ref ##### -->
288 <!-- ##### FUNCTION g_main_context_unref ##### -->
296 <!-- ##### FUNCTION g_main_context_default ##### -->
304 <!-- ##### FUNCTION g_main_context_iteration ##### -->
314 <!-- ##### MACRO g_main_iteration ##### -->
316 Runs a single iteration for the default #GMainContext.
317 A compatibility macro, see g_main_context_iteration().
320 @may_block: set to %TRUE if it should block (i.e. wait) until an event source
321 becomes ready. It will return after an event source has been processed.
322 If set to %FALSE it will return immediately if no event source is ready to be
324 @Returns: %TRUE if more events are pending.
327 <!-- ##### FUNCTION g_main_context_pending ##### -->
336 <!-- ##### MACRO g_main_pending ##### -->
338 Checks if any events are pending for the default #GMainContext
339 (i.e. ready to be processed). A compatibility macro, see
340 g_main_context_pending().
343 @Returns: %TRUE if any events are pending.
346 <!-- ##### FUNCTION g_main_context_find_source_by_id ##### -->
354 <!-- # Unused Parameters # -->
358 <!-- ##### FUNCTION g_main_context_find_source_by_user_data ##### -->
368 <!-- ##### FUNCTION g_main_context_find_source_by_funcs_user_data ##### -->
379 <!-- ##### FUNCTION g_main_context_wakeup ##### -->
387 <!-- ##### FUNCTION g_main_context_acquire ##### -->
396 <!-- ##### FUNCTION g_main_context_release ##### -->
404 <!-- ##### FUNCTION g_main_context_wait ##### -->
415 <!-- ##### FUNCTION g_main_context_prepare ##### -->
425 <!-- ##### FUNCTION g_main_context_query ##### -->
438 <!-- ##### FUNCTION g_main_context_check ##### -->
450 <!-- ##### FUNCTION g_main_context_dispatch ##### -->
458 <!-- ##### FUNCTION g_main_context_set_poll_func ##### -->
467 <!-- ##### FUNCTION g_main_context_get_poll_func ##### -->
476 <!-- ##### USER_FUNCTION GPollFunc ##### -->
478 Specifies the type of function passed to g_main_set_poll_func().
479 The semantics of the function should match those of the
480 <function>poll()</function> system call.
483 @ufds: an array of #GPollFD elements.
484 @nfsd: the number of elements in @ufds.
485 @timeout: the maximum time to wait for an event of the file descriptors.
486 A negative value indicates an infinite timeout.
487 @Returns: the number of #GPollFD elements which have events or errors reported,
488 or -1 if an error occurred.
491 <!-- ##### FUNCTION g_main_context_add_poll ##### -->
501 <!-- ##### FUNCTION g_main_context_remove_poll ##### -->
510 <!-- ##### MACRO g_main_set_poll_func ##### -->
512 Sets the function to use for the handle polling of file descriptors
513 for the default main context. This is a compatability macro, see
514 g_main_context_set_poll_func() for full details.
517 @func: the function to call to poll all file descriptors.
520 <!-- ##### FUNCTION g_timeout_source_new ##### -->
529 <!-- ##### FUNCTION g_timeout_add ##### -->
539 <!-- ##### FUNCTION g_timeout_add_full ##### -->
551 <!-- ##### FUNCTION g_idle_source_new ##### -->
559 <!-- ##### FUNCTION g_idle_add ##### -->
568 <!-- ##### FUNCTION g_idle_add_full ##### -->
579 <!-- ##### FUNCTION g_idle_remove_by_data ##### -->
587 <!-- ##### STRUCT GPollFD ##### -->
590 <informaltable pgwide=1 frame="none" role="struct">
591 <tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*">
595 <entry>#gint fd;</entry>
596 <entry>the file descriptor to poll (or a HANDLE on Win32 platforms).</entry>
600 <entry>#gushort events;</entry>
601 <entry>a bitwise combination of flags from #GIOCondition, specifying which
602 events should be polled for. Typically for reading from a file descriptor
603 you would use %G_IO_IN | %G_IO_HUP | %G_IO_ERR, and for writing you would use
604 %G_IO_OUT | %G_IO_ERR.
609 <entry>#gushort revents;</entry>
610 <entry>a bitwise combination of flags from #GIOCondition, returned from the
611 <function>poll()</function> function to indicate which events occurred.
614 </tbody></tgroup></informaltable>
622 <!-- ##### STRUCT GSource ##### -->
624 The <structname>GSource</structname> struct is an opaque data type representing
629 <!-- ##### STRUCT GSourceFuncs ##### -->
631 The #GSourceFuncs struct contains a table of functions used to handle
632 event sources in a generic manner.
634 <informaltable pgwide=1 frame="none" role="struct">
635 <tgroup cols="2"><colspec colwidth="2*"><colspec colwidth="8*">
639 <entry>prepare</entry>
641 Called before all the file descriptors are polled.
642 If the source can determine that it is ready here (without waiting for the
643 results of the poll() call) it should return %TRUE.
644 It can also return a @timeout value which should be the maximum timeout
645 (in milliseconds) which should be passed to the poll() call.
646 The actual timeout used will be -1 if all sources returned -1, or it will
647 be the minimum of all the @timeout values returned which were >= 0.
654 Called after all the file descriptors are polled.
655 The source should return %TRUE if it is ready to be dispatched.
656 Note that some time may have passed since the previous prepare function was
657 called, so the source should be checked again here.
662 <entry>dispatch</entry>
664 Called to dispatch the event source, after it has returned %TRUE in
665 either its @prepare or its @check function. The @dispatch function is
666 passed in a callback function and data. The callback function may be
667 %NULL if the source was never connected using g_source_connect(). The
668 @dispatch function should call the callback function with data and
669 whatever additional parameters are needed for this type of event source.
674 <entry>finalize</entry>
676 Called when the source is finalized.
679 </tbody></tgroup></informaltable>
683 For idle sources, the prepare and check functions always return %TRUE to
684 indicate that the source is always ready to be processed.
685 The prepare function also returns a timeout value of 0 to ensure that the
686 <function>poll()</function> call doesn't block (since that would be time
687 wasted which could have been spent running the idle function).
690 For timeout sources, the prepare and check functions both return %TRUE if the
691 timeout interval has expired. The prepare function also returns a timeout
692 value to ensure that the <function>poll()</function> call doesn't block too
693 long and miss the next timeout.
696 For file descriptor sources, the prepare function typically returns %FALSE,
697 since it must wait until <function>poll()</function> has been called before
698 it knows whether any events need to be processed. It sets the returned
699 timeout to -1 to indicate that it doesn't mind how long the
700 <function>poll()</function> call blocks.
701 In the check function, it tests the results of the <function>poll()</function>
702 call to see if the required condition has been met, and returns %TRUE if so.
712 <!-- ##### STRUCT GSourceCallbackFuncs ##### -->
714 The <structname>GSourceCallbackFuncs</structname> struct contains
715 functions for managing callback objects.
718 @ref: Called when a reference is added to the callback object.
719 @unref: Called when a reference to the callback object is dropped.
720 @get: Called to extract the callback function and data from the callback object.
722 <!-- ##### USER_FUNCTION GSourceDummyMarshal ##### -->
729 <!-- ##### FUNCTION g_source_new ##### -->
739 <!-- ##### FUNCTION g_source_ref ##### -->
748 <!-- ##### FUNCTION g_source_unref ##### -->
756 <!-- ##### FUNCTION g_source_attach ##### -->
766 <!-- ##### FUNCTION g_source_destroy ##### -->
774 <!-- ##### FUNCTION g_source_set_priority ##### -->
783 <!-- ##### FUNCTION g_source_get_priority ##### -->
792 <!-- ##### FUNCTION g_source_set_can_recurse ##### -->
801 <!-- ##### FUNCTION g_source_get_can_recurse ##### -->
810 <!-- ##### FUNCTION g_source_get_id ##### -->
819 <!-- ##### FUNCTION g_source_get_context ##### -->
828 <!-- ##### FUNCTION g_source_set_callback ##### -->
839 <!-- ##### USER_FUNCTION GSourceFunc ##### -->
841 Specifies the type of function passed to g_timeout_add(), g_timeout_add_full(),
842 g_idle_add(), and g_idle_add_full().
845 @data: data passed to the function, set when the source was created with one
846 of the above functions.
847 @Returns: it should return %FALSE if the source should be removed.
850 <!-- ##### FUNCTION g_source_set_callback_indirect ##### -->
860 <!-- ##### FUNCTION g_source_add_poll ##### -->
869 <!-- ##### FUNCTION g_source_remove_poll ##### -->
878 <!-- ##### FUNCTION g_source_get_current_time ##### -->
887 <!-- ##### FUNCTION g_source_remove ##### -->
895 <!-- ##### FUNCTION g_source_remove_by_funcs_user_data ##### -->
904 <!-- ##### FUNCTION g_source_remove_by_user_data ##### -->
914 sgml-parent-document: ("../glib-docs.sgml" "book" "refsect2" "")