#include "qwayland-server-wl-eglstream-controller.h"

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

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

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

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

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

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

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

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

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

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

    const struct wl_interface *wl_eglstream_controller::interface()
    {
        return &::wl_eglstream_controller_interface;
    }

    wl_eglstream_controller::Resource *wl_eglstream_controller::eglstream_controller_allocate()
    {
        return new Resource;
    }

    void wl_eglstream_controller::eglstream_controller_bind_resource(Resource *)
    {
    }

    void wl_eglstream_controller::eglstream_controller_destroy_resource(Resource *)
    {
    }

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

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

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

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

    wl_eglstream_controller::Resource *wl_eglstream_controller::bind(struct ::wl_resource *handle)
    {
        Resource *resource = eglstream_controller_allocate();
        resource->eglstream_controller_object = this;

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

    const struct ::wl_eglstream_controller_interface wl_eglstream_controller::m_wl_eglstream_controller_interface = {
        wl_eglstream_controller::handle_attach_eglstream_consumer
    };

    void wl_eglstream_controller::eglstream_controller_attach_eglstream_consumer(Resource *, struct ::wl_resource *, struct ::wl_resource *)
    {
    }


    void wl_eglstream_controller::handle_attach_eglstream_consumer(
        ::wl_client *client,
        struct wl_resource *resource,
        struct ::wl_resource *wl_surface,
        struct ::wl_resource *wl_resource)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<wl_eglstream_controller *>(r->eglstream_controller_object)->eglstream_controller_attach_eglstream_consumer(
            r,
            wl_surface,
            wl_resource);
    }
}

QT_WARNING_POP
QT_END_NAMESPACE
