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] Allow toggling cursor hiding workaround

Carsten Hartenfels6538f3f5ce7d
diff --git a/libs/global/KisAndroidUtils.cpp b/libs/global/KisAndroidUtils.cpp
index a0b9e89e7f..6e030d45ea 100644
--- a/libs/global/KisAndroidUtils.cpp
+++ b/libs/global/KisAndroidUtils.cpp
@@ -48,6 +48,40 @@ bool looksLikeXiaomiDevice()
return result;
}
+bool looksLikeBrokenCursorDevice()
+{
+ static bool checked;
+ static bool result;
+ if (!checked) {
+ checked = true;
+ result = QJniObject::callStaticMethod<jboolean>("org/krita/android/MainActivity",
+ "looksLikeBrokenCursorDevice",
+ "()Z");
+ }
+ return result;
+}
+
+bool canHideTouchAndStylusCursors()
+{
+ static bool checked;
+ static bool result;
+ if (!checked) {
+ checked = true;
+ result = QJniObject::callStaticMethod<jboolean>("org/krita/android/MainActivity",
+ "canHideTouchAndStylusCursors",
+ "()Z");
+ }
+ return result;
+}
+
+void setHideTouchAndStylusCursors(bool hide)
+{
+ QJniObject::callStaticMethod<void>("org/krita/android/MainActivity",
+ "setHideTouchAndStylusCursors",
+ "(Z)V",
+ jboolean(hide));
+}
+
bool isLowMemoryKillReportSupported()
{
// The support is device-bound and will never change, so cache the JNI call.
diff --git a/libs/global/KisAndroidUtils.h b/libs/global/KisAndroidUtils.h
index b90f08f4d4..11b786e2de 100644
--- a/libs/global/KisAndroidUtils.h
+++ b/libs/global/KisAndroidUtils.h
@@ -21,6 +21,21 @@ KRITAGLOBAL_EXPORT void performInitialSetup();
// on what's actually needed.
KRITAGLOBAL_EXPORT bool looksLikeXiaomiDevice();
+// Check whether we seem to be running a device with broken stylus cursors that
+// show up while hovering and then disappear when pressing them down, which is
+// immensely annoying and needs to be worked around. We consider this to be the
+// case for Wacom and XPPEN devices.
+KRITAGLOBAL_EXPORT bool looksLikeBrokenCursorDevice();
+
+// Checks whether the below setHideTouchAndStylusCursors is supported on this
+// patched version of Qt.
+KRITAGLOBAL_EXPORT bool canHideTouchAndStylusCursors();
+
+// Hides cursors on touch and stylus events made with internal devices. If the
+// method doesn't exist, such as when using an outdated patched Qt, this will
+// do nothing. Use the above canHideTouchAndStylusCursors to check in advance.
+KRITAGLOBAL_EXPORT void setHideTouchAndStylusCursors(bool hide);
+
// Check whether the device supports reporting low-memory situations as an exit
// reason. Devices that don't will instead report a SIGKILL.
KRITAGLOBAL_EXPORT bool isLowMemoryKillReportSupported();
diff --git a/libs/global/kis_config_notifier.cpp b/libs/global/kis_config_notifier.cpp
index ab38561815..eaaea28f51 100644
--- a/libs/global/kis_config_notifier.cpp
+++ b/libs/global/kis_config_notifier.cpp
@@ -90,4 +90,9 @@ void KisConfigNotifier::notifyUseIgnoreHistoricTabletEventsWorkaroundChanged(boo
{
Q_EMIT sigUseIgnoreHistoricTabletEventsWorkaroundChanged(enabled);
}
+
+void KisConfigNotifier::notifyHideTouchAndStylusCursorsWorkaroundChanged(bool enabled)
+{
+ Q_EMIT sigHideTouchAndStylusCursorsWorkaroundChanged(enabled);
+}
#endif
diff --git a/libs/global/kis_config_notifier.h b/libs/global/kis_config_notifier.h
index 0bc55c1256..e2cb557b97 100644
--- a/libs/global/kis_config_notifier.h
+++ b/libs/global/kis_config_notifier.h
@@ -46,6 +46,7 @@ public:
void notifyUsePageUpDownMouseButtonEmulationWorkaroundChanged(bool enabled);
void notifyUseHighFunctionKeyMouseButtonEmulationWorkaroundChanged(bool enabled);
void notifyUseIgnoreHistoricTabletEventsWorkaroundChanged(bool enabled);
+ void notifyHideTouchAndStylusCursorsWorkaroundChanged(bool enabled);
#endif
Q_SIGNALS:
@@ -64,6 +65,7 @@ Q_SIGNALS:
void sigUsePageUpDownMouseButtonEmulationWorkaroundChanged(bool enabled);
void sigUseHighFunctionKeyMouseButtonEmulationWorkaroundChanged(bool enabled);
void sigUseIgnoreHistoricTabletEventsWorkaroundChanged(bool enabled);
+ void sigHideTouchAndStylusCursorsWorkaroundChanged(bool enabled);
#endif
private:
diff --git a/libs/ui/KisApplication.cpp b/libs/ui/KisApplication.cpp
index f032391406..6eb368ce05 100644
--- a/libs/ui/KisApplication.cpp
+++ b/libs/ui/KisApplication.cpp
@@ -138,9 +138,12 @@
#endif /* KRITA_USE_SURFACE_COLOR_MANAGEMENT_API */
-#if defined(Q_OS_ANDROID) && KRITA_QT_HAS_ANDROID_QPLATFORMSCREEN_DENSITY_ADJUSTMENT
+#ifdef Q_OS_ANDROID
+#include <KisAndroidUtils.h>
+#if KRITA_QT_HAS_ANDROID_QPLATFORMSCREEN_DENSITY_ADJUSTMENT
#include <KisAndroidScaling.h>
#endif
+#endif
namespace {
const QTime appStartTime(QTime::currentTime());
@@ -700,6 +703,22 @@ bool KisApplication::start(const KisApplicationArguments &args)
setIgnoreHistoricTabletEventsWorkaround(cfg.useIgnoreHistoricTabletEventsWorkaround());
#endif
+ // Wacom and XPPEN workaround: these devices have broken cursors for stylus
+ // events, which will show up when hovering the stylus and disappear when
+ // pressing it down. Support for this is in Java code, so no #if.
+#ifdef Q_OS_ANDROID
+ if (KisAndroidUtils::canHideTouchAndStylusCursors()) {
+ auto setHideTouchAndStylusCursorsWorkaround = [](bool enabled) {
+ KisAndroidUtils::setHideTouchAndStylusCursors(enabled);
+ };
+ connect(cfgNotifier,
+ &KisConfigNotifier::sigHideTouchAndStylusCursorsWorkaroundChanged,
+ this,
+ setHideTouchAndStylusCursorsWorkaround);
+ setHideTouchAndStylusCursorsWorkaround(cfg.hideTouchAndStylusCursorsWorkaround());
+ }
+#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 9129552e12..bf593c695e 100644
--- a/libs/ui/dialogs/kis_dlg_preferences.cc
+++ b/libs/ui/dialogs/kis_dlg_preferences.cc
@@ -122,6 +122,10 @@
#include "KisWindowsPackageUtils.h"
#endif
+#ifdef Q_OS_ANDROID
+#include <KisAndroidUtils.h>
+#endif
+
/**
* HACK ALERT: this is a function from a private Qt's header qfont_p.h,
* we don't include the whole header, because it is painful in the
@@ -1799,6 +1803,12 @@ void TabletSettingsTab::setDefault()
m_page->chkUseIgnoreHistoricTabletEventsWorkaround->setChecked(cfg.useIgnoreHistoricTabletEventsWorkaround(true));
#endif
+#ifdef Q_OS_ANDROID
+ if (KisAndroidUtils::canHideTouchAndStylusCursors()) {
+ m_page->chkHideTouchAndStylusCursorsWorkaround->setChecked(cfg.hideTouchAndStylusCursorsWorkaround(true));
+ }
+#endif
+
m_page->chkUseTimestampsForBrushSpeed->setChecked(false);
m_page->intMaxAllowedBrushSpeed->setValue(30);
m_page->intBrushSpeedSmoothing->setValue(3);
@@ -1869,6 +1879,16 @@ TabletSettingsTab::TabletSettingsTab(QWidget* parent, const char* name): QWidget
m_page->chkUseIgnoreHistoricTabletEventsWorkaround->hide();
#endif
+#ifdef Q_OS_ANDROID
+ if (KisAndroidUtils::canHideTouchAndStylusCursors()) {
+ m_page->chkHideTouchAndStylusCursorsWorkaround->setChecked(cfg.hideTouchAndStylusCursorsWorkaround());
+ } else {
+ m_page->chkHideTouchAndStylusCursorsWorkaround->hide();
+ }
+#else
+ m_page->chkHideTouchAndStylusCursorsWorkaround->hide();
+#endif
+
m_page->intMaxAllowedBrushSpeed->setRange(1, 100);
m_page->intMaxAllowedBrushSpeed->setValue(cfg.readEntry("maxAllowedSpeedValue", 30));
KisSpinBoxI18nHelper::install(m_page->intMaxAllowedBrushSpeed, [](int value) {
@@ -3244,6 +3264,13 @@ bool KisDlgPreferences::editPreferences(std::optional<PageDesc>page)
m_tabletSettings->m_page->chkUseIgnoreHistoricTabletEventsWorkaround->isChecked());
#endif
+#ifdef Q_OS_ANDROID
+ if (KisAndroidUtils::canHideTouchAndStylusCursors()) {
+ cfg.setHideTouchAndStylusCursorsWorkaround(
+ m_tabletSettings->m_page->chkHideTouchAndStylusCursorsWorkaround->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 eff5ff58ae..3af2e5435c 100644
--- a/libs/ui/forms/wdgtabletsettings.ui
+++ b/libs/ui/forms/wdgtabletsettings.ui
@@ -209,6 +209,13 @@
</property>
</widget>
</item>
+ <item>
+ <widget class="QCheckBox" name="chkHideTouchAndStylusCursorsWorkaround">
+ <property name="text">
+ <string>Hide stylus cursor (workaround for Wacom and XP-PEN devices)</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 7f949c7a89..fc9a68634f 100644
--- a/libs/ui/kis_config.cc
+++ b/libs/ui/kis_config.cc
@@ -1732,6 +1732,22 @@ void KisConfig::setUseIgnoreHistoricTabletEventsWorkaround(bool value)
KisConfigNotifier::instance()->notifyUseIgnoreHistoricTabletEventsWorkaroundChanged(value);
}
+bool KisConfig::hideTouchAndStylusCursorsWorkaround(bool defaultValue) const
+{
+ bool fallback = KisAndroidUtils::looksLikeBrokenCursorDevice();
+ if (defaultValue) {
+ return fallback;
+ } else {
+ return m_cfg.readEntry("hideTouchAndStylusCursorsWorkaround", fallback);
+ }
+}
+
+void KisConfig::setHideTouchAndStylusCursorsWorkaround(bool value)
+{
+ m_cfg.writeEntry("hideTouchAndStylusCursorsWorkaround", value);
+ KisConfigNotifier::instance()->notifyHideTouchAndStylusCursorsWorkaroundChanged(value);
+}
+
qreal KisConfig::androidScalingLastInitialScale(bool defaultValue) const
{
if (defaultValue) {
diff --git a/libs/ui/kis_config.h b/libs/ui/kis_config.h
index 5cb41b24c8..fbe1259510 100644
--- a/libs/ui/kis_config.h
+++ b/libs/ui/kis_config.h
@@ -460,6 +460,9 @@ public:
bool useIgnoreHistoricTabletEventsWorkaround(bool defaultValue = false) const;
void setUseIgnoreHistoricTabletEventsWorkaround(bool value);
+ bool hideTouchAndStylusCursorsWorkaround(bool defaultValue = false) const;
+ void setHideTouchAndStylusCursorsWorkaround(bool value);
+
qreal androidScalingLastInitialScale(bool defaultValue = false) const;
void setAndroidScalingLastInitialScale(qreal value);
diff --git a/packaging/android/apk/src/org/krita/android/MainActivity.java b/packaging/android/apk/src/org/krita/android/MainActivity.java
index 093be86d51..1675ac90a1 100644
--- a/packaging/android/apk/src/org/krita/android/MainActivity.java
+++ b/packaging/android/apk/src/org/krita/android/MainActivity.java
@@ -29,6 +29,8 @@ import android.widget.Toast;
import androidx.annotation.RequiresApi;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.List;
import org.krita.R;
@@ -230,11 +232,40 @@ public class MainActivity extends QtActivity {
}
public static boolean looksLikeXiaomiDevice() {
- return containsXiaomi(Build.BRAND) || containsXiaomi(Build.MANUFACTURER);
+ return looksLikeBrandDevice("xiaomi");
}
- private static boolean containsXiaomi(String s) {
- return s != null && s.toLowerCase().contains("xiaomi");
+ public static boolean looksLikeBrokenCursorDevice() {
+ return looksLikeBrandDevice("wacom") || looksLikeBrandDevice("xppen");
+ }
+
+ private static boolean looksLikeBrandDevice(String brand) {
+ return containsBrand(brand, Build.BRAND) || containsBrand(brand, Build.MANUFACTURER);
+ }
+
+ private static boolean containsBrand(String brand, String s) {
+ return s != null && s.toLowerCase().contains(brand);
+ }
+
+ public static boolean canHideTouchAndStylusCursors() {
+ try {
+ Class<?> clazz = Class.forName("org.qtproject.qt5.android.QtSurface");
+ Method method = clazz.getDeclaredMethod("setHideTouchAndStylusCursors", boolean.class);
+ return Modifier.isStatic(method.getModifiers());
+ } catch (Exception e) {
+ Log.e(TAG, "Exception looking for setHideTouchAndStylusCursors", e);
+ return false;
+ }
+ }
+
+ public static void setHideTouchAndStylusCursors(boolean hide) {
+ try {
+ Class<?> clazz = Class.forName("org.qtproject.qt5.android.QtSurface");
+ Method method = clazz.getDeclaredMethod("setHideTouchAndStylusCursors", boolean.class);
+ method.invoke(null, hide);
+ } catch (Exception e) {
+ Log.e(TAG, "Exception calling setHideTouchAndStylusCursors", e);
+ }
}
public ApplicationExitInfo getLastApplicationExitInfo() {