Qt 6.x
The Qt SDK
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
20#if defined(QQNXNAVIGATOREVENTNOTIFIER_DEBUG)
21#define qNavigatorEventNotifierDebug qDebug
22#else
23#define qNavigatorEventNotifierDebug QT_NO_QDEBUG_MACRO
24#endif
25
27
28const char *QQnxNavigatorEventNotifier::navigatorControlPath = "/pps/services/navigator/control";
29const size_t QQnxNavigatorEventNotifier::ppsBufferSize = 4096;
30
32 : QObject(parent),
33 m_fd(-1),
34 m_readNotifier(0),
35 m_eventHandler(eventHandler)
36{
37}
38
40{
41 delete m_readNotifier;
42
43 // close connection to navigator
44 if (m_fd != -1)
45 close(m_fd);
46
47 qNavigatorEventNotifierDebug("navigator event notifier stopped");
48}
49
51{
52 qNavigatorEventNotifierDebug("navigator event notifier started");
53
54 // open connection to navigator
55 errno = 0;
56 m_fd = open(navigatorControlPath, O_RDWR);
57 if (m_fd == -1) {
58 qNavigatorEventNotifierDebug("failed to open navigator pps: %s", strerror(errno));
59 return;
60 }
61
62 m_readNotifier = new QSocketNotifier(m_fd, QSocketNotifier::Read);
63 connect(m_readNotifier, SIGNAL(activated(QSocketDescriptor)), this, SLOT(readData()));
64}
65
66void QQnxNavigatorEventNotifier::parsePPS(const QByteArray &ppsData, QByteArray &msg, QByteArray &dat, QByteArray &id)
67{
68 qNavigatorEventNotifierDebug() << "data=" << ppsData;
69
70 // tokenize pps data into lines
71 QList<QByteArray> lines = ppsData.split('\n');
72
73 // validate pps object
74 if (Q_UNLIKELY(lines.empty() || lines.at(0) != "@control"))
75 qFatal("QQNX: unrecognized pps object, data=%s", ppsData.constData());
76
77 // parse pps object attributes and extract values
78 for (int i = 1; i < lines.size(); ++i) {
79
80 // tokenize current attribute
81 const QByteArray &attr = lines.at(i);
82 qNavigatorEventNotifierDebug() << "attr=" << attr;
83
84 int firstColon = attr.indexOf(':');
85 if (firstColon == -1) {
86 // abort - malformed attribute
87 continue;
88 }
89
90 int secondColon = attr.indexOf(':', firstColon + 1);
91 if (secondColon == -1) {
92 // abort - malformed attribute
93 continue;
94 }
95
96 QByteArray key = attr.left(firstColon);
97 QByteArray value = attr.mid(secondColon + 1);
98
99 qNavigatorEventNotifierDebug() << "key=" << key;
100 qNavigatorEventNotifierDebug() << "val=" << value;
101
102 // save attribute value
103 if (key == "msg")
104 msg = value;
105 else if (key == "dat")
106 dat = value;
107 else if (key == "id")
108 id = value;
109 else
110 qFatal("QQNX: unrecognized pps attribute, attr=%s", key.constData());
111 }
112}
113
114void QQnxNavigatorEventNotifier::replyPPS(const QByteArray &res, const QByteArray &id, const QByteArray &dat)
115{
116 // construct pps message
117 QByteArray ppsData = "res::";
118 ppsData += res;
119 ppsData += "\nid::";
120 ppsData += id;
121 if (!dat.isEmpty()) {
122 ppsData += "\ndat::";
123 ppsData += dat;
124 }
125 ppsData += "\n";
126
127 qNavigatorEventNotifierDebug() << "reply=" << ppsData;
128
129 // send pps message to navigator
130 errno = 0;
131 int bytes = write(m_fd, ppsData.constData(), ppsData.size());
132 if (Q_UNLIKELY(bytes == -1))
133 qFatal("QQNX: failed to write navigator pps, errno=%d", errno);
134}
135
136void QQnxNavigatorEventNotifier::handleMessage(const QByteArray &msg, const QByteArray &dat, const QByteArray &id)
137{
138 qNavigatorEventNotifierDebug() << "msg=" << msg << ", dat=" << dat << ", id=" << id;
139
140 // check message type
141 if (msg == "orientationCheck") {
142 const bool response = m_eventHandler->handleOrientationCheck(dat.toInt());
143
144 // reply to navigator that (any) orientation is acceptable
145 replyPPS(msg, id, response ? "true" : "false");
146 } else if (msg == "orientation") {
147 m_eventHandler->handleOrientationChange(dat.toInt());
148 replyPPS(msg, id, "");
149 } else if (msg == "SWIPE_DOWN") {
150 m_eventHandler->handleSwipeDown();
151 } else if (msg == "exit") {
152 m_eventHandler->handleExit();
153 } else if (msg == "windowActive") {
154 m_eventHandler->handleWindowGroupActivated(dat);
155 } else if (msg == "windowInactive") {
156 m_eventHandler->handleWindowGroupDeactivated(dat);
157 }
158}
159
160void QQnxNavigatorEventNotifier::readData()
161{
162 qNavigatorEventNotifierDebug("reading navigator data");
163
164 // allocate buffer for pps data
165 char buffer[ppsBufferSize];
166
167 // attempt to read pps data
168 errno = 0;
169 int bytes = qt_safe_read(m_fd, buffer, ppsBufferSize - 1);
170 if (Q_UNLIKELY(bytes == -1))
171 qFatal("QQNX: failed to read navigator pps, errno=%d", errno);
172
173 // check if pps data was received
174 if (bytes > 0) {
175
176 // ensure data is null terminated
177 buffer[bytes] = '\0';
178
179 // process received message
180 QByteArray ppsData(buffer);
181 QByteArray msg;
182 QByteArray dat;
184 parsePPS(ppsData, msg, dat, id);
185 handleMessage(msg, dat, id);
186 }
187}
188
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
int toInt(bool *ok=nullptr, int base=10) const
Returns the byte array converted to an int using base base, which is ten by default.
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays.
qsizetype indexOf(char c, qsizetype from=0) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:106
QByteArray left(qsizetype len) const
Returns a byte array that contains the first len bytes of this byte array.
QByteArray mid(qsizetype index, qsizetype len=-1) const
Returns a byte array containing len bytes from this byte array, starting at position pos.
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool empty() const noexcept
Definition qlist.h:682
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
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)
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define qFatal
Definition qlogging.h:164
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLuint64 key
GLenum GLuint id
[7]
GLenum GLuint buffer
GLuint res
#define qNavigatorEventNotifierDebug
file open(QIODevice::ReadOnly)
gzip write("uncompressed data")
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent