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(G_DISABLE_SINGLE_INCLUDES) && !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 typedef struct _GMainContext GMainContext; /* Opaque */ 34 typedef struct _GMainLoop GMainLoop; /* Opaque */ 35 typedef struct _GSource GSource; 36 typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs; 37 typedef struct _GSourceFuncs GSourceFuncs; 38 39 typedef gboolean (*GSourceFunc) (gpointer data); 40 typedef void (*GChildWatchFunc) (GPid pid, 41 gint status, 42 gpointer data); 43 struct _GSource 44 { 45 /*< private >*/ 46 gpointer callback_data; 47 GSourceCallbackFuncs *callback_funcs; 48 49 GSourceFuncs *source_funcs; 50 guint ref_count; 51 52 GMainContext *context; 53 54 gint priority; 55 guint flags; 56 guint source_id; 57 58 GSList *poll_fds; 59 60 GSource *prev; 61 GSource *next; 62 63 gpointer reserved1; 64 gpointer reserved2; 65 }; 66 67 struct _GSourceCallbackFuncs 68 { 69 void (*ref) (gpointer cb_data); 70 void (*unref) (gpointer cb_data); 71 void (*get) (gpointer cb_data, 72 GSource *source, 73 GSourceFunc *func, 74 gpointer *data); 75 }; 76 77 typedef void (*GSourceDummyMarshal) (void); 78 79 struct _GSourceFuncs 80 { 81 gboolean (*prepare) (GSource *source, 82 gint *timeout_); 83 gboolean (*check) (GSource *source); 84 gboolean (*dispatch) (GSource *source, 85 GSourceFunc callback, 86 gpointer user_data); 87 void (*finalize) (GSource *source); /* Can be NULL */ 88 89 /* For use by g_source_set_closure */ 90 GSourceFunc closure_callback; 91 GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */ 92 }; 93 94 /* Standard priorities */ 95 96 #define G_PRIORITY_HIGH -100 97 #define G_PRIORITY_DEFAULT 0 98 #define G_PRIORITY_HIGH_IDLE 100 99 #define G_PRIORITY_DEFAULT_IDLE 200 100 #define G_PRIORITY_LOW 300 101 102 /* GMainContext: */ 103 104 GMainContext *g_main_context_new (void); 105 GMainContext *g_main_context_ref (GMainContext *context); 106 void g_main_context_unref (GMainContext *context); 107 GMainContext *g_main_context_default (void); 108 109 gboolean g_main_context_iteration (GMainContext *context, 110 gboolean may_block); 111 gboolean g_main_context_pending (GMainContext *context); 112 113 /* For implementation of legacy interfaces 114 */ 115 GSource *g_main_context_find_source_by_id (GMainContext *context, 116 guint source_id); 117 GSource *g_main_context_find_source_by_user_data (GMainContext *context, 118 gpointer user_data); 119 GSource *g_main_context_find_source_by_funcs_user_data (GMainContext *context, 120 GSourceFuncs *funcs, 121 gpointer user_data); 122 123 /* Low level functions for implementing custom main loops. 124 */ 125 void g_main_context_wakeup (GMainContext *context); 126 gboolean g_main_context_acquire (GMainContext *context); 127 void g_main_context_release (GMainContext *context); 128 gboolean g_main_context_is_owner (GMainContext *context); 129 gboolean g_main_context_wait (GMainContext *context, 130 GCond *cond, 131 GMutex *mutex); 132 133 gboolean g_main_context_prepare (GMainContext *context, 134 gint *priority); 135 gint g_main_context_query (GMainContext *context, 136 gint max_priority, 137 gint *timeout_, 138 GPollFD *fds, 139 gint n_fds); 140 gint g_main_context_check (GMainContext *context, 141 gint max_priority, 142 GPollFD *fds, 143 gint n_fds); 144 void g_main_context_dispatch (GMainContext *context); 145 146 void g_main_context_set_poll_func (GMainContext *context, 147 GPollFunc func); 148 GPollFunc g_main_context_get_poll_func (GMainContext *context); 149 150 /* Low level functions for use by source implementations 151 */ 152 void g_main_context_add_poll (GMainContext *context, 153 GPollFD *fd, 154 gint priority); 155 void g_main_context_remove_poll (GMainContext *context, 156 GPollFD *fd); 157 158 gint g_main_depth (void); 159 GSource *g_main_current_source (void); 160 161 162 /* GMainLoop: */ 163 164 GMainLoop *g_main_loop_new (GMainContext *context, 165 gboolean is_running); 166 void g_main_loop_run (GMainLoop *loop); 167 void g_main_loop_quit (GMainLoop *loop); 168 GMainLoop *g_main_loop_ref (GMainLoop *loop); 169 void g_main_loop_unref (GMainLoop *loop); 170 gboolean g_main_loop_is_running (GMainLoop *loop); 171 GMainContext *g_main_loop_get_context (GMainLoop *loop); 172 173 /* GSource: */ 174 175 GSource *g_source_new (GSourceFuncs *source_funcs, 176 guint struct_size); 177 GSource *g_source_ref (GSource *source); 178 void g_source_unref (GSource *source); 179 180 guint g_source_attach (GSource *source, 181 GMainContext *context); 182 void g_source_destroy (GSource *source); 183 184 void g_source_set_priority (GSource *source, 185 gint priority); 186 gint g_source_get_priority (GSource *source); 187 void g_source_set_can_recurse (GSource *source, 188 gboolean can_recurse); 189 gboolean g_source_get_can_recurse (GSource *source); 190 guint g_source_get_id (GSource *source); 191 192 GMainContext *g_source_get_context (GSource *source); 193 194 void g_source_set_callback (GSource *source, 195 GSourceFunc func, 196 gpointer data, 197 GDestroyNotify notify); 198 199 void g_source_set_funcs (GSource *source, 200 GSourceFuncs *funcs); 201 gboolean g_source_is_destroyed (GSource *source); 202 203 /* Used to implement g_source_connect_closure and internally*/ 204 void g_source_set_callback_indirect (GSource *source, 205 gpointer callback_data, 206 GSourceCallbackFuncs *callback_funcs); 207 208 void g_source_add_poll (GSource *source, 209 GPollFD *fd); 210 void g_source_remove_poll (GSource *source, 211 GPollFD *fd); 212 213 void g_source_get_current_time (GSource *source, 214 GTimeVal *timeval); 215 216 /* void g_source_connect_closure (GSource *source, 217 GClosure *closure); 218 */ 219 220 /* Specific source types 221 */ 222 GSource *g_idle_source_new (void); 223 GSource *g_child_watch_source_new (GPid pid); 224 GSource *g_timeout_source_new (guint interval); 225 GSource *g_timeout_source_new_seconds (guint interval); 226 227 /* Miscellaneous functions 228 */ 229 void g_get_current_time (GTimeVal *result); 230 231 /* ============== Compat main loop stuff ================== */ 232 233 #ifndef G_DISABLE_DEPRECATED 234 235 /* Legacy names for GMainLoop functions 236 */ 237 #define g_main_new(is_running) g_main_loop_new (NULL, is_running); 238 #define g_main_run(loop) g_main_loop_run(loop) 239 #define g_main_quit(loop) g_main_loop_quit(loop) 240 #define g_main_destroy(loop) g_main_loop_unref(loop) 241 #define g_main_is_running(loop) g_main_loop_is_running(loop) 242 243 /* Functions to manipulate the default main loop 244 */ 245 246 #define g_main_iteration(may_block) g_main_context_iteration (NULL, may_block) 247 #define g_main_pending() g_main_context_pending (NULL) 248 249 #define g_main_set_poll_func(func) g_main_context_set_poll_func (NULL, func) 250 251 #endif /* G_DISABLE_DEPRECATED */ 252 253 /* Source manipulation by ID */ 254 gboolean g_source_remove (guint tag); 255 gboolean g_source_remove_by_user_data (gpointer user_data); 256 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs, 257 gpointer user_data); 258 259 /* Idles, child watchers and timeouts */ 260 guint g_timeout_add_full (gint priority, 261 guint interval, 262 GSourceFunc function, 263 gpointer data, 264 GDestroyNotify notify); 265 guint g_timeout_add (guint interval, 266 GSourceFunc function, 267 gpointer data); 268 guint g_timeout_add_seconds_full (gint priority, 269 guint interval, 270 GSourceFunc function, 271 gpointer data, 272 GDestroyNotify notify); 273 guint g_timeout_add_seconds (guint interval, 274 GSourceFunc function, 275 gpointer data); 276 guint g_child_watch_add_full (gint priority, 277 GPid pid, 278 GChildWatchFunc function, 279 gpointer data, 280 GDestroyNotify notify); 281 guint g_child_watch_add (GPid pid, 282 GChildWatchFunc function, 283 gpointer data); 284 guint g_idle_add (GSourceFunc function, 285 gpointer data); 286 guint g_idle_add_full (gint priority, 287 GSourceFunc function, 288 gpointer data, 289 GDestroyNotify notify); 290 gboolean g_idle_remove_by_data (gpointer data); 291 292 /* Hook for GClosure / GSource integration. Don't touch */ 293 GLIB_VAR GSourceFuncs g_timeout_funcs; 294 GLIB_VAR GSourceFuncs g_child_watch_funcs; 295 GLIB_VAR GSourceFuncs g_idle_funcs; 296 297 G_END_DECLS 298 299 #endif /* __G_MAIN_H__ */ 300