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

/*
 * Copyright (c) 2011-2015 elementary LLC. (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 <glib.h>
#include <stdlib.h>
#include <string.h>
#include <libudev.h>
#include <glib-object.h>

#define POWER_SERVICES_BACKLIGHT_BACKLIGHT_NAME "backlight"
#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_SERVICES_BACKLIGHT_0_PROPERTY,
	POWER_SERVICES_BACKLIGHT_PRESENT_PROPERTY,
	POWER_SERVICES_BACKLIGHT_NUM_PROPERTIES
};
static GParamSpec* power_services_backlight_properties[POWER_SERVICES_BACKLIGHT_NUM_PROPERTIES];
#define _udev_device_unref0(var) ((var == NULL) ? NULL : (var = (udev_device_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _udev_enumerate_unref0(var) ((var == NULL) ? NULL : (var = (udev_enumerate_unref (var), NULL)))
#define _udev_unref0(var) ((var == NULL) ? NULL : (var = (udev_unref (var), NULL)))

struct _PowerServicesBacklightPrivate {
	gboolean _present;
};

static gint PowerServicesBacklight_private_offset;
static gpointer power_services_backlight_parent_class = NULL;

static gboolean power_services_backlight_get_backlight_present (void);
static GObject * power_services_backlight_constructor (GType type,
                                                guint n_construct_properties,
                                                GObjectConstructParam * construct_properties);
static void power_services_backlight_finalize (GObject * obj);
static GType power_services_backlight_get_type_once (void);
static void _vala_power_services_backlight_get_property (GObject * object,
                                                  guint property_id,
                                                  GValue * value,
                                                  GParamSpec * pspec);
static void _vala_power_services_backlight_set_property (GObject * object,
                                                  guint property_id,
                                                  const GValue * value,
                                                  GParamSpec * pspec);

static inline gpointer
power_services_backlight_get_instance_private (PowerServicesBacklight* self)
{
	return G_STRUCT_MEMBER_P (self, PowerServicesBacklight_private_offset);
}

static gboolean
power_services_backlight_get_backlight_present (void)
{
	struct udev* context = NULL;
	struct udev* _tmp0_;
	struct udev_enumerate* e = NULL;
	struct udev* _tmp1_;
	struct udev_enumerate* _tmp2_;
	struct udev_enumerate* _tmp3_;
	struct udev_enumerate* _tmp4_;
	gboolean result;
	_tmp0_ = udev_new ();
	context = _tmp0_;
	_tmp1_ = context;
	_tmp2_ = udev_enumerate_new (_tmp1_);
	e = _tmp2_;
	_tmp3_ = e;
	udev_enumerate_add_match_subsystem (_tmp3_, POWER_SERVICES_BACKLIGHT_BACKLIGHT_NAME);
	_tmp4_ = e;
	udev_enumerate_scan_devices (_tmp4_);
	{
		struct udev_list_entry* d = NULL;
		struct udev_enumerate* _tmp5_;
		struct udev_list_entry* _tmp6_;
		struct udev_list_entry* _tmp7_;
		_tmp5_ = e;
		_tmp6_ = udev_enumerate_get_list_entry (_tmp5_);
		_tmp7_ = _tmp6_;
		d = _tmp7_;
		{
			gboolean _tmp8_ = FALSE;
			_tmp8_ = TRUE;
			while (TRUE) {
				struct udev_list_entry* _tmp12_;
				gchar* path = NULL;
				struct udev_list_entry* _tmp13_;
				const gchar* _tmp14_;
				const gchar* _tmp15_;
				gchar* _tmp16_;
				struct udev_device* dev = NULL;
				struct udev* _tmp17_;
				const gchar* _tmp18_;
				struct udev_device* _tmp19_;
				struct udev_device* _tmp20_;
				if (!_tmp8_) {
					struct udev_list_entry* _tmp9_;
					struct udev_list_entry* _tmp10_;
					struct udev_list_entry* _tmp11_;
					_tmp9_ = d;
					_tmp10_ = udev_list_entry_get_next (_tmp9_);
					_tmp11_ = _tmp10_;
					d = _tmp11_;
				}
				_tmp8_ = FALSE;
				_tmp12_ = d;
				if (!(_tmp12_ != NULL)) {
					break;
				}
				_tmp13_ = d;
				_tmp14_ = udev_list_entry_get_name (_tmp13_);
				_tmp15_ = _tmp14_;
				_tmp16_ = g_strdup (_tmp15_);
				path = _tmp16_;
				_tmp17_ = context;
				_tmp18_ = path;
				_tmp19_ = udev_device_new_from_syspath (_tmp17_, _tmp18_);
				dev = _tmp19_;
				_tmp20_ = dev;
				if (_tmp20_ != NULL) {
					result = TRUE;
					_udev_device_unref0 (dev);
					_g_free0 (path);
					_udev_enumerate_unref0 (e);
					_udev_unref0 (context);
					return result;
				}
				_udev_device_unref0 (dev);
				_g_free0 (path);
			}
		}
	}
	result = FALSE;
	_udev_enumerate_unref0 (e);
	_udev_unref0 (context);
	return result;
}

PowerServicesBacklight*
power_services_backlight_construct (GType object_type)
{
	PowerServicesBacklight * self = NULL;
	self = (PowerServicesBacklight*) g_object_new (object_type, NULL);
	return self;
}

PowerServicesBacklight*
power_services_backlight_new (void)
{
	return power_services_backlight_construct (POWER_SERVICES_TYPE_BACKLIGHT);
}

gboolean
power_services_backlight_get_present (PowerServicesBacklight* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_present;
	return result;
}

void
power_services_backlight_set_present (PowerServicesBacklight* self,
                                      gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = power_services_backlight_get_present (self);
	if (old_value != value) {
		self->priv->_present = value;
		g_object_notify_by_pspec ((GObject *) self, power_services_backlight_properties[POWER_SERVICES_BACKLIGHT_PRESENT_PROPERTY]);
	}
}

static gchar*
bool_to_string (gboolean self)
{
	gchar* result;
	if (self) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup ("true");
		result = _tmp0_;
		return result;
	} else {
		gchar* _tmp1_;
		_tmp1_ = g_strdup ("false");
		result = _tmp1_;
		return result;
	}
}

static GObject *
power_services_backlight_constructor (GType type,
                                      guint n_construct_properties,
                                      GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	PowerServicesBacklight * self;
	gboolean _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	parent_class = G_OBJECT_CLASS (power_services_backlight_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, POWER_SERVICES_TYPE_BACKLIGHT, PowerServicesBacklight);
	power_services_backlight_set_present (self, power_services_backlight_get_backlight_present ());
	_tmp0_ = self->priv->_present;
	_tmp1_ = bool_to_string (_tmp0_);
	_tmp2_ = _tmp1_;
	g_debug ("Backlight.vala:28: backlight present: %s", _tmp2_);
	_g_free0 (_tmp2_);
	return obj;
}

static void
power_services_backlight_class_init (PowerServicesBacklightClass * klass,
                                     gpointer klass_data)
{
	power_services_backlight_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PowerServicesBacklight_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_power_services_backlight_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_power_services_backlight_set_property;
	G_OBJECT_CLASS (klass)->constructor = power_services_backlight_constructor;
	G_OBJECT_CLASS (klass)->finalize = power_services_backlight_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), POWER_SERVICES_BACKLIGHT_PRESENT_PROPERTY, power_services_backlight_properties[POWER_SERVICES_BACKLIGHT_PRESENT_PROPERTY] = g_param_spec_boolean ("present", "present", "present", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
}

static void
power_services_backlight_instance_init (PowerServicesBacklight * self,
                                        gpointer klass)
{
	self->priv = power_services_backlight_get_instance_private (self);
}

static void
power_services_backlight_finalize (GObject * obj)
{
	PowerServicesBacklight * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, POWER_SERVICES_TYPE_BACKLIGHT, PowerServicesBacklight);
	G_OBJECT_CLASS (power_services_backlight_parent_class)->finalize (obj);
}

static GType
power_services_backlight_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PowerServicesBacklightClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) power_services_backlight_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PowerServicesBacklight), 0, (GInstanceInitFunc) power_services_backlight_instance_init, NULL };
	GType power_services_backlight_type_id;
	power_services_backlight_type_id = g_type_register_static (G_TYPE_OBJECT, "PowerServicesBacklight", &g_define_type_info, 0);
	PowerServicesBacklight_private_offset = g_type_add_instance_private (power_services_backlight_type_id, sizeof (PowerServicesBacklightPrivate));
	return power_services_backlight_type_id;
}

GType
power_services_backlight_get_type (void)
{
	static volatile gsize power_services_backlight_type_id__once = 0;
	if (g_once_init_enter (&power_services_backlight_type_id__once)) {
		GType power_services_backlight_type_id;
		power_services_backlight_type_id = power_services_backlight_get_type_once ();
		g_once_init_leave (&power_services_backlight_type_id__once, power_services_backlight_type_id);
	}
	return power_services_backlight_type_id__once;
}

static void
_vala_power_services_backlight_get_property (GObject * object,
                                             guint property_id,
                                             GValue * value,
                                             GParamSpec * pspec)
{
	PowerServicesBacklight * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, POWER_SERVICES_TYPE_BACKLIGHT, PowerServicesBacklight);
	switch (property_id) {
		case POWER_SERVICES_BACKLIGHT_PRESENT_PROPERTY:
		g_value_set_boolean (value, power_services_backlight_get_present (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_power_services_backlight_set_property (GObject * object,
                                             guint property_id,
                                             const GValue * value,
                                             GParamSpec * pspec)
{
	PowerServicesBacklight * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, POWER_SERVICES_TYPE_BACKLIGHT, PowerServicesBacklight);
	switch (property_id) {
		case POWER_SERVICES_BACKLIGHT_PRESENT_PROPERTY:
		power_services_backlight_set_present (self, g_value_get_boolean (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

