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

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

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

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

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

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

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

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

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

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

    const struct wl_interface *qt_touch_extension::interface()
    {
        return &::qt_touch_extension_interface;
    }

    qt_touch_extension::Resource *qt_touch_extension::touch_extension_allocate()
    {
        return new Resource;
    }

    void qt_touch_extension::touch_extension_bind_resource(Resource *)
    {
    }

    void qt_touch_extension::touch_extension_destroy_resource(Resource *)
    {
    }

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

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

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

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

    qt_touch_extension::Resource *qt_touch_extension::bind(struct ::wl_resource *handle)
    {
        Resource *resource = touch_extension_allocate();
        resource->touch_extension_object = this;

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

    const struct ::qt_touch_extension_interface qt_touch_extension::m_qt_touch_extension_interface = {
        qt_touch_extension::handle_dummy
    };

    void qt_touch_extension::touch_extension_dummy(Resource *)
    {
    }


    void qt_touch_extension::handle_dummy(
        ::wl_client *client,
        struct wl_resource *resource)
    {
        Q_UNUSED(client);
        Resource *r = Resource::fromResource(resource);
        static_cast<qt_touch_extension *>(r->touch_extension_object)->touch_extension_dummy(
            r);
    }

    void qt_touch_extension::send_touch(uint32_t time, uint32_t id, uint32_t state, int32_t x, int32_t y, int32_t normalized_x, int32_t normalized_y, int32_t width, int32_t height, uint32_t pressure, int32_t velocity_x, int32_t velocity_y, uint32_t flags, const QByteArray &rawdata)
    {
        send_touch(
            m_resource->handle,
            time,
            id,
            state,
            x,
            y,
            normalized_x,
            normalized_y,
            width,
            height,
            pressure,
            velocity_x,
            velocity_y,
            flags,
            rawdata);
    }

    void qt_touch_extension::send_touch(struct ::wl_resource *resource, uint32_t time, uint32_t id, uint32_t state, int32_t x, int32_t y, int32_t normalized_x, int32_t normalized_y, int32_t width, int32_t height, uint32_t pressure, int32_t velocity_x, int32_t velocity_y, uint32_t flags, const QByteArray &rawdata)
    {
        struct wl_array rawdata_data;
        rawdata_data.size = rawdata.size();
        rawdata_data.data = static_cast<void *>(const_cast<char *>(rawdata.constData()));
        rawdata_data.alloc = 0;

        qt_touch_extension_send_touch(
            resource,
            time,
            id,
            state,
            x,
            y,
            normalized_x,
            normalized_y,
            width,
            height,
            pressure,
            velocity_x,
            velocity_y,
            flags,
            &rawdata_data);
    }


    void qt_touch_extension::send_configure(uint32_t flags)
    {
        send_configure(
            m_resource->handle,
            flags);
    }

    void qt_touch_extension::send_configure(struct ::wl_resource *resource, uint32_t flags)
    {
        qt_touch_extension_send_configure(
            resource,
            flags);
    }

}

QT_WARNING_POP
QT_END_NAMESPACE
