Blame gobject/gsourceclosure.c

Packit ae235b
/* GObject - GLib Type, Object, Parameter and Signal Library
Packit ae235b
 * Copyright (C) 2001 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gsourceclosure.h"
Packit ae235b
#include "gboxed.h"
Packit ae235b
#include "genums.h"
Packit ae235b
#include "gmarshal.h"
Packit ae235b
#include "gvalue.h"
Packit ae235b
#include "gvaluetypes.h"
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include "glib-unix.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
G_DEFINE_BOXED_TYPE (GIOChannel, g_io_channel, g_io_channel_ref, g_io_channel_unref)
Packit ae235b
Packit ae235b
GType
Packit ae235b
g_io_condition_get_type (void)
Packit ae235b
{
Packit ae235b
  static volatile GType etype = 0;
Packit ae235b
Packit ae235b
  if (g_once_init_enter (&etype))
Packit ae235b
    {
Packit ae235b
      static const GFlagsValue values[] = {
Packit ae235b
	{ G_IO_IN,   "G_IO_IN",   "in" },
Packit ae235b
	{ G_IO_OUT,  "G_IO_OUT",  "out" },
Packit ae235b
	{ G_IO_PRI,  "G_IO_PRI",  "pri" },
Packit ae235b
	{ G_IO_ERR,  "G_IO_ERR",  "err" },
Packit ae235b
	{ G_IO_HUP,  "G_IO_HUP",  "hup" },
Packit ae235b
	{ G_IO_NVAL, "G_IO_NVAL", "nval" },
Packit ae235b
	{ 0, NULL, NULL }
Packit ae235b
      };
Packit ae235b
      GType type_id = g_flags_register_static ("GIOCondition", values);
Packit ae235b
      g_once_init_leave (&etype, type_id);
Packit ae235b
    }
Packit ae235b
  return etype;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* We need to hand-write this marshaler, since it doesn't have an
Packit ae235b
 * instance object.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
source_closure_marshal_BOOLEAN__VOID (GClosure     *closure,
Packit ae235b
				      GValue       *return_value,
Packit ae235b
				      guint         n_param_values,
Packit ae235b
				      const GValue *param_values,
Packit ae235b
				      gpointer      invocation_hint,
Packit ae235b
				      gpointer      marshal_data)
Packit ae235b
{
Packit ae235b
  GSourceFunc callback;
Packit ae235b
  GCClosure *cc = (GCClosure*) closure;
Packit ae235b
  gboolean v_return;
Packit ae235b
Packit ae235b
  g_return_if_fail (return_value != NULL);
Packit ae235b
  g_return_if_fail (n_param_values == 0);
Packit ae235b
Packit ae235b
  callback = (GSourceFunc) (marshal_data ? marshal_data : cc->callback);
Packit ae235b
Packit ae235b
  v_return = callback (closure->data);
Packit ae235b
Packit ae235b
  g_value_set_boolean (return_value, v_return);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
io_watch_closure_callback (GIOChannel   *channel,
Packit ae235b
			   GIOCondition  condition,
Packit ae235b
			   gpointer      data)
Packit ae235b
{
Packit ae235b
  GClosure *closure = data;
Packit ae235b
Packit ae235b
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
  GValue result_value = G_VALUE_INIT;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  g_value_init (&result_value, G_TYPE_BOOLEAN);
Packit ae235b
  g_value_init (&params[0], G_TYPE_IO_CHANNEL);
Packit ae235b
  g_value_set_boxed (&params[0], channel);
Packit ae235b
		     
Packit ae235b
  g_value_init (&params[1], G_TYPE_IO_CONDITION);
Packit ae235b
  g_value_set_flags (&params[1], condition);
Packit ae235b
Packit ae235b
  g_closure_invoke (closure, &result_value, 2, params, NULL);
Packit ae235b
Packit ae235b
  result = g_value_get_boolean (&result_value);
Packit ae235b
  g_value_unset (&result_value);
Packit ae235b
  g_value_unset (&params[0]);
Packit ae235b
  g_value_unset (&params[1]);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_child_watch_closure_callback (GPid     pid,
Packit ae235b
                                gint     status,
Packit ae235b
                                gpointer data)
Packit ae235b
{
Packit ae235b
  GClosure *closure = data;
Packit ae235b
Packit ae235b
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
  GValue result_value = G_VALUE_INIT;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  g_value_init (&result_value, G_TYPE_BOOLEAN);
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  g_value_init (&params[0], G_TYPE_ULONG);
Packit ae235b
  g_value_set_ulong (&params[0], pid);
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  g_value_init (&params[0], G_TYPE_POINTER);
Packit ae235b
  g_value_set_pointer (&params[0], pid);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  g_value_init (&params[1], G_TYPE_INT);
Packit ae235b
  g_value_set_int (&params[1], status);
Packit ae235b
Packit ae235b
  g_closure_invoke (closure, &result_value, 2, params, NULL);
Packit ae235b
Packit ae235b
  result = g_value_get_boolean (&result_value);
Packit ae235b
  g_value_unset (&result_value);
Packit ae235b
  g_value_unset (&params[0]);
Packit ae235b
  g_value_unset (&params[1]);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static gboolean
Packit ae235b
g_unix_fd_source_closure_callback (int           fd,
Packit ae235b
                                   GIOCondition  condition,
Packit ae235b
                                   gpointer      data)
Packit ae235b
{
Packit ae235b
  GClosure *closure = data;
Packit ae235b
Packit ae235b
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
  GValue result_value = G_VALUE_INIT;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  g_value_init (&result_value, G_TYPE_BOOLEAN);
Packit ae235b
Packit ae235b
  g_value_init (&params[0], G_TYPE_INT);
Packit ae235b
  g_value_set_int (&params[0], fd);
Packit ae235b
Packit ae235b
  g_value_init (&params[1], G_TYPE_IO_CONDITION);
Packit ae235b
  g_value_set_flags (&params[1], condition);
Packit ae235b
Packit ae235b
  g_closure_invoke (closure, &result_value, 2, params, NULL);
Packit ae235b
Packit ae235b
  result = g_value_get_boolean (&result_value);
Packit ae235b
  g_value_unset (&result_value);
Packit ae235b
  g_value_unset (&params[0]);
Packit ae235b
  g_value_unset (&params[1]);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
source_closure_callback (gpointer data)
Packit ae235b
{
Packit ae235b
  GClosure *closure = data;
Packit ae235b
  GValue result_value = G_VALUE_INIT;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  g_value_init (&result_value, G_TYPE_BOOLEAN);
Packit ae235b
  
Packit ae235b
  g_closure_invoke (closure, &result_value, 0, NULL, NULL);
Packit ae235b
Packit ae235b
  result = g_value_get_boolean (&result_value);
Packit ae235b
  g_value_unset (&result_value);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
closure_callback_get (gpointer     cb_data,
Packit ae235b
		      GSource     *source,
Packit ae235b
		      GSourceFunc *func,
Packit ae235b
		      gpointer    *data)
Packit ae235b
{
Packit ae235b
  GSourceFunc closure_callback = source->source_funcs->closure_callback;
Packit ae235b
Packit ae235b
  if (!closure_callback)
Packit ae235b
    {
Packit ae235b
      if (source->source_funcs == &g_io_watch_funcs)
Packit ae235b
        closure_callback = (GSourceFunc)io_watch_closure_callback;
Packit ae235b
      else if (source->source_funcs == &g_child_watch_funcs)
Packit ae235b
        closure_callback = (GSourceFunc)g_child_watch_closure_callback;
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
      else if (source->source_funcs == &g_unix_fd_source_funcs)
Packit ae235b
        closure_callback = (GSourceFunc)g_unix_fd_source_closure_callback;
Packit ae235b
#endif
Packit ae235b
      else if (source->source_funcs == &g_timeout_funcs ||
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
               source->source_funcs == &g_unix_signal_funcs ||
Packit ae235b
#endif
Packit ae235b
               source->source_funcs == &g_idle_funcs)
Packit ae235b
        closure_callback = source_closure_callback;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *func = closure_callback;
Packit ae235b
  *data = cb_data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSourceCallbackFuncs closure_callback_funcs = {
Packit ae235b
  (void (*) (gpointer)) g_closure_ref,
Packit ae235b
  (void (*) (gpointer)) g_closure_unref,
Packit ae235b
  closure_callback_get
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
closure_invalidated (gpointer  user_data,
Packit ae235b
                     GClosure *closure)
Packit ae235b
{
Packit ae235b
  g_source_destroy (user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_source_set_closure:
Packit ae235b
 * @source: the source
Packit ae235b
 * @closure: a #GClosure
Packit ae235b
 *
Packit ae235b
 * Set the callback for a source as a #GClosure.
Packit ae235b
 *
Packit ae235b
 * If the source is not one of the standard GLib types, the @closure_callback
Packit ae235b
 * and @closure_marshal fields of the #GSourceFuncs structure must have been
Packit ae235b
 * filled in with pointers to appropriate functions.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_source_set_closure (GSource  *source,
Packit ae235b
		      GClosure *closure)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (source != NULL);
Packit ae235b
  g_return_if_fail (closure != NULL);
Packit ae235b
Packit ae235b
  if (!source->source_funcs->closure_callback &&
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
      source->source_funcs != &g_unix_fd_source_funcs &&
Packit ae235b
      source->source_funcs != &g_unix_signal_funcs &&
Packit ae235b
#endif
Packit ae235b
      source->source_funcs != &g_child_watch_funcs &&
Packit ae235b
      source->source_funcs != &g_io_watch_funcs &&
Packit ae235b
      source->source_funcs != &g_timeout_funcs &&
Packit ae235b
      source->source_funcs != &g_idle_funcs)
Packit ae235b
    {
Packit ae235b
      g_critical (G_STRLOC ": closure cannot be set on GSource without GSourceFuncs::closure_callback\n");
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_closure_ref (closure);
Packit ae235b
  g_closure_sink (closure);
Packit ae235b
  g_source_set_callback_indirect (source, closure, &closure_callback_funcs);
Packit ae235b
Packit ae235b
  g_closure_add_invalidate_notifier (closure, source, closure_invalidated);
Packit ae235b
Packit ae235b
  if (G_CLOSURE_NEEDS_MARSHAL (closure))
Packit ae235b
    {
Packit ae235b
      GClosureMarshal marshal = (GClosureMarshal)source->source_funcs->closure_marshal;
Packit ae235b
      if (marshal)
Packit ae235b
	g_closure_set_marshal (closure, marshal);
Packit ae235b
      else if (source->source_funcs == &g_idle_funcs ||
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
               source->source_funcs == &g_unix_signal_funcs ||
Packit ae235b
#endif
Packit ae235b
               source->source_funcs == &g_timeout_funcs)
Packit ae235b
	g_closure_set_marshal (closure, source_closure_marshal_BOOLEAN__VOID);
Packit ae235b
      else
Packit ae235b
        g_closure_set_marshal (closure, g_cclosure_marshal_generic);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
dummy_closure_marshal (GClosure     *closure,
Packit ae235b
		       GValue       *return_value,
Packit ae235b
		       guint         n_param_values,
Packit ae235b
		       const GValue *param_values,
Packit ae235b
		       gpointer      invocation_hint,
Packit ae235b
		       gpointer      marshal_data)
Packit ae235b
{
Packit ae235b
  if (G_VALUE_HOLDS_BOOLEAN (return_value))
Packit ae235b
    g_value_set_boolean (return_value, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_source_set_dummy_callback:
Packit ae235b
 * @source: the source
Packit ae235b
 *
Packit ae235b
 * Sets a dummy callback for @source. The callback will do nothing, and
Packit ae235b
 * if the source expects a #gboolean return value, it will return %TRUE.
Packit ae235b
 * (If the source expects any other type of return value, it will return
Packit ae235b
 * a 0/%NULL value; whatever g_value_init() initializes a #GValue to for
Packit ae235b
 * that type.)
Packit ae235b
 *
Packit ae235b
 * If the source is not one of the standard GLib types, the
Packit ae235b
 * @closure_callback and @closure_marshal fields of the #GSourceFuncs
Packit ae235b
 * structure must have been filled in with pointers to appropriate
Packit ae235b
 * functions.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_source_set_dummy_callback (GSource *source)
Packit ae235b
{
Packit ae235b
  GClosure *closure;
Packit ae235b
Packit ae235b
  closure = g_closure_new_simple (sizeof (GClosure), NULL);
Packit ae235b
  g_closure_set_meta_marshal (closure, NULL, dummy_closure_marshal);
Packit ae235b
  g_source_set_closure (source, closure);
Packit ae235b
}