558910a4724e77aa64d8231b71bfe94554263592
[platform/upstream/glib.git] / glib / gmain.h
1 /* gmain.h - the GLib Main loop
2  * Copyright (C) 1998-2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifndef __G_MAIN_H__
21 #define __G_MAIN_H__
22
23 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
24 #error "Only <glib.h> can be included directly."
25 #endif
26
27 #include <glib/gpoll.h>
28 #include <glib/gslist.h>
29 #include <glib/gthread.h>
30
31 G_BEGIN_DECLS
32
33 typedef enum /*< flags >*/
34 {
35   G_IO_IN       GLIB_SYSDEF_POLLIN,
36   G_IO_OUT      GLIB_SYSDEF_POLLOUT,
37   G_IO_PRI      GLIB_SYSDEF_POLLPRI,
38   G_IO_ERR      GLIB_SYSDEF_POLLERR,
39   G_IO_HUP      GLIB_SYSDEF_POLLHUP,
40   G_IO_NVAL     GLIB_SYSDEF_POLLNVAL
41 } GIOCondition;
42
43
44 /**
45  * GMainContext:
46  *
47  * The <structname>GMainContext</structname> struct is an opaque data
48  * type representing a set of sources to be handled in a main loop.
49  */
50 typedef struct _GMainContext            GMainContext;
51
52 /**
53  * GMainLoop:
54  *
55  * The <structname>GMainLoop</structname> struct is an opaque data type
56  * representing the main event loop of a GLib or GTK+ application.
57  */
58 typedef struct _GMainLoop               GMainLoop;
59
60 /**
61  * GSource:
62  *
63  * The <structname>GSource</structname> struct is an opaque data type
64  * representing an event source.
65  */
66 typedef struct _GSource                 GSource;
67 typedef struct _GSourcePrivate          GSourcePrivate;
68
69 /**
70  * GSourceCallbackFuncs:
71  * @ref: Called when a reference is added to the callback object
72  * @unref: Called when a reference to the callback object is dropped
73  * @get: Called to extract the callback function and data from the
74  *     callback object.
75
76  * The <structname>GSourceCallbackFuncs</structname> struct contains
77  * functions for managing callback objects.
78  */
79 typedef struct _GSourceCallbackFuncs    GSourceCallbackFuncs;
80
81 /**
82  * GSourceFuncs:
83  * @prepare: Called before all the file descriptors are polled. If the
84  *     source can determine that it is ready here (without waiting for the
85  *     results of the poll() call) it should return %TRUE. It can also return
86  *     a @timeout_ value which should be the maximum timeout (in milliseconds)
87  *     which should be passed to the poll() call. The actual timeout used will
88  *     be -1 if all sources returned -1, or it will be the minimum of all the
89  *     @timeout_ values returned which were >= 0.  Since 2.36 this may
90  *     be %NULL, in which case the effect is as if the function always
91  *     returns %FALSE with a timeout of -1.  If @prepare returns a
92  *     timeout and the source also has a 'ready time' set then the
93  *     nearer of the two will be used.
94  * @check: Called after all the file descriptors are polled. The source
95  *     should return %TRUE if it is ready to be dispatched. Note that some
96  *     time may have passed since the previous prepare function was called,
97  *     so the source should be checked again here.  Since 2.36 this may
98  *     be %NULL, in which case the effect is as if the function always
99  *     returns %FALSE.
100  * @dispatch: Called to dispatch the event source, after it has returned
101  *     %TRUE in either its @prepare or its @check function. The @dispatch
102  *     function is passed in a callback function and data. The callback
103  *     function may be %NULL if the source was never connected to a callback
104  *     using g_source_set_callback(). The @dispatch function should call the
105  *     callback function with @user_data and whatever additional parameters
106  *     are needed for this type of event source.
107  * @finalize: Called when the source is finalized.
108  *
109  * The <structname>GSourceFuncs</structname> struct contains a table of
110  * functions used to handle event sources in a generic manner.
111  *
112  * For idle sources, the prepare and check functions always return %TRUE
113  * to indicate that the source is always ready to be processed. The prepare
114  * function also returns a timeout value of 0 to ensure that the poll() call
115  * doesn't block (since that would be time wasted which could have been spent
116  * running the idle function).
117  *
118  * For timeout sources, the prepare and check functions both return %TRUE
119  * if the timeout interval has expired. The prepare function also returns
120  * a timeout value to ensure that the poll() call doesn't block too long
121  * and miss the next timeout.
122  *
123  * For file descriptor sources, the prepare function typically returns %FALSE,
124  * since it must wait until poll() has been called before it knows whether
125  * any events need to be processed. It sets the returned timeout to -1 to
126  * indicate that it doesn't mind how long the poll() call blocks. In the
127  * check function, it tests the results of the poll() call to see if the
128  * required condition has been met, and returns %TRUE if so.
129  */
130 typedef struct _GSourceFuncs            GSourceFuncs;
131
132 /**
133  * GPid:
134  *
135  * A type which is used to hold a process identification.
136  *
137  * On UNIX, processes are identified by a process id (an integer),
138  * while Windows uses process handles (which are pointers).
139  *
140  * GPid is used in GLib only for descendant processes spawned with
141  * the g_spawn functions.
142  */
143
144 /**
145  * GSourceFunc:
146  * @user_data: data passed to the function, set when the source was
147  *     created with one of the above functions
148  *
149  * Specifies the type of function passed to g_timeout_add(),
150  * g_timeout_add_full(), g_idle_add(), and g_idle_add_full().
151  *
152  * Returns: %FALSE if the source should be removed. #G_SOURCE_CONTINUE and
153  * #G_SOURCE_REMOVE are more memorable names for the return value.
154  */
155 typedef gboolean (*GSourceFunc)       (gpointer user_data);
156
157 /**
158  * GChildWatchFunc:
159  * @pid: the process id of the child process
160  * @status: Status information about the child process, encoded
161  *     in a platform-specific manner
162  * @user_data: user data passed to g_child_watch_add()
163  *
164  * Prototype of a #GChildWatchSource callback, called when a child
165  * process has exited.  To interpret @status, see the documentation
166  * for g_spawn_check_exit_status().
167  */
168 typedef void     (*GChildWatchFunc)   (GPid     pid,
169                                        gint     status,
170                                        gpointer user_data);
171 struct _GSource
172 {
173   /*< private >*/
174   gpointer callback_data;
175   GSourceCallbackFuncs *callback_funcs;
176
177   const GSourceFuncs *source_funcs;
178   guint ref_count;
179
180   GMainContext *context;
181
182   gint priority;
183   guint flags;
184   guint source_id;
185
186   GSList *poll_fds;
187   
188   GSource *prev;
189   GSource *next;
190
191   char    *name;
192
193   GSourcePrivate *priv;
194 };
195
196 struct _GSourceCallbackFuncs
197 {
198   void (*ref)   (gpointer     cb_data);
199   void (*unref) (gpointer     cb_data);
200   void (*get)   (gpointer     cb_data,
201                  GSource     *source, 
202                  GSourceFunc *func,
203                  gpointer    *data);
204 };
205
206 /**
207  * GSourceDummyMarshal:
208  *
209  * This is just a placeholder for #GClosureMarshal,
210  * which cannot be used here for dependency reasons.
211  */
212 typedef void (*GSourceDummyMarshal) (void);
213
214 struct _GSourceFuncs
215 {
216   gboolean (*prepare)  (GSource    *source,
217                         gint       *timeout_);
218   gboolean (*check)    (GSource    *source);
219   gboolean (*dispatch) (GSource    *source,
220                         GSourceFunc callback,
221                         gpointer    user_data);
222   void     (*finalize) (GSource    *source); /* Can be NULL */
223
224   /*< private >*/
225   /* For use by g_source_set_closure */
226   GSourceFunc     closure_callback;        
227   GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
228 };
229
230 /* Standard priorities */
231
232 /**
233  * G_PRIORITY_HIGH:
234  *
235  * Use this for high priority event sources.
236  *
237  * It is not used within GLib or GTK+.
238  */
239 #define G_PRIORITY_HIGH            -100
240
241 /**
242  * G_PRIORITY_DEFAULT:
243  *
244  * Use this for default priority event sources.
245  *
246  * In GLib this priority is used when adding timeout functions
247  * with g_timeout_add(). In GDK this priority is used for events
248  * from the X server.
249  */
250 #define G_PRIORITY_DEFAULT          0
251
252 /**
253  * G_PRIORITY_HIGH_IDLE:
254  *
255  * Use this for high priority idle functions.
256  *
257  * GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
258  * and #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
259  * done to ensure that any pending resizes are processed before any
260  * pending redraws, so that widgets are not redrawn twice unnecessarily.)
261  */
262 #define G_PRIORITY_HIGH_IDLE        100
263
264 /**
265  * G_PRIORITY_DEFAULT_IDLE:
266  *
267  * Use this for default priority idle functions.
268  *
269  * In GLib this priority is used when adding idle functions with
270  * g_idle_add().
271  */
272 #define G_PRIORITY_DEFAULT_IDLE     200
273
274 /**
275  * G_PRIORITY_LOW:
276  *
277  * Use this for very low priority background tasks.
278  *
279  * It is not used within GLib or GTK+.
280  */
281 #define G_PRIORITY_LOW              300
282
283 /**
284  * G_SOURCE_REMOVE:
285  *
286  * Use this macro as the return value of a #GSourceFunc to remove
287  * the #GSource from the main loop.
288  *
289  * Since: 2.32
290  */
291 #define G_SOURCE_REMOVE         FALSE
292
293 /**
294  * G_SOURCE_CONTINUE:
295  *
296  * Use this macro as the return value of a #GSourceFunc to leave
297  * the #GSource in the main loop.
298  *
299  * Since: 2.32
300  */
301 #define G_SOURCE_CONTINUE       TRUE
302
303 /* GMainContext: */
304
305 GLIB_AVAILABLE_IN_ALL
306 GMainContext *g_main_context_new       (void);
307 GLIB_AVAILABLE_IN_ALL
308 GMainContext *g_main_context_ref       (GMainContext *context);
309 GLIB_AVAILABLE_IN_ALL
310 void          g_main_context_unref     (GMainContext *context);
311 GLIB_AVAILABLE_IN_ALL
312 GMainContext *g_main_context_default   (void);
313
314 GLIB_AVAILABLE_IN_ALL
315 gboolean      g_main_context_iteration (GMainContext *context,
316                                         gboolean      may_block);
317 GLIB_AVAILABLE_IN_ALL
318 gboolean      g_main_context_pending   (GMainContext *context);
319
320 /* For implementation of legacy interfaces
321  */
322 GLIB_AVAILABLE_IN_ALL
323 GSource      *g_main_context_find_source_by_id              (GMainContext *context,
324                                                              guint         source_id);
325 GLIB_AVAILABLE_IN_ALL
326 GSource      *g_main_context_find_source_by_user_data       (GMainContext *context,
327                                                              gpointer      user_data);
328 GLIB_AVAILABLE_IN_ALL
329 GSource      *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
330                                                              GSourceFuncs *funcs,
331                                                              gpointer      user_data);
332
333 /* Low level functions for implementing custom main loops.
334  */
335 GLIB_AVAILABLE_IN_ALL
336 void     g_main_context_wakeup  (GMainContext *context);
337 GLIB_AVAILABLE_IN_ALL
338 gboolean g_main_context_acquire (GMainContext *context);
339 GLIB_AVAILABLE_IN_ALL
340 void     g_main_context_release (GMainContext *context);
341 GLIB_AVAILABLE_IN_ALL
342 gboolean g_main_context_is_owner (GMainContext *context);
343 GLIB_AVAILABLE_IN_ALL
344 gboolean g_main_context_wait    (GMainContext *context,
345                                  GCond        *cond,
346                                  GMutex       *mutex);
347
348 GLIB_AVAILABLE_IN_ALL
349 gboolean g_main_context_prepare  (GMainContext *context,
350                                   gint         *priority);
351 GLIB_AVAILABLE_IN_ALL
352 gint     g_main_context_query    (GMainContext *context,
353                                   gint          max_priority,
354                                   gint         *timeout_,
355                                   GPollFD      *fds,
356                                   gint          n_fds);
357 GLIB_AVAILABLE_IN_ALL
358 gint     g_main_context_check    (GMainContext *context,
359                                   gint          max_priority,
360                                   GPollFD      *fds,
361                                   gint          n_fds);
362 GLIB_AVAILABLE_IN_ALL
363 void     g_main_context_dispatch (GMainContext *context);
364
365 GLIB_AVAILABLE_IN_ALL
366 void     g_main_context_set_poll_func (GMainContext *context,
367                                        GPollFunc     func);
368 GLIB_AVAILABLE_IN_ALL
369 GPollFunc g_main_context_get_poll_func (GMainContext *context);
370
371 /* Low level functions for use by source implementations
372  */
373 GLIB_AVAILABLE_IN_ALL
374 void     g_main_context_add_poll    (GMainContext *context,
375                                      GPollFD      *fd,
376                                      gint          priority);
377 GLIB_AVAILABLE_IN_ALL
378 void     g_main_context_remove_poll (GMainContext *context,
379                                      GPollFD      *fd);
380
381 GLIB_AVAILABLE_IN_ALL
382 gint     g_main_depth               (void);
383 GLIB_AVAILABLE_IN_ALL
384 GSource *g_main_current_source      (void);
385
386 /* GMainContexts for other threads
387  */
388 GLIB_AVAILABLE_IN_ALL
389 void          g_main_context_push_thread_default (GMainContext *context);
390 GLIB_AVAILABLE_IN_ALL
391 void          g_main_context_pop_thread_default  (GMainContext *context);
392 GLIB_AVAILABLE_IN_ALL
393 GMainContext *g_main_context_get_thread_default  (void);
394 GLIB_AVAILABLE_IN_ALL
395 GMainContext *g_main_context_ref_thread_default  (void);
396
397 /* GMainLoop: */
398
399 GLIB_AVAILABLE_IN_ALL
400 GMainLoop *g_main_loop_new        (GMainContext *context,
401                                    gboolean      is_running);
402 GLIB_AVAILABLE_IN_ALL
403 void       g_main_loop_run        (GMainLoop    *loop);
404 GLIB_AVAILABLE_IN_ALL
405 void       g_main_loop_quit       (GMainLoop    *loop);
406 GLIB_AVAILABLE_IN_ALL
407 GMainLoop *g_main_loop_ref        (GMainLoop    *loop);
408 GLIB_AVAILABLE_IN_ALL
409 void       g_main_loop_unref      (GMainLoop    *loop);
410 GLIB_AVAILABLE_IN_ALL
411 gboolean   g_main_loop_is_running (GMainLoop    *loop);
412 GLIB_AVAILABLE_IN_ALL
413 GMainContext *g_main_loop_get_context (GMainLoop    *loop);
414
415 /* GSource: */
416
417 GLIB_AVAILABLE_IN_ALL
418 GSource *g_source_new             (GSourceFuncs   *source_funcs,
419                                    guint           struct_size);
420 GLIB_AVAILABLE_IN_ALL
421 GSource *g_source_ref             (GSource        *source);
422 GLIB_AVAILABLE_IN_ALL
423 void     g_source_unref           (GSource        *source);
424
425 GLIB_AVAILABLE_IN_ALL
426 guint    g_source_attach          (GSource        *source,
427                                    GMainContext   *context);
428 GLIB_AVAILABLE_IN_ALL
429 void     g_source_destroy         (GSource        *source);
430
431 GLIB_AVAILABLE_IN_ALL
432 void     g_source_set_priority    (GSource        *source,
433                                    gint            priority);
434 GLIB_AVAILABLE_IN_ALL
435 gint     g_source_get_priority    (GSource        *source);
436 GLIB_AVAILABLE_IN_ALL
437 void     g_source_set_can_recurse (GSource        *source,
438                                    gboolean        can_recurse);
439 GLIB_AVAILABLE_IN_ALL
440 gboolean g_source_get_can_recurse (GSource        *source);
441 GLIB_AVAILABLE_IN_ALL
442 guint    g_source_get_id          (GSource        *source);
443
444 GLIB_AVAILABLE_IN_ALL
445 GMainContext *g_source_get_context (GSource       *source);
446
447 GLIB_AVAILABLE_IN_ALL
448 void     g_source_set_callback    (GSource        *source,
449                                    GSourceFunc     func,
450                                    gpointer        data,
451                                    GDestroyNotify  notify);
452
453 GLIB_AVAILABLE_IN_ALL
454 void     g_source_set_funcs       (GSource        *source,
455                                    GSourceFuncs   *funcs);
456 GLIB_AVAILABLE_IN_ALL
457 gboolean g_source_is_destroyed    (GSource        *source);
458
459 GLIB_AVAILABLE_IN_ALL
460 void                 g_source_set_name       (GSource        *source,
461                                               const char     *name);
462 GLIB_AVAILABLE_IN_ALL
463 const char *         g_source_get_name       (GSource        *source);
464 GLIB_AVAILABLE_IN_ALL
465 void                 g_source_set_name_by_id (guint           tag,
466                                               const char     *name);
467
468 GLIB_AVAILABLE_IN_2_36
469 void                 g_source_set_ready_time (GSource        *source,
470                                               gint64          ready_time);
471 GLIB_AVAILABLE_IN_2_36
472 gint64               g_source_get_ready_time (GSource        *source);
473
474 #ifdef G_OS_UNIX
475 GLIB_AVAILABLE_IN_2_36
476 gpointer             g_source_add_unix_fd    (GSource        *source,
477                                               gint            fd,
478                                               GIOCondition    events);
479 GLIB_AVAILABLE_IN_2_36
480 void                 g_source_modify_unix_fd (GSource        *source,
481                                               gpointer        tag,
482                                               GIOCondition    new_events);
483 GLIB_AVAILABLE_IN_2_36
484 void                 g_source_remove_unix_fd (GSource        *source,
485                                               gpointer        tag);
486 GLIB_AVAILABLE_IN_2_36
487 GIOCondition         g_source_query_unix_fd  (GSource        *source,
488                                               gpointer        tag);
489 #endif
490
491 /* Used to implement g_source_connect_closure and internally*/
492 GLIB_AVAILABLE_IN_ALL
493 void g_source_set_callback_indirect (GSource              *source,
494                                      gpointer              callback_data,
495                                      GSourceCallbackFuncs *callback_funcs);
496
497 GLIB_AVAILABLE_IN_ALL
498 void     g_source_add_poll            (GSource        *source,
499                                        GPollFD        *fd);
500 GLIB_AVAILABLE_IN_ALL
501 void     g_source_remove_poll         (GSource        *source,
502                                        GPollFD        *fd);
503
504 GLIB_AVAILABLE_IN_ALL
505 void     g_source_add_child_source    (GSource        *source,
506                                        GSource        *child_source);
507 GLIB_AVAILABLE_IN_ALL
508 void     g_source_remove_child_source (GSource        *source,
509                                        GSource        *child_source);
510
511 GLIB_DEPRECATED_IN_2_28_FOR(g_source_get_time)
512 void     g_source_get_current_time (GSource        *source,
513                                     GTimeVal       *timeval);
514
515 GLIB_AVAILABLE_IN_ALL
516 gint64   g_source_get_time         (GSource        *source);
517
518  /* void g_source_connect_closure (GSource        *source,
519                                   GClosure       *closure);
520  */
521
522 /* Specific source types
523  */
524 GLIB_AVAILABLE_IN_ALL
525 GSource *g_idle_source_new        (void);
526 GLIB_AVAILABLE_IN_ALL
527 GSource *g_child_watch_source_new (GPid pid);
528 GLIB_AVAILABLE_IN_ALL
529 GSource *g_timeout_source_new     (guint interval);
530 GLIB_AVAILABLE_IN_ALL
531 GSource *g_timeout_source_new_seconds (guint interval);
532
533 /* Miscellaneous functions
534  */
535 GLIB_AVAILABLE_IN_ALL
536 void   g_get_current_time                 (GTimeVal       *result);
537 GLIB_AVAILABLE_IN_ALL
538 gint64 g_get_monotonic_time               (void);
539 GLIB_AVAILABLE_IN_ALL
540 gint64 g_get_real_time                    (void);
541
542
543 /* Source manipulation by ID */
544 GLIB_AVAILABLE_IN_ALL
545 gboolean g_source_remove                     (guint          tag);
546 GLIB_AVAILABLE_IN_ALL
547 gboolean g_source_remove_by_user_data        (gpointer       user_data);
548 GLIB_AVAILABLE_IN_ALL
549 gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
550                                               gpointer       user_data);
551
552 /* Idles, child watchers and timeouts */
553 GLIB_AVAILABLE_IN_ALL
554 guint    g_timeout_add_full         (gint            priority,
555                                      guint           interval,
556                                      GSourceFunc     function,
557                                      gpointer        data,
558                                      GDestroyNotify  notify);
559 GLIB_AVAILABLE_IN_ALL
560 guint    g_timeout_add              (guint           interval,
561                                      GSourceFunc     function,
562                                      gpointer        data);
563 GLIB_AVAILABLE_IN_ALL
564 guint    g_timeout_add_seconds_full (gint            priority,
565                                      guint           interval,
566                                      GSourceFunc     function,
567                                      gpointer        data,
568                                      GDestroyNotify  notify);
569 GLIB_AVAILABLE_IN_ALL
570 guint    g_timeout_add_seconds      (guint           interval,
571                                      GSourceFunc     function,
572                                      gpointer        data);
573 GLIB_AVAILABLE_IN_ALL
574 guint    g_child_watch_add_full     (gint            priority,
575                                      GPid            pid,
576                                      GChildWatchFunc function,
577                                      gpointer        data,
578                                      GDestroyNotify  notify);
579 GLIB_AVAILABLE_IN_ALL
580 guint    g_child_watch_add          (GPid            pid,
581                                      GChildWatchFunc function,
582                                      gpointer        data);
583 GLIB_AVAILABLE_IN_ALL
584 guint    g_idle_add                 (GSourceFunc     function,
585                                      gpointer        data);
586 GLIB_AVAILABLE_IN_ALL
587 guint    g_idle_add_full            (gint            priority,
588                                      GSourceFunc     function,
589                                      gpointer        data,
590                                      GDestroyNotify  notify);
591 GLIB_AVAILABLE_IN_ALL
592 gboolean g_idle_remove_by_data      (gpointer        data);
593
594 GLIB_AVAILABLE_IN_ALL
595 void     g_main_context_invoke_full (GMainContext   *context,
596                                      gint            priority,
597                                      GSourceFunc     function,
598                                      gpointer        data,
599                                      GDestroyNotify  notify);
600 GLIB_AVAILABLE_IN_ALL
601 void     g_main_context_invoke      (GMainContext   *context,
602                                      GSourceFunc     function,
603                                      gpointer        data);
604
605 /* Hook for GClosure / GSource integration. Don't touch */
606 GLIB_VAR GSourceFuncs g_timeout_funcs;
607 GLIB_VAR GSourceFuncs g_child_watch_funcs;
608 GLIB_VAR GSourceFuncs g_idle_funcs;
609 #ifdef G_OS_UNIX
610 GLIB_VAR GSourceFuncs g_unix_signal_funcs;
611 GLIB_VAR GSourceFuncs g_unix_fd_source_funcs;
612 #endif
613
614 G_END_DECLS
615
616 #endif /* __G_MAIN_H__ */