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

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

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

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

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

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

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

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

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

    wl_scaler::Resource *wl_scaler::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_scaler::init(struct ::wl_display *display, int version)
    {
        m_global = wl_global_create(display, &::wl_scaler_interface, version, this, bind_func);
        m_globalVersion = version;
        m_displayDestroyedListener.notify = wl_scaler::display_destroy_func;
        m_displayDestroyedListener.parent = this;
        wl_display_add_destroy_listener(display, &m_displayDestroyedListener);
    }

    const struct wl_interface *wl_scaler::interface()
    {
        return &::wl_scaler_interface;
    }

    wl_scaler::Resource *wl_scaler::scaler_allocate()
    {
        return new Resource;
    }

    void wl_scaler::scaler_bind_resource(Resource *)
    {
    }

    void wl_scaler::scaler_destroy_resource(Resource *)
    {
    }

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

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

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

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

    wl_scaler::Resource *wl_scaler::bind(struct ::wl_resource *handle)
    {
        Resource *resource = scaler_allocate();
        resource->scaler_object = this;

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

    const struct ::wl_scaler_interface wl_scaler::m_wl_scaler_interface = {
        wl_scaler::handle_destroy,
        wl_scaler::handle_get_viewport
    };

    void wl_scaler::scaler_destroy(Resource *)
    {
    }

    void wl_scaler::scaler_get_viewport(Resource *, uint32_t, struct ::wl_resource *)
    {
    }


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

    void wl_scaler::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<wl_scaler *>(r->scaler_object)->scaler_get_viewport(
            r,
            id,
            surface);
    }

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

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

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

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

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

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

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

    wl_viewport::Resource *wl_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 wl_viewport::init(struct ::wl_display *display, int version)
    {
        m_global = wl_global_create(display, &::wl_viewport_interface, version, this, bind_func);
        m_globalVersion = version;
        m_displayDestroyedListener.notify = wl_viewport::display_destroy_func;
        m_displayDestroyedListener.parent = this;
        wl_display_add_destroy_listener(display, &m_displayDestroyedListener);
    }

    const struct wl_interface *wl_viewport::interface()
    {
        return &::wl_viewport_interface;
    }

    wl_viewport::Resource *wl_viewport::viewport_allocate()
    {
        return new Resource;
    }

    void wl_viewport::viewport_bind_resource(Resource *)
    {
    }

    void wl_viewport::viewport_destroy_resource(Resource *)
    {
    }

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

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

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

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

    wl_viewport::Resource *wl_viewport::bind(struct ::wl_resource *handle)
    {
        Resource *resource = viewport_allocate();
        resource->viewport_object = this;

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

    const struct ::wl_viewport_interface wl_viewport::m_wl_viewport_interface = {
        wl_viewport::handle_destroy,
        wl_viewport::handle_set,
        wl_viewport::handle_set_source,
        wl_viewport::handle_set_destination
    };

    void wl_viewport::viewport_destroy(Resource *)
    {
    }

    void wl_viewport::viewport_set(Resource *, wl_fixed_t , wl_fixed_t , wl_fixed_t , wl_fixed_t , int32_t , int32_t )
    {
    }

    void wl_viewport::viewport_set_source(Resource *, wl_fixed_t , wl_fixed_t , wl_fixed_t , wl_fixed_t )
    {
    }

    void wl_viewport::viewport_set_destination(Resource *, int32_t , int32_t )
    {
    }


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

    void wl_viewport::handle_set(
        ::wl_client *client,
        struct wl_resource *resource,
        wl_fixed_t src_x,
        wl_fixed_t src_y,
        wl_fixed_t src_width,
        wl_fixed_t src_height,
        int32_t dst_width,
        int32_t dst_height)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<wl_viewport *>(r->viewport_object)->viewport_set(
            r,
            src_x,
            src_y,
            src_width,
            src_height,
            dst_width,
            dst_height);
    }

    void wl_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<wl_viewport *>(r->viewport_object)->viewport_set_source(
            r,
            x,
            y,
            width,
            height);
    }

    void wl_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<wl_viewport *>(r->viewport_object)->viewport_set_destination(
            r,
            width,
            height);
    }
}

QT_WARNING_POP
QT_END_NAMESPACE
