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

/*
 * Copyright 2011-2021 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 2 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, write to the
 * Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "power.h"
#include <gtk/gtk.h>
#include <glib.h>
#include <gdk/gdk.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <float.h>
#include <math.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  {
	POWER_WIDGETS_SCREEN_BRIGHTNESS_0_PROPERTY,
	POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_TOUCHPAD_PROPERTY,
	POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_MOUSE_PROPERTY,
	POWER_WIDGETS_SCREEN_BRIGHTNESS_NUM_PROPERTIES
};
static GParamSpec* power_widgets_screen_brightness_properties[POWER_WIDGETS_SCREEN_BRIGHTNESS_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block4Data Block4Data;

struct _PowerWidgetsScreenBrightnessPrivate {
	GtkScale* brightness_slider;
	PowerServicesDeviceManager* dm;
	gboolean _natural_scroll_touchpad;
	gboolean _natural_scroll_mouse;
};

struct _Block4Data {
	int _ref_count_;
	PowerWidgetsScreenBrightness* self;
	GtkRevealer* show_brightness_slider;
};

static gint PowerWidgetsScreenBrightness_private_offset;
static gpointer power_widgets_screen_brightness_parent_class = NULL;

static gboolean power_widgets_screen_brightness_on_scroll_event (PowerWidgetsScreenBrightness* self,
                                                          GdkEventScroll* e);
static GObject * power_widgets_screen_brightness_constructor (GType type,
                                                       guint n_construct_properties,
                                                       GObjectConstructParam * construct_properties);
static Block4Data* block4_data_ref (Block4Data* _data4_);
static void block4_data_unref (void * _userdata_);
static gboolean _power_widgets_screen_brightness___lambda15_ (PowerWidgetsScreenBrightness* self,
                                                       GdkEventScroll* e);
static gboolean __power_widgets_screen_brightness___lambda15__gtk_widget_scroll_event (GtkWidget* _sender,
                                                                                GdkEventScroll* event,
                                                                                gpointer self);
static void _power_widgets_screen_brightness___lambda16_ (PowerWidgetsScreenBrightness* self,
                                                   GtkRange* value);
static void __power_widgets_screen_brightness___lambda16__gtk_range_value_changed (GtkRange* _sender,
                                                                            gpointer self);
static void _power_widgets_screen_brightness___lambda17_ (Block4Data* _data4_,
                                                   gint brightness);
static void __power_widgets_screen_brightness___lambda17__power_services_device_manager_brightness_changed (PowerServicesDeviceManager* _sender,
                                                                                                     gint brightness,
                                                                                                     gpointer self);
static void power_widgets_screen_brightness_finalize (GObject * obj);
static GType power_widgets_screen_brightness_get_type_once (void);
static void _vala_power_widgets_screen_brightness_get_property (GObject * object,
                                                         guint property_id,
                                                         GValue * value,
                                                         GParamSpec * pspec);
static void _vala_power_widgets_screen_brightness_set_property (GObject * object,
                                                         guint property_id,
                                                         const GValue * value,
                                                         GParamSpec * pspec);

static inline gpointer
power_widgets_screen_brightness_get_instance_private (PowerWidgetsScreenBrightness* self)
{
	return G_STRUCT_MEMBER_P (self, PowerWidgetsScreenBrightness_private_offset);
}

static gboolean
power_widgets_screen_brightness_on_scroll_event (PowerWidgetsScreenBrightness* self,
                                                 GdkEventScroll* e)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (e != NULL, FALSE);
	_tmp0_ = self->priv->_natural_scroll_mouse;
	_tmp1_ = self->priv->_natural_scroll_touchpad;
	result = power_utils_handle_scroll_event (e, _tmp0_, _tmp1_);
	return result;
}

PowerWidgetsScreenBrightness*
power_widgets_screen_brightness_construct (GType object_type)
{
	PowerWidgetsScreenBrightness * self = NULL;
	self = (PowerWidgetsScreenBrightness*) g_object_new (object_type, NULL);
	return self;
}

PowerWidgetsScreenBrightness*
power_widgets_screen_brightness_new (void)
{
	return power_widgets_screen_brightness_construct (POWER_WIDGETS_TYPE_SCREEN_BRIGHTNESS);
}

gboolean
power_widgets_screen_brightness_get_natural_scroll_touchpad (PowerWidgetsScreenBrightness* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_natural_scroll_touchpad;
	return result;
}

void
power_widgets_screen_brightness_set_natural_scroll_touchpad (PowerWidgetsScreenBrightness* self,
                                                             gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = power_widgets_screen_brightness_get_natural_scroll_touchpad (self);
	if (old_value != value) {
		self->priv->_natural_scroll_touchpad = value;
		g_object_notify_by_pspec ((GObject *) self, power_widgets_screen_brightness_properties[POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_TOUCHPAD_PROPERTY]);
	}
}

gboolean
power_widgets_screen_brightness_get_natural_scroll_mouse (PowerWidgetsScreenBrightness* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_natural_scroll_mouse;
	return result;
}

void
power_widgets_screen_brightness_set_natural_scroll_mouse (PowerWidgetsScreenBrightness* self,
                                                          gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = power_widgets_screen_brightness_get_natural_scroll_mouse (self);
	if (old_value != value) {
		self->priv->_natural_scroll_mouse = value;
		g_object_notify_by_pspec ((GObject *) self, power_widgets_screen_brightness_properties[POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_MOUSE_PROPERTY]);
	}
}

static Block4Data*
block4_data_ref (Block4Data* _data4_)
{
	g_atomic_int_inc (&_data4_->_ref_count_);
	return _data4_;
}

static void
block4_data_unref (void * _userdata_)
{
	Block4Data* _data4_;
	_data4_ = (Block4Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data4_->_ref_count_)) {
		PowerWidgetsScreenBrightness* self;
		self = _data4_->self;
		_g_object_unref0 (_data4_->show_brightness_slider);
		_g_object_unref0 (self);
		g_slice_free (Block4Data, _data4_);
	}
}

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

static gboolean
_power_widgets_screen_brightness___lambda15_ (PowerWidgetsScreenBrightness* self,
                                              GdkEventScroll* e)
{
	gboolean result;
	g_return_val_if_fail (e != NULL, FALSE);
	power_widgets_screen_brightness_on_scroll_event (self, e);
	result = GDK_EVENT_STOP;
	return result;
}

static gboolean
__power_widgets_screen_brightness___lambda15__gtk_widget_scroll_event (GtkWidget* _sender,
                                                                       GdkEventScroll* event,
                                                                       gpointer self)
{
	gboolean result;
	result = _power_widgets_screen_brightness___lambda15_ ((PowerWidgetsScreenBrightness*) self, event);
	return result;
}

static void
_power_widgets_screen_brightness___lambda16_ (PowerWidgetsScreenBrightness* self,
                                              GtkRange* value)
{
	GtkScale* _tmp0_;
	PowerServicesDeviceManager* _tmp1_;
	g_return_if_fail (value != NULL);
	_tmp0_ = self->priv->brightness_slider;
	gtk_range_set_value ((GtkRange*) _tmp0_, gtk_range_get_value (value));
	_tmp1_ = self->priv->dm;
	power_services_device_manager_set_brightness (_tmp1_, (gint) gtk_range_get_value (value));
}

static void
__power_widgets_screen_brightness___lambda16__gtk_range_value_changed (GtkRange* _sender,
                                                                       gpointer self)
{
	_power_widgets_screen_brightness___lambda16_ ((PowerWidgetsScreenBrightness*) self, _sender);
}

static void
_power_widgets_screen_brightness___lambda17_ (Block4Data* _data4_,
                                              gint brightness)
{
	PowerWidgetsScreenBrightness* self;
	self = _data4_->self;
	if (brightness != -1) {
		GtkScale* _tmp0_;
		GtkRevealer* _tmp1_;
		_tmp0_ = self->priv->brightness_slider;
		gtk_range_set_value ((GtkRange*) _tmp0_, (gdouble) brightness);
		_tmp1_ = _data4_->show_brightness_slider;
		gtk_revealer_set_reveal_child (_tmp1_, TRUE);
	} else {
		GtkRevealer* _tmp2_;
		_tmp2_ = _data4_->show_brightness_slider;
		gtk_revealer_set_reveal_child (_tmp2_, FALSE);
	}
}

static void
__power_widgets_screen_brightness___lambda17__power_services_device_manager_brightness_changed (PowerServicesDeviceManager* _sender,
                                                                                                gint brightness,
                                                                                                gpointer self)
{
	_power_widgets_screen_brightness___lambda17_ (self, brightness);
}

static GObject *
power_widgets_screen_brightness_constructor (GType type,
                                             guint n_construct_properties,
                                             GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	PowerWidgetsScreenBrightness * self;
	Block4Data* _data4_;
	PowerServicesDeviceManager* _tmp0_;
	PowerServicesDeviceManager* _tmp1_;
	GSettings* mouse_settings = NULL;
	GSettings* _tmp2_;
	GSettings* _tmp3_;
	GSettings* touchpad_settings = NULL;
	GSettings* _tmp4_;
	GSettings* _tmp5_;
	GtkImage* image = NULL;
	GtkImage* _tmp6_ = NULL;
	GtkScale* _tmp7_ = NULL;
	GtkBox* box = NULL;
	GtkBox* _tmp8_ = NULL;
	GtkBox* _tmp9_;
	GtkImage* _tmp10_;
	GtkBox* _tmp11_;
	GtkScale* _tmp12_;
	GtkRevealer* _tmp13_;
	GtkRevealer* _tmp14_;
	GtkBox* _tmp15_;
	GtkRevealer* _tmp16_;
	PowerServicesDeviceManager* _tmp17_;
	gint _tmp18_;
	gint _tmp19_;
	GtkScale* _tmp25_;
	GtkScale* _tmp26_;
	PowerServicesDeviceManager* _tmp27_;
	parent_class = G_OBJECT_CLASS (power_widgets_screen_brightness_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, POWER_WIDGETS_TYPE_SCREEN_BRIGHTNESS, PowerWidgetsScreenBrightness);
	_data4_ = g_slice_new0 (Block4Data);
	_data4_->_ref_count_ = 1;
	_data4_->self = g_object_ref (self);
	_tmp0_ = power_services_device_manager_get_default ();
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->dm);
	self->priv->dm = _tmp1_;
	_tmp2_ = g_settings_new ("org.gnome.desktop.peripherals.mouse");
	mouse_settings = _tmp2_;
	_tmp3_ = mouse_settings;
	g_settings_bind (_tmp3_, "natural-scroll", (GObject*) self, "natural-scroll-mouse", G_SETTINGS_BIND_DEFAULT);
	_tmp4_ = g_settings_new ("org.gnome.desktop.peripherals.touchpad");
	touchpad_settings = _tmp4_;
	_tmp5_ = touchpad_settings;
	g_settings_bind (_tmp5_, "natural-scroll", (GObject*) self, "natural-scroll-touchpad", G_SETTINGS_BIND_DEFAULT);
	_tmp6_ = (GtkImage*) gtk_image_new_from_icon_name ("brightness-display-symbolic", (GtkIconSize) GTK_ICON_SIZE_DIALOG);
	gtk_widget_set_margin_start ((GtkWidget*) _tmp6_, 6);
	g_object_ref_sink (_tmp6_);
	image = _tmp6_;
	_tmp7_ = (GtkScale*) gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, (gdouble) 0, (gdouble) 100, (gdouble) 10);
	gtk_widget_set_margin_end ((GtkWidget*) _tmp7_, 12);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp7_, TRUE);
	gtk_scale_set_draw_value (_tmp7_, FALSE);
	g_object_set ((GtkWidget*) _tmp7_, "width-request", 175, NULL);
	g_object_ref_sink (_tmp7_);
	_g_object_unref0 (self->priv->brightness_slider);
	self->priv->brightness_slider = _tmp7_;
	_tmp8_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp8_, TRUE);
	gtk_widget_set_margin_start ((GtkWidget*) _tmp8_, 6);
	gtk_widget_set_margin_end ((GtkWidget*) _tmp8_, 12);
	g_object_ref_sink (_tmp8_);
	box = _tmp8_;
	_tmp9_ = box;
	_tmp10_ = image;
	gtk_container_add ((GtkContainer*) _tmp9_, (GtkWidget*) _tmp10_);
	_tmp11_ = box;
	_tmp12_ = self->priv->brightness_slider;
	gtk_container_add ((GtkContainer*) _tmp11_, (GtkWidget*) _tmp12_);
	_tmp13_ = (GtkRevealer*) gtk_revealer_new ();
	g_object_ref_sink (_tmp13_);
	_data4_->show_brightness_slider = _tmp13_;
	_tmp14_ = _data4_->show_brightness_slider;
	_tmp15_ = box;
	gtk_container_add ((GtkContainer*) _tmp14_, (GtkWidget*) _tmp15_);
	_tmp16_ = _data4_->show_brightness_slider;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp16_);
	_tmp17_ = self->priv->dm;
	_tmp18_ = power_services_device_manager_get_brightness (_tmp17_);
	_tmp19_ = _tmp18_;
	if (_tmp19_ != -1) {
		GtkScale* _tmp20_;
		PowerServicesDeviceManager* _tmp21_;
		gint _tmp22_;
		gint _tmp23_;
		GtkRevealer* _tmp24_;
		_tmp20_ = self->priv->brightness_slider;
		_tmp21_ = self->priv->dm;
		_tmp22_ = power_services_device_manager_get_brightness (_tmp21_);
		_tmp23_ = _tmp22_;
		gtk_range_set_value ((GtkRange*) _tmp20_, (gdouble) _tmp23_);
		_tmp24_ = _data4_->show_brightness_slider;
		gtk_revealer_set_reveal_child (_tmp24_, TRUE);
	}
	_tmp25_ = self->priv->brightness_slider;
	g_signal_connect_object ((GtkWidget*) _tmp25_, "scroll-event", (GCallback) __power_widgets_screen_brightness___lambda15__gtk_widget_scroll_event, self, 0);
	_tmp26_ = self->priv->brightness_slider;
	g_signal_connect_object ((GtkRange*) _tmp26_, "value-changed", (GCallback) __power_widgets_screen_brightness___lambda16__gtk_range_value_changed, self, 0);
	_tmp27_ = self->priv->dm;
	g_signal_connect_data (_tmp27_, "brightness-changed", (GCallback) __power_widgets_screen_brightness___lambda17__power_services_device_manager_brightness_changed, block4_data_ref (_data4_), (GClosureNotify) block4_data_unref, 0);
	_g_object_unref0 (box);
	_g_object_unref0 (image);
	_g_object_unref0 (touchpad_settings);
	_g_object_unref0 (mouse_settings);
	block4_data_unref (_data4_);
	_data4_ = NULL;
	return obj;
}

static void
power_widgets_screen_brightness_class_init (PowerWidgetsScreenBrightnessClass * klass,
                                            gpointer klass_data)
{
	power_widgets_screen_brightness_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PowerWidgetsScreenBrightness_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_power_widgets_screen_brightness_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_power_widgets_screen_brightness_set_property;
	G_OBJECT_CLASS (klass)->constructor = power_widgets_screen_brightness_constructor;
	G_OBJECT_CLASS (klass)->finalize = power_widgets_screen_brightness_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_TOUCHPAD_PROPERTY, power_widgets_screen_brightness_properties[POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_TOUCHPAD_PROPERTY] = g_param_spec_boolean ("natural-scroll-touchpad", "natural-scroll-touchpad", "natural-scroll-touchpad", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_MOUSE_PROPERTY, power_widgets_screen_brightness_properties[POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_MOUSE_PROPERTY] = g_param_spec_boolean ("natural-scroll-mouse", "natural-scroll-mouse", "natural-scroll-mouse", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
power_widgets_screen_brightness_instance_init (PowerWidgetsScreenBrightness * self,
                                               gpointer klass)
{
	self->priv = power_widgets_screen_brightness_get_instance_private (self);
}

static void
power_widgets_screen_brightness_finalize (GObject * obj)
{
	PowerWidgetsScreenBrightness * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, POWER_WIDGETS_TYPE_SCREEN_BRIGHTNESS, PowerWidgetsScreenBrightness);
	_g_object_unref0 (self->priv->brightness_slider);
	_g_object_unref0 (self->priv->dm);
	G_OBJECT_CLASS (power_widgets_screen_brightness_parent_class)->finalize (obj);
}

static GType
power_widgets_screen_brightness_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PowerWidgetsScreenBrightnessClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) power_widgets_screen_brightness_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PowerWidgetsScreenBrightness), 0, (GInstanceInitFunc) power_widgets_screen_brightness_instance_init, NULL };
	GType power_widgets_screen_brightness_type_id;
	power_widgets_screen_brightness_type_id = g_type_register_static (gtk_event_box_get_type (), "PowerWidgetsScreenBrightness", &g_define_type_info, 0);
	PowerWidgetsScreenBrightness_private_offset = g_type_add_instance_private (power_widgets_screen_brightness_type_id, sizeof (PowerWidgetsScreenBrightnessPrivate));
	return power_widgets_screen_brightness_type_id;
}

GType
power_widgets_screen_brightness_get_type (void)
{
	static volatile gsize power_widgets_screen_brightness_type_id__once = 0;
	if (g_once_init_enter (&power_widgets_screen_brightness_type_id__once)) {
		GType power_widgets_screen_brightness_type_id;
		power_widgets_screen_brightness_type_id = power_widgets_screen_brightness_get_type_once ();
		g_once_init_leave (&power_widgets_screen_brightness_type_id__once, power_widgets_screen_brightness_type_id);
	}
	return power_widgets_screen_brightness_type_id__once;
}

static void
_vala_power_widgets_screen_brightness_get_property (GObject * object,
                                                    guint property_id,
                                                    GValue * value,
                                                    GParamSpec * pspec)
{
	PowerWidgetsScreenBrightness * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, POWER_WIDGETS_TYPE_SCREEN_BRIGHTNESS, PowerWidgetsScreenBrightness);
	switch (property_id) {
		case POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_TOUCHPAD_PROPERTY:
		g_value_set_boolean (value, power_widgets_screen_brightness_get_natural_scroll_touchpad (self));
		break;
		case POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_MOUSE_PROPERTY:
		g_value_set_boolean (value, power_widgets_screen_brightness_get_natural_scroll_mouse (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_power_widgets_screen_brightness_set_property (GObject * object,
                                                    guint property_id,
                                                    const GValue * value,
                                                    GParamSpec * pspec)
{
	PowerWidgetsScreenBrightness * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, POWER_WIDGETS_TYPE_SCREEN_BRIGHTNESS, PowerWidgetsScreenBrightness);
	switch (property_id) {
		case POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_TOUCHPAD_PROPERTY:
		power_widgets_screen_brightness_set_natural_scroll_touchpad (self, g_value_get_boolean (value));
		break;
		case POWER_WIDGETS_SCREEN_BRIGHTNESS_NATURAL_SCROLL_MOUSE_PROPERTY:
		power_widgets_screen_brightness_set_natural_scroll_mouse (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

