File indexing completed on 2024-05-12 04:19:52

0001 /*
0002 Gwenview: an image viewer
0003 Copyright 2019 Steffen Hartleib <steffenhartleib@t-online.de>
0004 Copyright 2022 Carl Schwan <carlschwan@kde.org>
0005 Copyright 2022 Bharadwaj Raju <bharadwaj.raju777@protonmail.com>
0006 
0007 This program is free software; you can redistribute it and/or
0008 modify it under the terms of the GNU General Public License
0009 as published by the Free Software Foundation; either version 2
0010 of the License, or (at your option) any later version.
0011 
0012 This program is distributed in the hope that it will be useful,
0013 but WITHOUT ANY WARRANTY; without even the implied warranty of
0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015 GNU General Public License for more details.
0016 
0017 You should have received a copy of the GNU General Public License
0018 along with this program; if not, write to the Free Software
0019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0020 
0021 */
0022 
0023 #include "qwayland-pointer-gestures-unstable-v1.h"
0024 #include <QGuiApplication>
0025 #include <QtWaylandClient/qwaylandclientextension.h>
0026 #include <qnamespace.h>
0027 #include <qpa/qplatformnativeinterface.h>
0028 #include <wayland-util.h>
0029 
0030 #include "wayland-pointer-gestures-unstable-v1-client-protocol.h"
0031 #include "waylandgestures.h"
0032 
0033 using namespace Gwenview;
0034 
0035 class PointerGestures : public QWaylandClientExtensionTemplate<PointerGestures>, public QtWayland::zwp_pointer_gestures_v1
0036 {
0037 public:
0038     PointerGestures()
0039         : QWaylandClientExtensionTemplate<PointerGestures>(3)
0040     {
0041     }
0042 };
0043 
0044 class PinchGesture : public QObject, public QtWayland::zwp_pointer_gesture_pinch_v1
0045 {
0046     Q_OBJECT
0047 public:
0048 public:
0049     PinchGesture(struct ::zwp_pointer_gesture_pinch_v1 *object, QObject *parent)
0050         : QObject(parent)
0051         , zwp_pointer_gesture_pinch_v1(object)
0052     {
0053     }
0054 
0055 Q_SIGNALS:
0056     void gestureBegin(uint32_t serial, uint32_t time, uint32_t fingers);
0057     void gestureUpdate(uint32_t time, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t scale, wl_fixed_t rotation);
0058     void gestureEnd(uint32_t serial, uint32_t time, int32_t cancelled);
0059 
0060 private:
0061     virtual void zwp_pointer_gesture_pinch_v1_begin(uint32_t serial, uint32_t time, struct ::wl_surface *surface, uint32_t fingers) override
0062     {
0063         Q_UNUSED(surface);
0064         Q_EMIT gestureBegin(serial, time, fingers);
0065     }
0066 
0067     virtual void zwp_pointer_gesture_pinch_v1_update(uint32_t time, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t scale, wl_fixed_t rotation) override
0068     {
0069         Q_EMIT gestureUpdate(time, dx, dy, scale, rotation);
0070     }
0071 
0072     virtual void zwp_pointer_gesture_pinch_v1_end(uint32_t serial, uint32_t time, int32_t cancelled) override
0073     {
0074         Q_EMIT gestureEnd(serial, time, cancelled);
0075     }
0076 };
0077 
0078 WaylandGestures::WaylandGestures(QObject *parent)
0079     : QObject(parent)
0080 {
0081     m_startZoom = 1.0;
0082     m_zoomModifier = 1.0;
0083     m_pointerGestures = new PointerGestures();
0084     connect(m_pointerGestures, &PointerGestures::activeChanged, this, [this]() {
0085         init();
0086     });
0087 }
0088 
0089 WaylandGestures::~WaylandGestures()
0090 {
0091     if (m_pointerGestures) {
0092         m_pointerGestures->release();
0093     }
0094     if (m_pinchGesture) {
0095         m_pinchGesture->destroy();
0096     }
0097 }
0098 
0099 void WaylandGestures::setStartZoom(double startZoom)
0100 {
0101     m_startZoom = startZoom;
0102 }
0103 
0104 void WaylandGestures::init()
0105 {
0106     QPlatformNativeInterface *native = qGuiApp->platformNativeInterface();
0107     if (!native) {
0108         return;
0109     }
0110 
0111     const auto pointer = reinterpret_cast<wl_pointer *>(native->nativeResourceForIntegration(QByteArrayLiteral("wl_pointer")));
0112     if (!pointer) {
0113         return;
0114     }
0115 
0116     m_pinchGesture = new PinchGesture(m_pointerGestures->get_pinch_gesture(pointer), this);
0117 
0118     connect(m_pinchGesture, &PinchGesture::gestureBegin, this, [this]() {
0119         Q_EMIT pinchGestureStarted();
0120     });
0121 
0122     connect(m_pinchGesture, &PinchGesture::gestureUpdate, this, [this](uint32_t time, wl_fixed_t dx, wl_fixed_t dy, wl_fixed_t scale, wl_fixed_t rotation) {
0123         Q_EMIT pinchZoomChanged(m_startZoom * wl_fixed_to_double(scale) * m_zoomModifier);
0124     });
0125 }
0126 
0127 #include "waylandgestures.moc"
0128 
0129 #include "moc_waylandgestures.cpp"