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

#include <gtk/gtk.h>
#include <glib-object.h>
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <cairo-gobject.h>
#include <stdlib.h>
#include <string.h>
#include <gdk/gdk.h>
#include <math.h>
#include <float.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define GREETER_TYPE_BACKGROUND_IMAGE (greeter_background_image_get_type ())
#define GREETER_BACKGROUND_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GREETER_TYPE_BACKGROUND_IMAGE, GreeterBackgroundImage))
#define GREETER_BACKGROUND_IMAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GREETER_TYPE_BACKGROUND_IMAGE, GreeterBackgroundImageClass))
#define GREETER_IS_BACKGROUND_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GREETER_TYPE_BACKGROUND_IMAGE))
#define GREETER_IS_BACKGROUND_IMAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GREETER_TYPE_BACKGROUND_IMAGE))
#define GREETER_BACKGROUND_IMAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GREETER_TYPE_BACKGROUND_IMAGE, GreeterBackgroundImageClass))

typedef struct _GreeterBackgroundImage GreeterBackgroundImage;
typedef struct _GreeterBackgroundImageClass GreeterBackgroundImageClass;
typedef struct _GreeterBackgroundImagePrivate GreeterBackgroundImagePrivate;
enum  {
	GREETER_BACKGROUND_IMAGE_0_PROPERTY,
	GREETER_BACKGROUND_IMAGE_NUM_PROPERTIES
};
static GParamSpec* greeter_background_image_properties[GREETER_BACKGROUND_IMAGE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _GreeterBackgroundImage {
	GtkEventBox parent_instance;
	GreeterBackgroundImagePrivate * priv;
};

struct _GreeterBackgroundImageClass {
	GtkEventBoxClass parent_class;
};

struct _GreeterBackgroundImagePrivate {
	guint last_size_hash;
	GdkPixbuf* full_pixbuf;
	GdkPixbuf* fitting_pixbuf;
};

static gint GreeterBackgroundImage_private_offset;
static gpointer greeter_background_image_parent_class = NULL;

VALA_EXTERN GType greeter_background_image_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GreeterBackgroundImage, g_object_unref)
VALA_EXTERN GreeterBackgroundImage* greeter_background_image_new_from_path (const gchar* path);
VALA_EXTERN GreeterBackgroundImage* greeter_background_image_construct_from_path (GType object_type,
                                                                      const gchar* path);
VALA_EXTERN GreeterBackgroundImage* greeter_background_image_new_from_color (const gchar* color);
VALA_EXTERN GreeterBackgroundImage* greeter_background_image_construct_from_color (GType object_type,
                                                                       const gchar* color);
static gboolean greeter_background_image_real_draw (GtkWidget* base,
                                             cairo_t* cr);
VALA_EXTERN GreeterBackgroundImage* greeter_background_image_new (void);
VALA_EXTERN GreeterBackgroundImage* greeter_background_image_construct (GType object_type);
static GObject * greeter_background_image_constructor (GType type,
                                                guint n_construct_properties,
                                                GObjectConstructParam * construct_properties);
static void greeter_background_image_finalize (GObject * obj);
static GType greeter_background_image_get_type_once (void);

static inline gpointer
greeter_background_image_get_instance_private (GreeterBackgroundImage* self)
{
	return G_STRUCT_MEMBER_P (self, GreeterBackgroundImage_private_offset);
}

GreeterBackgroundImage*
greeter_background_image_construct_from_path (GType object_type,
                                              const gchar* path)
{
	GreeterBackgroundImage * self = NULL;
	GError* _inner_error0_ = NULL;
	self = (GreeterBackgroundImage*) g_object_new (object_type, NULL);
	if (path == NULL) {
		path = "/usr/share/backgrounds/elementaryos-default";
	}
	{
		GdkPixbuf* _tmp0_ = NULL;
		GdkPixbuf* _tmp1_;
		GdkPixbuf* _tmp2_;
		_tmp1_ = gdk_pixbuf_new_from_file (path, &_inner_error0_);
		_tmp0_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp2_ = _tmp0_;
		_tmp0_ = NULL;
		_g_object_unref0 (self->priv->full_pixbuf);
		self->priv->full_pixbuf = _tmp2_;
		_g_object_unref0 (_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp3_;
		const gchar* _tmp4_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp3_ = e;
		_tmp4_ = _tmp3_->message;
		g_critical ("BackgroundImage.vala:18: %s", _tmp4_);
		g_critical ("BackgroundImage.vala:19: Fallback to default wallpaper");
		{
			GdkPixbuf* _tmp5_ = NULL;
			GdkPixbuf* _tmp6_;
			GdkPixbuf* _tmp7_;
			_tmp6_ = gdk_pixbuf_new_from_file ("/usr/share/backgrounds/elementaryos-default", &_inner_error0_);
			_tmp5_ = _tmp6_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch1_g_error;
			}
			_tmp7_ = _tmp5_;
			_tmp5_ = NULL;
			_g_object_unref0 (self->priv->full_pixbuf);
			self->priv->full_pixbuf = _tmp7_;
			_g_object_unref0 (_tmp5_);
		}
		goto __finally1;
		__catch1_g_error:
		{
			GError* e = NULL;
			GError* _tmp8_;
			const gchar* _tmp9_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp8_ = e;
			_tmp9_ = _tmp8_->message;
			g_critical ("BackgroundImage.vala:24: %s", _tmp9_);
			_g_error_free0 (e);
		}
		__finally1:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_error_free0 (e);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	return self;
}

GreeterBackgroundImage*
greeter_background_image_new_from_path (const gchar* path)
{
	return greeter_background_image_construct_from_path (GREETER_TYPE_BACKGROUND_IMAGE, path);
}

GreeterBackgroundImage*
greeter_background_image_construct_from_color (GType object_type,
                                               const gchar* color)
{
	GreeterBackgroundImage * self = NULL;
	GdkPixbuf* _tmp0_;
	GdkRGBA rgba_color = {0};
	GdkRGBA _tmp1_ = {0};
	guint32 f = 0U;
	GdkRGBA _tmp2_;
	GdkRGBA _tmp3_;
	GdkRGBA _tmp4_;
	GdkPixbuf* _tmp5_;
	g_return_val_if_fail (color != NULL, NULL);
	self = (GreeterBackgroundImage*) g_object_new (object_type, NULL);
	_tmp0_ = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 1, 1);
	_g_object_unref0 (self->priv->full_pixbuf);
	self->priv->full_pixbuf = _tmp0_;
	rgba_color = _tmp1_;
	gdk_rgba_parse (&rgba_color, color);
	f = (guint32) 0x0;
	_tmp2_ = rgba_color;
	f += (guint32) ((guint) round (_tmp2_.red * 255));
	f <<= (guint32) 8;
	_tmp3_ = rgba_color;
	f += (guint32) ((guint) round (_tmp3_.green * 255));
	f <<= (guint32) 8;
	_tmp4_ = rgba_color;
	f += (guint32) ((guint) round (_tmp4_.blue * 255));
	f <<= (guint32) 8;
	f += (guint32) 255;
	_tmp5_ = self->priv->full_pixbuf;
	gdk_pixbuf_fill (_tmp5_, f);
	return self;
}

GreeterBackgroundImage*
greeter_background_image_new_from_color (const gchar* color)
{
	return greeter_background_image_construct_from_color (GREETER_TYPE_BACKGROUND_IMAGE, color);
}

static gboolean
greeter_background_image_real_draw (GtkWidget* base,
                                    cairo_t* cr)
{
	GreeterBackgroundImage * self;
	gint scale = 0;
	gint width = 0;
	gint height = 0;
	gint radius = 0;
	guint new_hash = 0U;
	GHashFunc _tmp0_;
	GHashFunc _tmp1_;
	GdkPixbuf* _tmp30_;
	gboolean result;
	self = (GreeterBackgroundImage*) base;
	g_return_val_if_fail (cr != NULL, FALSE);
	scale = gtk_widget_get_scale_factor ((GtkWidget*) self);
	width = gtk_widget_get_allocated_width ((GtkWidget*) self) * scale;
	height = gtk_widget_get_allocated_height ((GtkWidget*) self) * scale;
	radius = 5 * scale;
	_tmp0_ = g_int_hash;
	_tmp1_ = g_int_hash;
	new_hash = _tmp0_ (&width) + _tmp1_ (&height);
	if (new_hash != self->priv->last_size_hash) {
		gdouble full_ratio = 0.0;
		GdkPixbuf* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		GdkPixbuf* _tmp5_;
		gint _tmp6_;
		gint _tmp7_;
		GdkPixbuf* _tmp8_;
		GdkColorspace _tmp9_;
		GdkColorspace _tmp10_;
		GdkPixbuf* _tmp11_;
		gboolean _tmp12_;
		gboolean _tmp13_;
		GdkPixbuf* _tmp14_;
		gint _tmp15_;
		gint _tmp16_;
		GdkPixbuf* _tmp17_;
		GdkPixbuf* scaled_pixbuf = NULL;
		gint y = 0;
		GdkPixbuf* _tmp22_;
		gint _tmp23_;
		gint _tmp24_;
		gint x = 0;
		GdkPixbuf* _tmp25_;
		gint _tmp26_;
		gint _tmp27_;
		GdkPixbuf* _tmp28_;
		GdkPixbuf* _tmp29_;
		self->priv->last_size_hash = new_hash;
		_tmp2_ = self->priv->full_pixbuf;
		_tmp3_ = gdk_pixbuf_get_height (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp5_ = self->priv->full_pixbuf;
		_tmp6_ = gdk_pixbuf_get_width (_tmp5_);
		_tmp7_ = _tmp6_;
		full_ratio = ((gdouble) _tmp4_) / ((gdouble) _tmp7_);
		_tmp8_ = self->priv->full_pixbuf;
		_tmp9_ = gdk_pixbuf_get_colorspace (_tmp8_);
		_tmp10_ = _tmp9_;
		_tmp11_ = self->priv->full_pixbuf;
		_tmp12_ = gdk_pixbuf_get_has_alpha (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = self->priv->full_pixbuf;
		_tmp15_ = gdk_pixbuf_get_bits_per_sample (_tmp14_);
		_tmp16_ = _tmp15_;
		_tmp17_ = gdk_pixbuf_new (_tmp10_, _tmp13_, _tmp16_, width, height);
		_g_object_unref0 (self->priv->fitting_pixbuf);
		self->priv->fitting_pixbuf = _tmp17_;
		if (((gint) (width * full_ratio)) < height) {
			GdkPixbuf* _tmp18_;
			GdkPixbuf* _tmp19_;
			_tmp18_ = self->priv->full_pixbuf;
			_tmp19_ = gdk_pixbuf_scale_simple (_tmp18_, (gint) (width * (1 / full_ratio)), height, GDK_INTERP_BILINEAR);
			_g_object_unref0 (scaled_pixbuf);
			scaled_pixbuf = _tmp19_;
		} else {
			GdkPixbuf* _tmp20_;
			GdkPixbuf* _tmp21_;
			_tmp20_ = self->priv->full_pixbuf;
			_tmp21_ = gdk_pixbuf_scale_simple (_tmp20_, width, (gint) (width * full_ratio), GDK_INTERP_BILINEAR);
			_g_object_unref0 (scaled_pixbuf);
			scaled_pixbuf = _tmp21_;
		}
		_tmp22_ = scaled_pixbuf;
		_tmp23_ = gdk_pixbuf_get_height (_tmp22_);
		_tmp24_ = _tmp23_;
		y = abs ((height - _tmp24_) / 2);
		_tmp25_ = scaled_pixbuf;
		_tmp26_ = gdk_pixbuf_get_width (_tmp25_);
		_tmp27_ = _tmp26_;
		x = abs ((width - _tmp27_) / 2);
		_tmp28_ = scaled_pixbuf;
		_tmp29_ = self->priv->fitting_pixbuf;
		gdk_pixbuf_copy_area (_tmp28_, x, y, width, height, _tmp29_, 0, 0);
		_g_object_unref0 (scaled_pixbuf);
	}
	cairo_save (cr);
	cairo_scale (cr, 1.0 / scale, 1.0 / scale);
	cairo_new_sub_path (cr);
	cairo_arc (cr, (gdouble) (width - radius), (gdouble) radius, (gdouble) radius, -G_PI_2, (gdouble) 0);
	cairo_line_to (cr, (gdouble) width, (gdouble) height);
	cairo_line_to (cr, (gdouble) 0, (gdouble) height);
	cairo_arc (cr, (gdouble) radius, (gdouble) radius, (gdouble) radius, G_PI, G_PI + G_PI_2);
	cairo_close_path (cr);
	_tmp30_ = self->priv->fitting_pixbuf;
	gdk_cairo_set_source_pixbuf (cr, _tmp30_, (gdouble) 0, (gdouble) 0);
	cairo_clip (cr);
	cairo_paint (cr);
	cairo_restore (cr);
	result = TRUE;
	return result;
}

GreeterBackgroundImage*
greeter_background_image_construct (GType object_type)
{
	GreeterBackgroundImage * self = NULL;
	self = (GreeterBackgroundImage*) g_object_new (object_type, NULL);
	return self;
}

GreeterBackgroundImage*
greeter_background_image_new (void)
{
	return greeter_background_image_construct (GREETER_TYPE_BACKGROUND_IMAGE);
}

static GObject *
greeter_background_image_constructor (GType type,
                                      guint n_construct_properties,
                                      GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GreeterBackgroundImage * self;
	parent_class = G_OBJECT_CLASS (greeter_background_image_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GREETER_TYPE_BACKGROUND_IMAGE, GreeterBackgroundImage);
	g_object_set ((GtkWidget*) self, "height-request", 150, NULL);
	return obj;
}

static void
greeter_background_image_class_init (GreeterBackgroundImageClass * klass,
                                     gpointer klass_data)
{
	greeter_background_image_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GreeterBackgroundImage_private_offset);
	((GtkWidgetClass *) klass)->draw = (gboolean (*) (GtkWidget*, cairo_t*)) greeter_background_image_real_draw;
	G_OBJECT_CLASS (klass)->constructor = greeter_background_image_constructor;
	G_OBJECT_CLASS (klass)->finalize = greeter_background_image_finalize;
}

static void
greeter_background_image_instance_init (GreeterBackgroundImage * self,
                                        gpointer klass)
{
	self->priv = greeter_background_image_get_instance_private (self);
	self->priv->last_size_hash = (guint) 0;
}

static void
greeter_background_image_finalize (GObject * obj)
{
	GreeterBackgroundImage * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GREETER_TYPE_BACKGROUND_IMAGE, GreeterBackgroundImage);
	_g_object_unref0 (self->priv->full_pixbuf);
	_g_object_unref0 (self->priv->fitting_pixbuf);
	G_OBJECT_CLASS (greeter_background_image_parent_class)->finalize (obj);
}

static GType
greeter_background_image_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GreeterBackgroundImageClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) greeter_background_image_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GreeterBackgroundImage), 0, (GInstanceInitFunc) greeter_background_image_instance_init, NULL };
	GType greeter_background_image_type_id;
	greeter_background_image_type_id = g_type_register_static (gtk_event_box_get_type (), "GreeterBackgroundImage", &g_define_type_info, 0);
	GreeterBackgroundImage_private_offset = g_type_add_instance_private (greeter_background_image_type_id, sizeof (GreeterBackgroundImagePrivate));
	return greeter_background_image_type_id;
}

GType
greeter_background_image_get_type (void)
{
	static volatile gsize greeter_background_image_type_id__once = 0;
	if (g_once_init_enter (&greeter_background_image_type_id__once)) {
		GType greeter_background_image_type_id;
		greeter_background_image_type_id = greeter_background_image_get_type_once ();
		g_once_init_leave (&greeter_background_image_type_id__once, greeter_background_image_type_id);
	}
	return greeter_background_image_type_id__once;
}

