1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001 Red Hat, Inc.
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.
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.
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.
22 #include "gsourceclosure.h"
27 #include "gvaluetypes.h"
29 #include "glib-unix.h"
32 G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_unref)
35 g_io_condition_get_type (void)
37 static GType etype = 0;
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" },
49 etype = g_flags_register_static ("GIOCondition", values);
55 io_watch_closure_callback (GIOChannel *channel,
56 GIOCondition condition,
59 GClosure *closure = data;
61 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
62 GValue result_value = G_VALUE_INIT;
65 g_value_init (&result_value, G_TYPE_BOOLEAN);
66 g_value_init (¶ms[0], G_TYPE_IO_CHANNEL);
67 g_value_set_boxed (¶ms[0], channel);
69 g_value_init (¶ms[1], G_TYPE_IO_CONDITION);
70 g_value_set_flags (¶ms[1], condition);
72 g_closure_invoke (closure, &result_value, 2, params, NULL);
74 result = g_value_get_boolean (&result_value);
75 g_value_unset (&result_value);
76 g_value_unset (¶ms[0]);
77 g_value_unset (¶ms[1]);
84 g_unix_fd_source_closure_callback (int fd,
85 GIOCondition condition,
88 GClosure *closure = data;
90 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
91 GValue result_value = G_VALUE_INIT;
94 g_value_init (&result_value, G_TYPE_BOOLEAN);
96 g_value_init (¶ms[0], G_TYPE_INT);
97 g_value_set_int (¶ms[0], fd);
99 g_value_init (¶ms[1], G_TYPE_IO_CONDITION);
100 g_value_set_flags (¶ms[1], condition);
102 g_closure_invoke (closure, &result_value, 2, params, NULL);
104 result = g_value_get_boolean (&result_value);
105 g_value_unset (&result_value);
106 g_value_unset (¶ms[0]);
107 g_value_unset (¶ms[1]);
114 source_closure_callback (gpointer data)
116 GClosure *closure = data;
117 GValue result_value = G_VALUE_INIT;
120 g_value_init (&result_value, G_TYPE_BOOLEAN);
122 g_closure_invoke (closure, &result_value, 0, NULL, NULL);
124 result = g_value_get_boolean (&result_value);
125 g_value_unset (&result_value);
131 closure_callback_get (gpointer cb_data,
136 GSourceFunc closure_callback = source->source_funcs->closure_callback;
138 if (!closure_callback)
140 if (source->source_funcs == &g_io_watch_funcs)
141 closure_callback = (GSourceFunc)io_watch_closure_callback;
143 else if (source->source_funcs == &g_unix_fd_source_funcs)
144 closure_callback = (GSourceFunc)g_unix_fd_source_closure_callback;
146 else if (source->source_funcs == &g_timeout_funcs ||
148 source->source_funcs == &g_unix_signal_funcs ||
150 source->source_funcs == &g_idle_funcs)
151 closure_callback = source_closure_callback;
154 *func = closure_callback;
158 static GSourceCallbackFuncs closure_callback_funcs = {
159 (void (*) (gpointer)) g_closure_ref,
160 (void (*) (gpointer)) g_closure_unref,
165 closure_invalidated (gpointer user_data,
168 g_source_destroy (user_data);
172 * g_source_set_closure:
173 * @source: the source
174 * @closure: a #GClosure
176 * Set the callback for a source as a #GClosure.
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.
183 g_source_set_closure (GSource *source,
186 g_return_if_fail (source != NULL);
187 g_return_if_fail (closure != NULL);
189 if (!source->source_funcs->closure_callback &&
191 source->source_funcs != &g_unix_fd_source_funcs &&
192 source->source_funcs != &g_unix_signal_funcs &&
194 source->source_funcs != &g_io_watch_funcs &&
195 source->source_funcs != &g_timeout_funcs &&
196 source->source_funcs != &g_idle_funcs)
198 g_critical (G_STRLOC ": closure can not be set on closure without GSourceFuncs::closure_callback\n");
202 g_closure_ref (closure);
203 g_closure_sink (closure);
204 g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
206 g_closure_add_invalidate_notifier (closure, source, closure_invalidated);
208 if (G_CLOSURE_NEEDS_MARSHAL (closure))
210 GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
212 g_closure_set_marshal (closure, marshal);
214 g_closure_set_marshal (closure, g_cclosure_marshal_generic);
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)
226 if (G_VALUE_HOLDS_BOOLEAN (return_value))
227 g_value_set_boolean (return_value, TRUE);
231 * g_source_set_dummy_callback:
232 * @source: the source
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
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
246 g_source_set_dummy_callback (GSource *source)
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);