/*
 * 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/koneplus.h"

static gchar const * const sensor_name = "sensor";

KoneplusSensor *koneplus_sensor_read(RoccatDevice *koneplus, GError **error) {
	return (KoneplusSensor *)roccat_device_read(koneplus, sensor_name, sizeof(KoneplusSensor), error);
}

gboolean koneplus_sensor_write(RoccatDevice *koneplus, KoneplusSensor const *data, GError **error) {
	return roccat_device_write(koneplus, sensor_name, (char const *)data, sizeof(KoneplusSensor), error);
}

gboolean koneplus_sensor_tcu_test(RoccatDevice *koneplus, guint dcu, guint median, GError **error) {
	KoneplusSensor sensor;

	sensor.command = KONEPLUS_COMMAND_SENSOR;
	sensor.size = sizeof(KoneplusSensor);
	sensor.dcu = dcu;
	sensor.tcu = KONEPLUS_SENSOR_TCU_ON;
	sensor.median = median;
	sensor.action = KONEPLUS_SENSOR_ACTION_CANCEL;

	return koneplus_sensor_write(koneplus, &sensor, error);
}

gboolean koneplus_sensor_tcu_cancel(RoccatDevice *koneplus, guint dcu, GError **error) {
	return koneplus_sensor_tcu_test(koneplus, dcu, 0, error);
}

gboolean koneplus_sensor_tcu_accept(RoccatDevice *koneplus, guint dcu, guint median, GError **error) {
	KoneplusSensor sensor;

	sensor.command = KONEPLUS_COMMAND_SENSOR;
	sensor.size = sizeof(KoneplusSensor);
	sensor.dcu = dcu;
	sensor.tcu = KONEPLUS_SENSOR_TCU_ON;
	sensor.median = median;
	sensor.action = KONEPLUS_SENSOR_ACTION_ACCEPT;

	return koneplus_sensor_write(koneplus, &sensor, error);
}

gboolean koneplus_sensor_tcu_off(RoccatDevice *koneplus, guint dcu, GError **error) {
	KoneplusSensor sensor;

	sensor.command = KONEPLUS_COMMAND_SENSOR;
	sensor.size = sizeof(KoneplusSensor);
	sensor.dcu = dcu;
	sensor.tcu = KONEPLUS_SENSOR_TCU_OFF;
	sensor.median = 0;
	sensor.action = KONEPLUS_SENSOR_ACTION_OFF;

	return koneplus_sensor_write(koneplus, &sensor, error);
}

gboolean koneplus_sensor_dcu_try(RoccatDevice *koneplus, guint new_dcu, GError **error) {
	KoneplusSensor sensor;

	sensor.command = KONEPLUS_COMMAND_SENSOR;
	sensor.size = sizeof(KoneplusSensor);
	sensor.dcu = new_dcu;
	sensor.tcu = 0xff;
	sensor.median = 0xff;
	sensor.action = KONEPLUS_SENSOR_ACTION_UNDEFINED;

	return koneplus_sensor_write(koneplus, &sensor, error);
}

gboolean koneplus_sensor_dcu_cancel(RoccatDevice *koneplus, guint old_dcu, GError **error) {
	KoneplusSensor sensor;

	sensor.command = KONEPLUS_COMMAND_SENSOR;
	sensor.size = sizeof(KoneplusSensor);
	sensor.dcu = old_dcu;
	sensor.tcu = 0xff;
	sensor.median = 0xff;
	sensor.action = KONEPLUS_SENSOR_ACTION_CANCEL;

	return koneplus_sensor_write(koneplus, &sensor, error);
}

gboolean koneplus_sensor_dcu_accept(RoccatDevice *koneplus, guint new_dcu, GError **error) {
	KoneplusSensor sensor;

	sensor.command = KONEPLUS_COMMAND_SENSOR;
	sensor.size = sizeof(KoneplusSensor);
	sensor.dcu = new_dcu;
	sensor.tcu = 0xff;
	sensor.median = 0xff;
	sensor.action = KONEPLUS_SENSOR_ACTION_ACCEPT;

	return koneplus_sensor_write(koneplus, &sensor, error);
}
