/*
 * 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 "config.h"
#include "g_thread_roccat_helper.h"
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <linux/uinput.h>

struct _RoccatDevice {
	gchar *drivername;
	gchar *identifier;
	gchar *syspath; // where the sysfs attributes are
	gchar *roccat;
	gchar *input_mouse;
	gchar *input_kbd;
	gchar *input_none;
	guint product_id;
	GRoccatRecMutex lock;
};

void roccat_device_lock(RoccatDevice *roccat_dev) {
	g_roccat_rec_mutex_lock(&roccat_dev->lock);
}

void roccat_device_unlock(RoccatDevice *roccat_dev) {
	g_roccat_rec_mutex_unlock(&roccat_dev->lock);
}

guint roccat_device_get_product_id(RoccatDevice const *roccat_dev) {
	return roccat_dev->product_id;
}

void roccat_device_set_product_id(RoccatDevice *roccat_dev, guint product_id) {
	roccat_dev->product_id = product_id;
}

gchar const *roccat_device_get_drivername(RoccatDevice const *roccat_dev) {
	return roccat_dev->drivername;
}

void roccat_device_set_drivername(RoccatDevice *roccat_dev, gchar const *drivername) {
	g_free(roccat_dev->drivername);
	roccat_dev->drivername = g_strdup(drivername);
}

gchar const *roccat_device_get_identifier(RoccatDevice const *roccat_dev) {
	return roccat_dev->identifier;
}

static void roccat_device_set_identifier(RoccatDevice *roccat_dev, gchar const *identifier) {
	g_free(roccat_dev->identifier);
	roccat_dev->identifier = g_strdup(identifier);
}

gchar const *roccat_device_get_syspath(RoccatDevice const *roccat_dev) {
	return roccat_dev->syspath;
}

void roccat_device_set_syspath(RoccatDevice *roccat_dev, gchar const *syspath) {
	g_free(roccat_dev->syspath);
	roccat_dev->syspath = g_strdup(syspath);
}

static gchar const *roccat_device_get_mouse_evdev(RoccatDevice const *roccat_dev) {
	return roccat_dev->input_mouse;
}

void roccat_device_set_mouse_evdev(RoccatDevice *roccat_dev, gchar const *mouse_evdev) {
	g_free(roccat_dev->input_mouse);
	roccat_dev->input_mouse = g_strdup(mouse_evdev);
}

static gchar const *roccat_device_get_keyboard_evdev(RoccatDevice const *roccat_dev) {
	return roccat_dev->input_kbd;
}

void roccat_device_set_keyboard_evdev(RoccatDevice *roccat_dev, gchar const *keyboard_evdev) {
	g_free(roccat_dev->input_kbd);
	roccat_dev->input_kbd = g_strdup(keyboard_evdev);
}

static gchar const *roccat_device_get_none_evdev(RoccatDevice const *roccat_dev) {
	return roccat_dev->input_none;
}

void roccat_device_set_none_evdev(RoccatDevice *roccat_dev, gchar const *none_evdev) {
	g_free(roccat_dev->input_none);
	roccat_dev->input_none = g_strdup(none_evdev);
}

gchar const *roccat_device_get_roccat_dev(RoccatDevice const *roccat_dev) {
	return roccat_dev->roccat;
}

void roccat_device_set_roccat_dev(RoccatDevice *roccat_dev, gchar const *device) {
	g_free(roccat_dev->roccat);
	roccat_dev->roccat = g_strdup(device);
}

int roccat_device_open_roccat_dev(RoccatDevice const *roccat_dev) {
	if (!roccat_device_get_roccat_dev(roccat_dev))
		return -1;
	return open(roccat_device_get_roccat_dev(roccat_dev), O_RDONLY, 0);
}

static int roccat_device_open_evdev(gchar const *path) {
	if (!path)
		return -1;

	return open(path, O_WRONLY);
}

int roccat_device_open_mouse_evdev(RoccatDevice const *roccat_dev) {
	return roccat_device_open_evdev(roccat_device_get_mouse_evdev(roccat_dev));
}

int roccat_device_open_none_evdev(RoccatDevice const *roccat_dev) {
	return roccat_device_open_evdev(roccat_device_get_none_evdev(roccat_dev));
}

int roccat_device_open_keyboard_evdev(RoccatDevice const *roccat_dev) {
	return roccat_device_open_evdev(roccat_device_get_keyboard_evdev(roccat_dev));
}

int roccat_device_open_misc_evdev(RoccatDevice const *roccat_dev) {
	int fd;
	struct uinput_user_dev uidev;

	fd = open(UINPUT_DIR "/uinput", O_WRONLY | O_NONBLOCK);
	if (fd < 0) {
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
		goto error;
	}
		
	if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
//	if (ioctl(fd, UI_SET_EVBIT, EV_SYN) < 0)
//		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, KEY_MUTE) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, KEY_VOLUMEDOWN) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, KEY_VOLUMEUP) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, KEY_PREVIOUSSONG) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, KEY_NEXTSONG) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, KEY_PLAYPAUSE) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, KEY_STOPCD) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
	if (ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE) < 0)
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));

	memset(&uidev, 0, sizeof(uidev));

	snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "%s_%s", roccat_dev->drivername, roccat_dev->identifier);
	uidev.id.bustype = BUS_USB;
	uidev.id.vendor = USB_VENDOR_ID_ROCCAT;
	uidev.id.product = 1;
	uidev.id.version = 1;

	if (write(fd, &uidev, sizeof(uidev)) < 0) {
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
		goto close;
	}
	
	if (ioctl(fd, UI_DEV_CREATE) < 0) {
		g_warning("roccat_device_open_user_evdev: %s", g_strerror(errno));
		goto close;
	}
	
	return fd;
close:
	close(fd);
error:
	return -1;
}

void roccat_device_close_misc_evdev(int fd) {
	if (ioctl(fd, UI_DEV_DESTROY) < 0)
		g_warning("roccat_device_close_user_evdev: %s", g_strerror(errno));

	close(fd);
}

RoccatDevice *roccat_device_new(gchar const *identifier) {
	RoccatDevice *roccat_dev;

	roccat_dev = g_malloc0(sizeof(RoccatDevice));
	g_roccat_rec_mutex_init(&roccat_dev->lock);
	roccat_device_set_identifier(roccat_dev, identifier);
	return roccat_dev;
}

void roccat_device_free(RoccatDevice *roccat_dev) {
	if (roccat_dev == NULL)
		return;

	g_free(roccat_dev->drivername);
	g_free(roccat_dev->syspath);
	g_free(roccat_dev->identifier);
	g_free(roccat_dev->roccat);
	g_free(roccat_dev->input_mouse);
	g_free(roccat_dev->input_kbd);
	g_free(roccat_dev->input_none);
	g_roccat_rec_mutex_clear(&roccat_dev->lock);
	g_free(roccat_dev);
}
