Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

Tablets: hotplug, Wintab pressure, no view jumps, Alt+click-only Part switching (Wacom Intuos 3 report)

islandmed54d5560c8eea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f29c9447..67066e67 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,15 @@ All notable changes to SculptForge are listed here. The format follows
## [Unreleased]
+### Fixed
+- Tablets (a Wacom Intuos 3 report): a tablet plugged in after launch works without a restart ("Tablet connected" /
+ "Tablet disconnected" in the status bar), also through Wacom Bridge; on Windows, pressure now also comes from the
+ Wacom driver with "Use Windows Ink" off (Wintab); a pen without pressure draws at full strength instead of not at
+ all, and Preferences > Input and > Tablet say "Pressure: on" or "not detected" with a troubleshooting link. The
+ view no longer jumps when the pen comes back into range elsewhere or leaves mid-drag. The active Part changes only
+ with the keyboard's Alt and a quick click (not with a pen button set to Alt, a long pen press or quick pen taps),
+ and the status bar says "Active Part: <name> (Alt+click)".
+
## [0.10.7] - 2026-11-01
### Changed
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a54b9989..01ec668a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -323,6 +323,7 @@ if(SCULPTFORGE_BUILD_APP)
src/app/PanelTimeline.cpp
src/app/ProofMode.cpp
src/app/Tablet.cpp
+ src/app/Wintab.cpp
src/app/SpaceMouse.cpp
src/app/Input.cpp
src/app/RadialMenu.cpp
diff --git a/docs/guide/troubleshooting.md b/docs/guide/troubleshooting.md
index e6582e9c..2110aac2 100644
--- a/docs/guide/troubleshooting.md
+++ b/docs/guide/troubleshooting.md
@@ -67,15 +67,22 @@ and similar tablets, and pen displays, work without settings.
3. A custom brush can also set its own pressure response (**pressureCurve** in its recipe, see
[Brush Lab and sharing brushes](brush-lab.md)).
+**Preferences > Input** and **Preferences > Tablet** show "Pressure: on" once the pen has pressed, or
+"Pressure: not detected". Without pressure the pen still draws, at full strength. A tablet plugged in while
+SculptForge runs works straight away; the status bar says "Tablet connected" (also through Wacom Bridge).
+
If no pen is detected or pressure doesn't change:
-- Install or update the tablet maker's driver, then restart SculptForge.
-- **Windows:** most drivers have a **Windows Ink** option. If pressure doesn't reach SculptForge, try
- switching it (on or off) in the tablet driver's settings for SculptForge or for all applications, then
- restart SculptForge.
+- Install or update the tablet maker's driver.
+- **Windows:** SculptForge reads pressure through Windows Ink, and through the driver's Wintab when Windows Ink is
+ off (Wacom Tablet Properties > Mapping > **Use Windows Ink**). If pressure is still missing, switch that option
+ and bring the pen back to the tablet.
- **Linux:** check that the system sees the pen as a tablet (for example in your desktop's tablet settings).
- If the cursor moves but strokes have no pressure, test in another application to see whether the driver
reports pressure at all.
+- **The active Part changed by itself:** a Part becomes active with **Alt+click** on it (the keyboard's Alt, a
+ quick click), a triple-click with the mouse or a click in the Parts list; the status bar names it and how. A
+ tablet's ExpressKey set to Alt counts as the keyboard's Alt.
- **The cursor jumps back when you put the pen down elsewhere:** the tablet driver is set to mouse (relative)
mode. Turn on **Preferences > Tablet > Mouse mode (relative)**.
diff --git a/docs/guide/whats-new.md b/docs/guide/whats-new.md
index 83a50cb8..f7214aae 100644
--- a/docs/guide/whats-new.md
+++ b/docs/guide/whats-new.md
@@ -5,6 +5,14 @@ chapter opens by itself the first time you start a new version; you can always f
**Help > What's New**. Every detail is in the
[changelog](https://github.com/islandmed/SculptForge/blob/main/CHANGELOG.md).
+## Version 0.10.8
+
+- **Tablets**: plug a tablet in while SculptForge runs and it just works ("Tablet connected" in the status bar),
+ also through Wacom Bridge. On Windows, pressure works with the Wacom driver's **Use Windows Ink** on or off.
+ **Preferences > Input** shows whether pressure is detected, with a troubleshooting link. The view no longer jumps
+ when the pen comes back into range, and the active Part changes only with **Alt+click** on the keyboard; the
+ status bar names the new Part. See [Troubleshooting](troubleshooting.md#pen-tablets).
+
## Version 0.10.7
- **A clearer membership card**: the three memberships side by side with what each one gets you, in your look's
diff --git a/docs/tablet.md b/docs/tablet.md
index c8297490..3e1ab313 100644
--- a/docs/tablet.md
+++ b/docs/tablet.md
@@ -49,9 +49,11 @@ inverts.
## Setup by system
-**Windows**: SculptForge uses Windows Ink. Wacom: Wacom Tablet Properties > Grip Pen > Mapping, keep **Use
-Windows Ink** checked (without it Windows sees only a mouse, without pressure). XP-Pen and Huion drivers have a
-"Windows Ink" option that must be on as well. Surface pens work as they are. If strokes start late or the
+**Windows**: SculptForge uses Windows Ink, and the driver's Wintab when Windows Ink is off (Wacom Tablet
+Properties > Grip Pen > Mapping > **Use Windows Ink**): then the driver moves the cursor and SculptForge reads only
+the pressure, tip and eraser from Wintab32.dll (loaded from System32). Windows Ink is preferred while it sends pen
+events. XP-Pen and Huion drivers have a "Windows Ink" option too. Wacom Bridge (a tablet on a remote desktop)
+works the same way. Surface pens work as they are. If strokes start late or the
cursor shows a ripple, turn off "Press and hold for right-click" and the pen feedback in Windows Settings >
Bluetooth & devices > Pen & Windows Ink.
@@ -64,6 +66,14 @@ a screen and set the pen buttons; on X11 the xf86-input-wacom driver and `xsetwa
that pressure arrives at all, run `libinput debug-events` and look for `TABLET_TOOL_AXIS` lines with a
`pressure` value.
+## Hot-plugging
+
+A tablet plugged in while SculptForge runs works at once: SDL finds a new pen at its first proximity, and the
+tablet's mouse device (SDL_EVENT_MOUSE_ADDED / _REMOVED) says when it came and went; the status bar shows "Tablet
+connected" / "Tablet disconnected". Each new pen starts afresh (its name, whether it has pressure); unplugging
+releases the buttons it held, so no Alt stays down. On Windows the Wintab context is opened again when a device
+is added. The logic is `TabletHotplug` in `core/Tablet.h`.
+
## Troubleshooting
- **No pressure** (the meter stays at 1.00 or never moves): the system doesn't pass pressure on. On Windows
@@ -89,8 +99,9 @@ that pressure arrives at all, run `libinput debug-events` and look for `TABLET_T
The settings and the curve are in `src/core/Tablet.h` (unit tests: `tests/test_tablet.cpp`); the pen event
handling and the preferences panel in `src/app/Tablet.cpp`. UI tests drive a simulated pen with real SDL pen
events plus the mouse events SDL synthesizes from them: `pen_stroke`, `pen_drag_item`, `pen_button`,
-`pen_hover`, `pen_enter`, `pen_drag`, `pen_pressure` and `pen_out` (documented in `src/app/UiTest.cpp`); `tests/ui/tablet.sfui`
+`pen_hover`, `pen_enter`, `pen_drag`, `pen_pressure`, `pen_out`, `pen_new`, `tablet_plug` and `tablet_unplug` (documented in `src/app/UiTest.cpp`); `tests/ui/tablet.sfui`
covers pressure, the curve, Pressure > Size, the eraser, side buttons, leaving proximity, mouse mode and the panel, and
`tests/ui/pen_navigation.sfui` view drags with the pen tip, with and without a driver's stray mouse events, and
`tests/ui/pen_windows_ink.sfui` pen taps on menus, the Move button and the S slider with Windows Ink's stray
-mouse events.
+mouse events, and `tests/ui/tablet_hotplug.sfui` plugging in and out, a pen without pressure, the pen leaving and
+coming back mid-drag, side buttons, the eraser and Alt+click on a Part.
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 08c6194c..f55f8f2c 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -132,6 +132,7 @@ bool App::init(const AppOptions& options) {
SDL_Log("SDL_CreateWindow failed: %s", SDL_GetError());
return false;
}
+ wintab_.open(window_); // Windows: pressure from a tablet driver without Windows Ink (Wintab.h)
if (rememberWindow_) { // #35: where and how big it was last time
if (saved.placed) SDL_SetWindowPosition(window_, saved.x, saved.y);
#if defined(__APPLE__)
@@ -1134,6 +1135,7 @@ int App::run() {
void App::handleEvent(const SDL_Event& event) {
if (dropPenMouseEvent(event)) return; // pen side buttons are mapped in Tablet.cpp, not left to SDL
if (strayMouseMotion(event)) return; // neither the canvas nor the UI follows a driver's stray mouse events
+ wintabEvent(event); // Windows Ink off: the tablet driver's pressure for its mouse events (Tablet.cpp)
SDL_Event e = event;
mapPenPosition(e); // a tablet in mouse mode moves the cursor relatively (#54), for the canvas and the UI
if (supportCardEvent(e)) return; // the support card at launch takes clicks and keys while open
@@ -1168,6 +1170,10 @@ void App::handleEvent(const SDL_Event& event) {
case SDL_EVENT_PEN_BUTTON_UP:
handlePenEvent(e);
break;
+ case SDL_EVENT_MOUSE_ADDED:
+ case SDL_EVENT_MOUSE_REMOVED:
+ tabletDeviceEvent(e); // a tablet plugged in or out (Tablet.cpp)
+ break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
lastInputPen_ = e.button.which == SDL_PEN_MOUSEID;
// Strokes and navigation start only on the canvas, never on the shell around it.
@@ -1349,19 +1355,24 @@ void App::onMouseDown(float x, float y, Uint8 button) {
if (captureMouseDown(x, y)) return; // Capture from view: the drag draws the square (PanelKelvinlet.cpp)
// A triple-click on another SubTool makes it the active one and frames it (#43). (Its first two clicks
// don't sculpt it: brushes work on the active SubTool only.)
- if (mouseClicks_ >= 3 && !(mod & (SDL_KMOD_SHIFT | SDL_KMOD_CTRL | SDL_KMOD_ALT | SDL_KMOD_GUI)) &&
+ // Not from a pen: quick taps of a stippling pen counted as a triple-click and switched the Part.
+ if (mouseClicks_ >= 3 && !lastInputPen_ && !wintabPen_ && !(mod & (SDL_KMOD_SHIFT | SDL_KMOD_CTRL | SDL_KMOD_ALT | SDL_KMOD_GUI)) &&
insertType_.empty() && doc_.subtoolCount() > 1) {
const int target = pickSubtool(x, y);
if (target >= 0 && target != doc_.activeSubtool) {
runCommand("subtool_select", {{"index", double(target)}});
+ partSelected("triple-click");
frameActiveSubtool();
return;
}
}
// Alt+click on another SubTool selects it (ZBrush); Alt+drag stays an inverted stroke or a pan.
// Shift+Alt+click selects and frames it (ZBrush), the active one too.
- const bool frameClick = (mod & SDL_KMOD_ALT) && (mod & SDL_KMOD_SHIFT) && !ctrlDown(mod);
- if (!blenderStyle() && (mod & SDL_KMOD_ALT) && !ctrlDown(mod) && (frameClick || !(mod & SDL_KMOD_SHIFT)) &&
+ // Only the keyboard's Alt: a pen button set to Invert (Alt) is for subtracting, and a tap with it held
+ // switched the Part without the user knowing why.
+ const SDL_Keymod keys = keyMods();
+ const bool frameClick = (keys & SDL_KMOD_ALT) && (mod & SDL_KMOD_SHIFT) && !ctrlDown(mod);
+ if (!blenderStyle() && (keys & SDL_KMOD_ALT) && !ctrlDown(mod) && (frameClick || !(mod & SDL_KMOD_SHIFT)) &&
(doc_.subtoolCount() > 1 || frameClick)) {
const int target = pickSubtool(x, y);
subtoolClickFrame_ = frameClick;
@@ -1370,6 +1381,7 @@ void App::onMouseDown(float x, float y, Uint8 button) {
subtoolClickTarget_ = target;
subtoolClickX_ = x;
subtoolClickY_ = y;
+ subtoolClickNs_ = clickClockNs();
return;
}
}
@@ -1446,6 +1458,10 @@ void App::onMouseMove(float x, float y, float dx, float dy, bool fromPen) {
// X11, #7) each pen event's xrel jumped back from the stray position and the view shook instead of turning.
dx = x - mouseX_;
dy = y - mouseY_;
+ // The pen's first position after coming into range, or after the mouse moved the cursor, is a jump from
+ // wherever the cursor was, not a motion: it must not throw a drag (the view) across the screen.
+ if (penJump_ || !lastInputPen_) dx = dy = 0.0f;
+ penJump_ = false;
}
mouseX_ = x;
mouseY_ = y;
diff --git a/src/app/App.h b/src/app/App.h
index 34fbdfa2..c18085f1 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -26,6 +26,7 @@
#include "core/Document.h"
#include "core/Face.h"
#include "core/MeshTransform.h"
+#include "app/Wintab.h"
#include "core/PenPredictor.h"
#include "core/AutoUv.h"
#include "core/QRemesh.h"
@@ -1108,6 +1109,9 @@ private:
int mouseClicks_ = 1; // SDL's click count of the press being handled (3 = a triple-click, #43)
bool spaceHeld_ = false; // Space is down with Preferences > Input > Hold Space to move the camera on (#43)
float subtoolClickX_ = 0.0f, subtoolClickY_ = 0.0f;
+ Uint64 subtoolClickNs_ = 0; // when the Alt press on another Part began (a long pen press isn't a click)
+ void partSelected(const char* how);
+ Uint64 clickClockNs() const; // the status bar names the new active Part and how it was chosen
uint32_t renamingSubtool_ = 0; // SubTool id being renamed inline (0: none)
uint32_t listedActiveSubtool_ = 0; // the list scrolls to the active SubTool when it changes
// SubTool thumbnails (#43): rendered from each SubTool's GPU buffers while the SubTools panel shows, one per frame
@@ -1725,12 +1729,24 @@ private:
void penLeft(); // out of proximity: ends what the pen was doing
float strokePressure() const; // pressure after the global curve; 1 for the mouse
SDL_Keymod inputMods() const; // the keyboard's modifiers plus those a pen button holds (Alt, Shift)
+ SDL_Keymod keyMods() const; // the keyboard's alone (in the input style's terms)
bool erasing() const; // the pen's eraser end is drawing
PenAction eraserAction() const { return uiPrefs_.tablet.eraser; }
void firstDab(float x, float y); // the stroke's first dab, once the pen reports a real pressure
void drawTabletSection(); // Preferences > Tablet
void detectPenName();
+ void tabletDeviceEvent(const SDL_Event& e); // a tablet plugged in or out while running (TabletHotplug)
+ void tabletNote(TabletHotplug::Note note, const std::string& name);
+ const char* pressureStatus() const; // "Pressure: on" / "not detected" (Preferences > Input and > Tablet)
+ void drawPressureStatus(); // ... with the troubleshooting link
PenPressureTracker penTracker_;
+ TabletHotplug tabletHotplug_;
+ void wintabEvent(const SDL_Event& e); // a mouse event while the tablet driver reports pressure through Wintab
+ WintabPressure wintab_;
+ bool wintabPen_ = false; // the mouse is the pen of a driver without Windows Ink (pressure from Wintab)
+ bool wintabTip_ = false;
+ Uint64 wintabNs_ = 0; // the last Wintab packet
+ bool penJump_ = false; // the pen just came into range: its first position doesn't move a drag (no jump)
bool testSystemCursor_ = false; // UI tests: a system cursor set by the script (os_cursor), else none
float testSystemCursorX_ = 0.0f, testSystemCursorY_ = 0.0f;
PenCursor penCursor_; // where the pen puts the cursor (Preferences > Tablet > Mouse mode)
diff --git a/src/app/Demo.h b/src/app/Demo.h
index 075ab826..8d9839e9 100644
--- a/src/app/Demo.h
+++ b/src/app/Demo.h
@@ -40,6 +40,7 @@ public:
void update(float dt); // before the UI is built: plays the timeline
void drawOverlay(); // inside the ImGui frame: captions and cursor
bool finished() const { return step_ >= steps_.size(); }
+ float clock() const { return clock_; } // seconds of the script so far (its own time, not the wall clock)
int frameStride() const { return stride_; } // >1 while fast-forwarding through waits
// The recording's virtual camera: the part of the window the video shows (its centre and zoom, 1 = all of it),
// eased between the targets set by zoomTo/zoomToItem. Captions and the fade stay the same size on screen.
@@ -164,7 +165,8 @@ private:
std::string expandLibrary(std::string text) const; // $LIBRARY -> the LightBox library folder (UiTest.cpp)
SDL_Keymod held_ = SDL_KMOD_NONE; // modifiers held by `hold` (Ctrl to mask, Ctrl+Shift to hide...)
// Simulated pen (pen_* steps).
- static constexpr SDL_PenID kTestPen = 1;
+ SDL_PenID testPen_ = 1; // tablet_plug gives the next pen a new id, as SDL does
+ uint32_t testTabletMouse_ = 0; // the simulated tablet's mouse device (tablet_plug / tablet_unplug)
bool penIn_ = false, penDown_ = false, penEraser_ = false;
ImVec2 penAt_{960, 540}; // the simulated pen (mouse_ is SDL's shared cursor, which stray mouse events move too)
SDL_PenInputFlags penState_ = 0;
diff --git a/src/app/Input.cpp b/src/app/Input.cpp
index 2ab4aec6..cfab4fda 100644
--- a/src/app/Input.cpp
+++ b/src/app/Input.cpp
@@ -443,6 +443,7 @@ void App::drawInputSection() {
if (ui::Checkbox("Hold Space and drag to move the camera##input.space_camera", &spaceCamera))
uiPrefs_.spaceCamera = spaceCamera;
ui::HelpAction(*this, "input.space_camera");
+ drawPressureStatus(); // the pen's pressure, with the troubleshooting link (Tablet.cpp)
if (mayaStyle()) {
int zoom = uiPrefs_.mayaZoomHorizontal ? 0 : 1;
ImGui::TextDisabled("%s", tr("Zoom drag"));
diff --git a/src/app/PanelSubTools.cpp b/src/app/PanelSubTools.cpp
index 81d0c9d0..998ed9e1 100644
--- a/src/app/PanelSubTools.cpp
+++ b/src/app/PanelSubTools.cpp
@@ -6,6 +6,7 @@
// the ZBrush viewport gesture Alt+click to select a SubTool. Every action goes
// through the subtool_* commands (core/SubTools.cpp).
#include "app/App.h"
+#include "app/Demo.h"
#include "app/Theme.h"
#include "app/I18n.h"
#include "core/Multires.h"
@@ -26,6 +27,7 @@ namespace sf {
namespace {
constexpr float kClickSlopPx = 4.0f; // movement that turns an Alt+click into a drag
+constexpr Uint64 kPartClickNs = 500000000ull; // a pen pressed longer than this isn't an Alt+click
constexpr int kListRows = 6; // the list scrolls beyond this many SubTools
// The SubTool list's colours; should become theme tokens (Theme.h).
// The recessed area behind the rows, a step below panelBg (the Classic look's #1c1c1f; other looks their shelf shade).
@@ -120,14 +122,30 @@ void App::subtoolClickMove(float x, float y) {
}
void App::subtoolClickRelease() {
- if (subtoolClickTarget_ >= 0 && subtoolClickTarget_ < doc_.subtoolCount()) {
- if (subtoolClickTarget_ != doc_.activeSubtool) runCommand("subtool_select", {{"index", double(subtoolClickTarget_)}});
+ // A pen pressed this long was held down to sculpt, not clicked (and Windows Ink's press-and-hold).
+ const bool held = (dragFromPen_ || wintabPen_) && clickClockNs() - subtoolClickNs_ > kPartClickNs;
+ if (!held && subtoolClickTarget_ >= 0 && subtoolClickTarget_ < doc_.subtoolCount()) {
+ if (subtoolClickTarget_ != doc_.activeSubtool) {
+ runCommand("subtool_select", {{"index", double(subtoolClickTarget_)}});
+ partSelected("Alt+click");
+ }
if (subtoolClickFrame_) camera_.frame(doc_.mesh.bounds()); // Shift+Alt+click: frame it too
}
subtoolClickTarget_ = -1;
subtoolClickFrame_ = false;
}
+// Every Part switch from the viewport or the list says which Part and how (an unexpected switch had users
+// guessing which key they had hit).
+Uint64 App::clickClockNs() const {
+ return demo_ ? Uint64(double(demo_->clock()) * 1e9) : SDL_GetTicksNS(); // UI tests: the script's time
+}
+
+void App::partSelected(const char* how) {
+ if (!lastCommandOk_) return;
+ status_ = "Active Part: " + doc_.activeSubtoolInfo().name + " (" + how + ")";
+}
+
void App::updateSubtoolThumbnails() {
// #43: what each SubTool looks like now; the schedule picks at most one to render this frame, from the GPU
// buffers syncScene just brought up to date (never a CPU render, never a re-upload: cheap on 30M+ meshes).
@@ -337,6 +355,7 @@ void App::drawSubToolsContents() {
runCommand("subtool_rename", {{"index", double(renameIndex)}, {"to", renameTo}});
if (selectIndex >= 0) {
runCommand("subtool_select", {{"index", double(selectIndex)}});
+ partSelected("Parts list");
}
if (toggleIndex >= 0) runCommand("subtool_visibility", {{"index", double(toggleIndex)}});
if (duplicateIndex >= 0) runCommand("subtool_duplicate", {{"index", double(duplicateIndex)}});
diff --git a/src/app/Tablet.cpp b/src/app/Tablet.cpp
index 625505e2..673b0ecd 100644
--- a/src/app/Tablet.cpp
+++ b/src/app/Tablet.cpp
@@ -123,8 +123,14 @@ void App::handlePenEvent(const SDL_Event& e) {
lastPenNs_ = SDL_GetTicksNS();
switch (e.type) {
case SDL_EVENT_PEN_PROXIMITY_IN:
+ // A pen coming into range while a pen drag is still open: it left without saying so (some drivers never
+ // do). End that first, with the buttons it held, instead of carrying the drag on from somewhere else.
+ if (penInProximity_ || (dragFromPen_ && drag_ != Drag::None)) penLeft();
penSeen_ = penInProximity_ = true;
+ penJump_ = true;
penId_ = e.pproximity.which;
+ if (const TabletHotplug::Note note = tabletHotplug_.penIn(uint32_t(e.pproximity.which)); note != TabletHotplug::Note::None)
+ tabletNote(note, {});
if (penName_.empty()) detectPenName();
break;
case SDL_EVENT_PEN_PROXIMITY_OUT:
@@ -147,6 +153,7 @@ void App::handlePenEvent(const SDL_Event& e) {
case SDL_EVENT_PEN_MOTION:
penSeen_ = penInProximity_ = true;
penId_ = e.pmotion.which;
+ penTracker_.motion();
break;
case SDL_EVENT_PEN_AXIS:
if (e.paxis.axis == SDL_PEN_AXIS_PRESSURE) {
@@ -217,15 +224,113 @@ void App::penLeft() {
penNavButton_ = 0;
firstDabPending_ = false;
penTracker_.touchUp();
+ penJump_ = true;
lastInputPen_ = false; // whatever moves the cursor next is a mouse until the pen says otherwise
}
+// A tablet plugged in after launch works without a restart (SDL finds its pens as they come into range); this
+// forgets what the last one did (its name, pressure, held buttons) and says so in the status bar.
+void App::tabletDeviceEvent(const SDL_Event& e) {
+ if (e.type == SDL_EVENT_MOUSE_ADDED) {
+ wintab_.open(window_); // Windows: a tablet without Windows Ink plugged in after launch
+ const char* name = SDL_GetMouseNameForID(e.mdevice.which);
+ const std::string n = name ? name : "";
+ tabletNote(tabletHotplug_.mouseAdded(uint32_t(e.mdevice.which), n), n);
+ } else if (e.type == SDL_EVENT_MOUSE_REMOVED) {
+ tabletNote(tabletHotplug_.mouseRemoved(uint32_t(e.mdevice.which)), {});
+ }
+}
+
+void App::tabletNote(TabletHotplug::Note note, const std::string& name) {
+ if (note == TabletHotplug::Note::None) return;
+ if (note == TabletHotplug::Note::Disconnected) {
+ if (penInProximity_ || penButtons_ || (dragFromPen_ && drag_ != Drag::None)) penLeft(); // no Alt left held
+ penTracker_.reset();
+ wintabPen_ = wintabTip_ = false;
+ status_ = "Tablet disconnected";
+ return;
+ }
+ penTracker_.reset(); // a new pen: whether it has pressure is its own
+ penTiltSeen_ = false;
+ if (note == TabletHotplug::Note::NewPen) return;
+ if (!name.empty()) penName_ = name;
+ else detectPenName();
+ status_ = penName_.empty() ? std::string("Tablet connected") : "Tablet connected: " + penName_;
+}
+
+const char* App::pressureStatus() const {
+ if (!penSeen_) return "Pressure: no pen seen yet";
+ if (penTracker_.hasPressure()) return "Pressure: on";
+ if (penTracker_.noPressure() || penTracker_.everSampled()) return "Pressure: not detected";
+ return "Pressure: touch the tablet to check";
+}
+
+void App::drawPressureStatus() {
+ const bool ok = penSeen_ && penTracker_.hasPressure();
+ ImGui::TextColored(col(ok ? ui::color::accent : ui::color::textDim), "%s", pressureStatus());
+ ImGui::SameLine();
+ if (ImGui::SmallButton(ICON_LC_CIRCLE_HELP " Troubleshooting##pressure_help")) guide_.open("troubleshooting.md#pen-tablets");
+#if defined(_WIN32)
+ ui::HelpText("Pressure", nullptr,
+ "No pressure? In Wacom Tablet Properties > Mapping, switch \"Use Windows Ink\" (on for most tablets, "
+ "off for some older drivers), then bring the pen back. Without pressure the pen draws at full "
+ "strength.",
+ "Preferences > Tablet");
+#else
+ ui::HelpText("Pressure", nullptr,
+ "Whether the pen reports pressure. Without it the pen draws at full strength; check the tablet in "
+ "your desktop's tablet settings.",
+ "Preferences > Tablet");
+#endif
+}
+
+// Windows with "Use Windows Ink" off in the tablet driver: no SDL pen events, the driver moves the mouse, and the
+// pressure comes from Wintab. While Windows Ink speaks (SDL pen events in the last second) it is the one source.
+void App::wintabEvent(const SDL_Event& e) {
+ const bool mouse = (e.type == SDL_EVENT_MOUSE_MOTION && e.motion.which != SDL_PEN_MOUSEID) ||
+ ((e.type == SDL_EVENT_MOUSE_BUTTON_DOWN || e.type == SDL_EVENT_MOUSE_BUTTON_UP) &&
+ e.button.which != SDL_PEN_MOUSEID);
+ if (!mouse || !wintab_.isOpen()) return;
+ const Uint64 now = SDL_GetTicksNS();
+ if (penInProximity_ && now - lastPenNs_ < 1000000000ull) return; // Windows Ink is on: SDL's pen events count
+ WintabPressure::Sample s;
+ if (wintab_.poll(s)) {
+ wintabNs_ = now;
+ if (!wintabPen_) {
+ wintabPen_ = penSeen_ = true;
+ if (penName_.empty()) penName_ = "Tablet (Wintab)";
+ }
+ if (s.tip != wintabTip_) {
+ wintabTip_ = s.tip;
+ if (s.tip) penTracker_.touchDown();
+ else penTracker_.touchUp();
+ }
+ penTracker_.sample(s.pressure);
+ if (s.tip) penTracker_.motion();
+ penEraser_ = s.tip && s.eraser;
+ } else if (wintabPen_ && now - wintabNs_ > 250000000ull) {
+ wintabPen_ = wintabTip_ = penEraser_ = false; // the mouse itself: full pressure
+ penTracker_.touchUp();
+ }
+}
+
float App::strokePressure() const {
- if (!lastInputPen_) return 1.0f; // the mouse always presses fully
- return uiPrefs_.tablet.curve.apply(penTracker_.raw());
+ if (!lastInputPen_ && !wintabPen_) return 1.0f; // the mouse always presses fully
+ return uiPrefs_.tablet.curve.apply(penTracker_.pressure()); // a pen without pressure: full
}
SDL_Keymod App::inputMods() const {
+ SDL_Keymod mods = keyMods();
+ for (int b = 1; b <= kPenButtons; ++b) {
+ if (!(penButtons_ & (1u << b))) continue;
+ const PenAction a = penButtonAction(b);
+ if (a == PenAction::Invert) mods |= SDL_KMOD_LALT;
+ else if (a == PenAction::Smooth) mods |= SDL_KMOD_LSHIFT;
+ }
+ return mods;
+}
+
+SDL_Keymod App::keyMods() const {
SDL_Keymod mods = SDL_GetModState();
if (blenderStyle()) mods = blenderMods(mods); // Blender's Ctrl inverts, its Mask brush masks (Input.cpp)
if (mayaStyle()) { // Maya: Ctrl inverts (as ZBrush's Alt), Ctrl+Alt masks (as ZBrush's Ctrl)
@@ -235,16 +340,10 @@ SDL_Keymod App::inputMods() const {
else if (ctrl && alt) mods = SDL_Keymod((mods & ~SDL_KMOD_ALT) | SDL_KMOD_LCTRL);
else if (ctrl && !shift) mods = SDL_Keymod((mods & ~SDL_KMOD_CTRL) | SDL_KMOD_LALT);
}
- for (int b = 1; b <= kPenButtons; ++b) {
- if (!(penButtons_ & (1u << b))) continue;
- const PenAction a = penButtonAction(b);
- if (a == PenAction::Invert) mods |= SDL_KMOD_LALT;
- else if (a == PenAction::Smooth) mods |= SDL_KMOD_LSHIFT;
- }
return mods;
}
-bool App::erasing() const { return lastInputPen_ && penEraser_; }
+bool App::erasing() const { return (lastInputPen_ || wintabPen_) && penEraser_; }
void App::firstDab(float x, float y) {
if (lastInputPen_ && !penTracker_.readyForFirstDab()) {
@@ -321,6 +420,8 @@ void App::drawTabletSection() {
ImGui::TextColored(col(ui::color::textDim), "%s", info.c_str());
}
+ drawPressureStatus();
+
// --- Pen or mouse mode (#54) ---
ImGui::Checkbox(tr("Mouse mode (relative)##tablet_mouse_mode"), &t.mouseMode);
ui::HelpText("Mouse mode", nullptr,
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index fcef8be5..6506b550 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -94,6 +94,7 @@
// dabs keep the volume now: the option, flipped by letting go of Shift mid-stroke),
// moved_out (summed movement along the snapshot's normals: > 0 out, < 0 in),
// pen_pressure (raw), brush_pressure (after the curve; 1 for the mouse),
+// pressure_status (0 no pen yet, 1 on, 2 not detected, 3 not known yet),
// stroke_continuous (Stroke > Continuous, 1 on),
// stroking (1 while a stroke is open), cursor_x / cursor_y (the brush cursor, pixels),
// surface_cursor (1 when the brush ring last drawn lay on the surface, #25),
@@ -181,6 +182,11 @@
// pen_pressure P a pressure sample from the hovering pen (e.g. a stale value before a stroke)
// pen_enter U V the pen comes into range at a point on the model (put down elsewhere, no hover)
// pen_out the pen leaves proximity
+// tablet_plug [NAME] a tablet plugged in while running (default name: a generic HID mouse); its pen gets
+// a new id
+// tablet_unplug ... and unplugged
+// pen_new the next pen has a new id, without a device event (Wacom Bridge, eraser on Wayland)
+// click_subtool NAME U V 1 pen [S] tap the Part with the pen tip instead (held S seconds)
// os_cursor U V | off where the system cursor is (a tablet driver in mouse mode moves it, #72); off: unknown
// mouse_move U V a plain mouse (not the pen) moves to a point on the model, e.g. the stray mouse
// events Windows Ink sends beside the pen's
@@ -697,6 +703,10 @@ double Demo::query(const std::string& name, bool& known) const {
return name == "moved" ? count : name == "moved_max" ? maxD : name == "moved_sum" ? sum : out;
}
if (name == "pen_pressure") return app_.penTracker_.raw();
+ if (name == "pressure_status") { // 0 no pen yet, 1 on, 2 not detected, 3 not known yet
+ const std::string st = app_.pressureStatus();
+ return st == "Pressure: on" ? 1.0 : st == "Pressure: not detected" ? 2.0 : app_.penSeen_ ? 3.0 : 0.0;
+ }
if (name == "brush_pressure") return app_.strokePressure();
if (name == "stroking") return app_.drag_ == App::Drag::Stroke ? 1.0 : 0.0;
if (name == "tablet_mouse_mode") return app_.uiPrefs_.tablet.mouseMode ? 1.0 : 0.0;
@@ -982,7 +992,9 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e
const std::string name = w[1];
const float u = num(2, 0.5f), v = num(3, 0.5f);
const int times = std::clamp(int(num(4, 1.0f)), 1, 3); // click_subtool NAME U V 3: a triple-click
- steps_.push_back([this, line, name, u, v, times](float t) {
+ const bool pen = w.back() == "pen" || (w.size() > 5 && w[5] == "pen"); // taps with the pen tip instead
+ const float hold = pen && w.size() > 6 ? num(6, 0.0f) : 0.0f; // ... held this long
+ steps_.push_back([this, line, name, u, v, times, pen, hold](float t) {
const int index = app_.doc_.findSubtool(name);
if (index < 0) {
fail(line, "no Part named \"" + name + "\"");
@@ -1021,6 +1033,25 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e
fail(line, "Part \"" + name + "\" can't be clicked (hidden, covered or off screen)");
return true;
}
+ if (pen) {
+ if (t == 0.0f) {
+ if (!penIn_) penProximity(true);
+ penMove(p.x, p.y);
+ return false;
+ }
+ if (!penDown_) {
+ penTouch(true, false);
+ penPressure(0.5f);
+ if (hold <= 0.0f) {
+ penTouch(false, false);
+ return true;
+ }
+ return false;
+ }
+ if (t < hold) return false;
+ penTouch(false, false);
+ return true;
+ }
if (t == 0.0f) { // hover for a frame first
mouseMotion(p.x, p.y);
return false;
@@ -1859,6 +1890,27 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e
} else if (verb == "spacemouse_unplug") {
run([this] { detachVirtualSpaceMouse(); });
wait(0.1f);
+ } else if (verb == "tablet_plug") {
+ // A tablet plugged in while the app runs: its mouse device appears (NAME as SDL reports it; a generic one,
+ // as on Windows or with Wacom Bridge, counts once its pen comes into range), and its pen has a new id.
+ const std::string name = w.size() > 1 ? restOf(lineText_, 1) : std::string("HID-compliant mouse");
+ run([this, name] {
+ if (penIn_) penProximity(false);
+ ++testPen_;
+ testTabletMouse_ = 100 + uint32_t(testPen_);
+ app_.tabletNote(app_.tabletHotplug_.mouseAdded(testTabletMouse_, name), name);
+ });
+ wait(0.05f);
+ } else if (verb == "tablet_unplug") {
+ run([this] { app_.tabletNote(app_.tabletHotplug_.mouseRemoved(testTabletMouse_), {}); });
+ wait(0.05f);
+ } else if (verb == "pen_new") {
+ // A pen with an id not seen before and no device event (a Wacom Bridge session reconnecting, the eraser
+ // end on Wayland).
+ run([this] {
+ if (penIn_) penProximity(false);
+ ++testPen_;
+ });
} else if (verb == "pen_out") {
run([this] { penProximity(false); });
wait(0.05f);
@@ -1928,7 +1980,7 @@ void Demo::penProximity(bool in) {
SDL_Event e{};
e.type = in ? SDL_EVENT_PEN_PROXIMITY_IN : SDL_EVENT_PEN_PROXIMITY_OUT;
e.pproximity.windowID = SDL_GetWindowID(app_.window_);
- e.pproximity.which = kTestPen;
+ e.pproximity.which = testPen_;
penIn_ = in;
if (!in) penDown_ = penEraser_ = false;
penState_ = in ? SDL_PEN_INPUT_IN_PROXIMITY : 0; // leaving releases the tip and every button
@@ -1939,7 +1991,7 @@ void Demo::penMove(float x, float y) {
SDL_Event e{};
e.type = SDL_EVENT_PEN_MOTION;
e.pmotion.windowID = SDL_GetWindowID(app_.window_);
- e.pmotion.which = kTestPen;
+ e.pmotion.which = testPen_;
e.pmotion.pen_state = penState_;
e.pmotion.x = x;
e.pmotion.y = y;
@@ -1952,7 +2004,7 @@ void Demo::penPressure(float pressure) {
SDL_Event e{};
e.type = SDL_EVENT_PEN_AXIS;
e.paxis.windowID = SDL_GetWindowID(app_.window_);
- e.paxis.which = kTestPen;
+ e.paxis.which = testPen_;
e.paxis.pen_state = penState_;
e.paxis.x = penAt_.x;
e.paxis.y = penAt_.y;
@@ -1969,7 +2021,7 @@ void Demo::penTouch(bool down, bool eraser) {
SDL_Event e{};
e.type = down ? SDL_EVENT_PEN_DOWN : SDL_EVENT_PEN_UP;
e.ptouch.windowID = SDL_GetWindowID(app_.window_);
- e.ptouch.which = kTestPen;
+ e.ptouch.which = testPen_;
e.ptouch.pen_state = penState_;
e.ptouch.x = penAt_.x;
e.ptouch.y = penAt_.y;
@@ -1986,7 +2038,7 @@ void Demo::penButtonEvent(int button, bool down) {
SDL_Event e{};
e.type = down ? SDL_EVENT_PEN_BUTTON_DOWN : SDL_EVENT_PEN_BUTTON_UP;
e.pbutton.windowID = SDL_GetWindowID(app_.window_);
- e.pbutton.which = kTestPen;
+ e.pbutton.which = testPen_;
e.pbutton.pen_state = penState_;
e.pbutton.x = penAt_.x;
e.pbutton.y = penAt_.y;
diff --git a/src/app/Wintab.cpp b/src/app/Wintab.cpp
new file mode 100644
index 00000000..bbd0f65d
--- /dev/null
+++ b/src/app/Wintab.cpp
@@ -0,0 +1,119 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Wintab pressure (see Wintab.h). The few Wintab 1.4 types and constants used are declared here from the public
+// specification; nothing is linked: Wintab32.dll is loaded at run time from System32, where tablet drivers put it.
+#include "app/Wintab.h"
+
+#if defined(_WIN32)
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+#include <algorithm>
+#endif
+
+namespace sf {
+
+#if defined(_WIN32)
+
+namespace {
+
+// Wintab 1.4 (public specification).
+using WTPKT = DWORD;
+using FIX32 = DWORD;
+using HCTX = HANDLE;
+struct AXIS {
+ LONG axMin, axMax;
+ UINT axUnits;
+ FIX32 axResolution;
+};
+struct LOGCONTEXTW {
+ WCHAR lcName[40];
+ UINT lcOptions, lcStatus, lcLocks, lcMsgBase, lcDevice, lcPktRate;
+ WTPKT lcPktData, lcPktMode, lcMoveMask;
+ DWORD lcBtnDnMask, lcBtnUpMask;
+ LONG lcInOrgX, lcInOrgY, lcInOrgZ, lcInExtX, lcInExtY, lcInExtZ;
+ LONG lcOutOrgX, lcOutOrgY, lcOutOrgZ, lcOutExtX, lcOutExtY, lcOutExtZ;
+ FIX32 lcSensX, lcSensY, lcSensZ;
+ BOOL lcSysMode;
+ int lcSysOrgX, lcSysOrgY, lcSysExtX, lcSysExtY;
+ FIX32 lcSysSensX, lcSysSensY;
+};
+constexpr UINT WTI_DEFSYSCTX = 4, WTI_DEVICES = 100, DVC_NPRESSURE = 15;
+constexpr UINT CXO_SYSTEM = 0x0001;
+constexpr WTPKT PK_CURSOR = 0x0020, PK_BUTTONS = 0x0040, PK_NORMAL_PRESSURE = 0x0400;
+// A packet with PK_CURSOR | PK_BUTTONS | PK_NORMAL_PRESSURE: the fields in bit order.
+struct Packet {
+ UINT cursor;
+ DWORD buttons;
+ UINT pressure;
+};
+
+using WTInfoWFn = UINT(WINAPI*)(UINT, UINT, LPVOID);
+using WTOpenWFn = HCTX(WINAPI*)(HWND, LOGCONTEXTW*, BOOL);
+using WTCloseFn = BOOL(WINAPI*)(HCTX);
+using WTPacketsGetFn = int(WINAPI*)(HCTX, int, LPVOID);
+
+template <class F> F proc(void* lib, const char* name) {
+ return reinterpret_cast<F>(reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(lib), name)));
+}
+
+} // namespace
+
+bool WintabPressure::open(SDL_Window* window) {
+ if (ctx_) return true;
+ if (!window) return false;
+ if (!lib_) lib_ = LoadLibraryExW(L"Wintab32.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
+ if (!lib_) return false;
+ const auto info = proc<WTInfoWFn>(lib_, "WTInfoW");
+ const auto openCtx = proc<WTOpenWFn>(lib_, "WTOpenW");
+ if (!info || !openCtx || !proc<WTPacketsGetFn>(lib_, "WTPacketsGet") || !proc<WTCloseFn>(lib_, "WTClose")) return false;
+ const auto hwnd = static_cast<HWND>(
+ SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr));
+ if (!hwnd) return false;
+ LOGCONTEXTW lc{};
+ if (info(WTI_DEFSYSCTX, 0, &lc) == 0) return false; // no tablet (yet): tried again when a device is added
+ AXIS pressure{};
+ if (info(WTI_DEVICES, DVC_NPRESSURE, &pressure) != 0 && pressure.axMax > pressure.axMin)
+ pressureMax_ = pressure.axMax;
+ lc.lcOptions |= CXO_SYSTEM; // the driver keeps moving the cursor
+ lc.lcPktData = PK_CURSOR | PK_BUTTONS | PK_NORMAL_PRESSURE;
+ lc.lcPktMode = 0; // absolute
+ lc.lcMoveMask = PK_BUTTONS | PK_NORMAL_PRESSURE;
+ ctx_ = openCtx(hwnd, &lc, TRUE);
+ if (ctx_) SDL_Log("Wintab: pressure from the tablet driver (levels %ld)", pressureMax_ + 1);
+ return ctx_ != nullptr;
+}
+
+void WintabPressure::close() {
+ if (ctx_ && lib_)
+ if (const auto closeCtx = proc<WTCloseFn>(lib_, "WTClose")) closeCtx(static_cast<HCTX>(ctx_));
+ ctx_ = nullptr;
+ if (lib_) FreeLibrary(static_cast<HMODULE>(lib_));
+ lib_ = nullptr;
+}
+
+bool WintabPressure::poll(Sample& out) {
+ if (!ctx_) return false;
+ const auto get = proc<WTPacketsGetFn>(lib_, "WTPacketsGet");
+ if (!get) return false;
+ Packet packets[64];
+ int n = 0, last = 0;
+ while ((n = get(static_cast<HCTX>(ctx_), 64, packets)) > 0) last = n; // drain; the newest is what counts
+ if (last == 0) return false;
+ const Packet& p = packets[last - 1];
+ out.pressure = std::clamp(float(p.pressure) / float(std::max(pressureMax_, 1L)), 0.0f, 1.0f);
+ out.tip = (p.buttons & 1u) != 0 || out.pressure > 0.0f;
+ out.eraser = p.cursor % 3 == 2; // cursor types repeat per tablet: puck, pen, eraser
+ return true;
+}
+
+#else
+
+bool WintabPressure::open(SDL_Window*) { return false; }
+void WintabPressure::close() {}
+bool WintabPressure::poll(Sample&) { return false; }
+
+#endif
+
+} // namespace sf
diff --git a/src/app/Wintab.h b/src/app/Wintab.h
new file mode 100644
index 00000000..8984ca5a
--- /dev/null
+++ b/src/app/Wintab.h
@@ -0,0 +1,40 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Pen pressure through Wintab on Windows, for tablets whose driver doesn't use Windows Ink (Wacom Tablet
+// Properties > Mapping > "Use Windows Ink" off, common with older Intuos and with Wacom Bridge setups). SDL3 reads
+// pens only through Windows Ink; without it the driver moves the mouse and the pressure is lost. This reads just the
+// pressure, the tip and the eraser from the driver's Wintab32.dll (System32 only, no files of ours); the mouse events
+// keep doing the rest. The app uses it only while Windows Ink is silent (Tablet.cpp). Elsewhere it does nothing.
+#pragma once
+
+#include <SDL3/SDL.h>
+
+namespace sf {
+
+class WintabPressure {
+public:
+ struct Sample {
+ float pressure = 0.0f; // 0..1
+ bool tip = false; // touching
+ bool eraser = false; // the eraser end
+ };
+
+ WintabPressure() = default;
+ WintabPressure(const WintabPressure&) = delete;
+ WintabPressure& operator=(const WintabPressure&) = delete;
+ ~WintabPressure() { close(); }
+
+ bool open(SDL_Window* window); // true once a context is open (again: harmless); false without a Wintab driver
+ void close();
+ bool isOpen() const { return ctx_ != nullptr; }
+ // The newest packet since the last call (false: none came). Cheap: called on every mouse event.
+ bool poll(Sample& out);
+
+private:
+ void* lib_ = nullptr;
+ void* ctx_ = nullptr;
+ long pressureMax_ = 1023;
+};
+
+} // namespace sf
diff --git a/src/core/Tablet.cpp b/src/core/Tablet.cpp
index 3a10b8f7..6ce0c929 100644
--- a/src/core/Tablet.cpp
+++ b/src/core/Tablet.cpp
@@ -3,6 +3,7 @@
#include "core/Tablet.h"
#include <algorithm>
+#include <cctype>
#include <cmath>
namespace sf {
@@ -103,6 +104,57 @@ void PenPressureTracker::sample(float pressure) {
raw_ = clamp01(pressure);
sinceDown_ = true;
everSampled_ = true;
+ if (raw_ > kContact) pressed_ = true;
+}
+
+void PenPressureTracker::motion() {
+ if (down_ && !pressed_ && silentMotions_ < kSilentMotions) ++silentMotions_;
+}
+
+bool looksLikeTablet(const std::string& name) {
+ std::string s = name;
+ std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return char(std::tolower(c)); });
+ for (const char* what : {"wacom", "intuos", "cintiq", "bamboo", "huion", "kamvas", "xp-pen", "xppen", "gaomon",
+ "ugee", "veikk", "tablet", "stylus", "digitizer", " pen"})
+ if (s.find(what) != std::string::npos) return true;
+ return false;
+}
+
+TabletHotplug::Note TabletHotplug::mouseAdded(uint32_t id, const std::string& name) {
+ if (looksLikeTablet(name)) {
+ if (std::find(tabletMice_.begin(), tabletMice_.end(), id) == tabletMice_.end()) tabletMice_.push_back(id);
+ pens_.clear(); // its pens are new, even with an id seen before
+ candidate_ = 0;
+ announced_ = true;
+ return Note::Connected;
+ }
+ candidate_ = id;
+ announced_ = false;
+ return Note::None;
+}
+
+TabletHotplug::Note TabletHotplug::mouseRemoved(uint32_t id) {
+ if (id == candidate_) candidate_ = 0;
+ const auto it = std::find(tabletMice_.begin(), tabletMice_.end(), id);
+ if (it == tabletMice_.end()) return Note::None;
+ tabletMice_.erase(it);
+ pens_.clear();
+ announced_ = false;
+ return Note::Disconnected;
+}
+
+TabletHotplug::Note TabletHotplug::penIn(uint32_t penId) {
+ if (std::find(pens_.begin(), pens_.end(), penId) != pens_.end()) return Note::None;
+ pens_.push_back(penId);
+ if (candidate_ != 0) { // the generic device added just before was this tablet
+ tabletMice_.push_back(candidate_);
+ candidate_ = 0;
+ if (!announced_) {
+ announced_ = true;
+ return Note::Connected;
+ }
+ }
+ return Note::NewPen;
}
void PenCursor::cursorAt(float x, float y) {
diff --git a/src/core/Tablet.h b/src/core/Tablet.h
index 667f9c5f..bed25a24 100644
--- a/src/core/Tablet.h
+++ b/src/core/Tablet.h
@@ -13,6 +13,7 @@
#include <array>
#include <cstdint>
#include <string>
+#include <vector>
namespace sf {
@@ -71,23 +72,63 @@ void fromJson(const Json& j, TabletSettings& t);
// previous stroke (or the 1.0 assumed before any sample) may still be high. The
// first dab waits until a fresh, non-zero sample arrives; the app draws anyway at
// the first motion for pens that have never reported pressure.
+//
+// A pen without a pressure axis (Windows Ink switched off in the Wacom driver, some old tablets) may still send
+// pressure 0 while touching: after kSilentMotions touching motions without any real pressure the pen counts as
+// pressure-less and draws at full pressure (noPressure) instead of never starting the stroke.
class PenPressureTracker {
public:
static constexpr float kContact = 0.002f; // below this the tip is not really pressing
-
- void touchDown() { sinceDown_ = false; }
- void touchUp() { sinceDown_ = false; }
+ static constexpr int kSilentMotions = 8; // touching motions without pressure before the pen counts as without
+
+ void touchDown() {
+ sinceDown_ = false;
+ down_ = true;
+ }
+ void touchUp() {
+ sinceDown_ = false;
+ down_ = false;
+ }
void sample(float pressure);
+ void motion(); // the pen moved (counts touching motions that brought no pressure)
+ void reset() { *this = PenPressureTracker(); } // another pen or tablet: forget what this one did
float raw() const { return raw_; }
+ float pressure() const { return noPressure() ? 1.0f : raw_; } // what brushes get, before the curve
bool everSampled() const { return everSampled_; } // false: maybe a pen without pressure
- bool readyForFirstDab() const { return sinceDown_ && raw_ > kContact; }
+ bool hasPressure() const { return pressed_; } // the pen has reported a real pressure
+ bool noPressure() const { return !pressed_ && silentMotions_ >= kSilentMotions; }
+ bool readyForFirstDab() const { return (sinceDown_ && raw_ > kContact) || noPressure(); }
private:
float raw_ = 1.0f;
bool sinceDown_ = false;
bool everSampled_ = false;
+ bool down_ = false;
+ bool pressed_ = false;
+ int silentMotions_ = 0;
};
+// Tablets plugged in or out while the app runs. SDL has no pen hot-plug events: a new pen is a new SDL_PenID at
+// its first proximity, and the tablet itself shows up as a mouse device (SDL_EVENT_MOUSE_ADDED / _REMOVED) with
+// the maker's name on Linux and usually on Windows. A mouse device added after launch with a generic name may be
+// the tablet too: if a new pen follows it, it was, and unplugging it says so.
+class TabletHotplug {
+public:
+ enum class Note { None, NewPen, Connected, Disconnected };
+
+ Note mouseAdded(uint32_t id, const std::string& name);
+ Note mouseRemoved(uint32_t id);
+ Note penIn(uint32_t penId);
+
+private:
+ std::vector<uint32_t> tabletMice_, pens_;
+ uint32_t candidate_ = 0; // a generic mouse device added after launch (0: none)
+ bool announced_ = false; // "Tablet connected" already said for the device just added
+};
+
+// The name of an input device that is a pen tablet or pen display (Wacom, Huion, XP-Pen ...).
+bool looksLikeTablet(const std::string& name);
+
// Where the pen puts the cursor. In pen mode that is where the pen reports it is. In mouse mode (the driver's
// relative mode, TabletSettings::mouseMode) the pen moves the cursor by how far it moves, like a mouse: lifting
// it away and putting it down elsewhere on the tablet leaves the cursor where it was. Windows Ink reports the
diff --git a/tests/test_tablet.cpp b/tests/test_tablet.cpp
index 8b3c0bae..9388a5e6 100644
--- a/tests/test_tablet.cpp
+++ b/tests/test_tablet.cpp
@@ -216,3 +216,62 @@ TEST(tablet_settings_mouse_mode_round_trip) {
fromJson(j, d);
CHECK(!d.mouseMode);
}
+
+// A pen without a pressure axis (Windows Ink off) sends 0 while touching: after a few motions it draws at full
+// pressure instead of never starting the stroke; a pen with pressure never does.
+TEST(pen_pressure_tracker_no_pressure_axis) {
+ PenPressureTracker t;
+ t.touchDown();
+ t.sample(0.0f);
+ for (int i = 0; i + 1 < PenPressureTracker::kSilentMotions; ++i) t.motion();
+ CHECK(!t.readyForFirstDab());
+ CHECK(!t.noPressure());
+ t.motion();
+ CHECK(t.noPressure());
+ CHECK(t.readyForFirstDab());
+ CHECK(near(t.pressure(), 1.0f));
+ t.reset(); // another pen: its own
+ CHECK(!t.noPressure() && !t.hasPressure());
+
+ PenPressureTracker p;
+ p.touchDown();
+ p.sample(0.4f);
+ for (int i = 0; i < 3 * PenPressureTracker::kSilentMotions; ++i) p.motion();
+ p.touchUp();
+ p.touchDown();
+ p.sample(0.0f);
+ for (int i = 0; i < 3 * PenPressureTracker::kSilentMotions; ++i) p.motion();
+ CHECK(p.hasPressure());
+ CHECK(!p.noPressure()); // a light touch on a pen with pressure stays light
+ CHECK(near(p.pressure(), 0.0f));
+ PenPressureTracker hover;
+ for (int i = 0; i < 3 * PenPressureTracker::kSilentMotions; ++i) hover.motion();
+ CHECK(!hover.noPressure()); // hovering never counts
+}
+
+TEST(tablet_hotplug_notes) {
+ CHECK(looksLikeTablet("Wacom Intuos3 6x8 Pen"));
+ CHECK(looksLikeTablet("Intuos3 6x8"));
+ CHECK(looksLikeTablet("HUION Kamvas 13"));
+ CHECK(!looksLikeTablet("Logitech USB Optical Mouse"));
+ CHECK(!looksLikeTablet("HID-compliant mouse"));
+ CHECK(!looksLikeTablet("Openwheel")); // "pen" only as a word
+
+ using N = TabletHotplug::Note;
+ TabletHotplug h;
+ CHECK(h.penIn(7) == N::NewPen); // the tablet plugged in before launch: no note
+ CHECK(h.penIn(7) == N::None);
+ CHECK(h.mouseRemoved(3) == N::None); // a mouse, not the tablet
+
+ // Named tablet: connected at once, its pen needs no second note, unplugging says so.
+ CHECK(h.mouseAdded(10, "Wacom Intuos3 6x8 Pen") == N::Connected);
+ CHECK(h.penIn(7) == N::NewPen); // the same id after a replug is a new pen
+ CHECK(h.mouseRemoved(10) == N::Disconnected);
+
+ // Generic name (Windows): connected once its pen shows up; unplugging that device is the tablet leaving.
+ CHECK(h.mouseAdded(11, "HID-compliant mouse") == N::None);
+ CHECK(h.penIn(8) == N::Connected);
+ CHECK(h.penIn(9) == N::NewPen); // the eraser end, on systems where it is a pen of its own
+ CHECK(h.mouseRemoved(11) == N::Disconnected);
+ CHECK(h.mouseRemoved(11) == N::None);
+}
diff --git a/tests/ui/tablet_hotplug.sfui b/tests/ui/tablet_hotplug.sfui
new file mode 100644
index 00000000..99a1172d
--- /dev/null
+++ b/tests/ui/tablet_hotplug.sfui
@@ -0,0 +1,98 @@
+# A Wacom Intuos 3 report (0.10.8): the tablet worked only if plugged in before launch, the camera sometimes went
+# wild, pressure was sometimes missing and the active Part changed by itself.
+run ui_look look=classic layout=zbrush
+run new_sphere subdivisions=4
+key f
+
+# Plugged in after launch with a generic device name (Windows, Wacom Bridge): connected once its pen comes into range,
+# and it sculpts with pressure.
+tablet_plug
+pen_hover 0.5 0.5
+expect_status Tablet connected
+snapshot
+pen_stroke 0.3 0.45 0.7 0.55 0.2 0.8
+expect_value pressure_status == 1
+expect_value moved > 0
+tablet_unplug
+expect_status Tablet disconnected
+expect_value stroking == 0
+
+# With the maker's name: said at once. A pen without a pressure axis (pressure 0 while touching, as with Windows
+# Ink off) still sculpts, at full pressure, and Preferences says pressure isn't detected.
+tablet_plug Wacom Intuos3 6x8 Pen
+expect_status Tablet connected: Wacom Intuos3 6x8 Pen
+snapshot
+pen_stroke 0.3 0.45 0.7 0.55 0 0
+expect_value pressure_status == 2
+expect_value moved > 0
+expect_value brush_pressure == 1
+
+# Wacom Bridge reconnecting (a new pen, no device event): pressure is read again for it.
+pen_new
+snapshot
+pen_stroke 0.3 0.6 0.7 0.6 0.3 0.9
+expect_value pressure_status == 1
+expect_value moved > 0
+
+# The camera: the lower side button (Right-click) drags the view; the pen leaving mid-drag ends it, and coming
+# back elsewhere doesn't move the view.
+camera 10 -10
+pen_hover 0.5 0.5
+pen_button 1 down
+pen_hover 0.6 0.5
+pen_out
+remember camera_yaw y1
+remember camera_pitch p1
+pen_enter 0.1 0.9
+pen_hover 0.2 0.8
+pen_button 1 up
+expect_value camera_yaw == $y1
+expect_value camera_pitch == $p1
+# A side button pressed and let go without moving: nothing moves.
+pen_button 1 down
+pen_button 1 up
+pen_button 2 down
+pen_button 2 up
+expect_value camera_yaw == $y1
+expect_value camera_pitch == $p1
+# The mouse moved the cursor meanwhile: the pen's first position is a jump, not a drag.
+mouse_move 0.9 0.1
+pen_enter 0.5 0.5
+expect_value camera_yaw == $y1
+# Flipping to the eraser end (a pen of its own on some systems): it sculpts, the view stays.
+pen_out
+pen_new
+snapshot
+pen_stroke 0.3 0.45 0.7 0.55 0.6 0.6 eraser
+expect_value moved > 0
+expect_value camera_yaw == $y1
+expect_value camera_pitch == $p1
+expect_value stroking == 0
+
+# The active Part: only the keyboard's Alt with a quick click switches it, and the status bar says so.
+run subtool_append type=cube name=Cube
+run subtool_select index=0
+key f
+# The upper pen button (Invert, as Alt) held while tapping another Part: no switch.
+pen_button 2 down
+click_subtool Cube 0.5 0.5 1 pen
+pen_button 2 up
+expect_value active_subtool == 0
+# Alt held on the keyboard and a long pen press: sculpting, not a click.
+hold alt
+click_subtool Cube 0.5 0.5 1 pen 0.8
+hold none
+expect_value active_subtool == 0
+# Alt + a pen tap: the Cube, named in the status bar with the shortcut.
+hold alt
+click_subtool Cube 0.5 0.5 1 pen
+hold none
+expect_value active_subtool == 1
+expect_status Active Part: Cube (Alt+click)
+# The mouse's Alt+click still works.
+run subtool_select index=0
+hold alt
+click_subtool Cube 0.5 0.5
+hold none
+expect_value active_subtool == 1
+expect_status (Alt+click)
diff --git a/tests/ui/whats_new.sfui b/tests/ui/whats_new.sfui
index 6b70eb9d..b246f904 100644
--- a/tests/ui/whats_new.sfui
+++ b/tests/ui/whats_new.sfui
@@ -5,8 +5,9 @@ click "Help##palette.help"
click "What's New##help.whats_new"
expect_value guide_open == 1
expect_guide whats-new.md
+expect_text "Version 0.10.8"
+expect_text "Tablets"
expect_text "Version 0.10.7"
-expect_text "A new look"
expect_text "Version 0.10.6"
expect_text "Version 0.10.5"
expect_text "Version 0.10.4"