/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.34 from the
 * contents of UniqueApp.xs. Do not edit this file, edit UniqueApp.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "xs/UniqueApp.xs"
#include "unique-perl.h"
#include <gperl_marshal.h>

static void
perl_unique_app_marshall_message_received (
	GClosure *closure,
	GValue *return_value,
	guint n_param_values,
	const GValue *param_values,
	gpointer invocant_hint,
	gpointer marshal_data)
{
	UniqueApp *app;
	gint command;
	const gchar *command_name;

	dGPERL_CLOSURE_MARSHAL_ARGS;
	PERL_UNUSED_VAR (return_value);
	PERL_UNUSED_VAR (n_param_values);
	PERL_UNUSED_VAR (invocant_hint);

	GPERL_CLOSURE_MARSHAL_INIT (closure, marshal_data);

	ENTER;
	SAVETMPS;
	PUSHMARK (SP);

	GPERL_CLOSURE_MARSHAL_PUSH_INSTANCE (param_values);

	app = (UniqueApp *) g_value_get_object (param_values + 0);
	command = g_value_get_int (param_values + 1);
	command_name = (const gchar *) unique_command_to_string (app, command);

	XPUSHs (sv_2mortal (newSVpv (command_name, 0)));
	XPUSHs (sv_2mortal (gperl_sv_from_value (param_values + 2)));
	XPUSHs (sv_2mortal (gperl_sv_from_value (param_values + 3)));

	GPERL_CLOSURE_MARSHAL_PUSH_DATA;

	PUTBACK;

	GPERL_CLOSURE_MARSHAL_CALL (G_SCALAR);

	SPAGAIN;

	if (count != 1) {
		croak ("message-received handlers need to return a single value");
	}

	g_value_set_enum (return_value, SvUniqueResponse (POPs));

	FREETMPS;
	LEAVE;

}


#line 68 "xs/UniqueApp.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 212 "xs/UniqueApp.c"

XS_EUPXS(XS_Gtk2__UniqueApp_new); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Gtk2__UniqueApp_new)
{
    dVAR; dXSARGS;
    dXSI32;
    if (items < 3)
       croak_xs_usage(cv,  "class, name, startup_id, ...");
    {
#line 165 "xs/UniqueApp.xs"
		UniqueApp *app = NULL;

#line 225 "xs/UniqueApp.c"
	UniqueApp_noinc *	RETVAL;
	const gchar *	name;
	const gchar_ornull *	startup_id;

	/* same as SvGChar(), but not in a function */
	sv_utf8_upgrade (ST(1));
	name = (const gchar *)SvPV_nolen (ST(1))
;

	/* same as SvGChar(), but allows undef as NULL */
	if (gperl_sv_is_defined (ST(2))) {
		sv_utf8_upgrade (ST(2));
		startup_id = (const gchar_ornull *)SvPV_nolen (ST(2));
	} else {
		startup_id = NULL;
	}
;
#line 168 "xs/UniqueApp.xs"
		PERL_UNUSED_VAR(ix);

		if (items == 3) {
			app = unique_app_new(name, startup_id);
		}
		else if (items > 3 && (items % 2 == 1)) {
			/* Calling unique_app_new_with_command(), First create a new app with
			   unique_app_new() and then populate the commands one by one with
			   unique_app_add_command().
			 */
			int i;
			app = unique_app_new(name, startup_id);

			for (i = 3; i < items; i += 2) {
				SV *command_name_sv = ST(i);
				SV *command_id_sv = ST(i + 1);
				gchar *command_name = NULL;
				gint command_id;

				if (! looks_like_number(command_id_sv)) {
					g_object_unref(G_OBJECT(app));
					croak(
						"Invalid command_id at position %d, expected a number but got '%s'",
						i,
						SvGChar(command_id_sv)
					);
				}
				command_name = SvGChar(command_name_sv);
				command_id = SvIV(command_id_sv);
				unique_app_add_command(app, command_name, command_id);
			}
		}
		else {
			croak(
				"Usage: Gtk2::UniqueApp->new(name, startup_id)"
				"or Gtk2::UniqueApp->new_with_commands(name, startup_id, @commands)"
			);
		}

		RETVAL = app;

#line 285 "xs/UniqueApp.c"
	{
	    SV * RETVALSV;
	    RETVALSV = newSVUniqueApp_noinc (RETVAL);
	    RETVALSV = sv_2mortal(RETVALSV);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Gtk2__UniqueApp_add_command); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Gtk2__UniqueApp_add_command)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "app, command_name, command_id");
    {
	UniqueApp *	app = SvUniqueApp (ST(0))
;
	const gchar *	command_name;
	gint	command_id = (gint)SvIV(ST(2))
;

	/* same as SvGChar(), but not in a function */
	sv_utf8_upgrade (ST(1));
	command_name = (const gchar *)SvPV_nolen (ST(1))
;

	unique_app_add_command(app, command_name, command_id);
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Gtk2__UniqueApp_watch_window); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Gtk2__UniqueApp_watch_window)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "app, window");
    {
	UniqueApp *	app = SvUniqueApp (ST(0))
;
	GtkWindow *	window = SvGtkWindow (ST(1))
;

	unique_app_watch_window(app, window);
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Gtk2__UniqueApp_is_running); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Gtk2__UniqueApp_is_running)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "app");
    {
	gboolean	RETVAL;
	UniqueApp *	app = SvUniqueApp (ST(0))
;

	RETVAL = unique_app_is_running(app);
	ST(0) = boolSV(RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Gtk2__UniqueApp_send_message_by_name); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Gtk2__UniqueApp_send_message_by_name)
{
    dVAR; dXSARGS;
    dXSI32;
    if (items < 2)
       croak_xs_usage(cv,  "app, command, ...");
    {
#line 311 "xs/UniqueApp.xs"
		UniqueMessageData *message = NULL;
		SV **s = NULL;
		gint command_id = 0;

#line 370 "xs/UniqueApp.c"
	UniqueResponse	RETVAL;
	UniqueApp *	app = SvUniqueApp (ST(0))
;
	SV *	command = ST(1)
;
#line 317 "xs/UniqueApp.xs"
		switch (ix) {
			case 0:
				{
					gchar *command_name = SvGChar(command);
					command_id = unique_command_from_string(app, command_name);
					if (command_id == 0) {
							croak("Command '%s' isn't registered with the application", command_name);
					}
				}
			break;

			case 1:
				{
					command_id = (gint) SvIV(command);
				}
			break;

			default:
				croak("Method called with the wrong name");
		}

		if (items == 4) {
			SV *sv_data;
			gchar *type;

			message = unique_message_data_new();
			type = SvGChar(ST(2));
			sv_data = ST(3);

			if (g_strcmp0(type, "data") == 0) {
				SV *sv;
				STRLEN length;
				char *data;

				data = SvPV(sv_data, length);
				unique_message_data_set(message, data, length);
			}
			else if (g_strcmp0(type, "text") == 0) {
				STRLEN length;
				char *text;

				length = sv_len(sv_data);
				text = SvGChar(sv_data);
				unique_message_data_set_text(message, text, length);
			}
			else if (g_strcmp0(type, "filename") == 0) {
				SV *sv;
				char *filename;

				filename = SvGChar(sv_data);
				unique_message_data_set_filename(message, filename);
			}
			else if (g_strcmp0(type, "uris") == 0) {
				gchar **uris = NULL;
				gsize length;
				AV *av = NULL;
				int i;

				if (SvTYPE(SvRV(sv_data)) != SVt_PVAV) {
					unique_message_data_free(message);
					croak("Value for the type 'uris' must be an array ref");
				}

				/* Convert the Perl array into a C array of strings */
				av = (AV*) SvRV(sv_data);
				length = av_len(av) + 2; /* last index + extra NULL padding */

				uris = g_new0(gchar *, length);
				for (i = 0; i < length - 1; ++i) {
					SV **uri_sv = av_fetch(av, i, FALSE);
					uris[i] = SvGChar(*uri_sv);
				}
				uris[length - 1] = NULL;

				unique_message_data_set_uris(message, uris);
				g_free(uris);
			}
			else {
				unique_message_data_free(message);
				croak("Parameter 'type' must be: 'data', 'text', 'filename' or 'uris'; got %s", type);
			}
		}
		else if (items == 2) {
			message = NULL;
		}
		else {
			croak(
				"Usage: $app->send_message($id, $type => $data)"
				" or $app->send_message($id, uris => [])"
				" or $app->send_message($id)"
			);
		}

		RETVAL = unique_app_send_message(app, command_id, message);

		if (message) {
			unique_message_data_free(message);
		}

#line 476 "xs/UniqueApp.c"
	{
	    SV * RETVALSV;
	    RETVALSV = newSVUniqueResponse (RETVAL);
	    RETVALSV = sv_2mortal(RETVALSV);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_Gtk2__UniqueApp); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Gtk2__UniqueApp)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if (PERL_REVISION == 5 && PERL_VERSION < 9)
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

    {
        CV * cv;

        cv = newXS_deffile("Gtk2::UniqueApp::new", XS_Gtk2__UniqueApp_new);
        XSANY.any_i32 = 0;
        cv = newXS_deffile("Gtk2::UniqueApp::new_with_commands", XS_Gtk2__UniqueApp_new);
        XSANY.any_i32 = 1;
        newXS_deffile("Gtk2::UniqueApp::add_command", XS_Gtk2__UniqueApp_add_command);
        newXS_deffile("Gtk2::UniqueApp::watch_window", XS_Gtk2__UniqueApp_watch_window);
        newXS_deffile("Gtk2::UniqueApp::is_running", XS_Gtk2__UniqueApp_is_running);
        cv = newXS_deffile("Gtk2::UniqueApp::send_message", XS_Gtk2__UniqueApp_send_message_by_name);
        XSANY.any_i32 = 1;
        cv = newXS_deffile("Gtk2::UniqueApp::send_message_by_name", XS_Gtk2__UniqueApp_send_message_by_name);
        XSANY.any_i32 = 0;
    }

    /* Initialisation Section */

#line 61 "xs/UniqueApp.xs"
	gperl_signal_set_marshaller_for (
		UNIQUE_TYPE_APP,
		"message-received",
		perl_unique_app_marshall_message_received
	);

#line 540 "xs/UniqueApp.c"

    /* End of Initialisation Section */

#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

