/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: wangyan <wangyan@kylinos.cn>
 *
 */

#include "waylandinputdevicemonitor.h"

#include <QDBusConnection>

WaylandInputDeviceMonitor::WaylandInputDeviceMonitor(QObject* parent)
    : InputDeviceMonitor(parent)
{
    initDeviceListMonitor();
}

void WaylandInputDeviceMonitor::initDeviceListMonitor()
{
    QDBusConnection::sessionBus().connect(QString("org.kde.KWin"),
                                          QStringLiteral("/org/kde/KWin/InputDevice"),
                                          QStringLiteral("org.kde.KWin.InputDeviceManager"),
                                          QStringLiteral("deviceAdded"), this, SLOT(onDeviceAdded(QString)));
    QDBusConnection::sessionBus().connect(QString("org.kde.KWin"),
                                          QStringLiteral("/org/kde/KWin/InputDevice"),
                                          QStringLiteral("org.kde.KWin.InputDeviceManager"),
                                          QStringLiteral("deviceRemoved"), this, SLOT(onDeviceRemoved(QString)));
}

void WaylandInputDeviceMonitor::onDeviceAdded(QString deviceEvent)
{
    int deviceId;
    deviceId = deviceEvent.remove("event").toInt();

    emit deviceAdded(deviceId);
}

void WaylandInputDeviceMonitor::onDeviceRemoved(QString deviceEvent)
{
    int deviceId;
    deviceId = deviceEvent.remove("event").toInt();

    emit deviceRemoved(deviceId);
}
