/* # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg # # This program is free software distributed under the GPL version 2. # See http://openslx.org/COPYING # # If you have any feedback please consult http://openslx.org/feedback and # send your suggestions, praise, or complaints to feedback@openslx.org # # General information about OpenSLX can be found at http://openslx.org/ # -------------------------------------------------------------------------- # inputEvent.h: # - Definition of an input event # -------------------------------------------------------------------------- */ #ifndef INPUTEVENT_H_ #define INPUTEVENT_H_ #include #include // for translation #include #ifndef __linux # error "This will only run on a Linux system. Porting is required for other systems." #endif struct QDataStream; struct QString; struct QMouseEvent; struct QKeyEvent; class InputEvent { private: friend QDataStream& operator<<(QDataStream&, InputEvent const&); friend QDataStream& operator>>(QDataStream&, InputEvent&); friend void eventToString(InputEvent const& evt, QString& str); friend bool eventFromString(QString const& str, InputEvent& evt); quint16 _type; quint16 _code; quint32 _value; // InputEvents are immutable. Prohibit assignment: InputEvent& operator=(InputEvent const&); // There intentionally is no implementation. public: InputEvent(quint16 type, quint16 code, quint32 value = 0) : _type(type), _code(code), _value(value) { } InputEvent(InputEvent const& other) : _type(other._type), _code(other._code), _value(other._value) { } InputEvent() { } static InputEvent mouseMotion(quint16 x, quint16 y) { return InputEvent(ET_POINTER, 0, ((quint32)x << 16) | y); } static quint16 mouseButtonsFromQt(int b); static InputEvent mousePressRelease(int button, int buttons); static InputEvent keyboardPress(int key, int mods); static InputEvent keyboardRelease(int key, int mods); static const quint16 ET_KEY = 0; static const quint16 ET_BUTTON = 1; static const quint16 ET_POINTER = 2; static const quint16 ET_SPECIAL = 3; static const quint16 EC_PRESS = 0; static const quint16 EC_RELEASE = 1; static const quint16 EC_REBOOT = 2; static const quint16 EC_SYSRQ = 3; static const quint16 EC_KILL_X = 4; static const quint16 EC_SAY_HELLO = 5; //< for debugging purposes typedef quint32 event_key; typedef quint32 event_key_modifiers; static const quint16 EB_LEFT = 1; static const quint16 EB_MIDDLE = 2; static const quint16 EB_RIGHT = 4; static const quint32 MODIFIER_MASK = 0x7e000000; quint16 type() const { return _type; } quint16 code() const { return _code; } quint32 value() const { return _value; } bool isKeyboard() const { return _type == ET_KEY; } bool isButton() const { return _type == ET_BUTTON; } bool isPointer() const { return _type == ET_POINTER; } bool isSpecial() const { return _type == ET_SPECIAL; } bool isPress() const { return _code == EC_PRESS; } bool isRelease() const { return _code == EC_RELEASE; } quint16 pressedButton() const { assert(_type == ET_BUTTON); return (_value >> 16); } quint16 heldButtons() const { assert(_type == ET_BUTTON); return (_value & 0xffff); } quint16 xCoord() const { assert(_type == ET_POINTER); return (_value >> 16); } quint16 yCoord() const { assert(_type == ET_POINTER); return (_value & 0xffff); } static QString typeToString(quint16 type) { switch(type) { case ET_BUTTON: return "BUTTON"; case ET_KEY: return "KEY"; case ET_POINTER: return "POINTER"; case ET_SPECIAL: return "SPECIAL"; default: return QString::number(type, 16); } } static QString codeToString(quint16 code) { switch(code) { case EC_PRESS: return "PRESS"; case EC_RELEASE: return "RELEASE"; case EC_SYSRQ: return "SYSRQ"; case EC_REBOOT: return "REBOOT"; case EC_KILL_X: return "KILL_X"; default: return QString::number(code, 16); } } QString toString() const { return QString("%1:%2:%3").arg(typeToString(_type)).arg(codeToString(_code)).arg(_value, 16); } quint32 qtKeysym() const { return _value & ~MODIFIER_MASK; } quint32 qtModifiers() const { return _value & MODIFIER_MASK; } // We want to enable InputEvent as a translation context, so we fake the tr method: static QString tr(const char* string) { return QCoreApplication::translate("InputEvent", string); } }; struct SpecialInputEventDescription { SpecialInputEventDescription(quint16 type, quint16 code, quint32 value, QString const& description_) : type(type), code(code), value(value), description(description_) { } quint16 type; quint16 code; quint32 value; QString description; InputEvent toEvent() const { return InputEvent(type, code, value); } static QList describeSpecialEvents(); }; #endif /* INPUTEVENT_H_ */