/* pipe.c generated by valac 0.16.1, the Vala compiler
 * generated from pipe.vala, do not modify */

/**
 * Copyright (C) 2009-2012 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 **/

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define FSO_FRAMEWORK_TYPE_FD_PIPE (fso_framework_fd_pipe_get_type ())
#define FSO_FRAMEWORK_FD_PIPE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FSO_FRAMEWORK_TYPE_FD_PIPE, FsoFrameworkFdPipe))
#define FSO_FRAMEWORK_FD_PIPE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FSO_FRAMEWORK_TYPE_FD_PIPE, FsoFrameworkFdPipeClass))
#define FSO_FRAMEWORK_IS_FD_PIPE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FSO_FRAMEWORK_TYPE_FD_PIPE))
#define FSO_FRAMEWORK_IS_FD_PIPE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FSO_FRAMEWORK_TYPE_FD_PIPE))
#define FSO_FRAMEWORK_FD_PIPE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FSO_FRAMEWORK_TYPE_FD_PIPE, FsoFrameworkFdPipeClass))

typedef struct _FsoFrameworkFdPipe FsoFrameworkFdPipe;
typedef struct _FsoFrameworkFdPipeClass FsoFrameworkFdPipeClass;
typedef struct _FsoFrameworkFdPipePrivate FsoFrameworkFdPipePrivate;
#define _g_io_channel_unref0(var) ((var == NULL) ? NULL : (var = (g_io_channel_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _FsoFrameworkFdPipe {
	GObject parent_instance;
	FsoFrameworkFdPipePrivate * priv;
};

struct _FsoFrameworkFdPipeClass {
	GObjectClass parent_class;
};

struct _FsoFrameworkFdPipePrivate {
	gchar* buffer;
	gint buffer_length1;
	gint _buffer_size_;
	GIOChannel* source;
	GIOChannel* destout;
	guint sourceWatch;
	guint destoutWatch;
	gint sfd;
	gint dinfd;
	gint doutfd;
};


static gpointer fso_framework_fd_pipe_parent_class = NULL;

GType fso_framework_fd_pipe_get_type (void) G_GNUC_CONST;
#define FSO_FRAMEWORK_FD_PIPE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), FSO_FRAMEWORK_TYPE_FD_PIPE, FsoFrameworkFdPipePrivate))
enum  {
	FSO_FRAMEWORK_FD_PIPE_DUMMY_PROPERTY
};
#define FSO_FRAMEWORK_FD_PIPE_BUFSIZE ((guint) 512)
static gboolean fso_framework_fd_pipe_onAction (FsoFrameworkFdPipe* self, GIOChannel* source, GIOCondition condition);
FsoFrameworkFdPipe* fso_framework_fd_pipe_new (gint s, gint din, gint dout);
FsoFrameworkFdPipe* fso_framework_fd_pipe_construct (GType object_type, gint s, gint din, gint dout);
static gboolean _fso_framework_fd_pipe_onAction_gio_func (GIOChannel* source, GIOCondition condition, gpointer self);
static void fso_framework_fd_pipe_finalize (GObject* obj);


static gboolean fso_framework_fd_pipe_onAction (FsoFrameworkFdPipe* self, GIOChannel* source, GIOCondition condition) {
	gboolean result = FALSE;
	GIOCondition _tmp0_;
	GIOCondition _tmp7_;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (source != NULL, FALSE);
	_tmp0_ = condition;
	if ((_tmp0_ & G_IO_HUP) == G_IO_HUP) {
		GIOChannel* _tmp1_;
		gint _tmp2_ = 0;
		gchar* _tmp3_ = NULL;
		gchar* _tmp4_;
		gchar* _tmp5_ = NULL;
		gchar* _tmp6_;
		_tmp1_ = source;
		_tmp2_ = g_io_channel_unix_get_fd (_tmp1_);
		_tmp3_ = g_strdup_printf ("%i", _tmp2_);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_strconcat ("AutoPipe: HUP from source fd ", _tmp4_, ". Stopping.", NULL);
		_tmp6_ = _tmp5_;
		g_warning ("pipe.vala:42: %s", _tmp6_);
		_g_free0 (_tmp6_);
		_g_free0 (_tmp4_);
		result = FALSE;
		return result;
	}
	_tmp7_ = condition;
	if ((_tmp7_ & G_IO_IN) == G_IO_IN) {
		GIOChannel* _tmp8_;
		gint _tmp9_ = 0;
		gint readfd;
		gint _tmp10_ = 0;
		gint _tmp11_;
		gint _tmp12_;
		gint _tmp15_;
		gint writefd;
		gint _tmp16_;
		gchar* _tmp17_;
		gint _tmp17__length1;
		gssize _tmp18_ = 0L;
		gssize bread;
		gssize _tmp19_;
		gint _tmp20_;
		gchar* _tmp21_;
		gint _tmp21__length1;
		gssize _tmp22_;
		gssize _tmp23_ = 0L;
		gssize bwritten;
		gssize _tmp24_;
		gssize _tmp25_;
		_tmp8_ = source;
		_tmp9_ = g_io_channel_unix_get_fd (_tmp8_);
		readfd = _tmp9_;
		_tmp11_ = readfd;
		_tmp12_ = self->priv->sfd;
		if (_tmp11_ == _tmp12_) {
			gint _tmp13_;
			_tmp13_ = self->priv->dinfd;
			_tmp10_ = _tmp13_;
		} else {
			gint _tmp14_;
			_tmp14_ = self->priv->sfd;
			_tmp10_ = _tmp14_;
		}
		_tmp15_ = _tmp10_;
		writefd = _tmp15_;
		_tmp16_ = readfd;
		_tmp17_ = self->priv->buffer;
		_tmp17__length1 = self->priv->buffer_length1;
		_tmp18_ = read (_tmp16_, _tmp17_, (gsize) FSO_FRAMEWORK_FD_PIPE_BUFSIZE);
		bread = _tmp18_;
		_tmp19_ = bread;
		g_assert (_tmp19_ > ((gssize) 0));
		_tmp20_ = writefd;
		_tmp21_ = self->priv->buffer;
		_tmp21__length1 = self->priv->buffer_length1;
		_tmp22_ = bread;
		_tmp23_ = write (_tmp20_, _tmp21_, (gsize) _tmp22_);
		bwritten = _tmp23_;
		_tmp24_ = bwritten;
		_tmp25_ = bread;
		g_assert (_tmp24_ == _tmp25_);
	} else {
		g_warning ("pipe.vala:58: AutoPipe: Unknown IOCondition. Stopping.");
		result = FALSE;
		return result;
	}
	result = TRUE;
	return result;
}


static gboolean _fso_framework_fd_pipe_onAction_gio_func (GIOChannel* source, GIOCondition condition, gpointer self) {
	gboolean result;
	result = fso_framework_fd_pipe_onAction (self, source, condition);
	return result;
}


FsoFrameworkFdPipe* fso_framework_fd_pipe_construct (GType object_type, gint s, gint din, gint dout) {
	FsoFrameworkFdPipe * self = NULL;
	gint _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	GIOChannel* _tmp4_;
	gint _tmp5_;
	GIOChannel* _tmp6_;
	GIOChannel* _tmp7_;
	guint _tmp8_ = 0U;
	GIOChannel* _tmp9_;
	guint _tmp10_ = 0U;
	gchar* _tmp11_ = NULL;
	self = (FsoFrameworkFdPipe*) g_object_new (object_type, NULL);
	_tmp0_ = s;
	self->priv->sfd = _tmp0_;
	_tmp1_ = din;
	self->priv->dinfd = _tmp1_;
	_tmp2_ = dout;
	self->priv->doutfd = _tmp2_;
	_tmp3_ = s;
	_tmp4_ = g_io_channel_unix_new (_tmp3_);
	_g_io_channel_unref0 (self->priv->source);
	self->priv->source = _tmp4_;
	_tmp5_ = dout;
	_tmp6_ = g_io_channel_unix_new (_tmp5_);
	_g_io_channel_unref0 (self->priv->destout);
	self->priv->destout = _tmp6_;
	_tmp7_ = self->priv->source;
	_tmp8_ = g_io_add_watch (_tmp7_, G_IO_IN | G_IO_HUP, _fso_framework_fd_pipe_onAction_gio_func, self);
	self->priv->sourceWatch = _tmp8_;
	_tmp9_ = self->priv->destout;
	_tmp10_ = g_io_add_watch (_tmp9_, G_IO_IN | G_IO_HUP, _fso_framework_fd_pipe_onAction_gio_func, self);
	self->priv->destoutWatch = _tmp10_;
	_tmp11_ = g_new0 (gchar, FSO_FRAMEWORK_FD_PIPE_BUFSIZE);
	self->priv->buffer = (g_free (self->priv->buffer), NULL);
	self->priv->buffer = _tmp11_;
	self->priv->buffer_length1 = FSO_FRAMEWORK_FD_PIPE_BUFSIZE;
	self->priv->_buffer_size_ = self->priv->buffer_length1;
	return self;
}


FsoFrameworkFdPipe* fso_framework_fd_pipe_new (gint s, gint din, gint dout) {
	return fso_framework_fd_pipe_construct (FSO_FRAMEWORK_TYPE_FD_PIPE, s, din, dout);
}


static void fso_framework_fd_pipe_class_init (FsoFrameworkFdPipeClass * klass) {
	fso_framework_fd_pipe_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (FsoFrameworkFdPipePrivate));
	G_OBJECT_CLASS (klass)->finalize = fso_framework_fd_pipe_finalize;
}


static void fso_framework_fd_pipe_instance_init (FsoFrameworkFdPipe * self) {
	self->priv = FSO_FRAMEWORK_FD_PIPE_GET_PRIVATE (self);
}


static void fso_framework_fd_pipe_finalize (GObject* obj) {
	FsoFrameworkFdPipe * self;
	guint _tmp0_;
	guint _tmp1_;
	self = FSO_FRAMEWORK_FD_PIPE (obj);
	_tmp0_ = self->priv->sourceWatch;
	g_source_remove (_tmp0_);
	_tmp1_ = self->priv->destoutWatch;
	g_source_remove (_tmp1_);
	self->priv->buffer = (g_free (self->priv->buffer), NULL);
	_g_io_channel_unref0 (self->priv->source);
	_g_io_channel_unref0 (self->priv->destout);
	G_OBJECT_CLASS (fso_framework_fd_pipe_parent_class)->finalize (obj);
}


/**
 * @class FsoFramework.FdPipe
 **/
GType fso_framework_fd_pipe_get_type (void) {
	static volatile gsize fso_framework_fd_pipe_type_id__volatile = 0;
	if (g_once_init_enter (&fso_framework_fd_pipe_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (FsoFrameworkFdPipeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) fso_framework_fd_pipe_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FsoFrameworkFdPipe), 0, (GInstanceInitFunc) fso_framework_fd_pipe_instance_init, NULL };
		GType fso_framework_fd_pipe_type_id;
		fso_framework_fd_pipe_type_id = g_type_register_static (G_TYPE_OBJECT, "FsoFrameworkFdPipe", &g_define_type_info, 0);
		g_once_init_leave (&fso_framework_fd_pipe_type_id__volatile, fso_framework_fd_pipe_type_id);
	}
	return fso_framework_fd_pipe_type_id__volatile;
}



