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