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

/* vim:set et sts=4 sw=4:
 *
 * ibus - The Input Bus
 *
 * Copyright(c) 2014 Red Hat, Inc.
 * Copyright(c) 2014 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright(c) 2014-2025 Takao Fujiwara <tfujiwar@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <stdio.h>
#include <ibus.h>
#include <glib/gstdio.h>
#include <gobject/gvaluecollector.h>

#define XKB_LAYOUT_XKB_COMMAND "setxkbmap"
#define XKB_LAYOUT_XKB_LAYOUT_ARG "-layout"
#define XKB_LAYOUT_XMODMAP_COMMAND "xmodmap"
#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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_XKB_LAYOUT (xkb_layout_get_type ())
#define XKB_LAYOUT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_XKB_LAYOUT, XKBLayout))
#define XKB_LAYOUT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_XKB_LAYOUT, XKBLayoutClass))
#define IS_XKB_LAYOUT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_XKB_LAYOUT))
#define IS_XKB_LAYOUT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_XKB_LAYOUT))
#define XKB_LAYOUT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_XKB_LAYOUT, XKBLayoutClass))

typedef struct _XKBLayout XKBLayout;
typedef struct _XKBLayoutClass XKBLayoutClass;
typedef struct _XKBLayoutPrivate XKBLayoutPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _ParamSpecXKBLayout ParamSpecXKBLayout;
#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 _XKBLayout {
	GTypeInstance parent_instance;
	volatile int ref_count;
	XKBLayoutPrivate * priv;
};

struct _XKBLayoutClass {
	GTypeClass parent_class;
	void (*finalize) (XKBLayout *self);
};

struct _XKBLayoutPrivate {
	gchar** m_get_layout_args;
	gint m_get_layout_args_length1;
	gint _m_get_layout_args_size_;
	gchar** m_xkb_latin_layouts;
	gint m_xkb_latin_layouts_length1;
	gint _m_xkb_latin_layouts_size_;
	gchar* m_default_layout;
	gchar* m_default_variant;
	gchar* m_default_option;
	gboolean m_use_xmodmap;
};

struct _ParamSpecXKBLayout {
	GParamSpec parent_instance;
};

static gint XKBLayout_private_offset;
static gpointer xkb_layout_parent_class = NULL;

VALA_EXTERN gpointer xkb_layout_ref (gpointer instance);
VALA_EXTERN void xkb_layout_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_xkb_layout (const gchar* name,
                                   const gchar* nick,
                                   const gchar* blurb,
                                   GType object_type,
                                   GParamFlags flags);
VALA_EXTERN void value_set_xkb_layout (GValue* value,
                           gpointer v_object);
VALA_EXTERN void value_take_xkb_layout (GValue* value,
                            gpointer v_object);
VALA_EXTERN gpointer value_get_xkb_layout (const GValue* value);
VALA_EXTERN GType xkb_layout_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (XKBLayout, xkb_layout_unref)
VALA_EXTERN XKBLayout* xkb_layout_new (void);
VALA_EXTERN XKBLayout* xkb_layout_construct (GType object_type);
VALA_EXTERN void xkb_layout_set_latin_layouts (XKBLayout* self,
                                   gchar** xkb_latin_layouts,
                                   gint xkb_latin_layouts_length1);
static gchar** _vala_array_dup6 (gchar** self,
                          gssize length);
VALA_EXTERN void xkb_layout_get_layout (XKBLayout* self,
                            gchar** layout,
                            gchar** variant,
                            gchar** option);
static void xkb_layout_search_get_layout_program (XKBLayout* self);
static void _vala_array_add31 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void xkb_layout_parse_localectl_status_str (XKBLayout* self,
                                            const gchar* standard_output,
                                            gchar** layout,
                                            gchar** variant,
                                            gchar** option);
static void xkb_layout_parse_xkbmap_query_str (XKBLayout* self,
                                        const gchar* standard_output,
                                        gchar** layout,
                                        gchar** variant,
                                        gchar** option);
VALA_EXTERN gboolean binding_common_default_is_xdisplay (void);
VALA_EXTERN void xkb_layout_set_layout (XKBLayout* self,
                            IBusEngineDesc* engine);
static gboolean _vala_string_array_contains (gchar* * stack,
                                      gssize stack_length,
                                      const gchar* needle);
static void _vala_array_add32 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void _vala_array_add33 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void _vala_array_add34 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void _vala_array_add35 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void _vala_array_add36 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void _vala_array_add37 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void _vala_array_add38 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
static void _vala_array_add39 (gchar** * array,
                        gint* length,
                        gint* size,
                        gchar* value);
VALA_EXTERN void xkb_layout_run_xmodmap (XKBLayout* self);
VALA_EXTERN void xkb_layout_set_use_xmodmap (XKBLayout* self,
                                 gboolean use_xmodmap);
static void xkb_layout_finalize (XKBLayout * obj);
static GType xkb_layout_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

static const gchar* XKB_LAYOUT_XMODMAP_KNOWN_FILES[4] = {".xmodmap", ".xmodmaprc", ".Xmodmap", ".Xmodmaprc"};

static inline gpointer
xkb_layout_get_instance_private (XKBLayout* self)
{
	return G_STRUCT_MEMBER_P (self, XKBLayout_private_offset);
}

XKBLayout*
xkb_layout_construct (GType object_type)
{
	XKBLayout* self = NULL;
	self = (XKBLayout*) g_type_create_instance (object_type);
	return self;
}

XKBLayout*
xkb_layout_new (void)
{
	return xkb_layout_construct (TYPE_XKB_LAYOUT);
}

static gchar**
_vala_array_dup6 (gchar** self,
                  gssize length)
{
	if (length >= 0) {
		gchar** result;
		gssize i;
		result = g_new0 (gchar*, length + 1);
		for (i = 0; i < length; i++) {
			gchar* _tmp0_;
			_tmp0_ = g_strdup (self[i]);
			result[i] = _tmp0_;
		}
		return result;
	}
	return NULL;
}

void
xkb_layout_set_latin_layouts (XKBLayout* self,
                              gchar** xkb_latin_layouts,
                              gint xkb_latin_layouts_length1)
{
	gchar** _tmp0_;
	gint _tmp0__length1;
	g_return_if_fail (self != NULL);
	_tmp0_ = (xkb_latin_layouts != NULL) ? _vala_array_dup6 (xkb_latin_layouts, xkb_latin_layouts_length1) : xkb_latin_layouts;
	_tmp0__length1 = xkb_latin_layouts_length1;
	self->priv->m_xkb_latin_layouts = (_vala_array_free (self->priv->m_xkb_latin_layouts, self->priv->m_xkb_latin_layouts_length1, (GDestroyNotify) g_free), NULL);
	self->priv->m_xkb_latin_layouts = _tmp0_;
	self->priv->m_xkb_latin_layouts_length1 = _tmp0__length1;
	self->priv->_m_xkb_latin_layouts_size_ = self->priv->m_xkb_latin_layouts_length1;
}

static void
_vala_array_add31 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

void
xkb_layout_get_layout (XKBLayout* self,
                       gchar** layout,
                       gchar** variant,
                       gchar** option)
{
	gchar* _vala_layout = NULL;
	gchar* _vala_variant = NULL;
	gchar* _vala_option = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* o = NULL;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	const gchar* _tmp4_ = NULL;
	const gchar* _tmp5_;
	gchar* _tmp7_;
	gchar** _tmp8_;
	gint _tmp8__length1;
	const gchar* _tmp9_;
	gchar** exec_command = NULL;
	gchar** _tmp10_;
	gint exec_command_length1;
	gint _exec_command_size_;
	gchar** _tmp11_;
	gint _tmp11__length1;
	gchar* standard_output = NULL;
	gchar* standard_error = NULL;
	gint exit_status = 0;
	const gchar* _tmp24_;
	gchar** _tmp25_;
	gint _tmp25__length1;
	const gchar* _tmp26_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_strdup ("");
	_g_free0 (_vala_layout);
	_vala_layout = _tmp0_;
	_tmp1_ = g_strdup ("");
	_g_free0 (_vala_variant);
	_vala_variant = _tmp1_;
	_tmp2_ = g_getenv ("XKB_DEFAULT_OPTIONS");
	_tmp3_ = g_strdup (_tmp2_);
	o = _tmp3_;
	_tmp5_ = o;
	if (_tmp5_ != NULL) {
		const gchar* _tmp6_;
		_tmp6_ = o;
		_tmp4_ = _tmp6_;
	} else {
		_tmp4_ = "";
	}
	_tmp7_ = g_strdup (_tmp4_);
	_g_free0 (_vala_option);
	_vala_option = _tmp7_;
	xkb_layout_search_get_layout_program (self);
	_tmp8_ = self->priv->m_get_layout_args;
	_tmp8__length1 = self->priv->m_get_layout_args_length1;
	_tmp9_ = _tmp8_[0];
	if (_tmp9_ == NULL) {
		g_warning ("xkblayout.vala:58: Not found localectl or setxkbmap command in PATH");
		_g_free0 (o);
		if (layout) {
			*layout = _vala_layout;
		} else {
			_g_free0 (_vala_layout);
		}
		if (variant) {
			*variant = _vala_variant;
		} else {
			_g_free0 (_vala_variant);
		}
		if (option) {
			*option = _vala_option;
		} else {
			_g_free0 (_vala_option);
		}
		return;
	}
	_tmp10_ = g_new0 (gchar*, 0 + 1);
	exec_command = _tmp10_;
	exec_command_length1 = 0;
	_exec_command_size_ = exec_command_length1;
	_tmp11_ = self->priv->m_get_layout_args;
	_tmp11__length1 = self->priv->m_get_layout_args_length1;
	{
		gchar** arg_collection = NULL;
		gint arg_collection_length1 = 0;
		gint _arg_collection_size_ = 0;
		gint arg_it = 0;
		arg_collection = _tmp11_;
		arg_collection_length1 = _tmp11__length1;
		for (arg_it = 0; arg_it < arg_collection_length1; arg_it = arg_it + 1) {
			const gchar* arg = NULL;
			arg = arg_collection[arg_it];
			{
				const gchar* _tmp12_;
				gchar* _tmp13_;
				_tmp12_ = arg;
				_tmp13_ = g_strdup (_tmp12_);
				_vala_array_add31 (&exec_command, &exec_command_length1, &_exec_command_size_, _tmp13_);
			}
		}
	}
	standard_output = NULL;
	standard_error = NULL;
	exit_status = 0;
	{
		gchar** _tmp14_;
		gint _tmp14__length1;
		gchar* _tmp15_ = NULL;
		gchar* _tmp16_ = NULL;
		gint _tmp17_ = 0;
		_tmp14_ = exec_command;
		_tmp14__length1 = exec_command_length1;
		g_spawn_sync (NULL, _tmp14_, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &_tmp15_, &_tmp16_, &_tmp17_, &_inner_error0_);
		_g_free0 (standard_output);
		standard_output = _tmp15_;
		_g_free0 (standard_error);
		standard_error = _tmp16_;
		exit_status = _tmp17_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_SPAWN_ERROR) {
				goto __catch0_g_spawn_error;
			}
			_g_free0 (standard_error);
			_g_free0 (standard_output);
			exec_command = (_vala_array_free (exec_command, exec_command_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (o);
			_g_free0 (_vala_layout);
			_g_free0 (_vala_variant);
			_g_free0 (_vala_option);
			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;
		}
	}
	goto __finally0;
	__catch0_g_spawn_error:
	{
		GError* err = NULL;
		FILE* _tmp18_;
		GError* _tmp19_;
		const gchar* _tmp20_;
		err = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp18_ = stderr;
		_tmp19_ = err;
		_tmp20_ = _tmp19_->message;
		fprintf (_tmp18_, "IBUS_ERROR: %s\n", _tmp20_);
		_g_error_free0 (err);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (standard_error);
		_g_free0 (standard_output);
		exec_command = (_vala_array_free (exec_command, exec_command_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (o);
		_g_free0 (_vala_layout);
		_g_free0 (_vala_variant);
		_g_free0 (_vala_option);
		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 (exit_status != 0) {
		const gchar* _tmp21_ = NULL;
		const gchar* _tmp22_;
		FILE* _tmp23_;
		_tmp22_ = standard_error;
		_tmp21_ = _tmp22_;
		if (_tmp21_ == NULL) {
			_tmp21_ = "";
		}
		_tmp23_ = stderr;
		fprintf (_tmp23_, "IBUS_ERROR: %s\n", _tmp21_);
	}
	_tmp24_ = standard_output;
	if (_tmp24_ == NULL) {
		_g_free0 (standard_error);
		_g_free0 (standard_output);
		exec_command = (_vala_array_free (exec_command, exec_command_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (o);
		if (layout) {
			*layout = _vala_layout;
		} else {
			_g_free0 (_vala_layout);
		}
		if (variant) {
			*variant = _vala_variant;
		} else {
			_g_free0 (_vala_variant);
		}
		if (option) {
			*option = _vala_option;
		} else {
			_g_free0 (_vala_option);
		}
		return;
	}
	_tmp25_ = exec_command;
	_tmp25__length1 = exec_command_length1;
	_tmp26_ = _tmp25_[0];
	if (g_strcmp0 (_tmp26_, "localectl") == 0) {
		const gchar* _tmp27_;
		_tmp27_ = standard_output;
		xkb_layout_parse_localectl_status_str (self, _tmp27_, &_vala_layout, &_vala_variant, &_vala_option);
	} else {
		gchar** _tmp28_;
		gint _tmp28__length1;
		const gchar* _tmp29_;
		_tmp28_ = exec_command;
		_tmp28__length1 = exec_command_length1;
		_tmp29_ = _tmp28_[0];
		if (g_strcmp0 (_tmp29_, XKB_LAYOUT_XKB_COMMAND) == 0) {
			const gchar* _tmp30_;
			_tmp30_ = standard_output;
			xkb_layout_parse_xkbmap_query_str (self, _tmp30_, &_vala_layout, &_vala_variant, &_vala_option);
		}
	}
	_g_free0 (standard_error);
	_g_free0 (standard_output);
	exec_command = (_vala_array_free (exec_command, exec_command_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (o);
	if (layout) {
		*layout = _vala_layout;
	} else {
		_g_free0 (_vala_layout);
	}
	if (variant) {
		*variant = _vala_variant;
	} else {
		_g_free0 (_vala_variant);
	}
	if (option) {
		*option = _vala_option;
	} else {
		_g_free0 (_vala_option);
	}
}

static void
xkb_layout_search_get_layout_program (XKBLayout* self)
{
	gchar** _tmp0_;
	gint _tmp0__length1;
	const gchar* _tmp1_;
	gchar* get_layout_command = NULL;
	const gchar* _tmp11_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->m_get_layout_args;
	_tmp0__length1 = self->priv->m_get_layout_args_length1;
	_tmp1_ = _tmp0_[0];
	if (_tmp1_ != NULL) {
		return;
	}
	get_layout_command = NULL;
	if (binding_common_default_is_xdisplay ()) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup ("setxkbmap -query");
		_g_free0 (get_layout_command);
		get_layout_command = _tmp2_;
	} else {
		gchar* _tmp3_;
		gchar* _tmp4_;
		gboolean _tmp5_;
		_tmp3_ = g_find_program_in_path ("localectl");
		_tmp4_ = _tmp3_;
		_tmp5_ = _tmp4_ != NULL;
		_g_free0 (_tmp4_);
		if (_tmp5_) {
			gchar* _tmp6_;
			_tmp6_ = g_strdup ("localectl status");
			_g_free0 (get_layout_command);
			get_layout_command = _tmp6_;
		} else {
			gchar* _tmp7_;
			gchar* _tmp8_;
			gboolean _tmp9_;
			_tmp7_ = g_find_program_in_path ("setxkbmap");
			_tmp8_ = _tmp7_;
			_tmp9_ = _tmp8_ != NULL;
			_g_free0 (_tmp8_);
			if (_tmp9_) {
				gchar* _tmp10_;
				_tmp10_ = g_strdup ("setxkbmap -query");
				_g_free0 (get_layout_command);
				get_layout_command = _tmp10_;
			}
		}
	}
	_tmp11_ = get_layout_command;
	if (_tmp11_ != NULL) {
		const gchar* _tmp12_;
		gchar** _tmp13_;
		gchar** _tmp14_;
		_tmp12_ = get_layout_command;
		_tmp14_ = _tmp13_ = g_strsplit (_tmp12_, " ", 0);
		self->priv->m_get_layout_args = (_vala_array_free (self->priv->m_get_layout_args, self->priv->m_get_layout_args_length1, (GDestroyNotify) g_free), NULL);
		self->priv->m_get_layout_args = _tmp14_;
		self->priv->m_get_layout_args_length1 = _vala_array_length (_tmp13_);
		self->priv->_m_get_layout_args_size_ = self->priv->m_get_layout_args_length1;
	}
	_g_free0 (get_layout_command);
}

static gint
string_index_of (const gchar* self,
                 const gchar* needle,
                 gint start_index)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	g_return_val_if_fail (needle != NULL, -1);
	_tmp0_ = strstr (((gchar*) self) + start_index, (gchar*) needle);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	if (_tmp1_ != NULL) {
		gchar* _tmp2_;
		_tmp2_ = _result_;
		result = (gint) (_tmp2_ - ((gchar*) self));
		return result;
	} else {
		result = -1;
		return result;
	}
}

static gchar*
string_slice (const gchar* self,
              glong start,
              glong end)
{
	glong string_length = 0L;
	gint _tmp0_;
	gint _tmp1_;
	gboolean _tmp2_ = FALSE;
	gboolean _tmp3_ = FALSE;
	gchar* _tmp4_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	string_length = (glong) _tmp1_;
	if (start < ((glong) 0)) {
		start = string_length + start;
	}
	if (end < ((glong) 0)) {
		end = string_length + end;
	}
	if (start >= ((glong) 0)) {
		_tmp2_ = start <= string_length;
	} else {
		_tmp2_ = FALSE;
	}
	g_return_val_if_fail (_tmp2_, NULL);
	if (end >= ((glong) 0)) {
		_tmp3_ = end <= string_length;
	} else {
		_tmp3_ = FALSE;
	}
	g_return_val_if_fail (_tmp3_, NULL);
	g_return_val_if_fail (start <= end, NULL);
	_tmp4_ = g_strndup (((gchar*) self) + start, (gsize) (end - start));
	result = _tmp4_;
	return result;
}

static gchar*
string_strip (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strstrip (_result_);
	result = _result_;
	return result;
}

static void
xkb_layout_parse_localectl_status_str (XKBLayout* self,
                                       const gchar* standard_output,
                                       gchar** layout,
                                       gchar** variant,
                                       gchar** option)
{
	gchar** _tmp0_;
	gchar** _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (standard_output != NULL);
	g_return_if_fail (*layout != NULL);
	g_return_if_fail (*variant != NULL);
	g_return_if_fail (*option != NULL);
	_tmp1_ = _tmp0_ = g_strsplit (standard_output, "\n", 0);
	{
		gchar** line_collection = NULL;
		gint line_collection_length1 = 0;
		gint _line_collection_size_ = 0;
		gint line_it = 0;
		line_collection = _tmp1_;
		line_collection_length1 = _vala_array_length (_tmp0_);
		for (line_it = 0; line_it < line_collection_length1; line_it = line_it + 1) {
			gchar* _tmp2_;
			gchar* line = NULL;
			_tmp2_ = g_strdup (line_collection[line_it]);
			line = _tmp2_;
			{
				static const gchar* elements[2] = {"X11 Layout:", "X11 Variant:"};
				{
					const gchar** element_collection = NULL;
					gint element_collection_length1 = 0;
					gint _element_collection_size_ = 0;
					gint element_it = 0;
					element_collection = elements;
					element_collection_length1 = G_N_ELEMENTS (elements);
					for (element_it = 0; element_it < element_collection_length1; element_it = element_it + 1) {
						const gchar* element = NULL;
						element = element_collection[element_it];
						{
							gchar* retval = NULL;
							gchar* _tmp3_;
							gint index = 0;
							const gchar* _tmp4_;
							const gchar* _tmp5_;
							_tmp3_ = g_strdup ("");
							retval = _tmp3_;
							_tmp4_ = line;
							_tmp5_ = element;
							index = string_index_of (_tmp4_, _tmp5_, 0);
							if (index >= 0) {
								const gchar* _tmp6_;
								const gchar* _tmp7_;
								gint _tmp8_;
								gint _tmp9_;
								const gchar* _tmp10_;
								gint _tmp11_;
								gint _tmp12_;
								gchar* _tmp13_;
								const gchar* _tmp14_;
								const gchar* _tmp17_;
								const gchar* _tmp18_;
								_tmp6_ = line;
								_tmp7_ = element;
								_tmp8_ = strlen (_tmp7_);
								_tmp9_ = _tmp8_;
								_tmp10_ = line;
								_tmp11_ = strlen (_tmp10_);
								_tmp12_ = _tmp11_;
								_tmp13_ = string_slice (_tmp6_, (glong) (index + _tmp9_), (glong) _tmp12_);
								_g_free0 (retval);
								retval = _tmp13_;
								_tmp14_ = retval;
								if (_tmp14_ != NULL) {
									const gchar* _tmp15_;
									gchar* _tmp16_;
									_tmp15_ = retval;
									_tmp16_ = string_strip (_tmp15_);
									_g_free0 (retval);
									retval = _tmp16_;
								}
								_tmp17_ = element;
								_tmp18_ = elements[0];
								if (g_strcmp0 (_tmp17_, _tmp18_) == 0) {
									const gchar* _tmp19_;
									gchar* _tmp20_;
									_tmp19_ = retval;
									_tmp20_ = g_strdup (_tmp19_);
									_g_free0 (*layout);
									*layout = _tmp20_;
								} else {
									const gchar* _tmp21_;
									const gchar* _tmp22_;
									_tmp21_ = element;
									_tmp22_ = elements[1];
									if (g_strcmp0 (_tmp21_, _tmp22_) == 0) {
										const gchar* _tmp23_;
										gchar* _tmp24_;
										_tmp23_ = retval;
										_tmp24_ = g_strdup (_tmp23_);
										_g_free0 (*variant);
										*variant = _tmp24_;
									}
								}
							}
							_g_free0 (retval);
						}
					}
				}
				_g_free0 (line);
			}
		}
		line_collection = (_vala_array_free (line_collection, line_collection_length1, (GDestroyNotify) g_free), NULL);
	}
}

static void
xkb_layout_parse_xkbmap_query_str (XKBLayout* self,
                                   const gchar* standard_output,
                                   gchar** layout,
                                   gchar** variant,
                                   gchar** option)
{
	gchar** _tmp0_;
	gchar** _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (standard_output != NULL);
	g_return_if_fail (*layout != NULL);
	g_return_if_fail (*variant != NULL);
	g_return_if_fail (*option != NULL);
	_tmp1_ = _tmp0_ = g_strsplit (standard_output, "\n", 0);
	{
		gchar** line_collection = NULL;
		gint line_collection_length1 = 0;
		gint _line_collection_size_ = 0;
		gint line_it = 0;
		line_collection = _tmp1_;
		line_collection_length1 = _vala_array_length (_tmp0_);
		for (line_it = 0; line_it < line_collection_length1; line_it = line_it + 1) {
			gchar* _tmp2_;
			gchar* line = NULL;
			_tmp2_ = g_strdup (line_collection[line_it]);
			line = _tmp2_;
			{
				static const gchar* elements[3] = {"layout:", "variant:", "options:"};
				{
					const gchar** element_collection = NULL;
					gint element_collection_length1 = 0;
					gint _element_collection_size_ = 0;
					gint element_it = 0;
					element_collection = elements;
					element_collection_length1 = G_N_ELEMENTS (elements);
					for (element_it = 0; element_it < element_collection_length1; element_it = element_it + 1) {
						const gchar* element = NULL;
						element = element_collection[element_it];
						{
							gchar* retval = NULL;
							gchar* _tmp3_;
							const gchar* _tmp4_;
							const gchar* _tmp5_;
							_tmp3_ = g_strdup ("");
							retval = _tmp3_;
							_tmp4_ = line;
							_tmp5_ = element;
							if (g_str_has_prefix (_tmp4_, _tmp5_)) {
								const gchar* _tmp6_;
								const gchar* _tmp7_;
								gint _tmp8_;
								gint _tmp9_;
								const gchar* _tmp10_;
								gint _tmp11_;
								gint _tmp12_;
								gchar* _tmp13_;
								const gchar* _tmp14_;
								const gchar* _tmp17_;
								const gchar* _tmp18_;
								_tmp6_ = line;
								_tmp7_ = element;
								_tmp8_ = strlen (_tmp7_);
								_tmp9_ = _tmp8_;
								_tmp10_ = line;
								_tmp11_ = strlen (_tmp10_);
								_tmp12_ = _tmp11_;
								_tmp13_ = string_slice (_tmp6_, (glong) _tmp9_, (glong) _tmp12_);
								_g_free0 (retval);
								retval = _tmp13_;
								_tmp14_ = retval;
								if (_tmp14_ != NULL) {
									const gchar* _tmp15_;
									gchar* _tmp16_;
									_tmp15_ = retval;
									_tmp16_ = string_strip (_tmp15_);
									_g_free0 (retval);
									retval = _tmp16_;
								}
								_tmp17_ = element;
								_tmp18_ = elements[0];
								if (g_strcmp0 (_tmp17_, _tmp18_) == 0) {
									const gchar* _tmp19_;
									gchar* _tmp20_;
									_tmp19_ = retval;
									_tmp20_ = g_strdup (_tmp19_);
									_g_free0 (*layout);
									*layout = _tmp20_;
								} else {
									const gchar* _tmp21_;
									const gchar* _tmp22_;
									_tmp21_ = element;
									_tmp22_ = elements[1];
									if (g_strcmp0 (_tmp21_, _tmp22_) == 0) {
										const gchar* _tmp23_;
										gchar* _tmp24_;
										_tmp23_ = retval;
										_tmp24_ = g_strdup (_tmp23_);
										_g_free0 (*variant);
										*variant = _tmp24_;
									} else {
										const gchar* _tmp25_;
										const gchar* _tmp26_;
										_tmp25_ = element;
										_tmp26_ = elements[2];
										if (g_strcmp0 (_tmp25_, _tmp26_) == 0) {
											const gchar* _tmp27_;
											gchar* _tmp28_;
											_tmp27_ = retval;
											_tmp28_ = g_strdup (_tmp27_);
											_g_free0 (*option);
											*option = _tmp28_;
										}
									}
								}
							}
							_g_free0 (retval);
						}
					}
				}
				_g_free0 (line);
			}
		}
		line_collection = (_vala_array_free (line_collection, line_collection_length1, (GDestroyNotify) g_free), NULL);
	}
}

static gboolean
_vala_string_array_contains (gchar* * stack,
                             gssize stack_length,
                             const gchar* needle)
{
	gssize i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
_vala_array_add32 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add33 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add34 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add35 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add36 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add37 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add38 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

static void
_vala_array_add39 (gchar** * array,
                   gint* length,
                   gint* size,
                   gchar* value)
{
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}

void
xkb_layout_set_layout (XKBLayout* self,
                       IBusEngineDesc* engine)
{
	gchar* layout = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* variant = NULL;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* option = NULL;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	gboolean _tmp7_ = FALSE;
	gboolean _tmp8_ = FALSE;
	const gchar* _tmp9_;
	gboolean need_us_layout = FALSE;
	const gchar* _tmp16_;
	gboolean _tmp19_ = FALSE;
	const gchar* _tmp26_;
	gboolean _tmp30_ = FALSE;
	const gchar* _tmp31_;
	const gchar* _tmp37_;
	gboolean _tmp38_ = FALSE;
	const gchar* _tmp39_;
	gchar** args = NULL;
	gchar** _tmp59_;
	gint args_length1;
	gint _args_size_;
	gchar* _tmp60_;
	gchar* _tmp61_;
	const gchar* _tmp62_;
	gchar* _tmp63_;
	gboolean _tmp64_ = FALSE;
	gboolean _tmp65_ = FALSE;
	const gchar* _tmp66_;
	gboolean _tmp72_ = FALSE;
	gboolean _tmp73_ = FALSE;
	const gchar* _tmp74_;
	gchar* standard_error = NULL;
	gint exit_status = 0;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (engine != NULL);
	_tmp0_ = ibus_engine_desc_get_layout (engine);
	_tmp1_ = g_strdup (_tmp0_);
	layout = _tmp1_;
	_tmp2_ = ibus_engine_desc_get_layout_variant (engine);
	_tmp3_ = g_strdup (_tmp2_);
	variant = _tmp3_;
	_tmp4_ = ibus_engine_desc_get_layout_option (engine);
	_tmp5_ = g_strdup (_tmp4_);
	option = _tmp5_;
	_tmp6_ = layout;
	_vala_assert (_tmp6_ != NULL, "layout != null");
	_tmp9_ = layout;
	if (g_strcmp0 (_tmp9_, "default") == 0) {
		gboolean _tmp10_ = FALSE;
		const gchar* _tmp11_;
		_tmp11_ = variant;
		if (g_strcmp0 (_tmp11_, "default") == 0) {
			_tmp10_ = TRUE;
		} else {
			const gchar* _tmp12_;
			_tmp12_ = variant;
			_tmp10_ = g_strcmp0 (_tmp12_, "") == 0;
		}
		_tmp8_ = _tmp10_;
	} else {
		_tmp8_ = FALSE;
	}
	if (_tmp8_) {
		gboolean _tmp13_ = FALSE;
		const gchar* _tmp14_;
		_tmp14_ = option;
		if (g_strcmp0 (_tmp14_, "default") == 0) {
			_tmp13_ = TRUE;
		} else {
			const gchar* _tmp15_;
			_tmp15_ = option;
			_tmp13_ = g_strcmp0 (_tmp15_, "") == 0;
		}
		_tmp7_ = _tmp13_;
	} else {
		_tmp7_ = FALSE;
	}
	if (_tmp7_) {
		_g_free0 (option);
		_g_free0 (variant);
		_g_free0 (layout);
		return;
	}
	need_us_layout = FALSE;
	_tmp16_ = variant;
	if (g_strcmp0 (_tmp16_, "eng") != 0) {
		const gchar* _tmp17_;
		gchar** _tmp18_;
		gint _tmp18__length1;
		_tmp17_ = layout;
		_tmp18_ = self->priv->m_xkb_latin_layouts;
		_tmp18__length1 = self->priv->m_xkb_latin_layouts_length1;
		need_us_layout = _vala_string_array_contains (_tmp18_, _tmp18__length1, _tmp17_);
	}
	if (!need_us_layout) {
		const gchar* _tmp20_;
		_tmp20_ = variant;
		_tmp19_ = _tmp20_ != NULL;
	} else {
		_tmp19_ = FALSE;
	}
	if (_tmp19_) {
		const gchar* _tmp21_;
		const gchar* _tmp22_;
		gchar* _tmp23_;
		gchar* _tmp24_;
		gchar** _tmp25_;
		gint _tmp25__length1;
		_tmp21_ = layout;
		_tmp22_ = variant;
		_tmp23_ = g_strdup_printf ("%s(%s)", _tmp21_, _tmp22_);
		_tmp24_ = _tmp23_;
		_tmp25_ = self->priv->m_xkb_latin_layouts;
		_tmp25__length1 = self->priv->m_xkb_latin_layouts_length1;
		need_us_layout = _vala_string_array_contains (_tmp25_, _tmp25__length1, _tmp24_);
		_g_free0 (_tmp24_);
	}
	_tmp26_ = self->priv->m_default_layout;
	if (g_strcmp0 (_tmp26_, "") == 0) {
		gchar* _tmp27_ = NULL;
		gchar* _tmp28_ = NULL;
		gchar* _tmp29_ = NULL;
		xkb_layout_get_layout (self, &_tmp27_, &_tmp28_, &_tmp29_);
		_g_free0 (self->priv->m_default_layout);
		self->priv->m_default_layout = _tmp27_;
		_g_free0 (self->priv->m_default_variant);
		self->priv->m_default_variant = _tmp28_;
		_g_free0 (self->priv->m_default_option);
		self->priv->m_default_option = _tmp29_;
	}
	_tmp31_ = layout;
	if (g_strcmp0 (_tmp31_, "default") == 0) {
		_tmp30_ = TRUE;
	} else {
		const gchar* _tmp32_;
		_tmp32_ = layout;
		_tmp30_ = g_strcmp0 (_tmp32_, "") == 0;
	}
	if (_tmp30_) {
		const gchar* _tmp33_;
		gchar* _tmp34_;
		const gchar* _tmp35_;
		gchar* _tmp36_;
		_tmp33_ = self->priv->m_default_layout;
		_tmp34_ = g_strdup (_tmp33_);
		_g_free0 (layout);
		layout = _tmp34_;
		_tmp35_ = self->priv->m_default_variant;
		_tmp36_ = g_strdup (_tmp35_);
		_g_free0 (variant);
		variant = _tmp36_;
	}
	_tmp37_ = layout;
	if (g_strcmp0 (_tmp37_, "") == 0) {
		g_warning ("xkblayout.vala:198: Could not get the correct layout");
		_g_free0 (option);
		_g_free0 (variant);
		_g_free0 (layout);
		return;
	}
	_tmp39_ = option;
	if (g_strcmp0 (_tmp39_, "default") == 0) {
		_tmp38_ = TRUE;
	} else {
		const gchar* _tmp40_;
		_tmp40_ = option;
		_tmp38_ = g_strcmp0 (_tmp40_, "") == 0;
	}
	if (_tmp38_) {
		const gchar* _tmp41_;
		gchar* _tmp42_;
		_tmp41_ = self->priv->m_default_option;
		_tmp42_ = g_strdup (_tmp41_);
		_g_free0 (option);
		option = _tmp42_;
	} else {
		const gchar* _tmp43_;
		const gchar* _tmp44_;
		gchar** _tmp45_;
		gchar** _tmp46_;
		gchar** _tmp47_;
		gint _tmp47__length1;
		gboolean _tmp48_;
		_tmp43_ = option;
		_tmp44_ = self->priv->m_default_option;
		_tmp46_ = _tmp45_ = g_strsplit (_tmp44_, ",", 0);
		_tmp47_ = _tmp46_;
		_tmp47__length1 = _vala_array_length (_tmp45_);
		_tmp48_ = !_vala_string_array_contains (_tmp47_, _vala_array_length (_tmp45_), _tmp43_);
		_tmp47_ = (_vala_array_free (_tmp47_, _tmp47__length1, (GDestroyNotify) g_free), NULL);
		if (_tmp48_) {
			const gchar* _tmp49_;
			const gchar* _tmp50_;
			gchar* _tmp51_;
			_tmp49_ = self->priv->m_default_option;
			_tmp50_ = option;
			_tmp51_ = g_strdup_printf ("%s,%s", _tmp49_, _tmp50_);
			_g_free0 (option);
			option = _tmp51_;
		} else {
			const gchar* _tmp52_;
			gchar* _tmp53_;
			_tmp52_ = self->priv->m_default_option;
			_tmp53_ = g_strdup (_tmp52_);
			_g_free0 (option);
			option = _tmp53_;
		}
	}
	if (need_us_layout) {
		const gchar* _tmp54_;
		gchar* _tmp55_;
		const gchar* _tmp56_;
		_tmp54_ = layout;
		_tmp55_ = g_strconcat (_tmp54_, ",us", NULL);
		_g_free0 (layout);
		layout = _tmp55_;
		_tmp56_ = variant;
		if (_tmp56_ != NULL) {
			const gchar* _tmp57_;
			gchar* _tmp58_;
			_tmp57_ = variant;
			_tmp58_ = g_strconcat (_tmp57_, ",", NULL);
			_g_free0 (variant);
			variant = _tmp58_;
		}
	}
	_tmp59_ = g_new0 (gchar*, 0 + 1);
	args = _tmp59_;
	args_length1 = 0;
	_args_size_ = args_length1;
	_tmp60_ = g_strdup (XKB_LAYOUT_XKB_COMMAND);
	_vala_array_add32 (&args, &args_length1, &_args_size_, _tmp60_);
	_tmp61_ = g_strdup (XKB_LAYOUT_XKB_LAYOUT_ARG);
	_vala_array_add33 (&args, &args_length1, &_args_size_, _tmp61_);
	_tmp62_ = layout;
	_tmp63_ = g_strdup (_tmp62_);
	_vala_array_add34 (&args, &args_length1, &_args_size_, _tmp63_);
	_tmp66_ = variant;
	if (_tmp66_ != NULL) {
		const gchar* _tmp67_;
		_tmp67_ = variant;
		_tmp65_ = g_strcmp0 (_tmp67_, "") != 0;
	} else {
		_tmp65_ = FALSE;
	}
	if (_tmp65_) {
		const gchar* _tmp68_;
		_tmp68_ = variant;
		_tmp64_ = g_strcmp0 (_tmp68_, "default") != 0;
	} else {
		_tmp64_ = FALSE;
	}
	if (_tmp64_) {
		gchar* _tmp69_;
		const gchar* _tmp70_;
		gchar* _tmp71_;
		_tmp69_ = g_strdup ("-variant");
		_vala_array_add35 (&args, &args_length1, &_args_size_, _tmp69_);
		_tmp70_ = variant;
		_tmp71_ = g_strdup (_tmp70_);
		_vala_array_add36 (&args, &args_length1, &_args_size_, _tmp71_);
	}
	_tmp74_ = option;
	if (_tmp74_ != NULL) {
		const gchar* _tmp75_;
		_tmp75_ = option;
		_tmp73_ = g_strcmp0 (_tmp75_, "") != 0;
	} else {
		_tmp73_ = FALSE;
	}
	if (_tmp73_) {
		const gchar* _tmp76_;
		_tmp76_ = option;
		_tmp72_ = g_strcmp0 (_tmp76_, "default") != 0;
	} else {
		_tmp72_ = FALSE;
	}
	if (_tmp72_) {
		gchar* _tmp77_;
		gchar* _tmp78_;
		const gchar* _tmp79_;
		gchar* _tmp80_;
		_tmp77_ = g_strdup ("-option");
		_vala_array_add37 (&args, &args_length1, &_args_size_, _tmp77_);
		_tmp78_ = g_strdup ("-option");
		_vala_array_add38 (&args, &args_length1, &_args_size_, _tmp78_);
		_tmp79_ = option;
		_tmp80_ = g_strdup (_tmp79_);
		_vala_array_add39 (&args, &args_length1, &_args_size_, _tmp80_);
	}
	standard_error = NULL;
	exit_status = 0;
	{
		gboolean _tmp81_ = FALSE;
		gchar** _tmp82_;
		gint _tmp82__length1;
		gchar* _tmp83_ = NULL;
		gint _tmp84_ = 0;
		gboolean _tmp85_;
		_tmp82_ = args;
		_tmp82__length1 = args_length1;
		_tmp85_ = g_spawn_sync (NULL, _tmp82_, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &_tmp83_, &_tmp84_, &_inner_error0_);
		_g_free0 (standard_error);
		standard_error = _tmp83_;
		exit_status = _tmp84_;
		_tmp81_ = _tmp85_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_SPAWN_ERROR) {
				goto __catch0_g_spawn_error;
			}
			_g_free0 (standard_error);
			args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (option);
			_g_free0 (variant);
			_g_free0 (layout);
			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;
		}
		if (!_tmp81_) {
			const gchar* _tmp86_;
			_tmp86_ = ibus_engine_desc_get_layout (engine);
			g_warning ("xkblayout.vala:245: Switch xkb layout to %s failed.", _tmp86_);
		}
	}
	goto __finally0;
	__catch0_g_spawn_error:
	{
		GError* e = NULL;
		GError* _tmp87_;
		const gchar* _tmp88_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp87_ = e;
		_tmp88_ = _tmp87_->message;
		g_warning ("xkblayout.vala:248: Execute setxkbmap failed: %s", _tmp88_);
		_g_error_free0 (e);
		_g_free0 (standard_error);
		args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (option);
		_g_free0 (variant);
		_g_free0 (layout);
		return;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (standard_error);
		args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (option);
		_g_free0 (variant);
		_g_free0 (layout);
		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 (exit_status != 0) {
		const gchar* _tmp89_ = NULL;
		const gchar* _tmp90_;
		_tmp90_ = standard_error;
		_tmp89_ = _tmp90_;
		if (_tmp89_ == NULL) {
			_tmp89_ = "(null)";
		}
		g_warning ("xkblayout.vala:253: Execute setxkbmap failed: %s", _tmp89_);
	}
	xkb_layout_run_xmodmap (self);
	_g_free0 (standard_error);
	args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (option);
	_g_free0 (variant);
	_g_free0 (layout);
}

void
xkb_layout_run_xmodmap (XKBLayout* self)
{
	gchar* homedir = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	if (!self->priv->m_use_xmodmap) {
		return;
	}
	_tmp0_ = g_get_home_dir ();
	_tmp1_ = g_strdup (_tmp0_);
	homedir = _tmp1_;
	{
		const gchar** xmodmap_file_collection = NULL;
		gint xmodmap_file_collection_length1 = 0;
		gint _xmodmap_file_collection_size_ = 0;
		gint xmodmap_file_it = 0;
		xmodmap_file_collection = XKB_LAYOUT_XMODMAP_KNOWN_FILES;
		xmodmap_file_collection_length1 = G_N_ELEMENTS (XKB_LAYOUT_XMODMAP_KNOWN_FILES);
		for (xmodmap_file_it = 0; xmodmap_file_it < xmodmap_file_collection_length1; xmodmap_file_it = xmodmap_file_it + 1) {
			gchar* _tmp2_;
			gchar* xmodmap_file = NULL;
			_tmp2_ = g_strdup (xmodmap_file_collection[xmodmap_file_it]);
			xmodmap_file = _tmp2_;
			{
				gchar* xmodmap_filepath = NULL;
				const gchar* _tmp3_;
				const gchar* _tmp4_;
				gchar* _tmp5_;
				const gchar* _tmp6_;
				gchar** args = NULL;
				gchar* _tmp7_;
				const gchar* _tmp8_;
				gchar* _tmp9_;
				gchar** _tmp10_;
				gint args_length1;
				gint _args_size_;
				_tmp3_ = homedir;
				_tmp4_ = xmodmap_file;
				_tmp5_ = g_build_filename (_tmp3_, _tmp4_, NULL);
				xmodmap_filepath = _tmp5_;
				_tmp6_ = xmodmap_filepath;
				if (!g_file_test (_tmp6_, G_FILE_TEST_EXISTS)) {
					_g_free0 (xmodmap_filepath);
					_g_free0 (xmodmap_file);
					continue;
				}
				_tmp7_ = g_strdup (XKB_LAYOUT_XMODMAP_COMMAND);
				_tmp8_ = xmodmap_filepath;
				_tmp9_ = g_strdup (_tmp8_);
				_tmp10_ = g_new0 (gchar*, 2 + 1);
				_tmp10_[0] = _tmp7_;
				_tmp10_[1] = _tmp9_;
				args = _tmp10_;
				args_length1 = 2;
				_args_size_ = args_length1;
				{
					gchar** _tmp11_;
					gint _tmp11__length1;
					_tmp11_ = args;
					_tmp11__length1 = args_length1;
					g_spawn_async (NULL, _tmp11_, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &_inner_error0_);
					if (G_UNLIKELY (_inner_error0_ != NULL)) {
						if (_inner_error0_->domain == G_SPAWN_ERROR) {
							goto __catch0_g_spawn_error;
						}
						args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
						_g_free0 (xmodmap_filepath);
						_g_free0 (xmodmap_file);
						_g_free0 (homedir);
						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;
					}
				}
				goto __finally0;
				__catch0_g_spawn_error:
				{
					GError* e = NULL;
					GError* _tmp12_;
					const gchar* _tmp13_;
					e = _inner_error0_;
					_inner_error0_ = NULL;
					_tmp12_ = e;
					_tmp13_ = _tmp12_->message;
					g_warning ("xkblayout.vala:292: Execute xmodmap is failed: %s\n", _tmp13_);
					_g_error_free0 (e);
					args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (xmodmap_filepath);
					_g_free0 (xmodmap_file);
					_g_free0 (homedir);
					return;
				}
				__finally0:
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (xmodmap_filepath);
					_g_free0 (xmodmap_file);
					_g_free0 (homedir);
					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;
				}
				args = (_vala_array_free (args, args_length1, (GDestroyNotify) g_free), NULL);
				_g_free0 (xmodmap_filepath);
				_g_free0 (xmodmap_file);
				break;
			}
		}
	}
	_g_free0 (homedir);
}

void
xkb_layout_set_use_xmodmap (XKBLayout* self,
                            gboolean use_xmodmap)
{
	g_return_if_fail (self != NULL);
	self->priv->m_use_xmodmap = use_xmodmap;
}

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

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

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

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

static gchar*
value_xkb_layout_collect_value (GValue* value,
                                guint n_collect_values,
                                GTypeCValue* collect_values,
                                guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		XKBLayout * 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 = xkb_layout_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
value_xkb_layout_lcopy_value (const GValue* value,
                              guint n_collect_values,
                              GTypeCValue* collect_values,
                              guint collect_flags)
{
	XKBLayout ** 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 = xkb_layout_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
param_spec_xkb_layout (const gchar* name,
                       const gchar* nick,
                       const gchar* blurb,
                       GType object_type,
                       GParamFlags flags)
{
	ParamSpecXKBLayout* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_XKB_LAYOUT), 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
value_get_xkb_layout (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_XKB_LAYOUT), NULL);
	return value->data[0].v_pointer;
}

void
value_set_xkb_layout (GValue* value,
                      gpointer v_object)
{
	XKBLayout * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_XKB_LAYOUT));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_XKB_LAYOUT));
		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;
		xkb_layout_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		xkb_layout_unref (old);
	}
}

void
value_take_xkb_layout (GValue* value,
                       gpointer v_object)
{
	XKBLayout * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_XKB_LAYOUT));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_XKB_LAYOUT));
		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) {
		xkb_layout_unref (old);
	}
}

static void
xkb_layout_class_init (XKBLayoutClass * klass,
                       gpointer klass_data)
{
	xkb_layout_parent_class = g_type_class_peek_parent (klass);
	((XKBLayoutClass *) klass)->finalize = xkb_layout_finalize;
	g_type_class_adjust_private_offset (klass, &XKBLayout_private_offset);
}

static void
xkb_layout_instance_init (XKBLayout * self,
                          gpointer klass)
{
	gchar** _tmp0_;
	gchar** _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	self->priv = xkb_layout_get_instance_private (self);
	_tmp0_ = g_new0 (gchar*, 0 + 1);
	self->priv->m_get_layout_args = _tmp0_;
	self->priv->m_get_layout_args_length1 = 0;
	self->priv->_m_get_layout_args_size_ = self->priv->m_get_layout_args_length1;
	_tmp1_ = g_new0 (gchar*, 0 + 1);
	self->priv->m_xkb_latin_layouts = _tmp1_;
	self->priv->m_xkb_latin_layouts_length1 = 0;
	self->priv->_m_xkb_latin_layouts_size_ = self->priv->m_xkb_latin_layouts_length1;
	_tmp2_ = g_strdup ("");
	self->priv->m_default_layout = _tmp2_;
	_tmp3_ = g_strdup ("");
	self->priv->m_default_variant = _tmp3_;
	_tmp4_ = g_strdup ("");
	self->priv->m_default_option = _tmp4_;
	self->priv->m_use_xmodmap = TRUE;
	self->ref_count = 1;
}

static void
xkb_layout_finalize (XKBLayout * obj)
{
	XKBLayout * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_XKB_LAYOUT, XKBLayout);
	g_signal_handlers_destroy (self);
	self->priv->m_get_layout_args = (_vala_array_free (self->priv->m_get_layout_args, self->priv->m_get_layout_args_length1, (GDestroyNotify) g_free), NULL);
	self->priv->m_xkb_latin_layouts = (_vala_array_free (self->priv->m_xkb_latin_layouts, self->priv->m_xkb_latin_layouts_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (self->priv->m_default_layout);
	_g_free0 (self->priv->m_default_variant);
	_g_free0 (self->priv->m_default_option);
}

static GType
xkb_layout_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { value_xkb_layout_init, value_xkb_layout_free_value, value_xkb_layout_copy_value, value_xkb_layout_peek_pointer, "p", value_xkb_layout_collect_value, "p", value_xkb_layout_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (XKBLayoutClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) xkb_layout_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (XKBLayout), 0, (GInstanceInitFunc) xkb_layout_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 xkb_layout_type_id;
	xkb_layout_type_id = g_type_register_fundamental (g_type_fundamental_next (), "XKBLayout", &g_define_type_info, &g_define_type_fundamental_info, 0);
	XKBLayout_private_offset = g_type_add_instance_private (xkb_layout_type_id, sizeof (XKBLayoutPrivate));
	return xkb_layout_type_id;
}

GType
xkb_layout_get_type (void)
{
	static volatile gsize xkb_layout_type_id__once = 0;
	if (g_once_init_enter (&xkb_layout_type_id__once)) {
		GType xkb_layout_type_id;
		xkb_layout_type_id = xkb_layout_get_type_once ();
		g_once_init_leave (&xkb_layout_type_id__once, xkb_layout_type_id);
	}
	return xkb_layout_type_id__once;
}

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

void
xkb_layout_unref (gpointer instance)
{
	XKBLayout * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		XKB_LAYOUT_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

