GApplication: send platform data for actions again
[glib.git] / gio / gasynchelper.c
blob4928115d837ad6023cd42b0891b5cec8b3dd5bf6
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
25 #include "gasynchelper.h"
28 /*< private >
29 * SECTION:gasynchelper
30 * @short_description: Asynchronous Helper Functions
31 * @include: gio/gio.h
32 * @see_also: #GAsyncReady
34 * Provides helper functions for asynchronous operations.
36 **/
38 /*************************************************************************
39 * fd source *
40 ************************************************************************/
42 typedef struct
44 GSource source;
45 GPollFD pollfd;
46 } FDSource;
48 static gboolean
49 fd_source_prepare (GSource *source,
50 gint *timeout)
52 *timeout = -1;
53 return FALSE;
56 static gboolean
57 fd_source_check (GSource *source)
59 FDSource *fd_source = (FDSource *)source;
61 return fd_source->pollfd.revents != 0;
64 static gboolean
65 fd_source_dispatch (GSource *source,
66 GSourceFunc callback,
67 gpointer user_data)
70 GFDSourceFunc func = (GFDSourceFunc)callback;
71 FDSource *fd_source = (FDSource *)source;
73 g_warn_if_fail (func != NULL);
75 return (*func) (fd_source->pollfd.fd, fd_source->pollfd.revents, user_data);
78 static void
79 fd_source_finalize (GSource *source)
83 static gboolean
84 fd_source_closure_callback (int fd,
85 GIOCondition condition,
86 gpointer data)
88 GClosure *closure = data;
90 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
91 GValue result_value = G_VALUE_INIT;
92 gboolean result;
94 g_value_init (&result_value, G_TYPE_BOOLEAN);
96 g_value_init (&params[0], G_TYPE_INT);
97 g_value_set_int (&params[0], fd);
99 g_value_init (&params[1], G_TYPE_IO_CONDITION);
100 g_value_set_flags (&params[1], condition);
102 g_closure_invoke (closure, &result_value, 2, params, NULL);
104 result = g_value_get_boolean (&result_value);
105 g_value_unset (&result_value);
106 g_value_unset (&params[0]);
107 g_value_unset (&params[1]);
109 return result;
112 static void
113 fd_source_closure_marshal (GClosure *closure,
114 GValue *return_value,
115 guint n_param_values,
116 const GValue *param_values,
117 gpointer invocation_hint,
118 gpointer marshal_data)
120 GFDSourceFunc callback;
121 GCClosure *cc = (GCClosure*) closure;
122 gboolean v_return;
124 g_return_if_fail (return_value != NULL);
125 g_return_if_fail (n_param_values == 0);
127 callback = (GFDSourceFunc) (marshal_data ? marshal_data : cc->callback);
129 v_return = callback (g_value_get_int (param_values),
130 g_value_get_flags (param_values + 1),
131 closure->data);
133 g_value_set_boolean (return_value, v_return);
136 static GSourceFuncs fd_source_funcs = {
137 fd_source_prepare,
138 fd_source_check,
139 fd_source_dispatch,
140 fd_source_finalize,
141 (GSourceFunc)fd_source_closure_callback,
142 (GSourceDummyMarshal)fd_source_closure_marshal,
145 GSource *
146 _g_fd_source_new (int fd,
147 gushort events,
148 GCancellable *cancellable)
150 GSource *source;
151 FDSource *fd_source;
153 source = g_source_new (&fd_source_funcs, sizeof (FDSource));
154 fd_source = (FDSource *)source;
156 fd_source->pollfd.fd = fd;
157 fd_source->pollfd.events = events;
158 g_source_add_poll (source, &fd_source->pollfd);
160 if (cancellable)
162 GSource *cancellable_source = g_cancellable_source_new (cancellable);
164 g_source_set_dummy_callback (cancellable_source);
165 g_source_add_child_source (source, cancellable_source);
166 g_source_unref (cancellable_source);
169 return source;