/*
 * 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 <gaminggear/key_translations.h>
#include <gaminggear/hid_uid.h>
#include <gaminggear/evdev.h>
#include "roccat.h"
#include "pyra.h"
#include "roccat_process_helper.h"
#include "roccat_helper.h"
#include "g_roccat_helper.h"
#include <unistd.h>
#include <errno.h>

gboolean pyra_play_shortcut(int kbd_file, int mouse_file, PyraRMPMacroKeyInfo const *macro_key_info) {
	PyraRMPKeystroke const *keystroke;
	guint shift_keycode, ctrl_keycode, alt_keycode, win_keycode;

	if (macro_key_info->type != PYRA_RMP_MACRO_KEY_INFO_TYPE_SHORTCUT)
		return FALSE;

	keystroke = &macro_key_info->keystrokes[0];

	shift_keycode = gaminggear_hid_to_kbd_keycode(HID_UID_KB_LEFT_SHIFT);
	ctrl_keycode = gaminggear_hid_to_kbd_keycode(HID_UID_KB_LEFT_CONTROL);
	alt_keycode = gaminggear_hid_to_kbd_keycode(HID_UID_KB_LEFT_ALT);
	win_keycode = gaminggear_hid_to_kbd_keycode(HID_UID_KB_LEFT_GUI);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_SHIFT))
		gaminggear_input_event_write_with_sync(kbd_file, shift_keycode, ROCCAT_INPUT_EVENT_VALUE_PRESS);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_CTRL))
		gaminggear_input_event_write_with_sync(kbd_file, ctrl_keycode, ROCCAT_INPUT_EVENT_VALUE_PRESS);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_ALT))
		gaminggear_input_event_write_with_sync(kbd_file, alt_keycode, ROCCAT_INPUT_EVENT_VALUE_PRESS);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_WIN))
		gaminggear_input_event_write_with_sync(kbd_file, win_keycode, ROCCAT_INPUT_EVENT_VALUE_PRESS);

	gaminggear_input_event_write_with_sync(kbd_file, gaminggear_hid_to_kbd_keycode(keystroke->key), ROCCAT_INPUT_EVENT_VALUE_PRESS);
	gaminggear_input_event_write_with_sync(kbd_file, gaminggear_hid_to_kbd_keycode(keystroke->key), ROCCAT_INPUT_EVENT_VALUE_RELEASE);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_WIN))
		gaminggear_input_event_write_with_sync(kbd_file, win_keycode, ROCCAT_INPUT_EVENT_VALUE_RELEASE);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_ALT))
		gaminggear_input_event_write_with_sync(kbd_file, alt_keycode, ROCCAT_INPUT_EVENT_VALUE_RELEASE);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_CTRL))
		gaminggear_input_event_write_with_sync(kbd_file, ctrl_keycode, ROCCAT_INPUT_EVENT_VALUE_RELEASE);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_SHIFT))
		gaminggear_input_event_write_with_sync(kbd_file, shift_keycode, ROCCAT_INPUT_EVENT_VALUE_RELEASE);

	return TRUE;
}

gboolean pyra_play_quicklaunch_forked(PyraRMPMacroKeyInfo const *macro_key_info) {
	if (macro_key_info->type != PYRA_RMP_MACRO_KEY_INFO_TYPE_QUICKLAUNCH)
		return FALSE;
	return roccat_double_fork((gchar const *)macro_key_info->filename);
}
