/* Root.c generated by valac 0.56.19, the Vala compiler
 * generated from Root.vala, do not modify */

/*
 * Copyright 2025 elementary, Inc. (https://elementary.io)
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Authored by: Leonhard Kargl <leo.kargl@proton.me>
 */

#include "gala.h"
#include <glib.h>
#include <clutter/clutter.h>
#include <glib-object.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

enum  {
	GALA_ROOT_0_PROPERTY,
	GALA_ROOT_FOCUS_VISIBLE_PROPERTY,
	GALA_ROOT_NUM_PROPERTIES
};
static GParamSpec* gala_root_properties[GALA_ROOT_NUM_PROPERTIES];
#define _g_free0(var) ((var == NULL) ? NULL : (var = (g_free (var), NULL)))

struct _GalaRootPrivate {
	gboolean _focus_visible;
	guint timeout_id;
};

static gint GalaRoot_private_offset;
static gpointer gala_root_parent_class = NULL;

static void gala_root_real_map (ClutterActor* base);
static gboolean gala_root_check_focus (GalaRoot* self,
                                ClutterEvent* event);
static gboolean _gala_root_check_focus_clutter_actor_key_press_event (ClutterActor* _sender,
                                                               ClutterEvent* event,
                                                               gpointer self);
static void gala_root_real_unmap (ClutterActor* base);
static gboolean gala_root_real_key_press_event (ClutterActor* base,
                                         ClutterEvent* event);
static void gala_root_show_focus (GalaRoot* self);
 G_GNUC_INTERNAL void gala_root_set_focus_visible (GalaRoot* self,
                                  gboolean value);
static gboolean __lambda29_ (GalaRoot* self);
static gboolean ___lambda29__gsource_func (gpointer self);
 G_GNUC_INTERNAL gboolean gala_root_get_focus_visible (GalaRoot* self);
 G_GNUC_INTERNAL void gala_widget_focus_changed (GalaWidget* self);
static void gala_root_finalize (GObject * obj);
static GType gala_root_get_type_once (void);
static void _vala_gala_root_get_property (GObject * object,
                                   guint property_id,
                                   GValue * value,
                                   GParamSpec * pspec);
static void _vala_gala_root_set_property (GObject * object,
                                   guint property_id,
                                   const GValue * value,
                                   GParamSpec * pspec);

static inline gpointer
gala_root_get_instance_private (GalaRoot* self)
{
	return G_STRUCT_MEMBER_P (self, GalaRoot_private_offset);
}

static gboolean
_gala_root_check_focus_clutter_actor_key_press_event (ClutterActor* _sender,
                                                      ClutterEvent* event,
                                                      gpointer self)
{
	gboolean result;
	result = gala_root_check_focus ((GalaRoot*) self, event);
	return result;
}

static void
gala_root_real_map (ClutterActor* base)
{
	GalaRoot * self;
	ClutterStage* _tmp0_;
	self = (GalaRoot*) base;
	CLUTTER_ACTOR_CLASS (gala_root_parent_class)->map ((ClutterActor*) G_TYPE_CHECK_INSTANCE_CAST (self, GALA_TYPE_WIDGET, GalaWidget));
	_tmp0_ = (ClutterStage*) clutter_actor_get_stage ((ClutterActor*) self);
	g_signal_connect_object ((ClutterActor*) _tmp0_, "key-press-event", (GCallback) _gala_root_check_focus_clutter_actor_key_press_event, self, 0);
}

static void
gala_root_real_unmap (ClutterActor* base)
{
	GalaRoot * self;
	ClutterStage* _tmp0_;
	guint _tmp1_;
	self = (GalaRoot*) base;
	CLUTTER_ACTOR_CLASS (gala_root_parent_class)->unmap ((ClutterActor*) G_TYPE_CHECK_INSTANCE_CAST (self, GALA_TYPE_WIDGET, GalaWidget));
	_tmp0_ = (ClutterStage*) clutter_actor_get_stage ((ClutterActor*) self);
	g_signal_parse_name ("key-press-event", clutter_actor_get_type (), &_tmp1_, NULL, FALSE);
	g_signal_handlers_disconnect_matched ((ClutterActor*) _tmp0_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp1_, 0, NULL, (GCallback) _gala_root_check_focus_clutter_actor_key_press_event, self);
}

static gboolean
gala_root_real_key_press_event (ClutterActor* base,
                                ClutterEvent* event)
{
	GalaRoot * self;
	gboolean result;
	self = (GalaRoot*) base;
	g_return_val_if_fail (event != NULL, FALSE);
	result = gala_root_check_focus (self, event);
	return result;
}

static gboolean
gala_root_check_focus (GalaRoot* self,
                       ClutterEvent* event)
{
	GalaFocusDirection* direction = NULL;
	GalaFocusDirection* _tmp0_;
	GalaFocusDirection* _tmp1_;
	GalaFocusDirection* _tmp2_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (event != NULL, FALSE);
	_tmp0_ = gala_focus_direction_get_for_event (event);
	direction = _tmp0_;
	_tmp1_ = direction;
	if (_tmp1_ == NULL) {
		result = CLUTTER_EVENT_PROPAGATE;
		_g_free0 (direction);
		return result;
	}
	_tmp2_ = direction;
	if (!gala_widget_focus ((GalaWidget*) self, *_tmp2_)) {
		ClutterBackend* _tmp3_;
		ClutterSeat* _tmp4_;
		ClutterStage* _tmp5_;
		ClutterActor* _tmp6_;
		ClutterActor* _tmp7_;
		_tmp3_ = clutter_get_default_backend ();
		_tmp4_ = clutter_backend_get_default_seat (_tmp3_);
		clutter_seat_bell_notify (_tmp4_);
		_tmp5_ = (ClutterStage*) clutter_actor_get_stage ((ClutterActor*) self);
		_tmp6_ = clutter_stage_get_key_focus (_tmp5_);
		_tmp7_ = _tmp6_;
		if (!clutter_actor_contains ((ClutterActor*) self, _tmp7_)) {
			ClutterStage* _tmp8_;
			_tmp8_ = (ClutterStage*) clutter_actor_get_stage ((ClutterActor*) self);
			clutter_stage_set_key_focus (_tmp8_, (ClutterActor*) self);
		}
	}
	gala_root_show_focus (self);
	result = CLUTTER_EVENT_STOP;
	_g_free0 (direction);
	return result;
}

static gboolean
__lambda29_ (GalaRoot* self)
{
	gboolean result;
	gala_root_set_focus_visible (self, FALSE);
	self->priv->timeout_id = (guint) 0;
	result = G_SOURCE_REMOVE;
	return result;
}

static gboolean
___lambda29__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda29_ ((GalaRoot*) self);
	return result;
}

static void
gala_root_show_focus (GalaRoot* self)
{
	g_return_if_fail (self != NULL);
	if (self->priv->timeout_id != ((guint) 0)) {
		g_source_remove (self->priv->timeout_id);
	} else {
		gala_root_set_focus_visible (self, TRUE);
	}
	self->priv->timeout_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, (guint) 5, ___lambda29__gsource_func, g_object_ref (self), g_object_unref);
}

GalaRoot*
gala_root_construct (GType object_type)
{
	GalaRoot * self = NULL;
	self = (GalaRoot*) gala_widget_construct (object_type);
	return self;
}

GalaRoot*
gala_root_new (void)
{
	return gala_root_construct (GALA_TYPE_ROOT);
}

 G_GNUC_INTERNAL gboolean
gala_root_get_focus_visible (GalaRoot* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_focus_visible;
	return result;
}

 G_GNUC_INTERNAL void
gala_root_set_focus_visible (GalaRoot* self,
                             gboolean value)
{
	GalaWidget* _tmp0_ = NULL;
	ClutterStage* _tmp1_;
	ClutterActor* _tmp2_;
	ClutterActor* _tmp3_;
	g_return_if_fail (self != NULL);
	self->priv->_focus_visible = value;
	_tmp1_ = (ClutterStage*) clutter_actor_get_stage ((ClutterActor*) self);
	_tmp2_ = clutter_stage_get_key_focus (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp0_ = GALA_IS_WIDGET (_tmp3_) ? ((GalaWidget*) _tmp3_) : NULL;
	if (_tmp0_ != NULL) {
		gala_widget_focus_changed (_tmp0_);
	}
	g_object_notify_by_pspec ((GObject *) self, gala_root_properties[GALA_ROOT_FOCUS_VISIBLE_PROPERTY]);
}

static void
gala_root_class_init (GalaRootClass * klass,
                      gpointer klass_data)
{
	gala_root_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GalaRoot_private_offset);
	((ClutterActorClass *) klass)->map = (void (*) (ClutterActor*)) gala_root_real_map;
	((ClutterActorClass *) klass)->unmap = (void (*) (ClutterActor*)) gala_root_real_unmap;
	((ClutterActorClass *) klass)->key_press_event = (gboolean (*) (ClutterActor*, ClutterEvent*)) gala_root_real_key_press_event;
	G_OBJECT_CLASS (klass)->get_property = _vala_gala_root_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_gala_root_set_property;
	G_OBJECT_CLASS (klass)->finalize = gala_root_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GALA_ROOT_FOCUS_VISIBLE_PROPERTY, gala_root_properties[GALA_ROOT_FOCUS_VISIBLE_PROPERTY] = g_param_spec_boolean ("focus-visible", "focus-visible", "focus-visible", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
gala_root_instance_init (GalaRoot * self,
                         gpointer klass)
{
	self->priv = gala_root_get_instance_private (self);
	self->priv->_focus_visible = FALSE;
	self->priv->timeout_id = (guint) 0;
}

static void
gala_root_finalize (GObject * obj)
{
	GalaRoot * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GALA_TYPE_ROOT, GalaRoot);
	G_OBJECT_CLASS (gala_root_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
gala_root_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GalaRootClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gala_root_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GalaRoot), 0, (GInstanceInitFunc) gala_root_instance_init, NULL };
	GType gala_root_type_id;
	gala_root_type_id = g_type_register_static (GALA_TYPE_WIDGET, "GalaRoot", &g_define_type_info, G_TYPE_FLAG_NONE);
	GalaRoot_private_offset = g_type_add_instance_private (gala_root_type_id, sizeof (GalaRootPrivate));
	return gala_root_type_id;
}

GType
gala_root_get_type (void)
{
	static gsize gala_root_type_id__once = 0;
	if (g_once_init_enter (&gala_root_type_id__once)) {
		GType gala_root_type_id;
		gala_root_type_id = gala_root_get_type_once ();
		g_once_init_leave (&gala_root_type_id__once, gala_root_type_id);
	}
	return gala_root_type_id__once;
}

static void
_vala_gala_root_get_property (GObject * object,
                              guint property_id,
                              GValue * value,
                              GParamSpec * pspec)
{
	GalaRoot * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GALA_TYPE_ROOT, GalaRoot);
	switch (property_id) {
		case GALA_ROOT_FOCUS_VISIBLE_PROPERTY:
		g_value_set_boolean (value, gala_root_get_focus_visible (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_gala_root_set_property (GObject * object,
                              guint property_id,
                              const GValue * value,
                              GParamSpec * pspec)
{
	GalaRoot * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GALA_TYPE_ROOT, GalaRoot);
	switch (property_id) {
		case GALA_ROOT_FOCUS_VISIBLE_PROPERTY:
		gala_root_set_focus_visible (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

