/* gamepad-tester.c generated by valac 0.36.1.39-72bb2, the Vala compiler
 * generated from gamepad-tester.vala, do not modify */

/* This file is part of GNOME Games. License: GPL-3.0+.*/

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gamepad.h>
#include <stdlib.h>
#include <string.h>
#include <gamepad-input.h>
#include <event.h>
#include <linux/input-event-codes.h>


#define GAMES_TYPE_GAMEPAD_TESTER (games_gamepad_tester_get_type ())
#define GAMES_GAMEPAD_TESTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GAMEPAD_TESTER, GamesGamepadTester))
#define GAMES_GAMEPAD_TESTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GAMEPAD_TESTER, GamesGamepadTesterClass))
#define GAMES_IS_GAMEPAD_TESTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GAMEPAD_TESTER))
#define GAMES_IS_GAMEPAD_TESTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GAMEPAD_TESTER))
#define GAMES_GAMEPAD_TESTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GAMEPAD_TESTER, GamesGamepadTesterClass))

typedef struct _GamesGamepadTester GamesGamepadTester;
typedef struct _GamesGamepadTesterClass GamesGamepadTesterClass;
typedef struct _GamesGamepadTesterPrivate GamesGamepadTesterPrivate;

#define GAMES_TYPE_GAMEPAD_VIEW (games_gamepad_view_get_type ())
#define GAMES_GAMEPAD_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GAMEPAD_VIEW, GamesGamepadView))
#define GAMES_GAMEPAD_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GAMEPAD_VIEW, GamesGamepadViewClass))
#define GAMES_IS_GAMEPAD_VIEW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GAMEPAD_VIEW))
#define GAMES_IS_GAMEPAD_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GAMEPAD_VIEW))
#define GAMES_GAMEPAD_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GAMEPAD_VIEW, GamesGamepadViewClass))

typedef struct _GamesGamepadView GamesGamepadView;
typedef struct _GamesGamepadViewClass GamesGamepadViewClass;
enum  {
	GAMES_GAMEPAD_TESTER_DUMMY_PROPERTY,
	GAMES_GAMEPAD_TESTER_LAST_PROPERTY
};
static GParamSpec* games_gamepad_tester_properties[GAMES_GAMEPAD_TESTER_LAST_PROPERTY];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define GAMES_TYPE_GAMEPAD_VIEW_CONFIGURATION (games_gamepad_view_configuration_get_type ())

#define GAMES_TYPE_GAMEPAD_INPUT_PATH (games_gamepad_input_path_get_type ())
typedef struct _GamesGamepadInputPath GamesGamepadInputPath;
typedef struct _GamesGamepadViewConfiguration GamesGamepadViewConfiguration;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _GamesGamepadTester {
	GtkBox parent_instance;
	GamesGamepadTesterPrivate * priv;
};

struct _GamesGamepadTesterClass {
	GtkBoxClass parent_class;
};

struct _GamesGamepadTesterPrivate {
	GamesGamepadView* gamepad_view;
	GamesGamepad* gamepad;
	gulong gamepad_button_press_event_handler_id;
	gulong gamepad_button_release_event_handler_id;
	gulong gamepad_axis_event_handler_id;
	gulong gamepad_hat_event_handler_id;
};

struct _GamesGamepadInputPath {
	GamesGamepadInput input;
	gchar* path;
};

struct _GamesGamepadViewConfiguration {
	gchar* svg_path;
	GamesGamepadInputPath* input_paths;
	gint input_paths_length1;
	gint _input_paths_size_;
};


static gpointer games_gamepad_tester_parent_class = NULL;

GType games_gamepad_tester_get_type (void) G_GNUC_CONST;
GType games_gamepad_view_get_type (void) G_GNUC_CONST;
#define GAMES_GAMEPAD_TESTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GAMES_TYPE_GAMEPAD_TESTER, GamesGamepadTesterPrivate))
GType games_gamepad_view_configuration_get_type (void) G_GNUC_CONST;
GType games_gamepad_input_path_get_type (void) G_GNUC_CONST;
GamesGamepadInputPath* games_gamepad_input_path_dup (const GamesGamepadInputPath* self);
void games_gamepad_input_path_free (GamesGamepadInputPath* self);
void games_gamepad_input_path_copy (const GamesGamepadInputPath* self, GamesGamepadInputPath* dest);
void games_gamepad_input_path_destroy (GamesGamepadInputPath* self);
GamesGamepadViewConfiguration* games_gamepad_view_configuration_dup (const GamesGamepadViewConfiguration* self);
void games_gamepad_view_configuration_free (GamesGamepadViewConfiguration* self);
void games_gamepad_view_configuration_copy (const GamesGamepadViewConfiguration* self, GamesGamepadViewConfiguration* dest);
void games_gamepad_view_configuration_destroy (GamesGamepadViewConfiguration* self);
GamesGamepadTester* games_gamepad_tester_new (GamesGamepad* gamepad, GamesGamepadViewConfiguration* configuration);
GamesGamepadTester* games_gamepad_tester_construct (GType object_type, GamesGamepad* gamepad, GamesGamepadViewConfiguration* configuration);
void games_gamepad_view_set_configuration (GamesGamepadView* self, GamesGamepadViewConfiguration* configuration, GError** error);
void games_gamepad_tester_start (GamesGamepadTester* self);
void games_gamepad_view_reset (GamesGamepadView* self);
static void games_gamepad_tester_connect_to_gamepad (GamesGamepadTester* self);
void games_gamepad_tester_stop (GamesGamepadTester* self);
static void games_gamepad_tester_disconnect_from_gamepad (GamesGamepadTester* self);
static void __lambda28_ (GamesGamepadTester* self, GamesEvent* event);
static void games_gamepad_tester_on_button_event (GamesGamepadTester* self, GamesEventGamepadButton* event, gboolean pressed);
static void ___lambda28__games_gamepad_button_press_event (GamesGamepad* _sender, GamesEvent* event, gpointer self);
static void __lambda29_ (GamesGamepadTester* self, GamesEvent* event);
static void ___lambda29__games_gamepad_button_release_event (GamesGamepad* _sender, GamesEvent* event, gpointer self);
static void __lambda30_ (GamesGamepadTester* self, GamesEvent* event);
static void games_gamepad_tester_on_axis_event (GamesGamepadTester* self, GamesEventGamepadAxis* event);
static void ___lambda30__games_gamepad_axis_event (GamesGamepad* _sender, GamesEvent* event, gpointer self);
static void __lambda31_ (GamesGamepadTester* self, GamesEvent* event);
static void games_gamepad_tester_on_hat_event (GamesGamepadTester* self, GamesEventGamepadHat* event);
static void ___lambda31__games_gamepad_hat_event (GamesGamepad* _sender, GamesEvent* event, gpointer self);
gboolean games_gamepad_view_highlight (GamesGamepadView* self, GamesGamepadInput* input, gboolean highlight);
static void games_gamepad_tester_finalize (GObject * obj);


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


GamesGamepadTester* games_gamepad_tester_construct (GType object_type, GamesGamepad* gamepad, GamesGamepadViewConfiguration* configuration) {
	GamesGamepadTester * self = NULL;
	GamesGamepad* _tmp0_;
	GamesGamepad* _tmp1_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (gamepad != NULL, NULL);
	g_return_val_if_fail (configuration != NULL, NULL);
	self = (GamesGamepadTester*) g_object_new (object_type, NULL);
	_tmp0_ = gamepad;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->gamepad);
	self->priv->gamepad = _tmp1_;
	{
		GamesGamepadView* _tmp2_;
		GamesGamepadViewConfiguration _tmp3_;
		_tmp2_ = self->priv->gamepad_view;
		_tmp3_ = *configuration;
		games_gamepad_view_set_configuration (_tmp2_, &_tmp3_, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch36_g_error;
		}
	}
	goto __finally36;
	__catch36_g_error:
	{
		GError* e = NULL;
		GError* _tmp4_;
		const gchar* _tmp5_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp4_ = e;
		_tmp5_ = _tmp4_->message;
		g_critical ("gamepad-tester.vala:21: Could not set up gamepad view: %s", _tmp5_);
		_g_error_free0 (e);
	}
	__finally36:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return NULL;
	}
	return self;
}


GamesGamepadTester* games_gamepad_tester_new (GamesGamepad* gamepad, GamesGamepadViewConfiguration* configuration) {
	return games_gamepad_tester_construct (GAMES_TYPE_GAMEPAD_TESTER, gamepad, configuration);
}


void games_gamepad_tester_start (GamesGamepadTester* self) {
	GamesGamepadView* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->gamepad_view;
	games_gamepad_view_reset (_tmp0_);
	games_gamepad_tester_connect_to_gamepad (self);
}


void games_gamepad_tester_stop (GamesGamepadTester* self) {
	g_return_if_fail (self != NULL);
	games_gamepad_tester_disconnect_from_gamepad (self);
}


static void __lambda28_ (GamesGamepadTester* self, GamesEvent* event) {
	GamesEvent* _tmp0_;
	GamesEventGamepadButton* _tmp1_;
	GamesEventGamepadButton* _tmp2_;
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp1_ = (GamesEventGamepadButton *) (_tmp0_);
	_tmp2_ = _tmp1_;
	games_gamepad_tester_on_button_event (self, _tmp2_, TRUE);
}


static void ___lambda28__games_gamepad_button_press_event (GamesGamepad* _sender, GamesEvent* event, gpointer self) {
	__lambda28_ ((GamesGamepadTester*) self, event);
}


static void __lambda29_ (GamesGamepadTester* self, GamesEvent* event) {
	GamesEvent* _tmp0_;
	GamesEventGamepadButton* _tmp1_;
	GamesEventGamepadButton* _tmp2_;
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp1_ = (GamesEventGamepadButton *) (_tmp0_);
	_tmp2_ = _tmp1_;
	games_gamepad_tester_on_button_event (self, _tmp2_, FALSE);
}


static void ___lambda29__games_gamepad_button_release_event (GamesGamepad* _sender, GamesEvent* event, gpointer self) {
	__lambda29_ ((GamesGamepadTester*) self, event);
}


static void __lambda30_ (GamesGamepadTester* self, GamesEvent* event) {
	GamesEvent* _tmp0_;
	GamesEventGamepadAxis* _tmp1_;
	GamesEventGamepadAxis* _tmp2_;
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp1_ = (GamesEventGamepadAxis *) (_tmp0_);
	_tmp2_ = _tmp1_;
	games_gamepad_tester_on_axis_event (self, _tmp2_);
}


static void ___lambda30__games_gamepad_axis_event (GamesGamepad* _sender, GamesEvent* event, gpointer self) {
	__lambda30_ ((GamesGamepadTester*) self, event);
}


static void __lambda31_ (GamesGamepadTester* self, GamesEvent* event) {
	GamesEvent* _tmp0_;
	GamesEventGamepadHat* _tmp1_;
	GamesEventGamepadHat* _tmp2_;
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp1_ = (GamesEventGamepadHat *) (_tmp0_);
	_tmp2_ = _tmp1_;
	games_gamepad_tester_on_hat_event (self, _tmp2_);
}


static void ___lambda31__games_gamepad_hat_event (GamesGamepad* _sender, GamesEvent* event, gpointer self) {
	__lambda31_ ((GamesGamepadTester*) self, event);
}


static void games_gamepad_tester_connect_to_gamepad (GamesGamepadTester* self) {
	GamesGamepad* _tmp0_;
	gulong _tmp1_;
	GamesGamepad* _tmp2_;
	gulong _tmp3_;
	GamesGamepad* _tmp4_;
	gulong _tmp5_;
	GamesGamepad* _tmp6_;
	gulong _tmp7_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->gamepad;
	_tmp1_ = g_signal_connect_object (_tmp0_, "button-press-event", (GCallback) ___lambda28__games_gamepad_button_press_event, self, 0);
	self->priv->gamepad_button_press_event_handler_id = _tmp1_;
	_tmp2_ = self->priv->gamepad;
	_tmp3_ = g_signal_connect_object (_tmp2_, "button-release-event", (GCallback) ___lambda29__games_gamepad_button_release_event, self, 0);
	self->priv->gamepad_button_release_event_handler_id = _tmp3_;
	_tmp4_ = self->priv->gamepad;
	_tmp5_ = g_signal_connect_object (_tmp4_, "axis-event", (GCallback) ___lambda30__games_gamepad_axis_event, self, 0);
	self->priv->gamepad_axis_event_handler_id = _tmp5_;
	_tmp6_ = self->priv->gamepad;
	_tmp7_ = g_signal_connect_object (_tmp6_, "hat-event", (GCallback) ___lambda31__games_gamepad_hat_event, self, 0);
	self->priv->gamepad_hat_event_handler_id = _tmp7_;
}


static void games_gamepad_tester_disconnect_from_gamepad (GamesGamepadTester* self) {
	gulong _tmp0_;
	gulong _tmp3_;
	gulong _tmp6_;
	gulong _tmp9_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->gamepad_button_press_event_handler_id;
	if (_tmp0_ != ((gulong) 0)) {
		GamesGamepad* _tmp1_;
		gulong _tmp2_;
		_tmp1_ = self->priv->gamepad;
		_tmp2_ = self->priv->gamepad_button_press_event_handler_id;
		g_signal_handler_disconnect ((GObject*) _tmp1_, _tmp2_);
		self->priv->gamepad_button_press_event_handler_id = (gulong) 0;
	}
	_tmp3_ = self->priv->gamepad_button_release_event_handler_id;
	if (_tmp3_ != ((gulong) 0)) {
		GamesGamepad* _tmp4_;
		gulong _tmp5_;
		_tmp4_ = self->priv->gamepad;
		_tmp5_ = self->priv->gamepad_button_release_event_handler_id;
		g_signal_handler_disconnect ((GObject*) _tmp4_, _tmp5_);
		self->priv->gamepad_button_release_event_handler_id = (gulong) 0;
	}
	_tmp6_ = self->priv->gamepad_axis_event_handler_id;
	if (_tmp6_ != ((gulong) 0)) {
		GamesGamepad* _tmp7_;
		gulong _tmp8_;
		_tmp7_ = self->priv->gamepad;
		_tmp8_ = self->priv->gamepad_axis_event_handler_id;
		g_signal_handler_disconnect ((GObject*) _tmp7_, _tmp8_);
		self->priv->gamepad_axis_event_handler_id = (gulong) 0;
	}
	_tmp9_ = self->priv->gamepad_hat_event_handler_id;
	if (_tmp9_ != ((gulong) 0)) {
		GamesGamepad* _tmp10_;
		gulong _tmp11_;
		_tmp10_ = self->priv->gamepad;
		_tmp11_ = self->priv->gamepad_hat_event_handler_id;
		g_signal_handler_disconnect ((GObject*) _tmp10_, _tmp11_);
		self->priv->gamepad_axis_event_handler_id = (gulong) 0;
	}
}


static void games_gamepad_tester_on_button_event (GamesGamepadTester* self, GamesEventGamepadButton* event, gboolean pressed) {
	gboolean highlight = FALSE;
	gboolean _tmp0_;
	GamesGamepadView* _tmp1_;
	GamesEventGamepadButton* _tmp2_;
	guint16 _tmp3_;
	GamesGamepadInput _tmp4_ = {0};
	g_return_if_fail (self != NULL);
	g_return_if_fail (event != NULL);
	_tmp0_ = pressed;
	highlight = _tmp0_;
	_tmp1_ = self->priv->gamepad_view;
	_tmp2_ = event;
	_tmp3_ = _tmp2_->button;
	_tmp4_.type = EV_KEY;
	_tmp4_.code = _tmp3_;
	games_gamepad_view_highlight (_tmp1_, &_tmp4_, highlight);
}


static void games_gamepad_tester_on_axis_event (GamesGamepadTester* self, GamesEventGamepadAxis* event) {
	gboolean highlight = FALSE;
	GamesEventGamepadAxis* _tmp0_;
	GamesEventGamepadAxis* _tmp1_;
	GamesEventGamepadAxis* _tmp2_;
	gdouble _tmp3_;
	gdouble _tmp4_;
	GamesGamepadView* _tmp5_;
	GamesEventGamepadAxis* _tmp6_;
	guint16 _tmp7_;
	GamesGamepadInput _tmp8_ = {0};
	g_return_if_fail (self != NULL);
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp1_ = (GamesEventGamepadAxis *) ((GamesEvent*) _tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _tmp2_->value;
	_tmp4_ = _tmp3_;
	highlight = !(((-0.8) < _tmp4_) && (_tmp4_ < 0.8));
	_tmp5_ = self->priv->gamepad_view;
	_tmp6_ = event;
	_tmp7_ = _tmp6_->axis;
	_tmp8_.type = EV_ABS;
	_tmp8_.code = _tmp7_;
	games_gamepad_view_highlight (_tmp5_, &_tmp8_, highlight);
}


static void games_gamepad_tester_on_hat_event (GamesGamepadTester* self, GamesEventGamepadHat* event) {
	gboolean highlight = FALSE;
	GamesEventGamepadHat* _tmp0_;
	GamesEventGamepadHat* _tmp1_;
	GamesEventGamepadHat* _tmp2_;
	gint8 _tmp3_;
	GamesGamepadView* _tmp4_;
	GamesEventGamepadHat* _tmp5_;
	guint16 _tmp6_;
	GamesGamepadInput _tmp7_ = {0};
	g_return_if_fail (self != NULL);
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp1_ = (GamesEventGamepadHat *) ((GamesEvent*) _tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _tmp2_->value;
	highlight = !(((gint) _tmp3_) == 0);
	_tmp4_ = self->priv->gamepad_view;
	_tmp5_ = event;
	_tmp6_ = _tmp5_->axis;
	_tmp7_.type = EV_ABS;
	_tmp7_.code = _tmp6_;
	games_gamepad_view_highlight (_tmp4_, &_tmp7_, highlight);
}


static void games_gamepad_tester_class_init (GamesGamepadTesterClass * klass) {
	gint GamesGamepadTester_private_offset;
	games_gamepad_tester_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GamesGamepadTesterPrivate));
	G_OBJECT_CLASS (klass)->finalize = games_gamepad_tester_finalize;
	GamesGamepadTester_private_offset = g_type_class_get_instance_private_offset (klass);
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/org/gnome/Games/ui/gamepad-tester.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "gamepad_view", FALSE, GamesGamepadTester_private_offset + G_STRUCT_OFFSET (GamesGamepadTesterPrivate, gamepad_view));
}


static void games_gamepad_tester_instance_init (GamesGamepadTester * self) {
	self->priv = GAMES_GAMEPAD_TESTER_GET_PRIVATE (self);
	g_type_ensure (GAMES_TYPE_GAMEPAD_VIEW);
	gtk_widget_init_template (GTK_WIDGET (self));
}


static void games_gamepad_tester_finalize (GObject * obj) {
	GamesGamepadTester * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_GAMEPAD_TESTER, GamesGamepadTester);
	_g_object_unref0 (self->priv->gamepad_view);
	_g_object_unref0 (self->priv->gamepad);
	G_OBJECT_CLASS (games_gamepad_tester_parent_class)->finalize (obj);
}


GType games_gamepad_tester_get_type (void) {
	static volatile gsize games_gamepad_tester_type_id__volatile = 0;
	if (g_once_init_enter (&games_gamepad_tester_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (GamesGamepadTesterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_gamepad_tester_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesGamepadTester), 0, (GInstanceInitFunc) games_gamepad_tester_instance_init, NULL };
		GType games_gamepad_tester_type_id;
		games_gamepad_tester_type_id = g_type_register_static (gtk_box_get_type (), "GamesGamepadTester", &g_define_type_info, 0);
		g_once_init_leave (&games_gamepad_tester_type_id__volatile, games_gamepad_tester_type_id);
	}
	return games_gamepad_tester_type_id__volatile;
}



