// SPDX-FileCopyrightText: 2024 KylinSoft Co., Ltd.
//
// SPDX-License-Identifier: GPL-1.0-or-later

#include "qwaylandprimaryoutput_p.h"

#include "qwaylanddisplay_p.h"
#include "qwaylandscreen_p.h"

#include <QtGui/qpa/qwindowsysteminterface.h>

#include <QGuiApplication>
#include <QScreen>

QT_BEGIN_NAMESPACE

namespace QtWaylandClient
{

QWaylandPrimaryOutputV1::QWaylandPrimaryOutputV1(QWaylandDisplay *display, uint id, uint version)
    : QtWayland::kde_primary_output_v1(display->wl_registry(), id, qMin(version, uint(1)))
    , m_display(display)
{
}

void QWaylandPrimaryOutputV1::kde_primary_output_v1_primary_output(const QString &output_name)
{
    m_outputName = output_name;

    const auto screens = m_display->screens();
    auto primaryScreen = std::find_if(screens.begin(), screens.end(),[&output_name](QWaylandScreen *screen) {
        return screen->name() == output_name;
    });

    if (primaryScreen != screens.end()) {
        QWindowSystemInterface::handlePrimaryScreenChanged(*primaryScreen);
    }

}

} // namespace QtWaylandClient

QT_END_NAMESPACE
