regex: Remove obsolete patch
[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  *
92  * The <structname>GSourceFuncs</structname> struct contains a table of
93  * functions used to handle event sources in a generic manner.
94  *
95  * For idle sources, the prepare and check functions always return %TRUE
96  * to indicate that the source is always ready to be processed. The prepare
97  * function also returns a timeout value of 0 to ensure that the poll() call
98  * doesn't block (since that would be time wasted which could have been spent
99  * running the idle function).
100  *
101  * For timeout sources, the prepare and check functions both return %TRUE
102  * if the timeout interval has expired. The prepare function also returns
103  * a timeout value to ensure that the poll() call doesn't block too long
104  * and miss the next timeout.
105  *
106  * For file descriptor sources, the prepare function typically returns %FALSE,
107  * since it must wait until poll() has been called before it knows whether
108  * any events need to be processed. It sets the returned timeout to -1 to
109  * indicate that it doesn't mind how long the poll() call blocks. In the
110  * check function, it tests the results of the poll() call to see if the
111  * required condition has been met, and returns %TRUE if so.
112  */
113 typedef struct _GSourceFuncs            GSourceFuncs;
114
115 /**
116  * GPid:
117  *
118  * A type which is used to hold a process identification.
119  *
120  * On UNIX, processes are identified by a process id (an integer),
121  * while Windows uses process handles (which are pointers).
122  *
123  * GPid is used in GLib only for descendant processes spawned with
124  * the g_spawn functions.
125  */
126
127 /**
128  * GSourceFunc:
129  * @user_data: data passed to the function, set when the source was
130  *     created with one of the above functions
131  *
132  * Specifies the type of function passed to g_timeout_add(),
133  * g_timeout_add_full(), g_idle_add(), and g_idle_add_full().
134  *
135  * Returns: %FALSE if the source should be removed
136  */
137 typedef gboolean (*GSourceFunc)       (gpointer user_data);
138
139 /**
140  * GChildWatchFunc:
141  * @pid: the process id of the child process
142  * @status: Status information about the child process,
143  *     see waitpid(2) for more information about this field
144  * @user_data: user data passed to g_child_watch_add()
145  *
146  * The type of functions to be called when a child exists.
147  */
148 typedef void     (*GChildWatchFunc)   (GPid     pid,
149                                        gint     status,
150                                        gpointer user_data);
151 struct _GSource
152 {
153   /*< private >*/
154   gpointer callback_data;
155   GSourceCallbackFuncs *callback_funcs;
156
157   GSourceFuncs *source_funcs;
158   guint ref_count;
159
160   GMainContext *context;
161
162   gint priority;
163   guint flags;
164   guint source_id;
165
166   GSList *poll_fds;
167   
168   GSource *prev;
169   GSource *next;
170
171   char    *name;
172
173   GSourcePrivate *priv;
174 };
175
176 struct _GSourceCallbackFuncs
177 {
178   void (*ref)   (gpointer     cb_data);
179   void (*unref) (gpointer     cb_data);
180   void (*get)   (gpointer     cb_data,
181                  GSource     *source, 
182                  GSourceFunc *func,
183                  gpointer    *data);
184 };
185
186 /**
187  * GSourceDummyMarshal:
188  *
189  * This is just a placeholder for #GClosureMarshal,
190  * which cannot be used here for dependency reasons.
191  */
192 typedef void (*GSourceDummyMarshal) (void);
193
194 struct _GSourceFuncs
195 {
196   gboolean (*prepare)  (GSource    *source,
197                         gint       *timeout_);
198   gboolean (*check)    (GSource    *source);
199   gboolean (*dispatch) (GSource    *source,
200                         GSourceFunc callback,
201                         gpointer    user_data);
202   void     (*finalize) (GSource    *source); /* Can be NULL */
203
204   /*< private >*/
205   /* For use by g_source_set_closure */
206   GSourceFunc     closure_callback;        
207   GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
208 };
209
210 /* Standard priorities */
211
212 /**
213  * G_PRIORITY_HIGH:
214  *
215  * Use this for high priority event sources.
216  *
217  * It is not used within GLib or GTK+.
218  */
219 #define G_PRIORITY_HIGH            -100
220
221 /**
222  * G_PRIORITY_DEFAULT:
223  *
224  * Use this for default priority event sources.
225  *
226  * In GLib this priority is used when adding timeout functions
227  * with g_timeout_add(). In GDK this priority is used for events
228  * from the X server.
229  */
230 #define G_PRIORITY_DEFAULT          0
231
232 /**
233  * G_PRIORITY_HIGH_IDLE:
234  *
235  * Use this for high priority idle functions.
236  *
237  * GTK+ uses #G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
238  * and #G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
239  * done to ensure that any pending resizes are processed before any
240  * pending redraws, so that widgets are not redrawn twice unnecessarily.)
241  */
242 #define G_PRIORITY_HIGH_IDLE        100
243
244 /**
245  * G_PRIORITY_DEFAULT_IDLE:
246  *
247  * Use this for default priority idle functions.
248  *
249  * In GLib this priority is used when adding idle functions with
250  * g_idle_add().
251  */
252 #define G_PRIORITY_DEFAULT_IDLE     200
253
254 /**
255  * G_PRIORITY_LOW:
256  *
257  * Use this for very low priority background tasks.
258  *
259  * It is not used within GLib or GTK+.
260  */
261 #define G_PRIORITY_LOW              300
262
263 /**
264  * G_SOURCE_REMOVE:
265  *
266  * Use this macro as the return value of a #GSourceFunc to remove
267  * the #GSource from the main loop.
268  *
269  * Since: 2.28
270  */
271 #define G_SOURCE_REMOVE         FALSE
272
273 /**
274  * G_SOURCE_CONTINUE:
275  *
276  * Use this macro as the return value of a #GSourceFunc to leave
277  * the #GSource in the main loop.
278  *
279  * Since: 2.28
280  */
281 #define G_SOURCE_CONTINUE       TRUE
282
283 /* GMainContext: */
284
285 GMainContext *g_main_context_new       (void);
286 GMainContext *g_main_context_ref       (GMainContext *context);
287 void          g_main_context_unref     (GMainContext *context);
288 GMainContext *g_main_context_default   (void);
289
290 gboolean      g_main_context_iteration (GMainContext *context,
291                                         gboolean      may_block);
292 gboolean      g_main_context_pending   (GMainContext *context);
293
294 /* For implementation of legacy interfaces
295  */
296 GSource      *g_main_context_find_source_by_id              (GMainContext *context,
297                                                              guint         source_id);
298 GSource      *g_main_context_find_source_by_user_data       (GMainContext *context,
299                                                              gpointer      user_data);
300 GSource      *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
301                                                              GSourceFuncs *funcs,
302                                                              gpointer      user_data);
303
304 /* Low level functions for implementing custom main loops.
305  */
306 void     g_main_context_wakeup  (GMainContext *context);
307 gboolean g_main_context_acquire (GMainContext *context);
308 void     g_main_context_release (GMainContext *context);
309 gboolean g_main_context_is_owner (GMainContext *context);
310 gboolean g_main_context_wait    (GMainContext *context,
311                                  GCond        *cond,
312                                  GMutex       *mutex);
313
314 gboolean g_main_context_prepare  (GMainContext *context,
315                                   gint         *priority);
316 gint     g_main_context_query    (GMainContext *context,
317                                   gint          max_priority,
318                                   gint         *timeout_,
319                                   GPollFD      *fds,
320                                   gint          n_fds);
321 gint     g_main_context_check    (GMainContext *context,
322                                   gint          max_priority,
323                                   GPollFD      *fds,
324                                   gint          n_fds);
325 void     g_main_context_dispatch (GMainContext *context);
326
327 void     g_main_context_set_poll_func (GMainContext *context,
328                                        GPollFunc     func);
329 GPollFunc g_main_context_get_poll_func (GMainContext *context);
330
331 /* Low level functions for use by source implementations
332  */
333 void     g_main_context_add_poll    (GMainContext *context,
334                                      GPollFD      *fd,
335                                      gint          priority);
336 void     g_main_context_remove_poll (GMainContext *context,
337                                      GPollFD      *fd);
338
339 gint     g_main_depth               (void);
340 GSource *g_main_current_source      (void);
341
342 /* GMainContexts for other threads
343  */
344 void          g_main_context_push_thread_default (GMainContext *context);
345 void          g_main_context_pop_thread_default  (GMainContext *context);
346 GMainContext *g_main_context_get_thread_default  (void);
347 GMainContext *g_main_context_ref_thread_default  (void);
348
349 /* GMainLoop: */
350
351 GMainLoop *g_main_loop_new        (GMainContext *context,
352                                    gboolean      is_running);
353 void       g_main_loop_run        (GMainLoop    *loop);
354 void       g_main_loop_quit       (GMainLoop    *loop);
355 GMainLoop *g_main_loop_ref        (GMainLoop    *loop);
356 void       g_main_loop_unref      (GMainLoop    *loop);
357 gboolean   g_main_loop_is_running (GMainLoop    *loop);
358 GMainContext *g_main_loop_get_context (GMainLoop    *loop);
359
360 /* GSource: */
361
362 GSource *g_source_new             (GSourceFuncs   *source_funcs,
363                                    guint           struct_size);
364 GSource *g_source_ref             (GSource        *source);
365 void     g_source_unref           (GSource        *source);
366
367 guint    g_source_attach          (GSource        *source,
368                                    GMainContext   *context);
369 void     g_source_destroy         (GSource        *source);
370
371 void     g_source_set_priority    (GSource        *source,
372                                    gint            priority);
373 gint     g_source_get_priority    (GSource        *source);
374 void     g_source_set_can_recurse (GSource        *source,
375                                    gboolean        can_recurse);
376 gboolean g_source_get_can_recurse (GSource        *source);
377 guint    g_source_get_id          (GSource        *source);
378
379 GMainContext *g_source_get_context (GSource       *source);
380
381 void     g_source_set_callback    (GSource        *source,
382                                    GSourceFunc     func,
383                                    gpointer        data,
384                                    GDestroyNotify  notify);
385
386 void     g_source_set_funcs       (GSource        *source,
387                                    GSourceFuncs   *funcs);
388 gboolean g_source_is_destroyed    (GSource        *source);
389
390 void                 g_source_set_name       (GSource        *source,
391                                               const char     *name);
392 const char *         g_source_get_name       (GSource        *source);
393 void                 g_source_set_name_by_id (guint           tag,
394                                               const char     *name);
395
396
397 /* Used to implement g_source_connect_closure and internally*/
398 void g_source_set_callback_indirect (GSource              *source,
399                                      gpointer              callback_data,
400                                      GSourceCallbackFuncs *callback_funcs);
401
402 void     g_source_add_poll            (GSource        *source,
403                                        GPollFD        *fd);
404 void     g_source_remove_poll         (GSource        *source,
405                                        GPollFD        *fd);
406
407 void     g_source_add_child_source    (GSource        *source,
408                                        GSource        *child_source);
409 void     g_source_remove_child_source (GSource        *source,
410                                        GSource        *child_source);
411
412 GLIB_DEPRECATED_FOR(g_source_get_time)
413 void     g_source_get_current_time (GSource        *source,
414                                     GTimeVal       *timeval);
415
416 gint64   g_source_get_time         (GSource        *source);
417
418  /* void g_source_connect_closure (GSource        *source,
419                                   GClosure       *closure);
420  */
421
422 /* Specific source types
423  */
424 GSource *g_idle_source_new        (void);
425 GSource *g_child_watch_source_new (GPid pid);
426 GSource *g_timeout_source_new     (guint interval);
427 GSource *g_timeout_source_new_seconds (guint interval);
428
429 /* Miscellaneous functions
430  */
431 void   g_get_current_time                 (GTimeVal       *result);
432 gint64 g_get_monotonic_time               (void);
433 gint64 g_get_real_time                    (void);
434
435
436 /* Source manipulation by ID */
437 gboolean g_source_remove                     (guint          tag);
438 gboolean g_source_remove_by_user_data        (gpointer       user_data);
439 gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
440                                               gpointer       user_data);
441
442 /* Idles, child watchers and timeouts */
443 guint    g_timeout_add_full         (gint            priority,
444                                      guint           interval,
445                                      GSourceFunc     function,
446                                      gpointer        data,
447                                      GDestroyNotify  notify);
448 guint    g_timeout_add              (guint           interval,
449                                      GSourceFunc     function,
450                                      gpointer        data);
451 guint    g_timeout_add_seconds_full (gint            priority,
452                                      guint           interval,
453                                      GSourceFunc     function,
454                                      gpointer        data,
455                                      GDestroyNotify  notify);
456 guint    g_timeout_add_seconds      (guint           interval,
457                                      GSourceFunc     function,
458                                      gpointer        data);
459 guint    g_child_watch_add_full     (gint            priority,
460                                      GPid            pid,
461                                      GChildWatchFunc function,
462                                      gpointer        data,
463                                      GDestroyNotify  notify);
464 guint    g_child_watch_add          (GPid            pid,
465                                      GChildWatchFunc function,
466                                      gpointer        data);
467 guint    g_idle_add                 (GSourceFunc     function,
468                                      gpointer        data);
469 guint    g_idle_add_full            (gint            priority,
470                                      GSourceFunc     function,
471                                      gpointer        data,
472                                      GDestroyNotify  notify);
473 gboolean g_idle_remove_by_data      (gpointer        data);
474
475 void     g_main_context_invoke_full (GMainContext   *context,
476                                      gint            priority,
477                                      GSourceFunc     function,
478                                      gpointer        data,
479                                      GDestroyNotify  notify);
480 void     g_main_context_invoke      (GMainContext   *context,
481                                      GSourceFunc     function,
482                                      gpointer        data);
483
484 /* Hook for GClosure / GSource integration. Don't touch */
485 GLIB_VAR GSourceFuncs g_timeout_funcs;
486 GLIB_VAR GSourceFuncs g_child_watch_funcs;
487 GLIB_VAR GSourceFuncs g_idle_funcs;
488
489 G_END_DECLS
490
491 #endif /* __G_MAIN_H__ */