Fix the GObject Visual Studio Projects
[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
153  */
154 typedef gboolean (*GSourceFunc)       (gpointer user_data);
155
156 /**
157  * GChildWatchFunc:
158  * @pid: the process id of the child process
159  * @status: Status information about the child process, encoded
160  *     in a platform-specific manner
161  * @user_data: user data passed to g_child_watch_add()
162  *
163  * Prototype of a #GChildWatchSource callback, called when a child
164  * process has exited.  To interpret @status, see the documentation
165  * for g_spawn_check_exit_status().
166  */
167 typedef void     (*GChildWatchFunc)   (GPid     pid,
168                                        gint     status,
169                                        gpointer user_data);
170 struct _GSource
171 {
172   /*< private >*/
173   gpointer callback_data;
174   GSourceCallbackFuncs *callback_funcs;
175
176   const GSourceFuncs *source_funcs;
177   guint ref_count;
178
179   GMainContext *context;
180
181   gint priority;
182   guint flags;
183   guint source_id;
184
185   GSList *poll_fds;
186   
187   GSource *prev;
188   GSource *next;
189
190   char    *name;
191
192   GSourcePrivate *priv;
193 };
194
195 struct _GSourceCallbackFuncs
196 {
197   void (*ref)   (gpointer     cb_data);
198   void (*unref) (gpointer     cb_data);
199   void (*get)   (gpointer     cb_data,
200                  GSource     *source, 
201                  GSourceFunc *func,
202                  gpointer    *data);
203 };
204
205 /**
206  * GSourceDummyMarshal:
207  *
208  * This is just a placeholder for #GClosureMarshal,
209  * which cannot be used here for dependency reasons.
210  */
211 typedef void (*GSourceDummyMarshal) (void);
212
213 struct _GSourceFuncs
214 {
215   gboolean (*prepare)  (GSource    *source,
216                         gint       *timeout_);
217   gboolean (*check)    (GSource    *source);
218   gboolean (*dispatch) (GSource    *source,
219                         GSourceFunc callback,
220                         gpointer    user_data);
221   void     (*finalize) (GSource    *source); /* Can be NULL */
222
223   /*< private >*/
224   /* For use by g_source_set_closure */
225   GSourceFunc     closure_callback;        
226   GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
227 };
228
229 /* Standard priorities */
230
231 /**
232  * G_PRIORITY_HIGH:
233  *
234  * Use this for high priority event sources.
235  *
236  * It is not used within GLib or GTK+.
237  */
238 #define G_PRIORITY_HIGH            -100
239
240 /**
241  * G_PRIORITY_DEFAULT:
242  *
243  * Use this for default priority event sources.
244  *
245  * In GLib this priority is used when adding timeout functions
246  * with g_timeout_add(). In GDK this priority is used for events
247  * from the X server.
248  */
249 #define G_PRIORITY_DEFAULT          0
250
251 /**
252  * G_PRIORITY_HIGH_IDLE:
253  *
254  * Use this for high priority idle functions.
255  *
256  * GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
257  * and #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
258  * done to ensure that any pending resizes are processed before any
259  * pending redraws, so that widgets are not redrawn twice unnecessarily.)
260  */
261 #define G_PRIORITY_HIGH_IDLE        100
262
263 /**
264  * G_PRIORITY_DEFAULT_IDLE:
265  *
266  * Use this for default priority idle functions.
267  *
268  * In GLib this priority is used when adding idle functions with
269  * g_idle_add().
270  */
271 #define G_PRIORITY_DEFAULT_IDLE     200
272
273 /**
274  * G_PRIORITY_LOW:
275  *
276  * Use this for very low priority background tasks.
277  *
278  * It is not used within GLib or GTK+.
279  */
280 #define G_PRIORITY_LOW              300
281
282 /**
283  * G_SOURCE_REMOVE:
284  *
285  * Use this macro as the return value of a #GSourceFunc to remove
286  * the #GSource from the main loop.
287  *
288  * Since: 2.32
289  */
290 #define G_SOURCE_REMOVE         FALSE
291
292 /**
293  * G_SOURCE_CONTINUE:
294  *
295  * Use this macro as the return value of a #GSourceFunc to leave
296  * the #GSource in the main loop.
297  *
298  * Since: 2.32
299  */
300 #define G_SOURCE_CONTINUE       TRUE
301
302 /* GMainContext: */
303
304 GLIB_AVAILABLE_IN_ALL
305 GMainContext *g_main_context_new       (void);
306 GLIB_AVAILABLE_IN_ALL
307 GMainContext *g_main_context_ref       (GMainContext *context);
308 GLIB_AVAILABLE_IN_ALL
309 void          g_main_context_unref     (GMainContext *context);
310 GLIB_AVAILABLE_IN_ALL
311 GMainContext *g_main_context_default   (void);
312
313 GLIB_AVAILABLE_IN_ALL
314 gboolean      g_main_context_iteration (GMainContext *context,
315                                         gboolean      may_block);
316 GLIB_AVAILABLE_IN_ALL
317 gboolean      g_main_context_pending   (GMainContext *context);
318
319 /* For implementation of legacy interfaces
320  */
321 GLIB_AVAILABLE_IN_ALL
322 GSource      *g_main_context_find_source_by_id              (GMainContext *context,
323                                                              guint         source_id);
324 GLIB_AVAILABLE_IN_ALL
325 GSource      *g_main_context_find_source_by_user_data       (GMainContext *context,
326                                                              gpointer      user_data);
327 GLIB_AVAILABLE_IN_ALL
328 GSource      *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
329                                                              GSourceFuncs *funcs,
330                                                              gpointer      user_data);
331
332 /* Low level functions for implementing custom main loops.
333  */
334 GLIB_AVAILABLE_IN_ALL
335 void     g_main_context_wakeup  (GMainContext *context);
336 GLIB_AVAILABLE_IN_ALL
337 gboolean g_main_context_acquire (GMainContext *context);
338 GLIB_AVAILABLE_IN_ALL
339 void     g_main_context_release (GMainContext *context);
340 GLIB_AVAILABLE_IN_ALL
341 gboolean g_main_context_is_owner (GMainContext *context);
342 GLIB_AVAILABLE_IN_ALL
343 gboolean g_main_context_wait    (GMainContext *context,
344                                  GCond        *cond,
345                                  GMutex       *mutex);
346
347 GLIB_AVAILABLE_IN_ALL
348 gboolean g_main_context_prepare  (GMainContext *context,
349                                   gint         *priority);
350 GLIB_AVAILABLE_IN_ALL
351 gint     g_main_context_query    (GMainContext *context,
352                                   gint          max_priority,
353                                   gint         *timeout_,
354                                   GPollFD      *fds,
355                                   gint          n_fds);
356 GLIB_AVAILABLE_IN_ALL
357 gint     g_main_context_check    (GMainContext *context,
358                                   gint          max_priority,
359                                   GPollFD      *fds,
360                                   gint          n_fds);
361 GLIB_AVAILABLE_IN_ALL
362 void     g_main_context_dispatch (GMainContext *context);
363
364 GLIB_AVAILABLE_IN_ALL
365 void     g_main_context_set_poll_func (GMainContext *context,
366                                        GPollFunc     func);
367 GLIB_AVAILABLE_IN_ALL
368 GPollFunc g_main_context_get_poll_func (GMainContext *context);
369
370 /* Low level functions for use by source implementations
371  */
372 GLIB_AVAILABLE_IN_ALL
373 void     g_main_context_add_poll    (GMainContext *context,
374                                      GPollFD      *fd,
375                                      gint          priority);
376 GLIB_AVAILABLE_IN_ALL
377 void     g_main_context_remove_poll (GMainContext *context,
378                                      GPollFD      *fd);
379
380 GLIB_AVAILABLE_IN_ALL
381 gint     g_main_depth               (void);
382 GLIB_AVAILABLE_IN_ALL
383 GSource *g_main_current_source      (void);
384
385 /* GMainContexts for other threads
386  */
387 GLIB_AVAILABLE_IN_ALL
388 void          g_main_context_push_thread_default (GMainContext *context);
389 GLIB_AVAILABLE_IN_ALL
390 void          g_main_context_pop_thread_default  (GMainContext *context);
391 GLIB_AVAILABLE_IN_ALL
392 GMainContext *g_main_context_get_thread_default  (void);
393 GLIB_AVAILABLE_IN_ALL
394 GMainContext *g_main_context_ref_thread_default  (void);
395
396 /* GMainLoop: */
397
398 GLIB_AVAILABLE_IN_ALL
399 GMainLoop *g_main_loop_new        (GMainContext *context,
400                                    gboolean      is_running);
401 GLIB_AVAILABLE_IN_ALL
402 void       g_main_loop_run        (GMainLoop    *loop);
403 GLIB_AVAILABLE_IN_ALL
404 void       g_main_loop_quit       (GMainLoop    *loop);
405 GLIB_AVAILABLE_IN_ALL
406 GMainLoop *g_main_loop_ref        (GMainLoop    *loop);
407 GLIB_AVAILABLE_IN_ALL
408 void       g_main_loop_unref      (GMainLoop    *loop);
409 GLIB_AVAILABLE_IN_ALL
410 gboolean   g_main_loop_is_running (GMainLoop    *loop);
411 GLIB_AVAILABLE_IN_ALL
412 GMainContext *g_main_loop_get_context (GMainLoop    *loop);
413
414 /* GSource: */
415
416 GLIB_AVAILABLE_IN_ALL
417 GSource *g_source_new             (GSourceFuncs   *source_funcs,
418                                    guint           struct_size);
419 GLIB_AVAILABLE_IN_ALL
420 GSource *g_source_ref             (GSource        *source);
421 GLIB_AVAILABLE_IN_ALL
422 void     g_source_unref           (GSource        *source);
423
424 GLIB_AVAILABLE_IN_ALL
425 guint    g_source_attach          (GSource        *source,
426                                    GMainContext   *context);
427 GLIB_AVAILABLE_IN_ALL
428 void     g_source_destroy         (GSource        *source);
429
430 GLIB_AVAILABLE_IN_ALL
431 void     g_source_set_priority    (GSource        *source,
432                                    gint            priority);
433 GLIB_AVAILABLE_IN_ALL
434 gint     g_source_get_priority    (GSource        *source);
435 GLIB_AVAILABLE_IN_ALL
436 void     g_source_set_can_recurse (GSource        *source,
437                                    gboolean        can_recurse);
438 GLIB_AVAILABLE_IN_ALL
439 gboolean g_source_get_can_recurse (GSource        *source);
440 GLIB_AVAILABLE_IN_ALL
441 guint    g_source_get_id          (GSource        *source);
442
443 GLIB_AVAILABLE_IN_ALL
444 GMainContext *g_source_get_context (GSource       *source);
445
446 GLIB_AVAILABLE_IN_ALL
447 void     g_source_set_callback    (GSource        *source,
448                                    GSourceFunc     func,
449                                    gpointer        data,
450                                    GDestroyNotify  notify);
451
452 GLIB_AVAILABLE_IN_ALL
453 void     g_source_set_funcs       (GSource        *source,
454                                    GSourceFuncs   *funcs);
455 GLIB_AVAILABLE_IN_ALL
456 gboolean g_source_is_destroyed    (GSource        *source);
457
458 GLIB_AVAILABLE_IN_ALL
459 void                 g_source_set_name       (GSource        *source,
460                                               const char     *name);
461 GLIB_AVAILABLE_IN_ALL
462 const char *         g_source_get_name       (GSource        *source);
463 GLIB_AVAILABLE_IN_ALL
464 void                 g_source_set_name_by_id (guint           tag,
465                                               const char     *name);
466
467 GLIB_AVAILABLE_IN_2_36
468 void                 g_source_set_ready_time (GSource        *source,
469                                               gint64          ready_time);
470 GLIB_AVAILABLE_IN_2_36
471 gint64               g_source_get_ready_time (GSource        *source);
472
473 #ifdef G_OS_UNIX
474 GLIB_AVAILABLE_IN_2_36
475 gpointer             g_source_add_unix_fd    (GSource        *source,
476                                               gint            fd,
477                                               GIOCondition    events);
478 GLIB_AVAILABLE_IN_2_36
479 void                 g_source_modify_unix_fd (GSource        *source,
480                                               gpointer        tag,
481                                               GIOCondition    new_events);
482 GLIB_AVAILABLE_IN_2_36
483 void                 g_source_remove_unix_fd (GSource        *source,
484                                               gpointer        tag);
485 GLIB_AVAILABLE_IN_2_36
486 GIOCondition         g_source_query_unix_fd  (GSource        *source,
487                                               gpointer        tag);
488 #endif
489
490 /* Used to implement g_source_connect_closure and internally*/
491 GLIB_AVAILABLE_IN_ALL
492 void g_source_set_callback_indirect (GSource              *source,
493                                      gpointer              callback_data,
494                                      GSourceCallbackFuncs *callback_funcs);
495
496 GLIB_AVAILABLE_IN_ALL
497 void     g_source_add_poll            (GSource        *source,
498                                        GPollFD        *fd);
499 GLIB_AVAILABLE_IN_ALL
500 void     g_source_remove_poll         (GSource        *source,
501                                        GPollFD        *fd);
502
503 GLIB_AVAILABLE_IN_ALL
504 void     g_source_add_child_source    (GSource        *source,
505                                        GSource        *child_source);
506 GLIB_AVAILABLE_IN_ALL
507 void     g_source_remove_child_source (GSource        *source,
508                                        GSource        *child_source);
509
510 GLIB_DEPRECATED_IN_2_28_FOR(g_source_get_time)
511 void     g_source_get_current_time (GSource        *source,
512                                     GTimeVal       *timeval);
513
514 GLIB_AVAILABLE_IN_ALL
515 gint64   g_source_get_time         (GSource        *source);
516
517  /* void g_source_connect_closure (GSource        *source,
518                                   GClosure       *closure);
519  */
520
521 /* Specific source types
522  */
523 GLIB_AVAILABLE_IN_ALL
524 GSource *g_idle_source_new        (void);
525 GLIB_AVAILABLE_IN_ALL
526 GSource *g_child_watch_source_new (GPid pid);
527 GLIB_AVAILABLE_IN_ALL
528 GSource *g_timeout_source_new     (guint interval);
529 GLIB_AVAILABLE_IN_ALL
530 GSource *g_timeout_source_new_seconds (guint interval);
531
532 /* Miscellaneous functions
533  */
534 GLIB_AVAILABLE_IN_ALL
535 void   g_get_current_time                 (GTimeVal       *result);
536 GLIB_AVAILABLE_IN_ALL
537 gint64 g_get_monotonic_time               (void);
538 GLIB_AVAILABLE_IN_ALL
539 gint64 g_get_real_time                    (void);
540
541
542 /* Source manipulation by ID */
543 GLIB_AVAILABLE_IN_ALL
544 gboolean g_source_remove                     (guint          tag);
545 GLIB_AVAILABLE_IN_ALL
546 gboolean g_source_remove_by_user_data        (gpointer       user_data);
547 GLIB_AVAILABLE_IN_ALL
548 gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
549                                               gpointer       user_data);
550
551 /* Idles, child watchers and timeouts */
552 GLIB_AVAILABLE_IN_ALL
553 guint    g_timeout_add_full         (gint            priority,
554                                      guint           interval,
555                                      GSourceFunc     function,
556                                      gpointer        data,
557                                      GDestroyNotify  notify);
558 GLIB_AVAILABLE_IN_ALL
559 guint    g_timeout_add              (guint           interval,
560                                      GSourceFunc     function,
561                                      gpointer        data);
562 GLIB_AVAILABLE_IN_ALL
563 guint    g_timeout_add_seconds_full (gint            priority,
564                                      guint           interval,
565                                      GSourceFunc     function,
566                                      gpointer        data,
567                                      GDestroyNotify  notify);
568 GLIB_AVAILABLE_IN_ALL
569 guint    g_timeout_add_seconds      (guint           interval,
570                                      GSourceFunc     function,
571                                      gpointer        data);
572 GLIB_AVAILABLE_IN_ALL
573 guint    g_child_watch_add_full     (gint            priority,
574                                      GPid            pid,
575                                      GChildWatchFunc function,
576                                      gpointer        data,
577                                      GDestroyNotify  notify);
578 GLIB_AVAILABLE_IN_ALL
579 guint    g_child_watch_add          (GPid            pid,
580                                      GChildWatchFunc function,
581                                      gpointer        data);
582 GLIB_AVAILABLE_IN_ALL
583 guint    g_idle_add                 (GSourceFunc     function,
584                                      gpointer        data);
585 GLIB_AVAILABLE_IN_ALL
586 guint    g_idle_add_full            (gint            priority,
587                                      GSourceFunc     function,
588                                      gpointer        data,
589                                      GDestroyNotify  notify);
590 GLIB_AVAILABLE_IN_ALL
591 gboolean g_idle_remove_by_data      (gpointer        data);
592
593 GLIB_AVAILABLE_IN_ALL
594 void     g_main_context_invoke_full (GMainContext   *context,
595                                      gint            priority,
596                                      GSourceFunc     function,
597                                      gpointer        data,
598                                      GDestroyNotify  notify);
599 GLIB_AVAILABLE_IN_ALL
600 void     g_main_context_invoke      (GMainContext   *context,
601                                      GSourceFunc     function,
602                                      gpointer        data);
603
604 /* Hook for GClosure / GSource integration. Don't touch */
605 GLIB_VAR GSourceFuncs g_timeout_funcs;
606 GLIB_VAR GSourceFuncs g_child_watch_funcs;
607 GLIB_VAR GSourceFuncs g_idle_funcs;
608
609 G_END_DECLS
610
611 #endif /* __G_MAIN_H__ */