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