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

/*
 * Copyright (c) 2013, 2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "ft_core.h"
#include <float.h>
#include <math.h>
#include <glib/gi18n-lib.h>
#include <config.h>
#include <libpeas.h>
#include <gio/gio.h>
#include <glib-object.h>
#include <gobject/gvaluecollector.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

#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_thread_unref0(var) ((var == NULL) ? NULL : (var = (g_thread_unref (var), NULL)))
#define _g_ptr_array_unref0(var) ((var == NULL) ? NULL : (var = (g_ptr_array_unref (var), NULL)))
#define _ft_async_queue_unref0(var) ((var == NULL) ? NULL : (var = (ft_async_queue_unref (var), NULL)))
typedef struct _Block58Data Block58Data;
#define _g_source_unref0(var) ((var == NULL) ? NULL : (var = (g_source_unref (var), NULL)))
typedef struct _FtAsyncQueueWaitData FtAsyncQueueWaitData;
typedef struct _FtParamSpecAsyncQueue FtParamSpecAsyncQueue;

struct _FtAsyncQueuePrivate {
	GType t_type;
	GBoxedCopyFunc t_dup_func;
	GDestroyNotify t_destroy_func;
	GQueue* queue;
	GMutex mutex;
	GCond cond;
	gboolean in_dispose;
	GPtrArray* waiting_threads;
};

struct _Block58Data {
	int _ref_count_;
	FtAsyncQueue* self;
	GSourceFunc callback;
	gpointer callback_target;
	GDestroyNotify callback_target_destroy_notify;
	gpointer _async_data_;
};

struct _FtAsyncQueueWaitData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	FtAsyncQueue* self;
	Block58Data* _data58_;
	gboolean already_empty;
	GQueue* _tmp0_;
	GThread* thread;
	GThread* _tmp1_;
	GPtrArray* _tmp2_;
	GThread* _tmp3_;
	GThread* _tmp4_;
	GPtrArray* _tmp5_;
	GThread* _tmp6_;
};

struct _FtParamSpecAsyncQueue {
	GParamSpec parent_instance;
};

VALA_EXTERN gint ft__is_flatpak;
gint ft__is_flatpak = -1;
VALA_EXTERN gchar* ft__desktop_name;
gchar* ft__desktop_name = NULL;
static gint FtAsyncQueue_private_offset;
static gpointer ft_async_queue_parent_class = NULL;

VALA_EXTERN gboolean ft_is_test (void);
static void ft_async_callback_instance_init (FtAsyncCallback * self);
static void _g_thread_unref0_ (gpointer var);
static void ft_async_queue_wait_data_free (gpointer _data);
static gboolean ft_async_queue_wait_co (FtAsyncQueueWaitData* _data_);
static Block58Data* block58_data_ref (Block58Data* _data58_);
static void block58_data_unref (void * _userdata_);
static gboolean _ft_async_queue_wait_co_gsource_func (gpointer self);
static gboolean __lambda94_ (Block58Data* _data58_);
static gpointer ___lambda94__gthread_func (gpointer self);
static void ft_async_queue_finalize (FtAsyncQueue * obj);
static GType ft_async_queue_get_type_once (void);
static void _vala_clear_GMutex (GMutex * mutex);
static void _vala_clear_GRecMutex (GRecMutex * mutex);
static void _vala_clear_GRWLock (GRWLock * mutex);
static void _vala_clear_GCond (GCond * mutex);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

inline void
ft_ensure_timestamp (gint64* timestamp)
{
	if (ft_timestamp_is_undefined (*timestamp)) {
		*timestamp = ft_timestamp_from_now ();
	}
}

inline gchar*
ft_ensure_string (const gchar* str)
{
	const gchar* _tmp0_ = NULL;
	gchar* _tmp1_;
	gchar* result;
	if (str != NULL) {
		_tmp0_ = str;
	} else {
		_tmp0_ = "";
	}
	_tmp1_ = g_strdup (_tmp0_);
	result = _tmp1_;
	return result;
}

/**
     * Round seconds to 1s, 5s, 10s, 1m.
     *
     * Its intended for displaying rough estimation of duration.
     */
gdouble
ft_round_seconds (gdouble seconds)
{
	gdouble result;
	if (seconds < 10.0) {
		result = round (seconds);
		return result;
	}
	if (seconds < 30.0) {
		result = 5.0 * round (seconds / 5.0);
		return result;
	}
	if (seconds < 60.0) {
		result = 10.0 * round (seconds / 10.0);
		return result;
	}
	result = 60.0 * round (seconds / 60.0);
	return result;
}

/**
     * Convert seconds to text.
     *
     * If hours are present, seconds are omitted.
     */
gchar*
ft_format_time (guint seconds)
{
	guint hours = 0U;
	guint minutes = 0U;
	gchar* str = NULL;
	gchar* _tmp0_;
	gboolean _tmp9_ = FALSE;
	gboolean _tmp10_ = FALSE;
	gchar* result;
	hours = seconds / 3600;
	minutes = (seconds % 3600) / 60;
	_tmp0_ = g_strdup ("");
	str = _tmp0_;
	seconds = seconds % 60;
	if (hours > ((guint) 0)) {
		gchar* _tmp1_;
		_tmp1_ = g_strdup_printf (ngettext ("%u hour", "%u hours", (gulong) hours), hours);
		_g_free0 (str);
		str = _tmp1_;
	}
	if (minutes > ((guint) 0)) {
		const gchar* _tmp2_;
		const gchar* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		gchar* _tmp8_;
		_tmp2_ = str;
		if (g_strcmp0 (_tmp2_, "") != 0) {
			const gchar* _tmp3_;
			gchar* _tmp4_;
			_tmp3_ = str;
			_tmp4_ = g_strconcat (_tmp3_, " ", NULL);
			_g_free0 (str);
			str = _tmp4_;
		}
		_tmp5_ = str;
		_tmp6_ = g_strdup_printf (ngettext ("%u minute", "%u minutes", (gulong) minutes), minutes);
		_tmp7_ = _tmp6_;
		_tmp8_ = g_strconcat (_tmp5_, _tmp7_, NULL);
		_g_free0 (str);
		str = _tmp8_;
		_g_free0 (_tmp7_);
	}
	if (seconds > ((guint) 0)) {
		_tmp10_ = hours == ((guint) 0);
	} else {
		_tmp10_ = FALSE;
	}
	if (_tmp10_) {
		_tmp9_ = TRUE;
	} else {
		gboolean _tmp11_ = FALSE;
		gboolean _tmp12_ = FALSE;
		if (seconds == ((guint) 0)) {
			_tmp12_ = minutes == ((guint) 0);
		} else {
			_tmp12_ = FALSE;
		}
		if (_tmp12_) {
			_tmp11_ = hours == ((guint) 0);
		} else {
			_tmp11_ = FALSE;
		}
		_tmp9_ = _tmp11_;
	}
	if (_tmp9_) {
		const gchar* _tmp13_;
		const gchar* _tmp16_;
		gchar* _tmp17_;
		gchar* _tmp18_;
		gchar* _tmp19_;
		_tmp13_ = str;
		if (g_strcmp0 (_tmp13_, "") != 0) {
			const gchar* _tmp14_;
			gchar* _tmp15_;
			_tmp14_ = str;
			_tmp15_ = g_strconcat (_tmp14_, " ", NULL);
			_g_free0 (str);
			str = _tmp15_;
		}
		_tmp16_ = str;
		_tmp17_ = g_strdup_printf (ngettext ("%u second", "%u seconds", (gulong) seconds), seconds);
		_tmp18_ = _tmp17_;
		_tmp19_ = g_strconcat (_tmp16_, _tmp18_, NULL);
		_g_free0 (str);
		str = _tmp19_;
		_g_free0 (_tmp18_);
	}
	result = str;
	return result;
}

inline gdouble
ft_lerp (gdouble value_from,
         gdouble value_to,
         gdouble t)
{
	gdouble result;
	result = value_from + ((value_to - value_from) * t);
	return result;
}

inline gfloat
ft_lerpf (gfloat value_from,
          gfloat value_to,
          gfloat t)
{
	gfloat result;
	result = value_from + ((value_to - value_from) * t);
	return result;
}

gboolean
ft_is_flatpak (void)
{
	gboolean result;
	if (ft__is_flatpak < 0) {
		gboolean _tmp0_ = FALSE;
		const gchar* _tmp1_;
		gboolean value = FALSE;
		gint _tmp3_ = 0;
		_tmp1_ = g_getenv ("container");
		if (g_strcmp0 (_tmp1_, "flatpak") == 0) {
			const gchar* _tmp2_;
			_tmp2_ = g_getenv ("G_TEST_ROOT_PROCESS");
			_tmp0_ = _tmp2_ == NULL;
		} else {
			_tmp0_ = FALSE;
		}
		value = _tmp0_;
		if (value) {
			_tmp3_ = 1;
		} else {
			_tmp3_ = 0;
		}
		ft__is_flatpak = _tmp3_;
	}
	result = ft__is_flatpak > 0;
	return result;
}

gboolean
ft_is_test (void)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_;
	gboolean result;
	_tmp2_ = g_getenv ("G_TEST_ROOT_PROCESS");
	if (_tmp2_ != NULL) {
		_tmp1_ = TRUE;
	} else {
		const gchar* _tmp3_;
		_tmp3_ = g_getenv ("G_TEST_BUILDDIR");
		_tmp1_ = _tmp3_ != NULL;
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		const gchar* _tmp4_;
		_tmp4_ = g_getenv ("MESON_TEST_ITERATION");
		_tmp0_ = _tmp4_ != NULL;
	}
	result = _tmp0_;
	return result;
}

gboolean
ft_is_devel (void)
{
	gboolean result;
	result = g_str_has_suffix (APPLICATION_ID, ".Devel");
	return result;
}

static gboolean
string_contains (const gchar* self,
                 const gchar* needle)
{
	gchar* _tmp0_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (needle != NULL, FALSE);
	_tmp0_ = strstr ((gchar*) self, (gchar*) needle);
	result = _tmp0_ != NULL;
	return result;
}

gchar*
ft_get_desktop_name (void)
{
	const gchar* _tmp0_;
	const gchar* _tmp30_;
	gchar* _tmp31_;
	gchar* result;
	_tmp0_ = ft__desktop_name;
	if (_tmp0_ == NULL) {
		const gchar* _tmp1_ = NULL;
		const gchar* _tmp2_;
		gchar* desktop_name = NULL;
		gchar* _tmp3_;
		const gchar* _tmp4_;
		const gchar* _tmp8_;
		gchar* _tmp9_;
		const gchar* _tmp10_;
		const gchar* _tmp28_;
		gchar* _tmp29_;
		_tmp2_ = g_getenv ("XDG_SESSION_DESKTOP");
		_tmp1_ = _tmp2_;
		if (_tmp1_ == NULL) {
			_tmp1_ = "";
		}
		_tmp3_ = g_strdup (_tmp1_);
		desktop_name = _tmp3_;
		_tmp4_ = desktop_name;
		if (g_strcmp0 (_tmp4_, "") == 0) {
			const gchar* _tmp5_ = NULL;
			const gchar* _tmp6_;
			gchar* _tmp7_;
			_tmp6_ = g_getenv ("XDG_CURRENT_DESKTOP");
			_tmp5_ = _tmp6_;
			if (_tmp5_ == NULL) {
				_tmp5_ = "";
			}
			_tmp7_ = g_strdup (_tmp5_);
			_g_free0 (desktop_name);
			desktop_name = _tmp7_;
		}
		_tmp8_ = desktop_name;
		_tmp9_ = g_ascii_strdown (_tmp8_, (gssize) -1);
		_g_free0 (desktop_name);
		desktop_name = _tmp9_;
		_tmp10_ = desktop_name;
		if (string_contains (_tmp10_, "gnome")) {
			gchar* _tmp11_;
			_tmp11_ = g_strdup ("gnome");
			_g_free0 (desktop_name);
			desktop_name = _tmp11_;
		} else {
			gboolean _tmp12_ = FALSE;
			const gchar* _tmp13_;
			_tmp13_ = desktop_name;
			if (string_contains (_tmp13_, "kde")) {
				_tmp12_ = TRUE;
			} else {
				const gchar* _tmp14_;
				_tmp14_ = desktop_name;
				_tmp12_ = string_contains (_tmp14_, "plasma");
			}
			if (_tmp12_) {
				gchar* _tmp15_;
				_tmp15_ = g_strdup ("kde");
				_g_free0 (desktop_name);
				desktop_name = _tmp15_;
			} else {
				const gchar* _tmp16_;
				_tmp16_ = desktop_name;
				if (string_contains (_tmp16_, "xfce")) {
					gchar* _tmp17_;
					_tmp17_ = g_strdup ("xfce");
					_g_free0 (desktop_name);
					desktop_name = _tmp17_;
				} else {
					const gchar* _tmp18_;
					_tmp18_ = desktop_name;
					if (string_contains (_tmp18_, "budgie")) {
						gchar* _tmp19_;
						_tmp19_ = g_strdup ("budgie");
						_g_free0 (desktop_name);
						desktop_name = _tmp19_;
					} else {
						const gchar* _tmp20_;
						_tmp20_ = desktop_name;
						if (g_str_has_prefix (_tmp20_, "ubuntu")) {
							gchar* _tmp21_;
							_tmp21_ = g_strdup ("gnome");
							_g_free0 (desktop_name);
							desktop_name = _tmp21_;
						} else {
							const gchar* _tmp22_;
							_tmp22_ = desktop_name;
							if (g_str_has_prefix (_tmp22_, "kubuntu")) {
								gchar* _tmp23_;
								_tmp23_ = g_strdup ("kde");
								_g_free0 (desktop_name);
								desktop_name = _tmp23_;
							} else {
								const gchar* _tmp24_;
								_tmp24_ = desktop_name;
								if (g_str_has_prefix (_tmp24_, "xubuntu")) {
									gchar* _tmp25_;
									_tmp25_ = g_strdup ("xfce");
									_g_free0 (desktop_name);
									desktop_name = _tmp25_;
								} else {
									const gchar* _tmp26_;
									_tmp26_ = desktop_name;
									if (g_str_has_prefix (_tmp26_, "lubuntu")) {
										gchar* _tmp27_;
										_tmp27_ = g_strdup ("lxqt");
										_g_free0 (desktop_name);
										desktop_name = _tmp27_;
									}
								}
							}
						}
					}
				}
			}
		}
		_tmp28_ = desktop_name;
		_tmp29_ = g_strdup (_tmp28_);
		_g_free0 (ft__desktop_name);
		ft__desktop_name = _tmp29_;
		_g_free0 (desktop_name);
	}
	_tmp30_ = ft__desktop_name;
	_tmp31_ = g_strdup (_tmp30_);
	result = _tmp31_;
	return result;
}

static gboolean
string_get_next_char (const gchar* self,
                      gint* index,
                      gunichar* c)
{
	gunichar _vala_c = 0U;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_vala_c = g_utf8_get_char (((gchar*) self) + (*index));
	if (_vala_c != ((gunichar) 0)) {
		gchar* _tmp0_;
		_tmp0_ = g_utf8_next_char (((gchar*) self) + (*index));
		*index = (gint) (_tmp0_ - ((gchar*) self));
		result = TRUE;
		if (c) {
			*c = _vala_c;
		}
		return result;
	} else {
		result = FALSE;
		if (c) {
			*c = _vala_c;
		}
		return result;
	}
}

gchar*
ft_to_camel_case (const gchar* name)
{
	GString* _result_ = NULL;
	GString* _tmp0_;
	gboolean was_hyphen = FALSE;
	gunichar chr = 0U;
	gint chr_span_end = 0;
	GString* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* result;
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = g_string_new ("");
	_result_ = _tmp0_;
	was_hyphen = FALSE;
	chr_span_end = 0;
	while (TRUE) {
		gunichar _tmp1_ = 0U;
		gboolean _tmp2_;
		_tmp2_ = string_get_next_char (name, &chr_span_end, &_tmp1_);
		chr = _tmp1_;
		if (!_tmp2_) {
			break;
		}
		if (chr == ((gunichar) '-')) {
			was_hyphen = TRUE;
			continue;
		}
		if (was_hyphen) {
			GString* _tmp3_;
			was_hyphen = FALSE;
			_tmp3_ = _result_;
			g_string_append_unichar (_tmp3_, g_unichar_toupper (chr));
		} else {
			GString* _tmp4_;
			_tmp4_ = _result_;
			g_string_append_unichar (_tmp4_, chr);
		}
	}
	_tmp5_ = _result_;
	_tmp6_ = _tmp5_->str;
	_tmp7_ = g_strdup (_tmp6_);
	result = _tmp7_;
	_g_string_free0 (_result_);
	return result;
}

gchar*
ft_from_camel_case (const gchar* name)
{
	GString* _result_ = NULL;
	GString* _tmp0_;
	gboolean was_lowercase = FALSE;
	gunichar chr = 0U;
	gint chr_span_end = 0;
	GString* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* result;
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = g_string_new ("");
	_result_ = _tmp0_;
	was_lowercase = FALSE;
	chr_span_end = 0;
	while (TRUE) {
		gunichar _tmp1_ = 0U;
		gboolean _tmp2_;
		gboolean _tmp3_ = FALSE;
		_tmp2_ = string_get_next_char (name, &chr_span_end, &_tmp1_);
		chr = _tmp1_;
		if (!_tmp2_) {
			break;
		}
		if (g_unichar_isupper (chr)) {
			_tmp3_ = was_lowercase;
		} else {
			_tmp3_ = FALSE;
		}
		if (_tmp3_) {
			GString* _tmp4_;
			GString* _tmp5_;
			was_lowercase = FALSE;
			_tmp4_ = _result_;
			g_string_append_c (_tmp4_, '-');
			_tmp5_ = _result_;
			g_string_append_unichar (_tmp5_, g_unichar_tolower (chr));
		} else {
			gunichar _tmp6_ = 0U;
			GString* _tmp7_;
			was_lowercase = g_unichar_islower (chr);
			if (was_lowercase) {
				_tmp6_ = chr;
			} else {
				_tmp6_ = g_unichar_tolower (chr);
			}
			_tmp7_ = _result_;
			g_string_append_unichar (_tmp7_, _tmp6_);
		}
	}
	_tmp8_ = _result_;
	_tmp9_ = _tmp8_->str;
	_tmp10_ = g_strdup (_tmp9_);
	result = _tmp10_;
	_g_string_free0 (_result_);
	return result;
}

/**
     * Bindings for `Peas.ExtensionSet.@foreach` seem to be broken.
     *
     * Here's an simpler, Vala-friendly implementation.
     */
void
ft_foreach_extension (PeasExtensionSet* extension_set,
                      FtExtensionSetForeachFunc func,
                      gpointer func_target)
{
	guint n_items = 0U;
	g_return_if_fail (extension_set != NULL);
	n_items = g_list_model_get_n_items ((GListModel*) extension_set);
	{
		guint i = 0U;
		i = 0U;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				GObject* object = NULL;
				GObject* _tmp2_;
				GObject* _tmp3_;
				if (!_tmp0_) {
					guint _tmp1_;
					_tmp1_ = i;
					i = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				if (!(i < n_items)) {
					break;
				}
				_tmp2_ = g_list_model_get_item ((GListModel*) extension_set, i);
				object = _tmp2_;
				_tmp3_ = object;
				func (_tmp3_, func_target);
				_g_object_unref0 (object);
			}
		}
	}
}

FtAsyncCallback*
ft_async_callback_new (GSourceFunc func,
                       gpointer func_target,
                       GDestroyNotify func_target_destroy_notify)
{
	FtAsyncCallback* self;
	GSourceFunc _tmp0_;
	gpointer _tmp0__target;
	GDestroyNotify _tmp0__target_destroy_notify;
	self = g_slice_new0 (FtAsyncCallback);
	ft_async_callback_instance_init (self);
	_tmp0_ = func;
	_tmp0__target = func_target;
	_tmp0__target_destroy_notify = func_target_destroy_notify;
	func = NULL;
	func_target = NULL;
	func_target_destroy_notify = NULL;
	(self->func_target_destroy_notify == NULL) ? NULL : (self->func_target_destroy_notify (self->func_target), NULL);
	self->func = NULL;
	self->func_target = NULL;
	self->func_target_destroy_notify = NULL;
	self->func = _tmp0_;
	self->func_target = _tmp0__target;
	self->func_target_destroy_notify = _tmp0__target_destroy_notify;
	(func_target_destroy_notify == NULL) ? NULL : (func_target_destroy_notify (func_target), NULL);
	func = NULL;
	func_target = NULL;
	func_target_destroy_notify = NULL;
	return self;
}

static void
ft_async_callback_instance_init (FtAsyncCallback * self)
{
}

void
ft_async_callback_free (FtAsyncCallback * self)
{
	(self->func_target_destroy_notify == NULL) ? NULL : (self->func_target_destroy_notify (self->func_target), NULL);
	self->func = NULL;
	self->func_target = NULL;
	self->func_target_destroy_notify = NULL;
	g_slice_free (FtAsyncCallback, self);
}

static inline gpointer
ft_async_queue_get_instance_private (FtAsyncQueue* self)
{
	return G_STRUCT_MEMBER_P (self, FtAsyncQueue_private_offset);
}

static gpointer
_g_thread_ref0 (gpointer self)
{
	return self ? g_thread_ref (self) : NULL;
}

static void
_g_thread_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_thread_unref (var), NULL));
}

FtAsyncQueue*
ft_async_queue_construct (GType object_type,
                          GType t_type,
                          GBoxedCopyFunc t_dup_func,
                          GDestroyNotify t_destroy_func)
{
	FtAsyncQueue* self = NULL;
	GQueue* _tmp0_;
	GPtrArray* _tmp1_;
	self = (FtAsyncQueue*) g_type_create_instance (object_type);
	self->priv->t_type = t_type;
	self->priv->t_dup_func = t_dup_func;
	self->priv->t_destroy_func = t_destroy_func;
	_tmp0_ = g_queue_new ();
	(self->priv->queue == NULL) ? NULL : (self->priv->queue = (g_queue_free_full (self->priv->queue, t_destroy_func), NULL));
	self->priv->queue = _tmp0_;
	_vala_clear_GMutex (&self->priv->mutex);
	g_mutex_init (&self->priv->mutex);
	_vala_clear_GCond (&self->priv->cond);
	g_cond_init (&self->priv->cond);
	_tmp1_ = g_ptr_array_new_full ((guint) 0, _g_thread_unref0_);
	_g_ptr_array_unref0 (self->priv->waiting_threads);
	self->priv->waiting_threads = _tmp1_;
	return self;
}

FtAsyncQueue*
ft_async_queue_new (GType t_type,
                    GBoxedCopyFunc t_dup_func,
                    GDestroyNotify t_destroy_func)
{
	return ft_async_queue_construct (FT_TYPE_ASYNC_QUEUE, t_type, t_dup_func, t_destroy_func);
}

/**
         * Push an item to the tail of the queue.
         */
void
ft_async_queue_push (FtAsyncQueue* self,
                     gpointer item)
{
	GQueue* _tmp0_;
	gpointer _tmp1_;
	g_return_if_fail (self != NULL);
	g_mutex_lock (&self->priv->mutex);
	_tmp0_ = self->priv->queue;
	_tmp1_ = item;
	item = NULL;
	g_queue_push_tail (_tmp0_, _tmp1_);
	g_mutex_unlock (&self->priv->mutex);
	((item == NULL) || (self->priv->t_destroy_func == NULL)) ? NULL : (item = (self->priv->t_destroy_func (item), NULL));
}

/**
         * Try to pop an item without blocking. Returns null if empty.
         */
gpointer
ft_async_queue_pop (FtAsyncQueue* self)
{
	gpointer item = NULL;
	GQueue* _tmp0_;
	gpointer result;
	g_return_val_if_fail (self != NULL, NULL);
	g_mutex_lock (&self->priv->mutex);
	item = NULL;
	_tmp0_ = self->priv->queue;
	if (g_queue_get_length (_tmp0_) > ((guint) 0)) {
		GQueue* _tmp1_;
		gpointer _tmp2_;
		GQueue* _tmp3_;
		_tmp1_ = self->priv->queue;
		_tmp2_ = g_queue_pop_head (_tmp1_);
		((item == NULL) || (self->priv->t_destroy_func == NULL)) ? NULL : (item = (self->priv->t_destroy_func (item), NULL));
		item = _tmp2_;
		_tmp3_ = self->priv->queue;
		if (g_queue_get_length (_tmp3_) == ((guint) 0)) {
			g_cond_broadcast (&self->priv->cond);
		}
	}
	g_mutex_unlock (&self->priv->mutex);
	result = item;
	return result;
}

/**
         * Current length of the queue.
         */
guint
ft_async_queue_length (FtAsyncQueue* self)
{
	guint length = 0U;
	GQueue* _tmp0_;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	g_mutex_lock (&self->priv->mutex);
	_tmp0_ = self->priv->queue;
	length = g_queue_get_length (_tmp0_);
	g_mutex_unlock (&self->priv->mutex);
	result = length;
	return result;
}

static void
ft_async_queue_wait_data_free (gpointer _data)
{
	FtAsyncQueueWaitData* _data_;
	_data_ = _data;
	_ft_async_queue_unref0 (_data_->self);
	g_slice_free (FtAsyncQueueWaitData, _data_);
}

static gpointer
_ft_async_queue_ref0 (gpointer self)
{
	return self ? ft_async_queue_ref (self) : NULL;
}

void
ft_async_queue_wait (FtAsyncQueue* self,
                     GAsyncReadyCallback _callback_,
                     gpointer _user_data_)
{
	FtAsyncQueueWaitData* _data_;
	FtAsyncQueue* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (FtAsyncQueueWaitData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, ft_async_queue_wait_data_free);
	_tmp0_ = _ft_async_queue_ref0 (self);
	_data_->self = _tmp0_;
	ft_async_queue_wait_co (_data_);
}

void
ft_async_queue_wait_finish (FtAsyncQueue* self,
                            GAsyncResult* _res_)
{
	FtAsyncQueueWaitData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

/**
         * Asynchronously waits until the queue becomes empty.
         *
         * The completion is dispatched back to the main loop.
         */
static Block58Data*
block58_data_ref (Block58Data* _data58_)
{
	g_atomic_int_inc (&_data58_->_ref_count_);
	return _data58_;
}

static void
block58_data_unref (void * _userdata_)
{
	Block58Data* _data58_;
	_data58_ = (Block58Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data58_->_ref_count_)) {
		FtAsyncQueue* self;
		self = _data58_->self;
		(_data58_->callback_target_destroy_notify == NULL) ? NULL : (_data58_->callback_target_destroy_notify (_data58_->callback_target), NULL);
		_data58_->callback = NULL;
		_data58_->callback_target = NULL;
		_data58_->callback_target_destroy_notify = NULL;
		_ft_async_queue_unref0 (self);
		g_slice_free (Block58Data, _data58_);
	}
}

static gboolean
_ft_async_queue_wait_co_gsource_func (gpointer self)
{
	gboolean result;
	result = ft_async_queue_wait_co (self);
	return result;
}

static gboolean
__lambda94_ (Block58Data* _data58_)
{
	FtAsyncQueue* self;
	GSource* idle_source = NULL;
	GSource* _tmp2_;
	GSourceFunc _tmp3_;
	gpointer _tmp3__target;
	GDestroyNotify _tmp3__target_destroy_notify;
	GMainContext* _tmp4_;
	gboolean result;
	self = _data58_->self;
	g_mutex_lock (&self->priv->mutex);
	while (TRUE) {
		gboolean _tmp0_ = FALSE;
		GQueue* _tmp1_;
		_tmp1_ = self->priv->queue;
		if (g_queue_get_length (_tmp1_) > ((guint) 0)) {
			_tmp0_ = !self->priv->in_dispose;
		} else {
			_tmp0_ = FALSE;
		}
		if (!_tmp0_) {
			break;
		}
		g_cond_wait (&self->priv->cond, &self->priv->mutex);
	}
	g_mutex_unlock (&self->priv->mutex);
	_tmp2_ = g_idle_source_new ();
	idle_source = _tmp2_;
	g_source_set_name (idle_source, "Ft.AsyncQueue.wait");
	_tmp3_ = _data58_->callback;
	_tmp3__target = _data58_->callback_target;
	_tmp3__target_destroy_notify = _data58_->callback_target_destroy_notify;
	_data58_->callback = NULL;
	_data58_->callback_target = NULL;
	_data58_->callback_target_destroy_notify = NULL;
	g_source_set_callback (idle_source, _tmp3_, _tmp3__target, _tmp3__target_destroy_notify);
	_tmp4_ = g_main_context_default ();
	g_source_attach (idle_source, _tmp4_);
	result = TRUE;
	_g_source_unref0 (idle_source);
	return result;
}

static gpointer
___lambda94__gthread_func (gpointer self)
{
	gpointer result;
	result = (gpointer) ((gintptr) __lambda94_ (self));
	block58_data_unref (self);
	return result;
}

static gboolean
ft_async_queue_wait_co (FtAsyncQueueWaitData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data58_ = g_slice_new0 (Block58Data);
	_data_->_data58_->_ref_count_ = 1;
	_data_->_data58_->self = ft_async_queue_ref (_data_->self);
	_data_->_data58_->_async_data_ = _data_;
	if (_data_->self->priv->in_dispose) {
		block58_data_unref (_data_->_data58_);
		_data_->_data58_ = NULL;
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	g_mutex_lock (&_data_->self->priv->mutex);
	_data_->_tmp0_ = _data_->self->priv->queue;
	_data_->already_empty = g_queue_get_length (_data_->_tmp0_) == ((guint) 0);
	g_mutex_unlock (&_data_->self->priv->mutex);
	if (_data_->already_empty) {
		block58_data_unref (_data_->_data58_);
		_data_->_data58_ = NULL;
		g_task_return_pointer (_data_->_async_result, _data_, NULL);
		if (_data_->_state_ != 0) {
			while (!g_task_get_completed (_data_->_async_result)) {
				g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
			}
		}
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_data_->_data58_->callback = _ft_async_queue_wait_co_gsource_func;
	_data_->_data58_->callback_target = _data_;
	_data_->_data58_->callback_target_destroy_notify = NULL;
	_data_->_tmp1_ = g_thread_new ("async-queue-wait", ___lambda94__gthread_func, block58_data_ref (_data_->_data58_));
	_data_->thread = _data_->_tmp1_;
	g_mutex_lock (&_data_->self->priv->mutex);
	_data_->_tmp2_ = _data_->self->priv->waiting_threads;
	_data_->_tmp3_ = _data_->thread;
	_data_->_tmp4_ = _g_thread_ref0 (_data_->_tmp3_);
	g_ptr_array_add (_data_->_tmp2_, _data_->_tmp4_);
	g_mutex_unlock (&_data_->self->priv->mutex);
	_data_->_state_ = 1;
	return FALSE;
	_state_1:
	;
	g_mutex_lock (&_data_->self->priv->mutex);
	_data_->_tmp5_ = _data_->self->priv->waiting_threads;
	_data_->_tmp6_ = _data_->thread;
	g_ptr_array_remove_fast (_data_->_tmp5_, _data_->_tmp6_);
	g_mutex_unlock (&_data_->self->priv->mutex);
	_g_thread_unref0 (_data_->thread);
	block58_data_unref (_data_->_data58_);
	_data_->_data58_ = NULL;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
ft_value_async_queue_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
ft_value_async_queue_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		ft_async_queue_unref (value->data[0].v_pointer);
	}
}

static void
ft_value_async_queue_copy_value (const GValue* src_value,
                                 GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = ft_async_queue_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
ft_value_async_queue_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
ft_value_async_queue_collect_value (GValue* value,
                                    guint n_collect_values,
                                    GTypeCValue* collect_values,
                                    guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		FtAsyncQueue * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = ft_async_queue_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
ft_value_async_queue_lcopy_value (const GValue* value,
                                  guint n_collect_values,
                                  GTypeCValue* collect_values,
                                  guint collect_flags)
{
	FtAsyncQueue ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = ft_async_queue_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
ft_param_spec_async_queue (const gchar* name,
                           const gchar* nick,
                           const gchar* blurb,
                           GType object_type,
                           GParamFlags flags)
{
	FtParamSpecAsyncQueue* spec;
	g_return_val_if_fail (g_type_is_a (object_type, FT_TYPE_ASYNC_QUEUE), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
ft_value_get_async_queue (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, FT_TYPE_ASYNC_QUEUE), NULL);
	return value->data[0].v_pointer;
}

void
ft_value_set_async_queue (GValue* value,
                          gpointer v_object)
{
	FtAsyncQueue * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, FT_TYPE_ASYNC_QUEUE));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, FT_TYPE_ASYNC_QUEUE));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		ft_async_queue_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		ft_async_queue_unref (old);
	}
}

void
ft_value_take_async_queue (GValue* value,
                           gpointer v_object)
{
	FtAsyncQueue * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, FT_TYPE_ASYNC_QUEUE));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, FT_TYPE_ASYNC_QUEUE));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		ft_async_queue_unref (old);
	}
}

static void
ft_async_queue_class_init (FtAsyncQueueClass * klass,
                           gpointer klass_data)
{
	ft_async_queue_parent_class = g_type_class_peek_parent (klass);
	((FtAsyncQueueClass *) klass)->finalize = ft_async_queue_finalize;
	g_type_class_adjust_private_offset (klass, &FtAsyncQueue_private_offset);
}

static void
ft_async_queue_instance_init (FtAsyncQueue * self,
                              gpointer klass)
{
	self->priv = ft_async_queue_get_instance_private (self);
	self->priv->in_dispose = FALSE;
	self->ref_count = 1;
}

static void
ft_async_queue_finalize (FtAsyncQueue * obj)
{
	FtAsyncQueue * self;
	GPtrArray* _tmp0_;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_ASYNC_QUEUE, FtAsyncQueue);
	g_signal_handlers_destroy (self);
	g_mutex_lock (&self->priv->mutex);
	self->priv->in_dispose = TRUE;
	g_cond_broadcast (&self->priv->cond);
	g_mutex_unlock (&self->priv->mutex);
	_tmp0_ = self->priv->waiting_threads;
	{
		GPtrArray* thread_collection = NULL;
		guint thread_index = 0U;
		thread_collection = _tmp0_;
		for (thread_index = 0; thread_index < thread_collection->len; thread_index = thread_index + 1) {
			GThread* _tmp1_;
			GThread* thread = NULL;
			_tmp1_ = _g_thread_ref0 ((GThread*) g_ptr_array_index (thread_collection, thread_index));
			thread = _tmp1_;
			{
				GThread* _tmp2_;
				_tmp2_ = thread;
				thread = NULL;
				g_thread_join (_tmp2_);
				_g_thread_unref0 (thread);
			}
		}
	}
	(self->priv->queue == NULL) ? NULL : (self->priv->queue = (g_queue_free_full (self->priv->queue, self->priv->t_destroy_func), NULL));
	_vala_clear_GMutex (&self->priv->mutex);
	_vala_clear_GCond (&self->priv->cond);
	_g_ptr_array_unref0 (self->priv->waiting_threads);
}

/**
     * A convenience wrapper around GLib.Queue that provides an async wait()
     * which resolves in the main loop once the queue becomes empty.
     */
static GType
ft_async_queue_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { ft_value_async_queue_init, ft_value_async_queue_free_value, ft_value_async_queue_copy_value, ft_value_async_queue_peek_pointer, "p", ft_value_async_queue_collect_value, "p", ft_value_async_queue_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (FtAsyncQueueClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ft_async_queue_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FtAsyncQueue), 0, (GInstanceInitFunc) ft_async_queue_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType ft_async_queue_type_id;
	ft_async_queue_type_id = g_type_register_fundamental (g_type_fundamental_next (), "FtAsyncQueue", &g_define_type_info, &g_define_type_fundamental_info, 0);
	FtAsyncQueue_private_offset = g_type_add_instance_private (ft_async_queue_type_id, sizeof (FtAsyncQueuePrivate));
	return ft_async_queue_type_id;
}

GType
ft_async_queue_get_type (void)
{
	static volatile gsize ft_async_queue_type_id__once = 0;
	if (g_once_init_enter (&ft_async_queue_type_id__once)) {
		GType ft_async_queue_type_id;
		ft_async_queue_type_id = ft_async_queue_get_type_once ();
		g_once_init_leave (&ft_async_queue_type_id__once, ft_async_queue_type_id);
	}
	return ft_async_queue_type_id__once;
}

gpointer
ft_async_queue_ref (gpointer instance)
{
	FtAsyncQueue * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
ft_async_queue_unref (gpointer instance)
{
	FtAsyncQueue * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		FT_ASYNC_QUEUE_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

static void
_vala_clear_GMutex (GMutex * mutex)
{
	GMutex zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GMutex))) {
		g_mutex_clear (mutex);
		memset (mutex, 0, sizeof (GMutex));
	}
}

static void
_vala_clear_GRecMutex (GRecMutex * mutex)
{
	GRecMutex zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GRecMutex))) {
		g_rec_mutex_clear (mutex);
		memset (mutex, 0, sizeof (GRecMutex));
	}
}

static void
_vala_clear_GRWLock (GRWLock * mutex)
{
	GRWLock zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GRWLock))) {
		g_rw_lock_clear (mutex);
		memset (mutex, 0, sizeof (GRWLock));
	}
}

static void
_vala_clear_GCond (GCond * mutex)
{
	GCond zero_mutex = { 0 };
	if (memcmp (mutex, &zero_mutex, sizeof (GCond))) {
		g_cond_clear (mutex);
		memset (mutex, 0, sizeof (GCond));
	}
}

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;
}

