/* gamepad-tester.c generated by valac 0.36.12.13-3ae08, 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 <libmanette.h>
#include <stdlib.h>
#include <string.h>
#include <linux/input-event-codes.h>
#include <float.h>
#include <math.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;
#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 ())

#define GAMES_TYPE_GAMEPAD_INPUT (games_gamepad_input_get_type ())
typedef struct _GamesGamepadInput GamesGamepadInput;
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;
	ManetteDevice* device;
	gulong gamepad_button_press_event_handler_id;
	gulong gamepad_button_release_event_handler_id;
	gulong gamepad_axis_event_handler_id;
};

struct _GamesGamepadInput {
	guint16 type;
	guint16 code;
};

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))
enum  {
	GAMES_GAMEPAD_TESTER_0_PROPERTY
};
GType games_gamepad_view_configuration_get_type (void) G_GNUC_CONST;
GType games_gamepad_input_path_get_type (void) G_GNUC_CONST;
GType games_gamepad_input_get_type (void) G_GNUC_CONST;
GamesGamepadInput* games_gamepad_input_dup (const GamesGamepadInput* self);
void games_gamepad_input_free (GamesGamepadInput* self);
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 (ManetteDevice* device, GamesGamepadViewConfiguration* configuration);
GamesGamepadTester* games_gamepad_tester_construct (GType object_type, ManetteDevice* device, 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 games_gamepad_tester_on_button_press_event (GamesGamepadTester* self, ManetteEvent* event);
static void _games_gamepad_tester_on_button_press_event_manette_device_button_press_event (ManetteDevice* _sender, ManetteEvent* event, gpointer self);
static void games_gamepad_tester_on_button_release_event (GamesGamepadTester* self, ManetteEvent* event);
static void _games_gamepad_tester_on_button_release_event_manette_device_button_release_event (ManetteDevice* _sender, ManetteEvent* event, gpointer self);
static void games_gamepad_tester_on_absolute_axis_event (GamesGamepadTester* self, ManetteEvent* event);
static void _games_gamepad_tester_on_absolute_axis_event_manette_device_absolute_axis_event (ManetteDevice* _sender, ManetteEvent* 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, ManetteDevice* device, GamesGamepadViewConfiguration* configuration) {
	GamesGamepadTester * self = NULL;
	ManetteDevice* _tmp0_;
	ManetteDevice* _tmp1_;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (device != NULL, NULL);
	g_return_val_if_fail (configuration != NULL, NULL);
	self = (GamesGamepadTester*) g_object_new (object_type, NULL);
	_tmp0_ = device;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->device);
	self->priv->device = _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 __catch35_g_error;
		}
	}
	goto __finally35;
	__catch35_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:20: Could not set up gamepad view: %s", _tmp5_);
		_g_error_free0 (e);
	}
	__finally35:
	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 (ManetteDevice* device, GamesGamepadViewConfiguration* configuration) {
	return games_gamepad_tester_construct (GAMES_TYPE_GAMEPAD_TESTER, device, 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 _games_gamepad_tester_on_button_press_event_manette_device_button_press_event (ManetteDevice* _sender, ManetteEvent* event, gpointer self) {
	games_gamepad_tester_on_button_press_event ((GamesGamepadTester*) self, event);
}


static void _games_gamepad_tester_on_button_release_event_manette_device_button_release_event (ManetteDevice* _sender, ManetteEvent* event, gpointer self) {
	games_gamepad_tester_on_button_release_event ((GamesGamepadTester*) self, event);
}


static void _games_gamepad_tester_on_absolute_axis_event_manette_device_absolute_axis_event (ManetteDevice* _sender, ManetteEvent* event, gpointer self) {
	games_gamepad_tester_on_absolute_axis_event ((GamesGamepadTester*) self, event);
}


static void games_gamepad_tester_connect_to_gamepad (GamesGamepadTester* self) {
	ManetteDevice* _tmp0_;
	gulong _tmp1_;
	ManetteDevice* _tmp2_;
	gulong _tmp3_;
	ManetteDevice* _tmp4_;
	gulong _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->device;
	_tmp1_ = g_signal_connect_object (_tmp0_, "button-press-event", (GCallback) _games_gamepad_tester_on_button_press_event_manette_device_button_press_event, self, 0);
	self->priv->gamepad_button_press_event_handler_id = _tmp1_;
	_tmp2_ = self->priv->device;
	_tmp3_ = g_signal_connect_object (_tmp2_, "button-release-event", (GCallback) _games_gamepad_tester_on_button_release_event_manette_device_button_release_event, self, 0);
	self->priv->gamepad_button_release_event_handler_id = _tmp3_;
	_tmp4_ = self->priv->device;
	_tmp5_ = g_signal_connect_object (_tmp4_, "absolute-axis-event", (GCallback) _games_gamepad_tester_on_absolute_axis_event_manette_device_absolute_axis_event, self, 0);
	self->priv->gamepad_axis_event_handler_id = _tmp5_;
}


static void games_gamepad_tester_disconnect_from_gamepad (GamesGamepadTester* self) {
	gulong _tmp0_;
	gulong _tmp3_;
	gulong _tmp6_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->gamepad_button_press_event_handler_id;
	if (_tmp0_ != ((gulong) 0)) {
		ManetteDevice* _tmp1_;
		gulong _tmp2_;
		_tmp1_ = self->priv->device;
		_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)) {
		ManetteDevice* _tmp4_;
		gulong _tmp5_;
		_tmp4_ = self->priv->device;
		_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)) {
		ManetteDevice* _tmp7_;
		gulong _tmp8_;
		_tmp7_ = self->priv->device;
		_tmp8_ = self->priv->gamepad_axis_event_handler_id;
		g_signal_handler_disconnect ((GObject*) _tmp7_, _tmp8_);
		self->priv->gamepad_axis_event_handler_id = (gulong) 0;
	}
}


static void games_gamepad_tester_on_button_press_event (GamesGamepadTester* self, ManetteEvent* event) {
	guint16 button = 0U;
	ManetteEvent* _tmp0_;
	guint16 _tmp1_ = 0U;
	gboolean _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp2_ = manette_event_get_button (_tmp0_, &_tmp1_);
	button = _tmp1_;
	if (_tmp2_) {
		GamesGamepadView* _tmp3_;
		guint16 _tmp4_;
		GamesGamepadInput _tmp5_ = {0};
		_tmp3_ = self->priv->gamepad_view;
		_tmp4_ = button;
		_tmp5_.type = EV_KEY;
		_tmp5_.code = _tmp4_;
		games_gamepad_view_highlight (_tmp3_, &_tmp5_, TRUE);
	}
}


static void games_gamepad_tester_on_button_release_event (GamesGamepadTester* self, ManetteEvent* event) {
	guint16 button = 0U;
	ManetteEvent* _tmp0_;
	guint16 _tmp1_ = 0U;
	gboolean _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (event != NULL);
	_tmp0_ = event;
	_tmp2_ = manette_event_get_button (_tmp0_, &_tmp1_);
	button = _tmp1_;
	if (_tmp2_) {
		GamesGamepadView* _tmp3_;
		guint16 _tmp4_;
		GamesGamepadInput _tmp5_ = {0};
		_tmp3_ = self->priv->gamepad_view;
		_tmp4_ = button;
		_tmp5_.type = EV_KEY;
		_tmp5_.code = _tmp4_;
		games_gamepad_view_highlight (_tmp3_, &_tmp5_, FALSE);
	}
}


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


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->device);
	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;
}



