diff --git a/autotests/libinput/CMakeLists.txt b/autotests/libinput/CMakeLists.txt index 5a1fb6e1bb..d1156fd532 100644 --- a/autotests/libinput/CMakeLists.txt +++ b/autotests/libinput/CMakeLists.txt @@ -1,7 +1,7 @@ include_directories(${Libinput_INCLUDE_DIRS}) 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_include_directories(LibInputTestObjects PUBLIC ${CMAKE_SOURCE_DIR}/src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f99b90255f..09c9607ee5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -82,6 +82,7 @@ target_sources(kwin PRIVATE linux_dmabuf.cpp main.cpp modifier_only_shortcuts.cpp + mousebuttons.cpp moving_client_x11_filter.cpp netinfo.cpp onscreennotification.cpp diff --git a/src/backends/libinput/device.cpp b/src/backends/libinput/device.cpp index e503091825..139c6836ab 100644 --- a/src/backends/libinput/device.cpp +++ b/src/backends/libinput/device.cpp @@ -12,8 +12,10 @@ #include "libinput_logging.h" #include "main.h" +#include "mousebuttons.h" #include "output.h" #include "platform.h" +#include "pointer_input.h" #include #include @@ -333,31 +335,14 @@ Device::Device(libinput_device *device, QObject *parent) m_size = QSizeF(width, height); } if (m_pointer) { - if (libinput_device_pointer_has_button(m_device, BTN_LEFT) == 1) { - m_supportedButtons |= Qt::LeftButton; - } - if (libinput_device_pointer_has_button(m_device, BTN_MIDDLE) == 1) { - 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; + // 0x120 is the first joystick Button + for (int button = BTN_LEFT; button < 0x120; ++button) { + if (libinput_device_pointer_has_button(m_device, button)) { + m_supportedButtons |= buttonToQtMouseButton(button); + } } } + if (m_keyboard) { m_alphaNumericKeyboard = checkAlphaNumericKeyboard(m_device); } diff --git a/src/input.cpp b/src/input.cpp index 6eb8cf44e0..4db26d6ccd 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -24,6 +24,7 @@ #include "inputmethod.h" #include "keyboard_input.h" #include "main.h" +#include "mousebuttons.h" #include "pointer_input.h" #include "session.h" #include "tablet_input.h" diff --git a/src/mousebuttons.cpp b/src/mousebuttons.cpp new file mode 100644 index 0000000000..bb2d1d8a1c --- /dev/null +++ b/src/mousebuttons.cpp @@ -0,0 +1,52 @@ +/* + SPDX-FileCopyrightText: 2022 David Redondo + + SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ + +#include "mousebuttons.h" +#include +#include + +namespace KWin +{ + +static const QHash 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); +} + +} diff --git a/src/mousebuttons.h b/src/mousebuttons.h new file mode 100644 index 0000000000..8f77b44032 --- /dev/null +++ b/src/mousebuttons.h @@ -0,0 +1,17 @@ +/* + SPDX-FileCopyrightText: 2022 David Redondo + + SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL +*/ + +#pragma once + +#include + +namespace KWin +{ + +uint32_t qtMouseButtonToButton(Qt::MouseButton button); +Qt::MouseButton buttonToQtMouseButton(uint32_t button); + +} diff --git a/src/pointer_input.cpp b/src/pointer_input.cpp index 3bc3a5f441..666773c14b 100644 --- a/src/pointer_input.cpp +++ b/src/pointer_input.cpp @@ -16,6 +16,7 @@ #include "effects.h" #include "input_event.h" #include "input_event_spy.h" +#include "mousebuttons.h" #include "osd.h" #include "output.h" #include "platform.h" @@ -50,44 +51,6 @@ namespace KWin { -static const QHash 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) { const auto outputs = workspace()->outputs(); diff --git a/src/pointer_input.h b/src/pointer_input.h index 8b015bbe05..d4be6f5897 100644 --- a/src/pointer_input.h +++ b/src/pointer_input.h @@ -40,7 +40,6 @@ namespace Decoration class DecoratedClientImpl; } -uint32_t qtMouseButtonToButton(Qt::MouseButton button); class KWIN_EXPORT PointerInputRedirection : public InputDeviceHandler {