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

#define ROCCAT_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ROCCAT_DEVICE_TYPE, RoccatDeviceClass))
#define IS_ROCCAT_DEVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ROCCAT_DEVICE_TYPE))
#define ROCCAT_DEVICE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ROCCAT_DEVICE_TYPE, RoccatDevicePrivate))

typedef struct _RoccatDeviceClass RoccatDeviceClass;

struct _RoccatDeviceClass {
	GObjectClass parent_class;
};

struct _RoccatDevicePrivate {
	gchar *drivername;
	gchar *identifier; // unique per device, not interface
	gchar *syspath; // where the sysfs attributes are
	gchar *roccat;
	gchar *input_mouse;
	gchar *input_kbd;
	gchar *input_none;
	gchar *hidraw;
	guint product_id;
	GRoccatRecMutex lock;
};

G_DEFINE_TYPE(RoccatDevice, roccat_device, G_TYPE_OBJECT);

static int roccat_device_open_dev(gchar const *path, gint flags, GError **error) {
	int retval;

	if (!path) {
		g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "Error opening file: no path");
		return -1;
	}

	retval = open(path, flags, 0);
	if (retval < 0)
		g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), "Error opening %s: %s", path, g_strerror(errno));

	return retval;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

int roccat_device_open_roccat_dev(RoccatDevice const *roccat_dev, GError **error) {
	return roccat_device_open_dev(roccat_device_get_roccat_dev(roccat_dev), O_RDONLY, error);
}

gchar const *roccat_device_get_hidraw_dev(RoccatDevice const *roccat_dev) {
	return roccat_dev->priv->hidraw;
}

void roccat_device_set_hidraw_dev(RoccatDevice *roccat_dev, gchar const *device) {
	g_free(roccat_dev->priv->hidraw);
	roccat_dev->priv->hidraw = g_strdup(device);
}

int roccat_device_open_hidraw_dev(RoccatDevice const *roccat_dev, GError **error) {
	return roccat_device_open_dev(roccat_device_get_hidraw_dev(roccat_dev), O_RDONLY, error);
}

int roccat_device_open_mouse_evdev(RoccatDevice const *roccat_dev, GError **error) {
	return roccat_device_open_dev(roccat_device_get_mouse_evdev(roccat_dev), O_WRONLY, error);
}

int roccat_device_open_none_evdev(RoccatDevice const *roccat_dev, GError **error) {
	return roccat_device_open_dev(roccat_device_get_none_evdev(roccat_dev), O_WRONLY, error);
}

int roccat_device_open_keyboard_evdev(RoccatDevice const *roccat_dev, GError **error) {
	return roccat_device_open_dev(roccat_device_get_keyboard_evdev(roccat_dev), O_WRONLY, error);
}

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

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

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

	roccat_device = ROCCAT_DEVICE(g_object_new(ROCCAT_DEVICE_TYPE, NULL));
	roccat_device_set_identifier(roccat_device, identifier);

	return roccat_device;
}

static void roccat_device_init(RoccatDevice *macro_thread_data) {
	RoccatDevicePrivate *priv = ROCCAT_DEVICE_GET_PRIVATE(macro_thread_data);
	macro_thread_data->priv = priv;

	g_roccat_rec_mutex_init(&priv->lock);
}

static void roccat_device_finalize(GObject *object) {
	RoccatDevicePrivate *priv = ROCCAT_DEVICE(object)->priv;

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

	G_OBJECT_CLASS(roccat_device_parent_class)->finalize(object);
}

static void roccat_device_class_init(RoccatDeviceClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = (GObjectClass*)klass;

	gobject_class->finalize = roccat_device_finalize;

	g_type_class_add_private(klass, sizeof(RoccatDevicePrivate));
}

// FIXME error
int roccat_device_open_misc_evdev(RoccatDevice const *roccat_dev, GError **error) {
	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_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->priv->drivername, roccat_dev->priv->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);
}

void roccat_device_debug(RoccatDevice const *roccat_dev) {
	RoccatDevicePrivate *priv;

	if (!roccat_dev)
		return;

	priv = roccat_dev->priv;

	g_debug("Roccat Device 0x%04x", priv->product_id);
	g_debug("    Driver      %s", priv->drivername);
	g_debug("    Identifier  %s", priv->identifier);
	g_debug("    Syspath     %s", priv->syspath);
	g_debug("    Roccat      %s", priv->roccat);
	g_debug("    Hidraw      %s", priv->hidraw);
	g_debug("    Input None  %s", priv->input_none);
	g_debug("    Input Mouse %s", priv->input_mouse);
	g_debug("    Input Kbd   %s", priv->input_kbd);
}

gboolean roccat_device_equal(RoccatDevice const *one, RoccatDevice const *other) {
	return (strcmp(one->priv->identifier, other->priv->identifier)) ? FALSE : TRUE;
}

gchar const *roccat_device_get_name_static(RoccatDevice const *roccat_dev) {
	return roccat_talk_device_get_text_static(roccat_dev->priv->product_id);
}
