summaryrefslogtreecommitdiffstats
path: root/src/input/inputEvent.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input/inputEvent.cpp')
-rw-r--r--src/input/inputEvent.cpp98
1 files changed, 98 insertions, 0 deletions
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 <QBuffer>
+#include <QByteArray>
+#include <QDataStream>
+#include <QKeyEvent>
+#include <QMouseEvent>
+#include <QString>
+#include "inputEvent.h"
+#include <src/util/consoleLogger.h>
+
+// 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);
+}