Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qqnxscreeneventthread.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 QNX Software Systems. All rights reserved.
2// Copyright (C) 2011 - 2012 Research In Motion
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qqnxglobal.h"
6
9
10#include <QtCore/QDebug>
11
12#include <errno.h>
13#include <unistd.h>
14
15#include <cctype>
16
17#if defined(QQNXSCREENEVENTTHREAD_DEBUG)
18#define qScreenEventThreadDebug qDebug
19#else
20#define qScreenEventThreadDebug QT_NO_QDEBUG_MACRO
21#endif
22
23static const int c_screenCode = _PULSE_CODE_MINAVAIL + 0;
24static const int c_armCode = _PULSE_CODE_MINAVAIL + 1;
25static const int c_quitCode = _PULSE_CODE_MINAVAIL + 2;
26
27#if !defined(screen_register_event)
28int screen_register_event(screen_context_t, struct sigevent *event)
29{
30 return MsgRegisterEvent(event, -1);
31}
32
33int screen_unregister_event(struct sigevent *event)
34{
35 return MsgUnregisterEvent(event);
36}
37#endif
38
40 : QThread()
41 , m_screenContext(context)
42{
43 m_channelId = ChannelCreate(_NTO_CHF_DISCONNECT | _NTO_CHF_UNBLOCK | _NTO_CHF_PRIVATE);
44 if (m_channelId == -1) {
45 qFatal("QQnxScreenEventThread: Can't continue without a channel");
46 }
47
48 m_connectionId = ConnectAttach(0, 0, m_channelId, _NTO_SIDE_CHANNEL, 0);
49 if (m_connectionId == -1) {
50 ChannelDestroy(m_channelId);
51 qFatal("QQnxScreenEventThread: Can't continue without a channel connection");
52 }
53
54 SIGEV_PULSE_INIT(&m_screenEvent, m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_screenCode, 0);
55 if (screen_register_event(m_screenContext, &m_screenEvent) == -1) {
56 ConnectDetach(m_connectionId);
57 ChannelDestroy(m_channelId);
58 qFatal("QQnxScreenEventThread: Can't continue without a registered event");
59 }
60
61 screen_notify(m_screenContext, SCREEN_NOTIFY_EVENT, nullptr, &m_screenEvent);
62}
63
65{
66 // block until thread terminates
67 shutdown();
68
69 screen_notify(m_screenContext, SCREEN_NOTIFY_EVENT, nullptr, nullptr);
70 screen_unregister_event(&m_screenEvent);
71 ConnectDetach(m_connectionId);
72 ChannelDestroy(m_channelId);
73}
74
76{
77 qScreenEventThreadDebug("screen event thread started");
78
79 while (1) {
80 struct _pulse msg;
81 memset(&msg, 0, sizeof(msg));
82 int receiveId = MsgReceive(m_channelId, &msg, sizeof(msg), nullptr);
83 if (receiveId == 0 && msg.code == c_quitCode)
84 break;
85 else if (receiveId == 0)
86 handlePulse(msg);
87 else if (receiveId > 0)
88 qWarning() << "Unexpected message" << msg.code;
89 else
90 qWarning() << "MsgReceive error" << strerror(errno);
91 }
92
93 qScreenEventThreadDebug("screen event thread stopped");
94}
95
97{
98 MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_armCode, count);
99}
100
101void QQnxScreenEventThread::handleScreenPulse(const struct _pulse &msg)
102{
103 Q_UNUSED(msg);
104
105 ++m_screenPulsesSinceLastArmPulse;
106 if (m_emitNeededOnNextScreenPulse) {
107 m_emitNeededOnNextScreenPulse = false;
109 }
110}
111
112void QQnxScreenEventThread::handleArmPulse(const struct _pulse &msg)
113{
114 if (msg.value.sival_int == 0 && m_screenPulsesSinceLastArmPulse == 0) {
115 m_emitNeededOnNextScreenPulse = true;
116 } else {
117 m_screenPulsesSinceLastArmPulse = 0;
118 m_emitNeededOnNextScreenPulse = false;
120 }
121}
122
123void QQnxScreenEventThread::handlePulse(const struct _pulse &msg)
124{
125 if (msg.code == c_screenCode)
126 handleScreenPulse(msg);
127 else if (msg.code == c_armCode)
128 handleArmPulse(msg);
129 else
130 qWarning() << "Unexpected pulse" << msg.code;
131}
132
133void QQnxScreenEventThread::shutdown()
134{
135 MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_quitCode, 0);
136
137 qScreenEventThreadDebug("screen event thread shutdown begin");
138
139 // block until thread terminates
140 wait();
141
142 qScreenEventThreadDebug("screen event thread shutdown end");
143}
QQnxScreenEventThread(screen_context_t context)
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
Definition qthread.cpp:950
static void * context
#define qWarning
Definition qlogging.h:162
#define qFatal
Definition qlogging.h:164
GLenum GLenum GLsizei count
struct _cl_event * event
static const int c_screenCode
static const int c_armCode
static const int c_quitCode
#define qScreenEventThreadDebug
int screen_register_event(screen_context_t, struct sigevent *event)
int screen_unregister_event(struct sigevent *event)
#define Q_EMIT
#define Q_UNUSED(x)