File indexing completed on 2024-05-19 16:34:45

0001 /*
0002     SPDX-FileCopyrightText: 2022 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "scene/cursoritem.h"
0008 #include "cursor.h"
0009 #include "cursorsource.h"
0010 #include "scene/imageitem.h"
0011 #include "scene/itemrenderer.h"
0012 #include "scene/scene.h"
0013 #include "scene/surfaceitem_wayland.h"
0014 #include "wayland/surface_interface.h"
0015 #include "wayland_server.h"
0016 
0017 namespace KWin
0018 {
0019 
0020 CursorItem::CursorItem(Scene *scene, Item *parent)
0021     : Item(scene, parent)
0022 {
0023     refresh();
0024     connect(Cursors::self(), &Cursors::currentCursorChanged, this, &CursorItem::refresh);
0025 }
0026 
0027 CursorItem::~CursorItem()
0028 {
0029 }
0030 
0031 void CursorItem::refresh()
0032 {
0033     const CursorSource *source = Cursors::self()->currentCursor()->source();
0034     if (auto surfaceSource = qobject_cast<const SurfaceCursorSource *>(source)) {
0035         // TODO Plasma 6: Stop setting XCURSOR_SIZE and scale Xcursor.size in xrdb.
0036         if (surfaceSource->surface() && surfaceSource->surface()->client() == waylandServer()->xWaylandConnection()) {
0037             setImage(surfaceSource->image());
0038         } else {
0039             setSurface(surfaceSource->surface());
0040         }
0041     } else if (auto imageSource = qobject_cast<const ImageCursorSource *>(source)) {
0042         setImage(imageSource->image());
0043     } else if (auto shapeSource = qobject_cast<const ShapeCursorSource *>(source)) {
0044         setImage(shapeSource->image());
0045     }
0046 }
0047 
0048 void CursorItem::setSurface(KWaylandServer::SurfaceInterface *surface)
0049 {
0050     m_imageItem.reset();
0051 
0052     if (!m_surfaceItem || m_surfaceItem->surface() != surface) {
0053         if (surface) {
0054             m_surfaceItem = std::make_unique<SurfaceItemWayland>(surface, scene(), this);
0055         } else {
0056             m_surfaceItem.reset();
0057         }
0058     }
0059 }
0060 
0061 void CursorItem::setImage(const QImage &image)
0062 {
0063     m_surfaceItem.reset();
0064 
0065     if (!m_imageItem) {
0066         m_imageItem.reset(scene()->renderer()->createImageItem(scene(), this));
0067     }
0068     m_imageItem->setImage(image);
0069     m_imageItem->setSize(image.size() / image.devicePixelRatio());
0070 }
0071 
0072 } // namespace KWin