/*
 * 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 "iskufx_eventhandler.h"
#include "iskufx_eventhandler_channel.h"
#include "iskufx_dbus_server.h"
#include "roccat/iskufx.h"
#include "config.h"
#include "hid_uid.h"
#include <glib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#define ISKUFX_EVENTHANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), ISKUFX_EVENTHANDLER_TYPE, IskufxEventhandlerClass))
#define IS_ISKUFX_EVENTHANDLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), ISKUFX_EVENTHANDLER_TYPE))
#define ISKUFX_EVENTHANDLER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), ISKUFX_EVENTHANDLER_TYPE, IskufxEventhandlerPrivate))

typedef struct _IskufxEventhandlerClass IskufxEventhandlerClass;

struct _IskufxEventhandlerClass {
	GObjectClass parent_class;
};

struct _IskufxEventhandlerPrivate {
	RoccatEventhandlerHost *host;
	IskufxDBusServer *dbus_server;
	RoccatDeviceScanner *device_scanner;
	RoccatDevice *device;
	gboolean device_set_up;
	RoccatKeyFile *config;
	guint actual_profile_index;
	IskuRkp *rkp[ISKU_PROFILE_NUM];
	IskufxEventhandlerChannel *channel;
	
	RoccatNotificationProfile *profile_note;
	RoccatNotificationTimer *timer_note;
	RoccatNotificationLiveRecording *live_recording_note;
};

enum {
	PROP_0,
	PROP_HOST,
};

static void profile_changed(IskufxEventhandler *eventhandler, guint profile_number) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index = profile_number - 1;

	priv->actual_profile_index = profile_index;

	roccat_notification_profile_update(priv->profile_note, isku_configuration_get_profile_notification_type(priv->config),
			profile_number, isku_rkp_get_profile_name(priv->rkp[profile_index]));

	iskufx_dbus_server_emit_profile_changed(priv->dbus_server, profile_number);
}

static gboolean set_profile(IskufxEventhandler *eventhandler, guint profile_number) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index = profile_number - 1;
	GError *local_error = NULL;

	if (priv->actual_profile_index != profile_index) {
		if (!isku_actual_profile_write(priv->device, profile_index, &local_error)) {
			// FIXME error message
			g_clear_error(&local_error);
			return FALSE;
		}
		profile_changed(eventhandler, profile_number);
	}
	return TRUE;
}

static void window_changed_cb(RoccatEventhandlerHost *host, gchar const *title, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index;
	guint game_file_index;
	gchar *pattern;

	for (profile_index = 0; profile_index < ISKU_PROFILE_NUM; ++profile_index) {
		for (game_file_index = 0; game_file_index < ISKU_GAMEFILE_NUM; ++game_file_index) {
			pattern = isku_rkp_get_game_file_name(priv->rkp[profile_index], game_file_index);
			if (strcmp(pattern, "") == 0) {
				g_free(pattern);
				continue;
			}

			if (g_regex_match_simple(pattern, title, 0, 0)) {
				set_profile(eventhandler, profile_index + 1);
				g_free(pattern);
				return;
			}
			g_free(pattern);
		}
	}

	set_profile(eventhandler, isku_configuration_get_default_profile_number(priv->config));
	return;
}

static gboolean talk_easyshift(IskufxEventhandler *eventhandler, gboolean state) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;
	gboolean retval = FALSE;

	if (priv->device_set_up) {
		retval = isku_talk_easyshift(priv->device,
				(state != FALSE) ? ISKU_TALK_EASYSHIFT_ON : ISKU_TALK_EASYSHIFT_OFF,
				&error);
		if (error) {
			g_warning("Error issuing easyshift: %s", error->message);
			g_clear_error(&error);
		}
	}

	return retval;
}

static gboolean talk_easyshift_lock(IskufxEventhandler *eventhandler, gboolean state) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;
	gboolean retval = FALSE;

	if (priv->device_set_up) {
		retval = isku_talk_easyshift_lock(priv->device,
				(state != FALSE) ? ISKU_TALK_EASYSHIFT_ON : ISKU_TALK_EASYSHIFT_OFF,
				&error);
		if (error) {
			g_warning("Error issuing easyshift_lock: %s", error->message);
			g_clear_error(&error);
		}
	}

	return retval;
}

static gboolean set_led_rgb(IskufxEventhandler *eventhandler, guint32 effect, guint32 ambient_color, guint32 event_color) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;
	gboolean retval = FALSE;

	if (priv->device_set_up) {
		retval = iskufx_talkfx(priv->device, effect, ambient_color, event_color, &error);
		if (error) {
			g_warning("Error issuing talkfx: %s", error->message);
			g_clear_error(&error);
		}
	}

	return retval;
}

static gboolean restore_led_rgb(IskufxEventhandler *eventhandler) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;
	gboolean retval = FALSE;

	if (priv->device_set_up) {
		retval = iskufx_talkfx_off(priv->device, &error);
		if (error) {
			g_warning("Error issuing talkfx_off: %s", error->message);
			g_clear_error(&error);
		}
	}

	return retval;
}

static guint iskufx_eventhandler_get_talk_device(RoccatEventhandlerPlugin *self) {
	IskufxEventhandlerPrivate *priv = ISKUFX_EVENTHANDLER(self)->priv;

	if (!priv->device_set_up)
		return 0;

	return roccat_device_get_product_id(priv->device);
}

static void iskufx_eventhandler_talk_easyshift(RoccatEventhandlerPlugin *self, gboolean state) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(self);
	talk_easyshift(eventhandler, state);
}

static void iskufx_eventhandler_talk_easyshift_lock(RoccatEventhandlerPlugin *self, gboolean state) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(self);
	talk_easyshift_lock(eventhandler, state);
}

static void iskufx_eventhandler_set_led_rgb(RoccatEventhandlerPlugin *self, guint32 effect, guint32 ambient_color, guint32 event_color) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(self);
	set_led_rgb(eventhandler, effect, ambient_color, event_color);
}

static void iskufx_eventhandler_restore_led_rgb(RoccatEventhandlerPlugin *self) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(self);
	restore_led_rgb(eventhandler);
}

static void iskufx_roccat_eventhandler_interface_init(RoccatEventhandlerPluginInterface *iface) {
	iface->get_talk_device = iskufx_eventhandler_get_talk_device;
	iface->talk_easyshift = iskufx_eventhandler_talk_easyshift;
	iface->talk_easyshift_lock = iskufx_eventhandler_talk_easyshift_lock;
	iface->talk_easyaim = NULL;
	iface->set_led_rgb = iskufx_eventhandler_set_led_rgb;
	iface->restore_led_rgb = iskufx_eventhandler_restore_led_rgb;
}

G_DEFINE_TYPE_WITH_CODE(IskufxEventhandler, iskufx_eventhandler, G_TYPE_OBJECT,
		G_IMPLEMENT_INTERFACE(ROCCAT_EVENTHANDLER_PLUGIN_TYPE, iskufx_roccat_eventhandler_interface_init));

GObject *iskufx_eventhandler_new(RoccatEventhandlerHost *host) {
	return g_object_new(ISKUFX_EVENTHANDLER_TYPE,
			"host", host,
			NULL);
}

static void talk_easyshift_cb(IskufxDBusServer *server, guchar state, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	talk_easyshift(eventhandler, state);
}

static void talk_easyshift_lock_cb(IskufxDBusServer *server, guchar state, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	talk_easyshift_lock(eventhandler, state);
}

static void set_led_rgb_cb(IskufxDBusServer *server, guint effect, guint ambient_color, guint event_color, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	set_led_rgb(eventhandler, effect, ambient_color, event_color);
}

static void restore_led_rgb_cb(IskufxDBusServer *server, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	restore_led_rgb(eventhandler);
}

static void open_gui_cb(gpointer source, gpointer user_data) {
	gchar *path;
	path = g_build_path("/", INSTALL_PREFIX, "bin", "iskufxconfig", (gchar const *)NULL);
	roccat_double_fork(path);
	g_free(path);
}

static void profile_data_changed_outside_cb(IskufxDBusServer *server, guchar number, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index = number - 1;
	
	isku_rkp_free(priv->rkp[profile_index]);
	priv->rkp[profile_index] = iskufx_rkp_load_actual(profile_index);
}

static void configuration_reload(IskufxEventhandler *eventhandler) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	if (priv->config)
		isku_configuration_free(priv->config);
	priv->config = iskufx_configuration_load();
}

static void configuration_changed_outside_cb(IskufxDBusServer *server, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	configuration_reload(eventhandler);
}

static void profile_changed_cb(gpointer source, guchar profile_number, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	profile_changed(eventhandler, profile_number);
}

static void quicklaunch_cb(IskufxEventhandlerChannel *channel, guchar key_index, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *macro_key_info;

	macro_key_info = isku_rkp_get_macro_key_info(priv->rkp[priv->actual_profile_index], key_index);
	isku_play_quicklaunch_forked(macro_key_info);
	isku_rkp_macro_key_info_free(macro_key_info);
}

static guint key_type_to_key_index(guint key_type) {
	if (key_type >= ISKU_KEY_TYPE_M1 && key_type <= ISKU_KEY_TYPE_M5)
		return key_type - ISKU_KEY_TYPE_M1 + ISKU_KEY_INDEX_M1;
	if (key_type >= ISKU_KEY_TYPE_T1 && key_type <= ISKU_KEY_TYPE_T3)
		return key_type - ISKU_KEY_TYPE_T1 + ISKU_KEY_INDEX_T1;
	if (key_type == HID_UID_KB_CAPSLOCK)
		return ISKU_KEY_INDEX_CAPSLOCK;
	g_warning("unsupported key_type 0x%x", key_type);
	return key_type;
}

static void talk_cb(IskufxEventhandlerChannel *channel, guchar key_type, guchar event, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *macro_key_info;
	static gboolean state = FALSE;

	macro_key_info = isku_rkp_get_macro_key_info(priv->rkp[priv->actual_profile_index], key_type_to_key_index(key_type));
	switch (macro_key_info->type) {
	case ISKU_KEY_TYPE_EASYSHIFT:
		break;
	case ISKU_KEY_TYPE_TALK_BOTH_EASYSHIFT:
		roccat_eventhandler_plugin_emit_talk_easyshift(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				ROCCAT_TALK_DEVICE_MOUSE,
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? TRUE : FALSE);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYSHIFT:
		roccat_eventhandler_plugin_emit_talk_easyshift(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? TRUE : FALSE);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYSHIFT_LOCK:
		if (event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) {
			state = !state;
			roccat_eventhandler_plugin_emit_talk_easyshift_lock(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
					isku_rkp_macro_key_info_get_talk_device(macro_key_info),
					state);
		}
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_1:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_1 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_2:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_2 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_3:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_3 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_4:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_4 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	case ISKU_KEY_TYPE_TALK_OTHER_EASYAIM_5:
		roccat_eventhandler_plugin_emit_talk_easyaim(ROCCAT_EVENTHANDLER_PLUGIN(eventhandler),
				isku_rkp_macro_key_info_get_talk_device(macro_key_info),
				(event == ISKU_REPORT_BUTTON_EVENT_ACTION_PRESS) ? ROCCAT_TALK_EASYAIM_5 : ROCCAT_TALK_EASYAIM_OFF);
		break;
	default:
		g_debug("unknown talk event of type 0x%2x", macro_key_info->type);
	}
	isku_rkp_macro_key_info_free(macro_key_info);
}

static void light_changed_cb(IskufxEventhandlerChannel *channel, guchar brightness_number, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	guint profile_index;
	GError *error = NULL;

	profile_index = priv->actual_profile_index;

	isku_last_set_write(priv->device, &error);
	if (error) {
		g_clear_error(&error);
		return;
	}

	isku_rkp_set_light_brightness(priv->rkp[profile_index], brightness_number - 1);
	iskufx_rkp_save_actual(priv->rkp[profile_index], profile_index, &error);
	if (error) {
		g_clear_error(&error);
		return;
	}
	isku_rkp_set_unmodified(priv->rkp[profile_index]);

	iskufx_dbus_server_emit_brightness_changed(priv->dbus_server, profile_index + 1, brightness_number);
}

static gboolean recording_event_store_roccat_macro(IskuRkpMacroKeyInfo const *key_info, GError **error) {
	RoccatMacros *roccat_macros;
	RoccatMacro *roccat_macro;
	gboolean retval;

	roccat_macro = isku_rkp_macro_key_info_to_roccat_macro(key_info);

	/*
	 * Using device specific macroset name instead of default "MLR"
	 * because macro file is shared between devices.
	 * Macroset name stored in device is still "MLR" to reduce writes.
	 */
	roccat_macro_set_macro_set_name(roccat_macro, ISKUFX_DEVICE_NAME " MLR");

	roccat_macros = roccat_macros_load();
	roccat_macros_set(roccat_macros, roccat_macro);
	retval = roccat_macros_save(roccat_macros, error);
	roccat_macros_free(roccat_macros);
	roccat_macro_free(roccat_macro);

	return retval;
}

static IskuRkpMacroKeyInfo *recording_event_get_data(RoccatDevice *device, guint profile_index, guint key_index, GError **error) {
	IskuMacro *macro;
	IskuRkpMacroKeyInfo *key_info;

	macro = isku_macro_read(device, profile_index, key_index, error);
	if (!macro)
		return NULL;

	key_info = isku_macro_to_rkp_macro_key_info(macro);
	g_free(macro);

	return key_info;
}

static gboolean handle_recording_event(IskufxEventhandler *eventhandler, guint profile_index, guint key_index) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *key_info;
	GError *error = NULL;

	key_info = recording_event_get_data(priv->device, profile_index, key_index, &error);
	if (error) {
		g_clear_error(&error);
		return FALSE;
	}

	isku_last_set_write(priv->device, &error);
	if (error) {
		g_clear_error(&error);
		g_free(key_info);
		return FALSE;
	}

	isku_rkp_set_macro_key_info(priv->rkp[profile_index], key_index, key_info);
	iskufx_rkp_save_actual(priv->rkp[profile_index], profile_index, &error);
	if (error) {
		g_clear_error(&error);
		g_free(key_info);
		return FALSE;
	}
	isku_rkp_set_unmodified(priv->rkp[profile_index]);

	recording_event_store_roccat_macro(key_info, &error);
	if (error) {
		g_clear_error(&error);
		g_free(key_info);
		return FALSE;
	}

	/* rkp has to be stored before this */
	iskufx_dbus_server_emit_macro_changed(priv->dbus_server, profile_index + 1, key_index + 1, key_info);

	g_free(key_info);
	return TRUE;
}

static void live_recording_cb(IskufxEventhandlerChannel *channel, guchar key_index, guchar event, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;

	switch(event) {
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_START:
		roccat_notification_live_recording_select_macro_key(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_MACRO_KEY_SELECTED:
		roccat_notification_live_recording_record_macro(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_END_SUCCESS:
		roccat_notification_live_recording_end_success(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		handle_recording_event(eventhandler, priv->actual_profile_index, key_index);
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_END_ABORT:
		roccat_notification_live_recording_end_abort(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	case ISKU_REPORT_BUTTON_LIVE_RECORDING_INVALID_KEY:
		roccat_notification_live_recording_invalid_key(priv->live_recording_note,
				isku_configuration_get_live_recording_notification_type(priv->config));
		break;
	default:
		g_debug("unknown live recording event of type 0x%2x", event);
	}
}

static void timer_start_cb(IskufxEventhandlerChannel *channel, guchar key_index, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	IskuRkpMacroKeyInfo *macro_key_info;

	macro_key_info = isku_rkp_get_macro_key_info(priv->rkp[priv->actual_profile_index], key_index);
	roccat_notification_timer_start(priv->timer_note,
			isku_configuration_get_timer_notification_type(priv->config),
			(gchar const *)macro_key_info->timer_name,
			isku_rkp_macro_key_info_get_timer_length(macro_key_info));
	isku_rkp_macro_key_info_free(macro_key_info);
}

static void timer_stop_cb(IskufxEventhandlerChannel *channel, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;

	roccat_notification_timer_stop(priv->timer_note);
}

static gboolean profile_data_load(IskufxEventhandler *eventhandler, GError **error) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	guint i;

	for (i = 0; i < ISKU_PROFILE_NUM; ++i) {
		priv->rkp[i] = iskufx_rkp_load(priv->device, i, error);
		if (priv->rkp[i] == NULL)
			return FALSE;
	}
	return TRUE;
}

static gboolean add_cb(RoccatDevice *device, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	GError *error = NULL;

	if (priv->device) {
		g_critical("Multiple devices present.");
		return TRUE;
	}

	priv->device = device;

	configuration_reload(eventhandler);

	/* Notificator gets initialized only once if a device is found.
	 * Gets deinited once in finalizer.
	 */
	if (!priv->profile_note)
		priv->profile_note = roccat_notification_profile_new(roccat_eventhandler_host_get_notificator(priv->host), ISKUFX_DEVICE_NAME);
	if (!priv->timer_note)
		priv->timer_note = roccat_notification_timer_new(roccat_eventhandler_host_get_notificator(priv->host), ISKUFX_DEVICE_NAME);
	if (!priv->live_recording_note)
		priv->live_recording_note = roccat_notification_live_recording_new(roccat_eventhandler_host_get_notificator(priv->host), ISKUFX_DEVICE_NAME);
	
	priv->actual_profile_index = isku_actual_profile_read(priv->device, &error);
	if (error) {
		g_warning("Error reading actual profile: %s", error->message);
		g_clear_error(&error);
	}
	
	profile_data_load(eventhandler, &error);
	if (error) {
		g_warning("Error reading profile data: %s", error->message);
		g_clear_error(&error);
	}

	priv->dbus_server = iskufx_dbus_server_new();
	iskufx_dbus_server_connect(priv->dbus_server);
	g_signal_connect(G_OBJECT(priv->dbus_server), "easyshift", G_CALLBACK(talk_easyshift_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "easyshift-lock", G_CALLBACK(talk_easyshift_lock_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "set-led-rgb", G_CALLBACK(set_led_rgb_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "restore-led-rgb", G_CALLBACK(restore_led_rgb_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "open-gui", G_CALLBACK(open_gui_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "profile-changed-outside", G_CALLBACK(profile_changed_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "profile-data-changed-outside", G_CALLBACK(profile_data_changed_outside_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->dbus_server), "configuration-changed-outside", G_CALLBACK(configuration_changed_outside_cb), eventhandler);

	g_signal_connect(G_OBJECT(priv->host), "active-window-changed", G_CALLBACK(window_changed_cb), eventhandler);

	iskufx_eventhandler_channel_start(priv->channel, roccat_device_get_roccat_dev(priv->device), &error);
	if (error) {
		g_warning("Error starting chardev eventhandler: %s", error->message);
		g_clear_error(&error);
	}

	priv->device_set_up = TRUE;

	return FALSE;
}

static void stop(IskufxEventhandler *eventhandler) {
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	guint i;

	priv->device_set_up = FALSE;

	g_object_unref(priv->dbus_server);
	priv->dbus_server = NULL;

	iskufx_eventhandler_channel_stop(priv->channel);

	roccat_device_free(priv->device);
	priv->device = NULL;

	isku_configuration_free(priv->config);
	priv->config = NULL;

	for (i = 0; i < ISKU_PROFILE_NUM; ++i) {
		isku_rkp_free(priv->rkp[i]);
		priv->rkp[i] = NULL;
	}
}

static gboolean remove_cb(RoccatDevice *device, gpointer user_data) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(user_data);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;
	if (priv->device && strcmp(roccat_device_get_identifier(device), roccat_device_get_identifier(priv->device)) == 0) {
		stop(eventhandler);
	}
	return TRUE;
}

static void iskufx_eventhandler_init(IskufxEventhandler *eventhandler) {
	eventhandler->priv = ISKUFX_EVENTHANDLER_GET_PRIVATE(eventhandler);
}

static void finalize(GObject *object) {
	IskufxEventhandler *eventhandler = ISKUFX_EVENTHANDLER(object);
	IskufxEventhandlerPrivate *priv = eventhandler->priv;

	g_object_unref(priv->channel);
	priv->channel = NULL;

	roccat_device_scanner_free(priv->device_scanner);
	priv->device_scanner = NULL;

	stop(eventhandler);

	if (priv->live_recording_note) {
		roccat_notification_live_recording_free(priv->live_recording_note);
		priv->live_recording_note = NULL;
	}

	if (priv->timer_note) {
		roccat_notification_timer_free(priv->timer_note);
		priv->timer_note = NULL;
	}

	if (priv->profile_note) {
		roccat_notification_profile_free(priv->profile_note);
		priv->profile_note = NULL;
	}
	
	G_OBJECT_CLASS(iskufx_eventhandler_parent_class)->finalize(object);
}

static GObject *constructor(GType gtype, guint n_properties, GObjectConstructParam *properties) {
	IskufxEventhandler *eventhandler;
	IskufxEventhandlerPrivate *priv;
	GObject *obj;

	obj = G_OBJECT_CLASS(iskufx_eventhandler_parent_class)->constructor(gtype, n_properties, properties);
	eventhandler = ISKUFX_EVENTHANDLER(obj);
	priv = eventhandler->priv;

	priv->channel = iskufx_eventhandler_channel_new();
	g_signal_connect(G_OBJECT(priv->channel), "open-driver", G_CALLBACK(open_gui_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "profile-changed", G_CALLBACK(profile_changed_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "quicklaunch", G_CALLBACK(quicklaunch_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "talk", G_CALLBACK(talk_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "timer-start", G_CALLBACK(timer_start_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "timer-stop", G_CALLBACK(timer_stop_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "light-changed", G_CALLBACK(light_changed_cb), eventhandler);
	g_signal_connect(G_OBJECT(priv->channel), "live-recording", G_CALLBACK(live_recording_cb), eventhandler);

	priv->device_scanner = iskufx_device_scanner_new(add_cb, remove_cb, eventhandler);

	return obj;
}

static void set_property(GObject *object, guint prop_id, GValue const *value, GParamSpec *pspec) {
	IskufxEventhandlerPrivate *priv = ISKUFX_EVENTHANDLER(object)->priv;

	switch(prop_id) {
	case PROP_HOST:
		priv->host = g_value_get_pointer(value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	}
}

static void iskufx_eventhandler_class_init(IskufxEventhandlerClass *klass) {
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS(klass);

	gobject_class->constructor = constructor;
	gobject_class->finalize = finalize;
	gobject_class->set_property = set_property;

	g_type_class_add_private(klass, sizeof(IskufxEventhandlerPrivate));

	g_object_class_install_property(gobject_class, PROP_HOST,
			g_param_spec_pointer("host",
					"Host",
					"Pointer to RoccatEventhandlerHost interface",
					G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}

ROCCAT_EVENTHANDLER_PLUGIN_API RoccatEventhandlerPlugin *roccat_eventhandler_plugin_new(RoccatEventhandlerHost *host) {
	return ROCCAT_EVENTHANDLER_PLUGIN(iskufx_eventhandler_new(host));
}
