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