gsourceclosure: use g_cclosure_marshal_generic
[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
29 G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_unref)
30
31 GType
32 g_io_condition_get_type (void)
33 {
34   static GType etype = 0;
35   if (etype == 0)
36     {
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" },
44         { 0, NULL, NULL }
45       };
46       etype = g_flags_register_static ("GIOCondition", values);
47     }
48   return etype;
49 }
50
51 static gboolean
52 io_watch_closure_callback (GIOChannel   *channel,
53                            GIOCondition  condition,
54                            gpointer      data)
55 {
56   GClosure *closure = data;
57
58   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
59   GValue result_value = G_VALUE_INIT;
60   gboolean result;
61
62   g_value_init (&result_value, G_TYPE_BOOLEAN);
63   g_value_init (&params[0], G_TYPE_IO_CHANNEL);
64   g_value_set_boxed (&params[0], channel);
65                      
66   g_value_init (&params[1], G_TYPE_IO_CONDITION);
67   g_value_set_flags (&params[1], condition);
68
69   g_closure_invoke (closure, &result_value, 2, params, NULL);
70
71   result = g_value_get_boolean (&result_value);
72   g_value_unset (&result_value);
73   g_value_unset (&params[0]);
74   g_value_unset (&params[1]);
75
76   return result;
77 }
78
79 static gboolean
80 source_closure_callback (gpointer data)
81 {
82   GClosure *closure = data;
83   GValue result_value = G_VALUE_INIT;
84   gboolean result;
85
86   g_value_init (&result_value, G_TYPE_BOOLEAN);
87   
88   g_closure_invoke (closure, &result_value, 0, NULL, NULL);
89
90   result = g_value_get_boolean (&result_value);
91   g_value_unset (&result_value);
92
93   return result;
94 }
95
96 static void
97 closure_callback_get (gpointer     cb_data,
98                       GSource     *source,
99                       GSourceFunc *func,
100                       gpointer    *data)
101 {
102   GSourceFunc closure_callback = source->source_funcs->closure_callback;
103
104   if (!closure_callback)
105     {
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;
111     }
112
113   *func = closure_callback;
114   *data = cb_data;
115 }
116
117 static GSourceCallbackFuncs closure_callback_funcs = {
118   (void (*) (gpointer)) g_closure_ref,
119   (void (*) (gpointer)) g_closure_unref,
120   closure_callback_get
121 };
122
123 static void
124 closure_invalidated (gpointer  user_data,
125                      GClosure *closure)
126 {
127   g_source_destroy (user_data);
128 }
129
130 /**
131  * g_source_set_closure:
132  * @source: the source
133  * @closure: a #GClosure
134  *
135  * Set the callback for a source as a #GClosure.
136  *
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.
140  */
141 void
142 g_source_set_closure (GSource  *source,
143                       GClosure *closure)
144 {
145   g_return_if_fail (source != NULL);
146   g_return_if_fail (closure != NULL);
147
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)
152     {
153       g_critical (G_STRLOC ": closure can not be set on closure without GSourceFuncs::closure_callback\n");
154       return;
155     }
156
157   g_closure_ref (closure);
158   g_closure_sink (closure);
159   g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
160
161   g_closure_add_invalidate_notifier (closure, source, closure_invalidated);
162
163   if (G_CLOSURE_NEEDS_MARSHAL (closure))
164     {
165       GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
166       if (marshal)
167         g_closure_set_marshal (closure, marshal);
168       else
169         g_closure_set_marshal (closure, g_cclosure_marshal_generic);
170     }
171 }
172
173 static void
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)
180 {
181   if (G_VALUE_HOLDS_BOOLEAN (return_value))
182     g_value_set_boolean (return_value, TRUE);
183 }
184
185 /**
186  * g_source_set_dummy_callback:
187  * @source: the source
188  *
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
193  * that type.)
194  *
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
198  * functions.
199  */
200 void
201 g_source_set_dummy_callback (GSource *source)
202 {
203   GClosure *closure;
204
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);
208 }