11#include <QtCore/QDebug>
19static const int c_armCode = _PULSE_CODE_MINAVAIL + 1;
23#if !defined(screen_register_event)
26 return MsgRegisterEvent(event, -1);
31 return MsgUnregisterEvent(event);
35QQnxScreenEventThread::QQnxScreenEventThread(screen_context_t context)
37 , m_screenContext(context)
39 m_channelId = ChannelCreate(_NTO_CHF_DISCONNECT | _NTO_CHF_UNBLOCK | _NTO_CHF_PRIVATE);
40 if (m_channelId == -1) {
41 qFatal(
"QQnxScreenEventThread: Can't continue without a channel");
44 m_connectionId = ConnectAttach(0, 0, m_channelId, _NTO_SIDE_CHANNEL, 0);
45 if (m_connectionId == -1) {
46 ChannelDestroy(m_channelId);
47 qFatal(
"QQnxScreenEventThread: Can't continue without a channel connection");
50 SIGEV_PULSE_INIT(&m_screenEvent, m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_screenCode, 0);
51 if (screen_register_event(m_screenContext, &m_screenEvent) == -1) {
52 ConnectDetach(m_connectionId);
53 ChannelDestroy(m_channelId);
54 qFatal(
"QQnxScreenEventThread: Can't continue without a registered event");
57 screen_notify(m_screenContext, SCREEN_NOTIFY_EVENT,
nullptr, &m_screenEvent);
58 qRegisterMetaType<screen_window_t>(
"screen_window_t");
61QQnxScreenEventThread::~QQnxScreenEventThread()
66 screen_notify(m_screenContext, SCREEN_NOTIFY_EVENT,
nullptr,
nullptr);
67 screen_unregister_event(&m_screenEvent);
68 ConnectDetach(m_connectionId);
69 ChannelDestroy(m_channelId);
72void QQnxScreenEventThread::run()
74 qCDebug(lcQpaScreenEvents) <<
"Screen event thread started";
78 memset(&msg, 0,
sizeof(msg));
79 int receiveId = MsgReceive(m_channelId, &msg,
sizeof(msg),
nullptr);
80 if (receiveId == 0 && msg.code == c_quitCode)
82 else if (receiveId == 0)
84 else if (receiveId > 0)
85 qWarning() <<
"Unexpected message" << msg.code;
87 qWarning() <<
"MsgReceive error" << strerror(errno);
90 qCDebug(lcQpaScreenEvents) <<
"Screen event thread stopped";
93void QQnxScreenEventThread::armEventsPending(
int count)
95 MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_armCode, count);
98bool QQnxScreenEventThread::registerUpdateNotification(screen_window_t window)
101 SIGEV_PULSE_INIT(&evt, m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_postCode, 0);
102 evt.sigev_value.sival_ptr = window;
103 if (screen_register_event(m_screenContext, &evt) == -1) {
104 qWarning() <<
"QQnxScreenEventThread: Failed to register post event for window" << window;
107 int rc = screen_notify(m_screenContext, SCREEN_NOTIFY_UPDATE, window, &evt);
108 qCDebug(lcQpaScreenEvents) <<
"registerUpdateNotification window=" << window <<
"rc=" << rc;
110 qWarning() <<
"screen_notify(SCREEN_NOTIFY_UPDATE) failed for window=" << window << strerror(errno);
111 screen_unregister_event(&evt);
114 m_postEvents.insert(window, evt);
118void QQnxScreenEventThread::unregisterUpdateNotification(screen_window_t window)
120 int rc = screen_notify(m_screenContext, SCREEN_NOTIFY_UPDATE, window,
nullptr);
121 qCDebug(lcQpaScreenEvents) <<
"unregisterUpdateNotification window=" << window <<
"rc=" << rc;
122 if (m_postEvents.contains(window)) {
123 screen_unregister_event(&m_postEvents[window]);
124 m_postEvents.remove(window);
128void QQnxScreenEventThread::handlePostPulse(
const struct _pulse &msg)
130 screen_window_t window =
static_cast<screen_window_t>(msg.value.sival_ptr);
131 Q_EMIT postEventReceived(window);
134void QQnxScreenEventThread::handleScreenPulse(
const struct _pulse &msg)
138 ++m_screenPulsesSinceLastArmPulse;
139 if (m_emitNeededOnNextScreenPulse) {
140 m_emitNeededOnNextScreenPulse =
false;
141 Q_EMIT eventsPending();
145void QQnxScreenEventThread::handleArmPulse(
const struct _pulse &msg)
147 if (msg.value.sival_int == 0 && m_screenPulsesSinceLastArmPulse == 0) {
148 m_emitNeededOnNextScreenPulse =
true;
150 m_screenPulsesSinceLastArmPulse = 0;
151 m_emitNeededOnNextScreenPulse =
false;
152 Q_EMIT eventsPending();
156void QQnxScreenEventThread::handlePulse(
const struct _pulse &msg)
158 if (msg.code == c_screenCode)
159 handleScreenPulse(msg);
160 else if (msg.code == c_armCode)
162 else if (msg.code == c_postCode)
163 handlePostPulse(msg);
165 qWarning() <<
"Unexpected pulse" << msg.code;
168void QQnxScreenEventThread::shutdown()
170 MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_quitCode, 0);
172 qCDebug(lcQpaScreenEvents) <<
"Screen event thread shutdown begin";
177 qCDebug(lcQpaScreenEvents) <<
"Screen event thread shutdown end";
int screen_register_event(screen_context_t, struct sigevent *event)
static const int c_quitCode
int screen_unregister_event(struct sigevent *event)
static const int c_armCode
static const int c_screenCode
static const int c_postCode