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

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

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

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

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

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

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

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

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

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

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

    const struct wl_interface *wp_viewporter::interface()
    {
        return &::wp_viewporter_interface;
    }

    wp_viewporter::Resource *wp_viewporter::wp_viewporter_allocate()
    {
        return new Resource;
    }

    void wp_viewporter::wp_viewporter_bind_resource(Resource *)
    {
    }

    void wp_viewporter::wp_viewporter_destroy_resource(Resource *)
    {
    }

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

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

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

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

    wp_viewporter::Resource *wp_viewporter::bind(struct ::wl_resource *handle)
    {
        Resource *resource = wp_viewporter_allocate();
        resource->wp_viewporter_object = this;

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

    const struct ::wp_viewporter_interface wp_viewporter::m_wp_viewporter_interface = {
        wp_viewporter::handle_destroy,
        wp_viewporter::handle_get_viewport
    };

    void wp_viewporter::wp_viewporter_destroy(Resource *)
    {
    }

    void wp_viewporter::wp_viewporter_get_viewport(Resource *, uint32_t, struct ::wl_resource *)
    {
    }


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

    void wp_viewporter::handle_get_viewport(
        ::wl_client *client,
        struct wl_resource *resource,
        uint32_t id,
        struct ::wl_resource *surface)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<wp_viewporter *>(r->wp_viewporter_object)->wp_viewporter_get_viewport(
            r,
            id,
            surface);
    }

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

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

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

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

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

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

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

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

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

    const struct wl_interface *wp_viewport::interface()
    {
        return &::wp_viewport_interface;
    }

    wp_viewport::Resource *wp_viewport::wp_viewport_allocate()
    {
        return new Resource;
    }

    void wp_viewport::wp_viewport_bind_resource(Resource *)
    {
    }

    void wp_viewport::wp_viewport_destroy_resource(Resource *)
    {
    }

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

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

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

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

    wp_viewport::Resource *wp_viewport::bind(struct ::wl_resource *handle)
    {
        Resource *resource = wp_viewport_allocate();
        resource->wp_viewport_object = this;

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

    const struct ::wp_viewport_interface wp_viewport::m_wp_viewport_interface = {
        wp_viewport::handle_destroy,
        wp_viewport::handle_set_source,
        wp_viewport::handle_set_destination
    };

    void wp_viewport::wp_viewport_destroy(Resource *)
    {
    }

    void wp_viewport::wp_viewport_set_source(Resource *, wl_fixed_t , wl_fixed_t , wl_fixed_t , wl_fixed_t )
    {
    }

    void wp_viewport::wp_viewport_set_destination(Resource *, int32_t , int32_t )
    {
    }


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

    void wp_viewport::handle_set_source(
        ::wl_client *client,
        struct wl_resource *resource,
        wl_fixed_t x,
        wl_fixed_t y,
        wl_fixed_t width,
        wl_fixed_t height)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<wp_viewport *>(r->wp_viewport_object)->wp_viewport_set_source(
            r,
            x,
            y,
            width,
            height);
    }

    void wp_viewport::handle_set_destination(
        ::wl_client *client,
        struct wl_resource *resource,
        int32_t width,
        int32_t height)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<wp_viewport *>(r->wp_viewport_object)->wp_viewport_set_destination(
            r,
            width,
            height);
    }
}

QT_WARNING_POP
QT_END_NAMESPACE
