2008-07-30 13:47:54 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-09-30 13:51:01 +00:00
|
|
|
#ifndef COMMON_HARDWARE_KEY_H
|
|
|
|
#define COMMON_HARDWARE_KEY_H
|
|
|
|
|
|
|
|
#include "common/scummsys.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_KEYMAPPER
|
2008-07-21 00:11:25 +00:00
|
|
|
|
2011-04-29 12:53:58 +02:00
|
|
|
#include "common/textconsole.h"
|
2008-07-21 00:11:25 +00:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
/**
|
2009-05-24 15:17:42 +00:00
|
|
|
* Describes an available hardware key
|
2008-07-21 00:11:25 +00:00
|
|
|
*/
|
|
|
|
struct HardwareKey {
|
|
|
|
/** unique id used for saving/loading to config */
|
2012-02-22 07:17:38 -06:00
|
|
|
String id;
|
2009-01-21 02:02:55 +00:00
|
|
|
|
2008-07-21 00:11:25 +00:00
|
|
|
/** Human readable description */
|
2009-05-24 15:17:42 +00:00
|
|
|
String description;
|
2009-01-21 02:02:55 +00:00
|
|
|
|
2009-05-24 15:17:42 +00:00
|
|
|
/**
|
|
|
|
* The KeyState that is generated by the back-end
|
2008-07-21 00:11:25 +00:00
|
|
|
* when this hardware key is pressed.
|
|
|
|
*/
|
|
|
|
KeyState key;
|
|
|
|
|
2012-02-22 07:17:38 -06:00
|
|
|
HardwareKey(String i, KeyState ky = KeyState(), String desc = "")
|
|
|
|
: id(i), key(ky), description(desc) { }
|
2008-07-21 00:11:25 +00:00
|
|
|
};
|
|
|
|
|
2012-02-09 01:26:11 -06:00
|
|
|
/**
|
|
|
|
* Entry in a static table of available non-modifier keys
|
|
|
|
*/
|
|
|
|
struct KeyTableEntry {
|
|
|
|
const char *hwId;
|
|
|
|
KeyCode keycode;
|
|
|
|
uint16 ascii;
|
|
|
|
const char *desc;
|
|
|
|
bool shiftable;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry in a static table of available key modifiers
|
|
|
|
*/
|
|
|
|
struct ModifierTableEntry {
|
|
|
|
byte flag;
|
|
|
|
const char *id;
|
|
|
|
const char *desc;
|
|
|
|
bool shiftable;
|
|
|
|
};
|
2008-07-21 00:11:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple class to encapsulate a device's set of HardwareKeys.
|
2009-01-21 02:02:55 +00:00
|
|
|
* Each device should instantiate this and call addHardwareKey a number of times
|
2008-07-21 00:11:25 +00:00
|
|
|
* in its constructor to define the device's available keys.
|
2009-05-24 15:17:42 +00:00
|
|
|
*/
|
2008-07-21 00:11:25 +00:00
|
|
|
class HardwareKeySet {
|
|
|
|
public:
|
|
|
|
|
2012-02-09 01:26:11 -06:00
|
|
|
/**
|
|
|
|
* Add hardware keys to the set out of key and modifier tables.
|
|
|
|
* @param keys table of available keys
|
|
|
|
* @param modifiers table of available modifiers
|
|
|
|
*/
|
|
|
|
HardwareKeySet(const KeyTableEntry keys[], const ModifierTableEntry modifiers[]) {
|
|
|
|
addHardwareKeys(keys, modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
HardwareKeySet() { }
|
|
|
|
|
2008-08-06 14:21:05 +00:00
|
|
|
virtual ~HardwareKeySet() {
|
2012-02-15 00:06:13 -06:00
|
|
|
List<const HardwareKey *>::const_iterator it;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-07-21 00:11:25 +00:00
|
|
|
for (it = _keys.begin(); it != _keys.end(); it++)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
2012-02-14 23:33:34 -06:00
|
|
|
void addHardwareKey(const HardwareKey *key) {
|
2008-07-21 00:11:25 +00:00
|
|
|
checkForKey(key);
|
|
|
|
_keys.push_back(key);
|
|
|
|
}
|
|
|
|
|
2012-02-22 07:17:38 -06:00
|
|
|
const HardwareKey *findHardwareKey(String id) const {
|
2012-02-15 00:06:13 -06:00
|
|
|
List<const HardwareKey *>::const_iterator it;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-07-21 00:11:25 +00:00
|
|
|
for (it = _keys.begin(); it != _keys.end(); it++) {
|
2012-02-22 07:17:38 -06:00
|
|
|
if ((*it)->id == id)
|
2008-07-21 00:11:25 +00:00
|
|
|
return (*it);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const HardwareKey *findHardwareKey(const KeyState& keystate) const {
|
2012-02-15 00:06:13 -06:00
|
|
|
List<const HardwareKey *>::const_iterator it;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-07-21 00:11:25 +00:00
|
|
|
for (it = _keys.begin(); it != _keys.end(); it++) {
|
2011-12-30 13:59:05 +02:00
|
|
|
if ((*it)->key == keystate)
|
2011-12-30 01:15:06 +02:00
|
|
|
return (*it);
|
2008-07-21 00:11:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-15 00:06:13 -06:00
|
|
|
const List<const HardwareKey *> &getHardwareKeys() const {
|
2008-08-01 16:44:49 +00:00
|
|
|
return _keys;
|
|
|
|
}
|
|
|
|
|
2009-01-21 02:02:55 +00:00
|
|
|
uint size() const {
|
|
|
|
return _keys.size();
|
2008-08-01 16:44:49 +00:00
|
|
|
}
|
|
|
|
|
2012-02-09 01:26:11 -06:00
|
|
|
/**
|
|
|
|
* Add hardware keys to the set out of key and modifier tables.
|
|
|
|
* @param keys table of available keys
|
|
|
|
* @param modifiers table of available modifiers
|
|
|
|
*/
|
|
|
|
void addHardwareKeys(const KeyTableEntry keys[], const ModifierTableEntry modifiers[]) {
|
|
|
|
const KeyTableEntry *key;
|
|
|
|
const ModifierTableEntry *mod;
|
|
|
|
char fullKeyId[50];
|
|
|
|
char fullKeyDesc[100];
|
|
|
|
uint16 ascii;
|
|
|
|
|
|
|
|
for (mod = modifiers; mod->id; mod++) {
|
|
|
|
for (key = keys; key->hwId; key++) {
|
|
|
|
ascii = key->ascii;
|
|
|
|
|
|
|
|
if (mod->shiftable && key->shiftable) {
|
|
|
|
snprintf(fullKeyId, 50, "%s%c", mod->id, toupper(key->hwId[0]));
|
|
|
|
snprintf(fullKeyDesc, 100, "%s%c", mod->desc, toupper(key->desc[0]));
|
|
|
|
ascii = toupper(key->ascii);
|
|
|
|
} else if (mod->shiftable) {
|
|
|
|
snprintf(fullKeyId, 50, "S+%s%s", mod->id, key->hwId);
|
|
|
|
snprintf(fullKeyDesc, 100, "Shift+%s%s", mod->desc, key->desc);
|
|
|
|
} else {
|
|
|
|
snprintf(fullKeyId, 50, "%s%s", mod->id, key->hwId);
|
|
|
|
snprintf(fullKeyDesc, 100, "%s%s", mod->desc, key->desc);
|
|
|
|
}
|
|
|
|
|
2012-02-21 06:57:32 -06:00
|
|
|
addHardwareKey(new HardwareKey(fullKeyId, KeyState(key->keycode, ascii, mod->flag), fullKeyDesc));
|
2012-02-09 01:26:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-21 00:11:25 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2012-02-14 23:33:34 -06:00
|
|
|
void checkForKey(const HardwareKey *key) {
|
2012-02-15 00:06:13 -06:00
|
|
|
List<const HardwareKey *>::iterator it;
|
2009-05-10 17:18:59 +00:00
|
|
|
|
2008-07-21 00:11:25 +00:00
|
|
|
for (it = _keys.begin(); it != _keys.end(); it++) {
|
2012-02-22 07:17:38 -06:00
|
|
|
if ((*it)->id == key->id)
|
|
|
|
error("Error adding HardwareKey '%s' - id of %s already in use!", key->description.c_str(), key->id.c_str());
|
2008-07-21 00:11:25 +00:00
|
|
|
else if ((*it)->key == key->key)
|
2008-08-06 14:21:05 +00:00
|
|
|
error("Error adding HardwareKey '%s' - key already in use!", key->description.c_str());
|
2008-07-21 00:11:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 00:06:13 -06:00
|
|
|
List<const HardwareKey *> _keys;
|
2008-07-21 00:11:25 +00:00
|
|
|
};
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Common
|
2008-07-21 00:11:25 +00:00
|
|
|
|
2008-09-30 13:51:01 +00:00
|
|
|
#endif // #ifdef ENABLE_KEYMAPPER
|
|
|
|
|
|
|
|
#endif // #ifndef COMMON_HARDWARE_KEY_H
|