/*
 * 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/pyra.h"
#include "g_roccat_helper.h"
#include "hid_uid.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 = roccat_hid_to_kbd_keycode(HID_UID_KB_LEFT_SHIFT);
	ctrl_keycode = roccat_hid_to_kbd_keycode(HID_UID_KB_LEFT_CONTROL);
	alt_keycode = roccat_hid_to_kbd_keycode(HID_UID_KB_LEFT_ALT);
	win_keycode = roccat_hid_to_kbd_keycode(HID_UID_KB_LEFT_GUI);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_SHIFT))
		roccat_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))
		roccat_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))
		roccat_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))
		roccat_input_event_write_with_sync(kbd_file, win_keycode, ROCCAT_INPUT_EVENT_VALUE_PRESS);

	roccat_input_event_write_with_sync(kbd_file, roccat_hid_to_kbd_keycode(keystroke->key), ROCCAT_INPUT_EVENT_VALUE_PRESS);
	roccat_input_event_write_with_sync(kbd_file, roccat_hid_to_kbd_keycode(keystroke->key), ROCCAT_INPUT_EVENT_VALUE_RELEASE);

	if (roccat_get_bit(keystroke->action, PYRA_RMP_KEYSTROKE_SHORTCUT_ACTION_WIN))
		roccat_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))
		roccat_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))
		roccat_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))
		roccat_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);
}
