/* keyboard-state.c generated by valac 0.36.4, the Vala compiler
 * generated from keyboard-state.vala, do not modify */

/* This file is part of retro-gtk. License: GPLv3*/

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


#define RETRO_TYPE_KEYBOARD_STATE (retro_keyboard_state_get_type ())
#define RETRO_KEYBOARD_STATE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardState))
#define RETRO_KEYBOARD_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardStateClass))
#define RETRO_IS_KEYBOARD_STATE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RETRO_TYPE_KEYBOARD_STATE))
#define RETRO_IS_KEYBOARD_STATE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RETRO_TYPE_KEYBOARD_STATE))
#define RETRO_KEYBOARD_STATE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardStateClass))

typedef struct _RetroKeyboardState RetroKeyboardState;
typedef struct _RetroKeyboardStateClass RetroKeyboardStateClass;
typedef struct _RetroKeyboardStatePrivate RetroKeyboardStatePrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
#define _g_list_free0(var) ((var == NULL) ? NULL : (var = (g_list_free (var), NULL)))
enum  {
	RETRO_KEYBOARD_STATE_KEY_STATE_CHANGED_SIGNAL,
	RETRO_KEYBOARD_STATE_LAST_SIGNAL
};
static guint retro_keyboard_state_signals[RETRO_KEYBOARD_STATE_LAST_SIGNAL] = {0};

struct _RetroKeyboardState {
	GObject parent_instance;
	RetroKeyboardStatePrivate * priv;
};

struct _RetroKeyboardStateClass {
	GObjectClass parent_class;
};

struct _RetroKeyboardStatePrivate {
	GtkWidget* _widget;
	GHashTable* key_state;
	GdkKeymap* keymap;
};


static gpointer retro_keyboard_state_parent_class = NULL;

GType retro_keyboard_state_get_type (void) G_GNUC_CONST;
#define RETRO_KEYBOARD_STATE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardStatePrivate))
enum  {
	RETRO_KEYBOARD_STATE_DUMMY_PROPERTY,
	RETRO_KEYBOARD_STATE_WIDGET
};
RetroKeyboardState* retro_keyboard_state_new (GtkWidget* widget);
RetroKeyboardState* retro_keyboard_state_construct (GType object_type, GtkWidget* widget);
static gboolean retro_keyboard_state_on_key_press_event (RetroKeyboardState* self, GtkWidget* source, GdkEventKey* event);
static guint* _uint_dup (guint* self);
static gboolean* _bool_dup (gboolean* self);
static gboolean retro_keyboard_state_on_key_release_event (RetroKeyboardState* self, GtkWidget* source, GdkEventKey* event);
gboolean retro_keyboard_state_get_key_state (RetroKeyboardState* self, guint16 hardware_keycode);
GtkWidget* retro_keyboard_state_get_widget (RetroKeyboardState* self);
static void retro_keyboard_state_set_widget (RetroKeyboardState* self, GtkWidget* value);
static GObject * retro_keyboard_state_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static gboolean _retro_keyboard_state___lambda7_ (RetroKeyboardState* self);
static gboolean __retro_keyboard_state___lambda7__gtk_widget_focus_in_event (GtkWidget* _sender, GdkEventFocus* event, gpointer self);
static gboolean _retro_keyboard_state___lambda8_ (RetroKeyboardState* self);
static gboolean __retro_keyboard_state___lambda8__gtk_widget_focus_out_event (GtkWidget* _sender, GdkEventFocus* event, gpointer self);
static gboolean _retro_keyboard_state___lambda9_ (RetroKeyboardState* self);
static gboolean __retro_keyboard_state___lambda9__gtk_widget_button_press_event (GtkWidget* _sender, GdkEventButton* event, gpointer self);
static gboolean _retro_keyboard_state_on_key_press_event_gtk_widget_key_press_event (GtkWidget* _sender, GdkEventKey* event, gpointer self);
static gboolean _retro_keyboard_state_on_key_release_event_gtk_widget_key_release_event (GtkWidget* _sender, GdkEventKey* event, gpointer self);
static void _g_free0_ (gpointer var);
static void retro_keyboard_state_finalize (GObject * obj);
static void _vala_retro_keyboard_state_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_retro_keyboard_state_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


RetroKeyboardState* retro_keyboard_state_construct (GType object_type, GtkWidget* widget) {
	RetroKeyboardState * self = NULL;
	GtkWidget* _tmp0_;
	g_return_val_if_fail (widget != NULL, NULL);
	_tmp0_ = widget;
	self = (RetroKeyboardState*) g_object_new (object_type, "widget", _tmp0_, NULL);
	return self;
}


RetroKeyboardState* retro_keyboard_state_new (GtkWidget* widget) {
	return retro_keyboard_state_construct (RETRO_TYPE_KEYBOARD_STATE, widget);
}


static guint* _uint_dup (guint* self) {
	guint* dup;
	dup = g_new0 (guint, 1);
	memcpy (dup, self, sizeof (guint));
	return dup;
}


static gpointer __uint_dup0 (gpointer self) {
	return self ? _uint_dup (self) : NULL;
}


static gboolean* _bool_dup (gboolean* self) {
	gboolean* dup;
	dup = g_new0 (gboolean, 1);
	memcpy (dup, self, sizeof (gboolean));
	return dup;
}


static gpointer __bool_dup0 (gpointer self) {
	return self ? _bool_dup (self) : NULL;
}


static gboolean retro_keyboard_state_on_key_press_event (RetroKeyboardState* self, GtkWidget* source, GdkEventKey* event) {
	gboolean result = FALSE;
	GHashTable* _tmp0_;
	GdkEventKey* _tmp1_;
	guint16 _tmp2_;
	guint _tmp3_;
	gboolean _tmp4_;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (source != NULL, FALSE);
	g_return_val_if_fail (event != NULL, FALSE);
	_tmp0_ = self->priv->key_state;
	_tmp1_ = event;
	_tmp2_ = _tmp1_->hardware_keycode;
	_tmp3_ = (guint) _tmp2_;
	_tmp4_ = g_hash_table_contains (_tmp0_, &_tmp3_);
	if (_tmp4_) {
		GHashTable* _tmp5_;
		GdkEventKey* _tmp6_;
		guint16 _tmp7_;
		guint _tmp8_;
		guint* _tmp9_;
		gboolean _tmp10_;
		gboolean* _tmp11_;
		_tmp5_ = self->priv->key_state;
		_tmp6_ = event;
		_tmp7_ = _tmp6_->hardware_keycode;
		_tmp8_ = (guint) _tmp7_;
		_tmp9_ = __uint_dup0 (&_tmp8_);
		_tmp10_ = TRUE;
		_tmp11_ = __bool_dup0 (&_tmp10_);
		g_hash_table_replace (_tmp5_, _tmp9_, _tmp11_);
	} else {
		GHashTable* _tmp12_;
		GdkEventKey* _tmp13_;
		guint16 _tmp14_;
		guint _tmp15_;
		guint* _tmp16_;
		gboolean _tmp17_;
		gboolean* _tmp18_;
		_tmp12_ = self->priv->key_state;
		_tmp13_ = event;
		_tmp14_ = _tmp13_->hardware_keycode;
		_tmp15_ = (guint) _tmp14_;
		_tmp16_ = __uint_dup0 (&_tmp15_);
		_tmp17_ = TRUE;
		_tmp18_ = __bool_dup0 (&_tmp17_);
		g_hash_table_insert (_tmp12_, _tmp16_, _tmp18_);
	}
	g_signal_emit (self, retro_keyboard_state_signals[RETRO_KEYBOARD_STATE_KEY_STATE_CHANGED_SIGNAL], 0);
	result = FALSE;
	return result;
}


static gboolean retro_keyboard_state_on_key_release_event (RetroKeyboardState* self, GtkWidget* source, GdkEventKey* event) {
	gboolean result = FALSE;
	GHashTable* _tmp0_;
	GdkEventKey* _tmp1_;
	guint16 _tmp2_;
	guint _tmp3_;
	gboolean _tmp4_;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (source != NULL, FALSE);
	g_return_val_if_fail (event != NULL, FALSE);
	_tmp0_ = self->priv->key_state;
	_tmp1_ = event;
	_tmp2_ = _tmp1_->hardware_keycode;
	_tmp3_ = (guint) _tmp2_;
	_tmp4_ = g_hash_table_contains (_tmp0_, &_tmp3_);
	if (_tmp4_) {
		GHashTable* _tmp5_;
		GdkEventKey* _tmp6_;
		guint16 _tmp7_;
		guint _tmp8_;
		guint* _tmp9_;
		gboolean _tmp10_;
		gboolean* _tmp11_;
		_tmp5_ = self->priv->key_state;
		_tmp6_ = event;
		_tmp7_ = _tmp6_->hardware_keycode;
		_tmp8_ = (guint) _tmp7_;
		_tmp9_ = __uint_dup0 (&_tmp8_);
		_tmp10_ = FALSE;
		_tmp11_ = __bool_dup0 (&_tmp10_);
		g_hash_table_replace (_tmp5_, _tmp9_, _tmp11_);
	}
	g_signal_emit (self, retro_keyboard_state_signals[RETRO_KEYBOARD_STATE_KEY_STATE_CHANGED_SIGNAL], 0);
	result = FALSE;
	return result;
}


gboolean retro_keyboard_state_get_key_state (RetroKeyboardState* self, guint16 hardware_keycode) {
	gboolean result = FALSE;
	GHashTable* _tmp0_;
	guint16 _tmp1_;
	guint _tmp2_;
	gboolean _tmp3_;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->priv->key_state;
	_tmp1_ = hardware_keycode;
	_tmp2_ = (guint) _tmp1_;
	_tmp3_ = g_hash_table_contains (_tmp0_, &_tmp2_);
	if (_tmp3_) {
		GHashTable* _tmp4_;
		guint16 _tmp5_;
		guint _tmp6_;
		gconstpointer _tmp7_;
		_tmp4_ = self->priv->key_state;
		_tmp5_ = hardware_keycode;
		_tmp6_ = (guint) _tmp5_;
		_tmp7_ = g_hash_table_lookup (_tmp4_, &_tmp6_);
		result = *((gboolean*) _tmp7_);
		return result;
	}
	result = FALSE;
	return result;
}


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


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


static void retro_keyboard_state_set_widget (RetroKeyboardState* self, GtkWidget* value) {
	g_return_if_fail (self != NULL);
	if (retro_keyboard_state_get_widget (self) != value) {
		GtkWidget* _tmp0_;
		GtkWidget* _tmp1_;
		_tmp0_ = value;
		_tmp1_ = _g_object_ref0 (_tmp0_);
		_g_object_unref0 (self->priv->_widget);
		self->priv->_widget = _tmp1_;
		g_object_notify ((GObject *) self, "widget");
	}
}


static gboolean _retro_keyboard_state___lambda7_ (RetroKeyboardState* self) {
	gboolean result = FALSE;
	GtkWidget* _tmp0_;
	_tmp0_ = self->priv->_widget;
	g_object_set (_tmp0_, "has-focus", TRUE, NULL);
	result = FALSE;
	return result;
}


static gboolean __retro_keyboard_state___lambda7__gtk_widget_focus_in_event (GtkWidget* _sender, GdkEventFocus* event, gpointer self) {
	gboolean result;
	result = _retro_keyboard_state___lambda7_ ((RetroKeyboardState*) self);
	return result;
}


static gboolean _retro_keyboard_state___lambda8_ (RetroKeyboardState* self) {
	gboolean result = FALSE;
	GtkWidget* _tmp0_;
	GHashTable* _tmp1_;
	GList* _tmp2_;
	_tmp0_ = self->priv->_widget;
	g_object_set (_tmp0_, "has-focus", FALSE, NULL);
	_tmp1_ = self->priv->key_state;
	_tmp2_ = g_hash_table_get_keys (_tmp1_);
	{
		GList* key_collection = NULL;
		GList* key_it = NULL;
		key_collection = _tmp2_;
		for (key_it = key_collection; key_it != NULL; key_it = key_it->next) {
			guint* key = NULL;
			key = (guint*) key_it->data;
			{
				GHashTable* _tmp3_;
				guint* _tmp4_;
				guint* _tmp5_;
				gboolean _tmp6_;
				gboolean* _tmp7_;
				_tmp3_ = self->priv->key_state;
				_tmp4_ = key;
				_tmp5_ = __uint_dup0 (_tmp4_);
				_tmp6_ = FALSE;
				_tmp7_ = __bool_dup0 (&_tmp6_);
				g_hash_table_insert (_tmp3_, _tmp5_, _tmp7_);
			}
		}
		_g_list_free0 (key_collection);
	}
	g_signal_emit (self, retro_keyboard_state_signals[RETRO_KEYBOARD_STATE_KEY_STATE_CHANGED_SIGNAL], 0);
	result = FALSE;
	return result;
}


static gboolean __retro_keyboard_state___lambda8__gtk_widget_focus_out_event (GtkWidget* _sender, GdkEventFocus* event, gpointer self) {
	gboolean result;
	result = _retro_keyboard_state___lambda8_ ((RetroKeyboardState*) self);
	return result;
}


static gboolean _retro_keyboard_state___lambda9_ (RetroKeyboardState* self) {
	gboolean result = FALSE;
	GtkWidget* _tmp0_;
	_tmp0_ = self->priv->_widget;
	gtk_widget_grab_focus (_tmp0_);
	result = FALSE;
	return result;
}


static gboolean __retro_keyboard_state___lambda9__gtk_widget_button_press_event (GtkWidget* _sender, GdkEventButton* event, gpointer self) {
	gboolean result;
	result = _retro_keyboard_state___lambda9_ ((RetroKeyboardState*) self);
	return result;
}


static gboolean _retro_keyboard_state_on_key_press_event_gtk_widget_key_press_event (GtkWidget* _sender, GdkEventKey* event, gpointer self) {
	gboolean result;
	result = retro_keyboard_state_on_key_press_event ((RetroKeyboardState*) self, _sender, event);
	return result;
}


static gboolean _retro_keyboard_state_on_key_release_event_gtk_widget_key_release_event (GtkWidget* _sender, GdkEventKey* event, gpointer self) {
	gboolean result;
	result = retro_keyboard_state_on_key_release_event ((RetroKeyboardState*) self, _sender, event);
	return result;
}


static void _g_free0_ (gpointer var) {
	(var == NULL) ? NULL : (var = (g_free (var), NULL));
}


static GObject * retro_keyboard_state_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	RetroKeyboardState * self;
	GtkWidget* _tmp0_;
	GtkWidget* _tmp1_;
	GtkWidget* _tmp2_;
	GtkWidget* _tmp3_;
	GtkWidget* _tmp4_;
	GtkWidget* _tmp5_;
	GHashFunc _tmp6_;
	GEqualFunc _tmp7_;
	GHashTable* _tmp8_;
	GdkKeymap* _tmp9_;
	GdkKeymap* _tmp10_;
	parent_class = G_OBJECT_CLASS (retro_keyboard_state_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardState);
	_tmp0_ = self->priv->_widget;
	gtk_widget_set_can_focus (_tmp0_, TRUE);
	_tmp1_ = self->priv->_widget;
	g_signal_connect_object (_tmp1_, "focus-in-event", (GCallback) __retro_keyboard_state___lambda7__gtk_widget_focus_in_event, self, 0);
	_tmp2_ = self->priv->_widget;
	g_signal_connect_object (_tmp2_, "focus-out-event", (GCallback) __retro_keyboard_state___lambda8__gtk_widget_focus_out_event, self, 0);
	_tmp3_ = self->priv->_widget;
	g_signal_connect_object (_tmp3_, "button-press-event", (GCallback) __retro_keyboard_state___lambda9__gtk_widget_button_press_event, self, 0);
	_tmp4_ = self->priv->_widget;
	g_signal_connect_object (_tmp4_, "key-press-event", (GCallback) _retro_keyboard_state_on_key_press_event_gtk_widget_key_press_event, self, 0);
	_tmp5_ = self->priv->_widget;
	g_signal_connect_object (_tmp5_, "key-release-event", (GCallback) _retro_keyboard_state_on_key_release_event_gtk_widget_key_release_event, self, 0);
	_tmp6_ = g_int_hash;
	_tmp7_ = g_int_equal;
	_tmp8_ = g_hash_table_new_full (_tmp6_, _tmp7_, _g_free0_, _g_free0_);
	_g_hash_table_unref0 (self->priv->key_state);
	self->priv->key_state = _tmp8_;
	_tmp9_ = gdk_keymap_get_default ();
	_tmp10_ = _g_object_ref0 (_tmp9_);
	_g_object_unref0 (self->priv->keymap);
	self->priv->keymap = _tmp10_;
	return obj;
}


static void retro_keyboard_state_class_init (RetroKeyboardStateClass * klass) {
	retro_keyboard_state_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RetroKeyboardStatePrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_retro_keyboard_state_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_retro_keyboard_state_set_property;
	G_OBJECT_CLASS (klass)->constructor = retro_keyboard_state_constructor;
	G_OBJECT_CLASS (klass)->finalize = retro_keyboard_state_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), RETRO_KEYBOARD_STATE_WIDGET, g_param_spec_object ("widget", "widget", "widget", gtk_widget_get_type (), G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	retro_keyboard_state_signals[RETRO_KEYBOARD_STATE_KEY_STATE_CHANGED_SIGNAL] = g_signal_new ("key_state_changed", RETRO_TYPE_KEYBOARD_STATE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}


static void retro_keyboard_state_instance_init (RetroKeyboardState * self) {
	self->priv = RETRO_KEYBOARD_STATE_GET_PRIVATE (self);
}


static void retro_keyboard_state_finalize (GObject * obj) {
	RetroKeyboardState * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardState);
	_g_object_unref0 (self->priv->_widget);
	_g_hash_table_unref0 (self->priv->key_state);
	_g_object_unref0 (self->priv->keymap);
	G_OBJECT_CLASS (retro_keyboard_state_parent_class)->finalize (obj);
}


GType retro_keyboard_state_get_type (void) {
	static volatile gsize retro_keyboard_state_type_id__volatile = 0;
	if (g_once_init_enter (&retro_keyboard_state_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RetroKeyboardStateClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) retro_keyboard_state_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RetroKeyboardState), 0, (GInstanceInitFunc) retro_keyboard_state_instance_init, NULL };
		GType retro_keyboard_state_type_id;
		retro_keyboard_state_type_id = g_type_register_static (G_TYPE_OBJECT, "RetroKeyboardState", &g_define_type_info, 0);
		g_once_init_leave (&retro_keyboard_state_type_id__volatile, retro_keyboard_state_type_id);
	}
	return retro_keyboard_state_type_id__volatile;
}


static void _vala_retro_keyboard_state_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	RetroKeyboardState * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardState);
	switch (property_id) {
		case RETRO_KEYBOARD_STATE_WIDGET:
		g_value_set_object (value, retro_keyboard_state_get_widget (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_retro_keyboard_state_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	RetroKeyboardState * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, RETRO_TYPE_KEYBOARD_STATE, RetroKeyboardState);
	switch (property_id) {
		case RETRO_KEYBOARD_STATE_WIDGET:
		retro_keyboard_state_set_widget (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



