libinput: Support switching the targetted output with a shortcut

I was told it's how artists are used to using these tablets so it makes
sense to support the workflow.
This commit is contained in:
Aleix Pol 2022-10-26 19:23:45 +02:00 committed by Aleix Pol Gonzalez
parent 77c5198dc8
commit 47c3c2d143
3 changed files with 52 additions and 2 deletions

View file

@ -191,7 +191,7 @@ public:
{
return m_sysName;
}
QString outputName() const
QString outputName() const override
{
return m_outputName;
}
@ -586,7 +586,7 @@ public:
/**
* Used to deserialize monitor data from KConfig when initializing a device
*/
void setOutputName(const QString &uuid);
void setOutputName(const QString &uuid) override;
QString defaultOutputName() const
{
return {};

View file

@ -44,6 +44,15 @@ public:
virtual bool isTabletModeSwitch() const = 0;
virtual bool isLidSwitch() const = 0;
virtual QString outputName() const
{
return {};
}
virtual void setOutputName(const QString &outputName)
{
Q_UNUSED(outputName)
}
Q_SIGNALS:
void keyChanged(quint32 key, InputRedirection::KeyboardKeyState, quint32 time, InputDevice *device);
void pointerButtonChanged(quint32 button, InputRedirection::PointerButtonState state, quint32 time, InputDevice *device);

View file

@ -71,6 +71,7 @@
#include <xkbcommon/xkbcommon.h>
#include "osd.h"
#include <cmath>
namespace KWin
@ -2060,6 +2061,13 @@ public:
}
connect(input(), &InputRedirection::deviceAdded, this, &TabletInputFilter::integrateDevice);
connect(input(), &InputRedirection::deviceRemoved, this, &TabletInputFilter::removeDevice);
auto tabletNextOutput = new QAction(this);
tabletNextOutput->setProperty("componentName", QStringLiteral(KWIN_NAME));
tabletNextOutput->setText(i18n("Move the tablet to the next output"));
tabletNextOutput->setObjectName(QStringLiteral("Move Tablet to Next Output"));
KGlobalAccel::setGlobalShortcut(tabletNextOutput, QList<QKeySequence>());
connect(tabletNextOutput, &QAction::triggered, this, &TabletInputFilter::trackNextOutput);
}
static KWaylandServer::TabletSeatV2Interface *findTabletSeat()
@ -2105,6 +2113,39 @@ public:
}
}
static void trackNextOutput()
{
const auto outputs = workspace()->outputs();
if (outputs.isEmpty()) {
return;
}
int tabletToolCount = 0;
InputDevice *changedDevice = nullptr;
const auto devices = input()->devices();
for (const auto device : devices) {
if (!device->isTabletTool()) {
continue;
}
tabletToolCount++;
if (device->outputName().isEmpty()) {
device->setOutputName(outputs.constFirst()->name());
changedDevice = device;
continue;
}
auto it = std::find_if(outputs.begin(), outputs.end(), [device](const auto &output) {
return output->name() == device->outputName();
});
++it;
auto nextOutput = it == outputs.end() ? outputs.first() : *it;
device->setOutputName(nextOutput->name());
changedDevice = device;
}
OSD::show(i18np("Tablet moved to %2", "Tablets switched outputs", tabletToolCount, changedDevice->outputName()), QStringLiteral("input-tablet"), 5000);
}
void removeDevice(InputDevice *inputDevice)
{
auto device = qobject_cast<LibInput::Device *>(inputDevice);