Patch from Havoc Pennington to add functions for setting and getting a
[platform/upstream/glib.git] / docs / reference / glib / tmpl / main.sgml
1 <!-- ##### SECTION Title ##### -->
2 The Main Event Loop
3
4 <!-- ##### SECTION Short_Description ##### -->
5 manages all available sources of events.
6
7 <!-- ##### SECTION Long_Description ##### -->
8   <para>
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_attach().
14   </para>
15   <para>
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.
20   </para>
21   <para>
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.
27   </para>
28   <para>
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
31     processed.
32   </para>
33   <para>
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_loop_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_loop_quit() to exit the main loop, and g_main_loop_run() returns.
40   </para>
41   <para>
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.
47   </para>
48   <para>
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(). 
52   </para>
53   <refsect2>
54     <title>Creating new sources types</title>
55     <para>
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
67       types.
68     </para>
69     <para>
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().
76     </para>
77   </refsect2>
78   <refsect2>
79     <title>Customizing the main loop iteration</title>
80     <para>
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().
89     </para>
90     <para>
91       The operation of these functions can best be seen in terms
92       of a state diagram, as shown in <xref linkend="mainloop-states"/>.
93     </para>
94     <figure id="mainloop-states">
95       <title>States of a Main Context</title>
96       <graphic fileref="mainloop-states.gif" format="GIF"></graphic>
97     </figure>
98   </refsect2>
99
100 <!-- ##### SECTION See_Also ##### -->
101 <para>
102
103 </para>
104
105 <!-- ##### STRUCT GMainLoop ##### -->
106 <para>
107 The <structname>GMainLoop</structname> struct is an opaque data type 
108 representing the main event loop of a GLib or GTK+ application.
109 </para>
110
111
112 <!-- ##### FUNCTION g_main_loop_new ##### -->
113 <para>
114
115 </para>
116
117 @context: 
118 @is_running: 
119 @Returns: 
120
121
122 <!-- ##### FUNCTION g_main_loop_ref ##### -->
123 <para>
124
125 </para>
126
127 @loop: 
128 @Returns: 
129
130
131 <!-- ##### FUNCTION g_main_loop_unref ##### -->
132 <para>
133
134 </para>
135
136 @loop: 
137
138
139 <!-- ##### FUNCTION g_main_loop_run ##### -->
140 <para>
141
142 </para>
143
144 @loop: 
145
146
147 <!-- ##### FUNCTION g_main_loop_quit ##### -->
148 <para>
149
150 </para>
151
152 @loop: 
153
154
155 <!-- ##### FUNCTION g_main_loop_is_running ##### -->
156 <para>
157
158 </para>
159
160 @loop: 
161 @Returns: 
162
163
164 <!-- ##### FUNCTION g_main_loop_get_context ##### -->
165 <para>
166
167 </para>
168
169 @loop: 
170 @Returns: 
171
172
173 <!-- ##### MACRO g_main_new ##### -->
174 <para>
175 Creates a new #GMainLoop for the default main loop. A compatibility
176 macro, see g_main_loop_new().
177 </para>
178
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.
182
183
184 <!-- ##### MACRO g_main_destroy ##### -->
185 <para>
186 Frees the memory allocated for the #GMainLoop. A compatibility macro, see
187 g_main_loop_unref().
188 </para>
189
190 @loop: a #GMainLoop.
191
192
193 <!-- ##### MACRO g_main_run ##### -->
194 <para>
195 Runs a main loop until it stops running. A compatibility macro, see g_main_loop_run().
196 </para>
197
198 @loop: a #GMainLoop.
199
200
201 <!-- ##### MACRO g_main_quit ##### -->
202 <para>
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().
205 </para>
206
207 @loop: a #GMainLoop.
208
209
210 <!-- ##### MACRO g_main_is_running ##### -->
211 <para>
212 Checks if the main loop is running. A compatibility macro, see
213 g_main_loop_is_running().    
214 </para>
215
216 @loop: a #GMainLoop.
217 @Returns: %TRUE if the main loop is running.
218
219
220 <!-- ##### MACRO G_PRIORITY_HIGH ##### -->
221 <para>
222 Use this for high priority event sources.
223 It is not used within GLib or GTK+.
224 </para>
225
226
227
228 <!-- ##### MACRO G_PRIORITY_DEFAULT ##### -->
229 <para>
230 Use this for default priority event sources.
231 In GLib this priority is used when adding timeout functions with
232 g_timeout_add().
233 In GDK this priority is used for events from the X server.
234 </para>
235
236
237
238 <!-- ##### MACRO G_PRIORITY_HIGH_IDLE ##### -->
239 <para>
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.)
245 </para>
246
247
248
249 <!-- ##### MACRO G_PRIORITY_DEFAULT_IDLE ##### -->
250 <para>
251 Use this for default priority idle functions.
252 In GLib this priority is used when adding idle functions with g_idle_add().
253 </para>
254
255
256
257 <!-- ##### MACRO G_PRIORITY_LOW ##### -->
258 <para>
259 Use this for very low priority background tasks.
260 It is not used within GLib or GTK+.
261 </para>
262
263
264
265 <!-- ##### STRUCT GMainContext ##### -->
266 <para>
267 The <structname>GMainContext</structname> struct is an opaque data type 
268 representing a set of sources to be handled in a main loop. 
269 </para>
270
271
272 <!-- ##### FUNCTION g_main_context_new ##### -->
273 <para>
274
275 </para>
276
277 @Returns: 
278
279
280 <!-- ##### FUNCTION g_main_context_ref ##### -->
281 <para>
282
283 </para>
284
285 @context: 
286
287
288 <!-- ##### FUNCTION g_main_context_unref ##### -->
289 <para>
290
291 </para>
292
293 @context: 
294
295
296 <!-- ##### FUNCTION g_main_context_default ##### -->
297 <para>
298
299 </para>
300
301 @Returns: 
302
303
304 <!-- ##### FUNCTION g_main_context_iteration ##### -->
305 <para>
306
307 </para>
308
309 @context: 
310 @may_block: 
311 @Returns: 
312
313
314 <!-- ##### MACRO g_main_iteration ##### -->
315 <para>
316 Runs a single iteration for the default #GMainContext.
317 A compatibility macro, see g_main_context_iteration().
318 </para>
319
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
323 processed.
324 @Returns: %TRUE if more events are pending.
325
326
327 <!-- ##### FUNCTION g_main_context_pending ##### -->
328 <para>
329
330 </para>
331
332 @context: 
333 @Returns: 
334
335
336 <!-- ##### MACRO g_main_pending ##### -->
337 <para>
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().
341 </para>
342
343 @Returns: %TRUE if any events are pending.
344
345
346 <!-- ##### FUNCTION g_main_context_find_source_by_id ##### -->
347 <para>
348
349 </para>
350
351 @context: 
352 @source_id: 
353 @Returns: 
354 <!-- # Unused Parameters # -->
355 @id: 
356
357
358 <!-- ##### FUNCTION g_main_context_find_source_by_user_data ##### -->
359 <para>
360
361 </para>
362
363 @context: 
364 @user_data: 
365 @Returns: 
366
367
368 <!-- ##### FUNCTION g_main_context_find_source_by_funcs_user_data ##### -->
369 <para>
370
371 </para>
372
373 @context: 
374 @funcs: 
375 @user_data: 
376 @Returns: 
377
378
379 <!-- ##### FUNCTION g_main_context_wakeup ##### -->
380 <para>
381
382 </para>
383
384 @context: 
385
386
387 <!-- ##### FUNCTION g_main_context_acquire ##### -->
388 <para>
389
390 </para>
391
392 @context: 
393 @Returns: 
394
395
396 <!-- ##### FUNCTION g_main_context_release ##### -->
397 <para>
398
399 </para>
400
401 @context: 
402
403
404 <!-- ##### FUNCTION g_main_context_wait ##### -->
405 <para>
406
407 </para>
408
409 @context: 
410 @cond: 
411 @mutex: 
412 @Returns: 
413
414
415 <!-- ##### FUNCTION g_main_context_prepare ##### -->
416 <para>
417
418 </para>
419
420 @context: 
421 @priority: 
422 @Returns: 
423
424
425 <!-- ##### FUNCTION g_main_context_query ##### -->
426 <para>
427
428 </para>
429
430 @context: 
431 @max_priority: 
432 @timeout: 
433 @fds: 
434 @n_fds: 
435 @Returns: 
436
437
438 <!-- ##### FUNCTION g_main_context_check ##### -->
439 <para>
440
441 </para>
442
443 @context: 
444 @max_priority: 
445 @fds: 
446 @n_fds: 
447 @Returns: 
448
449
450 <!-- ##### FUNCTION g_main_context_dispatch ##### -->
451 <para>
452
453 </para>
454
455 @context: 
456
457
458 <!-- ##### FUNCTION g_main_context_set_poll_func ##### -->
459 <para>
460
461 </para>
462
463 @context: 
464 @func: 
465
466
467 <!-- ##### FUNCTION g_main_context_get_poll_func ##### -->
468 <para>
469
470 </para>
471
472 @context: 
473 @Returns: 
474
475
476 <!-- ##### USER_FUNCTION GPollFunc ##### -->
477 <para>
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.
481 </para>
482
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.
489
490
491 <!-- ##### FUNCTION g_main_context_add_poll ##### -->
492 <para>
493
494 </para>
495
496 @context: 
497 @fd: 
498 @priority: 
499
500
501 <!-- ##### FUNCTION g_main_context_remove_poll ##### -->
502 <para>
503
504 </para>
505
506 @context: 
507 @fd: 
508
509
510 <!-- ##### MACRO g_main_set_poll_func ##### -->
511 <para>
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.
515 </para>
516
517 @func: the function to call to poll all file descriptors.
518
519
520 <!-- ##### FUNCTION g_timeout_source_new ##### -->
521 <para>
522
523 </para>
524
525 @interval: 
526 @Returns: 
527
528
529 <!-- ##### FUNCTION g_timeout_add ##### -->
530 <para>
531 </para>
532
533 @interval: 
534 @function: 
535 @data: 
536 @Returns: 
537
538
539 <!-- ##### FUNCTION g_timeout_add_full ##### -->
540 <para>
541 </para>
542
543 @priority: 
544 @interval: 
545 @function: 
546 @data: 
547 @notify: 
548 @Returns: 
549
550
551 <!-- ##### FUNCTION g_idle_source_new ##### -->
552 <para>
553
554 </para>
555
556 @Returns: 
557
558
559 <!-- ##### FUNCTION g_idle_add ##### -->
560 <para>
561 </para>
562
563 @function: 
564 @data: 
565 @Returns: 
566
567
568 <!-- ##### FUNCTION g_idle_add_full ##### -->
569 <para>
570 </para>
571
572 @priority: 
573 @function: 
574 @data: 
575 @notify: 
576 @Returns: 
577
578
579 <!-- ##### FUNCTION g_idle_remove_by_data ##### -->
580 <para>
581 </para>
582
583 @data: 
584 @Returns: 
585
586
587 <!-- ##### STRUCT GPollFD ##### -->
588 <para>
589
590 <informaltable pgwide="1" frame="none" role="struct">
591 <tgroup cols="2"><colspec colwidth="2*"/><colspec colwidth="8*"/>
592 <tbody>
593
594 <row>
595 <entry>#gint fd;</entry>
596 <entry>the file descriptor to poll (or a <type>HANDLE</type> on Win32 platforms).</entry>
597 </row>
598
599 <row>
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.
605 </entry>
606 </row>
607
608 <row>
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.
612 </entry>
613 </row>
614 </tbody></tgroup></informaltable>
615
616 </para>
617
618 @fd: 
619 @events: 
620 @revents: 
621
622 <!-- ##### STRUCT GSource ##### -->
623 <para>
624 The <structname>GSource</structname> struct is an opaque data type representing
625 an event source.
626 </para>
627
628
629 <!-- ##### USER_FUNCTION GSourceDummyMarshal ##### -->
630 <para>
631
632 </para>
633
634
635
636 <!-- ##### STRUCT GSourceFuncs ##### -->
637 <para>
638 The #GSourceFuncs struct contains a table of functions used to handle
639 event sources in a generic manner.
640
641 <informaltable pgwide="1" frame="none" role="struct">
642 <tgroup cols="2"><colspec colwidth="2*"/><colspec colwidth="8*"/>
643 <tbody>
644
645 <row>
646 <entry>prepare</entry>
647 <entry>
648 Called before all the file descriptors are polled.
649 If the source can determine that it is ready here (without waiting for the
650 results of the <function>poll()</function> call) it should return %TRUE.
651 It can also return a @timeout value which should be the maximum timeout
652 (in milliseconds) which should be passed to the <function>poll()</function> call.
653 The actual timeout used will be -1 if all sources returned -1, or it will
654 be the minimum of all the @timeout values returned which were >= 0.
655 </entry>
656 </row>
657
658 <row>
659 <entry>check</entry>
660 <entry>
661 Called after all the file descriptors are polled.
662 The source should return %TRUE if it is ready to be dispatched.
663 Note that some time may have passed since the previous prepare function was
664 called, so the source should be checked again here.
665 </entry>
666 </row>
667
668 <row>
669 <entry>dispatch</entry>
670 <entry>
671 Called to dispatch the event source, after it has returned %TRUE in
672 either its @prepare or its @check function. The @dispatch function is
673 passed in a callback function and data. The callback function may be
674 %NULL if the source was never connected using g_source_attach(). The
675 @dispatch function should call the callback function with data and
676 whatever additional parameters are needed for this type of event source.
677 </entry>
678 </row>
679
680 <row>
681 <entry>finalize</entry>
682 <entry>
683 Called when the source is finalized.
684 </entry>
685 </row>
686 </tbody></tgroup></informaltable>
687 </para>
688
689 <para>
690 For idle sources, the prepare and check functions always return %TRUE to
691 indicate that the source is always ready to be processed.
692 The prepare function also returns a timeout value of 0 to ensure that the
693 <function>poll()</function> call doesn't block (since that would be time 
694 wasted which could have been spent running the idle function).
695 </para>
696 <para>
697 For timeout sources, the prepare and check functions both return %TRUE if the
698 timeout interval has expired. The prepare function also returns a timeout 
699 value to ensure that the <function>poll()</function> call doesn't block too 
700 long and miss the next timeout.
701 </para>
702 <para>
703 For file descriptor sources, the prepare function typically returns %FALSE,
704 since it must wait until <function>poll()</function> has been called before 
705 it knows whether any events need to be processed. It sets the returned 
706 timeout to -1 to indicate that it doesn't mind how long the 
707 <function>poll()</function> call blocks.
708 In the check function, it tests the results of the <function>poll()</function>
709 call to see if the required condition has been met, and returns %TRUE if so.
710 </para>
711
712 @prepare: 
713 @check: 
714 @dispatch: 
715 @finalize: 
716 @closure_callback: 
717 @closure_marshal: 
718
719 <!-- ##### STRUCT GSourceCallbackFuncs ##### -->
720 <para>
721 The <structname>GSourceCallbackFuncs</structname> struct contains
722 functions for managing callback objects. 
723 </para>
724
725 @ref: Called when a reference is added to the callback object.
726 @unref: Called when a reference to the callback object is dropped.
727 @get: Called to extract the callback function and data from the callback object.
728
729 <!-- ##### FUNCTION g_source_new ##### -->
730 <para>
731
732 </para>
733
734 @source_funcs: 
735 @struct_size: 
736 @Returns: 
737
738
739 <!-- ##### FUNCTION g_source_ref ##### -->
740 <para>
741
742 </para>
743
744 @source: 
745 @Returns: 
746
747
748 <!-- ##### FUNCTION g_source_unref ##### -->
749 <para>
750
751 </para>
752
753 @source: 
754
755
756 <!-- ##### FUNCTION g_source_attach ##### -->
757 <para>
758
759 </para>
760
761 @source: 
762 @context: 
763 @Returns: 
764
765
766 <!-- ##### FUNCTION g_source_destroy ##### -->
767 <para>
768
769 </para>
770
771 @source: 
772
773
774 <!-- ##### FUNCTION g_source_set_priority ##### -->
775 <para>
776
777 </para>
778
779 @source: 
780 @priority: 
781
782
783 <!-- ##### FUNCTION g_source_get_priority ##### -->
784 <para>
785
786 </para>
787
788 @source: 
789 @Returns: 
790
791
792 <!-- ##### FUNCTION g_source_set_can_recurse ##### -->
793 <para>
794
795 </para>
796
797 @source: 
798 @can_recurse: 
799
800
801 <!-- ##### FUNCTION g_source_get_can_recurse ##### -->
802 <para>
803
804 </para>
805
806 @source: 
807 @Returns: 
808
809
810 <!-- ##### FUNCTION g_source_get_id ##### -->
811 <para>
812
813 </para>
814
815 @source: 
816 @Returns: 
817
818
819 <!-- ##### FUNCTION g_source_get_context ##### -->
820 <para>
821
822 </para>
823
824 @source: 
825 @Returns: 
826
827
828 <!-- ##### FUNCTION g_source_set_callback ##### -->
829 <para>
830
831 </para>
832
833 @source: 
834 @func: 
835 @data: 
836 @notify: 
837
838
839 <!-- ##### USER_FUNCTION GSourceFunc ##### -->
840 <para>
841 Specifies the type of function passed to g_timeout_add(), g_timeout_add_full(),
842 g_idle_add(), and g_idle_add_full().
843 </para>
844
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.
848
849
850 <!-- ##### FUNCTION g_source_set_callback_indirect ##### -->
851 <para>
852
853 </para>
854
855 @source: 
856 @callback_data: 
857 @callback_funcs: 
858
859
860 <!-- ##### FUNCTION g_source_add_poll ##### -->
861 <para>
862
863 </para>
864
865 @source: 
866 @fd: 
867
868
869 <!-- ##### FUNCTION g_source_remove_poll ##### -->
870 <para>
871
872 </para>
873
874 @source: 
875 @fd: 
876
877
878 <!-- ##### FUNCTION g_source_get_current_time ##### -->
879 <para>
880
881 </para>
882
883 @source: 
884 @timeval: 
885
886
887 <!-- ##### FUNCTION g_source_remove ##### -->
888 <para>
889 </para>
890
891 @tag: 
892 @Returns: 
893
894
895 <!-- ##### FUNCTION g_source_remove_by_funcs_user_data ##### -->
896 <para>
897 </para>
898
899 @funcs: 
900 @user_data: 
901 @Returns: 
902
903
904 <!-- ##### FUNCTION g_source_remove_by_user_data ##### -->
905 <para>
906 </para>
907
908 @user_data: 
909 @Returns: 
910
911 <!--
912 Local variables:
913 mode: sgml
914 sgml-parent-document: ("../glib-docs.sgml" "book" "refsect2" "")
915 End:
916 -->
917
918