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

/*///////////////////////////////////////////////////////////////////////*/
/* Copyright 2011-2021 Simon Schneegans*/
/**/
/* Permission is hereby granted, free of charge, to any person obtaining*/
/* a copy of this software and associated documentation files (the*/
/* "Software"), to deal in the Software without restriction, including*/
/* without limitation the rights to use, copy, modify, merge, publish,*/
/* distribute, sublicense, and/or sell copies of the Software, and to*/
/* permit persons to whom the Software is furnished to do so, subject to*/
/* the following conditions:*/
/* */
/* The above copyright notice and this permission notice shall be*/
/* included in all copies or substantial portions of the Software.*/
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,*/
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF*/
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY*/
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,*/
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE*/
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/
/*///////////////////////////////////////////////////////////////////////*/

#include <glib-object.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <string.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
#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 GNOME_PIE_TYPE_FOCUS_GRABBER (gnome_pie_focus_grabber_get_type ())
#define GNOME_PIE_FOCUS_GRABBER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GNOME_PIE_TYPE_FOCUS_GRABBER, GnomePieFocusGrabber))
#define GNOME_PIE_FOCUS_GRABBER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GNOME_PIE_TYPE_FOCUS_GRABBER, GnomePieFocusGrabberClass))
#define GNOME_PIE_IS_FOCUS_GRABBER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GNOME_PIE_TYPE_FOCUS_GRABBER))
#define GNOME_PIE_IS_FOCUS_GRABBER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNOME_PIE_TYPE_FOCUS_GRABBER))
#define GNOME_PIE_FOCUS_GRABBER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GNOME_PIE_TYPE_FOCUS_GRABBER, GnomePieFocusGrabberClass))

typedef struct _GnomePieFocusGrabber GnomePieFocusGrabber;
typedef struct _GnomePieFocusGrabberClass GnomePieFocusGrabberClass;
typedef struct _GnomePieFocusGrabberPrivate GnomePieFocusGrabberPrivate;
enum  {
	GNOME_PIE_FOCUS_GRABBER_0_PROPERTY,
	GNOME_PIE_FOCUS_GRABBER_NUM_PROPERTIES
};
static GParamSpec* gnome_pie_focus_grabber_properties[GNOME_PIE_FOCUS_GRABBER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block25Data Block25Data;
typedef struct _Block26Data Block26Data;

struct _GnomePieFocusGrabber {
	GObject parent_instance;
	GnomePieFocusGrabberPrivate * priv;
};

struct _GnomePieFocusGrabberClass {
	GObjectClass parent_class;
};

struct _Block25Data {
	int _ref_count_;
	GdkWindow* window;
};

struct _Block26Data {
	int _ref_count_;
	Block25Data * _data25_;
	gint i;
};

static gpointer gnome_pie_focus_grabber_parent_class = NULL;

VALA_EXTERN GType gnome_pie_focus_grabber_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (GnomePieFocusGrabber, g_object_unref)
VALA_EXTERN void gnome_pie_focus_grabber_grab (GdkWindow* window);
static Block25Data* block25_data_ref (Block25Data* _data25_);
static void block25_data_unref (void * _userdata_);
static gboolean gnome_pie_focus_grabber_try_grab_window (GdkWindow* window);
static Block26Data* block26_data_ref (Block26Data* _data26_);
static void block26_data_unref (void * _userdata_);
static gboolean ___lambda8_ (Block26Data* _data26_);
static gboolean ____lambda8__gsource_func (gpointer self);
VALA_EXTERN void gnome_pie_focus_grabber_ungrab (void);
VALA_EXTERN GnomePieFocusGrabber* gnome_pie_focus_grabber_new (void);
VALA_EXTERN GnomePieFocusGrabber* gnome_pie_focus_grabber_construct (GType object_type);
static GType gnome_pie_focus_grabber_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static Block25Data*
block25_data_ref (Block25Data* _data25_)
{
	g_atomic_int_inc (&_data25_->_ref_count_);
	return _data25_;
}

static void
block25_data_unref (void * _userdata_)
{
	Block25Data* _data25_;
	_data25_ = (Block25Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data25_->_ref_count_)) {
		_g_object_unref0 (_data25_->window);
		g_slice_free (Block25Data, _data25_);
	}
}

static Block26Data*
block26_data_ref (Block26Data* _data26_)
{
	g_atomic_int_inc (&_data26_->_ref_count_);
	return _data26_;
}

static void
block26_data_unref (void * _userdata_)
{
	Block26Data* _data26_;
	_data26_ = (Block26Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data26_->_ref_count_)) {
		block25_data_unref (_data26_->_data25_);
		_data26_->_data25_ = NULL;
		g_slice_free (Block26Data, _data26_);
	}
}

static gboolean
___lambda8_ (Block26Data* _data26_)
{
	Block25Data* _data25_;
	gint _tmp0_;
	gboolean result;
	_data25_ = _data26_->_data25_;
	_data26_->i = _data26_->i + 1;
	_tmp0_ = _data26_->i;
	if (_tmp0_ >= 100) {
		result = FALSE;
		return result;
	}
	result = !gnome_pie_focus_grabber_try_grab_window (_data25_->window);
	return result;
}

static gboolean
____lambda8__gsource_func (gpointer self)
{
	gboolean result;
	result = ___lambda8_ (self);
	return result;
}

void
gnome_pie_focus_grabber_grab (GdkWindow* window)
{
	Block25Data* _data25_;
	GdkWindow* _tmp0_;
	g_return_if_fail (window != NULL);
	_data25_ = g_slice_new0 (Block25Data);
	_data25_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (window);
	_g_object_unref0 (_data25_->window);
	_data25_->window = _tmp0_;
	gdk_window_raise (_data25_->window);
	gdk_window_focus (_data25_->window, (guint32) GDK_CURRENT_TIME);
	if (!gnome_pie_focus_grabber_try_grab_window (_data25_->window)) {
		Block26Data* _data26_;
		_data26_ = g_slice_new0 (Block26Data);
		_data26_->_ref_count_ = 1;
		_data26_->_data25_ = block25_data_ref (_data25_);
		_data26_->i = 0;
		g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) 100, ____lambda8__gsource_func, block26_data_ref (_data26_), block26_data_unref);
		block26_data_unref (_data26_);
		_data26_ = NULL;
	}
	block25_data_unref (_data25_);
	_data25_ = NULL;
}

void
gnome_pie_focus_grabber_ungrab (void)
{
	GdkSeat* seat = NULL;
	GdkDisplay* _tmp0_;
	GdkSeat* _tmp1_;
	GdkSeat* _tmp2_;
	_tmp0_ = gdk_display_get_default ();
	_tmp1_ = gdk_display_get_default_seat (_tmp0_);
	_tmp2_ = _g_object_ref0 (_tmp1_);
	seat = _tmp2_;
	gdk_seat_ungrab (seat);
	_g_object_unref0 (seat);
}

static gboolean
gnome_pie_focus_grabber_try_grab_window (GdkWindow* window)
{
	GdkSeat* seat = NULL;
	GdkDisplay* _tmp0_;
	GdkSeat* _tmp1_;
	GdkSeat* _tmp2_;
	GdkSeatCapabilities caps = 0U;
	GdkGrabStatus _result_ = 0;
	GdkSeat* _tmp3_;
	gboolean result;
	g_return_val_if_fail (window != NULL, FALSE);
	if (!gdk_window_is_viewable (window)) {
		result = FALSE;
		return result;
	}
	_tmp0_ = gdk_display_get_default ();
	_tmp1_ = gdk_display_get_default_seat (_tmp0_);
	_tmp2_ = _g_object_ref0 (_tmp1_);
	seat = _tmp2_;
	caps = GDK_SEAT_CAPABILITY_POINTER | GDK_SEAT_CAPABILITY_KEYBOARD;
	_tmp3_ = seat;
	_result_ = gdk_seat_grab (_tmp3_, window, caps, TRUE, NULL, NULL, NULL, NULL);
	if (_result_ != GDK_GRAB_SUCCESS) {
		gdk_window_show (window);
	}
	result = _result_ == GDK_GRAB_SUCCESS;
	_g_object_unref0 (seat);
	return result;
}

GnomePieFocusGrabber*
gnome_pie_focus_grabber_construct (GType object_type)
{
	GnomePieFocusGrabber * self = NULL;
	self = (GnomePieFocusGrabber*) g_object_new (object_type, NULL);
	return self;
}

GnomePieFocusGrabber*
gnome_pie_focus_grabber_new (void)
{
	return gnome_pie_focus_grabber_construct (GNOME_PIE_TYPE_FOCUS_GRABBER);
}

static void
gnome_pie_focus_grabber_class_init (GnomePieFocusGrabberClass * klass,
                                    gpointer klass_data)
{
	gnome_pie_focus_grabber_parent_class = g_type_class_peek_parent (klass);
}

static void
gnome_pie_focus_grabber_instance_init (GnomePieFocusGrabber * self,
                                       gpointer klass)
{
}

static GType
gnome_pie_focus_grabber_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GnomePieFocusGrabberClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gnome_pie_focus_grabber_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GnomePieFocusGrabber), 0, (GInstanceInitFunc) gnome_pie_focus_grabber_instance_init, NULL };
	GType gnome_pie_focus_grabber_type_id;
	gnome_pie_focus_grabber_type_id = g_type_register_static (G_TYPE_OBJECT, "GnomePieFocusGrabber", &g_define_type_info, 0);
	return gnome_pie_focus_grabber_type_id;
}

GType
gnome_pie_focus_grabber_get_type (void)
{
	static volatile gsize gnome_pie_focus_grabber_type_id__once = 0;
	if (g_once_init_enter (&gnome_pie_focus_grabber_type_id__once)) {
		GType gnome_pie_focus_grabber_type_id;
		gnome_pie_focus_grabber_type_id = gnome_pie_focus_grabber_get_type_once ();
		g_once_init_leave (&gnome_pie_focus_grabber_type_id__once, gnome_pie_focus_grabber_type_id);
	}
	return gnome_pie_focus_grabber_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;
}

