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