/*
 * This file is part of roccat-tools.
 *
 * roccat-tools 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.
 *
 * roccat-tools 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include "roccat/roccat.h"
#include "roccat_macro_thread.h"
#include <unistd.h>
#include <linux/input.h>

struct _RoccatMacroThreads {
	RoccatMacroThread **threads;
	guint size;
};

static void roccat_macro_threads_unref_thread(RoccatMacroThreads *macro_threads, guint index) {
	g_assert(index < macro_threads->size);
	
	g_object_unref(macro_threads->threads[index]);
	macro_threads->threads[index] = NULL;
}

RoccatMacroThreads *roccat_macro_threads_new(guint size) {
	RoccatMacroThreads *result;

	result = g_malloc0(sizeof(RoccatMacroThreads));

	result->size = size;
	result->threads = g_malloc0(size * sizeof(RoccatMacroThread*));

	return result;
}

void roccat_macro_threads_free(RoccatMacroThreads *macro_threads) {
	guint i;

	for (i = 0; i < macro_threads->size; ++i) {
		if (macro_threads->threads[i]) {
			/* TODO cancel yes or no? */
			roccat_macro_thread_set_cancelled(macro_threads->threads[i]);
			roccat_macro_threads_unref_thread(macro_threads, i);
		}
	}

	g_free(macro_threads->threads);
	g_free(macro_threads);
}

void roccat_play_macro_threaded(RoccatMacroThreads *macro_threads, guint index, int kbd_file, int mouse_file, RoccatMacro const *macro) {
	g_assert(index < macro_threads->size);

	/* if there is a macro still playing, just stop it */
	if (macro_threads->threads[index] && roccat_macro_thread_get_running(macro_threads->threads[index])) {
		roccat_macro_thread_set_cancelled(macro_threads->threads[index]);
		
		/* allow for early finalization */
		roccat_macro_threads_unref_thread(macro_threads, index);
		return;
	}
	
	if (macro_threads->threads[index])
		roccat_macro_threads_unref_thread(macro_threads, index);
	
	macro_threads->threads[index] = roccat_macro_thread_new(kbd_file, mouse_file, macro);

	/* IDEA invent uncancellable, multiexec macro option
	 * call roccat_macro_threads_unref_thread immediatly
	 */
}

void roccat_input_event_write_with_sync(int fd, int code, int value) {
	struct input_event event;
	int written;

	if (fd == -1)
		return;

	event.type = EV_KEY;
	event.code = code;
	event.value = value;

	written = write(fd, (const void*) &event, sizeof(event));
	if (written != sizeof(event))
		g_warning("error with event %d", written);

	event.type = EV_SYN;
	event.code = SYN_REPORT;
	event.value = 0;

	written = write(fd, (const void*) &event, sizeof(event));
	if (written != sizeof(event))
		g_warning("error with sync event %d", written);
}

void roccat_input_event_write_single(int fd, int code) {
	roccat_input_event_write_with_sync(fd, code, 1);
	roccat_input_event_write_with_sync(fd, code, 0);
}
