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

/**/
/*  Copyright (C) 2020 elementary, Inc. (https://elementary.io)*/
/**/
/*  This program is free software: you can redistribute it and/or modify*/
/*  it under the terms of the GNU General Public License as published by*/
/*  the Free Software Foundation, either version 3 of the License, or*/
/*  (at your option) any later version.*/
/**/
/*  This program is distributed in the hope that it will be useful,*/
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of*/
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*/
/*  GNU General Public License for more details.*/
/**/
/*  You should have received a copy of the GNU General Public License*/
/*  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
/**/

#include <clutter/clutter.h>
#include <glib-object.h>
#include "gala.h"
#include <glib.h>
#include <gdk/gdk.h>
#include <meta/meta-plugin.h>
#include <meta/display.h>
#include <meta/common.h>
#include <float.h>
#include <math.h>
#include <string.h>

#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 GALA_TYPE_PIXEL_PICKER (gala_pixel_picker_get_type ())
#define GALA_PIXEL_PICKER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GALA_TYPE_PIXEL_PICKER, GalaPixelPicker))
#define GALA_PIXEL_PICKER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GALA_TYPE_PIXEL_PICKER, GalaPixelPickerClass))
#define GALA_IS_PIXEL_PICKER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GALA_TYPE_PIXEL_PICKER))
#define GALA_IS_PIXEL_PICKER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GALA_TYPE_PIXEL_PICKER))
#define GALA_PIXEL_PICKER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GALA_TYPE_PIXEL_PICKER, GalaPixelPickerClass))

typedef struct _GalaPixelPicker GalaPixelPicker;
typedef struct _GalaPixelPickerClass GalaPixelPickerClass;
typedef struct _GalaPixelPickerPrivate GalaPixelPickerPrivate;
enum  {
	GALA_PIXEL_PICKER_0_PROPERTY,
	GALA_PIXEL_PICKER_WM_PROPERTY,
	GALA_PIXEL_PICKER_CANCELLED_PROPERTY,
	GALA_PIXEL_PICKER_NUM_PROPERTIES
};
static GParamSpec* gala_pixel_picker_properties[GALA_PIXEL_PICKER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
enum  {
	GALA_PIXEL_PICKER_CLOSED_SIGNAL,
	GALA_PIXEL_PICKER_NUM_SIGNALS
};
static guint gala_pixel_picker_signals[GALA_PIXEL_PICKER_NUM_SIGNALS] = {0};

struct _GalaPixelPicker {
	ClutterActor parent_instance;
	GalaPixelPickerPrivate * priv;
};

struct _GalaPixelPickerClass {
	ClutterActorClass parent_class;
};

struct _GalaPixelPickerPrivate {
	GalaWindowManager* _wm;
	gboolean _cancelled;
	GalaModalProxy* modal_proxy;
	GdkPoint point;
};

static gint GalaPixelPicker_private_offset;
static gpointer gala_pixel_picker_parent_class = NULL;

VALA_EXTERN GType gala_pixel_picker_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GalaPixelPicker, g_object_unref)
VALA_EXTERN GalaPixelPicker* gala_pixel_picker_new (GalaWindowManager* wm);
VALA_EXTERN GalaPixelPicker* gala_pixel_picker_construct (GType object_type,
                                              GalaWindowManager* wm);
static gboolean gala_pixel_picker_real_key_press_event (ClutterActor* base,
                                                 ClutterKeyEvent* e);
VALA_EXTERN void gala_pixel_picker_close (GalaPixelPicker* self);
static void gala_pixel_picker_set_cancelled (GalaPixelPicker* self,
                                      gboolean value);
static gboolean gala_pixel_picker_real_button_release_event (ClutterActor* base,
                                                      ClutterButtonEvent* e);
VALA_EXTERN GalaWindowManager* gala_pixel_picker_get_wm (GalaPixelPicker* self);
VALA_EXTERN void gala_pixel_picker_start_selection (GalaPixelPicker* self);
VALA_EXTERN void gala_pixel_picker_get_point (GalaPixelPicker* self,
                                  gint* x,
                                  gint* y);
static void gala_pixel_picker_set_wm (GalaPixelPicker* self,
                               GalaWindowManager* value);
VALA_EXTERN gboolean gala_pixel_picker_get_cancelled (GalaPixelPicker* self);
static GObject * gala_pixel_picker_constructor (GType type,
                                         guint n_construct_properties,
                                         GObjectConstructParam * construct_properties);
static void gala_pixel_picker_finalize (GObject * obj);
static GType gala_pixel_picker_get_type_once (void);
static void _vala_gala_pixel_picker_get_property (GObject * object,
                                           guint property_id,
                                           GValue * value,
                                           GParamSpec * pspec);
static void _vala_gala_pixel_picker_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
gala_pixel_picker_get_instance_private (GalaPixelPicker* self)
{
	return G_STRUCT_MEMBER_P (self, GalaPixelPicker_private_offset);
}

GalaPixelPicker*
gala_pixel_picker_construct (GType object_type,
                             GalaWindowManager* wm)
{
	GalaPixelPicker * self = NULL;
	g_return_val_if_fail (wm != NULL, NULL);
	self = (GalaPixelPicker*) g_object_new (object_type, "wm", wm, NULL);
	return self;
}

GalaPixelPicker*
gala_pixel_picker_new (GalaWindowManager* wm)
{
	return gala_pixel_picker_construct (GALA_TYPE_PIXEL_PICKER, wm);
}

static gboolean
gala_pixel_picker_real_key_press_event (ClutterActor* base,
                                        ClutterKeyEvent* e)
{
	GalaPixelPicker * self;
	gboolean result;
	self = (GalaPixelPicker*) base;
	g_return_val_if_fail (e != NULL, FALSE);
	if (e->keyval == CLUTTER_KEY_Escape) {
		gala_pixel_picker_close (self);
		gala_pixel_picker_set_cancelled (self, TRUE);
		g_signal_emit (self, gala_pixel_picker_signals[GALA_PIXEL_PICKER_CLOSED_SIGNAL], 0);
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}

static gboolean
gala_pixel_picker_real_button_release_event (ClutterActor* base,
                                             ClutterButtonEvent* e)
{
	GalaPixelPicker * self;
	ClutterContent* _tmp0_;
	ClutterContent* _tmp1_;
	gboolean result;
	self = (GalaPixelPicker*) base;
	g_return_val_if_fail (e != NULL, FALSE);
	if (e->button != ((guint32) 1)) {
		result = TRUE;
		return result;
	}
	self->priv->point.x = (gint) e->x;
	self->priv->point.y = (gint) e->y;
	gala_pixel_picker_close (self);
	clutter_actor_hide ((ClutterActor*) self);
	_tmp0_ = clutter_actor_get_content ((ClutterActor*) self);
	_tmp1_ = _tmp0_;
	clutter_content_invalidate (_tmp1_);
	g_signal_emit (self, gala_pixel_picker_signals[GALA_PIXEL_PICKER_CLOSED_SIGNAL], 0);
	result = TRUE;
	return result;
}

void
gala_pixel_picker_close (GalaPixelPicker* self)
{
	GalaWindowManager* _tmp0_;
	MetaDisplay* _tmp1_;
	GalaModalProxy* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_wm;
	_tmp1_ = meta_plugin_get_display ((MetaPlugin*) _tmp0_);
	meta_display_set_cursor (_tmp1_, META_CURSOR_DEFAULT);
	_tmp2_ = self->priv->modal_proxy;
	if (_tmp2_ != NULL) {
		GalaWindowManager* _tmp3_;
		GalaModalProxy* _tmp4_;
		_tmp3_ = self->priv->_wm;
		_tmp4_ = self->priv->modal_proxy;
		gala_window_manager_pop_modal (_tmp3_, _tmp4_);
	}
}

void
gala_pixel_picker_start_selection (GalaPixelPicker* self)
{
	GalaWindowManager* _tmp0_;
	MetaDisplay* _tmp1_;
	GalaWindowManager* _tmp2_;
	GalaModalProxy* _tmp3_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_wm;
	_tmp1_ = meta_plugin_get_display ((MetaPlugin*) _tmp0_);
	meta_display_set_cursor (_tmp1_, META_CURSOR_CROSSHAIR);
	clutter_actor_grab_key_focus ((ClutterActor*) self);
	_tmp2_ = self->priv->_wm;
	_tmp3_ = gala_window_manager_push_modal (_tmp2_, (ClutterActor*) self);
	_g_object_unref0 (self->priv->modal_proxy);
	self->priv->modal_proxy = _tmp3_;
}

void
gala_pixel_picker_get_point (GalaPixelPicker* self,
                             gint* x,
                             gint* y)
{
	gint _vala_x = 0;
	gint _vala_y = 0;
	GdkPoint _tmp0_;
	GdkPoint _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->point;
	_vala_x = _tmp0_.x;
	_tmp1_ = self->priv->point;
	_vala_y = _tmp1_.y;
	if (x) {
		*x = _vala_x;
	}
	if (y) {
		*y = _vala_y;
	}
}

GalaWindowManager*
gala_pixel_picker_get_wm (GalaPixelPicker* self)
{
	GalaWindowManager* result;
	GalaWindowManager* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_wm;
	result = _tmp0_;
	return result;
}

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

static void
gala_pixel_picker_set_wm (GalaPixelPicker* self,
                          GalaWindowManager* value)
{
	GalaWindowManager* old_value;
	g_return_if_fail (self != NULL);
	old_value = gala_pixel_picker_get_wm (self);
	if (old_value != value) {
		GalaWindowManager* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_wm);
		self->priv->_wm = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, gala_pixel_picker_properties[GALA_PIXEL_PICKER_WM_PROPERTY]);
	}
}

gboolean
gala_pixel_picker_get_cancelled (GalaPixelPicker* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_cancelled;
	return result;
}

static void
gala_pixel_picker_set_cancelled (GalaPixelPicker* self,
                                 gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = gala_pixel_picker_get_cancelled (self);
	if (old_value != value) {
		self->priv->_cancelled = value;
		g_object_notify_by_pspec ((GObject *) self, gala_pixel_picker_properties[GALA_PIXEL_PICKER_CANCELLED_PROPERTY]);
	}
}

static GObject *
gala_pixel_picker_constructor (GType type,
                               guint n_construct_properties,
                               GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GalaPixelPicker * self;
	GdkPoint _tmp0_ = {0};
	gint screen_width = 0;
	gint screen_height = 0;
	GalaWindowManager* _tmp1_;
	MetaDisplay* _tmp2_;
	gint _tmp3_ = 0;
	gint _tmp4_ = 0;
	ClutterCanvas* canvas = NULL;
	ClutterCanvas* _tmp5_;
	ClutterCanvas* _tmp6_;
	ClutterCanvas* _tmp7_;
	parent_class = G_OBJECT_CLASS (gala_pixel_picker_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GALA_TYPE_PIXEL_PICKER, GalaPixelPicker);
	_tmp0_.x = 0;
	_tmp0_.y = 0;
	self->priv->point = _tmp0_;
	g_object_set ((ClutterActor*) self, "visible", TRUE, NULL);
	clutter_actor_set_reactive ((ClutterActor*) self, TRUE);
	_tmp1_ = self->priv->_wm;
	_tmp2_ = meta_plugin_get_display ((MetaPlugin*) _tmp1_);
	meta_display_get_size (_tmp2_, &_tmp3_, &_tmp4_);
	screen_width = _tmp3_;
	screen_height = _tmp4_;
	clutter_actor_set_width ((ClutterActor*) self, (gfloat) screen_width);
	clutter_actor_set_height ((ClutterActor*) self, (gfloat) screen_height);
	_tmp5_ = (ClutterCanvas*) clutter_canvas_new ();
	canvas = _tmp5_;
	_tmp6_ = canvas;
	clutter_canvas_set_size (_tmp6_, screen_width, screen_height);
	_tmp7_ = canvas;
	clutter_actor_set_content ((ClutterActor*) self, (ClutterContent*) _tmp7_);
	_g_object_unref0 (canvas);
	return obj;
}

static void
gala_pixel_picker_class_init (GalaPixelPickerClass * klass,
                              gpointer klass_data)
{
	gala_pixel_picker_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GalaPixelPicker_private_offset);
	((ClutterActorClass *) klass)->key_press_event = (gboolean (*) (ClutterActor*, ClutterKeyEvent*)) gala_pixel_picker_real_key_press_event;
	((ClutterActorClass *) klass)->button_release_event = (gboolean (*) (ClutterActor*, ClutterButtonEvent*)) gala_pixel_picker_real_button_release_event;
	G_OBJECT_CLASS (klass)->get_property = _vala_gala_pixel_picker_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_gala_pixel_picker_set_property;
	G_OBJECT_CLASS (klass)->constructor = gala_pixel_picker_constructor;
	G_OBJECT_CLASS (klass)->finalize = gala_pixel_picker_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GALA_PIXEL_PICKER_WM_PROPERTY, gala_pixel_picker_properties[GALA_PIXEL_PICKER_WM_PROPERTY] = g_param_spec_object ("wm", "wm", "wm", GALA_TYPE_WINDOW_MANAGER, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), GALA_PIXEL_PICKER_CANCELLED_PROPERTY, gala_pixel_picker_properties[GALA_PIXEL_PICKER_CANCELLED_PROPERTY] = g_param_spec_boolean ("cancelled", "cancelled", "cancelled", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	gala_pixel_picker_signals[GALA_PIXEL_PICKER_CLOSED_SIGNAL] = g_signal_new ("closed", GALA_TYPE_PIXEL_PICKER, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
gala_pixel_picker_instance_init (GalaPixelPicker * self,
                                 gpointer klass)
{
	self->priv = gala_pixel_picker_get_instance_private (self);
}

static void
gala_pixel_picker_finalize (GObject * obj)
{
	GalaPixelPicker * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GALA_TYPE_PIXEL_PICKER, GalaPixelPicker);
	_g_object_unref0 (self->priv->_wm);
	_g_object_unref0 (self->priv->modal_proxy);
	G_OBJECT_CLASS (gala_pixel_picker_parent_class)->finalize (obj);
}

static GType
gala_pixel_picker_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GalaPixelPickerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gala_pixel_picker_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GalaPixelPicker), 0, (GInstanceInitFunc) gala_pixel_picker_instance_init, NULL };
	GType gala_pixel_picker_type_id;
	gala_pixel_picker_type_id = g_type_register_static (clutter_actor_get_type (), "GalaPixelPicker", &g_define_type_info, 0);
	GalaPixelPicker_private_offset = g_type_add_instance_private (gala_pixel_picker_type_id, sizeof (GalaPixelPickerPrivate));
	return gala_pixel_picker_type_id;
}

GType
gala_pixel_picker_get_type (void)
{
	static volatile gsize gala_pixel_picker_type_id__once = 0;
	if (g_once_init_enter (&gala_pixel_picker_type_id__once)) {
		GType gala_pixel_picker_type_id;
		gala_pixel_picker_type_id = gala_pixel_picker_get_type_once ();
		g_once_init_leave (&gala_pixel_picker_type_id__once, gala_pixel_picker_type_id);
	}
	return gala_pixel_picker_type_id__once;
}

static void
_vala_gala_pixel_picker_get_property (GObject * object,
                                      guint property_id,
                                      GValue * value,
                                      GParamSpec * pspec)
{
	GalaPixelPicker * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GALA_TYPE_PIXEL_PICKER, GalaPixelPicker);
	switch (property_id) {
		case GALA_PIXEL_PICKER_WM_PROPERTY:
		g_value_set_object (value, gala_pixel_picker_get_wm (self));
		break;
		case GALA_PIXEL_PICKER_CANCELLED_PROPERTY:
		g_value_set_boolean (value, gala_pixel_picker_get_cancelled (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_gala_pixel_picker_set_property (GObject * object,
                                      guint property_id,
                                      const GValue * value,
                                      GParamSpec * pspec)
{
	GalaPixelPicker * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GALA_TYPE_PIXEL_PICKER, GalaPixelPicker);
	switch (property_id) {
		case GALA_PIXEL_PICKER_WM_PROPERTY:
		gala_pixel_picker_set_wm (self, g_value_get_object (value));
		break;
		case GALA_PIXEL_PICKER_CANCELLED_PROPERTY:
		gala_pixel_picker_set_cancelled (self, g_value_get_boolean (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;
}

