File indexing completed on 2024-11-10 04:57:33

0001 /*
0002     SPDX-FileCopyrightText: 2017 David Edmundson <kde@davidedmundson.co.uk>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #include "server_decoration_palette.h"
0007 #include "display.h"
0008 #include "surface.h"
0009 #include "utils/common.h"
0010 
0011 #include <QtGlobal>
0012 
0013 #include <qwayland-server-server-decoration-palette.h>
0014 
0015 namespace KWin
0016 {
0017 static const quint32 s_version = 1;
0018 
0019 class ServerSideDecorationPaletteManagerInterfacePrivate : public QtWaylandServer::org_kde_kwin_server_decoration_palette_manager
0020 {
0021 public:
0022     ServerSideDecorationPaletteManagerInterfacePrivate(ServerSideDecorationPaletteManagerInterface *q, Display *display);
0023 
0024     QList<ServerSideDecorationPaletteInterface *> palettes;
0025     ServerSideDecorationPaletteManagerInterface *q;
0026 
0027 protected:
0028     void org_kde_kwin_server_decoration_palette_manager_create(Resource *resource, uint32_t id, struct ::wl_resource *surface) override;
0029 };
0030 
0031 void ServerSideDecorationPaletteManagerInterfacePrivate::org_kde_kwin_server_decoration_palette_manager_create(Resource *resource,
0032                                                                                                                uint32_t id,
0033                                                                                                                wl_resource *surface)
0034 {
0035     SurfaceInterface *s = SurfaceInterface::get(surface);
0036     if (!s) {
0037         wl_resource_post_error(resource->handle, 0, "invalid surface");
0038         qCWarning(KWIN_CORE) << "ServerSideDecorationPaletteInterface requested for non existing SurfaceInterface";
0039         return;
0040     }
0041 
0042     wl_resource *palette_resource = wl_resource_create(resource->client(), &org_kde_kwin_server_decoration_palette_interface, resource->version(), id);
0043     if (!palette_resource) {
0044         wl_client_post_no_memory(resource->client());
0045         return;
0046     }
0047     auto palette = new ServerSideDecorationPaletteInterface(s, palette_resource);
0048 
0049     palettes.append(palette);
0050     QObject::connect(palette, &QObject::destroyed, q, [this, palette]() {
0051         palettes.removeOne(palette);
0052     });
0053     Q_EMIT q->paletteCreated(palette);
0054 }
0055 
0056 ServerSideDecorationPaletteManagerInterfacePrivate::ServerSideDecorationPaletteManagerInterfacePrivate(ServerSideDecorationPaletteManagerInterface *_q,
0057                                                                                                        Display *display)
0058     : QtWaylandServer::org_kde_kwin_server_decoration_palette_manager(*display, s_version)
0059     , q(_q)
0060 {
0061 }
0062 
0063 ServerSideDecorationPaletteManagerInterface::ServerSideDecorationPaletteManagerInterface(Display *display, QObject *parent)
0064     : QObject(parent)
0065     , d(new ServerSideDecorationPaletteManagerInterfacePrivate(this, display))
0066 {
0067 }
0068 
0069 ServerSideDecorationPaletteManagerInterface::~ServerSideDecorationPaletteManagerInterface() = default;
0070 
0071 ServerSideDecorationPaletteInterface *ServerSideDecorationPaletteManagerInterface::paletteForSurface(SurfaceInterface *surface)
0072 {
0073     for (ServerSideDecorationPaletteInterface *menu : std::as_const(d->palettes)) {
0074         if (menu->surface() == surface) {
0075             return menu;
0076         }
0077     }
0078     return nullptr;
0079 }
0080 
0081 class ServerSideDecorationPaletteInterfacePrivate : public QtWaylandServer::org_kde_kwin_server_decoration_palette
0082 {
0083 public:
0084     ServerSideDecorationPaletteInterfacePrivate(ServerSideDecorationPaletteInterface *_q, SurfaceInterface *surface, wl_resource *resource);
0085 
0086     SurfaceInterface *surface;
0087     QString palette;
0088     ServerSideDecorationPaletteInterface *q;
0089 
0090 protected:
0091     void org_kde_kwin_server_decoration_palette_destroy_resource(Resource *resource) override;
0092     void org_kde_kwin_server_decoration_palette_set_palette(Resource *resource, const QString &palette) override;
0093     void org_kde_kwin_server_decoration_palette_release(Resource *resource) override;
0094 };
0095 
0096 void ServerSideDecorationPaletteInterfacePrivate::org_kde_kwin_server_decoration_palette_release(Resource *resource)
0097 {
0098     wl_resource_destroy(resource->handle);
0099 }
0100 
0101 void ServerSideDecorationPaletteInterfacePrivate::org_kde_kwin_server_decoration_palette_set_palette(Resource *resource, const QString &palette)
0102 {
0103     if (this->palette == palette) {
0104         return;
0105     }
0106     this->palette = palette;
0107     Q_EMIT q->paletteChanged(this->palette);
0108 }
0109 
0110 void ServerSideDecorationPaletteInterfacePrivate::org_kde_kwin_server_decoration_palette_destroy_resource(Resource *resource)
0111 {
0112     delete q;
0113 }
0114 
0115 ServerSideDecorationPaletteInterfacePrivate::ServerSideDecorationPaletteInterfacePrivate(ServerSideDecorationPaletteInterface *_q,
0116                                                                                          SurfaceInterface *surface,
0117                                                                                          wl_resource *resource)
0118     : QtWaylandServer::org_kde_kwin_server_decoration_palette(resource)
0119     , surface(surface)
0120     , q(_q)
0121 {
0122 }
0123 
0124 ServerSideDecorationPaletteInterface::ServerSideDecorationPaletteInterface(SurfaceInterface *surface, wl_resource *resource)
0125     : QObject()
0126     , d(new ServerSideDecorationPaletteInterfacePrivate(this, surface, resource))
0127 {
0128 }
0129 
0130 ServerSideDecorationPaletteInterface::~ServerSideDecorationPaletteInterface() = default;
0131 
0132 QString ServerSideDecorationPaletteInterface::palette() const
0133 {
0134     return d->palette;
0135 }
0136 
0137 SurfaceInterface *ServerSideDecorationPaletteInterface::surface() const
0138 {
0139     return d->surface;
0140 }
0141 
0142 } // namespace
0143 
0144 #include "moc_server_decoration_palette.cpp"