#include <QtWaylandCompositor/private/qwayland-server-qt-key-unstable-v1.h>

QT_BEGIN_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wmissing-field-initializers")

namespace QtWaylandServer {
    zqt_key_v1::zqt_key_v1(struct ::wl_client *client, int id, int version)
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
        init(client, id, version);
    }

    zqt_key_v1::zqt_key_v1(struct ::wl_display *display, int version)
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
        init(display, version);
    }

    zqt_key_v1::zqt_key_v1(struct ::wl_resource *resource)
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
        init(resource);
    }

    zqt_key_v1::zqt_key_v1()
        : m_resource_map()
        , m_resource(nullptr)
        , m_global(nullptr)
    {
    }

    zqt_key_v1::~zqt_key_v1()
    {
        for (auto resource : qAsConst(m_resource_map))
            wl_resource_set_implementation(resource->handle, nullptr, nullptr, nullptr);

        if (m_global) {
            wl_global_destroy(m_global);
            wl_list_remove(&m_displayDestroyedListener.link);
        }
    }

    void zqt_key_v1::init(struct ::wl_client *client, int id, int version)
    {
        m_resource = bind(client, id, version);
    }

    void zqt_key_v1::init(struct ::wl_resource *resource)
    {
        m_resource = bind(resource);
    }

    zqt_key_v1::Resource *zqt_key_v1::add(struct ::wl_client *client, int version)
    {
        Resource *resource = bind(client, 0, version);
        m_resource_map.insert(client, resource);
        return resource;
    }

    zqt_key_v1::Resource *zqt_key_v1::add(struct ::wl_client *client, int id, int version)
    {
        Resource *resource = bind(client, id, version);
        m_resource_map.insert(client, resource);
        return resource;
    }

    void zqt_key_v1::init(struct ::wl_display *display, int version)
    {
        m_global = wl_global_create(display, &::zqt_key_v1_interface, version, this, bind_func);
        m_globalVersion = version;
        m_displayDestroyedListener.notify = zqt_key_v1::display_destroy_func;
        m_displayDestroyedListener.parent = this;
        wl_display_add_destroy_listener(display, &m_displayDestroyedListener);
    }

    const struct wl_interface *zqt_key_v1::interface()
    {
        return &::zqt_key_v1_interface;
    }

    zqt_key_v1::Resource *zqt_key_v1::zqt_key_v1_allocate()
    {
        return new Resource;
    }

    void zqt_key_v1::zqt_key_v1_bind_resource(Resource *)
    {
    }

    void zqt_key_v1::zqt_key_v1_destroy_resource(Resource *)
    {
    }

    void zqt_key_v1::bind_func(struct ::wl_client *client, void *data, uint32_t version, uint32_t id)
    {
        zqt_key_v1 *that = static_cast<zqt_key_v1 *>(data);
        that->add(client, id, qMin(that->m_globalVersion, version));
    }

    void zqt_key_v1::display_destroy_func(struct ::wl_listener *listener, void *data)
    {
        Q_UNUSED(data);
        zqt_key_v1 *that = static_cast<zqt_key_v1::DisplayDestroyedListener *>(listener)->parent;
        that->m_global = nullptr;
    }

    void zqt_key_v1::destroy_func(struct ::wl_resource *client_resource)
    {
        Resource *resource = Resource::fromResource(client_resource);
        Q_ASSERT(resource);
        zqt_key_v1 *that = resource->zqt_key_v1_object;
        that->m_resource_map.remove(resource->client(), resource);
        that->zqt_key_v1_destroy_resource(resource);
        delete resource;
    }

    zqt_key_v1::Resource *zqt_key_v1::bind(struct ::wl_client *client, uint32_t id, int version)
    {
        Q_ASSERT_X(!wl_client_get_object(client, id), "QWaylandObject bind", QStringLiteral("binding to object %1 more than once").arg(id).toLocal8Bit().constData());
        struct ::wl_resource *handle = wl_resource_create(client, &::zqt_key_v1_interface, version, id);
        return bind(handle);
    }

    zqt_key_v1::Resource *zqt_key_v1::bind(struct ::wl_resource *handle)
    {
        Resource *resource = zqt_key_v1_allocate();
        resource->zqt_key_v1_object = this;

        wl_resource_set_implementation(handle, nullptr, resource, destroy_func);
        resource->handle = handle;
        zqt_key_v1_bind_resource(resource);
        return resource;
    }
    zqt_key_v1::Resource *zqt_key_v1::Resource::fromResource(struct ::wl_resource *resource)
    {
        if (Q_UNLIKELY(!resource))
            return nullptr;
        if (wl_resource_instance_of(resource, &::zqt_key_v1_interface, nullptr))
            return static_cast<Resource *>(wl_resource_get_user_data(resource));
        return nullptr;
    }

    void zqt_key_v1::send_key(struct ::wl_resource *surface, uint32_t time, uint32_t type, uint32_t key, uint32_t modifiers, uint32_t nativeScanCode, uint32_t nativeVirtualKey, uint32_t nativeModifiers, const QString &text, uint32_t autorepeat, uint32_t count)
    {
        send_key(
            m_resource->handle,
            surface,
            time,
            type,
            key,
            modifiers,
            nativeScanCode,
            nativeVirtualKey,
            nativeModifiers,
            text,
            autorepeat,
            count);
    }

    void zqt_key_v1::send_key(struct ::wl_resource *resource, struct ::wl_resource *surface, uint32_t time, uint32_t type, uint32_t key, uint32_t modifiers, uint32_t nativeScanCode, uint32_t nativeVirtualKey, uint32_t nativeModifiers, const QString &text, uint32_t autorepeat, uint32_t count)
    {
        zqt_key_v1_send_key(
            resource,
            surface,
            time,
            type,
            key,
            modifiers,
            nativeScanCode,
            nativeVirtualKey,
            nativeModifiers,
            text.toUtf8().constData(),
            autorepeat,
            count);
    }

}

QT_WARNING_POP
QT_END_NAMESPACE
