/* retro-input-manager.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from retro-input-manager.vala, do not modify */

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

#include <glib-object.h>
#include <retro-gtk.h>
#include <libmanette.h>
#include "gnome-games.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <string.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define GAMES_TYPE_RETRO_INPUT_MANAGER (games_retro_input_manager_get_type ())
#define GAMES_RETRO_INPUT_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_RETRO_INPUT_MANAGER, GamesRetroInputManager))
#define GAMES_RETRO_INPUT_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_RETRO_INPUT_MANAGER, GamesRetroInputManagerClass))
#define GAMES_IS_RETRO_INPUT_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_RETRO_INPUT_MANAGER))
#define GAMES_IS_RETRO_INPUT_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_RETRO_INPUT_MANAGER))
#define GAMES_RETRO_INPUT_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_RETRO_INPUT_MANAGER, GamesRetroInputManagerClass))

typedef struct _GamesRetroInputManager GamesRetroInputManager;
typedef struct _GamesRetroInputManagerClass GamesRetroInputManagerClass;
typedef struct _GamesRetroInputManagerPrivate GamesRetroInputManagerPrivate;

#define GAMES_TYPE_KEYBOARD_MAPPING_MANAGER (games_keyboard_mapping_manager_get_type ())
#define GAMES_KEYBOARD_MAPPING_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_KEYBOARD_MAPPING_MANAGER, GamesKeyboardMappingManager))
#define GAMES_KEYBOARD_MAPPING_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_KEYBOARD_MAPPING_MANAGER, GamesKeyboardMappingManagerClass))
#define GAMES_IS_KEYBOARD_MAPPING_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_KEYBOARD_MAPPING_MANAGER))
#define GAMES_IS_KEYBOARD_MAPPING_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_KEYBOARD_MAPPING_MANAGER))
#define GAMES_KEYBOARD_MAPPING_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_KEYBOARD_MAPPING_MANAGER, GamesKeyboardMappingManagerClass))

typedef struct _GamesKeyboardMappingManager GamesKeyboardMappingManager;
typedef struct _GamesKeyboardMappingManagerClass GamesKeyboardMappingManagerClass;
enum  {
	GAMES_RETRO_INPUT_MANAGER_0_PROPERTY,
	GAMES_RETRO_INPUT_MANAGER_INPUT_MODE_PROPERTY,
	GAMES_RETRO_INPUT_MANAGER_NUM_PROPERTIES
};
static GParamSpec* games_retro_input_manager_properties[GAMES_RETRO_INPUT_MANAGER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define GAMES_TYPE_RETRO_GAMEPAD (games_retro_gamepad_get_type ())
#define GAMES_RETRO_GAMEPAD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_RETRO_GAMEPAD, GamesRetroGamepad))
#define GAMES_RETRO_GAMEPAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_RETRO_GAMEPAD, GamesRetroGamepadClass))
#define GAMES_IS_RETRO_GAMEPAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_RETRO_GAMEPAD))
#define GAMES_IS_RETRO_GAMEPAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_RETRO_GAMEPAD))
#define GAMES_RETRO_GAMEPAD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_RETRO_GAMEPAD, GamesRetroGamepadClass))

typedef struct _GamesRetroGamepad GamesRetroGamepad;
typedef struct _GamesRetroGamepadClass GamesRetroGamepadClass;
#define __vala_ManetteMonitorIter_free0(var) ((var == NULL) ? NULL : (var = (_vala_ManetteMonitorIter_free (var), NULL)))
enum  {
	GAMES_RETRO_INPUT_MANAGER_CONTROLLERS_CHANGED_SIGNAL,
	GAMES_RETRO_INPUT_MANAGER_NUM_SIGNALS
};
static guint games_retro_input_manager_signals[GAMES_RETRO_INPUT_MANAGER_NUM_SIGNALS] = {0};

struct _GamesRetroInputManager {
	GObject parent_instance;
	GamesRetroInputManagerPrivate * priv;
};

struct _GamesRetroInputManagerClass {
	GObjectClass parent_class;
};

struct _GamesRetroInputManagerPrivate {
	RetroCore* core;
	RetroCoreView* view;
	RetroController* core_view_joypad;
	GamesKeyboardMappingManager* keyboard_mapping_manager;
	ManetteMonitor* monitor;
	GamesInputMode _input_mode;
	ManetteDevice** devices;
	gint devices_length1;
	gint _devices_size_;
	RetroController** controllers;
	gint controllers_length1;
	gint _controllers_size_;
	gint core_view_joypad_port;
};

static gint GamesRetroInputManager_private_offset;
static gpointer games_retro_input_manager_parent_class = NULL;

VALA_EXTERN GType games_retro_input_manager_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType games_keyboard_mapping_manager_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GamesRetroInputManager* games_retro_input_manager_new (RetroCore* core,
                                                       RetroCoreView* view);
VALA_EXTERN GamesRetroInputManager* games_retro_input_manager_construct (GType object_type,
                                                             RetroCore* core,
                                                             RetroCoreView* view);
VALA_EXTERN GamesKeyboardMappingManager* games_keyboard_mapping_manager_new (void);
VALA_EXTERN GamesKeyboardMappingManager* games_keyboard_mapping_manager_construct (GType object_type);
VALA_EXTERN RetroKeyJoypadMapping* games_keyboard_mapping_manager_get_mapping (GamesKeyboardMappingManager* self);
static void games_retro_input_manager_on_keyboard_mapping_manager_changed (GamesRetroInputManager* self);
static void _games_retro_input_manager_on_keyboard_mapping_manager_changed_games_keyboard_mapping_manager_changed (GamesKeyboardMappingManager* _sender,
                                                                                                            gpointer self);
VALA_EXTERN GType games_retro_gamepad_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GamesRetroGamepad* games_retro_gamepad_new (ManetteDevice* device);
VALA_EXTERN GamesRetroGamepad* games_retro_gamepad_construct (GType object_type,
                                                  ManetteDevice* device);
static void _vala_array_add26 (ManetteDevice** * array,
                        gint* length,
                        gint* size,
                        ManetteDevice* value);
static void _vala_array_add27 (RetroController** * array,
                        gint* length,
                        gint* size,
                        RetroController* value);
static void games_retro_input_manager_on_device_disconnected (GamesRetroInputManager* self,
                                                       ManetteDevice* device);
static void _games_retro_input_manager_on_device_disconnected_manette_device_disconnected (ManetteDevice* _sender,
                                                                                    gpointer self);
static void _vala_array_add28 (ManetteDevice** * array,
                        gint* length,
                        gint* size,
                        ManetteDevice* value);
static void _vala_array_add29 (RetroController** * array,
                        gint* length,
                        gint* size,
                        RetroController* value);
static void games_retro_input_manager_on_device_connected (GamesRetroInputManager* self,
                                                    ManetteDevice* device);
static void _games_retro_input_manager_on_device_connected_manette_monitor_device_connected (ManetteMonitor* _sender,
                                                                                      ManetteDevice* device,
                                                                                      gpointer self);
VALA_EXTERN void games_retro_input_manager_set_input_mode (GamesRetroInputManager* self,
                                               GamesInputMode value);
static void _vala_ManetteMonitorIter_free (ManetteMonitorIter* self);
static void games_retro_input_manager_update_core_view_gamepad (GamesRetroInputManager* self);
VALA_EXTERN GamesInputMode games_retro_input_manager_get_input_mode (GamesRetroInputManager* self);
static void _vala_array_add30 (ManetteDevice** * array,
                        gint* length,
                        gint* size,
                        ManetteDevice* value);
static void _vala_array_add31 (RetroController** * array,
                        gint* length,
                        gint* size,
                        RetroController* value);
static void games_retro_input_manager_disconnect_port (GamesRetroInputManager* self,
                                                gint port);
static void games_retro_input_manager_finalize (GObject * obj);
static GType games_retro_input_manager_get_type_once (void);
static void _vala_games_retro_input_manager_get_property (GObject * object,
                                                   guint property_id,
                                                   GValue * value,
                                                   GParamSpec * pspec);
static void _vala_games_retro_input_manager_set_property (GObject * object,
                                                   guint property_id,
                                                   const GValue * value,
                                                   GParamSpec * pspec);
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 _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
games_retro_input_manager_get_instance_private (GamesRetroInputManager* self)
{
	return G_STRUCT_MEMBER_P (self, GamesRetroInputManager_private_offset);
}

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

static void
_games_retro_input_manager_on_keyboard_mapping_manager_changed_games_keyboard_mapping_manager_changed (GamesKeyboardMappingManager* _sender,
                                                                                                       gpointer self)
{
	games_retro_input_manager_on_keyboard_mapping_manager_changed ((GamesRetroInputManager*) self);
}

static void
_vala_array_add26 (ManetteDevice** * array,
                   gint* length,
                   gint* size,
                   ManetteDevice* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (ManetteDevice*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add27 (RetroController** * array,
                   gint* length,
                   gint* size,
                   RetroController* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (RetroController*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_games_retro_input_manager_on_device_disconnected_manette_device_disconnected (ManetteDevice* _sender,
                                                                               gpointer self)
{
	games_retro_input_manager_on_device_disconnected ((GamesRetroInputManager*) self, _sender);
}

static void
_vala_array_add28 (ManetteDevice** * array,
                   gint* length,
                   gint* size,
                   ManetteDevice* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (ManetteDevice*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add29 (RetroController** * array,
                   gint* length,
                   gint* size,
                   RetroController* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (RetroController*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_games_retro_input_manager_on_device_connected_manette_monitor_device_connected (ManetteMonitor* _sender,
                                                                                 ManetteDevice* device,
                                                                                 gpointer self)
{
	games_retro_input_manager_on_device_connected ((GamesRetroInputManager*) self, device);
}

static void
_vala_ManetteMonitorIter_free (ManetteMonitorIter* self)
{
	g_boxed_free (manette_monitor_iter_get_type (), self);
}

GamesRetroInputManager*
games_retro_input_manager_construct (GType object_type,
                                     RetroCore* core,
                                     RetroCoreView* view)
{
	GamesRetroInputManager * self = NULL;
	RetroCore* _tmp0_;
	RetroCoreView* _tmp1_;
	GamesKeyboardMappingManager* _tmp2_;
	GamesKeyboardMappingManager* _tmp3_;
	RetroKeyJoypadMapping* _tmp4_;
	RetroKeyJoypadMapping* _tmp5_;
	GamesKeyboardMappingManager* _tmp6_;
	RetroController* _tmp7_;
	ManetteMonitor* _tmp8_;
	ManetteMonitorIter* iterator = NULL;
	ManetteMonitor* _tmp9_;
	ManetteMonitorIter* _tmp10_;
	ManetteDevice* device = NULL;
	RetroController** _tmp24_;
	gint _tmp24__length1;
	ManetteMonitor* _tmp25_;
	g_return_val_if_fail (core != NULL, NULL);
	g_return_val_if_fail (view != NULL, NULL);
	self = (GamesRetroInputManager*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (core);
	_g_object_unref0 (self->priv->core);
	self->priv->core = _tmp0_;
	_tmp1_ = _g_object_ref0 (view);
	_g_object_unref0 (self->priv->view);
	self->priv->view = _tmp1_;
	_tmp2_ = games_keyboard_mapping_manager_new ();
	_g_object_unref0 (self->priv->keyboard_mapping_manager);
	self->priv->keyboard_mapping_manager = _tmp2_;
	_tmp3_ = self->priv->keyboard_mapping_manager;
	_tmp4_ = games_keyboard_mapping_manager_get_mapping (_tmp3_);
	_tmp5_ = _tmp4_;
	retro_core_view_set_key_joypad_mapping (view, _tmp5_);
	_tmp6_ = self->priv->keyboard_mapping_manager;
	g_signal_connect_object (_tmp6_, "changed", (GCallback) _games_retro_input_manager_on_keyboard_mapping_manager_changed_games_keyboard_mapping_manager_changed, self, 0);
	retro_core_view_set_as_default_controller (view, core);
	retro_core_set_default_controller (core, RETRO_CONTROLLER_TYPE_JOYPAD, NULL);
	_tmp7_ = retro_core_view_as_controller (view, RETRO_CONTROLLER_TYPE_JOYPAD);
	_g_object_unref0 (self->priv->core_view_joypad);
	self->priv->core_view_joypad = _tmp7_;
	_tmp8_ = manette_monitor_new ();
	_g_object_unref0 (self->priv->monitor);
	self->priv->monitor = _tmp8_;
	_tmp9_ = self->priv->monitor;
	_tmp10_ = manette_monitor_iterate (_tmp9_);
	iterator = _tmp10_;
	device = NULL;
	while (TRUE) {
		ManetteMonitorIter* _tmp11_;
		ManetteDevice* _tmp12_ = NULL;
		gboolean _tmp13_;
		ManetteDevice* _tmp14_;
		gint port = 0;
		RetroController** _tmp15_;
		gint _tmp15__length1;
		GamesRetroGamepad* retro_gamepad = NULL;
		ManetteDevice* _tmp16_;
		GamesRetroGamepad* _tmp17_;
		ManetteDevice* _tmp18_;
		ManetteDevice* _tmp19_;
		GamesRetroGamepad* _tmp20_;
		RetroController* _tmp21_;
		GamesRetroGamepad* _tmp22_;
		ManetteDevice* _tmp23_;
		_tmp11_ = iterator;
		_tmp13_ = manette_monitor_iter_next (_tmp11_, &_tmp12_);
		_g_object_unref0 (device);
		_tmp14_ = _g_object_ref0 (_tmp12_);
		device = _tmp14_;
		if (!_tmp13_) {
			break;
		}
		_tmp15_ = self->priv->controllers;
		_tmp15__length1 = self->priv->controllers_length1;
		port = _tmp15__length1;
		_tmp16_ = device;
		_tmp17_ = games_retro_gamepad_new (_tmp16_);
		retro_gamepad = _tmp17_;
		_tmp18_ = device;
		_tmp19_ = _g_object_ref0 (_tmp18_);
		_vala_array_add26 (&self->priv->devices, &self->priv->devices_length1, &self->priv->_devices_size_, _tmp19_);
		_tmp20_ = retro_gamepad;
		_tmp21_ = _g_object_ref0 ((RetroController*) _tmp20_);
		_vala_array_add27 (&self->priv->controllers, &self->priv->controllers_length1, &self->priv->_controllers_size_, _tmp21_);
		_tmp22_ = retro_gamepad;
		retro_core_set_controller (core, (guint) port, (RetroController*) _tmp22_);
		_tmp23_ = device;
		g_signal_connect_object (_tmp23_, "disconnected", (GCallback) _games_retro_input_manager_on_device_disconnected_manette_device_disconnected, self, 0);
		_g_object_unref0 (retro_gamepad);
	}
	_tmp24_ = self->priv->controllers;
	_tmp24__length1 = self->priv->controllers_length1;
	self->priv->core_view_joypad_port = _tmp24__length1;
	_vala_array_add28 (&self->priv->devices, &self->priv->devices_length1, &self->priv->_devices_size_, NULL);
	_vala_array_add29 (&self->priv->controllers, &self->priv->controllers_length1, &self->priv->_controllers_size_, NULL);
	_tmp25_ = self->priv->monitor;
	g_signal_connect_object (_tmp25_, "device-connected", (GCallback) _games_retro_input_manager_on_device_connected_manette_monitor_device_connected, self, 0);
	games_retro_input_manager_set_input_mode (self, GAMES_INPUT_MODE_GAMEPAD);
	_g_object_unref0 (device);
	__vala_ManetteMonitorIter_free0 (iterator);
	return self;
}

GamesRetroInputManager*
games_retro_input_manager_new (RetroCore* core,
                               RetroCoreView* view)
{
	return games_retro_input_manager_construct (GAMES_TYPE_RETRO_INPUT_MANAGER, core, view);
}

static void
games_retro_input_manager_update_core_view_gamepad (GamesRetroInputManager* self)
{
	GamesInputMode _tmp0_;
	GamesInputMode _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = games_retro_input_manager_get_input_mode (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == GAMES_INPUT_MODE_GAMEPAD) {
		RetroController** _tmp2_;
		gint _tmp2__length1;
		RetroController* _tmp3_;
		RetroController* _tmp4_;
		RetroCore* _tmp5_;
		RetroController* _tmp6_;
		_tmp2_ = self->priv->controllers;
		_tmp2__length1 = self->priv->controllers_length1;
		_tmp3_ = self->priv->core_view_joypad;
		_tmp4_ = _g_object_ref0 (_tmp3_);
		_g_object_unref0 (_tmp2_[self->priv->core_view_joypad_port]);
		_tmp2_[self->priv->core_view_joypad_port] = _tmp4_;
		_tmp5_ = self->priv->core;
		_tmp6_ = self->priv->core_view_joypad;
		retro_core_set_controller (_tmp5_, (guint) self->priv->core_view_joypad_port, _tmp6_);
	} else {
		RetroController** _tmp7_;
		gint _tmp7__length1;
		RetroCore* _tmp8_;
		_tmp7_ = self->priv->controllers;
		_tmp7__length1 = self->priv->controllers_length1;
		_g_object_unref0 (_tmp7_[self->priv->core_view_joypad_port]);
		_tmp7_[self->priv->core_view_joypad_port] = NULL;
		_tmp8_ = self->priv->core;
		retro_core_set_controller (_tmp8_, (guint) self->priv->core_view_joypad_port, NULL);
	}
}

static void
games_retro_input_manager_on_keyboard_mapping_manager_changed (GamesRetroInputManager* self)
{
	RetroCoreView* _tmp0_;
	GamesKeyboardMappingManager* _tmp1_;
	RetroKeyJoypadMapping* _tmp2_;
	RetroKeyJoypadMapping* _tmp3_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->view;
	_tmp1_ = self->priv->keyboard_mapping_manager;
	_tmp2_ = games_keyboard_mapping_manager_get_mapping (_tmp1_);
	_tmp3_ = _tmp2_;
	retro_core_view_set_key_joypad_mapping (_tmp0_, _tmp3_);
}

static void
_vala_array_add30 (ManetteDevice** * array,
                   gint* length,
                   gint* size,
                   ManetteDevice* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (ManetteDevice*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add31 (RetroController** * array,
                   gint* length,
                   gint* size,
                   RetroController* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (RetroController*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
games_retro_input_manager_on_device_connected (GamesRetroInputManager* self,
                                               ManetteDevice* device)
{
	gint port = 0;
	ManetteDevice** _tmp0_;
	gint _tmp0__length1;
	ManetteDevice* _tmp1_;
	GamesRetroGamepad* retro_gamepad = NULL;
	GamesRetroGamepad* _tmp2_;
	RetroController** _tmp3_;
	gint _tmp3__length1;
	GamesRetroGamepad* _tmp4_;
	RetroController* _tmp5_;
	RetroCore* _tmp6_;
	GamesRetroGamepad* _tmp7_;
	RetroController** _tmp14_;
	gint _tmp14__length1;
	g_return_if_fail (self != NULL);
	g_return_if_fail (device != NULL);
	port = self->priv->core_view_joypad_port;
	_tmp0_ = self->priv->devices;
	_tmp0__length1 = self->priv->devices_length1;
	_tmp1_ = _g_object_ref0 (device);
	_g_object_unref0 (_tmp0_[port]);
	_tmp0_[port] = _tmp1_;
	_tmp2_ = games_retro_gamepad_new (device);
	retro_gamepad = _tmp2_;
	_tmp3_ = self->priv->controllers;
	_tmp3__length1 = self->priv->controllers_length1;
	_tmp4_ = retro_gamepad;
	_tmp5_ = _g_object_ref0 ((RetroController*) _tmp4_);
	_g_object_unref0 (_tmp3_[port]);
	_tmp3_[port] = _tmp5_;
	_tmp6_ = self->priv->core;
	_tmp7_ = retro_gamepad;
	retro_core_set_controller (_tmp6_, (guint) port, (RetroController*) _tmp7_);
	g_signal_connect_object (device, "disconnected", (GCallback) _games_retro_input_manager_on_device_disconnected_manette_device_disconnected, self, 0);
	{
		gint i = 0;
		i = self->priv->core_view_joypad_port;
		{
			gboolean _tmp8_ = FALSE;
			_tmp8_ = TRUE;
			while (TRUE) {
				RetroController** _tmp10_;
				gint _tmp10__length1;
				RetroController** _tmp11_;
				gint _tmp11__length1;
				RetroController* _tmp12_;
				if (!_tmp8_) {
					gint _tmp9_;
					_tmp9_ = i;
					i = _tmp9_ + 1;
				}
				_tmp8_ = FALSE;
				_tmp10_ = self->priv->controllers;
				_tmp10__length1 = self->priv->controllers_length1;
				if (!(i < _tmp10__length1)) {
					break;
				}
				_tmp11_ = self->priv->controllers;
				_tmp11__length1 = self->priv->controllers_length1;
				_tmp12_ = _tmp11_[i];
				if (_tmp12_ == NULL) {
					ManetteDevice** _tmp13_;
					gint _tmp13__length1;
					self->priv->core_view_joypad_port = i;
					_tmp13_ = self->priv->devices;
					_tmp13__length1 = self->priv->devices_length1;
					_g_object_unref0 (_tmp13_[self->priv->core_view_joypad_port]);
					_tmp13_[self->priv->core_view_joypad_port] = NULL;
					games_retro_input_manager_update_core_view_gamepad (self);
					g_signal_emit (self, games_retro_input_manager_signals[GAMES_RETRO_INPUT_MANAGER_CONTROLLERS_CHANGED_SIGNAL], 0);
					_g_object_unref0 (retro_gamepad);
					return;
				}
			}
		}
	}
	_tmp14_ = self->priv->controllers;
	_tmp14__length1 = self->priv->controllers_length1;
	self->priv->core_view_joypad_port = _tmp14__length1;
	_vala_array_add30 (&self->priv->devices, &self->priv->devices_length1, &self->priv->_devices_size_, NULL);
	_vala_array_add31 (&self->priv->controllers, &self->priv->controllers_length1, &self->priv->_controllers_size_, NULL);
	games_retro_input_manager_update_core_view_gamepad (self);
	g_signal_emit (self, games_retro_input_manager_signals[GAMES_RETRO_INPUT_MANAGER_CONTROLLERS_CHANGED_SIGNAL], 0);
	_g_object_unref0 (retro_gamepad);
}

static void
games_retro_input_manager_on_device_disconnected (GamesRetroInputManager* self,
                                                  ManetteDevice* device)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (device != NULL);
	{
		gint port = 0;
		port = 0;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				ManetteDevice** _tmp2_;
				gint _tmp2__length1;
				ManetteDevice** _tmp3_;
				gint _tmp3__length1;
				ManetteDevice* _tmp4_;
				if (!_tmp0_) {
					gint _tmp1_;
					_tmp1_ = port;
					port = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				_tmp2_ = self->priv->devices;
				_tmp2__length1 = self->priv->devices_length1;
				if (!(port < _tmp2__length1)) {
					break;
				}
				_tmp3_ = self->priv->devices;
				_tmp3__length1 = self->priv->devices_length1;
				_tmp4_ = _tmp3_[port];
				if (_tmp4_ == device) {
					games_retro_input_manager_disconnect_port (self, port);
				}
			}
		}
	}
}

static void
games_retro_input_manager_disconnect_port (GamesRetroInputManager* self,
                                           gint port)
{
	g_return_if_fail (self != NULL);
	if (self->priv->core_view_joypad_port > port) {
		ManetteDevice** _tmp0_;
		gint _tmp0__length1;
		RetroController** _tmp1_;
		gint _tmp1__length1;
		RetroCore* _tmp2_;
		ManetteDevice** _tmp3_;
		gint _tmp3__length1;
		_tmp0_ = self->priv->devices;
		_tmp0__length1 = self->priv->devices_length1;
		_g_object_unref0 (_tmp0_[self->priv->core_view_joypad_port]);
		_tmp0_[self->priv->core_view_joypad_port] = NULL;
		_tmp1_ = self->priv->controllers;
		_tmp1__length1 = self->priv->controllers_length1;
		_g_object_unref0 (_tmp1_[self->priv->core_view_joypad_port]);
		_tmp1_[self->priv->core_view_joypad_port] = NULL;
		_tmp2_ = self->priv->core;
		retro_core_set_controller (_tmp2_, (guint) self->priv->core_view_joypad_port, NULL);
		self->priv->core_view_joypad_port = port;
		_tmp3_ = self->priv->devices;
		_tmp3__length1 = self->priv->devices_length1;
		_g_object_unref0 (_tmp3_[self->priv->core_view_joypad_port]);
		_tmp3_[self->priv->core_view_joypad_port] = NULL;
		games_retro_input_manager_update_core_view_gamepad (self);
	} else {
		ManetteDevice** _tmp4_;
		gint _tmp4__length1;
		RetroController** _tmp5_;
		gint _tmp5__length1;
		RetroCore* _tmp6_;
		_tmp4_ = self->priv->devices;
		_tmp4__length1 = self->priv->devices_length1;
		_g_object_unref0 (_tmp4_[port]);
		_tmp4_[port] = NULL;
		_tmp5_ = self->priv->controllers;
		_tmp5__length1 = self->priv->controllers_length1;
		_g_object_unref0 (_tmp5_[port]);
		_tmp5_[port] = NULL;
		_tmp6_ = self->priv->core;
		retro_core_set_controller (_tmp6_, (guint) port, NULL);
	}
	g_signal_emit (self, games_retro_input_manager_signals[GAMES_RETRO_INPUT_MANAGER_CONTROLLERS_CHANGED_SIGNAL], 0);
}

GamesInputMode
games_retro_input_manager_get_input_mode (GamesRetroInputManager* self)
{
	GamesInputMode result;
	g_return_val_if_fail (self != NULL, 0);
	result = self->priv->_input_mode;
	return result;
}

void
games_retro_input_manager_set_input_mode (GamesRetroInputManager* self,
                                          GamesInputMode value)
{
	g_return_if_fail (self != NULL);
	if (value == self->priv->_input_mode) {
		return;
	}
	self->priv->_input_mode = value;
	if (value == GAMES_INPUT_MODE_KEYBOARD) {
		RetroCore* _tmp0_;
		RetroCoreView* _tmp1_;
		_tmp0_ = self->priv->core;
		_tmp1_ = self->priv->view;
		retro_core_set_keyboard (_tmp0_, (GtkWidget*) _tmp1_);
	} else {
		RetroCore* _tmp2_;
		_tmp2_ = self->priv->core;
		retro_core_set_keyboard (_tmp2_, NULL);
	}
	games_retro_input_manager_update_core_view_gamepad (self);
	g_object_notify_by_pspec ((GObject *) self, games_retro_input_manager_properties[GAMES_RETRO_INPUT_MANAGER_INPUT_MODE_PROPERTY]);
}

static void
games_retro_input_manager_class_init (GamesRetroInputManagerClass * klass,
                                      gpointer klass_data)
{
	games_retro_input_manager_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesRetroInputManager_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_games_retro_input_manager_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_games_retro_input_manager_set_property;
	G_OBJECT_CLASS (klass)->finalize = games_retro_input_manager_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAMES_RETRO_INPUT_MANAGER_INPUT_MODE_PROPERTY, games_retro_input_manager_properties[GAMES_RETRO_INPUT_MANAGER_INPUT_MODE_PROPERTY] = g_param_spec_enum ("input-mode", "input-mode", "input-mode", GAMES_TYPE_INPUT_MODE, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	games_retro_input_manager_signals[GAMES_RETRO_INPUT_MANAGER_CONTROLLERS_CHANGED_SIGNAL] = g_signal_new ("controllers-changed", GAMES_TYPE_RETRO_INPUT_MANAGER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
games_retro_input_manager_instance_init (GamesRetroInputManager * self,
                                         gpointer klass)
{
	self->priv = games_retro_input_manager_get_instance_private (self);
}

static void
games_retro_input_manager_finalize (GObject * obj)
{
	GamesRetroInputManager * self;
	RetroCore* _tmp0_;
	RetroCore* _tmp1_;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_RETRO_INPUT_MANAGER, GamesRetroInputManager);
	_tmp0_ = self->priv->core;
	retro_core_set_keyboard (_tmp0_, NULL);
	_tmp1_ = self->priv->core;
	retro_core_set_controller (_tmp1_, (guint) self->priv->core_view_joypad_port, NULL);
	{
		gint port = 0;
		port = 0;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				RetroController** _tmp4_;
				gint _tmp4__length1;
				RetroCore* _tmp5_;
				if (!_tmp2_) {
					gint _tmp3_;
					_tmp3_ = port;
					port = _tmp3_ + 1;
				}
				_tmp2_ = FALSE;
				_tmp4_ = self->priv->controllers;
				_tmp4__length1 = self->priv->controllers_length1;
				if (!(port < _tmp4__length1)) {
					break;
				}
				_tmp5_ = self->priv->core;
				retro_core_set_controller (_tmp5_, (guint) port, NULL);
			}
		}
	}
	_g_object_unref0 (self->priv->core);
	_g_object_unref0 (self->priv->view);
	_g_object_unref0 (self->priv->core_view_joypad);
	_g_object_unref0 (self->priv->keyboard_mapping_manager);
	_g_object_unref0 (self->priv->monitor);
	self->priv->devices = (_vala_array_free (self->priv->devices, self->priv->devices_length1, (GDestroyNotify) g_object_unref), NULL);
	self->priv->controllers = (_vala_array_free (self->priv->controllers, self->priv->controllers_length1, (GDestroyNotify) g_object_unref), NULL);
	G_OBJECT_CLASS (games_retro_input_manager_parent_class)->finalize (obj);
}

static GType
games_retro_input_manager_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesRetroInputManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_retro_input_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesRetroInputManager), 0, (GInstanceInitFunc) games_retro_input_manager_instance_init, NULL };
	GType games_retro_input_manager_type_id;
	games_retro_input_manager_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesRetroInputManager", &g_define_type_info, 0);
	GamesRetroInputManager_private_offset = g_type_add_instance_private (games_retro_input_manager_type_id, sizeof (GamesRetroInputManagerPrivate));
	return games_retro_input_manager_type_id;
}

GType
games_retro_input_manager_get_type (void)
{
	static volatile gsize games_retro_input_manager_type_id__once = 0;
	if (g_once_init_enter (&games_retro_input_manager_type_id__once)) {
		GType games_retro_input_manager_type_id;
		games_retro_input_manager_type_id = games_retro_input_manager_get_type_once ();
		g_once_init_leave (&games_retro_input_manager_type_id__once, games_retro_input_manager_type_id);
	}
	return games_retro_input_manager_type_id__once;
}

static void
_vala_games_retro_input_manager_get_property (GObject * object,
                                              guint property_id,
                                              GValue * value,
                                              GParamSpec * pspec)
{
	GamesRetroInputManager * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_RETRO_INPUT_MANAGER, GamesRetroInputManager);
	switch (property_id) {
		case GAMES_RETRO_INPUT_MANAGER_INPUT_MODE_PROPERTY:
		g_value_set_enum (value, games_retro_input_manager_get_input_mode (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_games_retro_input_manager_set_property (GObject * object,
                                              guint property_id,
                                              const GValue * value,
                                              GParamSpec * pspec)
{
	GamesRetroInputManager * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_RETRO_INPUT_MANAGER, GamesRetroInputManager);
	switch (property_id) {
		case GAMES_RETRO_INPUT_MANAGER_INPUT_MODE_PROPERTY:
		games_retro_input_manager_set_input_mode (self, g_value_get_enum (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

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

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

