Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqnxnavigatoreventnotifier.cpp
Go to the documentation of this file.
1// Copyright (C) 2011 - 2012 Research In Motion
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
7
8#include <QtCore/QByteArray>
9#include <QtCore/QDebug>
10#include <QtCore/QList>
11#include <QtCore/QSocketNotifier>
12#include <QtCore/private/qcore_unix_p.h>
13
14#include <errno.h>
15#include <fcntl.h>
16#include <unistd.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19
21
22// Q_LOGGING_CATEGORY(lcQpaQnxNavigatorEvents, "qt.qpa.qnx.navigator.events");
23
24const char *QQnxNavigatorEventNotifier::navigatorControlPath = "/pps/services/navigator/control";
25const size_t QQnxNavigatorEventNotifier::ppsBufferSize = 4096;
26
28 : QObject(parent),
29 m_fd(-1),
30 m_readNotifier(0),
31 m_eventHandler(eventHandler)
32{
33}
34
36{
37 delete m_readNotifier;
38
39 // close connection to navigator
40 if (m_fd != -1)
41 close(m_fd);
42
43 qCDebug(lcQpaQnxNavigatorEvents) << "Navigator event notifier stopped";
44}
45
47{
48 qCDebug(lcQpaQnxNavigatorEvents) << "Navigator event notifier started";
49
50 // open connection to navigator
51 errno = 0;
52 m_fd = open(navigatorControlPath, O_RDWR);
53 if (m_fd == -1) {
54 qCDebug(lcQpaQnxNavigatorEvents, "Failed to open navigator pps: %s", strerror(errno));
55 return;
56 }
57
58 m_readNotifier = new QSocketNotifier(m_fd, QSocketNotifier::Read);
59 connect(m_readNotifier, SIGNAL(activated(QSocketDescriptor)), this, SLOT(readData()));
60}
61
62void QQnxNavigatorEventNotifier::parsePPS(const QByteArray &ppsData, QByteArray &msg, QByteArray &dat, QByteArray &id)
63{
64 qCDebug(lcQpaQnxNavigatorEvents) << Q_FUNC_INFO << "data=" << ppsData;
65
66 // tokenize pps data into lines
67 QList<QByteArray> lines = ppsData.split('\n');
68
69 // validate pps object
70 if (Q_UNLIKELY(lines.empty() || lines.at(0) != "@control"))
71 qFatal("QQNX: unrecognized pps object, data=%s", ppsData.constData());
72
73 // parse pps object attributes and extract values
74 for (int i = 1; i < lines.size(); ++i) {
75
76 // tokenize current attribute
77 const QByteArray &attr = lines.at(i);
78 qCDebug(lcQpaQnxNavigatorEvents) << Q_FUNC_INFO << "attr=" << attr;
79
80 int firstColon = attr.indexOf(':');
81 if (firstColon == -1) {
82 // abort - malformed attribute
83 continue;
84 }
85
86 int secondColon = attr.indexOf(':', firstColon + 1);
87 if (secondColon == -1) {
88 // abort - malformed attribute
89 continue;
90 }
91
92 QByteArray key = attr.left(firstColon);
93 QByteArray value = attr.mid(secondColon + 1);
94
95 qCDebug(lcQpaQnxNavigatorEvents) << Q_FUNC_INFO << "key =" << key << "value =" << value;
96
97 // save attribute value
98 if (key == "msg")
99 msg = value;
100 else if (key == "dat")
101 dat = value;
102 else if (key == "id")
103 id = value;
104 else
105 qFatal("QQNX: unrecognized pps attribute, attr=%s", key.constData());
106 }
107}
108
109void QQnxNavigatorEventNotifier::replyPPS(const QByteArray &res, const QByteArray &id, const QByteArray &dat)
110{
111 // construct pps message
112 QByteArray ppsData = "res::";
113 ppsData += res;
114 ppsData += "\nid::";
115 ppsData += id;
116 if (!dat.isEmpty()) {
117 ppsData += "\ndat::";
118 ppsData += dat;
119 }
120 ppsData += "\n";
121
122 qCDebug(lcQpaQnxNavigatorEvents) << Q_FUNC_INFO << "reply=" << ppsData;
123
124 // send pps message to navigator
125 errno = 0;
126 int bytes = write(m_fd, ppsData.constData(), ppsData.size());
127 if (Q_UNLIKELY(bytes == -1))
128 qFatal("QQNX: failed to write navigator pps, errno=%d", errno);
129}
130
131void QQnxNavigatorEventNotifier::handleMessage(const QByteArray &msg, const QByteArray &dat, const QByteArray &id)
132{
133 qCDebug(lcQpaQnxNavigatorEvents) << Q_FUNC_INFO << "msg=" << msg << ", dat=" << dat << ", id=" << id;
134
135 // check message type
136 if (msg == "orientationCheck") {
137 const bool response = m_eventHandler->handleOrientationCheck(dat.toInt());
138
139 // reply to navigator that (any) orientation is acceptable
140 replyPPS(msg, id, response ? "true" : "false");
141 } else if (msg == "orientation") {
142 m_eventHandler->handleOrientationChange(dat.toInt());
143 replyPPS(msg, id, "");
144 } else if (msg == "SWIPE_DOWN") {
145 m_eventHandler->handleSwipeDown();
146 } else if (msg == "exit") {
147 m_eventHandler->handleExit();
148 } else if (msg == "windowActive") {
149 m_eventHandler->handleWindowGroupActivated(dat);
150 } else if (msg == "windowInactive") {
151 m_eventHandler->handleWindowGroupDeactivated(dat);
152 }
153}
154
155void QQnxNavigatorEventNotifier::readData()
156{
157 qCDebug(lcQpaQnxNavigatorEvents) << "Reading navigator data";
158
159 // allocate buffer for pps data
160 char buffer[ppsBufferSize];
161
162 // attempt to read pps data
163 errno = 0;
164 int bytes = qt_safe_read(m_fd, buffer, ppsBufferSize - 1);
165 if (Q_UNLIKELY(bytes == -1))
166 qFatal("QQNX: failed to read navigator pps, errno=%d", errno);
167
168 // check if pps data was received
169 if (bytes > 0) {
170
171 // ensure data is null terminated
172 buffer[bytes] = '\0';
173
174 // process received message
175 QByteArray ppsData(buffer);
176 QByteArray msg;
177 QByteArray dat;
179 parsePPS(ppsData, msg, dat, id);
180 handleMessage(msg, dat, id);
181 }
182}
183
\inmodule QtCore
Definition qbytearray.h:57
QByteArray left(qsizetype n) const &
Definition qbytearray.h:169
qsizetype indexOf(char c, qsizetype from=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
QByteArray mid(qsizetype index, qsizetype len=-1) const &
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void handleWindowGroupActivated(const QByteArray &id)
void handleWindowGroupDeactivated(const QByteArray &id)
QQnxNavigatorEventNotifier(QQnxNavigatorEventHandler *eventHandler, QObject *parent=nullptr)
\inmodule QtCore
\inmodule QtCore
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
#define Q_FUNC_INFO
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qFatal
Definition qlogging.h:168
#define qCDebug(category,...)
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLuint64 key
GLenum GLuint id
[7]
GLenum GLuint buffer
GLuint res
file open(QIODevice::ReadOnly)
gzip write("uncompressed data")