diff --git a/autotests/test_screen_edges.cpp b/autotests/test_screen_edges.cpp index e60856f8d..1e91df853 100644 --- a/autotests/test_screen_edges.cpp +++ b/autotests/test_screen_edges.cpp @@ -1,849 +1,945 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2014 Martin Gräßlin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ // kwin #include "../atoms.h" #include "../cursor.h" #include "../input.h" +#include "../gestures.h" #include "../main.h" #include "../screenedge.h" #include "../screens.h" #include "../utils.h" #include "../virtualdesktops.h" #include "../xcbutils.h" #include "mock_client.h" #include "mock_screens.h" #include "mock_workspace.h" #include "testutils.h" // Frameworks #include // Qt #include #include // xcb #include Q_DECLARE_METATYPE(KWin::ElectricBorder) Q_LOGGING_CATEGORY(KWIN_CORE, "kwin_core") namespace KWin { Atoms* atoms; int screen_number = 0; Cursor *Cursor::s_self = nullptr; static QPoint s_cursorPos = QPoint(); QPoint Cursor::pos() { return s_cursorPos; } void Cursor::setPos(const QPoint &pos) { s_cursorPos = pos; } void Cursor::setPos(int x, int y) { setPos(QPoint(x, y)); } void Cursor::startMousePolling() { } void Cursor::stopMousePolling() { } InputRedirection *InputRedirection::s_self = nullptr; void InputRedirection::registerShortcut(const QKeySequence &shortcut, QAction *action) { Q_UNUSED(shortcut) Q_UNUSED(action) } void InputRedirection::registerAxisShortcut(Qt::KeyboardModifiers modifiers, PointerAxisDirection axis, QAction *action) { Q_UNUSED(modifiers) Q_UNUSED(axis) Q_UNUSED(action) } void InputRedirection::registerTouchpadSwipeShortcut(SwipeDirection, QAction*) { } void updateXTime() { } class TestObject : public QObject { Q_OBJECT public Q_SLOTS: bool callback(ElectricBorder border); Q_SIGNALS: void gotCallback(KWin::ElectricBorder); }; bool TestObject::callback(KWin::ElectricBorder border) { emit gotCallback(border); return true; } } class TestScreenEdges : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void init(); void cleanup(); void testInit(); void testCreatingInitialEdges(); void testCallback(); void testCallbackWithCheck(); void testPushBack_data(); void testPushBack(); void testFullScreenBlocking(); void testClientEdge(); + void testTouchEdge(); }; void TestScreenEdges::initTestCase() { qApp->setProperty("x11RootWindow", QVariant::fromValue(QX11Info::appRootWindow())); qApp->setProperty("x11Connection", QVariant::fromValue(QX11Info::connection())); KWin::atoms = new KWin::Atoms; qRegisterMetaType(); } void TestScreenEdges::cleanupTestCase() { delete KWin::atoms; } void TestScreenEdges::init() { using namespace KWin; new MockWorkspace; auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); Screens::create(); auto vd = VirtualDesktopManager::create(); vd->setConfig(config); vd->load(); auto s = ScreenEdges::create(); s->setConfig(config); } void TestScreenEdges::cleanup() { using namespace KWin; delete ScreenEdges::self(); delete VirtualDesktopManager::self(); delete Screens::self(); delete workspace(); } void TestScreenEdges::testInit() { using namespace KWin; auto s = ScreenEdges::self(); s->init(); QCOMPARE(s->isDesktopSwitching(), false); QCOMPARE(s->isDesktopSwitchingMovingClients(), false); QCOMPARE(s->timeThreshold(), 150); QCOMPARE(s->reActivationThreshold(), 350); QCOMPARE(s->cursorPushBackDistance(), QSize(1, 1)); QCOMPARE(s->actionTopLeft(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionTop(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionTopRight(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionRight(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionBottomRight(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionBottom(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionBottomLeft(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionLeft(), ElectricBorderAction::ElectricActionNone); QList edges = s->findChildren(QString(), Qt::FindDirectChildrenOnly); QCOMPARE(edges.size(), 8); for (auto e : edges) { QVERIFY(!e->isReserved()); QVERIFY(e->inherits("KWin::WindowBasedEdge")); QVERIFY(!e->inherits("KWin::AreaBasedEdge")); QVERIFY(!e->client()); QVERIFY(!e->isApproaching()); } Edge *te = edges.at(0); QVERIFY(te->isCorner()); QVERIFY(!te->isScreenEdge()); QVERIFY(te->isLeft()); QVERIFY(te->isTop()); QVERIFY(!te->isRight()); QVERIFY(!te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricTopLeft); te = edges.at(1); QVERIFY(te->isCorner()); QVERIFY(!te->isScreenEdge()); QVERIFY(te->isLeft()); QVERIFY(!te->isTop()); QVERIFY(!te->isRight()); QVERIFY(te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricBottomLeft); te = edges.at(2); QVERIFY(!te->isCorner()); QVERIFY(te->isScreenEdge()); QVERIFY(te->isLeft()); QVERIFY(!te->isTop()); QVERIFY(!te->isRight()); QVERIFY(!te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricLeft); te = edges.at(3); QVERIFY(te->isCorner()); QVERIFY(!te->isScreenEdge()); QVERIFY(!te->isLeft()); QVERIFY(te->isTop()); QVERIFY(te->isRight()); QVERIFY(!te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricTopRight); te = edges.at(4); QVERIFY(te->isCorner()); QVERIFY(!te->isScreenEdge()); QVERIFY(!te->isLeft()); QVERIFY(!te->isTop()); QVERIFY(te->isRight()); QVERIFY(te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricBottomRight); te = edges.at(5); QVERIFY(!te->isCorner()); QVERIFY(te->isScreenEdge()); QVERIFY(!te->isLeft()); QVERIFY(!te->isTop()); QVERIFY(te->isRight()); QVERIFY(!te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricRight); te = edges.at(6); QVERIFY(!te->isCorner()); QVERIFY(te->isScreenEdge()); QVERIFY(!te->isLeft()); QVERIFY(te->isTop()); QVERIFY(!te->isRight()); QVERIFY(!te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricTop); te = edges.at(7); QVERIFY(!te->isCorner()); QVERIFY(te->isScreenEdge()); QVERIFY(!te->isLeft()); QVERIFY(!te->isTop()); QVERIFY(!te->isRight()); QVERIFY(te->isBottom()); QCOMPARE(te->border(), ElectricBorder::ElectricBottom); // we shouldn't have any x windows, though QCOMPARE(s->windows().size(), 0); } void TestScreenEdges::testCreatingInitialEdges() { using namespace KWin; auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); config->group("Windows").writeEntry("ElectricBorders", 2/*ElectricAlways*/); config->sync(); auto s = ScreenEdges::self(); s->setConfig(config); s->init(); // we don't have multiple desktops, so it's returning false QCOMPARE(s->isDesktopSwitching(), true); QCOMPARE(s->isDesktopSwitchingMovingClients(), true); QCOMPARE(s->actionTopLeft(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionTop(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionTopRight(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionRight(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionBottomRight(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionBottom(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionBottomLeft(), ElectricBorderAction::ElectricActionNone); QCOMPARE(s->actionLeft(), ElectricBorderAction::ElectricActionNone); QEXPECT_FAIL("", "needs fixing", Continue); QCOMPARE(s->windows().size(), 0); // set some reasonable virtual desktops config->group("Desktops").writeEntry("Number", 4); config->sync(); auto vd = VirtualDesktopManager::self(); vd->setConfig(config); vd->load(); QCOMPARE(vd->count(), 4u); QCOMPARE(vd->grid().width(), 2); QCOMPARE(vd->grid().height(), 2); // approach windows for edges not created as screen too small s->updateLayout(); auto edgeWindows = s->windows(); QCOMPARE(edgeWindows.size(), 12); auto testWindowGeometry = [&](int index) { Xcb::WindowGeometry geo(edgeWindows[index]); return geo.rect(); }; QRect sg = screens()->geometry(); const int co = s->cornerOffset(); QList expectedGeometries{ QRect(0, 0, 1, 1), QRect(0, 0, co, co), QRect(0, sg.bottom(), 1, 1), QRect(0, sg.height() - co, co, co), QRect(0, co, 1, sg.height() - co*2), // QRect(0, co * 2 + 1, co, sg.height() - co*4), QRect(sg.right(), 0, 1, 1), QRect(sg.right() - co + 1, 0, co, co), QRect(sg.right(), sg.bottom(), 1, 1), QRect(sg.right() - co + 1, sg.bottom() - co + 1, co, co), QRect(sg.right(), co, 1, sg.height() - co*2), // QRect(sg.right() - co + 1, co * 2, co, sg.height() - co*4), QRect(co, 0, sg.width() - co * 2, 1), // QRect(co * 2, 0, sg.width() - co * 4, co), QRect(co, sg.bottom(), sg.width() - co * 2, 1), // QRect(co * 2, sg.height() - co, sg.width() - co * 4, co) }; for (int i = 0; i < 12; ++i) { QCOMPARE(testWindowGeometry(i), expectedGeometries.at(i)); } QList edges = s->findChildren(QString(), Qt::FindDirectChildrenOnly); QCOMPARE(edges.size(), 8); for (auto e : edges) { QVERIFY(e->isReserved()); + QCOMPARE(e->activatesForPointer(), true); + QCOMPARE(e->activatesForTouchGesture(), false); } static_cast(screens())->setGeometries(QList{QRect{0, 0, 1024, 768}}); QSignalSpy changedSpy(screens(), SIGNAL(changed())); QVERIFY(changedSpy.isValid()); // first is before it's updated QVERIFY(changedSpy.wait()); // second is after it's updated QVERIFY(changedSpy.wait()); // let's update the layout and verify that we have edges s->recreateEdges(); edgeWindows = s->windows(); QCOMPARE(edgeWindows.size(), 16); sg = screens()->geometry(); expectedGeometries = QList{ QRect(0, 0, 1, 1), QRect(0, 0, co, co), QRect(0, sg.bottom(), 1, 1), QRect(0, sg.height() - co, co, co), QRect(0, co, 1, sg.height() - co*2), QRect(0, co * 2 + 1, co, sg.height() - co*4), QRect(sg.right(), 0, 1, 1), QRect(sg.right() - co + 1, 0, co, co), QRect(sg.right(), sg.bottom(), 1, 1), QRect(sg.right() - co + 1, sg.bottom() - co + 1, co, co), QRect(sg.right(), co, 1, sg.height() - co*2), QRect(sg.right() - co + 1, co * 2, co, sg.height() - co*4), QRect(co, 0, sg.width() - co * 2, 1), QRect(co * 2, 0, sg.width() - co * 4, co), QRect(co, sg.bottom(), sg.width() - co * 2, 1), QRect(co * 2, sg.height() - co, sg.width() - co * 4, co) }; for (int i = 0; i < 16; ++i) { QCOMPARE(testWindowGeometry(i), expectedGeometries.at(i)); } // disable desktop switching again config->group("Windows").writeEntry("ElectricBorders", 1/*ElectricMoveOnly*/); s->reconfigure(); QCOMPARE(s->isDesktopSwitching(), false); QCOMPARE(s->isDesktopSwitchingMovingClients(), true); QCOMPARE(s->windows().size(), 0); edges = s->findChildren(QString(), Qt::FindDirectChildrenOnly); QCOMPARE(edges.size(), 8); for (int i = 0; i < 8; ++i) { auto e = edges.at(i); QVERIFY(!e->isReserved()); + QCOMPARE(e->activatesForPointer(), false); + QCOMPARE(e->activatesForTouchGesture(), false); QCOMPARE(e->approachGeometry(), expectedGeometries.at(i*2+1)); } } void TestScreenEdges::testCallback() { using namespace KWin; MockWorkspace ws; static_cast(screens())->setGeometries(QList{QRect{0, 0, 1024, 768}, QRect{200, 768, 1024, 768}}); QSignalSpy changedSpy(screens(), SIGNAL(changed())); QVERIFY(changedSpy.isValid()); // first is before it's updated QVERIFY(changedSpy.wait()); // second is after it's updated QVERIFY(changedSpy.wait()); auto s = ScreenEdges::self(); s->init(); TestObject callback; QSignalSpy spy(&callback, SIGNAL(gotCallback(KWin::ElectricBorder))); QVERIFY(spy.isValid()); s->reserve(ElectricLeft, &callback, "callback"); s->reserve(ElectricTopLeft, &callback, "callback"); s->reserve(ElectricTop, &callback, "callback"); s->reserve(ElectricTopRight, &callback, "callback"); s->reserve(ElectricRight, &callback, "callback"); s->reserve(ElectricBottomRight, &callback, "callback"); s->reserve(ElectricBottom, &callback, "callback"); s->reserve(ElectricBottomLeft, &callback, "callback"); QList edges = s->findChildren(QString(), Qt::FindDirectChildrenOnly); QCOMPARE(edges.size(), 10); for (auto e: edges) { QVERIFY(e->isReserved()); + QCOMPARE(e->activatesForPointer(), true); + QCOMPARE(e->activatesForTouchGesture(), false); } auto it = std::find_if(edges.constBegin(), edges.constEnd(), [](Edge *e) { return e->isScreenEdge() && e->isLeft() && e->approachGeometry().bottom() < 768; }); QVERIFY(it != edges.constEnd()); xcb_enter_notify_event_t event; auto setPos = [&event] (const QPoint &pos) { Cursor::setPos(pos); event.root_x = pos.x(); event.root_y = pos.y(); event.event_x = pos.x(); event.event_y = pos.y(); }; event.root = XCB_WINDOW_NONE; event.child = XCB_WINDOW_NONE; event.event = (*it)->window(); event.same_screen_focus = 1; event.time = QDateTime::currentMSecsSinceEpoch(); setPos(QPoint(0, 50)); QVERIFY(s->isEntered(&event)); // doesn't trigger as the edge was not triggered yet QVERIFY(spy.isEmpty()); QCOMPARE(Cursor::pos(), QPoint(1, 50)); // test doesn't trigger due to too much offset QTest::qWait(160); setPos(QPoint(0, 100)); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); QCOMPARE(Cursor::pos(), QPoint(1, 100)); // doesn't trigger as we are waiting too long already QTest::qWait(200); setPos(QPoint(0, 101)); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); QCOMPARE(Cursor::pos(), QPoint(1, 101)); // doesn't activate as we are waiting too short QTest::qWait(50); setPos(QPoint(0, 100)); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); QCOMPARE(Cursor::pos(), QPoint(1, 100)); // and this one triggers QTest::qWait(110); setPos(QPoint(0, 101)); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(!spy.isEmpty()); QCOMPARE(Cursor::pos(), QPoint(1, 101)); // now let's try to trigger again QTest::qWait(351); setPos(QPoint(0, 100)); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QCOMPARE(spy.count(), 1); QCOMPARE(Cursor::pos(), QPoint(1, 100)); // it's still under the reactivation QTest::qWait(50); setPos(QPoint(0, 100)); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QCOMPARE(spy.count(), 1); QCOMPARE(Cursor::pos(), QPoint(1, 100)); // now it should trigger again QTest::qWait(250); setPos(QPoint(0, 100)); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QCOMPARE(spy.count(), 2); QCOMPARE(spy.first().first().value(), ElectricLeft); QCOMPARE(spy.last().first().value(), ElectricLeft); QCOMPARE(Cursor::pos(), QPoint(1, 100)); // let's disable pushback auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); config->group("Windows").writeEntry("ElectricBorderPushbackPixels", 0); config->sync(); s->setConfig(config); s->reconfigure(); // it should trigger directly QTest::qWait(350); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QCOMPARE(spy.count(), 3); QCOMPARE(spy.at(0).first().value(), ElectricLeft); QCOMPARE(spy.at(1).first().value(), ElectricLeft); QCOMPARE(spy.at(2).first().value(), ElectricLeft); QCOMPARE(Cursor::pos(), QPoint(0, 100)); // now let's unreserve again s->unreserve(ElectricTopLeft, &callback); s->unreserve(ElectricTop, &callback); s->unreserve(ElectricTopRight, &callback); s->unreserve(ElectricRight, &callback); s->unreserve(ElectricBottomRight, &callback); s->unreserve(ElectricBottom, &callback); s->unreserve(ElectricBottomLeft, &callback); s->unreserve(ElectricLeft, &callback); for (auto e: s->findChildren(QString(), Qt::FindDirectChildrenOnly)) { QVERIFY(!e->isReserved()); + QCOMPARE(e->activatesForPointer(), false); + QCOMPARE(e->activatesForTouchGesture(), false); } } void TestScreenEdges::testCallbackWithCheck() { using namespace KWin; auto s = ScreenEdges::self(); s->init(); TestObject callback; QSignalSpy spy(&callback, SIGNAL(gotCallback(KWin::ElectricBorder))); QVERIFY(spy.isValid()); s->reserve(ElectricLeft, &callback, "callback"); // check activating a different edge doesn't do anything s->check(QPoint(50, 0), QDateTime::currentDateTime(), true); QVERIFY(spy.isEmpty()); // try a direct activate without pushback Cursor::setPos(0, 50); s->check(QPoint(0, 50), QDateTime::currentDateTime(), true); QCOMPARE(spy.count(), 1); QEXPECT_FAIL("", "Argument says force no pushback, but it gets pushed back. Needs investigation", Continue); QCOMPARE(Cursor::pos(), QPoint(0, 50)); // use a different edge, this time with pushback s->reserve(KWin::ElectricRight, &callback, "callback"); Cursor::setPos(99, 50); s->check(QPoint(99, 50), QDateTime::currentDateTime()); QCOMPARE(spy.count(), 1); QCOMPARE(spy.last().first().value(), ElectricLeft); QCOMPARE(Cursor::pos(), QPoint(98, 50)); // and trigger it again QTest::qWait(160); Cursor::setPos(99, 50); s->check(QPoint(99, 50), QDateTime::currentDateTime()); QCOMPARE(spy.count(), 2); QCOMPARE(spy.last().first().value(), ElectricRight); QCOMPARE(Cursor::pos(), QPoint(98, 50)); } void TestScreenEdges::testPushBack_data() { QTest::addColumn("border"); QTest::addColumn("pushback"); QTest::addColumn("trigger"); QTest::addColumn("expected"); QTest::newRow("topleft-3") << KWin::ElectricTopLeft << 3 << QPoint(0, 0) << QPoint(3, 3); QTest::newRow("top-5") << KWin::ElectricTop << 5 << QPoint(50, 0) << QPoint(50, 5); QTest::newRow("toprigth-2") << KWin::ElectricTopRight << 2 << QPoint(99, 0) << QPoint(97, 2); QTest::newRow("right-10") << KWin::ElectricRight << 10 << QPoint(99, 50) << QPoint(89, 50); QTest::newRow("bottomright-5") << KWin::ElectricBottomRight << 5 << QPoint(99, 99) << QPoint(94, 94); QTest::newRow("bottom-10") << KWin::ElectricBottom << 10 << QPoint(50, 99) << QPoint(50, 89); QTest::newRow("bottomleft-3") << KWin::ElectricBottomLeft << 3 << QPoint(0, 99) << QPoint(3, 96); QTest::newRow("left-10") << KWin::ElectricLeft << 10 << QPoint(0, 50) << QPoint(10, 50); QTest::newRow("invalid") << KWin::ElectricLeft << 10 << QPoint(50, 0) << QPoint(50, 0); } void TestScreenEdges::testPushBack() { using namespace KWin; QFETCH(int, pushback); auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); config->group("Windows").writeEntry("ElectricBorderPushbackPixels", pushback); config->sync(); // TODO: add screens auto s = ScreenEdges::self(); s->setConfig(config); s->init(); TestObject callback; QSignalSpy spy(&callback, SIGNAL(gotCallback(KWin::ElectricBorder))); QVERIFY(spy.isValid()); QFETCH(ElectricBorder, border); s->reserve(border, &callback, "callback"); QFETCH(QPoint, trigger); Cursor::setPos(trigger); xcb_enter_notify_event_t event; event.root_x = trigger.x(); event.root_y = trigger.y(); event.event_x = trigger.x(); event.event_y = trigger.y(); event.root = XCB_WINDOW_NONE; event.child = XCB_WINDOW_NONE; event.event = s->windows().first(); event.same_screen_focus = 1; event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); QTEST(Cursor::pos(), "expected"); // do the same without the event, but the check method Cursor::setPos(trigger); s->check(trigger, QDateTime::currentDateTime()); QVERIFY(spy.isEmpty()); QTEST(Cursor::pos(), "expected"); } void TestScreenEdges::testFullScreenBlocking() { using namespace KWin; MockWorkspace ws; Client client(&ws); auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); config->group("Windows").writeEntry("ElectricBorderPushbackPixels", 1); config->sync(); auto s = ScreenEdges::self(); s->setConfig(config); s->init(); TestObject callback; QSignalSpy spy(&callback, SIGNAL(gotCallback(KWin::ElectricBorder))); QVERIFY(spy.isValid()); s->reserve(KWin::ElectricLeft, &callback, "callback"); s->reserve(KWin::ElectricBottomRight, &callback, "callback"); // currently there is no active client yet, so check blocking shouldn't do anything emit s->checkBlocking(); xcb_enter_notify_event_t event; Cursor::setPos(0, 50); event.root_x = 0; event.root_y = 50; event.event_x = 0; event.event_y = 50; event.root = XCB_WINDOW_NONE; event.child = XCB_WINDOW_NONE; event.event = s->windows().first(); event.same_screen_focus = 1; event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); QCOMPARE(Cursor::pos(), QPoint(1, 50)); client.setGeometry(screens()->geometry()); client.setActive(true); client.setFullScreen(true); ws.setActiveClient(&client); emit s->checkBlocking(); // the signal doesn't trigger for corners, let's go over all windows just to be sure that it doesn't call for corners for (auto e: s->findChildren()) { e->checkBlocking(); } // calling again should not trigger QTest::qWait(160); Cursor::setPos(0, 50); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); // and no pushback QCOMPARE(Cursor::pos(), QPoint(0, 50)); // let's make the client not fullscreen, which should trigger client.setFullScreen(false); emit s->checkBlocking(); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(!spy.isEmpty()); QCOMPARE(Cursor::pos(), QPoint(1, 50)); // let's make the client fullscreen again, but with a geometry not intersecting the left edge QTest::qWait(351); client.setFullScreen(true); client.setGeometry(client.geometry().translated(10, 0)); emit s->checkBlocking(); spy.clear(); Cursor::setPos(0, 50); event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); // and a pushback QCOMPARE(Cursor::pos(), QPoint(1, 50)); // just to be sure, let's set geometry back client.setGeometry(screens()->geometry()); emit s->checkBlocking(); Cursor::setPos(0, 50); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); // and no pushback QCOMPARE(Cursor::pos(), QPoint(0, 50)); // the corner should always trigger s->unreserve(KWin::ElectricLeft, &callback); event.event_x = 99; event.event_y = 99; event.root_x = 99; event.root_y = 99; event.event = s->windows().first(); event.time = QDateTime::currentMSecsSinceEpoch(); Cursor::setPos(99, 99); QVERIFY(s->isEntered(&event)); QVERIFY(spy.isEmpty()); // and pushback QCOMPARE(Cursor::pos(), QPoint(98, 98)); QTest::qWait(160); event.time = QDateTime::currentMSecsSinceEpoch(); Cursor::setPos(99, 99); QVERIFY(s->isEntered(&event)); QVERIFY(!spy.isEmpty()); } void TestScreenEdges::testClientEdge() { using namespace KWin; Client client(workspace()); client.setGeometry(QRect(10, 50, 10, 50)); auto s = ScreenEdges::self(); s->init(); s->reserve(&client, KWin::ElectricBottom); QPointer edge = s->findChildren().last(); QCOMPARE(edge->isReserved(), true); + QCOMPARE(edge->activatesForPointer(), true); + QCOMPARE(edge->activatesForTouchGesture(), true); //remove old reserves and resize to be in the middle of the screen s->reserve(&client, KWin::ElectricNone); client.setGeometry(QRect(2, 2, 20, 20)); // for none of the edges it should be able to be set for (int i = 0; i < ELECTRIC_COUNT; ++i) { client.setHiddenInternal(true); s->reserve(&client, static_cast(i)); QCOMPARE(client.isHiddenInternal(), false); } // now let's try to set it and activate it client.setGeometry(screens()->geometry()); client.setHiddenInternal(true); s->reserve(&client, KWin::ElectricLeft); QCOMPARE(client.isHiddenInternal(), true); xcb_enter_notify_event_t event; Cursor::setPos(0, 50); event.root_x = 0; event.root_y = 50; event.event_x = 0; event.event_y = 50; event.root = XCB_WINDOW_NONE; event.child = XCB_WINDOW_NONE; event.event = s->windows().first(); event.same_screen_focus = 1; event.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event)); // autohiding panels shall activate instantly QCOMPARE(client.isHiddenInternal(), false); QCOMPARE(Cursor::pos(), QPoint(1, 50)); // now let's reserve the client for each of the edges, in the end for the right one client.setHiddenInternal(true); s->reserve(&client, KWin::ElectricTop); s->reserve(&client, KWin::ElectricBottom); QCOMPARE(client.isHiddenInternal(), true); // corners shouldn't get reserved s->reserve(&client, KWin::ElectricTopLeft); QCOMPARE(client.isHiddenInternal(), false); client.setHiddenInternal(true); s->reserve(&client, KWin::ElectricTopRight); QCOMPARE(client.isHiddenInternal(), false); client.setHiddenInternal(true); s->reserve(&client, KWin::ElectricBottomRight); QCOMPARE(client.isHiddenInternal(), false); client.setHiddenInternal(true); s->reserve(&client, KWin::ElectricBottomLeft); QCOMPARE(client.isHiddenInternal(), false); // now finally reserve on right one client.setHiddenInternal(true); s->reserve(&client, KWin::ElectricRight); QCOMPARE(client.isHiddenInternal(), true); // now let's emulate the removal of a Client through Workspace emit workspace()->clientRemoved(&client); for (auto e : s->findChildren()) { QVERIFY(!e->client()); } QCOMPARE(client.isHiddenInternal(), true); // now let's try to trigger the client showing with the check method instead of enter notify s->reserve(&client, KWin::ElectricTop); QCOMPARE(client.isHiddenInternal(), true); Cursor::setPos(50, 0); s->check(QPoint(50, 0), QDateTime::currentDateTime()); QCOMPARE(client.isHiddenInternal(), false); QCOMPARE(Cursor::pos(), QPoint(50, 1)); // unreserve by setting to none edge s->reserve(&client, KWin::ElectricNone); // check on previous edge again, should fail client.setHiddenInternal(true); Cursor::setPos(50, 0); s->check(QPoint(50, 0), QDateTime::currentDateTime()); QCOMPARE(client.isHiddenInternal(), true); QCOMPARE(Cursor::pos(), QPoint(50, 0)); // set to windows can cover client.setGeometry(screens()->geometry()); client.setHiddenInternal(false); client.setKeepBelow(true); s->reserve(&client, KWin::ElectricLeft); QCOMPARE(client.keepBelow(), true); QCOMPARE(client.isHiddenInternal(), false); xcb_enter_notify_event_t event2; Cursor::setPos(0, 50); event2.root_x = 0; event2.root_y = 50; event2.event_x = 0; event2.event_y = 50; event2.root = XCB_WINDOW_NONE; event2.child = XCB_WINDOW_NONE; event2.event = s->windows().first(); event2.same_screen_focus = 1; event2.time = QDateTime::currentMSecsSinceEpoch(); QVERIFY(s->isEntered(&event2)); QCOMPARE(client.keepBelow(), false); QCOMPARE(client.isHiddenInternal(), false); QCOMPARE(Cursor::pos(), QPoint(1, 50)); } +void TestScreenEdges::testTouchEdge() +{ + qRegisterMetaType("ElectricBorder"); + using namespace KWin; + auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig); + auto group = config->group("TouchEdges"); + group.writeEntry("Top", "krunner"); + group.writeEntry("Left", "krunner"); + group.writeEntry("Bottom", "krunner"); + group.writeEntry("Right", "krunner"); + config->sync(); + + auto s = ScreenEdges::self(); + s->setConfig(config); + s->init(); + // we don't have multiple desktops, so it's returning false + QCOMPARE(s->isDesktopSwitching(), false); + QCOMPARE(s->isDesktopSwitchingMovingClients(), false); + QCOMPARE(s->actionTopLeft(), ElectricBorderAction::ElectricActionNone); + QCOMPARE(s->actionTop(), ElectricBorderAction::ElectricActionNone); + QCOMPARE(s->actionTopRight(), ElectricBorderAction::ElectricActionNone); + QCOMPARE(s->actionRight(), ElectricBorderAction::ElectricActionNone); + QCOMPARE(s->actionBottomRight(), ElectricBorderAction::ElectricActionNone); + QCOMPARE(s->actionBottom(), ElectricBorderAction::ElectricActionNone); + QCOMPARE(s->actionBottomLeft(), ElectricBorderAction::ElectricActionNone); + QCOMPARE(s->actionLeft(), ElectricBorderAction::ElectricActionNone); + + QList edges = s->findChildren(QString(), Qt::FindDirectChildrenOnly); + QCOMPARE(edges.size(), 8); + for (auto e : edges) { + QCOMPARE(e->isReserved(), e->isScreenEdge()); + QCOMPARE(e->activatesForPointer(), false); + QCOMPARE(e->activatesForTouchGesture(), e->isScreenEdge()); + } + + // try to activate the edge through pointer, should not be possible + auto it = std::find_if(edges.constBegin(), edges.constEnd(), [](Edge *e) { + return e->isScreenEdge() && e->isLeft(); + }); + QVERIFY(it != edges.constEnd()); + + QSignalSpy approachingSpy(s, &ScreenEdges::approaching); + QVERIFY(approachingSpy.isValid()); + + xcb_enter_notify_event_t event; + auto setPos = [&event] (const QPoint &pos) { + Cursor::setPos(pos); + event.root_x = pos.x(); + event.root_y = pos.y(); + event.event_x = pos.x(); + event.event_y = pos.y(); + }; + event.root = XCB_WINDOW_NONE; + event.child = XCB_WINDOW_NONE; + event.event = (*it)->window(); + event.same_screen_focus = 1; + event.time = QDateTime::currentMSecsSinceEpoch(); + setPos(QPoint(0, 50)); + QCOMPARE(s->isEntered(&event), false); + QVERIFY(approachingSpy.isEmpty()); + + s->gestureRecognizer()->startSwipeGesture(QPoint(0, 50)); + QCOMPARE(approachingSpy.count(), 1); + s->gestureRecognizer()->cancelSwipeGesture(); + QCOMPARE(approachingSpy.count(), 2); + + // let's reconfigure + group.writeEntry("Top", "none"); + group.writeEntry("Left", "none"); + group.writeEntry("Bottom", "none"); + group.writeEntry("Right", "none"); + config->sync(); + s->reconfigure(); + + edges = s->findChildren(QString(), Qt::FindDirectChildrenOnly); + QCOMPARE(edges.size(), 8); + for (auto e : edges) { + QCOMPARE(e->isReserved(), false); + QCOMPARE(e->activatesForPointer(), false); + QCOMPARE(e->activatesForTouchGesture(), false); + } + +} + Q_CONSTRUCTOR_FUNCTION(forceXcb) QTEST_MAIN(TestScreenEdges) #include "test_screen_edges.moc" diff --git a/screenedge.cpp b/screenedge.cpp index 9eecbe603..c30b5e732 100644 --- a/screenedge.cpp +++ b/screenedge.cpp @@ -1,1314 +1,1440 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2011 Arthur Arlt Copyright (C) 2013 Martin Gräßlin Since the functionality provided in this class has been moved from class Workspace, it is not clear who exactly has written the code. The list below contains the copyright holders of the class Workspace. Copyright (C) 1999, 2000 Matthias Ettrich Copyright (C) 2003 Lubos Lunak Copyright (C) 2009 Lucas Murray This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ #include "screenedge.h" // KWin #include "atoms.h" #include "gestures.h" #include #include "cursor.h" #include "main.h" #include "platform.h" #include "screens.h" #include "utils.h" #include #include "virtualdesktops.h" #ifdef KWIN_UNIT_TEST #include "plugins/platforms/x11/standalone/edge.h" #endif // DBus generated #include "screenlocker_interface.h" // frameworks #include // Qt #include #include #include #include #include #include #include #include namespace KWin { // Mouse should not move more than this many pixels static const int DISTANCE_RESET = 30; Edge::Edge(ScreenEdges *parent) : QObject(parent) , m_edges(parent) , m_border(ElectricNone) , m_action(ElectricActionNone) , m_reserved(0) , m_approaching(false) , m_lastApproachingFactor(0) , m_blocked(false) , m_pushBackBlocked(false) , m_client(nullptr) , m_gesture(new SwipeGesture(this)) { m_gesture->setMinimumFingerCount(1); m_gesture->setMaximumFingerCount(1); connect(m_gesture, &Gesture::triggered, this, [this] { stopApproaching(); if (m_client) { m_client->showOnScreenEdge(); unreserve(); return; } - handleAction(); - handleByCallback(); + handleTouchAction(); }, Qt::QueuedConnection ); connect(m_gesture, &SwipeGesture::started, this, &Edge::startApproaching); connect(m_gesture, &SwipeGesture::cancelled, this, &Edge::stopApproaching); connect(m_gesture, &SwipeGesture::progress, this, [this] (qreal progress) { int factor = progress * 256.0f; if (m_lastApproachingFactor != factor) { m_lastApproachingFactor = factor; emit approaching(border(), m_lastApproachingFactor/256.0f, m_approachGeometry); } } ); + connect(this, &Edge::activatesForTouchGestureChanged, this, + [this] { + if (isReserved()) { + if (activatesForTouchGesture()) { + m_edges->gestureRecognizer()->registerGesture(m_gesture); + } else { + m_edges->gestureRecognizer()->unregisterGesture(m_gesture); + } + } + } + ); } Edge::~Edge() { } void Edge::reserve() { m_reserved++; if (m_reserved == 1) { // got activated activate(); } } void Edge::reserve(QObject *object, const char *slot) { connect(object, SIGNAL(destroyed(QObject*)), SLOT(unreserve(QObject*))); m_callBacks.insert(object, QByteArray(slot)); reserve(); } void Edge::unreserve() { m_reserved--; if (m_reserved == 0) { // got deactivated stopApproaching(); deactivate(); } } void Edge::unreserve(QObject *object) { if (m_callBacks.contains(object)) { m_callBacks.remove(object); disconnect(object, SIGNAL(destroyed(QObject*)), this, SLOT(unreserve(QObject*))); unreserve(); } } +bool Edge::activatesForPointer() const +{ + if (m_client) { + return true; + } + if (m_edges->isDesktopSwitching()) { + return true; + } + if (!m_callBacks.isEmpty()) { + return true; + } + if (m_action != ElectricActionNone) { + return true; + } + return false; +} + +bool Edge::activatesForTouchGesture() const +{ + if (!isScreenEdge()) { + return false; + } + if (m_client) { + return true; + } + if (m_touchAction != ElectricActionNone) { + return true; + } + return false; +} + bool Edge::triggersFor(const QPoint &cursorPos) const { if (isBlocked()) { return false; } + if (!activatesForPointer()) { + return false; + } if (!m_geometry.contains(cursorPos)) { return false; } if (isLeft() && cursorPos.x() != m_geometry.x()) { return false; } if (isRight() && cursorPos.x() != (m_geometry.x() + m_geometry.width() -1)) { return false; } if (isTop() && cursorPos.y() != m_geometry.y()) { return false; } if (isBottom() && cursorPos.y() != (m_geometry.y() + m_geometry.height() -1)) { return false; } return true; } bool Edge::check(const QPoint &cursorPos, const QDateTime &triggerTime, bool forceNoPushBack) { if (!triggersFor(cursorPos)) { return false; } if (m_lastTrigger.isValid() && // still in cooldown m_lastTrigger.msecsTo(triggerTime) < edges()->reActivationThreshold() - edges()->timeThreshold()) { return false; } // no pushback so we have to activate at once bool directActivate = forceNoPushBack || edges()->cursorPushBackDistance().isNull() || m_client; if (directActivate || canActivate(cursorPos, triggerTime)) { markAsTriggered(cursorPos, triggerTime); handle(cursorPos); return true; } else { pushCursorBack(cursorPos); m_triggeredPoint = cursorPos; } return false; } void Edge::markAsTriggered(const QPoint &cursorPos, const QDateTime &triggerTime) { m_lastTrigger = triggerTime; m_lastReset = QDateTime(); // invalidate m_triggeredPoint = cursorPos; } bool Edge::canActivate(const QPoint &cursorPos, const QDateTime &triggerTime) { // we check whether either the timer has explicitly been invalidated (successfull trigger) or is // bigger than the reactivation threshold (activation "aborted", usually due to moving away the cursor // from the corner after successfull activation) // either condition means that "this is the first event in a new attempt" if (!m_lastReset.isValid() || m_lastReset.msecsTo(triggerTime) > edges()->reActivationThreshold()) { m_lastReset = triggerTime; return false; } if (m_lastTrigger.isValid() && m_lastTrigger.msecsTo(triggerTime) < edges()->reActivationThreshold() - edges()->timeThreshold()) { return false; } if (m_lastReset.msecsTo(triggerTime) < edges()->timeThreshold()) { return false; } // does the check on position make any sense at all? if ((cursorPos - m_triggeredPoint).manhattanLength() > DISTANCE_RESET) { return false; } return true; } void Edge::handle(const QPoint &cursorPos) { AbstractClient *movingClient = Workspace::self()->getMovingClient(); bool isResize = false; if (Client *movingClientClient = qobject_cast(movingClient)) isResize = movingClientClient->isResize(); if ((edges()->isDesktopSwitchingMovingClients() && movingClient && !isResize) || (edges()->isDesktopSwitching() && isScreenEdge())) { // always switch desktops in case: // moving a Client and option for switch on client move is enabled // or switch on screen edge is enabled switchDesktop(cursorPos); return; } if (movingClient) { // if we are moving a window we don't want to trigger the actions. This just results in // problems, e.g. Desktop Grid activated or screen locker activated which just cannot // work as we hold a grab. return; } if (m_client) { pushCursorBack(cursorPos); m_client->showOnScreenEdge(); unreserve(); return; } - if (handleAction() || handleByCallback()) { + if (handlePointerAction() || handleByCallback()) { pushCursorBack(cursorPos); return; } if (edges()->isDesktopSwitching() && isCorner()) { // try again desktop switching for the corner switchDesktop(cursorPos); } } -bool Edge::handleAction() +bool Edge::handleAction(ElectricBorderAction action) { - switch (m_action) { + switch (action) { case ElectricActionShowDesktop: { Workspace::self()->setShowingDesktop(!Workspace::self()->showingDesktop()); return true; } case ElectricActionLockScreen: { // Lock the screen OrgFreedesktopScreenSaverInterface interface(QStringLiteral("org.freedesktop.ScreenSaver"), QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus()); if (interface.isValid()) { interface.Lock(); } return true; } case ElectricActionKRunner: { // open krunner QDBusConnection::sessionBus().asyncCall( QDBusMessage::createMethodCall(QStringLiteral("org.kde.krunner"), QStringLiteral("/App"), QStringLiteral("org.kde.krunner.App"), QStringLiteral("display") ) ); return true; } case ElectricActionActivityManager: { // open activity manager QDBusConnection::sessionBus().asyncCall( QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmashell"), QStringLiteral("/PlasmaShell"), QStringLiteral("org.kde.PlasmaShell"), QStringLiteral("toggleActivityManager") ) ); return true; } case ElectricActionApplicationLauncher: { QDBusConnection::sessionBus().asyncCall( QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmashell"), QStringLiteral("/PlasmaShell"), QStringLiteral("org.kde.PlasmaShell"), QStringLiteral("activateLauncherMenu") ) ); return true; } default: return false; } } bool Edge::handleByCallback() { if (m_callBacks.isEmpty()) { return false; } for (QHash::iterator it = m_callBacks.begin(); it != m_callBacks.end(); ++it) { bool retVal = false; QMetaObject::invokeMethod(it.key(), it.value().constData(), Q_RETURN_ARG(bool, retVal), Q_ARG(ElectricBorder, m_border)); if (retVal) { return true; } } return false; } void Edge::switchDesktop(const QPoint &cursorPos) { QPoint pos(cursorPos); VirtualDesktopManager *vds = VirtualDesktopManager::self(); const uint oldDesktop = vds->current(); uint desktop = oldDesktop; const int OFFSET = 2; if (isLeft()) { const uint interimDesktop = desktop; desktop = vds->toLeft(desktop, vds->isNavigationWrappingAround()); if (desktop != interimDesktop) pos.setX(displayWidth() - 1 - OFFSET); } else if (isRight()) { const uint interimDesktop = desktop; desktop = vds->toRight(desktop, vds->isNavigationWrappingAround()); if (desktop != interimDesktop) pos.setX(OFFSET); } if (isTop()) { const uint interimDesktop = desktop; desktop = vds->above(desktop, vds->isNavigationWrappingAround()); if (desktop != interimDesktop) pos.setY(displayHeight() - 1 - OFFSET); } else if (isBottom()) { const uint interimDesktop = desktop; desktop = vds->below(desktop, vds->isNavigationWrappingAround()); if (desktop != interimDesktop) pos.setY(OFFSET); } #ifndef KWIN_UNIT_TEST if (AbstractClient *c = Workspace::self()->getMovingClient()) { if (c->rules()->checkDesktop(desktop) != int(desktop)) { // user attempts to move a client to another desktop where it is ruleforced to not be return; } } #endif vds->setCurrent(desktop); if (vds->current() != oldDesktop) { m_pushBackBlocked = true; Cursor::setPos(pos); QSharedPointer me(new QMetaObject::Connection); *me = QObject::connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::aboutToBlock, this, [this, me](){ QObject::disconnect(*me); const_cast*>(&me)->reset(nullptr); m_pushBackBlocked = false; } ); } } void Edge::pushCursorBack(const QPoint &cursorPos) { if (m_pushBackBlocked) return; int x = cursorPos.x(); int y = cursorPos.y(); const QSize &distance = edges()->cursorPushBackDistance(); if (isLeft()) { x += distance.width(); } if (isRight()) { x -= distance.width(); } if (isTop()) { y += distance.height(); } if (isBottom()) { y -= distance.height(); } Cursor::setPos(x, y); } void Edge::setGeometry(const QRect &geometry) { if (m_geometry == geometry) { return; } m_geometry = geometry; int x = m_geometry.x(); int y = m_geometry.y(); int width = m_geometry.width(); int height = m_geometry.height(); const int size = m_edges->cornerOffset(); if (isCorner()) { if (isRight()) { x = x - size +1; } if (isBottom()) { y = y - size +1; } width = size; height = size; } else { if (isLeft()) { y += size + 1; width = size; height = height - size * 2; } else if (isRight()) { x = x - size + 1; y += size; width = size; height = height - size * 2; } else if (isTop()) { x += size; width = width - size * 2; height = size; } else if (isBottom()) { x += size; y = y - size +1; width = width - size * 2; height = size; } } m_approachGeometry = QRect(x, y, width, height); doGeometryUpdate(); + + if (isScreenEdge()) { + m_gesture->setStartGeometry(m_geometry); + m_gesture->setMinimumDelta(screens()->size(screens()->number(m_geometry.center())) * 0.2); + } } void Edge::checkBlocking() { if (isCorner()) { return; } bool newValue = false; if (AbstractClient *client = Workspace::self()->activeClient()) { newValue = client->isFullScreen() && client->geometry().contains(m_geometry.center()); } if (newValue == m_blocked) { return; } m_blocked = newValue; doUpdateBlocking(); } void Edge::doUpdateBlocking() { } void Edge::doGeometryUpdate() { - if (isScreenEdge()) { - m_gesture->setStartGeometry(m_geometry); - m_gesture->setMinimumDelta(screens()->size(screens()->number(m_geometry.center())) * 0.2); - } } void Edge::activate() { - if (isScreenEdge() && !m_edges->isDesktopSwitching()) { + if (activatesForTouchGesture()) { m_edges->gestureRecognizer()->registerGesture(m_gesture); } doActivate(); } void Edge::doActivate() { } void Edge::deactivate() { m_edges->gestureRecognizer()->unregisterGesture(m_gesture); doDeactivate(); } void Edge::doDeactivate() { } void Edge::startApproaching() { if (m_approaching) { return; } m_approaching = true; doStartApproaching(); m_lastApproachingFactor = 0; emit approaching(border(), 0.0, m_approachGeometry); } void Edge::doStartApproaching() { } void Edge::stopApproaching() { if (!m_approaching) { return; } m_approaching = false; doStopApproaching(); m_lastApproachingFactor = 0; emit approaching(border(), 0.0, m_approachGeometry); } void Edge::doStopApproaching() { } void Edge::updateApproaching(const QPoint &point) { if (approachGeometry().contains(point)) { int factor = 0; const int edgeDistance = m_edges->cornerOffset(); // manhattan length for our edge const int cornerDistance = 2*edgeDistance; switch (border()) { case ElectricTopLeft: factor = (point.manhattanLength()<<8) / cornerDistance; break; case ElectricTopRight: factor = ((point - approachGeometry().topRight()).manhattanLength()<<8) / cornerDistance; break; case ElectricBottomRight: factor = ((point - approachGeometry().bottomRight()).manhattanLength()<<8) / cornerDistance; break; case ElectricBottomLeft: factor = ((point - approachGeometry().bottomLeft()).manhattanLength()<<8) / cornerDistance; break; case ElectricTop: factor = (qAbs(point.y() - approachGeometry().y())<<8) / edgeDistance; break; case ElectricRight: factor = (qAbs(point.x() - approachGeometry().right())<<8) / edgeDistance; break; case ElectricBottom: factor = (qAbs(point.y() - approachGeometry().bottom())<<8) / edgeDistance; break; case ElectricLeft: factor = (qAbs(point.x() - approachGeometry().x())<<8) / edgeDistance; break; default: break; } factor = 256 - factor; if (m_lastApproachingFactor != factor) { m_lastApproachingFactor = factor; emit approaching(border(), m_lastApproachingFactor/256.0f, m_approachGeometry); } } else { stopApproaching(); } } quint32 Edge::window() const { return 0; } quint32 Edge::approachWindow() const { return 0; } void Edge::setBorder(ElectricBorder border) { m_border = border; switch (m_border) { case ElectricTop: m_gesture->setDirection(SwipeGesture::Direction::Down); break; case ElectricRight: m_gesture->setDirection(SwipeGesture::Direction::Left); break; case ElectricBottom: m_gesture->setDirection(SwipeGesture::Direction::Up); break; case ElectricLeft: m_gesture->setDirection(SwipeGesture::Direction::Right); break; default: break; } } +void Edge::setTouchAction(ElectricBorderAction action) { + const bool wasTouch = activatesForTouchGesture(); + m_touchAction = action; + if (wasTouch != activatesForTouchGesture()) { + emit activatesForTouchGestureChanged(); + } +} + +void Edge::setClient(AbstractClient *client) +{ + const bool wasTouch = activatesForTouchGesture(); + m_client = client; + if (wasTouch != activatesForTouchGesture()) { + emit activatesForTouchGestureChanged(); + } +} + /********************************************************** * ScreenEdges *********************************************************/ KWIN_SINGLETON_FACTORY(ScreenEdges) ScreenEdges::ScreenEdges(QObject *parent) : QObject(parent) , m_desktopSwitching(false) , m_desktopSwitchingMovingClients(false) , m_timeThreshold(0) , m_reactivateThreshold(0) , m_virtualDesktopLayout(0) , m_actionTopLeft(ElectricActionNone) , m_actionTop(ElectricActionNone) , m_actionTopRight(ElectricActionNone) , m_actionRight(ElectricActionNone) , m_actionBottomRight(ElectricActionNone) , m_actionBottom(ElectricActionNone) , m_actionBottomLeft(ElectricActionNone) , m_actionLeft(ElectricActionNone) , m_gestureRecognizer(new GestureRecognizer(this)) { QWidget w; m_cornerOffset = (w.physicalDpiX() + w.physicalDpiY() + 5) / 6; connect(workspace(), &Workspace::clientRemoved, this, &ScreenEdges::deleteEdgeForClient); } ScreenEdges::~ScreenEdges() { s_self = NULL; } void ScreenEdges::init() { reconfigure(); updateLayout(); recreateEdges(); } static ElectricBorderAction electricBorderAction(const QString& name) { QString lowerName = name.toLower(); if (lowerName == QStringLiteral("showdesktop")) { return ElectricActionShowDesktop; } else if (lowerName == QStringLiteral("lockscreen")) { return ElectricActionLockScreen; } else if (lowerName == QLatin1String("krunner")) { return ElectricActionKRunner; } else if (lowerName == QLatin1String("activitymanager")) { return ElectricActionActivityManager; } else if (lowerName == QLatin1String("applicationlauncher")) { return ElectricActionApplicationLauncher; } return ElectricActionNone; } void ScreenEdges::reconfigure() { if (!m_config) { return; } // TODO: migrate settings to a group ScreenEdges KConfigGroup windowsConfig = m_config->group("Windows"); setTimeThreshold(windowsConfig.readEntry("ElectricBorderDelay", 150)); setReActivationThreshold(qMax(timeThreshold() + 50, windowsConfig.readEntry("ElectricBorderCooldown", 350))); int desktopSwitching = windowsConfig.readEntry("ElectricBorders", static_cast(ElectricDisabled)); if (desktopSwitching == ElectricDisabled) { setDesktopSwitching(false); setDesktopSwitchingMovingClients(false); } else if (desktopSwitching == ElectricMoveOnly) { setDesktopSwitching(false); setDesktopSwitchingMovingClients(true); } else if (desktopSwitching == ElectricAlways) { setDesktopSwitching(true); setDesktopSwitchingMovingClients(true); } const int pushBack = windowsConfig.readEntry("ElectricBorderPushbackPixels", 1); m_cursorPushBackDistance = QSize(pushBack, pushBack); KConfigGroup borderConfig = m_config->group("ElectricBorders"); setActionForBorder(ElectricTopLeft, &m_actionTopLeft, electricBorderAction(borderConfig.readEntry("TopLeft", "None"))); setActionForBorder(ElectricTop, &m_actionTop, electricBorderAction(borderConfig.readEntry("Top", "None"))); setActionForBorder(ElectricTopRight, &m_actionTopRight, electricBorderAction(borderConfig.readEntry("TopRight", "None"))); setActionForBorder(ElectricRight, &m_actionRight, electricBorderAction(borderConfig.readEntry("Right", "None"))); setActionForBorder(ElectricBottomRight, &m_actionBottomRight, electricBorderAction(borderConfig.readEntry("BottomRight", "None"))); setActionForBorder(ElectricBottom, &m_actionBottom, electricBorderAction(borderConfig.readEntry("Bottom", "None"))); setActionForBorder(ElectricBottomLeft, &m_actionBottomLeft, electricBorderAction(borderConfig.readEntry("BottomLeft", "None"))); setActionForBorder(ElectricLeft, &m_actionLeft, electricBorderAction(borderConfig.readEntry("Left", "None"))); + + borderConfig = m_config->group("TouchEdges"); + setActionForTouchBorder(ElectricTop, electricBorderAction(borderConfig.readEntry("Top", "None"))); + setActionForTouchBorder(ElectricRight, electricBorderAction(borderConfig.readEntry("Right", "None"))); + setActionForTouchBorder(ElectricBottom, electricBorderAction(borderConfig.readEntry("Bottom", "None"))); + setActionForTouchBorder(ElectricLeft, electricBorderAction(borderConfig.readEntry("Left", "None"))); } void ScreenEdges::setActionForBorder(ElectricBorder border, ElectricBorderAction *oldValue, ElectricBorderAction newValue) { if (*oldValue == newValue) { return; } if (*oldValue == ElectricActionNone) { // have to reserve for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { if ((*it)->border() == border) { (*it)->reserve(); } } } if (newValue == ElectricActionNone) { // have to unreserve for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { if ((*it)->border() == border) { (*it)->unreserve(); } } } *oldValue = newValue; // update action on all Edges for given border for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { if ((*it)->border() == border) { (*it)->setAction(newValue); } } } +void ScreenEdges::setActionForTouchBorder(ElectricBorder border, ElectricBorderAction newValue) +{ + auto it = m_touchActions.find(border); + ElectricBorderAction oldValue = ElectricActionNone; + if (it != m_touchActions.constEnd()) { + oldValue = it.value(); + } + if (oldValue == newValue) { + return; + } + if (oldValue == ElectricActionNone) { + // have to reserve + for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { + if ((*it)->border() == border) { + (*it)->reserve(); + } + } + } + if (newValue == ElectricActionNone) { + // have to unreserve + for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { + if ((*it)->border() == border) { + (*it)->unreserve(); + } + } + + m_touchActions.erase(it); + } else { + m_touchActions.insert(border, newValue); + } + // update action on all Edges for given border + for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { + if ((*it)->border() == border) { + (*it)->setTouchAction(newValue); + } + } +} + void ScreenEdges::updateLayout() { const QSize desktopMatrix = VirtualDesktopManager::self()->grid().size(); Qt::Orientations newLayout = 0; if (desktopMatrix.width() > 1) { newLayout |= Qt::Horizontal; } if (desktopMatrix.height() > 1) { newLayout |= Qt::Vertical; } if (newLayout == m_virtualDesktopLayout) { return; } if (isDesktopSwitching()) { reserveDesktopSwitching(false, m_virtualDesktopLayout); } m_virtualDesktopLayout = newLayout; if (isDesktopSwitching()) { reserveDesktopSwitching(true, m_virtualDesktopLayout); } } static bool isLeftScreen(const QRect &screen, const QRect &fullArea) { if (screens()->count() == 1) { return true; } if (screen.x() == fullArea.x()) { return true; } // the screen is also on the left in case of a vertical layout with a second screen // more to the left. In that case no screen ends left of screen's x coord for (int i=0; icount(); ++i) { const QRect otherGeo = screens()->geometry(i); if (otherGeo == screen) { // that's our screen to test continue; } if (otherGeo.x() + otherGeo.width() <= screen.x()) { // other screen is completely in the left return false; } } // did not find a screen left of our current screen, so it is the left most return true; } static bool isRightScreen(const QRect &screen, const QRect &fullArea) { if (screens()->count() == 1) { return true; } if (screen.x() + screen.width() == fullArea.x() + fullArea.width()) { return true; } // the screen is also on the right in case of a vertical layout with a second screen // more to the right. In that case no screen starts right of this screen for (int i=0; icount(); ++i) { const QRect otherGeo = screens()->geometry(i); if (otherGeo == screen) { // that's our screen to test continue; } if (otherGeo.x() >= screen.x() + screen.width()) { // other screen is completely in the right return false; } } // did not find a screen right of our current screen, so it is the right most return true; } static bool isTopScreen(const QRect &screen, const QRect &fullArea) { if (screens()->count() == 1) { return true; } if (screen.y() == fullArea.y()) { return true; } // the screen is also top most in case of a horizontal layout with a second screen // more to the top. In that case no screen ends above screen's y coord for (int i=0; icount(); ++i) { const QRect otherGeo = screens()->geometry(i); if (otherGeo == screen) { // that's our screen to test continue; } if (otherGeo.y() + otherGeo.height() <= screen.y()) { // other screen is completely above return false; } } // did not find a screen above our current screen, so it is the top most return true; } static bool isBottomScreen(const QRect &screen, const QRect &fullArea) { if (screens()->count() == 1) { return true; } if (screen.y() + screen.height() == fullArea.y() + fullArea.height()) { return true; } // the screen is also bottom most in case of a horizontal layout with a second screen // more below. In that case no screen starts below screen's y coord + height for (int i=0; icount(); ++i) { const QRect otherGeo = screens()->geometry(i); if (otherGeo == screen) { // that's our screen to test continue; } if (otherGeo.y() >= screen.y() + screen.height()) { // other screen is completely below return false; } } // did not find a screen below our current screen, so it is the bottom most return true; } void ScreenEdges::recreateEdges() { QList oldEdges(m_edges); m_edges.clear(); const QRect fullArea = screens()->geometry(); QRegion processedRegion; for (int i=0; icount(); ++i) { const QRegion screen = QRegion(screens()->geometry(i)).subtracted(processedRegion); processedRegion += screen; Q_FOREACH (const QRect &screenPart, screen.rects()) { if (isLeftScreen(screenPart, fullArea)) { // left most screen createVerticalEdge(ElectricLeft, screenPart, fullArea); } if (isRightScreen(screenPart, fullArea)) { // right most screen createVerticalEdge(ElectricRight, screenPart, fullArea); } if (isTopScreen(screenPart, fullArea)) { // top most screen createHorizontalEdge(ElectricTop, screenPart, fullArea); } if (isBottomScreen(screenPart, fullArea)) { // bottom most screen createHorizontalEdge(ElectricBottom, screenPart, fullArea); } } } // copy over the effect/script reservations from the old edges for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { Edge *edge = *it; for (auto oldIt = oldEdges.constBegin(); oldIt != oldEdges.constEnd(); ++oldIt) { Edge *oldEdge = *oldIt; if (oldEdge->client()) { // show the client again and don't recreate the edge oldEdge->client()->showOnScreenEdge(); continue; } if (oldEdge->border() != edge->border()) { continue; } const QHash &callbacks = oldEdge->callBacks(); for (QHash::const_iterator callback = callbacks.begin(); callback != callbacks.end(); ++callback) { edge->reserve(callback.key(), callback.value().constData()); } } } qDeleteAll(oldEdges); } void ScreenEdges::createVerticalEdge(ElectricBorder border, const QRect &screen, const QRect &fullArea) { if (border != ElectricRight && border != KWin::ElectricLeft) { return; } int y = screen.y(); int height = screen.height(); const int x = (border == ElectricLeft) ? screen.x() : screen.x() + screen.width() -1; if (isTopScreen(screen, fullArea)) { // also top most screen height -= m_cornerOffset; y += m_cornerOffset; // create top left/right edge const ElectricBorder edge = (border == ElectricLeft) ? ElectricTopLeft : ElectricTopRight; m_edges << createEdge(edge, x, screen.y(), 1, 1); } if (isBottomScreen(screen, fullArea)) { // also bottom most screen height -= m_cornerOffset; // create bottom left/right edge const ElectricBorder edge = (border == ElectricLeft) ? ElectricBottomLeft : ElectricBottomRight; m_edges << createEdge(edge, x, screen.y() + screen.height() -1, 1, 1); } // create border m_edges << createEdge(border, x, y, 1, height); } void ScreenEdges::createHorizontalEdge(ElectricBorder border, const QRect &screen, const QRect &fullArea) { if (border != ElectricTop && border != ElectricBottom) { return; } int x = screen.x(); int width = screen.width(); if (isLeftScreen(screen, fullArea)) { // also left most - adjust only x and width x += m_cornerOffset; width -= m_cornerOffset; } if (isRightScreen(screen, fullArea)) { // also right most edge width -= m_cornerOffset; } const int y = (border == ElectricTop) ? screen.y() : screen.y() + screen.height() - 1; m_edges << createEdge(border, x, y, width, 1); } Edge *ScreenEdges::createEdge(ElectricBorder border, int x, int y, int width, int height, bool createAction) { #ifdef KWIN_UNIT_TEST Edge *edge = new WindowBasedEdge(this); #else Edge *edge = kwinApp()->platform()->createScreenEdge(this); #endif edge->setBorder(border); edge->setGeometry(QRect(x, y, width, height)); if (createAction) { const ElectricBorderAction action = actionForEdge(edge); if (action != KWin::ElectricActionNone) { edge->reserve(); edge->setAction(action); } + const ElectricBorderAction touchAction = actionForTouchEdge(edge); + if (touchAction != KWin::ElectricActionNone) { + edge->reserve(); + edge->setTouchAction(touchAction); + } } if (isDesktopSwitching()) { if (edge->isCorner()) { edge->reserve(); } else { if ((m_virtualDesktopLayout & Qt::Horizontal) && (edge->isLeft() || edge->isRight())) { edge->reserve(); } if ((m_virtualDesktopLayout & Qt::Vertical) && (edge->isTop() || edge->isBottom())) { edge->reserve(); } } } connect(edge, SIGNAL(approaching(ElectricBorder,qreal,QRect)), SIGNAL(approaching(ElectricBorder,qreal,QRect))); if (edge->isScreenEdge()) { connect(this, SIGNAL(checkBlocking()), edge, SLOT(checkBlocking())); } return edge; } ElectricBorderAction ScreenEdges::actionForEdge(Edge *edge) const { switch (edge->border()) { case ElectricTopLeft: return m_actionTopLeft; case ElectricTop: return m_actionTop; case ElectricTopRight: return m_actionTopRight; case ElectricRight: return m_actionRight; case ElectricBottomRight: return m_actionBottomRight; case ElectricBottom: return m_actionBottom; case ElectricBottomLeft: return m_actionBottomLeft; case ElectricLeft: return m_actionLeft; default: // fall through break; } return ElectricActionNone; } +ElectricBorderAction ScreenEdges::actionForTouchEdge(Edge *edge) const +{ + auto it = m_touchActions.find(edge->border()); + if (it != m_touchActions.end()) { + return it.value(); + } + return ElectricActionNone; +} + void ScreenEdges::reserveDesktopSwitching(bool isToReserve, Qt::Orientations o) { if (!o) return; for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { Edge *edge = *it; if (edge->isCorner()) { isToReserve ? edge->reserve() : edge->unreserve(); } else { if ((m_virtualDesktopLayout & Qt::Horizontal) && (edge->isLeft() || edge->isRight())) { isToReserve ? edge->reserve() : edge->unreserve(); } if ((m_virtualDesktopLayout & Qt::Vertical) && (edge->isTop() || edge->isBottom())) { isToReserve ? edge->reserve() : edge->unreserve(); } } } } void ScreenEdges::reserve(ElectricBorder border, QObject *object, const char *slot) { for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { if ((*it)->border() == border) { (*it)->reserve(object, slot); } } } void ScreenEdges::unreserve(ElectricBorder border, QObject *object) { for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { if ((*it)->border() == border) { (*it)->unreserve(object); } } } void ScreenEdges::reserve(AbstractClient *client, ElectricBorder border) { bool hadBorder = false; auto it = m_edges.begin(); while (it != m_edges.end()) { if ((*it)->client() == client) { hadBorder = true; delete *it; it = m_edges.erase(it); } else { it++; } } if (border != ElectricNone) { createEdgeForClient(client, border); } else { if (hadBorder) // show again client->showOnScreenEdge(); } } void ScreenEdges::createEdgeForClient(AbstractClient *client, ElectricBorder border) { int y = 0; int x = 0; int width = 0; int height = 0; const QRect geo = client->geometry(); const QRect fullArea = workspace()->clientArea(FullArea, 0, 1); for (int i = 0; i < screens()->count(); ++i) { const QRect screen = screens()->geometry(i); if (!screen.contains(geo)) { // ignoring Clients having a geometry overlapping with multiple screens // this would make the code more complex. If it's needed in future it can be added continue; } const bool bordersTop = (screen.y() == geo.y()); const bool bordersLeft = (screen.x() == geo.x()); const bool bordersBottom = (screen.y() + screen.height() == geo.y() + geo.height()); const bool bordersRight = (screen.x() + screen.width() == geo.x() + geo.width()); if (bordersTop && border == ElectricTop) { if (!isTopScreen(screen, fullArea)) { continue; } y = geo.y(); x = geo.x(); height = 1; width = geo.width(); break; } if (bordersBottom && border == ElectricBottom) { if (!isBottomScreen(screen, fullArea)) { continue; } y = geo.y() + geo.height() - 1; x = geo.x(); height = 1; width = geo.width(); break; } if (bordersLeft && border == ElectricLeft) { if (!isLeftScreen(screen, fullArea)) { continue; } x = geo.x(); y = geo.y(); width = 1; height = geo.height(); break; } if (bordersRight && border == ElectricRight) { if (!isRightScreen(screen, fullArea)) { continue; } x = geo.x() + geo.width() - 1; y = geo.y(); width = 1; height = geo.height(); break; } } if (width > 0 && height > 0) { Edge *edge = createEdge(border, x, y, width, height, false); edge->setClient(client); m_edges.append(edge); edge->reserve(); } else { // we could not create an edge window, so don't allow the window to hide client->showOnScreenEdge(); } } void ScreenEdges::deleteEdgeForClient(AbstractClient* c) { auto it = m_edges.begin(); while (it != m_edges.end()) { if ((*it)->client() == c) { delete *it; it = m_edges.erase(it); } else { it++; } } } void ScreenEdges::check(const QPoint &pos, const QDateTime &now, bool forceNoPushBack) { bool activatedForClient = false; for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { if (!(*it)->isReserved()) { continue; } if ((*it)->approachGeometry().contains(pos)) { (*it)->startApproaching(); } if ((*it)->client() != nullptr && activatedForClient) { (*it)->markAsTriggered(pos, now); continue; } if ((*it)->check(pos, now, forceNoPushBack)) { if ((*it)->client()) { activatedForClient = true; } } } } bool ScreenEdges::isEntered(xcb_enter_notify_event_t *event) { return handleEnterNotifiy(event->event, QPoint(event->root_x, event->root_y), QDateTime::fromMSecsSinceEpoch(event->time)); } bool ScreenEdges::isEntered(xcb_client_message_event_t *event) { if (event->type != atoms->xdnd_position) { return false; } return handleDndNotify(event->window, QPoint(event->data.data32[2] >> 16, event->data.data32[2] & 0xffff)); } bool ScreenEdges::isEntered(QMouseEvent *event) { if (event->type() != QEvent::MouseMove) { return false; } bool activated = false; bool activatedForClient = false; for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { Edge *edge = *it; if (!edge->isReserved()) { continue; } + if (!edge->activatesForPointer()) { + continue; + } if (edge->approachGeometry().contains(event->globalPos())) { if (!edge->isApproaching()) { edge->startApproaching(); } else { edge->updateApproaching(event->globalPos()); } } else { if (edge->isApproaching()) { edge->stopApproaching(); } } if (edge->geometry().contains(event->globalPos())) { if (edge->check(event->globalPos(), QDateTime::fromMSecsSinceEpoch(event->timestamp()))) { if (edge->client()) { activatedForClient = true; } } } } if (activatedForClient) { for (auto it = m_edges.constBegin(); it != m_edges.constEnd(); ++it) { if ((*it)->client()) { (*it)->markAsTriggered(event->globalPos(), QDateTime::fromMSecsSinceEpoch(event->timestamp())); } } } return activated; } bool ScreenEdges::handleEnterNotifiy(xcb_window_t window, const QPoint &point, const QDateTime ×tamp) { bool activated = false; bool activatedForClient = false; for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { Edge *edge = *it; if (!edge || edge->window() == XCB_WINDOW_NONE) { continue; } if (!edge->isReserved()) { continue; } + if (!edge->activatesForPointer()) { + continue; + } if (edge->window() == window) { if (edge->check(point, timestamp)) { if ((*it)->client()) { activatedForClient = true; } } activated = true; break; } if (edge->approachWindow() == window) { edge->startApproaching(); // TODO: if it's a corner, it should also trigger for other windows return true; } } if (activatedForClient) { for (auto it = m_edges.constBegin(); it != m_edges.constEnd(); ++it) { if ((*it)->client()) { (*it)->markAsTriggered(point, timestamp); } } } return activated; } bool ScreenEdges::handleDndNotify(xcb_window_t window, const QPoint &point) { for (auto it = m_edges.begin(); it != m_edges.end(); ++it) { Edge *edge = *it; if (!edge || edge->window() == XCB_WINDOW_NONE) { continue; } if (edge->isReserved() && edge->window() == window) { updateXTime(); edge->check(point, QDateTime::fromMSecsSinceEpoch(xTime()), true); return true; } } return false; } void ScreenEdges::ensureOnTop() { Xcb::restackWindowsWithRaise(windows()); } QVector< xcb_window_t > ScreenEdges::windows() const { QVector wins; for (auto it = m_edges.constBegin(); it != m_edges.constEnd(); ++it) { Edge *edge = *it; xcb_window_t w = edge->window(); if (w != XCB_WINDOW_NONE) { wins.append(w); } // TODO: lambda w = edge->approachWindow(); if (w != XCB_WINDOW_NONE) { wins.append(w); } } return wins; } } //namespace diff --git a/screenedge.h b/screenedge.h index a65183b79..6d398c35a 100644 --- a/screenedge.h +++ b/screenedge.h @@ -1,560 +1,570 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2011 Arthur Arlt Copyright (C) 2013 Martin Gräßlin Since the functionality provided in this class has been moved from class Workspace, it is not clear who exactly has written the code. The list below contains the copyright holders of the class Workspace. Copyright (C) 1999, 2000 Matthias Ettrich Copyright (C) 2003 Lubos Lunak Copyright (C) 2009 Lucas Murray This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ #ifndef KWIN_SCREENEDGE_H #define KWIN_SCREENEDGE_H // KWin #include "kwinglobals.h" // KDE includes #include // Qt #include #include #include #include class QMouseEvent; namespace KWin { class AbstractClient; class GestureRecognizer; class ScreenEdges; class SwipeGesture; class KWIN_EXPORT Edge : public QObject { Q_OBJECT public: explicit Edge(ScreenEdges *parent); virtual ~Edge(); bool isLeft() const; bool isTop() const; bool isRight() const; bool isBottom() const; bool isCorner() const; bool isScreenEdge() const; bool triggersFor(const QPoint &cursorPos) const; bool check(const QPoint &cursorPos, const QDateTime &triggerTime, bool forceNoPushBack = false); void markAsTriggered(const QPoint &cursorPos, const QDateTime &triggerTime); bool isReserved() const; const QRect &approachGeometry() const; ElectricBorder border() const; void reserve(QObject *object, const char *slot); const QHash &callBacks() const; void startApproaching(); void stopApproaching(); bool isApproaching() const; void setClient(AbstractClient *client); AbstractClient *client() const; const QRect &geometry() const; + void setTouchAction(ElectricBorderAction action); + + bool activatesForPointer() const; + bool activatesForTouchGesture() const; /** * The window id of the native window representing the edge. * Default implementation returns @c 0, which means no window. **/ virtual quint32 window() const; /** * The approach window is a special window to notice when get close to the screen border but * not yet triggering the border. * * The default implementation returns @c 0, which means no window. **/ virtual quint32 approachWindow() const; public Q_SLOTS: void reserve(); void unreserve(); void unreserve(QObject *object); void setBorder(ElectricBorder border); void setAction(ElectricBorderAction action); void setGeometry(const QRect &geometry); void updateApproaching(const QPoint &point); void checkBlocking(); Q_SIGNALS: void approaching(ElectricBorder border, qreal factor, const QRect &geometry); + void activatesForTouchGestureChanged(); protected: ScreenEdges *edges(); const ScreenEdges *edges() const; bool isBlocked() const; virtual void doGeometryUpdate(); virtual void doActivate(); virtual void doDeactivate(); virtual void doStartApproaching(); virtual void doStopApproaching(); virtual void doUpdateBlocking(); private: void activate(); void deactivate(); bool canActivate(const QPoint &cursorPos, const QDateTime &triggerTime); void handle(const QPoint &cursorPos); - bool handleAction(); + bool handleAction(ElectricBorderAction action); + bool handlePointerAction() { + return handleAction(m_action); + } + bool handleTouchAction() { + return handleAction(m_touchAction); + } bool handleByCallback(); void switchDesktop(const QPoint &cursorPos); void pushCursorBack(const QPoint &cursorPos); ScreenEdges *m_edges; ElectricBorder m_border; ElectricBorderAction m_action; + ElectricBorderAction m_touchAction = ElectricActionNone; int m_reserved; QRect m_geometry; QRect m_approachGeometry; QDateTime m_lastTrigger; QDateTime m_lastReset; QPoint m_triggeredPoint; QHash m_callBacks; bool m_approaching; int m_lastApproachingFactor; bool m_blocked; bool m_pushBackBlocked; AbstractClient *m_client; SwipeGesture *m_gesture; }; /** * @short Class for controlling screen edges. * * The screen edge functionality is split into three parts: * @li This manager class ScreenEdges * @li abstract class @link Edge * @li specific implementation of @link Edge, e.g. @link WindowBasedEdge * * The ScreenEdges creates an @link Edge for each screen edge which is also an edge in the * combination of all screens. E.g. if there are two screens, no Edge is created between the screens, * but at all other edges even if the screens have a different dimension. * * In addition at each corner of the overall display geometry an one-pixel large @link Edge is * created. No matter how many screens there are, there will only be exactly four of these corner * edges. This is motivated by Fitts's Law which show that it's easy to trigger such a corner, but * it would be very difficult to trigger a corner between two screens (one pixel target not visually * outlined). * * The ScreenEdges are used for one of the following functionality: * @li switch virtual desktop (see property @link desktopSwitching) * @li switch virtual desktop when moving a window (see property @link desktopSwitchingMovingClients) * @li trigger a pre-defined action (see properties @link actionTop and similar) * @li trigger an externally configured action (e.g. Effect, Script, see @link reserve, @link unreserve) * * An @link Edge is only active if there is at least one of the possible actions "reserved" for this * edge. The idea is to not block the screen edge if nothing could be triggered there, so that the * user e.g. can configure nothing on the top edge, which tends to interfere with full screen apps * having a hidden panel there. On X11 (currently only supported backend) the @link Edge is * represented by a @link WindowBasedEdge which creates an input only window for the geometry and * reacts on enter notify events. If the edge gets reserved for the first time a window is created * and mapped, once the edge gets unreserved again, the window gets destroyed. * * When the mouse enters one of the screen edges the following values are used to determine whether * the action should be triggered or the cursor be pushed back * @li Time difference between two entering events is not larger than a certain threshold * @li Time difference between two entering events is larger than @link timeThreshold * @li Time difference between two activations is larger than @link reActivateThreshold * @li Distance between two enter events is not larger than a defined pixel distance * These checks are performed in @link Edge * * @todo change way how Effects/Scripts can reserve an edge and are notified. */ class KWIN_EXPORT ScreenEdges : public QObject { Q_OBJECT Q_PROPERTY(bool desktopSwitching READ isDesktopSwitching) Q_PROPERTY(bool desktopSwitchingMovingClients READ isDesktopSwitchingMovingClients) Q_PROPERTY(QSize cursorPushBackDistance READ cursorPushBackDistance) Q_PROPERTY(int timeThreshold READ timeThreshold) Q_PROPERTY(int reActivateThreshold READ reActivationThreshold) Q_PROPERTY(int actionTopLeft READ actionTopLeft) Q_PROPERTY(int actionTop READ actionTop) Q_PROPERTY(int actionTopRight READ actionTopRight) Q_PROPERTY(int actionRight READ actionRight) Q_PROPERTY(int actionBottomRight READ actionBottomRight) Q_PROPERTY(int actionBottom READ actionBottom) Q_PROPERTY(int actionBottomLeft READ actionBottomLeft) Q_PROPERTY(int actionLeft READ actionLeft) public: virtual ~ScreenEdges(); /** * @internal **/ void setConfig(KSharedConfig::Ptr config); /** * Initialize the screen edges. * @internal */ void init(); /** * Check, if a screen edge is entered and trigger the appropriate action * if one is enabled for the current region and the timeout is satisfied * @param pos the position of the mouse pointer * @param now the time when the function is called * @param forceNoPushBack needs to be called to workaround some DnD clients, don't use unless you want to chek on a DnD event */ void check(const QPoint& pos, const QDateTime &now, bool forceNoPushBack = false); /** * The (dpi dependent) length, reserved for the active corners of each edge - 1/3" */ int cornerOffset() const; /** * Mark the specified screen edge as reserved. This method is provided for external activation * like effects and scripts. When the effect/script does no longer need the edge it is supposed * to call @link unreserve. * @param border the screen edge to mark as reserved * @param object The object on which the @p callback needs to be invoked * @param callback The method name to be invoked - uses QMetaObject::invokeMethod * @see unreserve * @todo: add pointer to script/effect */ void reserve(ElectricBorder border, QObject *object, const char *callback); /** * Mark the specified screen edge as unreserved. This method is provided for external activation * like effects and scripts. This method is only allowed to be called if @link reserve had been * called before for the same @p border. An unbalanced calling of reserve/unreserve leads to the * edge never being active or never being able to deactivate again. * @param border the screen edge to mark as unreserved * @param object the object on which the callback had been invoked * @see reserve * @todo: add pointer to script/effect */ void unreserve(ElectricBorder border, QObject *object); /** * Reserves an edge for the @p client. The idea behind this is to show the @p client if the * screen edge which the @p client borders gets triggered. * * When first called it is tried to create an Edge for the client. This is only done if the * client borders with a screen edge specified by @p border. If the client doesn't border the * screen edge, no Edge gets created and the client is shown again. Otherwise there would not * be a possibility to show the client again. * * On subsequent calls for the client no new Edge is created, but the existing one gets reused * and if the client is already hidden, the Edge gets reserved. * * Once the Edge for the client triggers, the client gets shown again and the Edge unreserved. * The idea is that the Edge can only get activated if the client is currently hidden. * * To make sure that the client can always be shown again the implementation also starts to * track geometry changes and shows the Client again. The same for screen geometry changes. * * The Edge gets automatically destroyed if the client gets released. * @param client The Client for which an Edge should be reserved * @param border The border which the client wants to use, only proper borders are supported (no corners) **/ void reserve(KWin::AbstractClient *client, ElectricBorder border); /** * Reserve desktop switching for screen edges, if @p isToReserve is @c true. Unreserve otherwise. * @param reserve indicated weather desktop switching should be reserved or unreseved */ void reserveDesktopSwitching(bool isToReserve, Qt::Orientations o); /** * Raise electric border windows to the real top of the screen. We only need * to do this if an effect input window is active. */ void ensureOnTop(); /** * Called when the user entered an electric border with the mouse. * It may switch to another virtual desktop. * @param e the X event which is passed to this method. */ bool isEntered(xcb_generic_event_t *e); bool isEntered(xcb_enter_notify_event_t *e); bool isEntered(xcb_client_message_event_t *e); bool isEntered(QMouseEvent *event); /** * Returns a QVector of all existing screen edge windows * @return all existing screen edge windows in a QVector */ QVector< xcb_window_t > windows() const; bool isDesktopSwitching() const; bool isDesktopSwitchingMovingClients() const; const QSize &cursorPushBackDistance() const; /** * Minimum time between the push back of the cursor and the activation by re-entering the edge. **/ int timeThreshold() const; /** * Minimum time between triggers **/ int reActivationThreshold() const; ElectricBorderAction actionTopLeft() const; ElectricBorderAction actionTop() const; ElectricBorderAction actionTopRight() const; ElectricBorderAction actionRight() const; ElectricBorderAction actionBottomRight() const; ElectricBorderAction actionBottom() const; ElectricBorderAction actionBottomLeft() const; ElectricBorderAction actionLeft() const; GestureRecognizer *gestureRecognizer() const { return m_gestureRecognizer; } public Q_SLOTS: void reconfigure(); /** * Updates the layout of virtual desktops and adjust the reserved borders in case of * virtual desktop switching on edges. **/ void updateLayout(); /** * Recreates all edges e.g. after the screen size changes. **/ void recreateEdges(); Q_SIGNALS: /** * Signal emitted during approaching of mouse towards @p border. The @p factor indicates how * far away the mouse is from the approaching area. The values are clamped into [0.0,1.0] with * @c 0.0 meaning far away from the border, @c 1.0 in trigger distance. **/ void approaching(ElectricBorder border, qreal factor, const QRect &geometry); void checkBlocking(); private: enum { ElectricDisabled = 0, ElectricMoveOnly = 1, ElectricAlways = 2 }; void setDesktopSwitching(bool enable); void setDesktopSwitchingMovingClients(bool enable); void setCursorPushBackDistance(const QSize &distance); void setTimeThreshold(int threshold); void setReActivationThreshold(int threshold); void createHorizontalEdge(ElectricBorder border, const QRect &screen, const QRect &fullArea); void createVerticalEdge(ElectricBorder border, const QRect &screen, const QRect &fullArea); Edge *createEdge(ElectricBorder border, int x, int y, int width, int height, bool createAction = true); void setActionForBorder(ElectricBorder border, ElectricBorderAction *oldValue, ElectricBorderAction newValue); + void setActionForTouchBorder(ElectricBorder border, ElectricBorderAction newValue); ElectricBorderAction actionForEdge(Edge *edge) const; + ElectricBorderAction actionForTouchEdge(Edge *edge) const; bool handleEnterNotifiy(xcb_window_t window, const QPoint &point, const QDateTime ×tamp); bool handleDndNotify(xcb_window_t window, const QPoint &point); void createEdgeForClient(AbstractClient *client, ElectricBorder border); void deleteEdgeForClient(AbstractClient *client); bool m_desktopSwitching; bool m_desktopSwitchingMovingClients; QSize m_cursorPushBackDistance; int m_timeThreshold; int m_reactivateThreshold; Qt::Orientations m_virtualDesktopLayout; QList m_edges; KSharedConfig::Ptr m_config; ElectricBorderAction m_actionTopLeft; ElectricBorderAction m_actionTop; ElectricBorderAction m_actionTopRight; ElectricBorderAction m_actionRight; ElectricBorderAction m_actionBottomRight; ElectricBorderAction m_actionBottom; ElectricBorderAction m_actionBottomLeft; ElectricBorderAction m_actionLeft; + QMap m_touchActions; int m_cornerOffset; GestureRecognizer *m_gestureRecognizer; KWIN_SINGLETON(ScreenEdges) }; /********************************************************** * Inlines Edge *********************************************************/ inline bool Edge::isBottom() const { return m_border == ElectricBottom || m_border == ElectricBottomLeft || m_border == ElectricBottomRight; } inline bool Edge::isLeft() const { return m_border == ElectricLeft || m_border == ElectricTopLeft || m_border == ElectricBottomLeft; } inline bool Edge::isRight() const { return m_border == ElectricRight || m_border == ElectricTopRight || m_border == ElectricBottomRight; } inline bool Edge::isTop() const { return m_border == ElectricTop || m_border == ElectricTopLeft || m_border == ElectricTopRight; } inline bool Edge::isCorner() const { return m_border == ElectricTopLeft || m_border == ElectricTopRight || m_border == ElectricBottomRight || m_border == ElectricBottomLeft; } inline bool Edge::isScreenEdge() const { return m_border == ElectricLeft || m_border == ElectricRight || m_border == ElectricTop || m_border == ElectricBottom; } inline bool Edge::isReserved() const { return m_reserved != 0; } inline void Edge::setAction(ElectricBorderAction action) { m_action = action; } inline ScreenEdges *Edge::edges() { return m_edges; } inline const ScreenEdges *Edge::edges() const { return m_edges; } inline const QRect &Edge::geometry() const { return m_geometry; } inline const QRect &Edge::approachGeometry() const { return m_approachGeometry; } inline ElectricBorder Edge::border() const { return m_border; } inline const QHash< QObject *, QByteArray > &Edge::callBacks() const { return m_callBacks; } inline bool Edge::isBlocked() const { return m_blocked; } -inline void Edge::setClient(AbstractClient *client) -{ - m_client = client; -} - inline AbstractClient *Edge::client() const { return m_client; } inline bool Edge::isApproaching() const { return m_approaching; } /********************************************************** * Inlines ScreenEdges *********************************************************/ inline void ScreenEdges::setConfig(KSharedConfig::Ptr config) { m_config = config; } inline int ScreenEdges::cornerOffset() const { return m_cornerOffset; } inline const QSize &ScreenEdges::cursorPushBackDistance() const { return m_cursorPushBackDistance; } inline bool ScreenEdges::isDesktopSwitching() const { return m_desktopSwitching; } inline bool ScreenEdges::isDesktopSwitchingMovingClients() const { return m_desktopSwitchingMovingClients; } inline int ScreenEdges::reActivationThreshold() const { return m_reactivateThreshold; } inline int ScreenEdges::timeThreshold() const { return m_timeThreshold; } inline void ScreenEdges::setCursorPushBackDistance(const QSize &distance) { m_cursorPushBackDistance = distance; } inline void ScreenEdges::setDesktopSwitching(bool enable) { if (enable == m_desktopSwitching) { return; } m_desktopSwitching = enable; reserveDesktopSwitching(enable, m_virtualDesktopLayout); } inline void ScreenEdges::setDesktopSwitchingMovingClients(bool enable) { m_desktopSwitchingMovingClients = enable; } inline void ScreenEdges::setReActivationThreshold(int threshold) { Q_ASSERT(threshold >= m_timeThreshold); m_reactivateThreshold = threshold; } inline void ScreenEdges::setTimeThreshold(int threshold) { m_timeThreshold = threshold; } #define ACTION( name ) \ inline ElectricBorderAction ScreenEdges::name() const \ { \ return m_##name; \ } ACTION(actionTopLeft) ACTION(actionTop) ACTION(actionTopRight) ACTION(actionRight) ACTION(actionBottomRight) ACTION(actionBottom) ACTION(actionBottomLeft) ACTION(actionLeft) #undef ACTION } #endif // KWIN_SCREENEDGE_H