File indexing completed on 2024-04-28 05:30:15

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "dpmsinputeventfilter.h"
0010 #include "core/output.h"
0011 #include "core/outputbackend.h"
0012 #include "core/session.h"
0013 #include "input_event.h"
0014 #include "main.h"
0015 #include "wayland/seat.h"
0016 #include "wayland_server.h"
0017 #include "workspace.h"
0018 
0019 #include <QGuiApplication>
0020 #include <QKeyEvent>
0021 
0022 namespace KWin
0023 {
0024 
0025 DpmsInputEventFilter::DpmsInputEventFilter()
0026     : InputEventFilter()
0027 {
0028     KSharedConfig::Ptr kwinSettings = kwinApp()->config();
0029     m_enableDoubleTap = kwinSettings->group(QStringLiteral("Wayland")).readEntry<bool>("DoubleTapWakeup", true);
0030     if (Session *session = kwinApp()->outputBackend()->session()) {
0031         connect(session, &Session::awoke, this, &DpmsInputEventFilter::notify);
0032     }
0033 }
0034 
0035 DpmsInputEventFilter::~DpmsInputEventFilter()
0036 {
0037     notify();
0038 }
0039 
0040 bool DpmsInputEventFilter::pointerEvent(MouseEvent *event, quint32 nativeButton)
0041 {
0042     notify();
0043     return true;
0044 }
0045 
0046 bool DpmsInputEventFilter::wheelEvent(WheelEvent *event)
0047 {
0048     notify();
0049     return true;
0050 }
0051 
0052 bool DpmsInputEventFilter::keyEvent(KeyEvent *event)
0053 {
0054     if (event->type() == QKeyEvent::KeyPress) {
0055         notify();
0056     } else if (event->type() == QKeyEvent::KeyRelease) {
0057         return false;
0058     }
0059     return true;
0060 }
0061 
0062 bool DpmsInputEventFilter::touchDown(qint32 id, const QPointF &pos, std::chrono::microseconds time)
0063 {
0064     if (m_enableDoubleTap) {
0065         if (m_touchPoints.isEmpty()) {
0066             if (!m_doubleTapTimer.isValid()) {
0067                 // this is the first tap
0068                 m_doubleTapTimer.start();
0069             } else {
0070                 if (m_doubleTapTimer.elapsed() < qApp->doubleClickInterval()) {
0071                     m_secondTap = true;
0072                 } else {
0073                     // took too long. Let's consider it a new click
0074                     m_doubleTapTimer.restart();
0075                 }
0076             }
0077         } else {
0078             // not a double tap
0079             m_doubleTapTimer.invalidate();
0080             m_secondTap = false;
0081         }
0082         m_touchPoints << id;
0083     }
0084     return true;
0085 }
0086 
0087 bool DpmsInputEventFilter::touchUp(qint32 id, std::chrono::microseconds time)
0088 {
0089     if (m_enableDoubleTap) {
0090         m_touchPoints.removeAll(id);
0091         if (m_touchPoints.isEmpty() && m_doubleTapTimer.isValid() && m_secondTap) {
0092             if (m_doubleTapTimer.elapsed() < qApp->doubleClickInterval()) {
0093                 waylandServer()->seat()->setTimestamp(std::chrono::duration_cast<std::chrono::milliseconds>(time));
0094                 notify();
0095             }
0096             m_doubleTapTimer.invalidate();
0097             m_secondTap = false;
0098         }
0099     }
0100     return true;
0101 }
0102 
0103 bool DpmsInputEventFilter::touchMotion(qint32 id, const QPointF &pos, std::chrono::microseconds time)
0104 {
0105     // ignore the event
0106     return true;
0107 }
0108 
0109 void DpmsInputEventFilter::notify()
0110 {
0111     const QList<Output *> outputs = workspace()->outputs();
0112     for (Output *output : outputs) {
0113         output->setDpmsMode(Output::DpmsMode::On);
0114     }
0115 }
0116 
0117 }
0118 #include "moc_dpmsinputeventfilter.cpp"