2012-12-14 17:52:03 +02:00
|
|
|
#ifndef _library__keyboard__hpp__included__
|
|
|
|
#define _library__keyboard__hpp__included__
|
|
|
|
|
|
|
|
#include "register-queue.hpp"
|
2014-03-23 09:45:42 +02:00
|
|
|
#include "threads.hpp"
|
2012-12-14 17:52:03 +02:00
|
|
|
#include <map>
|
2012-12-16 20:45:16 +02:00
|
|
|
#include <set>
|
2012-12-14 17:52:03 +02:00
|
|
|
#include <string>
|
2012-12-16 20:45:16 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
2012-12-14 17:52:03 +02:00
|
|
|
|
2013-12-19 00:46:12 +02:00
|
|
|
namespace keyboard
|
|
|
|
{
|
|
|
|
class modifier;
|
|
|
|
class key;
|
|
|
|
class key_axis;
|
|
|
|
class key_mouse;
|
|
|
|
class event_listener;
|
2012-12-14 17:52:03 +02:00
|
|
|
|
|
|
|
/**
|
2012-12-16 20:45:16 +02:00
|
|
|
* A group of modifiers and keys.
|
|
|
|
*
|
|
|
|
* Instances of this class allow registering, unregistering and looking up modifiers and keys.
|
2012-12-14 17:52:03 +02:00
|
|
|
*/
|
|
|
|
class keyboard
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2012-12-16 20:45:16 +02:00
|
|
|
* Create a new instance.
|
2012-12-14 17:52:03 +02:00
|
|
|
*/
|
|
|
|
keyboard() throw(std::bad_alloc);
|
|
|
|
/**
|
2012-12-16 20:45:16 +02:00
|
|
|
* Destroy an instance.
|
|
|
|
*
|
|
|
|
* The keys and modifiers in instance are not freed.
|
2012-12-14 17:52:03 +02:00
|
|
|
*/
|
|
|
|
~keyboard() throw();
|
|
|
|
/**
|
|
|
|
* Lookup modifier by name.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the modifier.
|
|
|
|
* Returns: The modifier.
|
|
|
|
* Throws std::runtime_error: No such modifier.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
modifier& lookup_modifier(const std::string& name) throw(std::runtime_error);
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Try lookup modifier by name.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the modifier.
|
|
|
|
* Returns: The modifier, or NULL if not found.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
modifier* try_lookup_modifier(const std::string& name) throw();
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Look up all modifiers.
|
|
|
|
*
|
|
|
|
* Returns: The set of modifiers.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
std::list<modifier*> all_modifiers() throw(std::bad_alloc);
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Register a modifier.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the modifier.
|
|
|
|
* Parameter mod: The modifier.
|
|
|
|
*/
|
2014-02-23 07:07:20 +02:00
|
|
|
void do_register(const std::string& name, modifier& mod) throw(std::bad_alloc);
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Unregister a modifier.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the modifier.
|
|
|
|
*/
|
2014-02-23 07:07:20 +02:00
|
|
|
void do_unregister(const std::string& name, modifier* dummy) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Lookup key by name.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the key.
|
|
|
|
* Returns: The key.
|
|
|
|
* Throws std::runtime_error: No such key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key& lookup_key(const std::string& name) throw(std::runtime_error);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Try lookup key by name.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the key.
|
|
|
|
* Returns: The key, or NULL if not found.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key* try_lookup_key(const std::string& name) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Look up all keys.
|
|
|
|
*
|
|
|
|
* Returns: The set of keys.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
std::list<key*> all_keys() throw(std::bad_alloc);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Register a key.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the key.
|
|
|
|
* Parameter mod: The key.
|
|
|
|
*/
|
2014-02-23 07:07:20 +02:00
|
|
|
void do_register(const std::string& name, key& mod) throw(std::bad_alloc);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Unregister a key.
|
|
|
|
*
|
|
|
|
* Parameter name: The name of the key.
|
|
|
|
*/
|
2014-02-23 07:07:20 +02:00
|
|
|
void do_unregister(const std::string& name, key* dummy) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set exclusive listener for all keys at once.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_exclusive(event_listener* listener) throw();
|
2013-03-14 14:12:04 +02:00
|
|
|
/**
|
|
|
|
* Set current key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_current_key(key* key) throw();
|
2013-03-14 14:12:04 +02:00
|
|
|
/**
|
|
|
|
* Get current key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key* get_current_key() throw();
|
2012-12-14 17:52:03 +02:00
|
|
|
private:
|
|
|
|
keyboard(const keyboard&);
|
|
|
|
keyboard& operator=(const keyboard&);
|
2013-12-19 00:46:12 +02:00
|
|
|
std::map<std::string, modifier*> modifiers;
|
|
|
|
std::map<std::string, key*> keys;
|
2014-03-23 09:45:42 +02:00
|
|
|
threads::lock mlock;
|
2013-12-19 00:46:12 +02:00
|
|
|
key* current_key;
|
2012-12-14 17:52:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A modifier or group of modifiers.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class modifier
|
2012-12-14 17:52:03 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a (group of) modifiers.
|
|
|
|
*
|
|
|
|
* Parameter keyb: The keyboard these will be on.
|
|
|
|
* Parameter _name: The name of the modifier.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
modifier(keyboard& keyb, const std::string& _name) throw(std::bad_alloc)
|
2012-12-14 17:52:03 +02:00
|
|
|
: kbd(keyb), name(_name)
|
|
|
|
{
|
2014-02-23 07:07:20 +02:00
|
|
|
register_queue<keyboard, modifier>::do_register(kbd, name, *this);
|
2012-12-14 17:52:03 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Create a linked modifier in group.
|
|
|
|
*
|
|
|
|
* Parameter keyb: The keyboard these will be on.
|
|
|
|
* Parameter _name: The name of the modifier.
|
|
|
|
* Parameter _link: The name of the modifier group this is in.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
modifier(keyboard& keyb, const std::string& _name, const std::string& _link) throw(std::bad_alloc)
|
2012-12-14 17:52:03 +02:00
|
|
|
: kbd(keyb), name(_name), link(_link)
|
|
|
|
{
|
2014-02-23 07:07:20 +02:00
|
|
|
register_queue<keyboard, modifier>::do_register(kbd, name, *this);
|
2012-12-14 17:52:03 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~modifier() throw()
|
2012-12-14 17:52:03 +02:00
|
|
|
{
|
2014-02-23 07:07:20 +02:00
|
|
|
register_queue<keyboard, modifier>::do_unregister(kbd, name);
|
2012-12-14 17:52:03 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get associated keyboard.
|
|
|
|
*/
|
|
|
|
keyboard& get_keyboard() const throw() { return kbd; }
|
|
|
|
/**
|
|
|
|
* Get name of the modifier.
|
|
|
|
*/
|
|
|
|
const std::string& get_name() const throw() { return name; }
|
|
|
|
/**
|
|
|
|
* Get linked name of the modifier.
|
|
|
|
*
|
|
|
|
* Returns: The linked name, or "" if none.
|
|
|
|
*/
|
|
|
|
const std::string& get_link_name() const throw() { return link; }
|
|
|
|
/**
|
|
|
|
* Get the linked modifier.
|
|
|
|
*
|
|
|
|
* Returns: The linked modifier, or NULL if none (or not initialized yet).
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
modifier* get_link() { return kbd.try_lookup_modifier(link); }
|
2012-12-14 17:52:03 +02:00
|
|
|
private:
|
|
|
|
keyboard& kbd;
|
|
|
|
std::string name;
|
|
|
|
std::string link;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A set of modifier keys.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class modifier_set
|
2012-12-14 17:52:03 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Add a modifier into the set.
|
|
|
|
*
|
|
|
|
* parameter mod: The modifier to add.
|
|
|
|
* parameter really: If true, actually add the key. If false, do nothing.
|
|
|
|
* throws std::bad_alloc: Not enough memory.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void add(modifier& mod, bool really = true) throw(std::bad_alloc);
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Remove a modifier from the set.
|
|
|
|
*
|
|
|
|
* parameter mod: The modifier to remove.
|
|
|
|
* parameter really: If true, actually remove the key. If false, do nothing.
|
|
|
|
* throws std::bad_alloc: Not enough memory.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void remove(modifier& mod, bool really = true) throw(std::bad_alloc);
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Construct modifier set from comma-separated string.
|
|
|
|
*
|
|
|
|
* parameter kbd: The keyboard to take the modifiers from.
|
|
|
|
* parameter modifiers: The modifiers as string
|
|
|
|
* returns: The constructed modifier set.
|
|
|
|
* throws std::bad_alloc: Not enough memory.
|
|
|
|
* throws std::runtime_error: Illegal modifier or wrong syntax.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
static modifier_set construct(keyboard& kbd, const std::string& modifiers) throw(std::bad_alloc,
|
2012-12-14 17:52:03 +02:00
|
|
|
std::runtime_error);
|
|
|
|
/**
|
|
|
|
* Check modifier against its mask for validity.
|
|
|
|
*
|
|
|
|
* This method checks that:
|
|
|
|
* - for each modifier in set, either that or its linkage group is in mask.
|
|
|
|
* - Both modifier and its linkage group isn't in either set or mask.
|
|
|
|
*
|
|
|
|
* parameter mask: The mask to check against.
|
|
|
|
* returns: True if set is valid, false if not.
|
|
|
|
* throws std::bad_alloc: Not enough memory.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
bool valid(modifier_set& mask) throw(std::bad_alloc);
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Check if this modifier set triggers the action.
|
|
|
|
*
|
|
|
|
* Modifier set triggers another if for each modifier or linkage group in mask:
|
|
|
|
* - Modifier appears in both set and trigger.
|
|
|
|
* - At least one modifier with this linkage group appears in both set and trigger.
|
|
|
|
* - Modifiers with this linkage group do not appear in either set nor trigger.
|
|
|
|
*
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
bool triggers(const modifier_set& trigger, const modifier_set& mask) throw(std::bad_alloc);
|
2012-12-22 16:16:46 +02:00
|
|
|
/**
|
|
|
|
* Stringify.
|
|
|
|
*/
|
|
|
|
operator std::string() const throw(std::bad_alloc);
|
2012-12-14 17:52:03 +02:00
|
|
|
/**
|
|
|
|
* Equality check.
|
|
|
|
*
|
|
|
|
* parameter m: Another set.
|
|
|
|
* returns: True if two sets are equal, false if not.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
bool operator==(const modifier_set& m) const throw();
|
2012-12-22 16:16:46 +02:00
|
|
|
/**
|
|
|
|
* Less than check.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
bool operator<(const modifier_set& m) const throw();
|
2012-12-14 17:52:03 +02:00
|
|
|
private:
|
2013-12-19 00:46:12 +02:00
|
|
|
friend std::ostream& operator<<(std::ostream& os, const modifier_set& m);
|
|
|
|
std::set<modifier*> set;
|
2012-12-14 17:52:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Debugging print. Prints textual version of set into stream.
|
|
|
|
*
|
|
|
|
* parameter os: The stream to print to.
|
|
|
|
* parameter m: The modifier set to print.
|
|
|
|
* returns: reference to os.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
std::ostream& operator<<(std::ostream& os, const modifier_set& m);
|
2012-12-14 17:52:03 +02:00
|
|
|
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Type of key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
enum keytype
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A simple key (pressed/released)
|
|
|
|
*/
|
|
|
|
KBD_KEYTYPE_KEY,
|
|
|
|
/**
|
|
|
|
* A joystick axis (pair of opposite directions or pressure-sensitive button).
|
|
|
|
*/
|
|
|
|
KBD_KEYTYPE_AXIS,
|
|
|
|
/**
|
|
|
|
* A joystick hat (directional control or a dpad).
|
|
|
|
*/
|
|
|
|
KBD_KEYTYPE_HAT,
|
|
|
|
/**
|
|
|
|
* A mouse axis.
|
|
|
|
*/
|
|
|
|
KBD_KEYTYPE_MOUSE
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Joystick axis calibration structure.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
struct axis_calibration
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Mode: -1 => Disabled, 0 => Pressure-sentive button, 1 => Axis.
|
|
|
|
*/
|
|
|
|
int mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mouse axis calibration structure.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
struct mouse_calibration
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The offset from left of screen area to left of game area.
|
|
|
|
*/
|
|
|
|
int32_t offset;
|
|
|
|
/**
|
|
|
|
* Translate from screen coordinate to game coordinate.
|
|
|
|
*/
|
|
|
|
int32_t get_calibrated_value(int32_t x) const throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Superclass of key event data.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class event
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a new event.
|
|
|
|
*
|
|
|
|
* Parameter _chngmask: The change mask.
|
|
|
|
* Parameter _type: Type of the event.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
event(uint32_t _chngmask, keytype _type) throw()
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
chngmask = _chngmask;
|
|
|
|
type = _type;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
virtual ~event() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state. The format is dependent on key type.
|
|
|
|
*/
|
|
|
|
virtual int32_t get_state() const throw() = 0;
|
|
|
|
/**
|
|
|
|
* Get key change mask.
|
|
|
|
*
|
|
|
|
* Returns: A bitmask. Bit 2*n is the state of subkey n. Bit 2*n+1 is set if subkey changed state, else clear.
|
|
|
|
*/
|
|
|
|
uint32_t get_change_mask() const throw() { return chngmask; }
|
|
|
|
/**
|
|
|
|
* Get type of event.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
keytype get_type() const throw() { return type; }
|
2012-12-16 20:45:16 +02:00
|
|
|
private:
|
|
|
|
uint32_t chngmask;
|
2013-12-19 00:46:12 +02:00
|
|
|
keytype type;
|
2012-12-16 20:45:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple key event.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class event_key : public event
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a new key event.
|
|
|
|
*
|
|
|
|
* Parameter chngmask: The change mask.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
event_key(uint32_t chngmask);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~event_key() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state.
|
|
|
|
*
|
|
|
|
* Returns: 1 if pressed, 0 if released.
|
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
private:
|
|
|
|
int32_t state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An axis event.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class event_axis : public event
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a new axis event.
|
|
|
|
*
|
|
|
|
* Parameter state: The analog state.
|
|
|
|
* Parameter chngmask: The change mask.
|
|
|
|
* Parameter cal: The calibration structure.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
event_axis(int32_t state, uint32_t chngmask);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~event_axis() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state.
|
|
|
|
*
|
2013-08-02 11:50:56 +03:00
|
|
|
* Returns: Analog position of axis, -32768...32767 (0...32767 for pressure-sensitive buttons).
|
2012-12-16 20:45:16 +02:00
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
private:
|
|
|
|
int32_t state;
|
2013-12-19 00:46:12 +02:00
|
|
|
axis_calibration cal;
|
2012-12-16 20:45:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hat event.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class event_hat : public event
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a new hat event.
|
|
|
|
*
|
|
|
|
* Parameter chngmask: The change mask to use.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
event_hat(uint32_t chngmask);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~event_hat() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state.
|
|
|
|
*
|
|
|
|
* Returns: Bitmask: 1 => Up, 2 => Right, 4 => Down, 8 => Left.
|
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A mouse event.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class event_mouse : public event
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a new mouse event.
|
|
|
|
*
|
|
|
|
* Parameter state: The game-relative position to use.
|
|
|
|
* Parameter cal: The calibration structure.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
event_mouse(int32_t state, const mouse_calibration& cal);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~event_mouse() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state.
|
|
|
|
*
|
|
|
|
* Returns: Position of mouse relative to game area (with right/down positive).
|
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
/**
|
|
|
|
* Get calibration data.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
mouse_calibration get_calibration() { return cal; }
|
2012-12-16 20:45:16 +02:00
|
|
|
private:
|
|
|
|
int32_t state;
|
2013-12-19 00:46:12 +02:00
|
|
|
mouse_calibration cal;
|
2012-12-16 20:45:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A keyboard event listener.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class event_listener
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
virtual ~event_listener() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Receive a key event.
|
|
|
|
*
|
|
|
|
* Parameter mods: Modifiers currently active.
|
|
|
|
* Parameter key: The key this event is about.
|
|
|
|
* Parameter event: The event.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
virtual void on_key_event(modifier_set& mods, key& key, event& event) = 0;
|
2012-12-16 20:45:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A (compound) key on keyboard.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class key
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Parameter keyb: The keyboard this is on.
|
|
|
|
* Parameter name: The base name of the key.
|
|
|
|
* Parameter clazz: The class of the key.
|
|
|
|
* Parameter type: The type of key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key(keyboard& keyb, const std::string& name, const std::string& clazz, keytype type)
|
2012-12-16 20:45:16 +02:00
|
|
|
throw(std::bad_alloc);
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
virtual ~key() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get class.
|
|
|
|
*/
|
|
|
|
const std::string& get_class() { return clazz; }
|
|
|
|
/**
|
|
|
|
* Get name.
|
|
|
|
*/
|
|
|
|
const std::string& get_name() { return name; }
|
|
|
|
/**
|
|
|
|
* Get keyboard this is on.
|
|
|
|
*/
|
|
|
|
keyboard& get_keyboard() { return kbd; }
|
|
|
|
/**
|
|
|
|
* Get key type.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
keytype get_type() const throw() { return type; }
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Add listener.
|
|
|
|
*
|
|
|
|
* Parameter listener: The listener.
|
|
|
|
* Parameter analog: If true, also pass analog events.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void add_listener(event_listener& listener, bool analog) throw(std::bad_alloc);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Remove listener.
|
|
|
|
*
|
|
|
|
* Parameter listener: The listener.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void remove_listener(event_listener& listener) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set exclusive listener.
|
|
|
|
*
|
|
|
|
* Parameter listener: The listener. NULL to ungrab key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_exclusive(event_listener* listener) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set analog state.
|
|
|
|
*
|
|
|
|
* Parameter mods: The current modifiers.
|
|
|
|
* Parameter state: The new state. The format is dependent on key type.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
virtual void set_state(modifier_set mods, int32_t state) throw() = 0;
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state. The format is dependent on key type.
|
|
|
|
*/
|
|
|
|
virtual int32_t get_state() const throw() = 0;
|
|
|
|
/**
|
|
|
|
* Get digital state. The format is dependent on key type.
|
|
|
|
*/
|
|
|
|
virtual int32_t get_state_digital() const throw() = 0;
|
|
|
|
/**
|
|
|
|
* Get the subkey suffixes.
|
|
|
|
*/
|
|
|
|
virtual std::vector<std::string> get_subkeys() throw(std::bad_alloc) = 0;
|
|
|
|
/**
|
|
|
|
* Dynamic cast to axis type.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key_axis* cast_axis() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Dynamic cast to mouse type.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key_mouse* cast_mouse() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Call all event listeners on this key.
|
|
|
|
*
|
|
|
|
* Parameter mods: The current modifiers.
|
|
|
|
* Parameter event: The event to pass.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void call_listeners(modifier_set& mods, event& event);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Mutex protecting state.
|
|
|
|
*/
|
2014-03-23 09:45:42 +02:00
|
|
|
mutable threads::lock mlock;
|
2012-12-16 20:45:16 +02:00
|
|
|
private:
|
2013-12-19 00:46:12 +02:00
|
|
|
key(key&);
|
|
|
|
key& operator=(key&);
|
2012-12-16 20:45:16 +02:00
|
|
|
keyboard& kbd;
|
|
|
|
std::string clazz;
|
|
|
|
std::string name;
|
2013-12-19 00:46:12 +02:00
|
|
|
std::set<event_listener*> digital_listeners;
|
|
|
|
std::set<event_listener*> analog_listeners;
|
|
|
|
event_listener* exclusive_listener;
|
|
|
|
keytype type;
|
2012-12-16 20:45:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple key on keyboard.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class key_key : public key
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Parameter keyb: The keyboard this is on.
|
|
|
|
* Parameter name: The base name of the key.
|
|
|
|
* Parameter clazz: The class of the key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key_key(keyboard& keyb, const std::string& name, const std::string& clazz) throw(std::bad_alloc);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~key_key() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set analog state.
|
|
|
|
*
|
|
|
|
* Parameter mods: The current modifiers.
|
|
|
|
* Parameter state: The new state. 1 for pressed, 0 for released.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_state(modifier_set mods, int32_t state) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state. 1 for pressed, 0 for released.
|
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
/**
|
|
|
|
* Get digital state. 1 for pressed, 0 for released.
|
|
|
|
*/
|
|
|
|
int32_t get_state_digital() const throw();
|
|
|
|
/**
|
|
|
|
* Get the subkey suffixes.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> get_subkeys() throw(std::bad_alloc);
|
|
|
|
private:
|
2013-12-19 00:46:12 +02:00
|
|
|
key_key(key_key&);
|
|
|
|
key_key& operator=(key_key&);
|
2012-12-16 20:45:16 +02:00
|
|
|
int32_t state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hat on keyboard.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class key_hat : public key
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Parameter keyb: The keyboard this is on.
|
|
|
|
* Parameter name: The base name of the key.
|
|
|
|
* Parameter clazz: The class of the key.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key_hat(keyboard& keyb, const std::string& name, const std::string& clazz) throw(std::bad_alloc);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~key_hat() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set analog state.
|
|
|
|
*
|
|
|
|
* Parameter mods: The current modifiers.
|
|
|
|
* Parameter state: The new state. 1 => up, 2 => right, 4 => down, 8 => left.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_state(modifier_set mods, int32_t state) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state. 1 => up, 2 => right, 4 => down, 8 => left.
|
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
/**
|
|
|
|
* Get digital state. 1 => up, 2 => right, 4 => down, 8 => left.
|
|
|
|
*/
|
|
|
|
int32_t get_state_digital() const throw();
|
|
|
|
/**
|
|
|
|
* Get the subkey suffixes.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> get_subkeys() throw(std::bad_alloc);
|
|
|
|
private:
|
2013-12-19 00:46:12 +02:00
|
|
|
key_hat(key_hat&);
|
|
|
|
key_hat& operator=(key_hat&);
|
2012-12-16 20:45:16 +02:00
|
|
|
int32_t state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An axis on keyboard.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class key_axis : public key
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Parameter keyb: The keyboard this is on.
|
|
|
|
* Parameter name: The base name of the key.
|
|
|
|
* Parameter clazz: The class of the key.
|
2013-08-02 11:50:56 +03:00
|
|
|
* Parameter mode: Initial mode: -1 => disabled, 0 => axis, 1 => pressure
|
2012-12-16 20:45:16 +02:00
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key_axis(keyboard& keyb, const std::string& name, const std::string& clazz, int mode)
|
2013-08-02 11:50:56 +03:00
|
|
|
throw(std::bad_alloc);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~key_axis() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set analog state.
|
|
|
|
*
|
|
|
|
* Parameter mods: The current modifiers.
|
|
|
|
* Parameter state: The new state. Uncalibrated analog position.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_state(modifier_set mods, int32_t state) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state. -32767...32767 for axes, 0...32767 for pressure-sensitive buttons.
|
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
/**
|
|
|
|
* Get digital state. -1 => left, 0 => center/unpressed, 1 => Right/pressed.
|
|
|
|
*/
|
|
|
|
int32_t get_state_digital() const throw();
|
|
|
|
/**
|
|
|
|
* Get the subkey suffixes.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> get_subkeys() throw(std::bad_alloc);
|
|
|
|
/**
|
2013-08-02 11:50:56 +03:00
|
|
|
* Get mode.
|
2012-12-16 20:45:16 +02:00
|
|
|
*/
|
2013-08-02 11:50:56 +03:00
|
|
|
int get_mode() const throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
2013-08-02 11:50:56 +03:00
|
|
|
* Set mode.
|
2012-12-16 20:45:16 +02:00
|
|
|
*/
|
2013-08-02 11:50:56 +03:00
|
|
|
void set_mode(int mode, double tolerance) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
private:
|
2013-12-19 00:46:12 +02:00
|
|
|
key_axis(key_axis&);
|
|
|
|
key_axis& operator=(key_axis&);
|
2012-12-16 20:45:16 +02:00
|
|
|
int32_t rawstate;
|
2013-08-02 11:50:56 +03:00
|
|
|
int digitalstate;
|
|
|
|
double last_tolerance;
|
|
|
|
int _mode;
|
2012-12-16 20:45:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A mouse axis on keyboard.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
class key_mouse : public key
|
2012-12-16 20:45:16 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Parameter keyb: The keyboard this is on.
|
|
|
|
* Parameter name: The base name of the key.
|
|
|
|
* Parameter clazz: The class of the key.
|
|
|
|
* Parameter cal: Initial calibration.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
key_mouse(keyboard& keyb, const std::string& name, const std::string& clazz,
|
|
|
|
mouse_calibration cal) throw(std::bad_alloc);
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
~key_mouse() throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set analog state.
|
|
|
|
*
|
|
|
|
* Parameter mods: The current modifiers.
|
|
|
|
* Parameter state: The new state. Screen-relative analog position.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_state(modifier_set mods, int32_t state) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Get analog state. Game-relative analog position.
|
|
|
|
*/
|
|
|
|
int32_t get_state() const throw();
|
|
|
|
/**
|
|
|
|
* Get digital state. Always returns 0.
|
|
|
|
*/
|
|
|
|
int32_t get_state_digital() const throw();
|
|
|
|
/**
|
|
|
|
* Get the subkey suffixes. Returns empty list.
|
|
|
|
*/
|
|
|
|
std::vector<std::string> get_subkeys() throw(std::bad_alloc);
|
|
|
|
/**
|
|
|
|
* Get calibration.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
mouse_calibration get_calibration() const throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
/**
|
|
|
|
* Set calibration.
|
|
|
|
*/
|
2013-12-19 00:46:12 +02:00
|
|
|
void set_calibration(mouse_calibration cal) throw();
|
2012-12-16 20:45:16 +02:00
|
|
|
private:
|
2013-12-19 00:46:12 +02:00
|
|
|
key_mouse(key_mouse&);
|
|
|
|
key_mouse& operator=(key_mouse&);
|
2012-12-16 20:45:16 +02:00
|
|
|
int32_t rawstate;
|
2013-12-19 00:46:12 +02:00
|
|
|
mouse_calibration cal;
|
2012-12-16 20:45:16 +02:00
|
|
|
};
|
2013-12-19 00:46:12 +02:00
|
|
|
}
|
2012-12-14 17:52:03 +02:00
|
|
|
#endif
|