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.
20 #include "gobjectalias.h"
21 #include "gsourceclosure.h"
26 #include "gvaluetypes.h"
28 /* This is needed for a proper GBoxedCopyFunc, until the g_io_channel_ref API
29 * returns it's GIOChannel itself #131076.
32 wrap_g_io_channel_ref (GIOChannel *channel)
34 g_io_channel_ref (channel);
39 g_io_channel_get_type (void)
41 static GType our_type = 0;
44 our_type = g_boxed_type_register_static ("GIOChannel",
45 (GBoxedCopyFunc) wrap_g_io_channel_ref,
46 (GBoxedFreeFunc) g_io_channel_unref);
52 g_io_condition_get_type (void)
54 static GType etype = 0;
57 static const GFlagsValue values[] = {
58 { G_IO_IN, "G_IO_IN", "in" },
59 { G_IO_OUT, "G_IO_OUT", "out" },
60 { G_IO_PRI, "G_IO_PRI", "pri" },
61 { G_IO_ERR, "G_IO_ERR", "err" },
62 { G_IO_HUP, "G_IO_HUP", "hup" },
63 { G_IO_NVAL, "G_IO_NVAL", "nval" },
66 etype = g_flags_register_static ("GIOCondition", values);
71 /* We need to hand-write this marshaler, since it doesn't have an
75 source_closure_marshal_BOOLEAN__VOID (GClosure *closure,
78 const GValue *param_values,
79 gpointer invocation_hint,
80 gpointer marshal_data)
83 GCClosure *cc = (GCClosure*) closure;
86 g_return_if_fail (return_value != NULL);
87 g_return_if_fail (n_param_values == 0);
89 callback = (GSourceFunc) (marshal_data ? marshal_data : cc->callback);
91 v_return = callback (closure->data);
93 g_value_set_boolean (return_value, v_return);
97 io_watch_closure_callback (GIOChannel *channel,
98 GIOCondition condition,
101 GClosure *closure = data;
103 GValue params[2] = { { 0, }, { 0, } };
104 GValue result_value = { 0, };
107 g_value_init (&result_value, G_TYPE_BOOLEAN);
108 g_value_init (¶ms[0], G_TYPE_IO_CHANNEL);
109 g_value_set_boxed (¶ms[0], channel);
111 g_value_init (¶ms[1], G_TYPE_IO_CONDITION);
112 g_value_set_flags (¶ms[1], condition);
114 g_closure_invoke (closure, &result_value, 2, params, NULL);
116 result = g_value_get_boolean (&result_value);
117 g_value_unset (&result_value);
118 g_value_unset (¶ms[0]);
119 g_value_unset (¶ms[1]);
125 source_closure_callback (gpointer data)
127 GClosure *closure = data;
128 GValue result_value = { 0, };
131 g_value_init (&result_value, G_TYPE_BOOLEAN);
133 g_closure_invoke (closure, &result_value, 0, NULL, NULL);
135 result = g_value_get_boolean (&result_value);
136 g_value_unset (&result_value);
142 closure_callback_get (gpointer cb_data,
147 GSourceFunc closure_callback = source->source_funcs->closure_callback;
149 if (!closure_callback)
151 if (source->source_funcs == &g_io_watch_funcs)
152 closure_callback = (GSourceFunc)io_watch_closure_callback;
153 else if (source->source_funcs == &g_timeout_funcs ||
154 source->source_funcs == &g_idle_funcs)
155 closure_callback = source_closure_callback;
158 *func = closure_callback;
162 static GSourceCallbackFuncs closure_callback_funcs = {
163 (void (*) (gpointer)) g_closure_ref,
164 (void (*) (gpointer)) g_closure_unref,
169 g_source_set_closure (GSource *source,
172 g_return_if_fail (source != NULL);
173 g_return_if_fail (closure != NULL);
175 if (!source->source_funcs->closure_callback &&
176 source->source_funcs != &g_io_watch_funcs &&
177 source->source_funcs != &g_timeout_funcs &&
178 source->source_funcs != &g_idle_funcs)
180 g_critical (G_STRLOC "closure can not be set on closure without GSourceFuncs::closure_callback\n");
184 g_closure_ref (closure);
185 g_closure_sink (closure);
186 g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
188 if (G_CLOSURE_NEEDS_MARSHAL (closure))
190 GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
193 if (source->source_funcs == &g_idle_funcs ||
194 source->source_funcs == &g_timeout_funcs)
195 marshal = source_closure_marshal_BOOLEAN__VOID;
196 else if (source->source_funcs == &g_io_watch_funcs)
197 marshal = g_cclosure_marshal_BOOLEAN__FLAGS;
200 g_closure_set_marshal (closure, marshal);