libinput/device: Use button mapping that is used in input handling for supported buttons

Fixes and prevents inconsistencies between those.
This commit is contained in:
David Redondo 2022-04-28 12:09:48 +02:00
parent 938b8c87a1
commit 80d28499e1
8 changed files with 81 additions and 63 deletions

View file

@ -1,7 +1,7 @@
include_directories(${Libinput_INCLUDE_DIRS}) include_directories(${Libinput_INCLUDE_DIRS})
add_definitions(-DKWIN_BUILD_TESTING) add_definitions(-DKWIN_BUILD_TESTING)
add_library(LibInputTestObjects STATIC ../../src/backends/libinput/device.cpp ../../src/backends/libinput/events.cpp ../../src/inputdevice.cpp mock_libinput.cpp) add_library(LibInputTestObjects STATIC ../../src/backends/libinput/device.cpp ../../src/backends/libinput/events.cpp ../../src/inputdevice.cpp ../../src/mousebuttons.cpp mock_libinput.cpp)
target_link_libraries(LibInputTestObjects Qt::Test Qt::Widgets Qt::DBus Qt::Gui KF5::ConfigCore) target_link_libraries(LibInputTestObjects Qt::Test Qt::Widgets Qt::DBus Qt::Gui KF5::ConfigCore)
target_include_directories(LibInputTestObjects PUBLIC ${CMAKE_SOURCE_DIR}/src) target_include_directories(LibInputTestObjects PUBLIC ${CMAKE_SOURCE_DIR}/src)

View file

@ -82,6 +82,7 @@ target_sources(kwin PRIVATE
linux_dmabuf.cpp linux_dmabuf.cpp
main.cpp main.cpp
modifier_only_shortcuts.cpp modifier_only_shortcuts.cpp
mousebuttons.cpp
moving_client_x11_filter.cpp moving_client_x11_filter.cpp
netinfo.cpp netinfo.cpp
onscreennotification.cpp onscreennotification.cpp

View file

@ -12,8 +12,10 @@
#include "libinput_logging.h" #include "libinput_logging.h"
#include "main.h" #include "main.h"
#include "mousebuttons.h"
#include "output.h" #include "output.h"
#include "platform.h" #include "platform.h"
#include "pointer_input.h"
#include <QDBusArgument> #include <QDBusArgument>
#include <QDBusConnection> #include <QDBusConnection>
@ -333,31 +335,14 @@ Device::Device(libinput_device *device, QObject *parent)
m_size = QSizeF(width, height); m_size = QSizeF(width, height);
} }
if (m_pointer) { if (m_pointer) {
if (libinput_device_pointer_has_button(m_device, BTN_LEFT) == 1) { // 0x120 is the first joystick Button
m_supportedButtons |= Qt::LeftButton; for (int button = BTN_LEFT; button < 0x120; ++button) {
} if (libinput_device_pointer_has_button(m_device, button)) {
if (libinput_device_pointer_has_button(m_device, BTN_MIDDLE) == 1) { m_supportedButtons |= buttonToQtMouseButton(button);
m_supportedButtons |= Qt::MiddleButton; }
}
if (libinput_device_pointer_has_button(m_device, BTN_RIGHT) == 1) {
m_supportedButtons |= Qt::RightButton;
}
if (libinput_device_pointer_has_button(m_device, BTN_SIDE) == 1) {
m_supportedButtons |= Qt::ExtraButton1;
}
if (libinput_device_pointer_has_button(m_device, BTN_EXTRA) == 1) {
m_supportedButtons |= Qt::ExtraButton2;
}
if (libinput_device_pointer_has_button(m_device, BTN_BACK) == 1) {
m_supportedButtons |= Qt::BackButton;
}
if (libinput_device_pointer_has_button(m_device, BTN_FORWARD) == 1) {
m_supportedButtons |= Qt::ForwardButton;
}
if (libinput_device_pointer_has_button(m_device, BTN_TASK) == 1) {
m_supportedButtons |= Qt::TaskButton;
} }
} }
if (m_keyboard) { if (m_keyboard) {
m_alphaNumericKeyboard = checkAlphaNumericKeyboard(m_device); m_alphaNumericKeyboard = checkAlphaNumericKeyboard(m_device);
} }

View file

@ -24,6 +24,7 @@
#include "inputmethod.h" #include "inputmethod.h"
#include "keyboard_input.h" #include "keyboard_input.h"
#include "main.h" #include "main.h"
#include "mousebuttons.h"
#include "pointer_input.h" #include "pointer_input.h"
#include "session.h" #include "session.h"
#include "tablet_input.h" #include "tablet_input.h"

52
src/mousebuttons.cpp Normal file
View file

@ -0,0 +1,52 @@
/*
SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "mousebuttons.h"
#include <QHash>
#include <linux/input-event-codes.h>
namespace KWin
{
static const QHash<uint32_t, Qt::MouseButton> s_buttonToQtMouseButton = {
{BTN_LEFT, Qt::LeftButton},
{BTN_MIDDLE, Qt::MiddleButton},
{BTN_RIGHT, Qt::RightButton},
// in QtWayland mapped like that
{BTN_SIDE, Qt::ExtraButton1},
// in QtWayland mapped like that
{BTN_EXTRA, Qt::ExtraButton2},
{BTN_BACK, Qt::BackButton},
{BTN_FORWARD, Qt::ForwardButton},
{BTN_TASK, Qt::TaskButton},
// mapped like that in QtWayland
{0x118, Qt::ExtraButton6},
{0x119, Qt::ExtraButton7},
{0x11a, Qt::ExtraButton8},
{0x11b, Qt::ExtraButton9},
{0x11c, Qt::ExtraButton10},
{0x11d, Qt::ExtraButton11},
{0x11e, Qt::ExtraButton12},
{0x11f, Qt::ExtraButton13},
};
uint32_t qtMouseButtonToButton(Qt::MouseButton button)
{
return s_buttonToQtMouseButton.key(button);
}
Qt::MouseButton buttonToQtMouseButton(uint32_t button)
{
// all other values get mapped to ExtraButton24
// this is actually incorrect but doesn't matter in our usage
// KWin internally doesn't use these high extra buttons anyway
// it's only needed for recognizing whether buttons are pressed
// if multiple buttons are mapped to the value the evaluation whether
// buttons are pressed is correct and that's all we care about.
return s_buttonToQtMouseButton.value(button, Qt::ExtraButton24);
}
}

17
src/mousebuttons.h Normal file
View file

@ -0,0 +1,17 @@
/*
SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#pragma once
#include <qnamespace.h>
namespace KWin
{
uint32_t qtMouseButtonToButton(Qt::MouseButton button);
Qt::MouseButton buttonToQtMouseButton(uint32_t button);
}

View file

@ -16,6 +16,7 @@
#include "effects.h" #include "effects.h"
#include "input_event.h" #include "input_event.h"
#include "input_event_spy.h" #include "input_event_spy.h"
#include "mousebuttons.h"
#include "osd.h" #include "osd.h"
#include "output.h" #include "output.h"
#include "platform.h" #include "platform.h"
@ -50,44 +51,6 @@
namespace KWin namespace KWin
{ {
static const QHash<uint32_t, Qt::MouseButton> s_buttonToQtMouseButton = {
{BTN_LEFT, Qt::LeftButton},
{BTN_MIDDLE, Qt::MiddleButton},
{BTN_RIGHT, Qt::RightButton},
// in QtWayland mapped like that
{BTN_SIDE, Qt::ExtraButton1},
// in QtWayland mapped like that
{BTN_EXTRA, Qt::ExtraButton2},
{BTN_BACK, Qt::BackButton},
{BTN_FORWARD, Qt::ForwardButton},
{BTN_TASK, Qt::TaskButton},
// mapped like that in QtWayland
{0x118, Qt::ExtraButton6},
{0x119, Qt::ExtraButton7},
{0x11a, Qt::ExtraButton8},
{0x11b, Qt::ExtraButton9},
{0x11c, Qt::ExtraButton10},
{0x11d, Qt::ExtraButton11},
{0x11e, Qt::ExtraButton12},
{0x11f, Qt::ExtraButton13},
};
uint32_t qtMouseButtonToButton(Qt::MouseButton button)
{
return s_buttonToQtMouseButton.key(button);
}
static Qt::MouseButton buttonToQtMouseButton(uint32_t button)
{
// all other values get mapped to ExtraButton24
// this is actually incorrect but doesn't matter in our usage
// KWin internally doesn't use these high extra buttons anyway
// it's only needed for recognizing whether buttons are pressed
// if multiple buttons are mapped to the value the evaluation whether
// buttons are pressed is correct and that's all we care about.
return s_buttonToQtMouseButton.value(button, Qt::ExtraButton24);
}
static bool screenContainsPos(const QPointF &pos) static bool screenContainsPos(const QPointF &pos)
{ {
const auto outputs = workspace()->outputs(); const auto outputs = workspace()->outputs();

View file

@ -40,7 +40,6 @@ namespace Decoration
class DecoratedClientImpl; class DecoratedClientImpl;
} }
uint32_t qtMouseButtonToButton(Qt::MouseButton button);
class KWIN_EXPORT PointerInputRedirection : public InputDeviceHandler class KWIN_EXPORT PointerInputRedirection : public InputDeviceHandler
{ {