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