gsourceclosure: Add support for GUnixSignalWatchSource and GUnixFDSource
[platform/upstream/glib.git] / gobject / gsourceclosure.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001 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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public 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 #include "config.h"
21
22 #include "gsourceclosure.h"
23 #include "gboxed.h"
24 #include "genums.h"
25 #include "gmarshal.h"
26 #include "gvalue.h"
27 #include "gvaluetypes.h"
28 #ifdef G_OS_UNIX
29 #include "glib-unix.h"
30 #endif
31
32 G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_unref)
33
34 GType
35 g_io_condition_get_type (void)
36 {
37   static GType etype = 0;
38   if (etype == 0)
39     {
40       static const GFlagsValue values[] = {
41         { G_IO_IN,   "G_IO_IN",   "in" },
42         { G_IO_OUT,  "G_IO_OUT",  "out" },
43         { G_IO_PRI,  "G_IO_PRI",  "pri" },
44         { G_IO_ERR,  "G_IO_ERR",  "err" },
45         { G_IO_HUP,  "G_IO_HUP",  "hup" },
46         { G_IO_NVAL, "G_IO_NVAL", "nval" },
47         { 0, NULL, NULL }
48       };
49       etype = g_flags_register_static ("GIOCondition", values);
50     }
51   return etype;
52 }
53
54 static gboolean
55 io_watch_closure_callback (GIOChannel   *channel,
56                            GIOCondition  condition,
57                            gpointer      data)
58 {
59   GClosure *closure = data;
60
61   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
62   GValue result_value = G_VALUE_INIT;
63   gboolean result;
64
65   g_value_init (&result_value, G_TYPE_BOOLEAN);
66   g_value_init (&params[0], G_TYPE_IO_CHANNEL);
67   g_value_set_boxed (&params[0], channel);
68                      
69   g_value_init (&params[1], G_TYPE_IO_CONDITION);
70   g_value_set_flags (&params[1], condition);
71
72   g_closure_invoke (closure, &result_value, 2, params, NULL);
73
74   result = g_value_get_boolean (&result_value);
75   g_value_unset (&result_value);
76   g_value_unset (&params[0]);
77   g_value_unset (&params[1]);
78
79   return result;
80 }
81
82 #ifdef G_OS_UNIX
83 static gboolean
84 g_unix_fd_source_closure_callback (int           fd,
85                                    GIOCondition  condition,
86                                    gpointer      data)
87 {
88   GClosure *closure = data;
89
90   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
91   GValue result_value = G_VALUE_INIT;
92   gboolean result;
93
94   g_value_init (&result_value, G_TYPE_BOOLEAN);
95
96   g_value_init (&params[0], G_TYPE_INT);
97   g_value_set_int (&params[0], fd);
98
99   g_value_init (&params[1], G_TYPE_IO_CONDITION);
100   g_value_set_flags (&params[1], condition);
101
102   g_closure_invoke (closure, &result_value, 2, params, NULL);
103
104   result = g_value_get_boolean (&result_value);
105   g_value_unset (&result_value);
106   g_value_unset (&params[0]);
107   g_value_unset (&params[1]);
108
109   return result;
110 }
111 #endif
112
113 static gboolean
114 source_closure_callback (gpointer data)
115 {
116   GClosure *closure = data;
117   GValue result_value = G_VALUE_INIT;
118   gboolean result;
119
120   g_value_init (&result_value, G_TYPE_BOOLEAN);
121   
122   g_closure_invoke (closure, &result_value, 0, NULL, NULL);
123
124   result = g_value_get_boolean (&result_value);
125   g_value_unset (&result_value);
126
127   return result;
128 }
129
130 static void
131 closure_callback_get (gpointer     cb_data,
132                       GSource     *source,
133                       GSourceFunc *func,
134                       gpointer    *data)
135 {
136   GSourceFunc closure_callback = source->source_funcs->closure_callback;
137
138   if (!closure_callback)
139     {
140       if (source->source_funcs == &g_io_watch_funcs)
141         closure_callback = (GSourceFunc)io_watch_closure_callback;
142 #ifdef G_OS_UNIX
143       else if (source->source_funcs == &g_unix_fd_source_funcs)
144         closure_callback = (GSourceFunc)g_unix_fd_source_closure_callback;
145 #endif
146       else if (source->source_funcs == &g_timeout_funcs ||
147 #ifdef G_OS_UNIX
148                source->source_funcs == &g_unix_signal_funcs ||
149 #endif
150                source->source_funcs == &g_idle_funcs)
151         closure_callback = source_closure_callback;
152     }
153
154   *func = closure_callback;
155   *data = cb_data;
156 }
157
158 static GSourceCallbackFuncs closure_callback_funcs = {
159   (void (*) (gpointer)) g_closure_ref,
160   (void (*) (gpointer)) g_closure_unref,
161   closure_callback_get
162 };
163
164 static void
165 closure_invalidated (gpointer  user_data,
166                      GClosure *closure)
167 {
168   g_source_destroy (user_data);
169 }
170
171 /**
172  * g_source_set_closure:
173  * @source: the source
174  * @closure: a #GClosure
175  *
176  * Set the callback for a source as a #GClosure.
177  *
178  * If the source is not one of the standard GLib types, the @closure_callback
179  * and @closure_marshal fields of the #GSourceFuncs structure must have been
180  * filled in with pointers to appropriate functions.
181  */
182 void
183 g_source_set_closure (GSource  *source,
184                       GClosure *closure)
185 {
186   g_return_if_fail (source != NULL);
187   g_return_if_fail (closure != NULL);
188
189   if (!source->source_funcs->closure_callback &&
190 #ifdef G_OS_UNIX
191       source->source_funcs != &g_unix_fd_source_funcs &&
192       source->source_funcs != &g_unix_signal_funcs &&
193 #endif
194       source->source_funcs != &g_io_watch_funcs &&
195       source->source_funcs != &g_timeout_funcs &&
196       source->source_funcs != &g_idle_funcs)
197     {
198       g_critical (G_STRLOC ": closure can not be set on closure without GSourceFuncs::closure_callback\n");
199       return;
200     }
201
202   g_closure_ref (closure);
203   g_closure_sink (closure);
204   g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
205
206   g_closure_add_invalidate_notifier (closure, source, closure_invalidated);
207
208   if (G_CLOSURE_NEEDS_MARSHAL (closure))
209     {
210       GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
211       if (marshal)
212         g_closure_set_marshal (closure, marshal);
213       else
214         g_closure_set_marshal (closure, g_cclosure_marshal_generic);
215     }
216 }
217
218 static void
219 dummy_closure_marshal (GClosure     *closure,
220                        GValue       *return_value,
221                        guint         n_param_values,
222                        const GValue *param_values,
223                        gpointer      invocation_hint,
224                        gpointer      marshal_data)
225 {
226   if (G_VALUE_HOLDS_BOOLEAN (return_value))
227     g_value_set_boolean (return_value, TRUE);
228 }
229
230 /**
231  * g_source_set_dummy_callback:
232  * @source: the source
233  *
234  * Sets a dummy callback for @source. The callback will do nothing, and
235  * if the source expects a #gboolean return value, it will return %TRUE.
236  * (If the source expects any other type of return value, it will return
237  * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
238  * that type.)
239  *
240  * If the source is not one of the standard GLib types, the
241  * @closure_callback and @closure_marshal fields of the #GSourceFuncs
242  * structure must have been filled in with pointers to appropriate
243  * functions.
244  */
245 void
246 g_source_set_dummy_callback (GSource *source)
247 {
248   GClosure *closure;
249
250   closure = g_closure_new_simple (sizeof (GClosure), NULL);
251   g_closure_set_meta_marshal (closure, NULL, dummy_closure_marshal);
252   g_source_set_closure (source, closure);
253 }