Commit dc4436a7 authored by Xaver Hugl's avatar Xaver Hugl
Browse files

core: port a few things away from manual memory management

parent faa006d7
......@@ -44,18 +44,18 @@ GetAddrInfo::GetAddrInfo(const QByteArray &hostName, QObject *parent)
, m_resolved(false)
, m_ownResolved(false)
, m_hostName(hostName)
, m_addressHints(new addrinfo)
, m_addressHints(std::make_unique<addrinfo>())
, m_address(nullptr)
, m_ownAddress(nullptr)
, m_watcher(new QFutureWatcher<int>(this))
, m_ownAddressWatcher(new QFutureWatcher<int>(this))
, m_watcher(std::make_unique<QFutureWatcher<int>>())
, m_ownAddressWatcher(std::make_unique<QFutureWatcher<int>>())
{
// watcher will be deleted together with the GetAddrInfo once the future
// got canceled or finished
connect(m_watcher, &QFutureWatcher<int>::canceled, this, &GetAddrInfo::deleteLater);
connect(m_watcher, &QFutureWatcher<int>::finished, this, &GetAddrInfo::slotResolved);
connect(m_ownAddressWatcher, &QFutureWatcher<int>::canceled, this, &GetAddrInfo::deleteLater);
connect(m_ownAddressWatcher, &QFutureWatcher<int>::finished, this, &GetAddrInfo::slotOwnAddressResolved);
connect(m_watcher.get(), &QFutureWatcher<int>::canceled, this, &GetAddrInfo::deleteLater);
connect(m_watcher.get(), &QFutureWatcher<int>::finished, this, &GetAddrInfo::slotResolved);
connect(m_ownAddressWatcher.get(), &QFutureWatcher<int>::canceled, this, &GetAddrInfo::deleteLater);
connect(m_ownAddressWatcher.get(), &QFutureWatcher<int>::finished, this, &GetAddrInfo::slotOwnAddressResolved);
}
GetAddrInfo::~GetAddrInfo()
......@@ -74,7 +74,6 @@ GetAddrInfo::~GetAddrInfo()
if (m_ownAddress) {
freeaddrinfo(m_ownAddress);
}
delete m_addressHints;
}
void GetAddrInfo::resolve()
......@@ -83,22 +82,22 @@ void GetAddrInfo::resolve()
return;
}
m_resolving = true;
memset(m_addressHints, 0, sizeof(*m_addressHints));
*m_addressHints = {};
m_addressHints->ai_family = PF_UNSPEC;
m_addressHints->ai_socktype = SOCK_STREAM;
m_addressHints->ai_flags |= AI_CANONNAME;
m_watcher->setFuture(QtConcurrent::run(getaddrinfo, m_hostName.constData(), nullptr, m_addressHints, &m_address));
m_watcher->setFuture(QtConcurrent::run(getaddrinfo, m_hostName.constData(), nullptr, m_addressHints.get(), &m_address));
m_ownAddressWatcher->setFuture(QtConcurrent::run([this] {
// needs to be performed in a lambda as getHostName() returns a temporary value which would
// get destroyed in the main thread before the getaddrinfo thread is able to read it
return getaddrinfo(getHostName().constData(), nullptr, m_addressHints, &m_ownAddress);
return getaddrinfo(getHostName().constData(), nullptr, m_addressHints.get(), &m_ownAddress);
}));
}
void GetAddrInfo::slotResolved()
{
if (resolved(m_watcher)) {
if (resolved(m_watcher.get())) {
m_resolved = true;
compare();
}
......@@ -106,7 +105,7 @@ void GetAddrInfo::slotResolved()
void GetAddrInfo::slotOwnAddressResolved()
{
if (resolved(m_ownAddressWatcher)) {
if (resolved(m_ownAddressWatcher.get())) {
m_ownResolved = true;
compare();
}
......
......@@ -10,6 +10,7 @@
#define KWIN_CLIENT_MACHINE_H
#include <QObject>
#include <memory>
#include <xcb/xcb.h>
// forward declaration
......@@ -43,11 +44,11 @@ private:
bool m_resolved;
bool m_ownResolved;
QByteArray m_hostName;
addrinfo *m_addressHints;
std::unique_ptr<addrinfo> m_addressHints;
addrinfo *m_address;
addrinfo *m_ownAddress;
QFutureWatcher<int> *m_watcher;
QFutureWatcher<int> *m_ownAddressWatcher;
std::unique_ptr<QFutureWatcher<int>> m_watcher;
std::unique_ptr<QFutureWatcher<int>> m_ownAddressWatcher;
};
class ClientMachine : public QObject
......
......@@ -338,9 +338,8 @@ void Compositor::initializeX11()
if (!m_selectionOwner) {
char selection_name[100];
sprintf(selection_name, "_NET_WM_CM_S%d", Application::x11ScreenNumber());
m_selectionOwner = new CompositorSelectionOwner(selection_name);
connect(m_selectionOwner, &CompositorSelectionOwner::lostOwnership,
this, &Compositor::stop);
m_selectionOwner = std::make_unique<CompositorSelectionOwner>(selection_name);
connect(m_selectionOwner.get(), &CompositorSelectionOwner::lostOwnership, this, &Compositor::stop);
}
if (!m_selectionOwner->owning()) {
// Force claim ownership.
......@@ -354,8 +353,7 @@ void Compositor::initializeX11()
void Compositor::cleanupX11()
{
delete m_selectionOwner;
m_selectionOwner = nullptr;
m_selectionOwner.reset();
}
void Compositor::startupWithWorkspace()
......@@ -540,8 +538,7 @@ void Compositor::stop()
void Compositor::destroyCompositorSelection()
{
delete m_selectionOwner;
m_selectionOwner = nullptr;
m_selectionOwner.reset();
}
void Compositor::releaseCompositorSelection()
......
......@@ -149,7 +149,7 @@ private:
void paintPass(RenderLayer *layer, RenderTarget *target, const QRegion &region);
State m_state = State::Off;
CompositorSelectionOwner *m_selectionOwner = nullptr;
std::unique_ptr<CompositorSelectionOwner> m_selectionOwner;
QTimer m_releaseSelectionTimer;
QList<xcb_atom_t> m_unusedSupportProperties;
QTimer m_unusedSupportPropertyTimer;
......
......@@ -119,11 +119,10 @@ void GlobalShortcutsManager::init()
{
if (kwinApp()->shouldUseWaylandForCompositing()) {
qputenv("KGLOBALACCELD_PLATFORM", QByteArrayLiteral("org.kde.kwin"));
m_kglobalAccel = new KGlobalAccelD(this);
m_kglobalAccel = std::make_unique<KGlobalAccelD>();
if (!m_kglobalAccel->init()) {
qCDebug(KWIN_CORE) << "Init of kglobalaccel failed";
delete m_kglobalAccel;
m_kglobalAccel = nullptr;
m_kglobalAccel.reset();
} else {
qCDebug(KWIN_CORE) << "KGlobalAcceld inited";
}
......
......@@ -124,7 +124,7 @@ private:
QVector<GlobalShortcut> m_shortcuts;
KGlobalAccelD *m_kglobalAccel = nullptr;
std::unique_ptr<KGlobalAccelD> m_kglobalAccel;
KGlobalAccelInterface *m_kglobalAccelInterface = nullptr;
QScopedPointer<GestureRecognizer> m_touchpadGestureRecognizer;
QScopedPointer<GestureRecognizer> m_touchscreenGestureRecognizer;
......
......@@ -34,18 +34,14 @@ Group::Group(xcb_window_t leader_P)
{
if (leader_P != XCB_WINDOW_NONE) {
leader_client = workspace()->findClient(Predicate::WindowMatch, leader_P);
leader_info = new NETWinInfo(kwinApp()->x11Connection(), leader_P, kwinApp()->x11RootWindow(),
NET::Properties(), NET::WM2StartupId);
leader_info = std::make_unique<NETWinInfo>(kwinApp()->x11Connection(), leader_P, kwinApp()->x11RootWindow(),
NET::Properties(), NET::WM2StartupId);
}
effect_group = new EffectWindowGroupImpl(this);
effect_group = std::make_unique<EffectWindowGroupImpl>(this);
workspace()->addGroup(this);
}
Group::~Group()
{
delete leader_info;
delete effect_group;
}
Group::~Group() = default;
QIcon Group::icon() const
{
......
......@@ -45,10 +45,10 @@ private:
QList<X11Window *> _members;
X11Window *leader_client;
xcb_window_t leader_wid;
NETWinInfo *leader_info;
std::unique_ptr<NETWinInfo> leader_info;
xcb_timestamp_t user_time;
int refcount;
EffectWindowGroupImpl *effect_group;
std::unique_ptr<EffectWindowGroupImpl> effect_group;
};
inline xcb_window_t Group::leader() const
......@@ -78,7 +78,7 @@ inline xcb_timestamp_t Group::userTime() const
inline EffectWindowGroupImpl *Group::effectGroup()
{
return effect_group;
return effect_group.get();
}
} // namespace
......
......@@ -945,13 +945,8 @@ class GlobalShortcutFilter : public InputEventFilter
public:
GlobalShortcutFilter()
{
m_powerDown = new QTimer;
m_powerDown->setSingleShot(true);
m_powerDown->setInterval(1000);
}
~GlobalShortcutFilter()
{
delete m_powerDown;
m_powerDown.setSingleShot(true);
m_powerDown.setInterval(1000);
}
bool pointerEvent(QMouseEvent *event, quint32 nativeButton) override
......@@ -986,16 +981,16 @@ public:
if (event->key() == Qt::Key_PowerOff) {
const auto modifiers = static_cast<KeyEvent *>(event)->modifiersRelevantForGlobalShortcuts();
if (event->type() == QEvent::KeyPress && !event->isAutoRepeat()) {
QObject::connect(m_powerDown, &QTimer::timeout, input()->shortcuts(), [this, modifiers] {
QObject::disconnect(m_powerDown, &QTimer::timeout, input()->shortcuts(), nullptr);
m_powerDown->stop();
QObject::connect(&m_powerDown, &QTimer::timeout, input()->shortcuts(), [this, modifiers] {
QObject::disconnect(&m_powerDown, &QTimer::timeout, input()->shortcuts(), nullptr);
m_powerDown.stop();
input()->shortcuts()->processKey(modifiers, Qt::Key_PowerDown);
});
m_powerDown->start();
m_powerDown.start();
return true;
} else if (event->type() == QEvent::KeyRelease) {
const bool ret = !m_powerDown->isActive() || input()->shortcuts()->processKey(modifiers, event->key());
m_powerDown->stop();
const bool ret = !m_powerDown.isActive() || input()->shortcuts()->processKey(modifiers, event->key());
m_powerDown.stop();
return ret;
}
} else if (event->type() == QEvent::KeyPress) {
......@@ -1167,7 +1162,7 @@ private:
QPointF m_lastAverageDistance;
QMap<int32_t, QPointF> m_touchPoints;
QTimer *m_powerDown = nullptr;
QTimer m_powerDown;
};
namespace
......
......@@ -108,7 +108,6 @@ void KeyboardLayout::reconfigure()
const QString policyKey = m_configGroup.readEntry("SwitchMode", QStringLiteral("Global"));
m_xkb->reconfigure();
if (!m_policy || m_policy->name() != policyKey) {
delete m_policy;
m_policy = KeyboardLayoutSwitching::Policy::create(m_xkb, this, m_configGroup, policyKey);
}
} else {
......
......@@ -12,6 +12,7 @@
#include "input_event_spy.h"
#include <QObject>
#include <QVector>
#include <memory>
#include <KConfigGroup>
#include <KSharedConfig>
......@@ -62,7 +63,7 @@ private:
KConfigGroup m_configGroup;
QVector<QAction *> m_layoutShortcuts;
KeyboardLayoutDBusInterface *m_dbusInterface = nullptr;
KeyboardLayoutSwitching::Policy *m_policy = nullptr;
std::unique_ptr<KeyboardLayoutSwitching::Policy> m_policy;
};
class KeyboardLayoutDBusInterface : public QObject
......
......@@ -42,18 +42,18 @@ void Policy::setLayout(uint index)
}
}
Policy *Policy::create(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config, const QString &policy)
std::unique_ptr<Policy> Policy::create(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config, const QString &policy)
{
if (policy.toLower() == QStringLiteral("desktop")) {
return new VirtualDesktopPolicy(xkb, layout, config);
return std::make_unique<VirtualDesktopPolicy>(xkb, layout, config);
}
if (policy.toLower() == QStringLiteral("window")) {
return new WindowPolicy(xkb, layout);
return std::make_unique<WindowPolicy>(xkb, layout);
}
if (policy.toLower() == QStringLiteral("winclass")) {
return new ApplicationPolicy(xkb, layout, config);
return std::make_unique<ApplicationPolicy>(xkb, layout, config);
}
return new GlobalPolicy(xkb, layout, config);
return std::make_unique<GlobalPolicy>(xkb, layout, config);
}
const char Policy::defaultLayoutEntryKeyPrefix[] = "LayoutDefault";
......
......@@ -12,6 +12,7 @@
#include <KConfigGroup>
#include <QHash>
#include <QObject>
#include <memory>
namespace KWin
{
......@@ -32,7 +33,7 @@ public:
virtual QString name() const = 0;
static Policy *create(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config, const QString &policy);
static std::unique_ptr<Policy> create(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config, const QString &policy);
protected:
explicit Policy(Xkb *xkb, KeyboardLayout *layout, const KConfigGroup &config = KConfigGroup());
......
......@@ -190,33 +190,29 @@ void Workspace::propagateWindows(bool propagate_new_windows)
Q_ASSERT(newWindowStack.at(0) == rootInfo()->supportWindow());
Xcb::restackWindows(newWindowStack);
int pos = 0;
xcb_window_t *cl(nullptr);
QVector<xcb_window_t> cl;
if (propagate_new_windows) {
cl = new xcb_window_t[manual_overlays.count() + m_x11Clients.count()];
cl.reserve(manual_overlays.size() + m_x11Clients.size());
for (const auto win : qAsConst(manual_overlays)) {
cl[pos++] = win;
cl.push_back(win);
}
for (auto it = m_x11Clients.constBegin(); it != m_x11Clients.constEnd(); ++it) {
cl[pos++] = (*it)->window();
cl.push_back((*it)->window());
}
rootInfo()->setClientList(cl, pos);
delete[] cl;
rootInfo()->setClientList(cl.constData(), cl.size());
}
cl = new xcb_window_t[manual_overlays.count() + stacking_order.count()];
pos = 0;
cl.clear();
for (auto it = stacking_order.constBegin(); it != stacking_order.constEnd(); ++it) {
X11Window *window = qobject_cast<X11Window *>(*it);
if (window) {
cl[pos++] = window->window();
cl.push_back(window->window());
}
}
for (const auto win : qAsConst(manual_overlays)) {
cl[pos++] = win;
cl.push_back(win);
}
rootInfo()->setClientListStacking(cl, pos);
delete[] cl;
rootInfo()->setClientListStacking(cl.constData(), cl.size());
}
/**
......
......@@ -119,8 +119,7 @@ ApplicationWayland::~ApplicationWayland()
if (effects) {
static_cast<EffectsHandlerImpl *>(effects)->unloadAllEffects();
}
delete m_xwayland;
m_xwayland = nullptr;
m_xwayland.reset();
destroyWorkspace();
destroyInputMethod();
......@@ -171,20 +170,20 @@ void ApplicationWayland::continueStartupWithScene()
return;
}
m_xwayland = new Xwl::Xwayland(this);
m_xwayland = std::make_unique<Xwl::Xwayland>(this);
m_xwayland->xwaylandLauncher()->setListenFDs(m_xwaylandListenFds);
m_xwayland->xwaylandLauncher()->setDisplayName(m_xwaylandDisplay);
m_xwayland->xwaylandLauncher()->setXauthority(m_xwaylandXauthority);
connect(m_xwayland, &Xwl::Xwayland::errorOccurred, this, &ApplicationWayland::finalizeStartup);
connect(m_xwayland, &Xwl::Xwayland::started, this, &ApplicationWayland::finalizeStartup);
connect(m_xwayland.get(), &Xwl::Xwayland::errorOccurred, this, &ApplicationWayland::finalizeStartup);
connect(m_xwayland.get(), &Xwl::Xwayland::started, this, &ApplicationWayland::finalizeStartup);
m_xwayland->start();
}
void ApplicationWayland::finalizeStartup()
{
if (m_xwayland) {
disconnect(m_xwayland, &Xwl::Xwayland::errorOccurred, this, &ApplicationWayland::finalizeStartup);
disconnect(m_xwayland, &Xwl::Xwayland::started, this, &ApplicationWayland::finalizeStartup);
disconnect(m_xwayland.get(), &Xwl::Xwayland::errorOccurred, this, &ApplicationWayland::finalizeStartup);
disconnect(m_xwayland.get(), &Xwl::Xwayland::started, this, &ApplicationWayland::finalizeStartup);
}
startSession();
notifyStarted();
......
......@@ -69,7 +69,7 @@ private:
QString m_inputMethodServerToStart;
QString m_sessionArgument;
Xwl::Xwayland *m_xwayland = nullptr;
std::unique_ptr<Xwl::Xwayland> m_xwayland;
QVector<int> m_xwaylandListenFds;
QString m_xwaylandDisplay;
QString m_xwaylandXauthority;
......
......@@ -22,7 +22,7 @@ namespace KWin
{
extern int screen_number;
RootInfo *RootInfo::s_self = nullptr;
std::unique_ptr<RootInfo> RootInfo::s_self;
RootInfo *RootInfo::create()
{
......@@ -116,8 +116,8 @@ RootInfo *RootInfo::create()
| NET::ActionChangeDesktop
| NET::ActionClose;
s_self = new RootInfo(supportWindow, "KWin", properties, types, states, properties2, actions, screen_number);
return s_self;
s_self.reset(new RootInfo(supportWindow, "KWin", properties, types, states, properties2, actions, screen_number));
return s_self.get();
}
void RootInfo::destroy()
......@@ -126,8 +126,7 @@ void RootInfo::destroy()
return;
}
xcb_window_t supportWindow = s_self->supportWindow();
delete s_self;
s_self = nullptr;
s_self.reset();
xcb_destroy_window(kwinApp()->x11Connection(), supportWindow);
}
......
......@@ -50,7 +50,7 @@ protected:
private:
RootInfo(xcb_window_t w, const char *name, NET::Properties properties, NET::WindowTypes types,
NET::States states, NET::Properties2 properties2, NET::Actions actions, int scr = -1);
static RootInfo *s_self;
static std::unique_ptr<RootInfo> s_self;
friend RootInfo *rootInfo();
xcb_window_t m_activeWindow;
......@@ -59,7 +59,7 @@ private:
inline RootInfo *rootInfo()
{
return RootInfo::s_self;
return RootInfo::s_self.get();
}
/**
......
......@@ -73,9 +73,8 @@ bool XrandrEventFilter::event(xcb_generic_event_t *event)
return false;
}
Xwayland::Xwayland(Application *app, QObject *parent)
: XwaylandInterface(parent)
, m_app(app)
Xwayland::Xwayland(Application *app)
: m_app(app)
, m_launcher(new XwaylandLauncher(this))
{
connect(m_launcher, &XwaylandLauncher::started, this, &Xwayland::handleXwaylandReady);
......
......@@ -30,7 +30,7 @@ class KWIN_EXPORT Xwayland : public XwaylandInterface
Q_OBJECT
public:
Xwayland(Application *app, QObject *parent = nullptr);
Xwayland(Application *app);
~Xwayland() override;
void start();
......
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