/*
 * 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_gdk_key_translations.h"
#include "roccat/roccat.h"
#include "g_roccat_helper.h"

static guint roccat_xkeycode_to_keyval(guint keycode) {
	GdkKeymap *keymap;
	GdkKeymapKey key = {keycode, 0, 0};
	guint keyval;

	keymap = gdk_keymap_get_default();
	keyval = gdk_keymap_lookup_key(keymap, &key);
	if (keyval == 0)
		g_warning("in roccat_keycode_to_keyval: keycode %i has no keyval!\n", keycode);
	return keyval;
}

static gchar *roccat_keyval_to_keyname(guint keyval) {
	guint32 unicode;

	unicode = gdk_keyval_to_unicode(keyval);

	if (g_unichar_isgraph(unicode))
		return g_roccat_unichar_to_utf8(unicode);
	else
		return g_strdup(gdk_keyval_name(keyval));
}

gchar *roccat_xkeycode_to_keyname(guint keycode) {
	return roccat_keyval_to_keyname(roccat_xkeycode_to_keyval(keycode));
}

gchar *roccat_hid_to_keyname(guint8 usage_id) {
	gchar const *string;

	switch (usage_id) {
	case ROCCAT_MACRO_KEYSTROKE_KEY_BUTTON_LEFT:
		string = "Button left";
		break;
	case ROCCAT_MACRO_KEYSTROKE_KEY_BUTTON_RIGHT:
		string = "Button right";
		break;
	case ROCCAT_MACRO_KEYSTROKE_KEY_BUTTON_MIDDLE:
		string = "Button middle";
		break;
	default:
		string = roccat_xkeycode_to_keyname(roccat_hid_to_xkeycode(usage_id));
	}
	return g_strdup(string);
}
