Commit 5f886d72 authored by Xaver Hugl's avatar Xaver Hugl
Browse files

backends/fakeinput: port away from QObject memory management

parent b0c852aa
Pipeline #231089 passed with stage
in 13 minutes and 36 seconds
......@@ -12,21 +12,23 @@
namespace KWin
{
FakeInputBackend::FakeInputBackend(QObject *parent)
: InputBackend(parent)
{
}
FakeInputBackend::FakeInputBackend() = default;
FakeInputBackend::~FakeInputBackend() = default;
void FakeInputBackend::initialize()
{
auto fakeInput = new KWaylandServer::FakeInputInterface(waylandServer()->display(), this);
connect(fakeInput, &KWaylandServer::FakeInputInterface::deviceCreated, this, [this](KWaylandServer::FakeInputDevice *fakeDevice) {
auto device = new FakeInputDevice(fakeDevice, this);
Q_EMIT deviceAdded(device);
connect(fakeDevice, &QObject::destroyed, this, [this, device]() {
Q_EMIT deviceRemoved(device);
});
m_interface = std::make_unique<KWaylandServer::FakeInputInterface>(waylandServer()->display());
connect(m_interface.get(), &KWaylandServer::FakeInputInterface::deviceCreated, this, [this](KWaylandServer::FakeInputDevice *fakeDevice) {
m_devices[fakeDevice] = std::make_unique<FakeInputDevice>(fakeDevice);
Q_EMIT deviceAdded(m_devices[fakeDevice].get());
});
connect(m_interface.get(), &KWaylandServer::FakeInputInterface::deviceDestroyed, this, [this](KWaylandServer::FakeInputDevice *fakeDevice) {
auto it = m_devices.find(fakeDevice);
if (it != m_devices.end()) {
const std::unique_ptr<FakeInputDevice> device = std::move(it->second);
m_devices.erase(it);
Q_EMIT deviceRemoved(device.get());
}
});
}
......
......@@ -8,17 +8,33 @@
#include "core/inputbackend.h"
#include <map>
#include <memory>
namespace KWaylandServer
{
class FakeInputInterface;
class FakeInputDevice;
}
namespace KWin
{
class FakeInputDevice;
class FakeInputBackend : public InputBackend
{
Q_OBJECT
public:
explicit FakeInputBackend(QObject *parent = nullptr);
explicit FakeInputBackend();
~FakeInputBackend();
void initialize() override;
private:
std::unique_ptr<KWaylandServer::FakeInputInterface> m_interface;
std::map<KWaylandServer::FakeInputDevice *, std::unique_ptr<FakeInputDevice>> m_devices;
};
} // namespace KWin
......@@ -119,7 +119,6 @@ void FakeInputTest::cleanup()
m_thread = nullptr;
}
CLEANUP(m_device)
CLEANUP(m_display)
#undef CLEANUP
......
......@@ -8,6 +8,8 @@
#include <QPointF>
#include <QSizeF>
#include <memory>
#include <vector>
#include <qwayland-server-fake-input.h>
#include <wayland-server.h>
......@@ -20,7 +22,7 @@ class FakeInputInterfacePrivate : public QtWaylandServer::org_kde_kwin_fake_inpu
{
public:
FakeInputInterfacePrivate(FakeInputInterface *_q, Display *display);
QList<FakeInputDevice *> devices;
std::map<wl_resource *, std::unique_ptr<FakeInputDevice>> devices;
private:
FakeInputDevice *device(wl_resource *r);
......@@ -52,9 +54,8 @@ FakeInputInterfacePrivate::FakeInputInterfacePrivate(FakeInputInterface *_q, Dis
{
}
FakeInputInterface::FakeInputInterface(Display *display, QObject *parent)
: QObject(parent)
, d(new FakeInputInterfacePrivate(this, display))
FakeInputInterface::FakeInputInterface(Display *display)
: d(std::make_unique<FakeInputInterfacePrivate>(this, display))
{
}
......@@ -62,30 +63,24 @@ FakeInputInterface::~FakeInputInterface() = default;
void FakeInputInterfacePrivate::org_kde_kwin_fake_input_bind_resource(Resource *resource)
{
FakeInputDevice *device = new FakeInputDevice(q, resource->handle);
devices << device;
QObject::connect(device, &FakeInputDevice::destroyed, q, [device, this] {
devices.removeAll(device);
});
auto device = new FakeInputDevice(q, resource->handle);
devices[resource->handle] = std::unique_ptr<FakeInputDevice>(device);
Q_EMIT q->deviceCreated(device);
}
void FakeInputInterfacePrivate::org_kde_kwin_fake_input_destroy_resource(Resource *resource)
{
if (FakeInputDevice *d = device(resource->handle)) {
d->deleteLater();
auto it = devices.find(resource->handle);
if (it != devices.end()) {
const auto [resource, device] = std::move(*it);
devices.erase(it);
Q_EMIT q->deviceDestroyed(device.get());
}
}
FakeInputDevice *FakeInputInterfacePrivate::device(wl_resource *r)
{
auto it = std::find_if(devices.constBegin(), devices.constEnd(), [r](FakeInputDevice *device) {
return device->resource() == r;
});
if (it != devices.constEnd()) {
return *it;
}
return nullptr;
return devices[r].get();
}
void FakeInputInterfacePrivate::org_kde_kwin_fake_input_authenticate(Resource *resource, const QString &application, const QString &reason)
......@@ -246,8 +241,7 @@ FakeInputDevicePrivate::FakeInputDevicePrivate(FakeInputInterface *interface, wl
}
FakeInputDevice::FakeInputDevice(FakeInputInterface *parent, wl_resource *resource)
: QObject(parent)
, d(new FakeInputDevicePrivate(parent, resource))
: d(std::make_unique<FakeInputDevicePrivate>(parent, resource))
{
}
......
......@@ -42,7 +42,7 @@ class KWIN_EXPORT FakeInputInterface : public QObject
Q_OBJECT
public:
explicit FakeInputInterface(Display *display, QObject *parent = nullptr);
explicit FakeInputInterface(Display *display);
~FakeInputInterface() override;
Q_SIGNALS:
......@@ -52,6 +52,8 @@ Q_SIGNALS:
*/
void deviceCreated(KWaylandServer::FakeInputDevice *device);
void deviceDestroyed(KWaylandServer::FakeInputDevice *device);
private:
std::unique_ptr<FakeInputInterfacePrivate> d;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment