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

#include <gtk/gtk.h>
#include <glib-object.h>
#include <glib.h>
#include <float.h>
#include <math.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libsoup/soup.h>
#include <stdlib.h>
#include <string.h>
#include "qrencode.h"
#include <cairo-gobject.h>
#include <gdk/gdk.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 PDFPC_WINDOW_TYPE_QR_CODE (pdfpc_window_qr_code_get_type ())
#define PDFPC_WINDOW_QR_CODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PDFPC_WINDOW_TYPE_QR_CODE, pdfpcWindowQRCode))
#define PDFPC_WINDOW_QR_CODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDFPC_WINDOW_TYPE_QR_CODE, pdfpcWindowQRCodeClass))
#define PDFPC_WINDOW_IS_QR_CODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PDFPC_WINDOW_TYPE_QR_CODE))
#define PDFPC_WINDOW_IS_QR_CODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDFPC_WINDOW_TYPE_QR_CODE))
#define PDFPC_WINDOW_QR_CODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDFPC_WINDOW_TYPE_QR_CODE, pdfpcWindowQRCodeClass))

typedef struct _pdfpcWindowQRCode pdfpcWindowQRCode;
typedef struct _pdfpcWindowQRCodeClass pdfpcWindowQRCodeClass;
typedef struct _pdfpcWindowQRCodePrivate pdfpcWindowQRCodePrivate;
enum  {
	PDFPC_WINDOW_QR_CODE_0_PROPERTY,
	PDFPC_WINDOW_QR_CODE_NUM_PROPERTIES
};
static GParamSpec* pdfpc_window_qr_code_properties[PDFPC_WINDOW_QR_CODE_NUM_PROPERTIES];

#define PDFPC_TYPE_PRESENTATION_CONTROLLER (pdfpc_presentation_controller_get_type ())
#define PDFPC_PRESENTATION_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PDFPC_TYPE_PRESENTATION_CONTROLLER, pdfpcPresentationController))
#define PDFPC_PRESENTATION_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDFPC_TYPE_PRESENTATION_CONTROLLER, pdfpcPresentationControllerClass))
#define PDFPC_IS_PRESENTATION_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PDFPC_TYPE_PRESENTATION_CONTROLLER))
#define PDFPC_IS_PRESENTATION_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDFPC_TYPE_PRESENTATION_CONTROLLER))
#define PDFPC_PRESENTATION_CONTROLLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDFPC_TYPE_PRESENTATION_CONTROLLER, pdfpcPresentationControllerClass))

typedef struct _pdfpcPresentationController pdfpcPresentationController;
typedef struct _pdfpcPresentationControllerClass pdfpcPresentationControllerClass;
typedef struct _Block14Data Block14Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define PDFPC_TYPE_REST_SERVER (pdfpc_rest_server_get_type ())
#define PDFPC_REST_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PDFPC_TYPE_REST_SERVER, pdfpcRestServer))
#define PDFPC_REST_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PDFPC_TYPE_REST_SERVER, pdfpcRestServerClass))
#define PDFPC_IS_REST_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PDFPC_TYPE_REST_SERVER))
#define PDFPC_IS_REST_SERVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PDFPC_TYPE_REST_SERVER))
#define PDFPC_REST_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PDFPC_TYPE_REST_SERVER, pdfpcRestServerClass))

typedef struct _pdfpcRestServer pdfpcRestServer;
typedef struct _pdfpcRestServerClass pdfpcRestServerClass;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _QRcode_free0(var) ((var == NULL) ? NULL : (var = (QRcode_free (var), NULL)))

struct _pdfpcWindowQRCode {
	GtkDrawingArea parent_instance;
	pdfpcWindowQRCodePrivate * priv;
};

struct _pdfpcWindowQRCodeClass {
	GtkDrawingAreaClass parent_class;
};

struct _Block14Data {
	int _ref_count_;
	pdfpcWindowQRCode* self;
	gint scale;
	gint swidth;
	GdkPixbuf* pixbuf;
};

static gpointer pdfpc_window_qr_code_parent_class = NULL;

VALA_EXTERN GType pdfpc_window_qr_code_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (pdfpcWindowQRCode, g_object_unref)
VALA_EXTERN GType pdfpc_presentation_controller_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (pdfpcPresentationController, g_object_unref)
VALA_EXTERN pdfpcWindowQRCode* pdfpc_window_qr_code_new (pdfpcPresentationController* controller,
                                             gdouble size);
VALA_EXTERN pdfpcWindowQRCode* pdfpc_window_qr_code_construct (GType object_type,
                                                   pdfpcPresentationController* controller,
                                                   gdouble size);
static Block14Data* block14_data_ref (Block14Data* _data14_);
static void block14_data_unref (void * _userdata_);
VALA_EXTERN GType pdfpc_rest_server_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (pdfpcRestServer, g_object_unref)
VALA_EXTERN pdfpcRestServer* pdfpc_presentation_controller_get_rest_server (pdfpcPresentationController* self);
VALA_EXTERN gchar* pdfpc_rest_server_get_connection_info (pdfpcRestServer* self);
static guint8* _vala_array_dup2 (guint8* self,
                          gssize length);
static void _g_free_gdk_pixbuf_destroy_notify (guint8* pixels,
                                        gpointer self);
static gboolean __lambda38_ (Block14Data* _data14_,
                      GtkWidget* w,
                      cairo_t* cr);
static gboolean ___lambda38__gtk_widget_draw (GtkWidget* _sender,
                                       cairo_t* cr,
                                       gpointer self);
static GType pdfpc_window_qr_code_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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_)) {
		pdfpcWindowQRCode* self;
		self = _data14_->self;
		_g_object_unref0 (_data14_->pixbuf);
		_g_object_unref0 (self);
		g_slice_free (Block14Data, _data14_);
	}
}

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

static guint8*
_vala_array_dup2 (guint8* self,
                  gssize length)
{
	if (length > 0) {
		return _vala_memdup2 (self, length * sizeof (guint8));
	}
	return NULL;
}

static void
_g_free_gdk_pixbuf_destroy_notify (guint8* pixels,
                                   gpointer self)
{
	g_free (pixels);
}

static gboolean
__lambda38_ (Block14Data* _data14_,
             GtkWidget* w,
             cairo_t* cr)
{
	pdfpcWindowQRCode* self;
	GdkPixbuf* _tmp0_;
	gboolean result;
	self = _data14_->self;
	g_return_val_if_fail (w != NULL, FALSE);
	g_return_val_if_fail (cr != NULL, FALSE);
	cairo_set_source_rgb (cr, (gdouble) 255, (gdouble) 255, (gdouble) 255);
	cairo_rectangle (cr, (gdouble) 0, (gdouble) 0, (gdouble) (_data14_->swidth + (2 * _data14_->scale)), (gdouble) (_data14_->swidth + (2 * _data14_->scale)));
	cairo_fill (cr);
	_tmp0_ = _data14_->pixbuf;
	gdk_cairo_set_source_pixbuf (cr, _tmp0_, (gdouble) _data14_->scale, (gdouble) _data14_->scale);
	cairo_paint (cr);
	result = TRUE;
	return result;
}

static gboolean
___lambda38__gtk_widget_draw (GtkWidget* _sender,
                              cairo_t* cr,
                              gpointer self)
{
	gboolean result;
	result = __lambda38_ (self, _sender, cr);
	return result;
}

pdfpcWindowQRCode*
pdfpc_window_qr_code_construct (GType object_type,
                                pdfpcPresentationController* controller,
                                gdouble size)
{
	pdfpcWindowQRCode * self = NULL;
	Block14Data* _data14_;
	pdfpcRestServer* rs = NULL;
	pdfpcRestServer* _tmp0_;
	pdfpcRestServer* _tmp1_;
	pdfpcRestServer* _tmp2_;
	pdfpcRestServer* _tmp3_;
	gchar* conn_str = NULL;
	pdfpcRestServer* _tmp4_;
	gchar* _tmp5_;
	const gchar* _tmp6_;
	GtkStyleContext* _tmp7_;
	QRcode* qrcode = NULL;
	const gchar* _tmp8_;
	QRcode* _tmp9_;
	gint width = 0;
	QRcode* _tmp10_;
	guint8* buf = NULL;
	guint8* _tmp11_;
	gint buf_length1;
	gint _buf_size_;
	guint8* _tmp26_;
	gint _tmp26__length1;
	guint8* _tmp27_;
	gint _tmp27__length1;
	GdkPixbuf* _tmp28_;
	g_return_val_if_fail (controller != NULL, NULL);
	_data14_ = g_slice_new0 (Block14Data);
	_data14_->_ref_count_ = 1;
	self = (pdfpcWindowQRCode*) g_object_new (object_type, NULL);
	_data14_->self = g_object_ref (self);
	_tmp0_ = pdfpc_presentation_controller_get_rest_server (controller);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	rs = _tmp2_;
	_tmp3_ = rs;
	if (_tmp3_ == NULL) {
		_g_object_unref0 (rs);
		block14_data_unref (_data14_);
		_data14_ = NULL;
		return self;
	}
	_tmp4_ = rs;
	_tmp5_ = pdfpc_rest_server_get_connection_info (_tmp4_);
	conn_str = _tmp5_;
	_tmp6_ = conn_str;
	if (_tmp6_ == NULL) {
		_g_free0 (conn_str);
		_g_object_unref0 (rs);
		block14_data_unref (_data14_);
		_data14_ = NULL;
		return self;
	}
	_tmp7_ = gtk_widget_get_style_context ((GtkWidget*) self);
	gtk_style_context_add_class (_tmp7_, "qrcode");
	gtk_widget_set_halign ((GtkWidget*) self, GTK_ALIGN_CENTER);
	gtk_widget_set_valign ((GtkWidget*) self, GTK_ALIGN_CENTER);
	gtk_widget_set_can_focus ((GtkWidget*) self, TRUE);
	_tmp8_ = conn_str;
	_tmp9_ = QRcode_encodeString (_tmp8_, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
	qrcode = _tmp9_;
	_tmp10_ = qrcode;
	width = _tmp10_->width;
	_data14_->scale = (gint) floor (size / (width + 2));
	if (_data14_->scale < 1) {
		_data14_->scale = 1;
	}
	_data14_->swidth = _data14_->scale * width;
	gtk_widget_set_size_request ((GtkWidget*) self, _data14_->swidth + (2 * _data14_->scale), _data14_->swidth + (2 * _data14_->scale));
	_tmp11_ = g_new0 (guint8, (3 * _data14_->swidth) * _data14_->swidth);
	buf = _tmp11_;
	buf_length1 = (3 * _data14_->swidth) * _data14_->swidth;
	_buf_size_ = buf_length1;
	{
		gint ix = 0;
		ix = 0;
		{
			gboolean _tmp12_ = FALSE;
			_tmp12_ = TRUE;
			while (TRUE) {
				if (!_tmp12_) {
					gint _tmp13_;
					_tmp13_ = ix;
					ix = _tmp13_ + 1;
				}
				_tmp12_ = FALSE;
				if (!(ix < width)) {
					break;
				}
				{
					gint iy = 0;
					iy = 0;
					{
						gboolean _tmp14_ = FALSE;
						_tmp14_ = TRUE;
						while (TRUE) {
							gint idx = 0;
							guint8 bit = 0U;
							QRcode* _tmp16_;
							guint8* _tmp17_;
							gint _tmp17__length1;
							guint8 _tmp18_;
							gboolean black = FALSE;
							guint8 byte = 0U;
							if (!_tmp14_) {
								gint _tmp15_;
								_tmp15_ = iy;
								iy = _tmp15_ + 1;
							}
							_tmp14_ = FALSE;
							if (!(iy < width)) {
								break;
							}
							idx = (iy * width) + ix;
							_tmp16_ = qrcode;
							_tmp17_ = _tmp16_->data;
							_tmp17__length1 = -1;
							_tmp18_ = _tmp17_[idx];
							bit = _tmp18_;
							black = ((gint) (bit & 1)) == 1;
							if (black) {
								byte = (guint8) 0x00;
							} else {
								byte = (guint8) 0xff;
							}
							{
								gint sx = 0;
								sx = 0;
								{
									gboolean _tmp19_ = FALSE;
									_tmp19_ = TRUE;
									while (TRUE) {
										if (!_tmp19_) {
											gint _tmp20_;
											_tmp20_ = sx;
											sx = _tmp20_ + 1;
										}
										_tmp19_ = FALSE;
										if (!(sx < _data14_->scale)) {
											break;
										}
										{
											gint sy = 0;
											sy = 0;
											{
												gboolean _tmp21_ = FALSE;
												_tmp21_ = TRUE;
												while (TRUE) {
													guint8* _tmp23_;
													gint _tmp23__length1;
													guint8* _tmp24_;
													gint _tmp24__length1;
													guint8* _tmp25_;
													gint _tmp25__length1;
													if (!_tmp21_) {
														gint _tmp22_;
														_tmp22_ = sy;
														sy = _tmp22_ + 1;
													}
													_tmp21_ = FALSE;
													if (!(sy < _data14_->scale)) {
														break;
													}
													idx = ((((_data14_->scale * iy) + sy) * _data14_->swidth) + (_data14_->scale * ix)) + sx;
													_tmp23_ = buf;
													_tmp23__length1 = buf_length1;
													_tmp23_[3 * idx] = byte;
													_tmp24_ = buf;
													_tmp24__length1 = buf_length1;
													_tmp24_[(3 * idx) + 1] = byte;
													_tmp25_ = buf;
													_tmp25__length1 = buf_length1;
													_tmp25_[(3 * idx) + 2] = byte;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	_tmp26_ = buf;
	_tmp26__length1 = buf_length1;
	_tmp27_ = (_tmp26_ != NULL) ? _vala_array_dup2 (_tmp26_, _tmp26__length1) : _tmp26_;
	_tmp27__length1 = _tmp26__length1;
	_tmp28_ = gdk_pixbuf_new_from_data (_tmp27_, GDK_COLORSPACE_RGB, FALSE, 8, _data14_->swidth, _data14_->swidth, 3 * _data14_->swidth, _g_free_gdk_pixbuf_destroy_notify, NULL);
	_data14_->pixbuf = _tmp28_;
	g_signal_connect_data ((GtkWidget*) self, "draw", (GCallback) ___lambda38__gtk_widget_draw, block14_data_ref (_data14_), (GClosureNotify) block14_data_unref, 0);
	buf = (g_free (buf), NULL);
	_QRcode_free0 (qrcode);
	_g_free0 (conn_str);
	_g_object_unref0 (rs);
	block14_data_unref (_data14_);
	_data14_ = NULL;
	return self;
}

pdfpcWindowQRCode*
pdfpc_window_qr_code_new (pdfpcPresentationController* controller,
                          gdouble size)
{
	return pdfpc_window_qr_code_construct (PDFPC_WINDOW_TYPE_QR_CODE, controller, size);
}

static void
pdfpc_window_qr_code_class_init (pdfpcWindowQRCodeClass * klass,
                                 gpointer klass_data)
{
	pdfpc_window_qr_code_parent_class = g_type_class_peek_parent (klass);
}

static void
pdfpc_window_qr_code_instance_init (pdfpcWindowQRCode * self,
                                    gpointer klass)
{
}

/**
     * Window showing QR code of the connection properties
     */
static GType
pdfpc_window_qr_code_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (pdfpcWindowQRCodeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pdfpc_window_qr_code_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (pdfpcWindowQRCode), 0, (GInstanceInitFunc) pdfpc_window_qr_code_instance_init, NULL };
	GType pdfpc_window_qr_code_type_id;
	pdfpc_window_qr_code_type_id = g_type_register_static (gtk_drawing_area_get_type (), "pdfpcWindowQRCode", &g_define_type_info, 0);
	return pdfpc_window_qr_code_type_id;
}

GType
pdfpc_window_qr_code_get_type (void)
{
	static volatile gsize pdfpc_window_qr_code_type_id__once = 0;
	if (g_once_init_enter (&pdfpc_window_qr_code_type_id__once)) {
		GType pdfpc_window_qr_code_type_id;
		pdfpc_window_qr_code_type_id = pdfpc_window_qr_code_get_type_once ();
		g_once_init_leave (&pdfpc_window_qr_code_type_id__once, pdfpc_window_qr_code_type_id);
	}
	return pdfpc_window_qr_code_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;
}

