File indexing completed on 2024-05-19 05:31:33

0001 /*
0002     SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "colormanager.h"
0008 #include "colordevice.h"
0009 #include "core/output.h"
0010 #include "core/session.h"
0011 #include "main.h"
0012 #include "utils/common.h"
0013 #include "workspace.h"
0014 
0015 namespace KWin
0016 {
0017 
0018 class ColorManagerPrivate
0019 {
0020 public:
0021     QList<ColorDevice *> devices;
0022 };
0023 
0024 ColorManager::ColorManager()
0025     : d(std::make_unique<ColorManagerPrivate>())
0026 {
0027     const QList<Output *> outputs = workspace()->outputs();
0028     for (Output *output : outputs) {
0029         handleOutputAdded(output);
0030     }
0031 
0032     connect(workspace(), &Workspace::outputAdded, this, &ColorManager::handleOutputAdded);
0033     connect(workspace(), &Workspace::outputRemoved, this, &ColorManager::handleOutputRemoved);
0034     connect(kwinApp()->session(), &Session::activeChanged, this, &ColorManager::handleSessionActiveChanged);
0035 }
0036 
0037 ColorManager::~ColorManager() = default;
0038 
0039 QList<ColorDevice *> ColorManager::devices() const
0040 {
0041     return d->devices;
0042 }
0043 
0044 ColorDevice *ColorManager::findDevice(Output *output) const
0045 {
0046     auto it = std::find_if(d->devices.begin(), d->devices.end(), [&output](ColorDevice *device) {
0047         return device->output() == output;
0048     });
0049     if (it != d->devices.end()) {
0050         return *it;
0051     }
0052     return nullptr;
0053 }
0054 
0055 void ColorManager::handleOutputAdded(Output *output)
0056 {
0057     ColorDevice *device = new ColorDevice(output, this);
0058     d->devices.append(device);
0059     Q_EMIT deviceAdded(device);
0060 }
0061 
0062 void ColorManager::handleOutputRemoved(Output *output)
0063 {
0064     auto it = std::find_if(d->devices.begin(), d->devices.end(), [&output](ColorDevice *device) {
0065         return device->output() == output;
0066     });
0067     if (it == d->devices.end()) {
0068         qCWarning(KWIN_CORE) << "Could not find any color device for output" << output;
0069         return;
0070     }
0071     ColorDevice *device = *it;
0072     d->devices.erase(it);
0073     Q_EMIT deviceRemoved(device);
0074     delete device;
0075 }
0076 
0077 void ColorManager::handleSessionActiveChanged(bool active)
0078 {
0079     if (!active) {
0080         return;
0081     }
0082     for (ColorDevice *device : std::as_const(d->devices)) {
0083         device->scheduleUpdate();
0084     }
0085 }
0086 
0087 } // namespace KWin
0088 
0089 #include "moc_colormanager.cpp"