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 G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_unref)
32 g_io_condition_get_type (void)
34 static GType etype = 0;
37 static const GFlagsValue values[] = {
38 { G_IO_IN, "G_IO_IN", "in" },
39 { G_IO_OUT, "G_IO_OUT", "out" },
40 { G_IO_PRI, "G_IO_PRI", "pri" },
41 { G_IO_ERR, "G_IO_ERR", "err" },
42 { G_IO_HUP, "G_IO_HUP", "hup" },
43 { G_IO_NVAL, "G_IO_NVAL", "nval" },
46 etype = g_flags_register_static ("GIOCondition", values);
52 io_watch_closure_callback (GIOChannel *channel,
53 GIOCondition condition,
56 GClosure *closure = data;
58 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
59 GValue result_value = G_VALUE_INIT;
62 g_value_init (&result_value, G_TYPE_BOOLEAN);
63 g_value_init (¶ms[0], G_TYPE_IO_CHANNEL);
64 g_value_set_boxed (¶ms[0], channel);
66 g_value_init (¶ms[1], G_TYPE_IO_CONDITION);
67 g_value_set_flags (¶ms[1], condition);
69 g_closure_invoke (closure, &result_value, 2, params, NULL);
71 result = g_value_get_boolean (&result_value);
72 g_value_unset (&result_value);
73 g_value_unset (¶ms[0]);
74 g_value_unset (¶ms[1]);
80 source_closure_callback (gpointer data)
82 GClosure *closure = data;
83 GValue result_value = G_VALUE_INIT;
86 g_value_init (&result_value, G_TYPE_BOOLEAN);
88 g_closure_invoke (closure, &result_value, 0, NULL, NULL);
90 result = g_value_get_boolean (&result_value);
91 g_value_unset (&result_value);
97 closure_callback_get (gpointer cb_data,
102 GSourceFunc closure_callback = source->source_funcs->closure_callback;
104 if (!closure_callback)
106 if (source->source_funcs == &g_io_watch_funcs)
107 closure_callback = (GSourceFunc)io_watch_closure_callback;
108 else if (source->source_funcs == &g_timeout_funcs ||
109 source->source_funcs == &g_idle_funcs)
110 closure_callback = source_closure_callback;
113 *func = closure_callback;
117 static GSourceCallbackFuncs closure_callback_funcs = {
118 (void (*) (gpointer)) g_closure_ref,
119 (void (*) (gpointer)) g_closure_unref,
124 closure_invalidated (gpointer user_data,
127 g_source_destroy (user_data);
131 * g_source_set_closure:
132 * @source: the source
133 * @closure: a #GClosure
135 * Set the callback for a source as a #GClosure.
137 * If the source is not one of the standard GLib types, the @closure_callback
138 * and @closure_marshal fields of the #GSourceFuncs structure must have been
139 * filled in with pointers to appropriate functions.
142 g_source_set_closure (GSource *source,
145 g_return_if_fail (source != NULL);
146 g_return_if_fail (closure != NULL);
148 if (!source->source_funcs->closure_callback &&
149 source->source_funcs != &g_io_watch_funcs &&
150 source->source_funcs != &g_timeout_funcs &&
151 source->source_funcs != &g_idle_funcs)
153 g_critical (G_STRLOC ": closure can not be set on closure without GSourceFuncs::closure_callback\n");
157 g_closure_ref (closure);
158 g_closure_sink (closure);
159 g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
161 g_closure_add_invalidate_notifier (closure, source, closure_invalidated);
163 if (G_CLOSURE_NEEDS_MARSHAL (closure))
165 GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
167 g_closure_set_marshal (closure, marshal);
169 g_closure_set_marshal (closure, g_cclosure_marshal_generic);
174 dummy_closure_marshal (GClosure *closure,
175 GValue *return_value,
176 guint n_param_values,
177 const GValue *param_values,
178 gpointer invocation_hint,
179 gpointer marshal_data)
181 if (G_VALUE_HOLDS_BOOLEAN (return_value))
182 g_value_set_boolean (return_value, TRUE);
186 * g_source_set_dummy_callback:
187 * @source: the source
189 * Sets a dummy callback for @source. The callback will do nothing, and
190 * if the source expects a #gboolean return value, it will return %TRUE.
191 * (If the source expects any other type of return value, it will return
192 * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
195 * If the source is not one of the standard GLib types, the
196 * @closure_callback and @closure_marshal fields of the #GSourceFuncs
197 * structure must have been filled in with pointers to appropriate
201 g_source_set_dummy_callback (GSource *source)
205 closure = g_closure_new_simple (sizeof (GClosure), NULL);
206 g_closure_set_meta_marshal (closure, NULL, dummy_closure_marshal);
207 g_source_set_closure (source, closure);