/*
 * Disc.c
 *
 * Copyright (c) 2007  Vreixo Formoso
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See COPYING file for details.
 */

#include "org_pykix_libburnia_libburn_Disc.h"
#include "BindingsUtil.h"

#include "libburn.h"


/*
 * Class:     org_pykix_libburnia_libburn_Disc
 * Method:    burn_disc_create
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL 
Java_org_pykix_libburnia_libburn_Disc_burn_1disc_1create
(
	JNIEnv *env, jclass cls
)
{
	return (jlong) burn_disc_create();
}

/*
 * Class:     org_pykix_libburnia_libburn_Disc
 * Method:    burn_disc_free
 * Signature: (J)V
 */
JNIEXPORT void JNICALL 
Java_org_pykix_libburnia_libburn_Disc_burn_1disc_1free
(
	JNIEnv *env, jclass cls, jlong d
)
{
	burn_disc_free( (struct burn_disc *) d);
}

/*
 * Class:     org_pykix_libburnia_libburn_Disc
 * Method:    burn_disc_add_session
 * Signature: (JJI)Z
 */
JNIEXPORT jboolean JNICALL 
Java_org_pykix_libburnia_libburn_Disc_burn_1disc_1add_1session
(
	JNIEnv *env, jclass cls, jlong d, jlong s, jint pos
)
{
	return burn_disc_add_session( (struct burn_disc *) d, 
		(struct burn_session *) s, pos);
}

/*
 * Class:     org_pykix_libburnia_libburn_Disc
 * Method:    burn_disc_remove_session
 * Signature: (JJ)Z
 */
JNIEXPORT jboolean JNICALL 
Java_org_pykix_libburnia_libburn_Disc_burn_1disc_1remove_1session
(
	JNIEnv *env, jclass cls, jlong d, jlong s
)
{
	return burn_disc_remove_session( (struct burn_disc *) d, (struct burn_session *) s);
}

/*
 * Class:     org_pykix_libburnia_libburn_Disc
 * Method:    burn_disc_get_sessions
 * Signature: (J)[J
 */
JNIEXPORT jlongArray JNICALL 
Java_org_pykix_libburnia_libburn_Disc_burn_1disc_1get_1sessions
(
	JNIEnv *env, jclass cls, jlong d
)
{
	int num;
	struct burn_session **sessions;
	int i;
	
	sessions = burn_disc_get_sessions( (struct burn_disc *) d, &num);

	/* create C array to store the pointers */
	jlong cresult[num];

	for ( i = 0; i < num; ++i ) {
		struct burn_session *session = sessions[i];
		cresult[i] = (jlong) session;
	}

	/* move the pointer to a Java array */
	jlongArray result = (*env)->NewLongArray(env, num);
	if ( result == NULL) {
		return NULL; /* exception already thrown */
	}
	(*env)->SetLongArrayRegion(env, result, 0, num, cresult);

	return result;
}

/*
 * Class:     org_pykix_libburnia_libburn_Disc
 * Method:    burn_disc_get_sectors
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL 
Java_org_pykix_libburnia_libburn_Disc_burn_1disc_1get_1sectors
(
	JNIEnv *env, jclass cls, jlong d
)
{
	return burn_disc_get_sectors( (struct burn_disc *) d);
}
