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