/* konami-code.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from konami-code.vala, do not modify */

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

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

#define GAMES_KONAMI_CODE_LAST_INDEX 9
#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_KONAMI_CODE (games_konami_code_get_type ())
#define GAMES_KONAMI_CODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_KONAMI_CODE, GamesKonamiCode))
#define GAMES_KONAMI_CODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_KONAMI_CODE, GamesKonamiCodeClass))
#define GAMES_IS_KONAMI_CODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_KONAMI_CODE))
#define GAMES_IS_KONAMI_CODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_KONAMI_CODE))
#define GAMES_KONAMI_CODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_KONAMI_CODE, GamesKonamiCodeClass))

typedef struct _GamesKonamiCode GamesKonamiCode;
typedef struct _GamesKonamiCodeClass GamesKonamiCodeClass;
typedef struct _GamesKonamiCodePrivate GamesKonamiCodePrivate;
enum  {
	GAMES_KONAMI_CODE_0_PROPERTY,
	GAMES_KONAMI_CODE_WIDGET_PROPERTY,
	GAMES_KONAMI_CODE_NUM_PROPERTIES
};
static GParamSpec* games_konami_code_properties[GAMES_KONAMI_CODE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
enum  {
	GAMES_KONAMI_CODE_CODE_PERFORMED_SIGNAL,
	GAMES_KONAMI_CODE_NUM_SIGNALS
};
static guint games_konami_code_signals[GAMES_KONAMI_CODE_NUM_SIGNALS] = {0};

struct _GamesKonamiCode {
	GObject parent_instance;
	GamesKonamiCodePrivate * priv;
};

struct _GamesKonamiCodeClass {
	GObjectClass parent_class;
};

struct _GamesKonamiCodePrivate {
	guint current_index;
	GtkWidget* _widget;
};

static gint GamesKonamiCode_private_offset;
static gpointer games_konami_code_parent_class = NULL;

VALA_EXTERN GType games_konami_code_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GamesKonamiCode* games_konami_code_new (GtkWidget* widget);
VALA_EXTERN GamesKonamiCode* games_konami_code_construct (GType object_type,
                                              GtkWidget* widget);
VALA_EXTERN void games_konami_code_reset (GamesKonamiCode* self);
static gboolean games_konami_code_on_key_pressed (GamesKonamiCode* self,
                                           GdkEventKey* event);
VALA_EXTERN GtkWidget* games_konami_code_get_widget (GamesKonamiCode* self);
static void games_konami_code_set_widget (GamesKonamiCode* self,
                                   GtkWidget* value);
static GObject * games_konami_code_constructor (GType type,
                                         guint n_construct_properties,
                                         GObjectConstructParam * construct_properties);
static gboolean _games_konami_code_on_key_pressed_gtk_widget_key_press_event (GtkWidget* _sender,
                                                                       GdkEventKey* event,
                                                                       gpointer self);
static void games_konami_code_finalize (GObject * obj);
static GType games_konami_code_get_type_once (void);
static void _vala_games_konami_code_get_property (GObject * object,
                                           guint property_id,
                                           GValue * value,
                                           GParamSpec * pspec);
static void _vala_games_konami_code_set_property (GObject * object,
                                           guint property_id,
                                           const GValue * value,
                                           GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static const guint GAMES_KONAMI_CODE_CODE_LOWER_KEYS[10] = {GDK_KEY_Up, GDK_KEY_Up, GDK_KEY_Down, GDK_KEY_Down, GDK_KEY_Left, GDK_KEY_Right, GDK_KEY_Left, GDK_KEY_Right, GDK_KEY_b, GDK_KEY_a};
static const guint GAMES_KONAMI_CODE_CODE_UPPER_KEYS[10] = {GDK_KEY_Up, GDK_KEY_Up, GDK_KEY_Down, GDK_KEY_Down, GDK_KEY_Left, GDK_KEY_Right, GDK_KEY_Left, GDK_KEY_Right, GDK_KEY_B, GDK_KEY_A};

static inline gpointer
games_konami_code_get_instance_private (GamesKonamiCode* self)
{
	return G_STRUCT_MEMBER_P (self, GamesKonamiCode_private_offset);
}

GamesKonamiCode*
games_konami_code_construct (GType object_type,
                             GtkWidget* widget)
{
	GamesKonamiCode * self = NULL;
	g_return_val_if_fail (widget != NULL, NULL);
	self = (GamesKonamiCode*) g_object_new (object_type, "widget", widget, NULL);
	return self;
}

GamesKonamiCode*
games_konami_code_new (GtkWidget* widget)
{
	return games_konami_code_construct (GAMES_TYPE_KONAMI_CODE, widget);
}

void
games_konami_code_reset (GamesKonamiCode* self)
{
	g_return_if_fail (self != NULL);
	self->priv->current_index = (guint) 0;
}

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

static gboolean
games_konami_code_on_key_pressed (GamesKonamiCode* self,
                                  GdkEventKey* event)
{
	guint keyval = 0U;
	GdkKeymap* keymap = NULL;
	GtkWidget* _tmp0_;
	GdkDisplay* _tmp1_;
	GdkKeymap* _tmp2_;
	GdkKeymap* _tmp3_;
	GdkKeymap* _tmp4_;
	guint _tmp5_ = 0U;
	gboolean _tmp6_ = FALSE;
	guint _tmp7_;
	guint _tmp9_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (event != NULL, FALSE);
	_tmp0_ = self->priv->_widget;
	_tmp1_ = gtk_widget_get_display (_tmp0_);
	_tmp2_ = gdk_keymap_get_for_display (_tmp1_);
	_tmp3_ = _g_object_ref0 (_tmp2_);
	keymap = _tmp3_;
	_tmp4_ = keymap;
	gdk_keymap_translate_keyboard_state (_tmp4_, (guint) event->hardware_keycode, event->state, (gint) event->group, &_tmp5_, NULL, NULL, NULL);
	keyval = _tmp5_;
	_tmp7_ = GAMES_KONAMI_CODE_CODE_LOWER_KEYS[self->priv->current_index];
	if (keyval != _tmp7_) {
		guint _tmp8_;
		_tmp8_ = GAMES_KONAMI_CODE_CODE_UPPER_KEYS[self->priv->current_index];
		_tmp6_ = keyval != _tmp8_;
	} else {
		_tmp6_ = FALSE;
	}
	if (_tmp6_) {
		self->priv->current_index = (guint) 0;
		result = FALSE;
		_g_object_unref0 (keymap);
		return result;
	}
	if (self->priv->current_index == ((guint) GAMES_KONAMI_CODE_LAST_INDEX)) {
		self->priv->current_index = (guint) 0;
		g_debug ("konami-code.vala:73: ↑ ↑ ↓ ↓ ← → ← → B A performed.");
		g_signal_emit (self, games_konami_code_signals[GAMES_KONAMI_CODE_CODE_PERFORMED_SIGNAL], 0);
		result = FALSE;
		_g_object_unref0 (keymap);
		return result;
	}
	_tmp9_ = self->priv->current_index;
	self->priv->current_index = _tmp9_ + 1;
	result = FALSE;
	_g_object_unref0 (keymap);
	return result;
}

GtkWidget*
games_konami_code_get_widget (GamesKonamiCode* self)
{
	GtkWidget* result;
	GtkWidget* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_widget;
	result = _tmp0_;
	return result;
}

static void
games_konami_code_set_widget (GamesKonamiCode* self,
                              GtkWidget* value)
{
	GtkWidget* old_value;
	g_return_if_fail (self != NULL);
	old_value = games_konami_code_get_widget (self);
	if (old_value != value) {
		GtkWidget* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_widget);
		self->priv->_widget = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, games_konami_code_properties[GAMES_KONAMI_CODE_WIDGET_PROPERTY]);
	}
}

static gboolean
_games_konami_code_on_key_pressed_gtk_widget_key_press_event (GtkWidget* _sender,
                                                              GdkEventKey* event,
                                                              gpointer self)
{
	gboolean result;
	result = games_konami_code_on_key_pressed ((GamesKonamiCode*) self, event);
	return result;
}

static GObject *
games_konami_code_constructor (GType type,
                               guint n_construct_properties,
                               GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GamesKonamiCode * self;
	GtkWidget* _tmp0_;
	parent_class = G_OBJECT_CLASS (games_konami_code_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_KONAMI_CODE, GamesKonamiCode);
	_tmp0_ = self->priv->_widget;
	g_signal_connect_object (_tmp0_, "key-press-event", (GCallback) _games_konami_code_on_key_pressed_gtk_widget_key_press_event, self, 0);
	return obj;
}

static void
games_konami_code_class_init (GamesKonamiCodeClass * klass,
                              gpointer klass_data)
{
	games_konami_code_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GamesKonamiCode_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_games_konami_code_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_games_konami_code_set_property;
	G_OBJECT_CLASS (klass)->constructor = games_konami_code_constructor;
	G_OBJECT_CLASS (klass)->finalize = games_konami_code_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GAMES_KONAMI_CODE_WIDGET_PROPERTY, games_konami_code_properties[GAMES_KONAMI_CODE_WIDGET_PROPERTY] = g_param_spec_object ("widget", "widget", "widget", gtk_widget_get_type (), G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	games_konami_code_signals[GAMES_KONAMI_CODE_CODE_PERFORMED_SIGNAL] = g_signal_new ("code-performed", GAMES_TYPE_KONAMI_CODE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
games_konami_code_instance_init (GamesKonamiCode * self,
                                 gpointer klass)
{
	self->priv = games_konami_code_get_instance_private (self);
}

static void
games_konami_code_finalize (GObject * obj)
{
	GamesKonamiCode * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GAMES_TYPE_KONAMI_CODE, GamesKonamiCode);
	_g_object_unref0 (self->priv->_widget);
	G_OBJECT_CLASS (games_konami_code_parent_class)->finalize (obj);
}

static GType
games_konami_code_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GamesKonamiCodeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) games_konami_code_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GamesKonamiCode), 0, (GInstanceInitFunc) games_konami_code_instance_init, NULL };
	GType games_konami_code_type_id;
	games_konami_code_type_id = g_type_register_static (G_TYPE_OBJECT, "GamesKonamiCode", &g_define_type_info, 0);
	GamesKonamiCode_private_offset = g_type_add_instance_private (games_konami_code_type_id, sizeof (GamesKonamiCodePrivate));
	return games_konami_code_type_id;
}

GType
games_konami_code_get_type (void)
{
	static volatile gsize games_konami_code_type_id__once = 0;
	if (g_once_init_enter (&games_konami_code_type_id__once)) {
		GType games_konami_code_type_id;
		games_konami_code_type_id = games_konami_code_get_type_once ();
		g_once_init_leave (&games_konami_code_type_id__once, games_konami_code_type_id);
	}
	return games_konami_code_type_id__once;
}

static void
_vala_games_konami_code_get_property (GObject * object,
                                      guint property_id,
                                      GValue * value,
                                      GParamSpec * pspec)
{
	GamesKonamiCode * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_KONAMI_CODE, GamesKonamiCode);
	switch (property_id) {
		case GAMES_KONAMI_CODE_WIDGET_PROPERTY:
		g_value_set_object (value, games_konami_code_get_widget (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_games_konami_code_set_property (GObject * object,
                                      guint property_id,
                                      const GValue * value,
                                      GParamSpec * pspec)
{
	GamesKonamiCode * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GAMES_TYPE_KONAMI_CODE, GamesKonamiCode);
	switch (property_id) {
		case GAMES_KONAMI_CODE_WIDGET_PROPERTY:
		games_konami_code_set_widget (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;
}

