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

/*
 * Copyright 2022 elementary, Inc. (https://elementary.io)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
/* code adapted from libcanberra */

#include <canberra.h>
#include <glib.h>
#include "io.elementary.settings.sound.h"
#include <gdk/gdk.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gdk/x11/gdkx.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

typedef struct _Block3Data Block3Data;
#define _ca_proplist_destroy0(var) ((var == NULL) ? NULL : (var = (ca_proplist_destroy (var), NULL)))
#define _ca_context_destroy0(var) ((var == NULL) ? NULL : (var = (ca_context_destroy (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _Block3Data {
	int _ref_count_;
	GdkDisplay* display;
};

VALA_EXTERN ca_context* canberra_gtk4_context;
ca_context* canberra_gtk4_context = NULL;

static Block3Data* block3_data_ref (Block3Data* _data3_);
static void block3_data_unref (void * _userdata_);
static void __lambda22_ (Block3Data* _data3_,
                  const gchar* setting);
static void ___lambda22__gdk_display_setting_changed (GdkDisplay* _sender,
                                               const gchar* setting,
                                               gpointer self);

static Block3Data*
block3_data_ref (Block3Data* _data3_)
{
	g_atomic_int_inc (&_data3_->_ref_count_);
	return _data3_;
}

static void
block3_data_unref (void * _userdata_)
{
	Block3Data* _data3_;
	_data3_ = (Block3Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data3_->_ref_count_)) {
		g_slice_free (Block3Data, _data3_);
	}
}

static void
__lambda22_ (Block3Data* _data3_,
             const gchar* setting)
{
	GValue new_val = {0};
	g_return_if_fail (setting != NULL);
	if (g_strcmp0 (setting, "gtk-sound-theme-name") == 0) {
		GValue _tmp0_ = {0};
		GdkDisplay* _tmp1_;
		GValue _tmp2_;
		ca_context* _tmp3_;
		const gchar* _tmp4_;
		g_value_init (&_tmp0_, G_TYPE_STRING);
		G_IS_VALUE (&new_val) ? (g_value_unset (&new_val), NULL) : NULL;
		new_val = _tmp0_;
		_tmp1_ = _data3_->display;
		_tmp2_ = new_val;
		gdk_display_get_setting (_tmp1_, "gtk-sound-theme-name", &_tmp2_);
		_tmp3_ = canberra_gtk4_context;
		_tmp4_ = g_value_get_string (&new_val);
		ca_context_change_props (_tmp3_, CA_PROP_CANBERRA_ENABLE, _tmp4_, NULL);
	} else {
		if (g_strcmp0 (setting, "gtk-enable-event-sounds") == 0) {
			GValue _tmp5_ = {0};
			const gchar* env = NULL;
			const gchar* _tmp6_;
			GdkDisplay* _tmp7_;
			GValue _tmp8_;
			gboolean _tmp9_ = FALSE;
			const gchar* _tmp10_;
			ca_context* _tmp11_;
			g_value_init (&_tmp5_, G_TYPE_BOOLEAN);
			G_IS_VALUE (&new_val) ? (g_value_unset (&new_val), NULL) : NULL;
			new_val = _tmp5_;
			_tmp6_ = g_getenv ("CANBERRA_FORCE_EVENT_SOUNDS");
			env = _tmp6_;
			_tmp7_ = _data3_->display;
			_tmp8_ = new_val;
			gdk_display_get_setting (_tmp7_, "gtk-enable-event-sounds", &_tmp8_);
			_tmp10_ = env;
			if (_tmp10_ != NULL) {
				_tmp9_ = TRUE;
			} else {
				_tmp9_ = g_value_get_boolean (&new_val);
			}
			_tmp11_ = canberra_gtk4_context;
			ca_context_change_props (_tmp11_, CA_PROP_CANBERRA_ENABLE, _tmp9_, NULL);
		}
	}
	G_IS_VALUE (&new_val) ? (g_value_unset (&new_val), NULL) : NULL;
}

static void
___lambda22__gdk_display_setting_changed (GdkDisplay* _sender,
                                          const gchar* setting,
                                          gpointer self)
{
	__lambda22_ (self, setting);
}

ca_context*
canberra_gtk4_context_get (void)
{
	Block3Data* _data3_;
	ca_proplist* proplist = NULL;
	ca_context* _tmp0_;
	ca_context* _tmp2_ = NULL;
	gint _tmp3_;
	ca_proplist* _tmp4_ = NULL;
	gint _tmp5_;
	const gchar* name = NULL;
	const gchar* _tmp6_;
	const gchar* _tmp7_;
	const gchar* icon = NULL;
	const gchar* _tmp15_;
	const gchar* _tmp16_;
	GdkDisplay* _tmp19_;
	GdkDisplay* _tmp20_;
	ca_context* _tmp31_;
	ca_proplist* _tmp32_;
	GValue val = {0};
	GValue _tmp33_ = {0};
	GdkDisplay* _tmp34_;
	GValue _tmp35_;
	GValue _tmp38_ = {0};
	GdkDisplay* _tmp39_;
	GValue _tmp40_;
	GdkDisplay* _tmp45_;
	ca_context* _tmp46_;
	ca_context* result;
	_data3_ = g_slice_new0 (Block3Data);
	_data3_->_ref_count_ = 1;
	_tmp0_ = canberra_gtk4_context;
	if (_tmp0_ != NULL) {
		ca_context* _tmp1_;
		_tmp1_ = canberra_gtk4_context;
		result = _tmp1_;
		_ca_proplist_destroy0 (proplist);
		block3_data_unref (_data3_);
		_data3_ = NULL;
		return result;
	}
	_tmp3_ = ca_context_create (&_tmp2_);
	_ca_context_destroy0 (canberra_gtk4_context);
	canberra_gtk4_context = _tmp2_;
	if (_tmp3_ != CA_SUCCESS) {
		result = NULL;
		_ca_proplist_destroy0 (proplist);
		block3_data_unref (_data3_);
		_data3_ = NULL;
		return result;
	}
	_tmp5_ = ca_proplist_create (&_tmp4_);
	_ca_proplist_destroy0 (proplist);
	proplist = _tmp4_;
	if (_tmp5_ != CA_SUCCESS) {
		result = NULL;
		_ca_proplist_destroy0 (proplist);
		block3_data_unref (_data3_);
		_data3_ = NULL;
		return result;
	}
	_tmp6_ = g_get_application_name ();
	name = _tmp6_;
	_tmp7_ = name;
	if (_tmp7_ != NULL) {
		ca_proplist* _tmp8_;
		const gchar* _tmp9_;
		_tmp8_ = proplist;
		_tmp9_ = name;
		ca_proplist_sets (_tmp8_, CA_PROP_APPLICATION_NAME, _tmp9_);
	} else {
		ca_proplist* _tmp10_;
		ca_proplist* _tmp11_;
		gchar* _tmp12_;
		gchar* _tmp13_;
		ca_proplist* _tmp14_;
		_tmp10_ = proplist;
		ca_proplist_sets (_tmp10_, CA_PROP_APPLICATION_NAME, "libcanberra-gtk");
		_tmp11_ = proplist;
		_tmp12_ = g_strdup_printf ("%i.%i", CA_MAJOR, CA_MINOR);
		_tmp13_ = _tmp12_;
		ca_proplist_sets (_tmp11_, CA_PROP_APPLICATION_VERSION, _tmp13_);
		_g_free0 (_tmp13_);
		_tmp14_ = proplist;
		ca_proplist_sets (_tmp14_, CA_PROP_APPLICATION_ID, "org.freedesktop.libcanberra.gtk");
	}
	_tmp15_ = gtk_window_get_default_icon_name ();
	icon = _tmp15_;
	_tmp16_ = icon;
	if (_tmp16_ != NULL) {
		ca_proplist* _tmp17_;
		const gchar* _tmp18_;
		_tmp17_ = proplist;
		_tmp18_ = icon;
		ca_proplist_sets (_tmp17_, CA_PROP_APPLICATION_ICON_NAME, _tmp18_);
	}
	_tmp19_ = gdk_display_get_default ();
	_data3_->display = _tmp19_;
	_tmp20_ = _data3_->display;
	if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp20_, gdk_x11_display_get_type ())) {
		const gchar* display_name = NULL;
		GdkDisplay* _tmp21_;
		const gchar* _tmp22_;
		const gchar* _tmp23_;
		gchar* screen = NULL;
		GdkDisplay* _tmp26_;
		GdkX11Screen* _tmp27_;
		gchar* _tmp28_;
		ca_proplist* _tmp29_;
		const gchar* _tmp30_;
		_tmp21_ = _data3_->display;
		_tmp22_ = gdk_display_get_name (_tmp21_);
		display_name = _tmp22_;
		_tmp23_ = display_name;
		if (_tmp23_ != NULL) {
			ca_proplist* _tmp24_;
			const gchar* _tmp25_;
			_tmp24_ = proplist;
			_tmp25_ = display_name;
			ca_proplist_sets (_tmp24_, CA_PROP_WINDOW_X11_SCREEN, _tmp25_);
		}
		_tmp26_ = _data3_->display;
		_tmp27_ = gdk_x11_display_get_screen (G_TYPE_CHECK_INSTANCE_CAST (_tmp26_, gdk_x11_display_get_type (), GdkX11Display));
		_tmp28_ = g_strdup_printf ("%i", gdk_x11_screen_get_screen_number (_tmp27_));
		screen = _tmp28_;
		_tmp29_ = proplist;
		_tmp30_ = screen;
		ca_proplist_sets (_tmp29_, CA_PROP_WINDOW_X11_SCREEN, _tmp30_);
		_g_free0 (screen);
	}
	_tmp31_ = canberra_gtk4_context;
	_tmp32_ = proplist;
	ca_context_change_props_full (_tmp31_, _tmp32_);
	g_value_init (&_tmp33_, G_TYPE_STRING);
	val = _tmp33_;
	_tmp34_ = _data3_->display;
	_tmp35_ = val;
	if (gdk_display_get_setting (_tmp34_, "gtk-sound-theme-name", &_tmp35_)) {
		ca_context* _tmp36_;
		const gchar* _tmp37_;
		_tmp36_ = canberra_gtk4_context;
		_tmp37_ = g_value_get_string (&val);
		ca_context_change_props (_tmp36_, CA_PROP_CANBERRA_XDG_THEME_NAME, _tmp37_, NULL);
	}
	g_value_init (&_tmp38_, G_TYPE_BOOLEAN);
	G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
	val = _tmp38_;
	_tmp39_ = _data3_->display;
	_tmp40_ = val;
	if (gdk_display_get_setting (_tmp39_, "gtk-enable-event-sounds", &_tmp40_)) {
		const gchar* env = NULL;
		const gchar* _tmp41_;
		gboolean _tmp42_ = FALSE;
		const gchar* _tmp43_;
		ca_context* _tmp44_;
		_tmp41_ = g_getenv ("CANBERRA_FORCE_EVENT_SOUNDS");
		env = _tmp41_;
		_tmp43_ = env;
		if (_tmp43_ != NULL) {
			_tmp42_ = TRUE;
		} else {
			_tmp42_ = g_value_get_boolean (&val);
		}
		_tmp44_ = canberra_gtk4_context;
		ca_context_change_props (_tmp44_, CA_PROP_CANBERRA_ENABLE, _tmp42_, NULL);
	}
	_tmp45_ = _data3_->display;
	g_signal_connect_data (_tmp45_, "setting-changed", (GCallback) ___lambda22__gdk_display_setting_changed, block3_data_ref (_data3_), (GClosureNotify) block3_data_unref, 0);
	_tmp46_ = canberra_gtk4_context;
	result = _tmp46_;
	G_IS_VALUE (&val) ? (g_value_unset (&val), NULL) : NULL;
	_ca_proplist_destroy0 (proplist);
	block3_data_unref (_data3_);
	_data3_ = NULL;
	return result;
}

