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

/*
 * Copyright 2018 elementary, Inc. (https://elementary.io)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Authors: Corentin Noël <corentin@elementary.io>
 */

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

#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define GREETER_TYPE_SUBPROCESS_SUPERVISOR (greeter_subprocess_supervisor_get_type ())
#define GREETER_SUBPROCESS_SUPERVISOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GREETER_TYPE_SUBPROCESS_SUPERVISOR, GreeterSubprocessSupervisor))
#define GREETER_SUBPROCESS_SUPERVISOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GREETER_TYPE_SUBPROCESS_SUPERVISOR, GreeterSubprocessSupervisorClass))
#define GREETER_IS_SUBPROCESS_SUPERVISOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GREETER_TYPE_SUBPROCESS_SUPERVISOR))
#define GREETER_IS_SUBPROCESS_SUPERVISOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GREETER_TYPE_SUBPROCESS_SUPERVISOR))
#define GREETER_SUBPROCESS_SUPERVISOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GREETER_TYPE_SUBPROCESS_SUPERVISOR, GreeterSubprocessSupervisorClass))

typedef struct _GreeterSubprocessSupervisor GreeterSubprocessSupervisor;
typedef struct _GreeterSubprocessSupervisorClass GreeterSubprocessSupervisorClass;
typedef struct _GreeterSubprocessSupervisorPrivate GreeterSubprocessSupervisorPrivate;
enum  {
	GREETER_SUBPROCESS_SUPERVISOR_0_PROPERTY,
	GREETER_SUBPROCESS_SUPERVISOR_NUM_PROPERTIES
};
static GParamSpec* greeter_subprocess_supervisor_properties[GREETER_SUBPROCESS_SUPERVISOR_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _GreeterSubprocessSupervisorEnsureRunData GreeterSubprocessSupervisorEnsureRunData;
enum  {
	GREETER_SUBPROCESS_SUPERVISOR_SPAWNED_SIGNAL,
	GREETER_SUBPROCESS_SUPERVISOR_NUM_SIGNALS
};
static guint greeter_subprocess_supervisor_signals[GREETER_SUBPROCESS_SUPERVISOR_NUM_SIGNALS] = {0};

struct _GreeterSubprocessSupervisor {
	GObject parent_instance;
	GreeterSubprocessSupervisorPrivate * priv;
};

struct _GreeterSubprocessSupervisorClass {
	GObjectClass parent_class;
};

struct _GreeterSubprocessSupervisorPrivate {
	GSubprocess* subprocess;
	gchar** exec;
	gint exec_length1;
	gint _exec_size_;
	GSubprocessFlags flags;
};

struct _GreeterSubprocessSupervisorEnsureRunData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GreeterSubprocessSupervisor* self;
	GSubprocess* _tmp0_;
	gchar** _tmp1_;
	gint _tmp1__length1;
	GSubprocess* _tmp2_;
	GSubprocess* _tmp3_;
	gboolean _tmp4_;
	GSubprocess* _tmp5_;
	GError* e;
	GError* _tmp6_;
	const gchar* _tmp7_;
	GError* _inner_error0_;
};

static gint GreeterSubprocessSupervisor_private_offset;
static gpointer greeter_subprocess_supervisor_parent_class = NULL;

VALA_EXTERN GType greeter_subprocess_supervisor_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GreeterSubprocessSupervisor, g_object_unref)
VALA_EXTERN GreeterSubprocessSupervisor* greeter_subprocess_supervisor_new (gchar** exec,
                                                                gint exec_length1,
                                                                GError** error);
VALA_EXTERN GreeterSubprocessSupervisor* greeter_subprocess_supervisor_construct (GType object_type,
                                                                      gchar** exec,
                                                                      gint exec_length1,
                                                                      GError** error);
static gchar** _vala_array_dup1 (gchar** self,
                          gssize length);
static void greeter_subprocess_supervisor_ensure_run (GreeterSubprocessSupervisor* self,
                                               GAsyncReadyCallback _callback_,
                                               gpointer _user_data_);
static void greeter_subprocess_supervisor_ensure_run_finish (GreeterSubprocessSupervisor* self,
                                                      GAsyncResult* _res_);
static void greeter_subprocess_supervisor_ensure_run_data_free (gpointer _data);
static gboolean greeter_subprocess_supervisor_ensure_run_co (GreeterSubprocessSupervisorEnsureRunData* _data_);
static void greeter_subprocess_supervisor_ensure_run_ready (GObject* source_object,
                                                     GAsyncResult* _res_,
                                                     gpointer _user_data_);
static void greeter_subprocess_supervisor_finalize (GObject * obj);
static GType greeter_subprocess_supervisor_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

static inline gpointer
greeter_subprocess_supervisor_get_instance_private (GreeterSubprocessSupervisor* self)
{
	return G_STRUCT_MEMBER_P (self, GreeterSubprocessSupervisor_private_offset);
}

static gchar**
_vala_array_dup1 (gchar** self,
                  gssize length)
{
	if (length >= 0) {
		gchar** result;
		gssize i;
		result = g_new0 (gchar*, length + 1);
		for (i = 0; i < length; i++) {
			gchar* _tmp0_;
			_tmp0_ = g_strdup (self[i]);
			result[i] = _tmp0_;
		}
		return result;
	}
	return NULL;
}

GreeterSubprocessSupervisor*
greeter_subprocess_supervisor_construct (GType object_type,
                                         gchar** exec,
                                         gint exec_length1,
                                         GError** error)
{
	GreeterSubprocessSupervisor * self = NULL;
	gchar** _tmp0_;
	gint _tmp0__length1;
	self = (GreeterSubprocessSupervisor*) g_object_new (object_type, NULL);
	_tmp0_ = (exec != NULL) ? _vala_array_dup1 (exec, exec_length1) : exec;
	_tmp0__length1 = exec_length1;
	self->priv->exec = (_vala_array_free (self->priv->exec, self->priv->exec_length1, (GDestroyNotify) g_free), NULL);
	self->priv->exec = _tmp0_;
	self->priv->exec_length1 = _tmp0__length1;
	self->priv->_exec_size_ = self->priv->exec_length1;
	self->priv->flags = (G_SUBPROCESS_FLAGS_STDIN_INHERIT | G_SUBPROCESS_FLAGS_STDOUT_SILENCE) | G_SUBPROCESS_FLAGS_STDERR_MERGE;
	greeter_subprocess_supervisor_ensure_run (self, NULL, NULL);
	return self;
}

GreeterSubprocessSupervisor*
greeter_subprocess_supervisor_new (gchar** exec,
                                   gint exec_length1,
                                   GError** error)
{
	return greeter_subprocess_supervisor_construct (GREETER_TYPE_SUBPROCESS_SUPERVISOR, exec, exec_length1, error);
}

static void
greeter_subprocess_supervisor_ensure_run_data_free (gpointer _data)
{
	GreeterSubprocessSupervisorEnsureRunData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (GreeterSubprocessSupervisorEnsureRunData, _data_);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
greeter_subprocess_supervisor_ensure_run (GreeterSubprocessSupervisor* self,
                                          GAsyncReadyCallback _callback_,
                                          gpointer _user_data_)
{
	GreeterSubprocessSupervisorEnsureRunData* _data_;
	GreeterSubprocessSupervisor* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (GreeterSubprocessSupervisorEnsureRunData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, greeter_subprocess_supervisor_ensure_run_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	greeter_subprocess_supervisor_ensure_run_co (_data_);
}

static void
greeter_subprocess_supervisor_ensure_run_finish (GreeterSubprocessSupervisor* self,
                                                 GAsyncResult* _res_)
{
	GreeterSubprocessSupervisorEnsureRunData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static void
greeter_subprocess_supervisor_ensure_run_ready (GObject* source_object,
                                                GAsyncResult* _res_,
                                                gpointer _user_data_)
{
	GreeterSubprocessSupervisorEnsureRunData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	greeter_subprocess_supervisor_ensure_run_co (_data_);
}

static gboolean
greeter_subprocess_supervisor_ensure_run_co (GreeterSubprocessSupervisorEnsureRunData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_tmp1_ = _data_->self->priv->exec;
		_data_->_tmp1__length1 = _data_->self->priv->exec_length1;
		_data_->_tmp2_ = g_subprocess_newv (_data_->_tmp1_, _data_->self->priv->flags, &_data_->_inner_error0_);
		_data_->_tmp0_ = _data_->_tmp2_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp3_ = _data_->_tmp0_;
		_data_->_tmp0_ = NULL;
		_g_object_unref0 (_data_->self->priv->subprocess);
		_data_->self->priv->subprocess = _data_->_tmp3_;
		_data_->_tmp5_ = _data_->self->priv->subprocess;
		_data_->_state_ = 1;
		g_subprocess_wait_check_async (_data_->_tmp5_, NULL, greeter_subprocess_supervisor_ensure_run_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp4_ = g_subprocess_wait_check_finish (_data_->_tmp5_, _data_->_res_, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_object_unref0 (_data_->_tmp0_);
			goto __catch0_g_error;
		}
		if (!_data_->_tmp4_) {
			greeter_subprocess_supervisor_ensure_run (_data_->self, NULL, NULL);
		}
		_g_object_unref0 (_data_->_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp6_ = _data_->e;
		_data_->_tmp7_ = _data_->_tmp6_->message;
		g_critical ("SubprocessSupervisor.vala:46: %s", _data_->_tmp7_);
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
		g_clear_error (&_data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
greeter_subprocess_supervisor_class_init (GreeterSubprocessSupervisorClass * klass,
                                          gpointer klass_data)
{
	greeter_subprocess_supervisor_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GreeterSubprocessSupervisor_private_offset);
	G_OBJECT_CLASS (klass)->finalize = greeter_subprocess_supervisor_finalize;
	greeter_subprocess_supervisor_signals[GREETER_SUBPROCESS_SUPERVISOR_SPAWNED_SIGNAL] = g_signal_new ("spawned", GREETER_TYPE_SUBPROCESS_SUPERVISOR, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, g_subprocess_get_type ());
}

static void
greeter_subprocess_supervisor_instance_init (GreeterSubprocessSupervisor * self,
                                             gpointer klass)
{
	self->priv = greeter_subprocess_supervisor_get_instance_private (self);
}

static void
greeter_subprocess_supervisor_finalize (GObject * obj)
{
	GreeterSubprocessSupervisor * self;
	gchar** _tmp0_;
	GSubprocess* _tmp1_;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GREETER_TYPE_SUBPROCESS_SUPERVISOR, GreeterSubprocessSupervisor);
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	self->priv->exec = (_vala_array_free (self->priv->exec, self->priv->exec_length1, (GDestroyNotify) g_free), NULL);
	self->priv->exec = _tmp0_;
	self->priv->exec_length1 = 0;
	self->priv->_exec_size_ = self->priv->exec_length1;
	_tmp1_ = self->priv->subprocess;
	g_subprocess_force_exit (_tmp1_);
	_g_object_unref0 (self->priv->subprocess);
	self->priv->exec = (_vala_array_free (self->priv->exec, self->priv->exec_length1, (GDestroyNotify) g_free), NULL);
	G_OBJECT_CLASS (greeter_subprocess_supervisor_parent_class)->finalize (obj);
}

static GType
greeter_subprocess_supervisor_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GreeterSubprocessSupervisorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) greeter_subprocess_supervisor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GreeterSubprocessSupervisor), 0, (GInstanceInitFunc) greeter_subprocess_supervisor_instance_init, NULL };
	GType greeter_subprocess_supervisor_type_id;
	greeter_subprocess_supervisor_type_id = g_type_register_static (G_TYPE_OBJECT, "GreeterSubprocessSupervisor", &g_define_type_info, 0);
	GreeterSubprocessSupervisor_private_offset = g_type_add_instance_private (greeter_subprocess_supervisor_type_id, sizeof (GreeterSubprocessSupervisorPrivate));
	return greeter_subprocess_supervisor_type_id;
}

GType
greeter_subprocess_supervisor_get_type (void)
{
	static volatile gsize greeter_subprocess_supervisor_type_id__once = 0;
	if (g_once_init_enter (&greeter_subprocess_supervisor_type_id__once)) {
		GType greeter_subprocess_supervisor_type_id;
		greeter_subprocess_supervisor_type_id = greeter_subprocess_supervisor_get_type_once ();
		g_once_init_leave (&greeter_subprocess_supervisor_type_id__once, greeter_subprocess_supervisor_type_id);
	}
	return greeter_subprocess_supervisor_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

