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


#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <fsobasics.h>
#include <fcntl.h>
#include <errno.h>


#define FSO_FRAMEWORK_TYPE_TRANSPORT (fso_framework_transport_get_type ())
#define FSO_FRAMEWORK_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FSO_FRAMEWORK_TYPE_TRANSPORT, FsoFrameworkTransport))
#define FSO_FRAMEWORK_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FSO_FRAMEWORK_TYPE_TRANSPORT, FsoFrameworkTransportClass))
#define FSO_FRAMEWORK_IS_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FSO_FRAMEWORK_TYPE_TRANSPORT))
#define FSO_FRAMEWORK_IS_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FSO_FRAMEWORK_TYPE_TRANSPORT))
#define FSO_FRAMEWORK_TRANSPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FSO_FRAMEWORK_TYPE_TRANSPORT, FsoFrameworkTransportClass))

typedef struct _FsoFrameworkTransport FsoFrameworkTransport;
typedef struct _FsoFrameworkTransportClass FsoFrameworkTransportClass;
typedef struct _FsoFrameworkTransportPrivate FsoFrameworkTransportPrivate;

#define FSO_FRAMEWORK_TYPE_BASE_TRANSPORT (fso_framework_base_transport_get_type ())
#define FSO_FRAMEWORK_BASE_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FSO_FRAMEWORK_TYPE_BASE_TRANSPORT, FsoFrameworkBaseTransport))
#define FSO_FRAMEWORK_BASE_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FSO_FRAMEWORK_TYPE_BASE_TRANSPORT, FsoFrameworkBaseTransportClass))
#define FSO_FRAMEWORK_IS_BASE_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FSO_FRAMEWORK_TYPE_BASE_TRANSPORT))
#define FSO_FRAMEWORK_IS_BASE_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FSO_FRAMEWORK_TYPE_BASE_TRANSPORT))
#define FSO_FRAMEWORK_BASE_TRANSPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FSO_FRAMEWORK_TYPE_BASE_TRANSPORT, FsoFrameworkBaseTransportClass))

typedef struct _FsoFrameworkBaseTransport FsoFrameworkBaseTransport;
typedef struct _FsoFrameworkBaseTransportClass FsoFrameworkBaseTransportClass;
typedef struct _FsoFrameworkBaseTransportPrivate FsoFrameworkBaseTransportPrivate;

#define FSO_FRAMEWORK_TYPE_PTY_TRANSPORT (fso_framework_pty_transport_get_type ())
#define FSO_FRAMEWORK_PTY_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FSO_FRAMEWORK_TYPE_PTY_TRANSPORT, FsoFrameworkPtyTransport))
#define FSO_FRAMEWORK_PTY_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FSO_FRAMEWORK_TYPE_PTY_TRANSPORT, FsoFrameworkPtyTransportClass))
#define FSO_FRAMEWORK_IS_PTY_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FSO_FRAMEWORK_TYPE_PTY_TRANSPORT))
#define FSO_FRAMEWORK_IS_PTY_TRANSPORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FSO_FRAMEWORK_TYPE_PTY_TRANSPORT))
#define FSO_FRAMEWORK_PTY_TRANSPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FSO_FRAMEWORK_TYPE_PTY_TRANSPORT, FsoFrameworkPtyTransportClass))

typedef struct _FsoFrameworkPtyTransport FsoFrameworkPtyTransport;
typedef struct _FsoFrameworkPtyTransportClass FsoFrameworkPtyTransportClass;
typedef struct _FsoFrameworkPtyTransportPrivate FsoFrameworkPtyTransportPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))

typedef void (*FsoFrameworkTransportFunc) (FsoFrameworkTransport* transport, void* user_data);
struct _FsoFrameworkTransport {
	GObject parent_instance;
	FsoFrameworkTransportPrivate * priv;
	FsoFrameworkLogger* logger;
};

struct _FsoFrameworkTransportClass {
	GObjectClass parent_class;
	gboolean (*isOpen) (FsoFrameworkTransport* self);
	void (*openAsync) (FsoFrameworkTransport* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
	gboolean (*openAsync_finish) (FsoFrameworkTransport* self, GAsyncResult* _res_);
	gboolean (*open) (FsoFrameworkTransport* self);
	void (*close) (FsoFrameworkTransport* self);
	gchar* (*getName) (FsoFrameworkTransport* self);
	void (*setDelegates) (FsoFrameworkTransport* self, FsoFrameworkTransportFunc readfunc, void* readfunc_target, FsoFrameworkTransportFunc hupfunc, void* hupfunc_target);
	void (*getDelegates) (FsoFrameworkTransport* self, FsoFrameworkTransportFunc* readfun, void** readfun_target, GDestroyNotify* readfun_target_destroy_notify, FsoFrameworkTransportFunc* hupfun, void** hupfun_target, GDestroyNotify* hupfun_target_destroy_notify);
	void (*setPriorities) (FsoFrameworkTransport* self, gint rp, gint wp);
	void (*setBuffered) (FsoFrameworkTransport* self, gboolean on);
	gint (*writeAndRead) (FsoFrameworkTransport* self, void* wdata, gint wlength, void* rdata, gint rlength, gint maxWait);
	gint (*read) (FsoFrameworkTransport* self, void* data, gint length);
	gint (*write) (FsoFrameworkTransport* self, void* data, gint length);
	gint (*freeze) (FsoFrameworkTransport* self);
	void (*thaw) (FsoFrameworkTransport* self);
	void (*drain) (FsoFrameworkTransport* self);
	void (*flush) (FsoFrameworkTransport* self);
	gboolean (*suspend) (FsoFrameworkTransport* self);
	void (*resume) (FsoFrameworkTransport* self);
};

struct _FsoFrameworkBaseTransport {
	FsoFrameworkTransport parent_instance;
	FsoFrameworkBaseTransportPrivate * priv;
	gchar* name;
	guint speed;
	gboolean raw;
	gboolean hard;
	gint fd;
	FsoFrameworkTransportFunc hupfunc;
	gpointer hupfunc_target;
	GDestroyNotify hupfunc_target_destroy_notify;
	FsoFrameworkTransportFunc readfunc;
	gpointer readfunc_target;
	GDestroyNotify readfunc_target_destroy_notify;
	GByteArray* buffer;
};

struct _FsoFrameworkBaseTransportClass {
	FsoFrameworkTransportClass parent_class;
	gssize (*_real_write) (FsoFrameworkBaseTransport* self, gint fd, void* data, gint len);
	void (*configure) (FsoFrameworkBaseTransport* self);
	gchar* (*repr) (FsoFrameworkBaseTransport* self);
	gssize (*_real_read) (FsoFrameworkBaseTransport* self, gint fd, void* data, gint len);
};

struct _FsoFrameworkPtyTransport {
	FsoFrameworkBaseTransport parent_instance;
	FsoFrameworkPtyTransportPrivate * priv;
};

struct _FsoFrameworkPtyTransportClass {
	FsoFrameworkBaseTransportClass parent_class;
};

struct _FsoFrameworkPtyTransportPrivate {
	gchar* ptyname;
	gint ptyname_length1;
	gint _ptyname_size_;
};


static gpointer fso_framework_pty_transport_parent_class = NULL;

GType fso_framework_transport_get_type (void) G_GNUC_CONST;
GType fso_framework_base_transport_get_type (void) G_GNUC_CONST;
GType fso_framework_pty_transport_get_type (void) G_GNUC_CONST;
#define FSO_FRAMEWORK_PTY_TRANSPORT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), FSO_FRAMEWORK_TYPE_PTY_TRANSPORT, FsoFrameworkPtyTransportPrivate))
enum  {
	FSO_FRAMEWORK_PTY_TRANSPORT_DUMMY_PROPERTY
};
FsoFrameworkPtyTransport* fso_framework_pty_transport_new (void);
FsoFrameworkPtyTransport* fso_framework_pty_transport_construct (GType object_type);
FsoFrameworkBaseTransport* fso_framework_base_transport_new (const gchar* name, guint speed, gboolean raw, gboolean hard);
FsoFrameworkBaseTransport* fso_framework_base_transport_construct (GType object_type, const gchar* name, guint speed, gboolean raw, gboolean hard);
static gchar* fso_framework_pty_transport_real_getName (FsoFrameworkTransport* base);
static gchar* fso_framework_pty_transport_real_repr (FsoFrameworkBaseTransport* base);
gchar* fso_framework_transport_getName (FsoFrameworkTransport* self);
static gboolean fso_framework_pty_transport_real_open (FsoFrameworkTransport* base);
void fso_framework_base_transport_configure (FsoFrameworkBaseTransport* self);
gboolean fso_framework_transport_open (FsoFrameworkTransport* self);
static void fso_framework_pty_transport_finalize (GObject* obj);


FsoFrameworkPtyTransport* fso_framework_pty_transport_construct (GType object_type) {
	FsoFrameworkPtyTransport * self = NULL;
	self = (FsoFrameworkPtyTransport*) fso_framework_base_transport_construct (object_type, "Pty", (guint) 115200, TRUE, TRUE);
	return self;
}


FsoFrameworkPtyTransport* fso_framework_pty_transport_new (void) {
	return fso_framework_pty_transport_construct (FSO_FRAMEWORK_TYPE_PTY_TRANSPORT);
}


static gchar* fso_framework_pty_transport_real_getName (FsoFrameworkTransport* base) {
	FsoFrameworkPtyTransport * self;
	gchar* result = NULL;
	gchar* _tmp0_;
	gint _tmp0__length1;
	gchar* _tmp1_;
	self = (FsoFrameworkPtyTransport*) base;
	_tmp0_ = self->priv->ptyname;
	_tmp0__length1 = self->priv->ptyname_length1;
	_tmp1_ = g_strdup ((const gchar*) _tmp0_);
	result = _tmp1_;
	return result;
}


static gchar* fso_framework_pty_transport_real_repr (FsoFrameworkBaseTransport* base) {
	FsoFrameworkPtyTransport * self;
	gchar* result = NULL;
	gchar* _tmp0_ = NULL;
	gchar* _tmp1_;
	gint _tmp2_;
	gchar* _tmp3_ = NULL;
	gchar* _tmp4_;
	self = (FsoFrameworkPtyTransport*) base;
	_tmp0_ = fso_framework_transport_getName ((FsoFrameworkTransport*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = ((FsoFrameworkBaseTransport*) self)->fd;
	_tmp3_ = g_strdup_printf ("<PTY %s (fd %d)>", _tmp1_, _tmp2_);
	_tmp4_ = _tmp3_;
	_g_free0 (_tmp1_);
	result = _tmp4_;
	return result;
}


static gboolean fso_framework_pty_transport_real_open (FsoFrameworkTransport* base) {
	FsoFrameworkPtyTransport * self;
	gboolean result = FALSE;
	gint _tmp0_ = 0;
	gint _tmp1_;
	gint _tmp8_;
	gint _tmp9_;
	gint _tmp10_;
	gchar* _tmp11_;
	gint _tmp11__length1;
	gint _tmp12_;
	gint _tmp13_ = 0;
	gint flags;
	gint _tmp14_;
	gint _tmp15_;
	gint _tmp16_ = 0;
	gint res;
	gint _tmp17_;
	gboolean _tmp23_ = FALSE;
	self = (FsoFrameworkPtyTransport*) base;
	_tmp0_ = posix_openpt ((O_RDWR | O_NOCTTY) | O_NONBLOCK);
	((FsoFrameworkBaseTransport*) self)->fd = _tmp0_;
	_tmp1_ = ((FsoFrameworkBaseTransport*) self)->fd;
	if (_tmp1_ == (-1)) {
		FsoFrameworkLogger* _tmp2_;
		const gchar* _tmp3_;
		gint _tmp4_;
		const gchar* _tmp5_ = NULL;
		gchar* _tmp6_ = NULL;
		gchar* _tmp7_;
		_tmp2_ = ((FsoFrameworkTransport*) self)->logger;
		_tmp3_ = ((FsoFrameworkBaseTransport*) self)->name;
		_tmp4_ = errno;
		_tmp5_ = strerror (_tmp4_);
		_tmp6_ = g_strdup_printf ("could not open %s: %s", _tmp3_, _tmp5_);
		_tmp7_ = _tmp6_;
		fso_framework_logger_warning (_tmp2_, _tmp7_);
		_g_free0 (_tmp7_);
		result = FALSE;
		return result;
	}
	_tmp8_ = ((FsoFrameworkBaseTransport*) self)->fd;
	grantpt (_tmp8_);
	_tmp9_ = ((FsoFrameworkBaseTransport*) self)->fd;
	unlockpt (_tmp9_);
	_tmp10_ = ((FsoFrameworkBaseTransport*) self)->fd;
	_tmp11_ = self->priv->ptyname;
	_tmp11__length1 = self->priv->ptyname_length1;
	ptsname_r (_tmp10_, _tmp11_, _tmp11__length1);
	_tmp12_ = ((FsoFrameworkBaseTransport*) self)->fd;
	_tmp13_ = fcntl (_tmp12_, F_GETFL, NULL);
	flags = _tmp13_;
	_tmp14_ = ((FsoFrameworkBaseTransport*) self)->fd;
	_tmp15_ = flags;
	_tmp16_ = fcntl (_tmp14_, F_SETFL, _tmp15_ | O_NONBLOCK, NULL);
	res = _tmp16_;
	_tmp17_ = res;
	if (_tmp17_ < 0) {
		FsoFrameworkLogger* _tmp18_;
		gint _tmp19_;
		const gchar* _tmp20_ = NULL;
		gchar* _tmp21_ = NULL;
		gchar* _tmp22_;
		_tmp18_ = ((FsoFrameworkTransport*) self)->logger;
		_tmp19_ = errno;
		_tmp20_ = strerror (_tmp19_);
		_tmp21_ = g_strdup_printf ("can't set pty master to NONBLOCK: %s", _tmp20_);
		_tmp22_ = _tmp21_;
		fso_framework_logger_warning (_tmp18_, _tmp22_);
		_g_free0 (_tmp22_);
	}
	fso_framework_base_transport_configure ((FsoFrameworkBaseTransport*) self);
	_tmp23_ = FSO_FRAMEWORK_TRANSPORT_CLASS (fso_framework_pty_transport_parent_class)->open ((FsoFrameworkTransport*) FSO_FRAMEWORK_BASE_TRANSPORT (self));
	result = _tmp23_;
	return result;
}


static void fso_framework_pty_transport_class_init (FsoFrameworkPtyTransportClass * klass) {
	fso_framework_pty_transport_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (FsoFrameworkPtyTransportPrivate));
	FSO_FRAMEWORK_TRANSPORT_CLASS (klass)->getName = fso_framework_pty_transport_real_getName;
	FSO_FRAMEWORK_BASE_TRANSPORT_CLASS (klass)->repr = fso_framework_pty_transport_real_repr;
	FSO_FRAMEWORK_TRANSPORT_CLASS (klass)->open = fso_framework_pty_transport_real_open;
	G_OBJECT_CLASS (klass)->finalize = fso_framework_pty_transport_finalize;
}


static void fso_framework_pty_transport_instance_init (FsoFrameworkPtyTransport * self) {
	gchar* _tmp0_ = NULL;
	self->priv = FSO_FRAMEWORK_PTY_TRANSPORT_GET_PRIVATE (self);
	_tmp0_ = g_new0 (gchar, 1024);
	self->priv->ptyname = _tmp0_;
	self->priv->ptyname_length1 = 1024;
	self->priv->_ptyname_size_ = self->priv->ptyname_length1;
}


static void fso_framework_pty_transport_finalize (GObject* obj) {
	FsoFrameworkPtyTransport * self;
	self = FSO_FRAMEWORK_PTY_TRANSPORT (obj);
	self->priv->ptyname = (g_free (self->priv->ptyname), NULL);
	G_OBJECT_CLASS (fso_framework_pty_transport_parent_class)->finalize (obj);
}


/**
 * 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
 *
 */
GType fso_framework_pty_transport_get_type (void) {
	static volatile gsize fso_framework_pty_transport_type_id__volatile = 0;
	if (g_once_init_enter (&fso_framework_pty_transport_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (FsoFrameworkPtyTransportClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) fso_framework_pty_transport_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FsoFrameworkPtyTransport), 0, (GInstanceInitFunc) fso_framework_pty_transport_instance_init, NULL };
		GType fso_framework_pty_transport_type_id;
		fso_framework_pty_transport_type_id = g_type_register_static (FSO_FRAMEWORK_TYPE_BASE_TRANSPORT, "FsoFrameworkPtyTransport", &g_define_type_info, 0);
		g_once_init_leave (&fso_framework_pty_transport_type_id__volatile, fso_framework_pty_transport_type_id);
	}
	return fso_framework_pty_transport_type_id__volatile;
}



