Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

[android] Xiaomi stylus button and curve fixes

Carsten Hartenfels950becdf8b3d
diff --git a/libs/global/CMakeLists.txt b/libs/global/CMakeLists.txt
index 4b75bfbd24..ff12944e4f 100644
--- a/libs/global/CMakeLists.txt
+++ b/libs/global/CMakeLists.txt
@@ -56,17 +56,17 @@ if(WIN32)
endif(WIN32)
if (ANDROID)
- set (kritaglobal_LIB_SRCS ${kritaglobal_LIB_SRCS} KisAndroidCrashHandler.cpp)
+ list(APPEND kritaglobal_LIB_SRCS
+ KisAndroidCrashHandler.cpp
+ KisAndroidLogHandler.cpp
+ KisAndroidUtils.cpp
+ )
include_directories(${unwindstack_INCLUDE_DIR})
if (QT_MAJOR_VERSION STREQUAL "5")
include_directories(${Qt5AndroidExtras_INCLUDE_DIRS})
endif()
endif()
-if (ANDROID)
- set (kritaglobal_LIB_SRCS ${kritaglobal_LIB_SRCS} KisAndroidLogHandler.cpp)
-endif()
-
kis_add_library(kritaglobal SHARED ${kritaglobal_LIB_SRCS}
)
diff --git a/libs/global/KisAndroidUtils.cpp b/libs/global/KisAndroidUtils.cpp
new file mode 100644
index 0000000000..8e7191afe4
--- /dev/null
+++ b/libs/global/KisAndroidUtils.cpp
@@ -0,0 +1,24 @@
+/*
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+#include "KisAndroidUtils.h"
+#include <QtAndroid>
+
+namespace KisAndroidUtils
+{
+
+bool looksLikeXiaomiDevice()
+{
+ // The device isn't going to change, so let's cache the slow JNI call.
+ static bool checked;
+ static bool result;
+ if (!checked) {
+ checked = true;
+ result = QAndroidJniObject::callStaticMethod<jboolean>("org/krita/android/MainActivity",
+ "looksLikeXiaomiDevice",
+ "()Z");
+ }
+ return result;
+}
+
+} // namespace KisAndroidUtils
diff --git a/libs/global/KisAndroidUtils.h b/libs/global/KisAndroidUtils.h
new file mode 100644
index 0000000000..de6bbda182
--- /dev/null
+++ b/libs/global/KisAndroidUtils.h
@@ -0,0 +1,20 @@
+/*
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+#ifndef __KISANDROIDUTILS_H_
+#define __KISANDROIDUTILS_H_
+
+#include <kritaglobal_export.h>
+
+namespace KisAndroidUtils
+{
+
+// Check whether we seem to be running on a Xiaomi device, which requires
+// enabling several workarounds by default. If we need additional workarounds
+// in the future, change this to return an enum or a flag set instead, depending
+// on what's actually needed.
+KRITAGLOBAL_EXPORT bool looksLikeXiaomiDevice();
+
+} // namespace KisAndroidUtils
+
+#endif // __KISANDROIDUTILS_H_
diff --git a/libs/global/kis_config_notifier.cpp b/libs/global/kis_config_notifier.cpp
index 67b0eb5260..4c970e4058 100644
--- a/libs/global/kis_config_notifier.cpp
+++ b/libs/global/kis_config_notifier.cpp
@@ -74,3 +74,15 @@ void KisConfigNotifier::notifyLongPressChanged(bool enabled)
{
Q_EMIT sigLongPressChanged(enabled);
}
+
+#ifdef Q_OS_ANDROID
+void KisConfigNotifier::notifyUsePageUpDownMouseButtonEmulationWorkaroundChanged(bool enabled)
+{
+ Q_EMIT sigUsePageUpDownMouseButtonEmulationWorkaroundChanged(enabled);
+}
+
+void KisConfigNotifier::notifyUseIgnoreHistoricTabletEventsWorkaroundChanged(bool enabled)
+{
+ Q_EMIT sigUseIgnoreHistoricTabletEventsWorkaroundChanged(enabled);
+}
+#endif
diff --git a/libs/global/kis_config_notifier.h b/libs/global/kis_config_notifier.h
index 39e5291c52..a7caa7b6f8 100644
--- a/libs/global/kis_config_notifier.h
+++ b/libs/global/kis_config_notifier.h
@@ -42,6 +42,11 @@ public:
void notifyColorThemeChanged(const QString &filename);
void notifyLongPressChanged(bool enabled);
+#ifdef Q_OS_ANDROID
+ void notifyUsePageUpDownMouseButtonEmulationWorkaroundChanged(bool enabled);
+ void notifyUseIgnoreHistoricTabletEventsWorkaroundChanged(bool enabled);
+#endif
+
Q_SIGNALS:
/**
* This signal is emitted whenever notifyConfigChanged() is called.
@@ -54,6 +59,11 @@ Q_SIGNALS:
void sigColorSamplerPreviewStyleChanged();
void signalColorThemeChanged(const QString &filename);
void sigLongPressChanged(bool enabled);
+#ifdef Q_OS_ANDROID
+ void sigUsePageUpDownMouseButtonEmulationWorkaroundChanged(bool enabled);
+ void sigUseIgnoreHistoricTabletEventsWorkaroundChanged(bool enabled);
+#endif
+
private:
KisConfigNotifier(const KisConfigNotifier&);
KisConfigNotifier operator=(const KisConfigNotifier&);
diff --git a/libs/ui/KisApplication.cpp b/libs/ui/KisApplication.cpp
index d1020f2e1d..4ea730e463 100644
--- a/libs/ui/KisApplication.cpp
+++ b/libs/ui/KisApplication.cpp
@@ -127,6 +127,7 @@
#include <input/KisExtendedModifiersMapperPluginInterface.h>
#include <KisPlatformPluginInterfaceFactory.h>
+#include <config-qt-patches-present.h>
#include <config-use-surface-color-management-api.h>
#if KRITA_USE_SURFACE_COLOR_MANAGEMENT_API
@@ -647,9 +648,39 @@ bool KisApplication::start(const KisApplicationArguments &args)
//the new syntax slot syntax allow to connect to a non q_object static method.
// Long-press emulation.
- connect(KisConfigNotifier::instance(), &KisConfigNotifier::sigLongPressChanged, this, &KisApplication::slotSetLongPress);
+ KisConfigNotifier *cfgNotifier = KisConfigNotifier::instance();
+ connect(cfgNotifier, &KisConfigNotifier::sigLongPressChanged, this, &KisApplication::slotSetLongPress);
slotSetLongPress(cfg.longPressEnabled());
+ // Xiaomi workaround: their stylus inexplicably inputs page up and down keys
+ // when pressing stylus buttons. This flag causes the Android platform
+ // integration to turn those into right and middle clicks instead.
+#if KRITA_QT_HAS_ANDROID_EMULATE_MOUSE_BUTTONS_FOR_PAGE_UP_DOWN
+ auto setPageUpDownMouseButtonEmulationWorkaround = [](bool enabled) {
+ QCoreApplication::setKritaAttribute(KRITA_QATTRIBUTE_ANDROID_EMULATE_MOUSE_BUTTONS_FOR_PAGE_UP_DOWN, enabled);
+ };
+ connect(cfgNotifier,
+ &KisConfigNotifier::sigUsePageUpDownMouseButtonEmulationWorkaroundChanged,
+ this,
+ setPageUpDownMouseButtonEmulationWorkaround);
+ setPageUpDownMouseButtonEmulationWorkaround(cfg.usePageUpDownMouseButtonEmulationWorkaround());
+#endif
+
+ // Xiaomi workaround: historic tablet motion events are garbage, they just
+ // connect the actual points that the tablet sampled with a straight line
+ // and no pressure emulation, leading to jagged curves that don't get
+ // smoothed out. This flag disables reading those historic events.
+#if KRITA_QT_HAS_ANDROID_IGNORE_HISTORIC_TABLET_EVENTS
+ auto setIgnoreHistoricTabletEventsWorkaround = [](bool enabled) {
+ QCoreApplication::setKritaAttribute(KRITA_QATTRIBUTE_ANDROID_IGNORE_HISTORIC_TABLET_EVENTS, enabled);
+ };
+ connect(cfgNotifier,
+ &KisConfigNotifier::sigUseIgnoreHistoricTabletEventsWorkaroundChanged,
+ this,
+ setIgnoreHistoricTabletEventsWorkaround);
+ setIgnoreHistoricTabletEventsWorkaround(cfg.usePageUpDownMouseButtonEmulationWorkaround());
+#endif
+
// Create a new image, if needed
if (doNewImage) {
KisDocument *doc = args.createDocumentFromArguments();
diff --git a/libs/ui/dialogs/kis_dlg_preferences.cc b/libs/ui/dialogs/kis_dlg_preferences.cc
index 4f210ebeef..b1aadc297d 100644
--- a/libs/ui/dialogs/kis_dlg_preferences.cc
+++ b/libs/ui/dialogs/kis_dlg_preferences.cc
@@ -1617,6 +1617,15 @@ void TabletSettingsTab::setDefault()
m_page->grpTabletApi->setVisible(false);
#endif
+#if KRITA_QT_HAS_ANDROID_EMULATE_MOUSE_BUTTONS_FOR_PAGE_UP_DOWN
+ m_page->chkUsePageUpDownMouseButtonEmulationWorkaround->setChecked(
+ cfg.usePageUpDownMouseButtonEmulationWorkaround(true));
+#endif
+
+#if KRITA_QT_HAS_ANDROID_IGNORE_HISTORIC_TABLET_EVENTS
+ m_page->chkUseIgnoreHistoricTabletEventsWorkaround->setChecked(cfg.useIgnoreHistoricTabletEventsWorkaround(true));
+#endif
+
m_page->chkUseTimestampsForBrushSpeed->setChecked(false);
m_page->intMaxAllowedBrushSpeed->setValue(30);
m_page->intBrushSpeedSmoothing->setValue(3);
@@ -1667,6 +1676,19 @@ TabletSettingsTab::TabletSettingsTab(QWidget* parent, const char* name): QWidget
#endif
m_page->chkUseTimestampsForBrushSpeed->setChecked(cfg.readEntry("useTimestampsForBrushSpeed", false));
+#if KRITA_QT_HAS_ANDROID_EMULATE_MOUSE_BUTTONS_FOR_PAGE_UP_DOWN
+ m_page->chkUsePageUpDownMouseButtonEmulationWorkaround->setChecked(
+ cfg.usePageUpDownMouseButtonEmulationWorkaround());
+#else
+ m_page->chkUsePageUpDownMouseButtonEmulationWorkaround->hide();
+#endif
+
+#if KRITA_QT_HAS_ANDROID_IGNORE_HISTORIC_TABLET_EVENTS
+ m_page->chkUseIgnoreHistoricTabletEventsWorkaround->setChecked(cfg.useIgnoreHistoricTabletEventsWorkaround());
+#else
+ m_page->chkUseIgnoreHistoricTabletEventsWorkaround->hide();
+#endif
+
m_page->intMaxAllowedBrushSpeed->setRange(1, 100);
m_page->intMaxAllowedBrushSpeed->setValue(cfg.readEntry("maxAllowedSpeedValue", 30));
KisSpinBoxI18nHelper::install(m_page->intMaxAllowedBrushSpeed, [](int value) {
@@ -2845,6 +2867,17 @@ bool KisDlgPreferences::editPreferences()
# endif
#endif
cfg.writeEntry<bool>("useTimestampsForBrushSpeed", m_tabletSettings->m_page->chkUseTimestampsForBrushSpeed->isChecked());
+
+#if KRITA_QT_HAS_ANDROID_EMULATE_MOUSE_BUTTONS_FOR_PAGE_UP_DOWN
+ cfg.setUsePageUpDownMouseButtonEmulationWorkaround(
+ m_tabletSettings->m_page->chkUsePageUpDownMouseButtonEmulationWorkaround->isChecked());
+#endif
+
+#if KRITA_QT_HAS_ANDROID_IGNORE_HISTORIC_TABLET_EVENTS
+ cfg.setUseIgnoreHistoricTabletEventsWorkaround(
+ m_tabletSettings->m_page->chkUseIgnoreHistoricTabletEventsWorkaround->isChecked());
+#endif
+
cfg.writeEntry<int>("maxAllowedSpeedValue", m_tabletSettings->m_page->intMaxAllowedBrushSpeed->value());
cfg.writeEntry<int>("speedValueSmoothing", m_tabletSettings->m_page->intBrushSpeedSmoothing->value());
// the angle is saved in clockwise direction to be consistent with Drawing Angle, so negate
diff --git a/libs/ui/forms/wdgtabletsettings.ui b/libs/ui/forms/wdgtabletsettings.ui
index 9a28d69c31..750bdb1462 100644
--- a/libs/ui/forms/wdgtabletsettings.ui
+++ b/libs/ui/forms/wdgtabletsettings.ui
@@ -188,6 +188,20 @@
</property>
</widget>
</item>
+ <item>
+ <widget class="QCheckBox" name="chkUsePageUpDownMouseButtonEmulationWorkaround">
+ <property name="text">
+ <string>Treat page up/down keys as right/middle mouse buttons (workaround for Xiaomi styluses)</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QCheckBox" name="chkUseIgnoreHistoricTabletEventsWorkaround">
+ <property name="text">
+ <string>Disregard tablet position history (workaround for jagged lines with Xiaomi styluses)</string>
+ </property>
+ </widget>
+ </item>
<item>
<widget class="KisSliderSpinBox" name="intMaxAllowedBrushSpeed" native="true">
<property name="sizePolicy">
diff --git a/libs/ui/kis_config.cc b/libs/ui/kis_config.cc
index 6767d39ac7..5f60467992 100644
--- a/libs/ui/kis_config.cc
+++ b/libs/ui/kis_config.cc
@@ -49,6 +49,10 @@
# include <QtGui/qpa/qplatformintegration.h>
#endif
+#ifdef Q_OS_ANDROID
+#include <KisAndroidUtils.h>
+#endif
+
KisConfig::KisConfig(bool readOnly)
: m_cfg( KSharedConfig::openConfig()->group(""))
, m_readOnly(readOnly)
@@ -1637,6 +1641,40 @@ void KisConfig::setUseRightMiddleTabletButtonWorkaround(bool value)
m_cfg.writeEntry("useRightMiddleTabletButtonWorkaround", value);
}
+#ifdef Q_OS_ANDROID
+bool KisConfig::usePageUpDownMouseButtonEmulationWorkaround(bool defaultValue) const
+{
+ bool fallback = KisAndroidUtils::looksLikeXiaomiDevice();
+ if (defaultValue) {
+ return fallback;
+ } else {
+ return m_cfg.readEntry("usePageUpDownMouseButtonEmulationWorkaround", fallback);
+ }
+}
+
+void KisConfig::setUsePageUpDownMouseButtonEmulationWorkaround(bool value)
+{
+ m_cfg.writeEntry("usePageUpDownMouseButtonEmulationWorkaround", value);
+ KisConfigNotifier::instance()->notifyUsePageUpDownMouseButtonEmulationWorkaroundChanged(value);
+}
+
+bool KisConfig::useIgnoreHistoricTabletEventsWorkaround(bool defaultValue) const
+{
+ bool fallback = KisAndroidUtils::looksLikeXiaomiDevice();
+ if (defaultValue) {
+ return fallback;
+ } else {
+ return m_cfg.readEntry("useIgnoreHistoricTabletEventsWorkaround", fallback);
+ }
+}
+
+void KisConfig::setUseIgnoreHistoricTabletEventsWorkaround(bool value)
+{
+ m_cfg.writeEntry("useIgnoreHistoricTabletEventsWorkaround", value);
+ KisConfigNotifier::instance()->notifyUseIgnoreHistoricTabletEventsWorkaroundChanged(value);
+}
+#endif
+
qreal KisConfig::vastScrolling(bool defaultValue) const
{
return (defaultValue ? 0.9 : m_cfg.readEntry("vastScrolling", 0.9));
diff --git a/libs/ui/kis_config.h b/libs/ui/kis_config.h
index 5752fbba64..b56ec180d0 100644
--- a/libs/ui/kis_config.h
+++ b/libs/ui/kis_config.h
@@ -416,6 +416,14 @@ public:
bool useRightMiddleTabletButtonWorkaround(bool defaultValue = false) const;
void setUseRightMiddleTabletButtonWorkaround(bool value);
+#ifdef Q_OS_ANDROID
+ bool usePageUpDownMouseButtonEmulationWorkaround(bool defaultValue = false) const;
+ void setUsePageUpDownMouseButtonEmulationWorkaround(bool value);
+
+ bool useIgnoreHistoricTabletEventsWorkaround(bool defaultValue = false) const;
+ void setUseIgnoreHistoricTabletEventsWorkaround(bool value);
+#endif
+
qreal vastScrolling(bool defaultValue = false) const;
void setVastScrolling(const qreal factor) const;
diff --git a/packaging/android/apk/src/org/krita/android/MainActivity.java b/packaging/android/apk/src/org/krita/android/MainActivity.java
index 6df9efc6a0..59321dc408 100644
--- a/packaging/android/apk/src/org/krita/android/MainActivity.java
+++ b/packaging/android/apk/src/org/krita/android/MainActivity.java
@@ -193,4 +193,12 @@ public class MainActivity extends QtActivity {
return 500;
}
}
+
+ public static boolean looksLikeXiaomiDevice() {
+ return containsXiaomi(Build.BRAND) || containsXiaomi(Build.MANUFACTURER);
+ }
+
+ private static boolean containsXiaomi(String s) {
+ return s != null && s.toLowerCase().contains("xiaomi");
+ }
}