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