// This file was generated by qtwaylandscanner
// source file is /build/qt5-wayland/src/qtwayland/src/extensions/qt-dmabuf-server-buffer.xml

#include "qwayland-server-qt-dmabuf-server-buffer.h"

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

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

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

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

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

    qt_dmabuf_server_buffer::~qt_dmabuf_server_buffer()
    {
        for (auto resource : qAsConst(m_resource_map))
            resource->dmabuf_server_buffer_object = nullptr;

        if (m_resource)
            m_resource->dmabuf_server_buffer_object = nullptr;

        if (m_global) {
            wl_global_destroy(m_global);
            wl_list_remove(&m_displayDestroyedListener.link);
        }
    }

    void qt_dmabuf_server_buffer::init(struct ::wl_client *client, int id, int version)
    {
        m_resource = bind(client, id, version);
    }

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

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

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

    const struct wl_interface *qt_dmabuf_server_buffer::interface()
    {
        return &::qt_dmabuf_server_buffer_interface;
    }

    qt_dmabuf_server_buffer::Resource *qt_dmabuf_server_buffer::dmabuf_server_buffer_allocate()
    {
        return new Resource;
    }

    void qt_dmabuf_server_buffer::dmabuf_server_buffer_bind_resource(Resource *)
    {
    }

    void qt_dmabuf_server_buffer::dmabuf_server_buffer_destroy_resource(Resource *)
    {
    }

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

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

    void qt_dmabuf_server_buffer::destroy_func(struct ::wl_resource *client_resource)
    {
        Resource *resource = Resource::fromResource(client_resource);
        Q_ASSERT(resource);
        qt_dmabuf_server_buffer *that = resource->dmabuf_server_buffer_object;
        if (Q_LIKELY(that)) {
            that->m_resource_map.remove(resource->client(), resource);
            that->dmabuf_server_buffer_destroy_resource(resource);

            that = resource->dmabuf_server_buffer_object;
            if (that && that->m_resource == resource)
                that->m_resource = nullptr;
        }
        delete resource;
    }

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

    qt_dmabuf_server_buffer::Resource *qt_dmabuf_server_buffer::bind(struct ::wl_resource *handle)
    {
        Resource *resource = dmabuf_server_buffer_allocate();
        resource->dmabuf_server_buffer_object = this;

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

    void qt_dmabuf_server_buffer::send_server_buffer_created(struct ::wl_resource *id, int32_t fd, int32_t width, int32_t height, int32_t stride, int32_t offset, int32_t fourcc_format)
    {
        Q_ASSERT_X(m_resource, "qt_dmabuf_server_buffer::server_buffer_created", "Uninitialised resource");
        if (Q_UNLIKELY(!m_resource)) {
            qWarning("could not call qt_dmabuf_server_buffer::server_buffer_created as it's not initialised");
            return;
        }
        send_server_buffer_created(
            m_resource->handle,
            id,
            fd,
            width,
            height,
            stride,
            offset,
            fourcc_format);
    }

    void qt_dmabuf_server_buffer::send_server_buffer_created(struct ::wl_resource *resource, struct ::wl_resource *id, int32_t fd, int32_t width, int32_t height, int32_t stride, int32_t offset, int32_t fourcc_format)
    {
        qt_dmabuf_server_buffer_send_server_buffer_created(
            resource,
            id,
            fd,
            width,
            height,
            stride,
            offset,
            fourcc_format);
    }

}

QT_WARNING_POP
QT_END_NAMESPACE
