/*
Minetest-c55
Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>

This program 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.

This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "keycode.h"
#include "main.h" // For g_settings

#define CHECKKEY(x){if(strcmp(name, #x)==0) return irr::x;}

irr::EKEY_CODE keyname_to_keycode(const char *name)
{
	CHECKKEY(KEY_LBUTTON)
	CHECKKEY(KEY_RBUTTON)
	CHECKKEY(KEY_CANCEL)
	CHECKKEY(KEY_MBUTTON)
	CHECKKEY(KEY_XBUTTON1)
	CHECKKEY(KEY_XBUTTON2)
	CHECKKEY(KEY_BACK)
	CHECKKEY(KEY_TAB)
	CHECKKEY(KEY_CLEAR)
	CHECKKEY(KEY_RETURN)
	CHECKKEY(KEY_SHIFT)
	CHECKKEY(KEY_CONTROL)
	CHECKKEY(KEY_MENU)
	CHECKKEY(KEY_PAUSE)
	CHECKKEY(KEY_CAPITAL)
	CHECKKEY(KEY_KANA)
	CHECKKEY(KEY_HANGUEL)
	CHECKKEY(KEY_HANGUL)
	CHECKKEY(KEY_JUNJA)
	CHECKKEY(KEY_FINAL)
	CHECKKEY(KEY_HANJA)
	CHECKKEY(KEY_KANJI)
	CHECKKEY(KEY_ESCAPE)
	CHECKKEY(KEY_CONVERT)
	CHECKKEY(KEY_NONCONVERT)
	CHECKKEY(KEY_ACCEPT)
	CHECKKEY(KEY_MODECHANGE)
	CHECKKEY(KEY_SPACE)
	CHECKKEY(KEY_PRIOR)
	CHECKKEY(KEY_NEXT)
	CHECKKEY(KEY_END)
	CHECKKEY(KEY_HOME)
	CHECKKE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EY_EXSEL)
	CHECKKEY(KEY_EREOF)
	CHECKKEY(KEY_PLAY)
	CHECKKEY(KEY_ZOOM)
	CHECKKEY(KEY_PA1)
	CHECKKEY(KEY_OEM_CLEAR)

	return irr::KEY_KEY_CODES_COUNT;
}

/*
	Key config
*/

// A simple cache for quicker lookup
core::map<std::string, irr::EKEY_CODE> g_key_setting_cache;

irr::EKEY_CODE getKeySetting(const char *settingname)
{
	core::map<std::string, irr::EKEY_CODE>::Node *n;
	n = g_key_setting_cache.find(settingname);
	if(n)
		return n->getValue();
	irr::EKEY_CODE c = keyname_to_keycode(g_settings.get(settingname).c_str());
	g_key_setting_cache.insert(settingname, c);
	return c;
}