67250da4bca8d561f3b033f6eea375d4df4a12e5
[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 #include <glib/gslist.h>
24 #include <glib/gthread.h>
25
26 G_BEGIN_DECLS
27
28 typedef struct _GMainContext            GMainContext;   /* Opaque */
29 typedef struct _GMainLoop               GMainLoop;      /* Opaque */
30 typedef struct _GSource                 GSource;
31 typedef struct _GSourceCallbackFuncs    GSourceCallbackFuncs;
32 typedef struct _GSourceFuncs            GSourceFuncs;
33
34 typedef gboolean (*GSourceFunc)       (gpointer data);
35 typedef void     (*GChildWatchFunc)   (GPid     pid,
36                                        gint     status,
37                                        gpointer data);
38 struct _GSource
39 {
40   /*< private >*/
41   gpointer callback_data;
42   GSourceCallbackFuncs *callback_funcs;
43
44   GSourceFuncs *source_funcs;
45   guint ref_count;
46
47   GMainContext *context;
48
49   gint priority;
50   guint flags;
51   guint source_id;
52
53   GSList *poll_fds;
54   
55   GSource *prev;
56   GSource *next;
57
58   gpointer reserved1;
59   gpointer reserved2;
60 };
61
62 struct _GSourceCallbackFuncs
63 {
64   void (*ref)   (gpointer     cb_data);
65   void (*unref) (gpointer     cb_data);
66   void (*get)   (gpointer     cb_data,
67                  GSource     *source, 
68                  GSourceFunc *func,
69                  gpointer    *data);
70 };
71
72 typedef void (*GSourceDummyMarshal) (void);
73
74 struct _GSourceFuncs
75 {
76   gboolean (*prepare)  (GSource    *source,
77                         gint       *timeout_);
78   gboolean (*check)    (GSource    *source);
79   gboolean (*dispatch) (GSource    *source,
80                         GSourceFunc callback,
81                         gpointer    user_data);
82   void     (*finalize) (GSource    *source); /* Can be NULL */
83
84   /* For use by g_source_set_closure */
85   GSourceFunc     closure_callback;        
86   GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
87 };
88
89 /* Any definitions using GPollFD or GPollFunc are primarily
90  * for Unix and not guaranteed to be the compatible on all
91  * operating systems on which GLib runs. Right now, the
92  * GLib does use these functions on Win32 as well, but interprets
93  * them in a fairly different way than on Unix. If you use
94  * these definitions, you are should be prepared to recode
95  * for different operating systems.
96  *
97  *
98  * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
99  * descriptor as provided by the C runtime) that can be used by
100  * MsgWaitForMultipleObjects. This does *not* include file handles
101  * from CreateFile, SOCKETs, nor pipe handles. (But you can use
102  * WSAEventSelect to signal events when a SOCKET is readable).
103  *
104  * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
105  * indicate polling for messages.
106  *
107  * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
108  * (GTK) programs, as GDK itself wants to read messages and convert them
109  * to GDK events.
110  *
111  * So, unless you really know what you are doing, it's best not to try
112  * to use the main loop polling stuff for your own needs on
113  * Win32. It's really only written for the GIMP's needs so
114  * far.
115  */
116 typedef struct _GPollFD GPollFD;
117 typedef gint    (*GPollFunc)    (GPollFD *ufds,
118                                  guint    nfsd,
119                                  gint     timeout_);
120
121 struct _GPollFD
122 {
123 #if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
124   gint64        fd;
125 #else
126   gint          fd;
127 #endif
128   gushort       events;
129   gushort       revents;
130 };
131
132 /* Standard priorities */
133
134 #define G_PRIORITY_HIGH            -100
135 #define G_PRIORITY_DEFAULT          0
136 #define G_PRIORITY_HIGH_IDLE        100
137 #define G_PRIORITY_DEFAULT_IDLE     200
138 #define G_PRIORITY_LOW              300
139
140 /* GMainContext: */
141
142 GMainContext *g_main_context_new       (void);
143 GMainContext *g_main_context_ref       (GMainContext *context);
144 void          g_main_context_unref     (GMainContext *context);
145 GMainContext *g_main_context_default   (void);
146
147 gboolean      g_main_context_iteration (GMainContext *context,
148                                         gboolean      may_block);
149 gboolean      g_main_context_pending   (GMainContext *context);
150
151 /* For implementation of legacy interfaces
152  */
153 GSource      *g_main_context_find_source_by_id              (GMainContext *context,
154                                                              guint         source_id);
155 GSource      *g_main_context_find_source_by_user_data       (GMainContext *context,
156                                                              gpointer      user_data);
157 GSource      *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
158                                                              GSourceFuncs *funcs,
159                                                              gpointer      user_data);
160
161 /* Low level functions for implementing custom main loops.
162  */
163 void     g_main_context_wakeup  (GMainContext *context);
164 gboolean g_main_context_acquire (GMainContext *context);
165 void     g_main_context_release (GMainContext *context);
166 gboolean g_main_context_is_owner (GMainContext *context);
167 gboolean g_main_context_wait    (GMainContext *context,
168                                  GCond        *cond,
169                                  GMutex       *mutex);
170
171 gboolean g_main_context_prepare  (GMainContext *context,
172                                   gint         *priority);
173 gint     g_main_context_query    (GMainContext *context,
174                                   gint          max_priority,
175                                   gint         *timeout_,
176                                   GPollFD      *fds,
177                                   gint          n_fds);
178 gint     g_main_context_check    (GMainContext *context,
179                                   gint          max_priority,
180                                   GPollFD      *fds,
181                                   gint          n_fds);
182 void     g_main_context_dispatch (GMainContext *context);
183
184 void     g_main_context_set_poll_func (GMainContext *context,
185                                        GPollFunc     func);
186 GPollFunc g_main_context_get_poll_func (GMainContext *context);
187
188 /* Low level functions for use by source implementations
189  */
190 void     g_main_context_add_poll    (GMainContext *context,
191                                      GPollFD      *fd,
192                                      gint          priority);
193 void     g_main_context_remove_poll (GMainContext *context,
194                                      GPollFD      *fd);
195
196 gint     g_main_depth               (void);
197 GSource *g_main_current_source      (void);
198
199
200 /* GMainLoop: */
201
202 GMainLoop *g_main_loop_new        (GMainContext *context,
203                                    gboolean      is_running);
204 void       g_main_loop_run        (GMainLoop    *loop);
205 void       g_main_loop_quit       (GMainLoop    *loop);
206 GMainLoop *g_main_loop_ref        (GMainLoop    *loop);
207 void       g_main_loop_unref      (GMainLoop    *loop);
208 gboolean   g_main_loop_is_running (GMainLoop    *loop);
209 GMainContext *g_main_loop_get_context (GMainLoop    *loop);
210
211 /* GSource: */
212
213 GSource *g_source_new             (GSourceFuncs   *source_funcs,
214                                    guint           struct_size);
215 GSource *g_source_ref             (GSource        *source);
216 void     g_source_unref           (GSource        *source);
217
218 guint    g_source_attach          (GSource        *source,
219                                    GMainContext   *context);
220 void     g_source_destroy         (GSource        *source);
221
222 void     g_source_set_priority    (GSource        *source,
223                                    gint            priority);
224 gint     g_source_get_priority    (GSource        *source);
225 void     g_source_set_can_recurse (GSource        *source,
226                                    gboolean        can_recurse);
227 gboolean g_source_get_can_recurse (GSource        *source);
228 guint    g_source_get_id          (GSource        *source);
229
230 GMainContext *g_source_get_context (GSource       *source);
231
232 void     g_source_set_callback    (GSource        *source,
233                                    GSourceFunc     func,
234                                    gpointer        data,
235                                    GDestroyNotify  notify);
236
237 void     g_source_set_funcs       (GSource        *source,
238                                    GSourceFuncs   *funcs);
239 gboolean g_source_is_destroyed    (GSource        *source);
240
241 /* Used to implement g_source_connect_closure and internally*/
242 void g_source_set_callback_indirect (GSource              *source,
243                                      gpointer              callback_data,
244                                      GSourceCallbackFuncs *callback_funcs);
245
246 void     g_source_add_poll         (GSource        *source,
247                                     GPollFD        *fd);
248 void     g_source_remove_poll      (GSource        *source,
249                                     GPollFD        *fd);
250
251 void     g_source_get_current_time (GSource        *source,
252                                     GTimeVal       *timeval);
253
254  /* void g_source_connect_closure (GSource        *source,
255                                   GClosure       *closure);
256  */
257
258 /* Specific source types
259  */
260 GSource *g_idle_source_new        (void);
261 GSource *g_child_watch_source_new (GPid pid);
262 GSource *g_timeout_source_new     (guint interval);
263 GSource *g_timeout_source_new_seconds (guint interval);
264
265 /* Miscellaneous functions
266  */
267 void g_get_current_time                 (GTimeVal       *result);
268
269 /* ============== Compat main loop stuff ================== */
270
271 #ifndef G_DISABLE_DEPRECATED
272
273 /* Legacy names for GMainLoop functions
274  */
275 #define         g_main_new(is_running)  g_main_loop_new (NULL, is_running);
276 #define         g_main_run(loop)        g_main_loop_run(loop)
277 #define         g_main_quit(loop)       g_main_loop_quit(loop)
278 #define         g_main_destroy(loop)    g_main_loop_unref(loop)
279 #define         g_main_is_running(loop) g_main_loop_is_running(loop)
280
281 /* Functions to manipulate the default main loop
282  */
283
284 #define g_main_iteration(may_block) g_main_context_iteration      (NULL, may_block)
285 #define g_main_pending()            g_main_context_pending        (NULL)
286
287 #define g_main_set_poll_func(func)   g_main_context_set_poll_func (NULL, func)
288
289 #endif /* G_DISABLE_DEPRECATED */
290
291 /* Source manipulation by ID */
292 gboolean g_source_remove                     (guint          tag);
293 gboolean g_source_remove_by_user_data        (gpointer       user_data);
294 gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
295                                               gpointer       user_data);
296
297 /* Idles, child watchers and timeouts */
298 guint    g_timeout_add_full         (gint            priority,
299                                      guint           interval,
300                                      GSourceFunc     function,
301                                      gpointer        data,
302                                      GDestroyNotify  notify);
303 guint    g_timeout_add              (guint           interval,
304                                      GSourceFunc     function,
305                                      gpointer        data);
306 guint    g_timeout_add_seconds_full (gint            priority,
307                                      guint           interval,
308                                      GSourceFunc     function,
309                                      gpointer        data,
310                                      GDestroyNotify  notify);
311 guint    g_timeout_add_seconds      (guint           interval,
312                                      GSourceFunc     function,
313                                      gpointer        data);
314 guint    g_child_watch_add_full     (gint            priority,
315                                      GPid            pid,
316                                      GChildWatchFunc function,
317                                      gpointer        data,
318                                      GDestroyNotify  notify);
319 guint    g_child_watch_add          (GPid            pid,
320                                      GChildWatchFunc function,
321                                      gpointer        data);
322 guint    g_idle_add                 (GSourceFunc     function,
323                                      gpointer        data);
324 guint    g_idle_add_full            (gint            priority,
325                                      GSourceFunc     function,
326                                      gpointer        data,
327                                      GDestroyNotify  notify);
328 gboolean g_idle_remove_by_data      (gpointer        data);
329
330 /* Hook for GClosure / GSource integration. Don't touch */
331 GLIB_VAR GSourceFuncs g_timeout_funcs;
332 GLIB_VAR GSourceFuncs g_child_watch_funcs;
333 GLIB_VAR GSourceFuncs g_idle_funcs;
334
335 G_END_DECLS
336
337 #endif /* __G_MAIN_H__ */