#include <QtWaylandCompositor/private/qwayland-server-ivi-application.h>

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

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

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

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

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

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

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

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

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

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

    const struct wl_interface *ivi_surface::interface()
    {
        return &::ivi_surface_interface;
    }

    ivi_surface::Resource *ivi_surface::ivi_surface_allocate()
    {
        return new Resource;
    }

    void ivi_surface::ivi_surface_bind_resource(Resource *)
    {
    }

    void ivi_surface::ivi_surface_destroy_resource(Resource *)
    {
    }

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

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

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

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

    ivi_surface::Resource *ivi_surface::bind(struct ::wl_resource *handle)
    {
        Resource *resource = ivi_surface_allocate();
        resource->ivi_surface_object = this;

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

    const struct ::ivi_surface_interface ivi_surface::m_ivi_surface_interface = {
        ivi_surface::handle_destroy
    };

    void ivi_surface::ivi_surface_destroy(Resource *)
    {
    }


    void ivi_surface::handle_destroy(
        ::wl_client *client,
        struct wl_resource *resource)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<ivi_surface *>(r->ivi_surface_object)->ivi_surface_destroy(
            r);
    }

    void ivi_surface::send_configure(int32_t width, int32_t height)
    {
        send_configure(
            m_resource->handle,
            width,
            height);
    }

    void ivi_surface::send_configure(struct ::wl_resource *resource, int32_t width, int32_t height)
    {
        ivi_surface_send_configure(
            resource,
            width,
            height);
    }


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

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

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

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

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

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

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

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

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

    const struct wl_interface *ivi_application::interface()
    {
        return &::ivi_application_interface;
    }

    ivi_application::Resource *ivi_application::ivi_application_allocate()
    {
        return new Resource;
    }

    void ivi_application::ivi_application_bind_resource(Resource *)
    {
    }

    void ivi_application::ivi_application_destroy_resource(Resource *)
    {
    }

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

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

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

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

    ivi_application::Resource *ivi_application::bind(struct ::wl_resource *handle)
    {
        Resource *resource = ivi_application_allocate();
        resource->ivi_application_object = this;

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

    const struct ::ivi_application_interface ivi_application::m_ivi_application_interface = {
        ivi_application::handle_surface_create
    };

    void ivi_application::ivi_application_surface_create(Resource *, uint32_t , struct ::wl_resource *, uint32_t)
    {
    }


    void ivi_application::handle_surface_create(
        ::wl_client *client,
        struct wl_resource *resource,
        uint32_t ivi_id,
        struct ::wl_resource *surface,
        uint32_t id)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<ivi_application *>(r->ivi_application_object)->ivi_application_surface_create(
            r,
            ivi_id,
            surface,
            id);
    }
}

QT_WARNING_POP
QT_END_NAMESPACE
