summaryrefslogtreecommitdiffstats
path: root/src/plugins/qnx/mediaplayer/mmreventthread.cpp
blob: 8f9f91c878c60e091886bb85bb3d9a2697294632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/****************************************************************************
**
** Copyright (C) 2017 QNX Software Systems. All rights reserved.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "mmreventthread.h"

#include <QtCore/QDebug>

#include <errno.h>
#include <mm/renderer/events.h>
#include <sys/neutrino.h>

static const int c_mmrCode = _PULSE_CODE_MINAVAIL + 0;
static const int c_readCode = _PULSE_CODE_MINAVAIL + 1;
static const int c_quitCode = _PULSE_CODE_MINAVAIL + 2;

MmrEventThread::MmrEventThread(mmr_context_t *context)
    : QThread(),
      m_mmrContext(context)
{
    if (Q_UNLIKELY((m_channelId = ChannelCreate(_NTO_CHF_DISCONNECT
                                                | _NTO_CHF_UNBLOCK
                                                | _NTO_CHF_PRIVATE)) == -1)) {
        qFatal("MmrEventThread: Can't continue without a channel");
    }

    if (Q_UNLIKELY((m_connectionId = ConnectAttach(0, 0, m_channelId,
                                                   _NTO_SIDE_CHANNEL, 0)) == -1)) {
        ChannelDestroy(m_channelId);
        qFatal("MmrEventThread: Can't continue without a channel connection");
    }

    SIGEV_PULSE_INIT(&m_mmrEvent, m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_mmrCode, 0);
}

MmrEventThread::~MmrEventThread()
{
    // block until thread terminates
    shutdown();

    ConnectDetach(m_connectionId);
    ChannelDestroy(m_channelId);
}

void MmrEventThread::run()
{
    int armResult = mmr_event_arm(m_mmrContext, &m_mmrEvent);
    if (armResult > 0)
        emit eventPending();

    while (1) {
        struct _pulse msg;
        memset(&msg, 0, sizeof(msg));
        int receiveId = MsgReceive(m_channelId, &msg, sizeof(msg), nullptr);
        if (receiveId == 0) {
            if (msg.code == c_mmrCode) {
                emit eventPending();
            } else if (msg.code == c_readCode) {
                armResult = mmr_event_arm(m_mmrContext, &m_mmrEvent);
                if (armResult > 0)
                    emit eventPending();
            } else if (msg.code == c_quitCode) {
                break;
            } else {
                qWarning() << Q_FUNC_INFO << "Unexpected pulse" << msg.code;
            }
        } else if (receiveId > 0) {
            qWarning() << Q_FUNC_INFO << "Unexpected message" << msg.code;
        } else {
            qWarning() << Q_FUNC_INFO << "MsgReceive error" << strerror(errno);
        }
    }
}

void MmrEventThread::signalRead()
{
    MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_readCode, 0);
}

void MmrEventThread::shutdown()
{
    MsgSendPulse(m_connectionId, SIGEV_PULSE_PRIO_INHERIT, c_quitCode, 0);

    // block until thread terminates
    wait();
}