1 /***************************************************************************
2 **
3 ** Copyright (C) 2017 QNX Software Systems. All rights reserved.
4 ** Copyright (C) 2011 - 2012 Research In Motion
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the plugins of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "qqnxglobal.h"
42
43 #include "qqnxscreeneventthread.h"
44 #include "qqnxscreeneventhandler.h"
45
46 #include <QtCore/QDebug>
47
48 #include <errno.h>
49 #include <unistd.h>
50
51 #include <cctype>
52
53 #if defined(QQNXSCREENEVENTTHREAD_DEBUG)
54 #define qScreenEventThreadDebug qDebug
55 #else
56 #define qScreenEventThreadDebug QT_NO_QDEBUG_MACRO
57 #endif
58
59 static const int c_screenCode = _PULSE_CODE_MINAVAIL + 0;
60 static const int c_armCode = _PULSE_CODE_MINAVAIL + 1;
61 static const int c_quitCode = _PULSE_CODE_MINAVAIL + 2;
62
63 #if !defined(screen_register_event)
screen_register_event(screen_context_t,struct sigevent * event)64 int screen_register_event(screen_context_t, struct sigevent *event)
65 {
66 return MsgRegisterEvent(event, -1);
67 }
68
screen_unregister_event(struct sigevent * event)69 int screen_unregister_event(struct sigevent *event)
70 {
71 return MsgUnregisterEvent(event);
72 }
73 #endif
74
QQnxScreenEventThread(screen_context_t context)75 QQnxScreenEventThread::QQnxScreenEventThread(screen_context_t context)
76 : QThread()
77 , m_screenContext(context)
78 {
79 m_channelId = ChannelCreate(_NTO_CHF_DISCONNECT | _NTO_CHF_UNBLOCK | _NTO_CHF_PRIVATE);
80 if (m_channelId == -1) {
81 qFatal("QQnxScreenEventThread: Can't continue without a channel");
82 }
83
84 m_connectionId = ConnectAttach(0, 0, m_channelId, _NTO_SIDE_CHANNEL, 0);
85 if (m_connectionId == -1) {
86 ChannelDestroy(m_channelId);
87 qFatal("QQnxScreenEventThread: Can't continue without a channel connection");
88 }
89
90 SIGEV_PULSE_INIT(&m_screenEvent, m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_screenCode, 0);
91 if (screen_register_event(m_screenContext, &m_screenEvent) == -1) {
92 ConnectDetach(m_connectionId);
93 ChannelDestroy(m_channelId);
94 qFatal("QQnxScreenEventThread: Can't continue without a registered event");
95 }
96
97 screen_notify(m_screenContext, SCREEN_NOTIFY_EVENT, nullptr, &m_screenEvent);
98 }
99
~QQnxScreenEventThread()100 QQnxScreenEventThread::~QQnxScreenEventThread()
101 {
102 // block until thread terminates
103 shutdown();
104
105 screen_notify(m_screenContext, SCREEN_NOTIFY_EVENT, nullptr, nullptr);
106 screen_unregister_event(&m_screenEvent);
107 ConnectDetach(m_connectionId);
108 ChannelDestroy(m_channelId);
109 }
110
run()111 void QQnxScreenEventThread::run()
112 {
113 qScreenEventThreadDebug("screen event thread started");
114
115 while (1) {
116 struct _pulse msg;
117 memset(&msg, 0, sizeof(msg));
118 int receiveId = MsgReceive(m_channelId, &msg, sizeof(msg), nullptr);
119 if (receiveId == 0 && msg.code == c_quitCode)
120 break;
121 else if (receiveId == 0)
122 handlePulse(msg);
123 else if (receiveId > 0)
124 qWarning() << "Unexpected message" << msg.code;
125 else
126 qWarning() << "MsgReceive error" << strerror(errno);
127 }
128
129 qScreenEventThreadDebug("screen event thread stopped");
130 }
131
armEventsPending(int count)132 void QQnxScreenEventThread::armEventsPending(int count)
133 {
134 MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_armCode, count);
135 }
136
handleScreenPulse(const struct _pulse & msg)137 void QQnxScreenEventThread::handleScreenPulse(const struct _pulse &msg)
138 {
139 Q_UNUSED(msg);
140
141 ++m_screenPulsesSinceLastArmPulse;
142 if (m_emitNeededOnNextScreenPulse) {
143 m_emitNeededOnNextScreenPulse = false;
144 Q_EMIT eventsPending();
145 }
146 }
147
handleArmPulse(const struct _pulse & msg)148 void QQnxScreenEventThread::handleArmPulse(const struct _pulse &msg)
149 {
150 if (msg.value.sival_int == 0 && m_screenPulsesSinceLastArmPulse == 0) {
151 m_emitNeededOnNextScreenPulse = true;
152 } else {
153 m_screenPulsesSinceLastArmPulse = 0;
154 m_emitNeededOnNextScreenPulse = false;
155 Q_EMIT eventsPending();
156 }
157 }
158
handlePulse(const struct _pulse & msg)159 void QQnxScreenEventThread::handlePulse(const struct _pulse &msg)
160 {
161 if (msg.code == c_screenCode)
162 handleScreenPulse(msg);
163 else if (msg.code == c_armCode)
164 handleArmPulse(msg);
165 else
166 qWarning() << "Unexpected pulse" << msg.code;
167 }
168
shutdown()169 void QQnxScreenEventThread::shutdown()
170 {
171 MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_quitCode, 0);
172
173 qScreenEventThreadDebug("screen event thread shutdown begin");
174
175 // block until thread terminates
176 wait();
177
178 qScreenEventThreadDebug("screen event thread shutdown end");
179 }
180