Removed is_refcounted and GBoxedInitFunc from
[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 "gsourceclosure.h"
21 #include "gboxed.h"
22 #include "genums.h"
23 #include "gmarshal.h"
24 #include "gvalue.h"
25 #include "gvaluetypes.h"
26
27 GType
28 g_io_channel_get_type (void)
29 {
30   static GType our_type = 0;
31   
32   if (our_type == 0)
33     our_type = g_boxed_type_register_static ("GIOChannel",
34                                              (GBoxedCopyFunc) g_io_channel_ref,
35                                              (GBoxedFreeFunc) g_io_channel_unref);
36
37   return our_type;
38 }
39
40 GType
41 g_io_condition_get_type (void)
42 {
43   static GType etype = 0;
44   if (etype == 0)
45     {
46       static const GFlagsValue values[] = {
47         { G_IO_IN,   "G_IO_IN",   "in" },
48         { G_IO_OUT,  "G_IO_OUT",  "out" },
49         { G_IO_PRI,  "G_IO_PRI",  "pri" },
50         { G_IO_ERR,  "G_IO_ERR",  "err" },
51         { G_IO_HUP,  "G_IO_HUP",  "hup" },
52         { G_IO_NVAL, "G_IO_NVAL", "nval" },
53         { 0, NULL, NULL }
54       };
55       etype = g_flags_register_static ("GIOCondition", values);
56     }
57   return etype;
58 }
59
60 /* We need to hand-write this marshaler, since it doesn't have an
61  * instance object.
62  */
63 static void
64 source_closure_marshal_BOOLEAN__VOID (GClosure     *closure,
65                                       GValue       *return_value,
66                                       guint         n_param_values,
67                                       const GValue *param_values,
68                                       gpointer      invocation_hint,
69                                       gpointer      marshal_data)
70 {
71   GSourceFunc callback;
72   GCClosure *cc = (GCClosure*) closure;
73   gboolean v_return;
74
75   g_return_if_fail (return_value != NULL);
76   g_return_if_fail (n_param_values == 0);
77
78   callback = (GSourceFunc) (marshal_data ? marshal_data : cc->callback);
79
80   v_return = callback (closure->data);
81
82   g_value_set_boolean (return_value, v_return);
83 }
84
85 static gboolean
86 io_watch_closure_callback (GIOChannel   *channel,
87                            GIOCondition  condition,
88                            gpointer      data)
89 {
90   GClosure *closure = data;
91
92   GValue params[2] = { { 0, }, { 0, } };
93   GValue result_value = { 0, };
94   gboolean result;
95
96   g_value_init (&result_value, G_TYPE_BOOLEAN);
97   g_value_init (&params[0], G_TYPE_IO_CHANNEL);
98   g_value_set_boxed (&params[0], channel);
99                      
100   g_value_init (&params[1], G_TYPE_IO_CONDITION);
101   g_value_set_flags (&params[1], condition);
102
103   g_closure_invoke (closure, &result_value, 2, params, NULL);
104
105   result = g_value_get_boolean (&result_value);
106   g_value_unset (&result_value);
107   g_value_unset (&params[0]);
108   g_value_unset (&params[1]);
109
110   return result;
111 }
112
113 static gboolean
114 source_closure_callback (gpointer data)
115 {
116   GClosure *closure = data;
117   GValue result_value = { 0, };
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       else if (source->source_funcs == &g_timeout_funcs ||
143                source->source_funcs == &g_idle_funcs)
144         closure_callback = source_closure_callback;
145     }
146
147   *func = closure_callback;
148   *data = cb_data;
149 }
150
151 static GSourceCallbackFuncs closure_callback_funcs = {
152   (void (*) (gpointer)) g_closure_ref,
153   (void (*) (gpointer)) g_closure_unref,
154   closure_callback_get
155 };
156
157 /**
158  * g_source_set_callback:
159  * @source: the source
160  * @func: a #GClosure
161  *
162  * Set the callback for a source as a #GClosure.
163  *
164  * If the source is not one of the standard GLib types, the closure_callback
165  * and closure_marshal fields of the GSourceFuncs structure must have been
166  * filled in with pointers to appropriate functions.
167  **/
168 void
169 g_source_set_closure (GSource  *source,
170                       GClosure *closure)
171 {
172   g_return_if_fail (source != NULL);
173   g_return_if_fail (closure != NULL);
174
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)
179     {
180       g_critical (G_STRLOC "closure can not be set on closure without GSourceFuncs::closure_callback\n");
181       return;
182     }
183
184   g_closure_ref (closure);
185   g_closure_sink (closure);
186   g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
187
188   if (G_CLOSURE_NEEDS_MARSHAL (closure))
189     {
190       GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
191       if (!marshal)
192         {
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;
198         }
199       if (marshal)
200         g_closure_set_marshal (closure, marshal);
201     }
202 }