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

/*
 * Copyright (c) 2024-2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Authors: Kamil Prusko <kamilprusko@gmail.com>
 */

#include "ft_core.h"
#include <glib.h>
#include <stdlib.h>
#include <string.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 _FtEventBusEventWatch FtEventBusEventWatch;
typedef struct _FtEventBusConditionWatch FtEventBusConditionWatch;
enum  {
	FT_EVENT_BUS_0_PROPERTY,
	FT_EVENT_BUS_NUM_PROPERTIES
};
static GParamSpec* ft_event_bus_properties[FT_EVENT_BUS_NUM_PROPERTIES];
#define _ft_context_unref0(var) ((var == NULL) ? NULL : (var = (ft_context_unref (var), NULL)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
typedef struct _Block13Data Block13Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _ft_expression_unref0(var) ((var == NULL) ? NULL : (var = (ft_expression_unref (var), NULL)))
#define _g_array_unref0(var) ((var == NULL) ? NULL : (var = (g_array_unref (var), NULL)))
#define _ft_event_bus_event_watch_free0(var) ((var == NULL) ? NULL : (var = (ft_event_bus_event_watch_free (var), NULL)))
#define _ft_event_bus_condition_watch_free0(var) ((var == NULL) ? NULL : (var = (ft_event_bus_condition_watch_free (var), NULL)))
typedef struct _Block14Data Block14Data;
#define _ft_value_unref0(var) ((var == NULL) ? NULL : (var = (ft_value_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
enum  {
	FT_EVENT_BUS_EVENT_SIGNAL,
	FT_EVENT_BUS_NUM_SIGNALS
};
static guint ft_event_bus_signals[FT_EVENT_BUS_NUM_SIGNALS] = {0};
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

struct _FtEventBusPrivate {
	FtContext* last_context;
	GHashTable* event_watches;
	GHashTable* event_watches_by_name;
	GHashTable* condition_watches;
	guint check_conditions_idle_id;
};

struct _Block13Data {
	int _ref_count_;
	FtEventBus* self;
	FtContext* context;
};

struct _FtEventBusEventWatch {
	guint id;
	gchar* event_name;
	FtExpression* condition;
	FtEventCallback callback;
	gpointer callback_target;
	GDestroyNotify callback_target_destroy_notify;
};

struct _FtEventBusConditionWatch {
	guint id;
	FtExpression* condition;
	FtConditionCallback enter_callback;
	gpointer enter_callback_target;
	GDestroyNotify enter_callback_target_destroy_notify;
	FtConditionCallback leave_callback;
	gpointer leave_callback_target;
	GDestroyNotify leave_callback_target_destroy_notify;
	gboolean active;
};

struct _Block14Data {
	int _ref_count_;
	FtEventBus* self;
	FtContext* context;
};

static gint FtEventBus_private_offset;
static gpointer ft_event_bus_parent_class = NULL;
static guint ft_event_bus_next_watch_id;
static guint ft_event_bus_next_watch_id = (guint) 1;

static void ft_event_bus_event_watch_free (FtEventBusEventWatch * self);
static void ft_event_bus_condition_watch_free (FtEventBusConditionWatch * self);
static void ft_event_bus_event (FtEventBus* self,
                         FtEvent* event);
static void ft_event_bus_check_conditions (FtEventBus* self,
                                    FtContext* context);
static Block13Data* block13_data_ref (Block13Data* _data13_);
static void block13_data_unref (void * _userdata_);
static void __lambda68_ (Block13Data* _data13_,
                  guint id,
                  FtEventBusConditionWatch* watch);
static void ft_event_bus_condition_watch_check_condition (FtEventBusConditionWatch* self,
                                                   FtContext* context);
static void ___lambda68__gh_func (gconstpointer key,
                           gconstpointer value,
                           gpointer self);
static void ft_event_bus_schedule_check_conditions (FtEventBus* self);
static gboolean __lambda69_ (FtEventBus* self);
static gboolean ___lambda69__gsource_func (gpointer self);
static FtEventBusEventWatch* ft_event_bus_event_watch_new (void);
static FtEventBusConditionWatch* ft_event_bus_condition_watch_new (void);
static Block14Data* block14_data_ref (Block14Data* _data14_);
static void block14_data_unref (void * _userdata_);
static void __lambda70_ (Block14Data* _data14_,
                  guint id,
                  FtEventBusConditionWatch* watch);
static void ___lambda70__gh_func (gconstpointer key,
                           gconstpointer value,
                           gpointer self);
static void ft_event_bus_real_dispose (GObject* base);
static gboolean ft_event_bus_event_watch_check_condition (FtEventBusEventWatch* self,
                                                   FtContext* context);
static void ft_event_bus_event_watch_instance_init (FtEventBusEventWatch * self);
static void ft_event_bus_condition_watch_instance_init (FtEventBusConditionWatch * self);
static GObject * ft_event_bus_constructor (GType type,
                                    guint n_construct_properties,
                                    GObjectConstructParam * construct_properties);
static void _ft_event_bus_event_watch_free0_ (gpointer var);
static void _g_free0_ (gpointer var);
static void _g_array_unref0_ (gpointer var);
static void _ft_event_bus_condition_watch_free0_ (gpointer var);
static void ft_event_bus_finalize (GObject * obj);
static GType ft_event_bus_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
ft_event_bus_get_instance_private (FtEventBus* self)
{
	return G_STRUCT_MEMBER_P (self, FtEventBus_private_offset);
}

static gpointer
_ft_context_ref0 (gpointer self)
{
	return self ? ft_context_ref (self) : NULL;
}

static Block13Data*
block13_data_ref (Block13Data* _data13_)
{
	g_atomic_int_inc (&_data13_->_ref_count_);
	return _data13_;
}

static void
block13_data_unref (void * _userdata_)
{
	Block13Data* _data13_;
	_data13_ = (Block13Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data13_->_ref_count_)) {
		FtEventBus* self;
		self = _data13_->self;
		_ft_context_unref0 (_data13_->context);
		_g_object_unref0 (self);
		g_slice_free (Block13Data, _data13_);
	}
}

static void
__lambda68_ (Block13Data* _data13_,
             guint id,
             FtEventBusConditionWatch* watch)
{
	FtEventBus* self;
	self = _data13_->self;
	g_return_if_fail (watch != NULL);
	ft_event_bus_condition_watch_check_condition (watch, _data13_->context);
}

static void
___lambda68__gh_func (gconstpointer key,
                      gconstpointer value,
                      gpointer self)
{
	__lambda68_ (self, (guint) ((guintptr) key), (FtEventBusConditionWatch*) value);
}

static void
ft_event_bus_check_conditions (FtEventBus* self,
                               FtContext* context)
{
	Block13Data* _data13_;
	FtContext* _tmp0_;
	FtContext* _tmp1_;
	GHashTable* _tmp2_;
	FtContext* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	_data13_ = g_slice_new0 (Block13Data);
	_data13_->_ref_count_ = 1;
	_data13_->self = g_object_ref (self);
	_tmp0_ = _ft_context_ref0 (context);
	_ft_context_unref0 (_data13_->context);
	_data13_->context = _tmp0_;
	if (self->priv->check_conditions_idle_id != ((guint) 0)) {
		g_source_remove (self->priv->check_conditions_idle_id);
		self->priv->check_conditions_idle_id = (guint) 0;
	}
	_tmp1_ = self->priv->last_context;
	if (_data13_->context == _tmp1_) {
		block13_data_unref (_data13_);
		_data13_ = NULL;
		return;
	}
	_tmp2_ = self->priv->condition_watches;
	g_hash_table_foreach (_tmp2_, ___lambda68__gh_func, _data13_);
	_tmp3_ = _ft_context_ref0 (_data13_->context);
	_ft_context_unref0 (self->priv->last_context);
	self->priv->last_context = _tmp3_;
	block13_data_unref (_data13_);
	_data13_ = NULL;
}

static gboolean
__lambda69_ (FtEventBus* self)
{
	FtContext* _tmp0_;
	FtContext* _tmp1_;
	gboolean result;
	self->priv->check_conditions_idle_id = (guint) 0;
	_tmp0_ = ft_context_new_build (FT_TIMESTAMP_UNDEFINED);
	_tmp1_ = _tmp0_;
	ft_event_bus_check_conditions (self, _tmp1_);
	_ft_context_unref0 (_tmp1_);
	result = G_SOURCE_REMOVE;
	return result;
}

static gboolean
___lambda69__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda69_ ((FtEventBus*) self);
	return result;
}

static void
ft_event_bus_schedule_check_conditions (FtEventBus* self)
{
	g_return_if_fail (self != NULL);
	if (self->priv->check_conditions_idle_id != ((guint) 0)) {
		return;
	}
	self->priv->check_conditions_idle_id = g_idle_add_full (G_PRIORITY_DEFAULT, ___lambda69__gsource_func, g_object_ref (self), g_object_unref);
	g_source_set_name_by_id (self->priv->check_conditions_idle_id, "Ft.EventBus.check_conditions");
}

void
ft_event_bus_push_event (FtEventBus* self,
                         FtEvent* event)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (event != NULL);
	g_signal_emit (self, ft_event_bus_signals[FT_EVENT_BUS_EVENT_SIGNAL], 0, event);
}

static gpointer
_ft_expression_ref0 (gpointer self)
{
	return self ? ft_expression_ref (self) : NULL;
}

static gpointer
_g_array_ref0 (gpointer self)
{
	return self ? g_array_ref (self) : NULL;
}

guint
ft_event_bus_add_event_watch (FtEventBus* self,
                              const gchar* event_name,
                              FtExpression* condition,
                              FtEventCallback callback,
                              gpointer callback_target,
                              GDestroyNotify callback_target_destroy_notify)
{
	guint watch_id = 0U;
	guint _tmp0_;
	FtEventBusEventWatch* watch = NULL;
	FtEventBusEventWatch* _tmp1_;
	FtEventBusEventWatch* _tmp2_;
	FtEventBusEventWatch* _tmp3_;
	gchar* _tmp4_;
	FtEventBusEventWatch* _tmp5_;
	FtExpression* _tmp6_;
	FtEventBusEventWatch* _tmp7_;
	FtEventCallback _tmp8_;
	gpointer _tmp8__target;
	GDestroyNotify _tmp8__target_destroy_notify;
	FtEventBusEventWatch* unowned_watch = NULL;
	FtEventBusEventWatch* _tmp9_;
	GArray* unowned_watches_array = NULL;
	GHashTable* _tmp10_;
	gconstpointer _tmp11_;
	GHashTable* _tmp12_;
	FtEventBusEventWatch* _tmp13_;
	GArray* _tmp14_;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	g_return_val_if_fail (event_name != NULL, 0U);
	watch_id = ft_event_bus_next_watch_id;
	_tmp0_ = ft_event_bus_next_watch_id;
	ft_event_bus_next_watch_id = _tmp0_ + 1;
	_tmp1_ = ft_event_bus_event_watch_new ();
	watch = _tmp1_;
	_tmp2_ = watch;
	_tmp2_->id = watch_id;
	_tmp3_ = watch;
	_tmp4_ = g_strdup (event_name);
	_g_free0 (_tmp3_->event_name);
	_tmp3_->event_name = _tmp4_;
	_tmp5_ = watch;
	_tmp6_ = _ft_expression_ref0 (condition);
	_ft_expression_unref0 (_tmp5_->condition);
	_tmp5_->condition = _tmp6_;
	_tmp7_ = watch;
	_tmp8_ = callback;
	_tmp8__target = callback_target;
	_tmp8__target_destroy_notify = callback_target_destroy_notify;
	callback = NULL;
	callback_target = NULL;
	callback_target_destroy_notify = NULL;
	(_tmp7_->callback_target_destroy_notify == NULL) ? NULL : (_tmp7_->callback_target_destroy_notify (_tmp7_->callback_target), NULL);
	_tmp7_->callback = NULL;
	_tmp7_->callback_target = NULL;
	_tmp7_->callback_target_destroy_notify = NULL;
	_tmp7_->callback = _tmp8_;
	_tmp7_->callback_target = _tmp8__target;
	_tmp7_->callback_target_destroy_notify = _tmp8__target_destroy_notify;
	_tmp9_ = watch;
	unowned_watch = _tmp9_;
	_tmp10_ = self->priv->event_watches_by_name;
	_tmp11_ = g_hash_table_lookup (_tmp10_, event_name);
	unowned_watches_array = (GArray*) _tmp11_;
	_tmp12_ = self->priv->event_watches;
	_tmp13_ = watch;
	watch = NULL;
	g_hash_table_insert (_tmp12_, (gpointer) ((guintptr) watch_id), _tmp13_);
	_tmp14_ = unowned_watches_array;
	if (_tmp14_ == NULL) {
		GArray* watches_array = NULL;
		GArray* _tmp15_;
		GArray* _tmp16_;
		FtEventBusEventWatch* _tmp17_;
		GHashTable* _tmp18_;
		gchar* _tmp19_;
		GArray* _tmp20_;
		GArray* _tmp21_;
		_tmp15_ = g_array_new (TRUE, TRUE, sizeof (FtEventBusEventWatch*));
		watches_array = _tmp15_;
		_tmp16_ = watches_array;
		_tmp17_ = unowned_watch;
		g_array_append_val (_tmp16_, _tmp17_);
		_tmp18_ = self->priv->event_watches_by_name;
		_tmp19_ = g_strdup (event_name);
		_tmp20_ = watches_array;
		_tmp21_ = _g_array_ref0 (_tmp20_);
		g_hash_table_insert (_tmp18_, _tmp19_, _tmp21_);
		_g_array_unref0 (watches_array);
	} else {
		GArray* _tmp22_;
		FtEventBusEventWatch* _tmp23_;
		_tmp22_ = unowned_watches_array;
		_tmp23_ = unowned_watch;
		g_array_append_val (_tmp22_, _tmp23_);
	}
	result = watch_id;
	_ft_event_bus_event_watch_free0 (watch);
	(callback_target_destroy_notify == NULL) ? NULL : (callback_target_destroy_notify (callback_target), NULL);
	callback = NULL;
	callback_target = NULL;
	callback_target_destroy_notify = NULL;
	return result;
}

guint
ft_event_bus_add_condition_watch (FtEventBus* self,
                                  FtExpression* condition,
                                  FtConditionCallback enter_callback,
                                  gpointer enter_callback_target,
                                  GDestroyNotify enter_callback_target_destroy_notify,
                                  FtConditionCallback leave_callback,
                                  gpointer leave_callback_target,
                                  GDestroyNotify leave_callback_target_destroy_notify)
{
	guint watch_id = 0U;
	guint _tmp0_;
	FtEventBusConditionWatch* watch = NULL;
	FtEventBusConditionWatch* _tmp1_;
	FtEventBusConditionWatch* _tmp2_;
	FtEventBusConditionWatch* _tmp3_;
	FtExpression* _tmp4_;
	FtEventBusConditionWatch* _tmp5_;
	FtConditionCallback _tmp6_;
	gpointer _tmp6__target;
	GDestroyNotify _tmp6__target_destroy_notify;
	FtEventBusConditionWatch* _tmp7_;
	FtConditionCallback _tmp8_;
	gpointer _tmp8__target;
	GDestroyNotify _tmp8__target_destroy_notify;
	GHashTable* _tmp9_;
	FtEventBusConditionWatch* _tmp10_;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	g_return_val_if_fail (condition != NULL, 0U);
	watch_id = ft_event_bus_next_watch_id;
	_tmp0_ = ft_event_bus_next_watch_id;
	ft_event_bus_next_watch_id = _tmp0_ + 1;
	_tmp1_ = ft_event_bus_condition_watch_new ();
	watch = _tmp1_;
	_tmp2_ = watch;
	_tmp2_->id = watch_id;
	_tmp3_ = watch;
	_tmp4_ = _ft_expression_ref0 (condition);
	_ft_expression_unref0 (_tmp3_->condition);
	_tmp3_->condition = _tmp4_;
	_tmp5_ = watch;
	_tmp6_ = enter_callback;
	_tmp6__target = enter_callback_target;
	_tmp6__target_destroy_notify = enter_callback_target_destroy_notify;
	enter_callback = NULL;
	enter_callback_target = NULL;
	enter_callback_target_destroy_notify = NULL;
	(_tmp5_->enter_callback_target_destroy_notify == NULL) ? NULL : (_tmp5_->enter_callback_target_destroy_notify (_tmp5_->enter_callback_target), NULL);
	_tmp5_->enter_callback = NULL;
	_tmp5_->enter_callback_target = NULL;
	_tmp5_->enter_callback_target_destroy_notify = NULL;
	_tmp5_->enter_callback = _tmp6_;
	_tmp5_->enter_callback_target = _tmp6__target;
	_tmp5_->enter_callback_target_destroy_notify = _tmp6__target_destroy_notify;
	_tmp7_ = watch;
	_tmp8_ = leave_callback;
	_tmp8__target = leave_callback_target;
	_tmp8__target_destroy_notify = leave_callback_target_destroy_notify;
	leave_callback = NULL;
	leave_callback_target = NULL;
	leave_callback_target_destroy_notify = NULL;
	(_tmp7_->leave_callback_target_destroy_notify == NULL) ? NULL : (_tmp7_->leave_callback_target_destroy_notify (_tmp7_->leave_callback_target), NULL);
	_tmp7_->leave_callback = NULL;
	_tmp7_->leave_callback_target = NULL;
	_tmp7_->leave_callback_target_destroy_notify = NULL;
	_tmp7_->leave_callback = _tmp8_;
	_tmp7_->leave_callback_target = _tmp8__target;
	_tmp7_->leave_callback_target_destroy_notify = _tmp8__target_destroy_notify;
	_tmp9_ = self->priv->condition_watches;
	_tmp10_ = watch;
	watch = NULL;
	g_hash_table_insert (_tmp9_, (gpointer) ((guintptr) watch_id), _tmp10_);
	ft_event_bus_schedule_check_conditions (self);
	result = watch_id;
	_ft_event_bus_condition_watch_free0 (watch);
	(enter_callback_target_destroy_notify == NULL) ? NULL : (enter_callback_target_destroy_notify (enter_callback_target), NULL);
	enter_callback = NULL;
	enter_callback_target = NULL;
	enter_callback_target_destroy_notify = NULL;
	(leave_callback_target_destroy_notify == NULL) ? NULL : (leave_callback_target_destroy_notify (leave_callback_target), NULL);
	leave_callback = NULL;
	leave_callback_target = NULL;
	leave_callback_target_destroy_notify = NULL;
	return result;
}

static gpointer
vala_g_array_remove_index (GArray* self,
                           guint index)
{
	gpointer g = NULL;
	gpointer* _tmp0_;
	gint _tmp0__length1;
	gpointer _tmp1_;
	gpointer result;
	g_return_val_if_fail (self != NULL, NULL);
	_vala_assert (self->len > index, "length > index");
	_tmp0_ = self->data;
	_tmp0__length1 = self->len;
	_tmp1_ = _tmp0_[index];
	_tmp0_[index] = NULL;
	g = _tmp1_;
	g_array_remove_index (self, index);
	result = g;
	return result;
}

void
ft_event_bus_remove_event_watch (FtEventBus* self,
                                 guint watch_id)
{
	FtEventBusEventWatch* watch = NULL;
	GHashTable* _tmp0_;
	gconstpointer _tmp1_;
	FtEventBusEventWatch* _tmp2_;
	GArray* watches_array = NULL;
	GHashTable* _tmp3_;
	FtEventBusEventWatch* _tmp4_;
	const gchar* _tmp5_;
	gconstpointer _tmp6_;
	GArray* _tmp7_;
	GHashTable* _tmp16_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->event_watches;
	_tmp1_ = g_hash_table_lookup (_tmp0_, (gpointer) ((guintptr) watch_id));
	watch = (FtEventBusEventWatch*) _tmp1_;
	_tmp2_ = watch;
	if (_tmp2_ == NULL) {
		g_warning ("event-bus.vala:217: Unable to remove event watch %u.", watch_id);
		return;
	}
	_tmp3_ = self->priv->event_watches_by_name;
	_tmp4_ = watch;
	_tmp5_ = _tmp4_->event_name;
	_tmp6_ = g_hash_table_lookup (_tmp3_, _tmp5_);
	watches_array = (GArray*) _tmp6_;
	_tmp7_ = watches_array;
	if (_tmp7_ != NULL) {
		{
			guint index = 0U;
			index = 0U;
			{
				gboolean _tmp8_ = FALSE;
				_tmp8_ = TRUE;
				while (TRUE) {
					GArray* _tmp10_;
					FtEventBusEventWatch* item = NULL;
					GArray* _tmp11_;
					FtEventBusEventWatch* _tmp12_;
					FtEventBusEventWatch* _tmp13_;
					FtEventBusEventWatch* _tmp14_;
					if (!_tmp8_) {
						guint _tmp9_;
						_tmp9_ = index;
						index = _tmp9_ + 1;
					}
					_tmp8_ = FALSE;
					_tmp10_ = watches_array;
					if (!(index < _tmp10_->len)) {
						break;
					}
					_tmp11_ = watches_array;
					_tmp12_ = g_array_index (_tmp11_, FtEventBusEventWatch*, index);
					item = _tmp12_;
					_tmp13_ = item;
					_tmp14_ = watch;
					if (_tmp13_ == _tmp14_) {
						GArray* _tmp15_;
						_tmp15_ = watches_array;
						vala_g_array_remove_index (_tmp15_, index);
						break;
					}
				}
			}
		}
	}
	_tmp16_ = self->priv->event_watches;
	g_hash_table_remove (_tmp16_, (gpointer) ((guintptr) watch_id));
}

void
ft_event_bus_remove_condition_watch (FtEventBus* self,
                                     guint watch_id)
{
	FtEventBusConditionWatch* watch = NULL;
	GHashTable* _tmp0_;
	gconstpointer _tmp1_;
	FtEventBusConditionWatch* _tmp2_;
	FtEventBusConditionWatch* _tmp3_;
	GHashTable* _tmp11_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->condition_watches;
	_tmp1_ = g_hash_table_lookup (_tmp0_, (gpointer) ((guintptr) watch_id));
	watch = (FtEventBusConditionWatch*) _tmp1_;
	_tmp2_ = watch;
	if (_tmp2_ == NULL) {
		g_warning ("event-bus.vala:244: Unable to remove condition watch %u.", watch_id);
		return;
	}
	_tmp3_ = watch;
	if (_tmp3_->active) {
		FtEventBusConditionWatch* _tmp4_;
		FtConditionCallback _tmp5_;
		gpointer _tmp5__target;
		FtEventBusConditionWatch* _tmp10_;
		_tmp4_ = watch;
		_tmp5_ = _tmp4_->leave_callback;
		_tmp5__target = _tmp4_->leave_callback_target;
		if (_tmp5_ != NULL) {
			FtEventBusConditionWatch* _tmp6_;
			FtConditionCallback _tmp7_;
			gpointer _tmp7__target;
			FtContext* _tmp8_;
			FtContext* _tmp9_;
			_tmp6_ = watch;
			_tmp7_ = _tmp6_->leave_callback;
			_tmp7__target = _tmp6_->leave_callback_target;
			_tmp8_ = ft_context_new_build (FT_TIMESTAMP_UNDEFINED);
			_tmp9_ = _tmp8_;
			_tmp7_ (_tmp9_, _tmp7__target);
			_ft_context_unref0 (_tmp9_);
		}
		_tmp10_ = watch;
		_tmp10_->active = FALSE;
	}
	_tmp11_ = self->priv->condition_watches;
	g_hash_table_remove (_tmp11_, (gpointer) ((guintptr) watch_id));
}

static Block14Data*
block14_data_ref (Block14Data* _data14_)
{
	g_atomic_int_inc (&_data14_->_ref_count_);
	return _data14_;
}

static void
block14_data_unref (void * _userdata_)
{
	Block14Data* _data14_;
	_data14_ = (Block14Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data14_->_ref_count_)) {
		FtEventBus* self;
		self = _data14_->self;
		_ft_context_unref0 (_data14_->context);
		_g_object_unref0 (self);
		g_slice_free (Block14Data, _data14_);
	}
}

static void
__lambda70_ (Block14Data* _data14_,
             guint id,
             FtEventBusConditionWatch* watch)
{
	FtEventBus* self;
	self = _data14_->self;
	g_return_if_fail (watch != NULL);
	if (watch->active) {
		FtConditionCallback _tmp0_;
		gpointer _tmp0__target;
		_tmp0_ = watch->leave_callback;
		_tmp0__target = watch->leave_callback_target;
		if (_tmp0_ != NULL) {
			FtConditionCallback _tmp1_;
			gpointer _tmp1__target;
			_tmp1_ = watch->leave_callback;
			_tmp1__target = watch->leave_callback_target;
			_tmp1_ (_data14_->context, _tmp1__target);
		}
		watch->active = FALSE;
	}
}

static void
___lambda70__gh_func (gconstpointer key,
                      gconstpointer value,
                      gpointer self)
{
	__lambda70_ (self, (guint) ((guintptr) key), (FtEventBusConditionWatch*) value);
}

void
ft_event_bus_destroy (FtEventBus* self)
{
	Block14Data* _data14_;
	FtContext* _tmp0_;
	GHashTable* _tmp1_;
	g_return_if_fail (self != NULL);
	_data14_ = g_slice_new0 (Block14Data);
	_data14_->_ref_count_ = 1;
	_data14_->self = g_object_ref (self);
	if (self->priv->check_conditions_idle_id != ((guint) 0)) {
		g_source_remove (self->priv->check_conditions_idle_id);
		self->priv->check_conditions_idle_id = (guint) 0;
	}
	_tmp0_ = ft_context_new_build (FT_TIMESTAMP_UNDEFINED);
	_data14_->context = _tmp0_;
	_tmp1_ = self->priv->condition_watches;
	g_hash_table_foreach (_tmp1_, ___lambda70__gh_func, _data14_);
	block14_data_unref (_data14_);
	_data14_ = NULL;
}

static void
ft_event_bus_real_dispose (GObject* base)
{
	FtEventBus * self;
	GHashTable* _tmp0_;
	GHashTable* _tmp2_;
	GHashTable* _tmp4_;
	self = (FtEventBus*) base;
	ft_event_bus_destroy (self);
	_ft_context_unref0 (self->priv->last_context);
	self->priv->last_context = NULL;
	_tmp0_ = self->priv->event_watches_by_name;
	if (_tmp0_ != NULL) {
		GHashTable* _tmp1_;
		_tmp1_ = self->priv->event_watches_by_name;
		g_hash_table_remove_all (_tmp1_);
		_g_hash_table_unref0 (self->priv->event_watches_by_name);
		self->priv->event_watches_by_name = NULL;
	}
	_tmp2_ = self->priv->event_watches;
	if (_tmp2_ != NULL) {
		GHashTable* _tmp3_;
		_tmp3_ = self->priv->event_watches;
		g_hash_table_remove_all (_tmp3_);
		_g_hash_table_unref0 (self->priv->event_watches);
		self->priv->event_watches = NULL;
	}
	_tmp4_ = self->priv->condition_watches;
	if (_tmp4_ != NULL) {
		GHashTable* _tmp5_;
		_tmp5_ = self->priv->condition_watches;
		g_hash_table_remove_all (_tmp5_);
		_g_hash_table_unref0 (self->priv->condition_watches);
		self->priv->condition_watches = NULL;
	}
	G_OBJECT_CLASS (ft_event_bus_parent_class)->dispose (G_TYPE_CHECK_INSTANCE_CAST (self, G_TYPE_OBJECT, GObject));
}

FtEventBus*
ft_event_bus_construct (GType object_type)
{
	FtEventBus * self = NULL;
	self = (FtEventBus*) g_object_new (object_type, NULL);
	return self;
}

FtEventBus*
ft_event_bus_new (void)
{
	return ft_event_bus_construct (FT_TYPE_EVENT_BUS);
}

static void
ft_event_bus_real_event (FtEventBus* self,
                         FtEvent* event)
{
	GArray* watches_array = NULL;
	GHashTable* _tmp0_;
	FtEventSpec* _tmp1_;
	const gchar* _tmp2_;
	gconstpointer _tmp3_;
	GArray* _tmp4_;
	FtEventSpec* _tmp14_;
	const gchar* _tmp15_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (event != NULL);
	_tmp0_ = self->priv->event_watches_by_name;
	_tmp1_ = event->spec;
	_tmp2_ = _tmp1_->name;
	_tmp3_ = g_hash_table_lookup (_tmp0_, _tmp2_);
	watches_array = (GArray*) _tmp3_;
	_tmp4_ = watches_array;
	if (_tmp4_ != NULL) {
		{
			guint index = 0U;
			index = 0U;
			{
				gboolean _tmp5_ = FALSE;
				_tmp5_ = TRUE;
				while (TRUE) {
					GArray* _tmp7_;
					FtEventBusEventWatch* watch = NULL;
					GArray* _tmp8_;
					FtEventBusEventWatch* _tmp9_;
					FtEventBusEventWatch* _tmp10_;
					FtContext* _tmp11_;
					if (!_tmp5_) {
						guint _tmp6_;
						_tmp6_ = index;
						index = _tmp6_ + 1;
					}
					_tmp5_ = FALSE;
					_tmp7_ = watches_array;
					if (!(index < _tmp7_->len)) {
						break;
					}
					_tmp8_ = watches_array;
					_tmp9_ = g_array_index (_tmp8_, FtEventBusEventWatch*, index);
					watch = _tmp9_;
					_tmp10_ = watch;
					_tmp11_ = event->context;
					if (ft_event_bus_event_watch_check_condition (_tmp10_, _tmp11_)) {
						FtEventBusEventWatch* _tmp12_;
						FtEventCallback _tmp13_;
						gpointer _tmp13__target;
						_tmp12_ = watch;
						_tmp13_ = _tmp12_->callback;
						_tmp13__target = _tmp12_->callback_target;
						_tmp13_ (event, _tmp13__target);
					}
				}
			}
		}
	}
	_tmp14_ = event->spec;
	_tmp15_ = _tmp14_->name;
	if (g_strcmp0 (_tmp15_, "reschedule") != 0) {
		FtContext* _tmp16_;
		_tmp16_ = event->context;
		ft_event_bus_check_conditions (self, _tmp16_);
	}
}

static gboolean
ft_event_bus_event_watch_check_condition (FtEventBusEventWatch* self,
                                          FtContext* context)
{
	FtExpression* _tmp0_;
	gboolean _tmp9_ = FALSE;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (context != NULL, FALSE);
	_tmp0_ = self->condition;
	if (_tmp0_ == NULL) {
		result = TRUE;
		return result;
	}
	{
		FtValue* _result_ = NULL;
		FtExpression* _tmp1_;
		FtValue* _tmp2_;
		gboolean _tmp4_ = FALSE;
		FtValue* _tmp5_;
		_tmp1_ = self->condition;
		_tmp2_ = ft_expression_evaluate (_tmp1_, context, &_inner_error0_);
		_result_ = _tmp2_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp3_ = FALSE;
			if (_inner_error0_->domain == FT_EXPRESSION_ERROR) {
				goto __catch0_ft_expression_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return _tmp3_;
		}
		_tmp5_ = _result_;
		if (_tmp5_ != NULL) {
			FtValue* _tmp6_;
			_tmp6_ = _result_;
			_tmp4_ = ft_value_to_boolean (_tmp6_);
		} else {
			_tmp4_ = FALSE;
		}
		result = _tmp4_;
		_ft_value_unref0 (_result_);
		return result;
	}
	goto __finally0;
	__catch0_ft_expression_error:
	{
		GError* _error_ = NULL;
		GError* _tmp7_;
		const gchar* _tmp8_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp7_ = _error_;
		_tmp8_ = _tmp7_->message;
		g_warning ("event-bus.vala:48: Error while evaluating event condition: %s", _tmp8_);
		result = FALSE;
		_g_error_free0 (_error_);
		return result;
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return _tmp9_;
}

static FtEventBusEventWatch*
ft_event_bus_event_watch_new (void)
{
	FtEventBusEventWatch* self;
	self = g_slice_new0 (FtEventBusEventWatch);
	ft_event_bus_event_watch_instance_init (self);
	return self;
}

static void
ft_event_bus_event_watch_instance_init (FtEventBusEventWatch * self)
{
}

static void
ft_event_bus_event_watch_free (FtEventBusEventWatch * self)
{
	_g_free0 (self->event_name);
	self->event_name = NULL;
	_ft_expression_unref0 (self->condition);
	self->condition = NULL;
	(self->callback_target_destroy_notify == NULL) ? NULL : (self->callback_target_destroy_notify (self->callback_target), NULL);
	self->callback = NULL;
	self->callback_target = NULL;
	self->callback_target_destroy_notify = NULL;
	self->callback = NULL;
	self->callback_target = NULL;
	self->callback_target_destroy_notify = NULL;
	_g_free0 (self->event_name);
	_ft_expression_unref0 (self->condition);
	(self->callback_target_destroy_notify == NULL) ? NULL : (self->callback_target_destroy_notify (self->callback_target), NULL);
	self->callback = NULL;
	self->callback_target = NULL;
	self->callback_target_destroy_notify = NULL;
	g_slice_free (FtEventBusEventWatch, self);
}

static void
ft_event_bus_condition_watch_check_condition (FtEventBusConditionWatch* self,
                                              FtContext* context)
{
	gboolean active = FALSE;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	active = self->active;
	{
		FtValue* _result_ = NULL;
		FtExpression* _tmp0_;
		FtValue* _tmp1_;
		gboolean _tmp2_ = FALSE;
		FtValue* _tmp3_;
		_tmp0_ = self->condition;
		_tmp1_ = ft_expression_evaluate (_tmp0_, context, &_inner_error0_);
		_result_ = _tmp1_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == FT_EXPRESSION_ERROR) {
				goto __catch0_ft_expression_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
		_tmp3_ = _result_;
		if (_tmp3_ != NULL) {
			FtValue* _tmp4_;
			_tmp4_ = _result_;
			_tmp2_ = ft_value_to_boolean (_tmp4_);
		} else {
			_tmp2_ = FALSE;
		}
		active = _tmp2_;
		_ft_value_unref0 (_result_);
	}
	goto __finally0;
	__catch0_ft_expression_error:
	{
		GError* _error_ = NULL;
		GError* _tmp5_;
		const gchar* _tmp6_;
		_error_ = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp5_ = _error_;
		_tmp6_ = _tmp5_->message;
		g_warning ("event-bus.vala:80: Error while evaluating condition: %s", _tmp6_);
		_g_error_free0 (_error_);
		return;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	if (self->active != active) {
		gboolean _tmp7_ = FALSE;
		gboolean _tmp10_ = FALSE;
		if (self->active) {
			FtConditionCallback _tmp8_;
			gpointer _tmp8__target;
			_tmp8_ = self->leave_callback;
			_tmp8__target = self->leave_callback_target;
			_tmp7_ = _tmp8_ != NULL;
		} else {
			_tmp7_ = FALSE;
		}
		if (_tmp7_) {
			FtConditionCallback _tmp9_;
			gpointer _tmp9__target;
			_tmp9_ = self->leave_callback;
			_tmp9__target = self->leave_callback_target;
			_tmp9_ (context, _tmp9__target);
		}
		if (active) {
			FtConditionCallback _tmp11_;
			gpointer _tmp11__target;
			_tmp11_ = self->enter_callback;
			_tmp11__target = self->enter_callback_target;
			_tmp10_ = _tmp11_ != NULL;
		} else {
			_tmp10_ = FALSE;
		}
		if (_tmp10_) {
			FtConditionCallback _tmp12_;
			gpointer _tmp12__target;
			_tmp12_ = self->enter_callback;
			_tmp12__target = self->enter_callback_target;
			_tmp12_ (context, _tmp12__target);
		}
		self->active = active;
	}
}

static FtEventBusConditionWatch*
ft_event_bus_condition_watch_new (void)
{
	FtEventBusConditionWatch* self;
	self = g_slice_new0 (FtEventBusConditionWatch);
	ft_event_bus_condition_watch_instance_init (self);
	return self;
}

static void
ft_event_bus_condition_watch_instance_init (FtEventBusConditionWatch * self)
{
	self->active = FALSE;
}

static void
ft_event_bus_condition_watch_free (FtEventBusConditionWatch * self)
{
	_ft_expression_unref0 (self->condition);
	self->condition = NULL;
	(self->enter_callback_target_destroy_notify == NULL) ? NULL : (self->enter_callback_target_destroy_notify (self->enter_callback_target), NULL);
	self->enter_callback = NULL;
	self->enter_callback_target = NULL;
	self->enter_callback_target_destroy_notify = NULL;
	self->enter_callback = NULL;
	self->enter_callback_target = NULL;
	self->enter_callback_target_destroy_notify = NULL;
	(self->leave_callback_target_destroy_notify == NULL) ? NULL : (self->leave_callback_target_destroy_notify (self->leave_callback_target), NULL);
	self->leave_callback = NULL;
	self->leave_callback_target = NULL;
	self->leave_callback_target_destroy_notify = NULL;
	self->leave_callback = NULL;
	self->leave_callback_target = NULL;
	self->leave_callback_target_destroy_notify = NULL;
	_ft_expression_unref0 (self->condition);
	(self->enter_callback_target_destroy_notify == NULL) ? NULL : (self->enter_callback_target_destroy_notify (self->enter_callback_target), NULL);
	self->enter_callback = NULL;
	self->enter_callback_target = NULL;
	self->enter_callback_target_destroy_notify = NULL;
	(self->leave_callback_target_destroy_notify == NULL) ? NULL : (self->leave_callback_target_destroy_notify (self->leave_callback_target), NULL);
	self->leave_callback = NULL;
	self->leave_callback_target = NULL;
	self->leave_callback_target_destroy_notify = NULL;
	g_slice_free (FtEventBusConditionWatch, self);
}

static void
_ft_event_bus_event_watch_free0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (ft_event_bus_event_watch_free (var), NULL));
}

static void
_g_free0_ (gpointer var)
{
	var = (g_free (var), NULL);
}

static void
_g_array_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_array_unref (var), NULL));
}

static void
_ft_event_bus_condition_watch_free0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (ft_event_bus_condition_watch_free (var), NULL));
}

static GObject *
ft_event_bus_constructor (GType type,
                          guint n_construct_properties,
                          GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	static GWeakRef FtEventBus_singleton__ref;
	static GMutex FtEventBus_singleton__lock;
	FtEventBus * self;
	GHashFunc _tmp0_;
	GEqualFunc _tmp1_;
	GHashTable* _tmp2_;
	GHashFunc _tmp3_;
	GEqualFunc _tmp4_;
	GHashTable* _tmp5_;
	GHashFunc _tmp6_;
	GEqualFunc _tmp7_;
	GHashTable* _tmp8_;
	g_mutex_lock (&FtEventBus_singleton__lock);
	obj = g_weak_ref_get (&FtEventBus_singleton__ref);
	if (obj != NULL) {
		g_mutex_unlock (&FtEventBus_singleton__lock);
		return obj;
	}
	parent_class = G_OBJECT_CLASS (ft_event_bus_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_EVENT_BUS, FtEventBus);
	_tmp0_ = g_direct_hash;
	_tmp1_ = g_direct_equal;
	_tmp2_ = g_hash_table_new_full (_tmp0_, _tmp1_, NULL, _ft_event_bus_event_watch_free0_);
	_g_hash_table_unref0 (self->priv->event_watches);
	self->priv->event_watches = _tmp2_;
	_tmp3_ = g_str_hash;
	_tmp4_ = g_str_equal;
	_tmp5_ = g_hash_table_new_full (_tmp3_, _tmp4_, _g_free0_, _g_array_unref0_);
	_g_hash_table_unref0 (self->priv->event_watches_by_name);
	self->priv->event_watches_by_name = _tmp5_;
	_tmp6_ = g_direct_hash;
	_tmp7_ = g_direct_equal;
	_tmp8_ = g_hash_table_new_full (_tmp6_, _tmp7_, NULL, _ft_event_bus_condition_watch_free0_);
	_g_hash_table_unref0 (self->priv->condition_watches);
	self->priv->condition_watches = _tmp8_;
	g_weak_ref_set (&FtEventBus_singleton__ref, obj);
	g_mutex_unlock (&FtEventBus_singleton__lock);
	return obj;
}

static void
ft_event_bus_class_init (FtEventBusClass * klass,
                         gpointer klass_data)
{
	ft_event_bus_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &FtEventBus_private_offset);
	((GObjectClass *) klass)->dispose = (void (*) (GObject*)) ft_event_bus_real_dispose;
	G_OBJECT_CLASS (klass)->constructor = ft_event_bus_constructor;
	G_OBJECT_CLASS (klass)->finalize = ft_event_bus_finalize;
	ft_event_bus_signals[FT_EVENT_BUS_EVENT_SIGNAL] = g_signal_new_class_handler ("event", FT_TYPE_EVENT_BUS, G_SIGNAL_RUN_LAST, (GCallback) ft_event_bus_real_event, NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
}

static void
ft_event_bus_instance_init (FtEventBus * self,
                            gpointer klass)
{
	self->priv = ft_event_bus_get_instance_private (self);
	self->priv->last_context = NULL;
	self->priv->event_watches = NULL;
	self->priv->event_watches_by_name = NULL;
	self->priv->condition_watches = NULL;
	self->priv->check_conditions_idle_id = (guint) 0;
}

static void
ft_event_bus_finalize (GObject * obj)
{
	FtEventBus * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_EVENT_BUS, FtEventBus);
	_ft_context_unref0 (self->priv->last_context);
	_g_hash_table_unref0 (self->priv->event_watches);
	_g_hash_table_unref0 (self->priv->event_watches_by_name);
	_g_hash_table_unref0 (self->priv->condition_watches);
	G_OBJECT_CLASS (ft_event_bus_parent_class)->finalize (obj);
}

static GType
ft_event_bus_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (FtEventBusClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ft_event_bus_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FtEventBus), 0, (GInstanceInitFunc) ft_event_bus_instance_init, NULL };
	GType ft_event_bus_type_id;
	ft_event_bus_type_id = g_type_register_static (G_TYPE_OBJECT, "FtEventBus", &g_define_type_info, 0);
	FtEventBus_private_offset = g_type_add_instance_private (ft_event_bus_type_id, sizeof (FtEventBusPrivate));
	return ft_event_bus_type_id;
}

GType
ft_event_bus_get_type (void)
{
	static volatile gsize ft_event_bus_type_id__once = 0;
	if (g_once_init_enter (&ft_event_bus_type_id__once)) {
		GType ft_event_bus_type_id;
		ft_event_bus_type_id = ft_event_bus_get_type_once ();
		g_once_init_leave (&ft_event_bus_type_id__once, ft_event_bus_type_id);
	}
	return ft_event_bus_type_id__once;
}

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

