#include <QtWaylandCompositor/private/qwayland-server-hardware-integration.h>

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

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

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

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

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

    qt_hardware_integration::~qt_hardware_integration()
    {
        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 qt_hardware_integration::init(struct ::wl_client *client, int id, int version)
    {
        m_resource = bind(client, id, version);
    }

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

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

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

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

    const struct wl_interface *qt_hardware_integration::interface()
    {
        return &::qt_hardware_integration_interface;
    }

    qt_hardware_integration::Resource *qt_hardware_integration::hardware_integration_allocate()
    {
        return new Resource;
    }

    void qt_hardware_integration::hardware_integration_bind_resource(Resource *)
    {
    }

    void qt_hardware_integration::hardware_integration_destroy_resource(Resource *)
    {
    }

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

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

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

    qt_hardware_integration::Resource *qt_hardware_integration::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, &::qt_hardware_integration_interface, version, id);
        return bind(handle);
    }

    qt_hardware_integration::Resource *qt_hardware_integration::bind(struct ::wl_resource *handle)
    {
        Resource *resource = hardware_integration_allocate();
        resource->hardware_integration_object = this;

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

    void qt_hardware_integration::send_client_backend(const QString &name)
    {
        send_client_backend(
            m_resource->handle,
            name);
    }

    void qt_hardware_integration::send_client_backend(struct ::wl_resource *resource, const QString &name)
    {
        qt_hardware_integration_send_client_backend(
            resource,
            name.toUtf8().constData());
    }


    void qt_hardware_integration::send_server_backend(const QString &name)
    {
        send_server_backend(
            m_resource->handle,
            name);
    }

    void qt_hardware_integration::send_server_backend(struct ::wl_resource *resource, const QString &name)
    {
        qt_hardware_integration_send_server_backend(
            resource,
            name.toUtf8().constData());
    }

}

QT_WARNING_POP
QT_END_NAMESPACE
