From 266eb5fb14c07e67aa211a5860e9abf3009136e3 Mon Sep 17 00:00:00 2001 From: Sebastien Braun Date: Mon, 4 Oct 2010 00:22:14 +0200 Subject: Implement first version of basic input event support --- src/input/inputEvent.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/input/inputEvent.cpp (limited to 'src/input/inputEvent.cpp') diff --git a/src/input/inputEvent.cpp b/src/input/inputEvent.cpp new file mode 100644 index 0000000..04a84e7 --- /dev/null +++ b/src/input/inputEvent.cpp @@ -0,0 +1,98 @@ +/* + * inputEvent.cpp + * + * Created on: 06.09.2010 + * Author: brs + */ + +#include +#include +#include +#include +#include +#include +#include "inputEvent.h" +#include + +// We implement operators to serialize and load an event: +QDataStream& operator <<(QDataStream& ostrm, InputEvent const& evt) +{ + ostrm << evt.type_ << evt.code_ << evt.value_; + return ostrm; +} + +QDataStream& operator >>(QDataStream& istrm, InputEvent& evt) +{ + istrm >> evt.type_ >> evt.code_ >> evt.value_; + return istrm; +} + +void eventToString(InputEvent const& evt, QString& str) +{ + QByteArray ba; + QBuffer buf(&ba); + buf.open(QIODevice::WriteOnly); + QDataStream out(&buf); + out << evt; + str = QString::fromAscii(ba.toBase64()); +} + +bool eventFromString(QString const& str, InputEvent& evt) +{ + // TODO This does not do proper error checking. Only use from trusted sources! + QByteArray ba = QByteArray::fromBase64(str.toAscii()); + QBuffer buf(&ba); + buf.open(QIODevice::ReadOnly); + QDataStream in(&buf); + in >> evt; + return true; +} + +quint16 InputEvent::mouseButtonsFromQt(int b) +{ + quint16 ret = 0; + if(b & Qt::LeftButton) + { + ret |= EB_LEFT; + } + if(b & Qt::RightButton) + { + ret |= EB_RIGHT; + } + if(b & Qt::MidButton) + { + ret |= EB_MIDDLE; + } + return ret; +} + +InputEvent InputEvent::mousePressRelease(QMouseEvent const* evt) +{ + quint16 button = mouseButtonsFromQt(evt->button()); + quint16 buttons = mouseButtonsFromQt(evt->buttons()); + quint16 code; + + if(buttons & button) + { + code = EC_PRESS; + } + else + { + code = EC_RELEASE; + } + + quint32 value = ((quint32)button << 16) | buttons; + return InputEvent(ET_BUTTON, code, value); +} + +InputEvent InputEvent::keyboardPress(QKeyEvent const* evt) +{ + quint32 value = evt->key() | evt->modifiers(); + return InputEvent(ET_KEY, EC_PRESS, value); +} + +InputEvent InputEvent::keyboardRelease(QKeyEvent const* evt) +{ + quint32 value = evt->key() | evt->modifiers(); + return InputEvent(ET_KEY, EC_RELEASE, value); +} -- cgit v1.2.3-55-g7522 From 6f5efe4759276bb5f4add3db93b14051700d2e0d Mon Sep 17 00:00:00 2001 From: Sebastien Braun Date: Tue, 5 Oct 2010 14:57:32 +0200 Subject: Remove unnecessary Qt dependency from inputEvent.cpp --- src/gui/frame.cpp | 10 ++++------ src/input/inputEvent.cpp | 15 ++++++++------- src/input/inputEvent.h | 7 ++++--- 3 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/input/inputEvent.cpp') diff --git a/src/gui/frame.cpp b/src/gui/frame.cpp index 96ea26c..32257ec 100644 --- a/src/gui/frame.cpp +++ b/src/gui/frame.cpp @@ -255,7 +255,7 @@ void Frame::mousePressEvent(QMouseEvent* event) ConsoleLog writeLine("Captured remote control mousePressEvent"); updateMousePosition(event); - sendInputEvent(InputEvent::mousePressRelease(event)); + sendInputEvent(InputEvent::mousePressRelease(event->button(), event->buttons())); } } @@ -271,7 +271,7 @@ void Frame::mouseReleaseEvent ( QMouseEvent * event ) ConsoleLog writeLine("Captured remote control mouseReleaseEvent"); updateMousePosition(event); - sendInputEvent(InputEvent::mousePressRelease(event)); + sendInputEvent(InputEvent::mousePressRelease(event->button(), event->buttons())); } } @@ -501,8 +501,7 @@ void Frame::keyPressEvent(QKeyEvent* event) { // The action of the keyboard may depend on the position of the pointer sendMouseMotionEvent(); - InputEvent evt = InputEvent::keyboardPress(event); - sendInputEvent(evt); + sendInputEvent(InputEvent::keyboardPress(event->key(), event->modifiers())); } else { @@ -516,8 +515,7 @@ void Frame::keyReleaseEvent(QKeyEvent* event) { // The action of the keyboard may depend on the position of the pointer sendMouseMotionEvent(); - InputEvent evt = InputEvent::keyboardRelease(event); - sendInputEvent(evt); + sendInputEvent(InputEvent::keyboardRelease(event->key(), event->modifiers())); } else { diff --git a/src/input/inputEvent.cpp b/src/input/inputEvent.cpp index 04a84e7..9b69a3a 100644 --- a/src/input/inputEvent.cpp +++ b/src/input/inputEvent.cpp @@ -66,10 +66,10 @@ quint16 InputEvent::mouseButtonsFromQt(int b) return ret; } -InputEvent InputEvent::mousePressRelease(QMouseEvent const* evt) +InputEvent InputEvent::mousePressRelease(int qt_button, int qt_buttons) { - quint16 button = mouseButtonsFromQt(evt->button()); - quint16 buttons = mouseButtonsFromQt(evt->buttons()); + quint16 button = mouseButtonsFromQt(qt_button); + quint16 buttons = mouseButtonsFromQt(qt_buttons); quint16 code; if(buttons & button) @@ -85,14 +85,15 @@ InputEvent InputEvent::mousePressRelease(QMouseEvent const* evt) return InputEvent(ET_BUTTON, code, value); } -InputEvent InputEvent::keyboardPress(QKeyEvent const* evt) +InputEvent InputEvent::keyboardPress(int key, int mods) { - quint32 value = evt->key() | evt->modifiers(); + quint32 value = key | mods; return InputEvent(ET_KEY, EC_PRESS, value); } -InputEvent InputEvent::keyboardRelease(QKeyEvent const* evt) +InputEvent InputEvent::keyboardRelease(int key, int mods) { - quint32 value = evt->key() | evt->modifiers(); + quint32 value = key | mods; return InputEvent(ET_KEY, EC_RELEASE, value); } + diff --git a/src/input/inputEvent.h b/src/input/inputEvent.h index 917cc64..7a64bfc 100644 --- a/src/input/inputEvent.h +++ b/src/input/inputEvent.h @@ -62,9 +62,9 @@ public: static uint16_t mouseButtonsFromQt(int b); - static InputEvent mousePressRelease(QMouseEvent const* event); - static InputEvent keyboardPress(QKeyEvent const* event); - static InputEvent keyboardRelease(QKeyEvent const* event); + 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; @@ -76,6 +76,7 @@ public: 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; -- cgit v1.2.3-55-g7522