Add d-pointer to Server::Display

This commit is contained in:
Martin Gräßlin 2014-09-18 15:40:22 +02:00
parent 6fbb8462d4
commit 2bee5b0e2b
2 changed files with 84 additions and 57 deletions

View file

@ -35,14 +35,33 @@ namespace KWayland
namespace Server namespace Server
{ {
class Display::Private
{
public:
Private(Display *q);
void flush();
void setRunning(bool running);
wl_display *display = nullptr;
wl_event_loop *loop = nullptr;
QString socketName = QStringLiteral("wayland-0");
bool running = false;
QList<OutputInterface*> outputs;
private:
Display *q;
};
Display::Private::Private(Display *q)
: q(q)
{
}
Display::Display(QObject *parent) Display::Display(QObject *parent)
: QObject(parent) : QObject(parent)
, m_display(nullptr) , d(new Private(this))
, m_loop(nullptr)
, m_socketName(QStringLiteral("wayland-0"))
, m_running(false)
{ {
connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::aboutToBlock, this, &Display::flush); connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::aboutToBlock, this, [this] { d->flush(); });
} }
Display::~Display() Display::~Display()
@ -50,77 +69,77 @@ Display::~Display()
terminate(); terminate();
} }
void Display::flush() void Display::Private::flush()
{ {
if (!m_display || !m_loop) { if (!display || !loop) {
return; return;
} }
if (wl_event_loop_dispatch(m_loop, 0) != 0) { if (wl_event_loop_dispatch(loop, 0) != 0) {
qWarning() << "Error on dispatching Wayland event loop"; qWarning() << "Error on dispatching Wayland event loop";
} }
wl_display_flush_clients(m_display); wl_display_flush_clients(display);
} }
void Display::setSocketName(const QString &name) void Display::setSocketName(const QString &name)
{ {
if (m_socketName == name) { if (d->socketName == name) {
return; return;
} }
m_socketName = name; d->socketName = name;
emit socketNameChanged(m_socketName); emit socketNameChanged(d->socketName);
} }
QString Display::socketName() const QString Display::socketName() const
{ {
return m_socketName; return d->socketName;
} }
void Display::start() void Display::start()
{ {
Q_ASSERT(!m_running); Q_ASSERT(!d->running);
Q_ASSERT(!m_display); Q_ASSERT(!d->display);
m_display = wl_display_create(); d->display = wl_display_create();
if (wl_display_add_socket(m_display, qPrintable(m_socketName)) != 0) { if (wl_display_add_socket(d->display, qPrintable(d->socketName)) != 0) {
return; return;
} }
m_loop = wl_display_get_event_loop(m_display); d->loop = wl_display_get_event_loop(d->display);
int fd = wl_event_loop_get_fd(m_loop); int fd = wl_event_loop_get_fd(d->loop);
if (fd == -1) { if (fd == -1) {
qWarning() << "Did not get the file descriptor for the event loop"; qWarning() << "Did not get the file descriptor for the event loop";
return; return;
} }
QSocketNotifier *m_notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this); QSocketNotifier *m_notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
connect(m_notifier, &QSocketNotifier::activated, this, &Display::flush); connect(m_notifier, &QSocketNotifier::activated, this, [this] { d->flush(); } );
setRunning(true); d->setRunning(true);
} }
void Display::terminate() void Display::terminate()
{ {
if (!m_running) { if (!d->running) {
return; return;
} }
emit aboutToTerminate(); emit aboutToTerminate();
wl_display_terminate(m_display); wl_display_terminate(d->display);
wl_display_destroy(m_display); wl_display_destroy(d->display);
m_display = nullptr; d->display = nullptr;
m_loop = nullptr; d->loop = nullptr;
setRunning(false); d->setRunning(false);
} }
void Display::setRunning(bool running) void Display::Private::setRunning(bool r)
{ {
Q_ASSERT(m_running != running); Q_ASSERT(running != r);
m_running = running; running = r;
emit runningChanged(m_running); emit q->runningChanged(running);
} }
OutputInterface *Display::createOutput(QObject *parent) OutputInterface *Display::createOutput(QObject *parent)
{ {
OutputInterface *output = new OutputInterface(this, parent); OutputInterface *output = new OutputInterface(this, parent);
connect(output, &QObject::destroyed, this, [this,output] { m_outputs.removeAll(output); }); connect(output, &QObject::destroyed, this, [this,output] { d->outputs.removeAll(output); });
connect(this, &Display::aboutToTerminate, output, [this,output] { removeOutput(output); }); connect(this, &Display::aboutToTerminate, output, [this,output] { removeOutput(output); });
m_outputs << output; d->outputs << output;
return output; return output;
} }
@ -147,25 +166,46 @@ SeatInterface *Display::createSeat(QObject *parent)
void Display::createShm() void Display::createShm()
{ {
Q_ASSERT(m_running); Q_ASSERT(d->running);
wl_display_init_shm(m_display); wl_display_init_shm(d->display);
} }
void Display::removeOutput(OutputInterface *output) void Display::removeOutput(OutputInterface *output)
{ {
m_outputs.removeAll(output); d->outputs.removeAll(output);
delete output; delete output;
} }
quint32 Display::nextSerial() quint32 Display::nextSerial()
{ {
return wl_display_next_serial(m_display); return wl_display_next_serial(d->display);
} }
quint32 Display::serial() quint32 Display::serial()
{ {
return wl_display_get_serial(m_display); return wl_display_get_serial(d->display);
} }
bool Display::isRunning() const
{
return d->running;
}
Display::operator wl_display*()
{
return d->display;
}
Display::operator wl_display*() const
{
return d->display;
}
QList< OutputInterface* > Display::outputs() const
{
return d->outputs;
}
} }
} }

View file

@ -56,21 +56,13 @@ public:
void start(); void start();
void terminate(); void terminate();
operator wl_display*() { operator wl_display*();
return m_display; operator wl_display*() const;
} bool isRunning() const;
operator wl_display*() const {
return m_display;
}
bool isRunning() const {
return m_running;
}
OutputInterface *createOutput(QObject *parent = nullptr); OutputInterface *createOutput(QObject *parent = nullptr);
void removeOutput(OutputInterface *output); void removeOutput(OutputInterface *output);
const QList<OutputInterface*> &outputs() const { QList<OutputInterface*> outputs() const;
return m_outputs;
}
CompositorInterface *createCompositor(QObject *parent = nullptr); CompositorInterface *createCompositor(QObject *parent = nullptr);
void createShm(); void createShm();
@ -83,13 +75,8 @@ Q_SIGNALS:
void aboutToTerminate(); void aboutToTerminate();
private: private:
void flush(); class Private;
void setRunning(bool running); QScopedPointer<Private> d;
wl_display *m_display;
wl_event_loop *m_loop;
QString m_socketName;
bool m_running;
QList<OutputInterface*> m_outputs;
}; };
} }