summaryrefslogtreecommitdiffstats
path: root/src/input/inputEvent.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/input/inputEvent.h')
-rw-r--r--src/input/inputEvent.h315
1 files changed, 247 insertions, 68 deletions
diff --git a/src/input/inputEvent.h b/src/input/inputEvent.h
index 7a64bfc..afb33e5 100644
--- a/src/input/inputEvent.h
+++ b/src/input/inputEvent.h
@@ -18,19 +18,26 @@
#define INPUTEVENT_H_
#include <cassert>
-#include <string>
-#include <sstream>
-#include <stdint.h>
-
-#ifndef __linux
-# error "This will only run on a Linux system. Porting is required for other systems."
-#endif
+#include <QCoreApplication> // for translation
+#include <QString>
struct QDataStream;
struct QString;
struct QMouseEvent;
struct QKeyEvent;
+/**
+ * The inputEvent class represents a single input event.
+ * An input event is any of:
+ * - Pressing or releasing a keyboard key,
+ * - Moving the mouse pointer,
+ * - Pressing or releasing a mouse button,
+ * - Special system events like "reboot".
+ *
+ * Events are described by the \ref type(), \ref code()
+ * and \ref value(). Possible values for \ref type() are given by
+ * the \c ET_* constants, and for \ref code() by the \c EC_* constants.
+ */
class InputEvent
{
private:
@@ -40,125 +47,250 @@ private:
friend void eventToString(InputEvent const& evt, QString& str);
friend bool eventFromString(QString const& str, InputEvent& evt);
- uint16_t type_;
- uint16_t code_;
- uint32_t value_;
+ quint16 _type;
+ quint16 _code;
+ quint32 _value;
// InputEvents are immutable. Prohibit assignment:
InputEvent& operator=(InputEvent const&); // There intentionally is no implementation.
+
public:
- InputEvent(uint16_t type, uint16_t code, uint32_t value) : type_(type), code_(code), value_(value)
+ InputEvent(quint16 type, quint16 code, quint32 value = 0) : _type(type), _code(code), _value(value)
{
}
- InputEvent()
+ InputEvent(InputEvent const& other) : _type(other._type), _code(other._code), _value(other._value)
{
}
- static InputEvent mouseMotion(uint16_t x, uint16_t y)
+ InputEvent()
{
- return InputEvent(ET_POINTER, 0, ((uint32_t)x << 16) | y);
}
- static uint16_t mouseButtonsFromQt(int b);
+ /** \name Factory Functions */
+ /* @{ */
+ /**
+ * Generates an event for a mouse button press or release.
+ * \param button The button that caused this event, as defined by \ref Qt::MouseButton.
+ * \param buttons The buttons that were pressed at the moment the event was generated, as defined by \ref Qt::MouseButtons.
+ * If the event is a button press, this includes the pressed button. If it is a button release,
+ * it does not include the pressed button.
+ */
static InputEvent mousePressRelease(int button, int buttons);
- static InputEvent keyboardPress(int key, int mods);
- static InputEvent keyboardRelease(int key, int mods);
- static const uint16_t ET_KEY = 0;
- static const uint16_t ET_BUTTON = 1;
- static const uint16_t ET_POINTER = 2;
- static const uint16_t ET_SPECIAL = 3;
-
- static const uint16_t EC_PRESS = 0;
- static const uint16_t EC_RELEASE = 1;
- static const uint16_t EC_REBOOT = 2;
- static const uint16_t EC_SYSRQ = 3;
- static const uint16_t EC_KILL_X = 4;
- static const uint16_t EC_SAY_HELLO = 5; //< for debugging purposes
-
- typedef uint32_t event_key;
+ /**
+ * Generates an event for a keyboard press.
+ * \param key The key code that generated this event, as defined by \ref Qt::Key.
+ * \param mods The modificator keys that were pressed at the moment the event was generated,
+ * as defined by \ref Qt::KeyboardModifiers.
+ */
+ static InputEvent keyboardPress(int key, int mods);
- typedef uint32_t event_key_modifiers;
+ /**
+ * Generates an event for a keyboard release.
+ * \param key The key code that generated this event, as defined by \ref Qt::Key.
+ * \param mods The modificator keys that were pressed at the moment the event was generated,
+ * as defined by \ref Qt::KeyboardModifiers.
+ */
+ static InputEvent keyboardRelease(int key, int mods);
- static const uint16_t EB_LEFT = 1;
- static const uint16_t EB_MIDDLE = 2;
- static const uint16_t EB_RIGHT = 4;
+ /**
+ * Generates an event for a mouse pointer movement.
+ * \param x,y X and Y coordinates of the mouse pointer.
+ */
+ static InputEvent mouseMotion(uint16_t x, uint16_t y)
+ {
+ return InputEvent(ET_POINTER, 0, ((uint32_t)x << 16) | y);
+ }
- static const uint32_t MODIFIER_MASK =
+ /* @} */
+
+ /** \name Event Types */
+ /* @{ */
+ static const uint16_t ET_KEY = 0; /**< The event is related to the keyboard */
+ static const uint16_t ET_BUTTON = 1; /**< The event is related to a mouse button */
+ static const uint16_t ET_POINTER = 2; /**< The event is related to the mouse pointer */
+ static const uint16_t ET_SPECIAL = 3; /**< The event is a special system event */
+ /* @} */
+
+ /** \name Event Codes */
+ /* @{ */
+ static const uint16_t EC_PRESS = 0; /**< The event is a press (keyboard or mouse button) */
+ static const uint16_t EC_RELEASE = 1; /**< The event is a release (keyboard or mouse button) */
+ static const uint16_t EC_REBOOT = 2; /**< The event is a request to reboot (special system event) */
+ static const uint16_t EC_SYSRQ = 3; /**< The event is a request to perform a Magic-SysRq function (special system event) */
+ static const uint16_t EC_KILL_X = 4; /**< The event is a request to kill the X Server (special system event) */
+ static const uint16_t EC_SAY_HELLO = 5; /**< The event is a request to write a line to the log file (special system event) */
+ /* @} */
+
+ /** \name Mouse Button Flags */
+ /* @{ */
+ static const uint16_t EB_LEFT = 1; /**< The left mouse button */
+ static const uint16_t EB_MIDDLE = 2; /**< The middle mouse button */
+ static const uint16_t EB_RIGHT = 4; /**< The right mouse button */
+ /* @} */
+
+ static const quint32 MODIFIER_MASK =
0x7e000000;
+ /** \name Event Decoding Functions */
+ /* @{ */
+
+ /**
+ * Return the event type, as defined by the \c ET_* constants.
+ */
uint16_t type() const
{
- return type_;
+ return _type;
}
+ /**
+ * Return the event code, as defined by the \c EC_* constants.
+ */
uint16_t code() const
{
- return code_;
+ return _code;
}
+ /**
+ * Return the value associated with the event. The interpretation
+ * differs according to event type and code:
+ * - If the event type is \c ET_KEY, the value specifies which
+ * key and modifiers were pressed.
+ * - If the event type is \c ET_BUTTON, the value specifies which
+ * buttons were pressed and which button generated the event.
+ * - If the event type is \c ET_POINTER, the value specifies the
+ * screen coordinates the mouse has moved to.
+ * - If the event type is \c ET_SPECIAL, the interpretation
+ * depends on the event code.
+ */
uint32_t value() const
{
- return value_;
+ return _value;
}
+ /**
+ * True if the event is a keyboard event.
+ */
bool isKeyboard() const
{
- return type_ == ET_KEY;
+ return _type == ET_KEY;
}
+ /**
+ * True if the event is a mouse button event.
+ */
bool isButton() const
{
- return type_ == ET_BUTTON;
+ return _type == ET_BUTTON;
}
+ /**
+ * True if the event is a mouse pointer event.
+ */
bool isPointer() const
{
- return type_ == ET_POINTER;
+ return _type == ET_POINTER;
}
+ /**
+ * True if the event is a special system event.
+ */
bool isSpecial() const
{
- return type_ == ET_SPECIAL;
+ return _type == ET_SPECIAL;
}
+ /**
+ * True if the event is a keyboard or mouse button press event.
+ */
bool isPress() const
{
- return code_ == EC_PRESS;
+ return _code == EC_PRESS;
}
+ /**
+ * True if the event is a keyboard or mouse button release event.
+ */
bool isRelease() const
{
- return code_ == EC_RELEASE;
+ return _code == EC_RELEASE;
}
+ /**
+ * The mouse button that generated this event.
+ * \return one of the \c EB_* constants.
+ */
uint16_t pressedButton() const
{
- assert(type_ == ET_BUTTON);
- return (value_ >> 16);
+ assert(_type == ET_BUTTON);
+ return (_value >> 16);
}
+ /**
+ * The mouse buttons that were pressed at the moment the
+ * event was generated.
+ * \return a bitwise or of \c EB_* constants.
+ */
uint16_t heldButtons() const
{
- assert(type_ == ET_BUTTON);
- return (value_ & 0xffff);
+ assert(_type == ET_BUTTON);
+ return (_value & 0xffff);
}
+ /**
+ * The X coordinate the pointer was moved to.
+ */
uint16_t xCoord() const
{
- assert(type_ == ET_POINTER);
- return (value_ >> 16);
+ assert(_type == ET_POINTER);
+ return (_value >> 16);
}
+ /**
+ * The Y coordinate the pointer was moved to.
+ */
uint16_t yCoord() const
{
- assert(type_ == ET_POINTER);
- return (value_ & 0xffff);
+ assert(_type == ET_POINTER);
+ return (_value & 0xffff);
}
- static std::string typeToString(uint16_t type)
+ /**
+ * The key that generated the event, as defined by \ref Qt::Key.
+ */
+ quint32 qtKeysym() const
+ {
+ return _value & ~MODIFIER_MASK;
+ }
+
+ /**
+ * The modifier keys that were pressed at the moment the event was generated,
+ * as defined by \ref Qt::KeyboardModifiers.
+ */
+ quint32 qtModifiers() const
+ {
+ return _value & MODIFIER_MASK;
+ }
+ /* @} */
+
+
+ /** \name Conversion Functions */
+ /* @{ */
+
+ /**
+ * Converts mouse buttons from the bit flags defined by \ref Qt::MouseButton
+ * to their internal representation as defined in \ref "Mouse Button Flags".
+ * \returns A bitwise or of \c EB_* values.
+ */
+ static uint16_t mouseButtonsFromQt(int b);
+
+ /**
+ * Converts an event type as given by the \c ET_* constants to
+ * a string.
+ */
+ static QString typeToString(quint16 type)
{
switch(type)
{
@@ -171,13 +303,15 @@ public:
case ET_SPECIAL:
return "SPECIAL";
default:
- std::ostringstream s;
- s << std::hex << type;
- return s.str();
+ return QString::number(type, 16);
}
}
- static std::string codeToString(uint16_t code)
+ /**
+ * Converts an event code as given by the \c EC_* constants to
+ * a string.
+ */
+ static QString codeToString(quint16 code)
{
switch(code)
{
@@ -192,28 +326,73 @@ public:
case EC_KILL_X:
return "KILL_X";
default:
- std::ostringstream s;
- s << std::hex << code;
- return s.str();
+ return QString::number(code, 16);
}
}
- std::string toString() const
+ /**
+ * Convert the event to a human-readable representation.
+ */
+ QString toString() const
{
- std::ostringstream s;
- s << typeToString(type_) << ':' << codeToString(code_) << ':' << std::hex << value_;
- return s.str();
+ return QString("%1:%2:%3").arg(typeToString(_type)).arg(codeToString(_code)).arg(_value, 8, 16, QLatin1Char('0'));
}
- uint32_t qt_keysym() const
+ /* @} */
+
+ // We want to enable InputEvent as a translation context, so we fake the tr method:
+ static QString tr(const char* string)
{
- return value_ & ~MODIFIER_MASK;
+ return QCoreApplication::translate("InputEvent", string);
}
+};
- uint32_t qt_modifiers() const
+/**
+ * The SpecialInputEventDescription class provides a human-readable
+ * name for special system events.
+ *
+ * To use the translations of these descriptions in your program,
+ * you need to link to the \c libpvsinput library and
+ * use the \ref USE_PVSINPUT_TRANSLATIONS macro in your
+ * \c main function.
+ */
+struct SpecialInputEventDescription
+{
+ /**
+ * Initialize an instance of the SpecialInputEventDescription class.
+ * This is only meant to be called from describeSpecialEvents().
+ * If you need to add more special event descriptions, do so from there.
+ */
+ SpecialInputEventDescription(quint16 type, quint16 code, quint32 value, QString const& description_)
+ : type(type), code(code), value(value), description(description_)
{
- return value_ & MODIFIER_MASK;
}
+
+ /** \name InputEvent fields
+ * \see InputEvent
+ */
+ /* @{ */
+ quint16 type;
+ quint16 code;
+ quint32 value;
+ /* @} */
+
+ QString description; /**< Human-readable description for a special input event. Can be translated via the
+ InputEvent::tr(char const*) method. */
+
+ /**
+ * Returns an \ref InputEvent corresponding to this description.
+ */
+ InputEvent toEvent() const
+ {
+ return InputEvent(type, code, value);
+ }
+
+ /**
+ * Returns a \ref QList of all known special system events with their human-readable
+ * description.
+ */
+ static QList<SpecialInputEventDescription> describeSpecialEvents();
};
#endif /* INPUTEVENT_H_ */