/* keyboard-mapping-builder.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from keyboard-mapping-builder.vala, do not modify */

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

#include <glib-object.h>
#include <retro-gtk.h>
#include <glib.h>
#include <string.h>
#include "gnome-games.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_KEYBOARD_MAPPING_BUILDER (games_keyboard_mapping_builder_get_type ())
#define GAMES_KEYBOARD_MAPPING_BUILDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_KEYBOARD_MAPPING_BUILDER, GamesKeyboardMappingBuilder))
#define GAMES_KEYBOARD_MAPPING_BUILDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_KEYBOARD_MAPPING_BUILDER, GamesKeyboardMappingBuilderClass))
#define GAMES_IS_KEYBOARD_MAPPING_BUILDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_KEYBOARD_MAPPING_BUILDER))
#define GAMES_IS_KEYBOARD_MAPPING_BUILDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_KEYBOARD_MAPPING_BUILDER))
#define GAMES_KEYBOARD_MAPPING_BUILDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_KEYBOARD_MAPPING_BUILDER, GamesKeyboardMappingBuilderClass))

typedef struct _GamesKeyboardMappingBuilder GamesKeyboardMappingBuilder;
typedef struct _GamesKeyboardMappingBuilderClass GamesKeyboardMappingBuilderClass;
typedef struct _GamesKeyboardMappingBuilderPrivate GamesKeyboardMappingBuilderPrivate;
enum  {
	GAMES_KEYBOARD_MAPPING_BUILDER_0_PROPERTY,
	GAMES_KEYBOARD_MAPPING_BUILDER_MAPPING_PROPERTY,
	GAMES_KEYBOARD_MAPPING_BUILDER_NUM_PROPERTIES
};
static GParamSpec* games_keyboard_mapping_builder_properties[GAMES_KEYBOARD_MAPPING_BUILDER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define GAMES_TYPE_GAMEPAD_INPUT (games_gamepad_input_get_type ())
typedef struct _GamesGamepadInput GamesGamepadInput;

struct _GamesKeyboardMappingBuilder {
	GObject parent_instance;
	GamesKeyboardMappingBuilderPrivate * priv;
};

struct _GamesKeyboardMappingBuilderClass {
	GObjectClass parent_class;
};

struct _GamesKeyboardMappingBuilderPrivate {
	RetroKeyJoypadMapping* _mapping;
};

struct _GamesGamepadInput {
	guint16 type;
	guint16 code;
};

static gint GamesKeyboardMappingBuilder_private_offset;
static gpointer games_keyboard_mapping_builder_parent_class = NULL;

VALA_EXTERN GType games_keyboard_mapping_builder_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType games_gamepad_input_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GamesGamepadInput* games_gamepad_input_dup (const GamesGamepadInput* self);
VALA_EXTERN void games_gamepad_input_free (GamesGamepadInput* self);
VALA_EXTERN gboolean games_keyboard_mapping_builder_set_input_mapping (GamesKeyboardMappingBuilder* self,
                                                           GamesGamepadInput* input,
                                                           guint16 keycode);
VALA_EXTERN RetroKeyJoypadMapping* games_keyboard_mapping_builder_get_mapping (GamesKeyboardMappingBuilder* self);
VALA_EXTERN GamesKeyboardMappingBuilder* games_keyboard_mapping_builder_new (void);
VALA_EXTERN GamesKeyboardMappingBuilder* games_keyboard_mapping_builder_construct (GType object_type);
static void games_keyboard_mapping_builder_set_mapping (GamesKeyboardMappingBuilder* self,
                                                 RetroKeyJoypadMapping* value);
static GObject * games_keyboard_mapping_builder_constructor (GType type,
                                                      guint n_construct_properties,
                                                      GObjectConstructParam * construct_properties);
static void games_keyboard_mapping_builder_finalize (GObject * obj);
static GType games_keyboard_mapping_builder_get_type_once (void);
static void _vala_games_keyboard_mapping_builder_get_property (GObject * object,
                                                        guint property_id,
                                                        GValue * value,
                                                        GParamSpec * pspec);
static void _vala_games_keyboard_mapping_builder_set_property (GObject * object,
                                                        guint property_id,
                                                        const GValue * value,
                                                        GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
games_keyboard_mapping_builder_get_instance_private (GamesKeyboardMappingBuilder* self)
{
	return G_STRUCT_MEMBER_P (self, GamesKeyboardMappingBuilder_private_offset);
}

gboolean
games_keyboard_mapping_builder_set_input_mapping (GamesKeyboardMappingBuilder* self,
                                                  GamesGamepadInput* input,
                                                  guint16 keycode)
{
	RetroJoypadId joypad_id = 0;
	GamesGamepadInput _tmp0_;
	gint count = 0;
	RetroKeyJoypadMapping* _tmp3_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (input != NULL, FALSE);
	_tmp0_ = *input;
	joypad_id = retro_joypad_id_from_button_code (_tmp0_.code);
	count = retro_controller_type_get_id_count (RETRO_CONTROLLER_TYPE_JOYPAD);
	if (joypad_id >= count) {
		result = FALSE;
		return result;
	}
	{
		RetroJoypadId i = 0;
		i = 0;
		{
			gboolean _tmp1_ = FALSE;
			_tmp1_ = TRUE;
			while (TRUE) {
				RetroKeyJoypadMapping* _tmp2_;
				if (!_tmp1_) {
					i += 1;
				}
				_tmp1_ = FALSE;
				if (!(i < count)) {
					break;
				}
				_tmp2_ = self->priv->_mapping;
				if (retro_key_joypad_mapping_get_button_key (_tmp2_, i) == keycode) {
					result = FALSE;
					return result;
				}
			}
		}
	}
	_tmp3_ = self->priv->_mapping;
	retro_key_joypad_mapping_set_button_key (_tmp3_, joypad_id, keycode);
	result = TRUE;
	return result;
}

GamesKeyboardMappingBuilder*
games_keyboard_mapping_builder_construct (GType object_type)
{
	GamesKeyboardMappingBuilder * self = NULL;
	self = (GamesKeyboardMappingBuilder*) g_object_new (object_type, NULL);
	return self;
}

GamesKeyboardMappingBuilder*
games_keyboard_mapping_builder_new (void)
{
	return games_keyboard_mapping_builder_construct (GAMES_TYPE_KEYBOARD_MAPPING_BUILDER);
}

RetroKeyJoypadMapping*
games_keyboard_mapping_builder_get_mapping (GamesKeyboardMappingBuilder* self)
{
	RetroKeyJoypadMapping* result;
	RetroKeyJoypadMapping* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_mapping;
	result = _tmp0_;
	return result;
}

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

static void
games_keyboard_mapping_builder_set_mapping (GamesKeyboardMappingBuilder* self,
                                            RetroKeyJoypadMapping* value)
{
	RetroKeyJoypadMapping* old_value;
	g_return_if_fail (self != NULL);
	old_value = games_keyboard_mapping_builder_get_mapping (self);
	if (old_value != value) {
		RetroKeyJoypadMapping* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_mapping);
		self->priv->_mapping = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, games_keyboard_mapping_builder_properties[GAMES_KEYBOARD_MAPPING_BUILDER_MAPPING_PROPERTY]);
	}
}

static GObject *
games_keyboard_mapping_builder_constructor (GType type,
                                            guint n_construct_properties,
                                            GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GamesKeyboardMappingBuilder * self;
	RetroKeyJoypadMapping* _tmp0_;
	RetroKeyJoypadMapping* _tmp1_;
	parent_class = G_OBJECT_CLASS (games_keyboard_mapping_builder_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_KEYBOARD_MAPPING_BUILDER, GamesKeyboardMappingBuilder);
	_tmp0_ = retro_key_joypad_mapping_new ();
	_tmp1_ = _tmp0_;
	games_keyboard_mapping_builder_set_mapping (self, _tmp1_);
	_g_object_unref0 (_tmp1_);
	return obj;
}

static void
games_keyboard_mapping_builder_class_init (GamesKeyboardMappingBuilderClass * klass,
                                           gpointer klass_data)
{
	games_keyboard_mapping_builder_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesKeyboardMappingBuilder_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_games_keyboard_mapping_builder_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_games_keyboard_mapping_builder_set_property;
	G_OBJECT_CLASS (klass)->constructor = games_keyboard_mapping_builder_constructor;
	G_OBJECT_CLASS (klass)->finalize = games_keyboard_mapping_builder_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAMES_KEYBOARD_MAPPING_BUILDER_MAPPING_PROPERTY, games_keyboard_mapping_builder_properties[GAMES_KEYBOARD_MAPPING_BUILDER_MAPPING_PROPERTY] = g_param_spec_object ("mapping", "mapping", "mapping", retro_key_joypad_mapping_get_type (), G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
games_keyboard_mapping_builder_instance_init (GamesKeyboardMappingBuilder * self,
                                              gpointer klass)
{
	self->priv = games_keyboard_mapping_builder_get_instance_private (self);
}

static void
games_keyboard_mapping_builder_finalize (GObject * obj)
{
	GamesKeyboardMappingBuilder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_KEYBOARD_MAPPING_BUILDER, GamesKeyboardMappingBuilder);
	_g_object_unref0 (self->priv->_mapping);
	G_OBJECT_CLASS (games_keyboard_mapping_builder_parent_class)->finalize (obj);
}

static GType
games_keyboard_mapping_builder_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesKeyboardMappingBuilderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_keyboard_mapping_builder_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesKeyboardMappingBuilder), 0, (GInstanceInitFunc) games_keyboard_mapping_builder_instance_init, NULL };
	GType games_keyboard_mapping_builder_type_id;
	games_keyboard_mapping_builder_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesKeyboardMappingBuilder", &g_define_type_info, 0);
	GamesKeyboardMappingBuilder_private_offset = g_type_add_instance_private (games_keyboard_mapping_builder_type_id, sizeof (GamesKeyboardMappingBuilderPrivate));
	return games_keyboard_mapping_builder_type_id;
}

GType
games_keyboard_mapping_builder_get_type (void)
{
	static volatile gsize games_keyboard_mapping_builder_type_id__once = 0;
	if (g_once_init_enter (&games_keyboard_mapping_builder_type_id__once)) {
		GType games_keyboard_mapping_builder_type_id;
		games_keyboard_mapping_builder_type_id = games_keyboard_mapping_builder_get_type_once ();
		g_once_init_leave (&games_keyboard_mapping_builder_type_id__once, games_keyboard_mapping_builder_type_id);
	}
	return games_keyboard_mapping_builder_type_id__once;
}

static void
_vala_games_keyboard_mapping_builder_get_property (GObject * object,
                                                   guint property_id,
                                                   GValue * value,
                                                   GParamSpec * pspec)
{
	GamesKeyboardMappingBuilder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_KEYBOARD_MAPPING_BUILDER, GamesKeyboardMappingBuilder);
	switch (property_id) {
		case GAMES_KEYBOARD_MAPPING_BUILDER_MAPPING_PROPERTY:
		g_value_set_object (value, games_keyboard_mapping_builder_get_mapping (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_games_keyboard_mapping_builder_set_property (GObject * object,
                                                   guint property_id,
                                                   const GValue * value,
                                                   GParamSpec * pspec)
{
	GamesKeyboardMappingBuilder * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_KEYBOARD_MAPPING_BUILDER, GamesKeyboardMappingBuilder);
	switch (property_id) {
		case GAMES_KEYBOARD_MAPPING_BUILDER_MAPPING_PROPERTY:
		games_keyboard_mapping_builder_set_mapping (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

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

