summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wayland_common/qwaylandeventthread.cpp
blob: 3392d36c6675b77f48ed30bfc931c56915932042 (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
#include "qwaylandeventthread.h"
#include <QtCore/QSocketNotifier>
#include <QCoreApplication>

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

QT_BEGIN_NAMESPACE

QWaylandEventThread::QWaylandEventThread(QObject *parent)
    : QObject(parent)
    , m_display(0)
    , m_fileDescriptor(-1)
    , m_readNotifier(0)
    , m_displayLock(new QMutex)
{
}

QWaylandEventThread::~QWaylandEventThread()
{
    delete m_displayLock;
    wl_display_disconnect(m_display);
}

void QWaylandEventThread::displayConnect()
{
    m_displayLock->lock();
    QMetaObject::invokeMethod(this, "waylandDisplayConnect", Qt::QueuedConnection);
}

void QWaylandEventThread::readWaylandEvents()
{
    if (wl_display_dispatch(m_display) == -1 && errno == EPIPE) {
        qWarning("The Wayland connection broke. Did the Wayland compositor die?");
        ::exit(1);
    }
    emit newEventsRead();
}

void QWaylandEventThread::waylandDisplayConnect()
{
    m_display = wl_display_connect(NULL);
    if (m_display == NULL) {
        qErrnoWarning(errno, "Failed to create display");
        ::exit(1);
    }
    m_displayLock->unlock();

    m_fileDescriptor = wl_display_get_fd(m_display);

    m_readNotifier = new QSocketNotifier(m_fileDescriptor, QSocketNotifier::Read, this);
    connect(m_readNotifier, SIGNAL(activated(int)), this, SLOT(readWaylandEvents()));
}

wl_display *QWaylandEventThread::display() const
{
    QMutexLocker displayLock(m_displayLock);
    return m_display;
}

QT_END_NAMESPACE