1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  *    (See accompanying file LICENSE_1_0.txt or copy at
4  *          http://www.boost.org/LICENSE_1_0.txt)
5  */
6 module glib.gmain;
7 
8 import glib.gtypes;
9 import glib.gpoll;
10 import glib.gslist;
11 import glib.gthread;
12 
13 
14 enum GIOCondition
15 {
16     G_IO_IN,
17     G_IO_OUT,
18     G_IO_PRI,
19     G_IO_ERR,
20     G_IO_HUP,
21     G_IO_NVAL
22 }
23 
24 
25 
26 
27 struct GMainContext;
28 
29 struct GMainLoop;
30 
31 
32 extern (C) alias GSourceFunc = gboolean function (gpointer user_data);
33 
34 extern (C) alias GChildWatchFunc = void function (GPid     pid,
35                                        gint     status,
36                                        gpointer user_data);
37 
38 
39 private struct GSourcePrivate;
40 
41 struct GSource
42 {
43     gpointer callback_data;
44     GSourceCallbackFuncs *callback_funcs;
45 
46     const GSourceFuncs *source_funcs;
47     guint ref_count;
48 
49     GMainContext *context;
50 
51     gint priority;
52     guint flags;
53     guint source_id;
54 
55     GSList *poll_fds;
56 
57     GSource *prev;
58     GSource *next;
59 
60     char    *name;
61 
62     GSourcePrivate *priv;
63 }
64 
65 extern (C) {
66 
67     struct GSourceCallbackFuncs
68     {
69         void function (gpointer cb_data) ref_;
70         void function (gpointer cb_data) unref;
71         void function (gpointer     cb_data,
72                      GSource     *source,
73                      GSourceFunc *func,
74                      gpointer    *data) get;
75     };
76 
77     alias GSourceDummyMarshal = void function();
78 
79     struct GSourceFuncs
80     {
81         gboolean function (GSource    *source,
82                             gint       *timeout_) prepare;
83         gboolean function    (GSource    *source) check;
84         gboolean function (GSource    *source,
85                             GSourceFunc callback,
86                             gpointer    user_data) dispatch;
87         void     function (GSource    *source) finalize;
88 
89         GSourceFunc     closure_callback;
90         GSourceDummyMarshal closure_marshal;
91     }
92 }
93 
94 enum G_PRIORITY_HIGH = -100;
95 
96 enum G_PRIORITY_DEFAULT = 0;
97 
98 enum G_PRIORITY_HIGH_IDLE = 100;
99 
100 enum G_PRIORITY_DEFAULT_IDLE = 200;
101 
102 enum G_PRIORITY_LOW = 300;
103 
104 enum G_SOURCE_REMOVE = FALSE;
105 
106 enum G_SOURCE_CONTINUE = TRUE;
107 
108 
109 
110 extern (C) {
111 
112     GMainContext *g_main_context_new       ();
113 
114     GMainContext *g_main_context_ref       (GMainContext *context);
115 
116     void          g_main_context_unref     (GMainContext *context);
117 
118     GMainContext *g_main_context_default   ();
119 
120 
121     gboolean      g_main_context_iteration (GMainContext *context,
122                                             gboolean      may_block);
123 
124     gboolean      g_main_context_pending   (GMainContext *context);
125 
126     /* For implementation of legacy interfaces
127      */
128 
129     GSource      *g_main_context_find_source_by_id              (GMainContext *context,
130                                                                  guint         source_id);
131 
132     GSource      *g_main_context_find_source_by_user_data       (GMainContext *context,
133                                                                  gpointer      user_data);
134 
135     GSource      *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
136                                                                  GSourceFuncs *funcs,
137                                                                  gpointer      user_data);
138 
139     /* Low level functions for implementing custom main loops.
140      */
141 
142     void     g_main_context_wakeup  (GMainContext *context);
143 
144     gboolean g_main_context_acquire (GMainContext *context);
145 
146     void     g_main_context_release (GMainContext *context);
147 
148     gboolean g_main_context_is_owner (GMainContext *context);
149 
150     gboolean g_main_context_wait    (GMainContext *context,
151                                      GCond        *cond,
152                                      GMutex       *mutex);
153 
154 
155     gboolean g_main_context_prepare  (GMainContext *context,
156                                       gint         *priority);
157 
158     gint     g_main_context_query    (GMainContext *context,
159                                       gint          max_priority,
160                                       gint         *timeout_,
161                                       GPollFD      *fds,
162                                       gint          n_fds);
163 
164     gint     g_main_context_check    (GMainContext *context,
165                                       gint          max_priority,
166                                       GPollFD      *fds,
167                                       gint          n_fds);
168 
169     void     g_main_context_dispatch (GMainContext *context);
170 
171 
172     void     g_main_context_set_poll_func (GMainContext *context,
173                                            GPollFunc     func);
174 
175     GPollFunc g_main_context_get_poll_func (GMainContext *context);
176 
177     /* Low level functions for use by source implementations
178      */
179 
180     void     g_main_context_add_poll    (GMainContext *context,
181                                          GPollFD      *fd,
182                                          gint          priority);
183 
184     void     g_main_context_remove_poll (GMainContext *context,
185                                          GPollFD      *fd);
186 
187 
188     gint     g_main_depth               ();
189 
190     GSource *g_main_current_source      ();
191 
192     /* GMainContexts for other threads
193      */
194 
195     void          g_main_context_push_thread_default (GMainContext *context);
196 
197     void          g_main_context_pop_thread_default  (GMainContext *context);
198 
199     GMainContext *g_main_context_get_thread_default  ();
200 
201     GMainContext *g_main_context_ref_thread_default  ();
202 
203     /* GMainLoop: */
204 
205 
206     GMainLoop *g_main_loop_new        (GMainContext *context,
207                                        gboolean      is_running);
208 
209     void       g_main_loop_run        (GMainLoop    *loop);
210 
211     void       g_main_loop_quit       (GMainLoop    *loop);
212 
213     GMainLoop *g_main_loop_ref        (GMainLoop    *loop);
214 
215     void       g_main_loop_unref      (GMainLoop    *loop);
216 
217     gboolean   g_main_loop_is_running (GMainLoop    *loop);
218 
219     GMainContext *g_main_loop_get_context (GMainLoop    *loop);
220 
221     /* GSource: */
222 
223 
224     GSource *g_source_new             (GSourceFuncs   *source_funcs,
225                                        guint           struct_size);
226 
227     GSource *g_source_ref             (GSource        *source);
228 
229     void     g_source_unref           (GSource        *source);
230 
231 
232     guint    g_source_attach          (GSource        *source,
233                                        GMainContext   *context);
234 
235     void     g_source_destroy         (GSource        *source);
236 
237 
238     void     g_source_set_priority    (GSource        *source,
239                                        gint            priority);
240 
241     gint     g_source_get_priority    (GSource        *source);
242 
243     void     g_source_set_can_recurse (GSource        *source,
244                                        gboolean        can_recurse);
245 
246     gboolean g_source_get_can_recurse (GSource        *source);
247 
248     guint    g_source_get_id          (GSource        *source);
249 
250 
251     GMainContext *g_source_get_context (GSource       *source);
252 
253 
254     void     g_source_set_callback    (GSource        *source,
255                                        GSourceFunc     func,
256                                        gpointer        data,
257                                        GDestroyNotify  notify);
258 
259 
260     void     g_source_set_funcs       (GSource        *source,
261                                        GSourceFuncs   *funcs);
262 
263     gboolean g_source_is_destroyed    (GSource        *source);
264 
265 
266     void                 g_source_set_name       (GSource        *source,
267                                                   const(char)    *name);
268 
269     const(char) *        g_source_get_name       (GSource        *source);
270 
271     void                 g_source_set_name_by_id (guint           tag,
272                                                   const(char)    *name);
273 
274 
275     void                 g_source_set_ready_time (GSource        *source,
276                                                   gint64          ready_time);
277 
278     gint64               g_source_get_ready_time (GSource        *source);
279 
280     version(Posix) {
281 
282         gpointer             g_source_add_unix_fd    (GSource        *source,
283                                                       gint            fd,
284                                                       GIOCondition    events);
285 
286         void                 g_source_modify_unix_fd (GSource        *source,
287                                                       gpointer        tag,
288                                                       GIOCondition    new_events);
289 
290         void                 g_source_remove_unix_fd (GSource        *source,
291                                                       gpointer        tag);
292 
293         GIOCondition         g_source_query_unix_fd  (GSource        *source,
294                                                       gpointer        tag);
295     }
296 
297 
298     void g_source_set_callback_indirect (GSource              *source,
299                                          gpointer              callback_data,
300                                          GSourceCallbackFuncs *callback_funcs);
301 
302 
303     void     g_source_add_poll            (GSource        *source,
304                            GPollFD        *fd);
305 
306     void     g_source_remove_poll         (GSource        *source,
307                            GPollFD        *fd);
308 
309 
310     void     g_source_add_child_source    (GSource        *source,
311                            GSource        *child_source);
312 
313     void     g_source_remove_child_source (GSource        *source,
314                            GSource        *child_source);
315 
316     deprecated("use g_source_get_time")
317     void     g_source_get_current_time (GSource        *source,
318                                         GTimeVal       *timeval);
319 
320 
321     gint64   g_source_get_time         (GSource        *source);
322 
323      /* void g_source_connect_closure (GSource        *source,
324                                       GClosure       *closure);
325      */
326 
327 
328     GSource *g_idle_source_new        ();
329 
330     GSource *g_child_watch_source_new (GPid pid);
331 
332     GSource *g_timeout_source_new     (guint interval);
333 
334     GSource *g_timeout_source_new_seconds (guint interval);
335 
336     /* Miscellaneous functions
337      */
338 
339     void   g_get_current_time                 (GTimeVal       *result);
340 
341     gint64 g_get_monotonic_time               ();
342 
343     gint64 g_get_real_time                    ();
344 
345 
346     /* Source manipulation by ID */
347 
348     gboolean g_source_remove                     (guint          tag);
349 
350     gboolean g_source_remove_by_user_data        (gpointer       user_data);
351 
352     gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
353                                                   gpointer       user_data);
354 
355     /* Idles, child watchers and timeouts */
356 
357     guint    g_timeout_add_full         (gint            priority,
358                                          guint           interval,
359                                          GSourceFunc     func,
360                                          gpointer        data,
361                                          GDestroyNotify  notify);
362 
363     guint    g_timeout_add              (guint           interval,
364                                          GSourceFunc     func,
365                                          gpointer        data);
366 
367     guint    g_timeout_add_seconds_full (gint            priority,
368                                          guint           interval,
369                                          GSourceFunc     func,
370                                          gpointer        data,
371                                          GDestroyNotify  notify);
372 
373     guint    g_timeout_add_seconds      (guint           interval,
374                                          GSourceFunc     func,
375                                          gpointer        data);
376 
377     guint    g_child_watch_add_full     (gint            priority,
378                                          GPid            pid,
379                                          GChildWatchFunc func,
380                                          gpointer        data,
381                                          GDestroyNotify  notify);
382 
383     guint    g_child_watch_add          (GPid            pid,
384                                          GChildWatchFunc func,
385                                          gpointer        data);
386 
387     guint    g_idle_add                 (GSourceFunc     func,
388                                          gpointer        data);
389 
390     guint    g_idle_add_full            (gint            priority,
391                                          GSourceFunc     func,
392                                          gpointer        data,
393                                          GDestroyNotify  notify);
394 
395     gboolean g_idle_remove_by_data      (gpointer        data);
396 
397 
398     void     g_main_context_invoke_full (GMainContext   *context,
399                                          gint            priority,
400                                          GSourceFunc     func,
401                                          gpointer        data,
402                                          GDestroyNotify  notify);
403 
404     void     g_main_context_invoke      (GMainContext   *context,
405                                          GSourceFunc     func,
406                                          gpointer        data);
407 
408     /* Hook for GClosure / GSource integration. Don't touch */
409     extern __gshared GSourceFuncs g_timeout_funcs;
410     extern __gshared GSourceFuncs g_child_watch_funcs;
411     extern __gshared GSourceFuncs g_idle_funcs;
412     version (Posix) {
413         extern __gshared GSourceFuncs g_unix_signal_funcs;
414         extern __gshared GSourceFuncs g_unix_fd_source_funcs;
415     }
416 }
417