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.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Pen: the app makes the pen tip's clicks (SDL 3.2 got stuck, #95); Mouse mode relative with the arrow at the brush (#72); focus, not hover, keeps frames coming (two screens)
islandmed5c0733ea95f8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 16064aba..ec5425e0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -78,6 +78,20 @@ All notable changes to SculptForge are listed here. The format followssculpt keymap does, instead of turning the view (#77, reported by TheRedWaxPolice). Beside the model Alt+drag stillturns the view, and Alt+Shift+drag pans anywhere. Shift+F (Strength) sets the brush's Intensity instead of Smooth's.- UI test runs remove their temporary settings folder when they pass (thousands of runs had filled `/tmp`).+- The pen picks a brush, then stops clicking the panels and the canvas until the mouse is used (#95; Huion Kamvas,+ display tablets, Wacom with Windows Ink on or off): SDL 3.2 keeps one "a pen is touching" flag for all pens, and+ once Windows took a pen away while it touched (a drag capturing the mouse), it made no mouse events for any pen+ again. SculptForge now makes the pen tip's mouse events from the pen's own and ignores SDL's, lets go of the+ button whenever the pen leaves, and ignores the mouse presses some drivers (Huion) send beside the pen's, whose+ release may never come. A replay test covers Windows Ink, a Huion-style pen + mouse stream, SDL's stuck state and+ Wintab-style input.+- Tablet Mouse mode (#72, 0.10.8): it followed the system cursor, which with the tablet driver in pen mode is where+ the pen is, so it felt the same as Pen mode. Now only the pen's movement moves the cursor, with no jump when the+ pen comes back elsewhere, and while the pen is in range the arrow is drawn at the brush (the system's, at the+ pen's place on the tablet, is hidden), so arrow, brush and clicks agree.+- With two screens, moving the pointer to the other screen made SculptForge draw as if it were in the background+ until the pointer came back (Windows 11). It now keeps drawing after any input while its window has focus,+ wherever the pointer is, and rests only when it loses focus.- 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 theWacom driver with "Use Windows Ink" off (Wintab); a pen without pressure draws at full strength instead of not atdiff --git a/docs/guide/sculpting.md b/docs/guide/sculpting.mdindex 24649f85..d313e492 100644--- a/docs/guide/sculpting.md+++ b/docs/guide/sculpting.md@@ -633,9 +633,10 @@ also have a **Pressure curve** setting that makes them respond to a light or a f- the eraser end (default **Invert**, like holding <kbd>Alt</kbd>) and the side buttons (default:lower button right-click to rotate the view, upper button **Invert**, third button **Pan view**). A side buttoncan open a [radial menu](#radial-menus) instead (Preferences > Input > Radial Menus).-- **Mouse mode (relative)**, for a tablet whose driver is set to mouse mode: the pen moves the cursor by how far- it moves, and putting it down elsewhere on the tablet leaves the cursor where it was. The brush follows the- system cursor the driver moves, so brush, arrow and clicks always agree (#72).+- **Mouse mode (relative)**: the pen moves the cursor by how far it moves, like a mouse, and putting it down+ elsewhere on the tablet leaves the cursor where it was, with the driver in pen mode or in its own mouse mode.+ While the pen is in range the arrow is drawn at the brush (the system's is hidden), so brush, arrow and clicks+ always agree (#72).**Store Config** (<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>I</kbd>) keeps these settings. Setup notes for WindowsInk, macOS and Linux are indiff --git a/docs/guide/troubleshooting.md b/docs/guide/troubleshooting.mdindex 2110aac2..0da9a1a2 100644--- a/docs/guide/troubleshooting.md+++ b/docs/guide/troubleshooting.md@@ -84,7 +84,13 @@ If no pen is detected or pressure doesn't change:quick click), a triple-click with the mouse or a click in the Parts list; the status bar names it and how. Atablet'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)**.+ mode. Turn on **Preferences > Tablet > Mouse mode (relative)**. It also makes the pen relative with the driver+ in pen mode; the arrow is drawn at the brush while the pen is in range.+- **The pen picks a brush, then stops clicking menus or the canvas until you use the mouse** (Huion, display+ tablets, Wacom; 0.10.8 and earlier): update to 0.10.9 or later. If it still happens, tell us the tablet, its+ driver version and whether Windows Ink is on.+- **With two screens, the app slows down when the pointer is on the other screen:** fixed in 0.10.9; SculptForge+ now rests only when its window loses focus.## AI servicesdiff --git a/docs/guide/whats-new.md b/docs/guide/whats-new.mdindex 230aea77..1274d4c3 100644--- a/docs/guide/whats-new.md+++ b/docs/guide/whats-new.md@@ -53,6 +53,13 @@ action: guide:whats-new.md**Preferences > Input** shows whether pressure is detected, with a troubleshooting link. The view no longer jumpswhen the pen comes back into range, and the active Part changes only with **Alt+click** on the keyboard; thestatus bar names the new Part. See [Troubleshooting](troubleshooting.md#pen-tablets).+- **The pen keeps working the panels**: with some tablets (Huion, pen displays, Wacom with Windows Ink on or off)+ the pen picked one brush, then stopped clicking menus and the canvas until you used the mouse. SculptForge now+ makes the pen's clicks itself (#95). See [Troubleshooting](troubleshooting.md#pen-tablets).+- **Mouse mode is relative again**: with **Mouse mode** on (Preferences > Tablet) the pen moves the cursor by how+ far it moves, whatever the driver's mode, and the arrow is drawn right at the brush (#72).+- **Two screens**: SculptForge no longer slows down when the pointer is on your other screen; it rests only when+ its window loses focus.- **Smaller (or larger) corner axes**: **Draw > Camera > Gizmo size** sets Small, Medium or Large([Navigation](navigation.md)).- **View Figure**: turn it on beside **Axes** for a small head in the corner that turns with the view; click it todiff --git a/docs/tablet.md b/docs/tablet.mdindex 3e1ab313..709ead62 100644--- a/docs/tablet.md+++ b/docs/tablet.md@@ -9,10 +9,11 @@ Open **Preferences > Tablet** to check the pen and tune it:- **Pen**: whether a pen has been seen, whether it is in range or touching (and with which end), the displayserver, and on Linux the device's name. SDL 3.4 and later also says whether it's a pen display or a tablet.-- **Mouse mode (relative)**: turn it on when the tablet driver is set to mouse mode (Wacom: Mapping > Mode >- Mouse; XP-Pen and Huion: Relative / Mouse mode). The pen then moves the cursor by how far it moves, like a- mouse, and lifting it to put it down elsewhere on the tablet leaves the cursor where it was. Off (pen mode,- the default): the cursor is where the pen is.+- **Mouse mode (relative)**: the pen moves the cursor by how far it moves, like a mouse, and lifting it to put+ it down elsewhere on the tablet leaves the cursor where it was. It works with the tablet driver in pen mode or+ in its own mouse mode (Wacom: Mapping > Mode > Mouse; XP-Pen and Huion: Relative / Mouse mode). While the pen is+ in range SculptForge draws the arrow at the brush and hides the system's, which sits where the pen is on the+ tablet; clicks land at the arrow. Off (pen mode, the default): the cursor is where the pen is.- **Pressure meter**: the pressure the tablet reports, and what brushes get after the curve below.- **Draw-test area**: draw in it with the pen; the stroke is as thick as the brush pressure and the line alongthe bottom plots the pressure along the stroke. Good for checking a light touch and a full press.@@ -93,13 +94,23 @@ is added. The logic is `TabletHotplug` in `core/Tablet.h`.- **The cursor jumps back to where it was when you put the pen down elsewhere** (the tablet in mouse orrelative mode, #54): turn on **Mouse mode (relative)** in Preferences > Tablet. Windows Ink reports where thepen is on the tablet even in mouse mode, which SculptForge 0.10.2 and earlier followed.+- **Mouse mode felt the same as pen mode** (0.10.8, #72): it followed the system cursor, which with the driver+ in pen mode is where the pen is. Since 0.10.9 only the pen's movement counts.+- **The pen picks one brush, then stops clicking the panels and the canvas until you use the mouse** (0.10.8 and+ earlier, #95; Huion, display tablets, Wacom with Windows Ink on or off): SDL 3.2 stopped making mouse clicks+ for every pen once Windows had taken one pen away while it touched. Since 0.10.9 SculptForge makes the pen's+ clicks itself from the pen's own events, and ignores the extra mouse presses some drivers send beside them.## For developersThe settings and the curve are in `src/core/Tablet.h` (unit tests: `tests/test_tablet.cpp`); the pen eventhandling 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`, `pen_out`, `pen_new`, `tablet_plug` and `tablet_unplug` (documented in `src/app/UiTest.cpp`); `tests/ui/tablet.sfui`+events plus the mouse events SDL 3.2 synthesizes from them (which the app ignores: it makes the pen's mouse events+itself, `App::penMouseEvents`): `pen_stroke`, `pen_drag_item`, `pen_button`, `pen_hover`, `pen_enter`, `pen_drag`,+`pen_pressure`, `pen_out`, `pen_new`, `pen_lost`, `pen_stream`, `sdl_pen_mouse`, `tablet_plug` and `tablet_unplug`+(documented in `src/app/UiTest.cpp`); `tests/ui/pen_ui_windows.sfui` replays #95 (pen taps on brushes, out of+range and back, a second brush, back to the canvas) for Windows Ink, a Huion-style pen + mouse stream, SDL's stuck+pen state and Wintab-style mouse input; `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 straydiff --git a/src/app/App.cpp b/src/app/App.cppindex 276596e7..84e9e8f0 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -99,6 +99,8 @@ bool App::init(const AppOptions& options) {// UI tests run in a hidden window that never has focus; SDL drops joystick input without focus otherwise, and// the scripted SpaceMouse is a virtual joystick (UiTest.cpp).if (!options.uiTest.empty()) SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");+ // The pen tip's mouse events come from the app, not SDL (App::penMouseEvents, #95).+ SDL_SetHint(SDL_HINT_PEN_MOUSE_EVENTS, "0");if (!headless && options.demo.empty()) applyCachedThreadCount(); // the hardware profile's threads, before any brush workif (!SDL_Init(SDL_INIT_VIDEO)) {SDL_Log("SDL_Init failed: %s", SDL_GetError());@@ -696,8 +698,22 @@ void App::redrawIn(double seconds) {redrawAt_ = redrawAt_ > 0.0 ? std::min(redrawAt_, at) : at;}+// With two screens the pointer often rests on the other one while the keyboard works here: no pointer events+// then, so nothing kept the frames coming and the app looked as if in the background until the pointer came back+// (Discord, Windows 11). Whether the app is in use follows the window's focus, never the pointer: while focused,+// it keeps drawing for a while after any input (keys included) and while a key is held.+bool App::focusKeepsDrawing(double now) const {+ if (!windowFocused_) return false;+ if (now - lastInputAt_ < kFocusedActiveSeconds) return true;+ int n = 0;+ const bool* keys = SDL_GetKeyboardState(&n);+ for (int i = 0; keys && i < n; ++i)+ if (keys[i]) return true;+ return false;+}+bool App::wantsFrames(double now) const {- return now < activeUntil_ || drag_ != Drag::None || doc_.sculptor.strokeActive() ||+ return now < activeUntil_ || focusKeepsDrawing(now) || drag_ != Drag::None || doc_.sculptor.strokeActive() ||!dabQueue_.empty() || // a stroke's dabs still waiting (drainStrokeDabs) are placed a few each framestrokeFinishing_ || solverBusy() || // the solver thread is placing a stroke's dabs (AsyncStroke.cpp)backgroundBusy() || playingLayer_ >= 0 || faceTakePlaying_ ||@@ -1135,8 +1151,20 @@ 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+ if (dropPenMouseEvent(event)) return; // the pen's mouse events are made here, not by SDL (#95, Tablet.cpp)+ penMouseEvents(event, true); // a pen leaving lets go of the tip's button first+ dispatchEvent(event);+ penMouseEvents(event, false); // the tip's mouse events, after the pen's own+}++void App::dispatchEvent(const SDL_Event& event) {+ if (strayMouseMotion(event)) return; // neither the canvas nor the UI follows a driver's stray mouse events+ if ((event.type >= SDL_EVENT_KEY_DOWN && event.type < SDL_EVENT_JOYSTICK_AXIS_MOTION &&+ event.type != SDL_EVENT_KEYMAP_CHANGED && event.type != SDL_EVENT_KEYBOARD_ADDED &&+ event.type != SDL_EVENT_KEYBOARD_REMOVED && event.type != SDL_EVENT_MOUSE_ADDED &&+ event.type != SDL_EVENT_MOUSE_REMOVED) ||+ (event.type >= SDL_EVENT_PEN_PROXIMITY_IN && event.type <= SDL_EVENT_PEN_AXIS))+ lastInputAt_ = double(SDL_GetTicksNS()) * 1e-9; // (focusKeepsDrawing)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@@ -1156,7 +1184,11 @@ void App::handleEvent(const SDL_Event& event) {case SDL_EVENT_JOYSTICK_BUTTON_DOWN:spaceMouseEvent(e); // a SpaceMouse (SpaceMouse.cpp)break;+ case SDL_EVENT_WINDOW_FOCUS_GAINED:+ windowFocused_ = true;+ break;case SDL_EVENT_WINDOW_FOCUS_LOST:+ windowFocused_ = false;if (radial_.hold != RadialHold::None) closeRadialMenu(); // its key or button's release won't come herespaceHeld_ = false; // Space let go in another window never reaches usbreak;@@ -1263,7 +1295,11 @@ void App::updateMouseCursor() {ImGuiIO& io = ImGui::GetIO();io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;const ImGuiMouseCursor ui = ImGui::GetMouseCursor();- cursorKind_ = viewportCursorWanted() ? kCursorCrosshair : ui == ImGuiMouseCursor_None || io.MouseDrawCursor ? kCursorHidden : int(ui);+ // (Mouse mode with the pen: the system's would sit where the pen is on the tablet, not at the cursor.)+ cursorKind_ = penSoftCursor() ? kCursorHidden+ : viewportCursorWanted() ? kCursorCrosshair+ : ui == ImGuiMouseCursor_None || io.MouseDrawCursor ? kCursorHidden+ : int(ui);if (cursorKind_ == cursorShown_) return; // (the shape on screen already: nothing for the window system to do)const bool wasHidden = cursorShown_ == kCursorHidden || cursorShown_ == -2;cursorShown_ = cursorKind_;diff --git a/src/app/App.h b/src/app/App.hindex 0d871b76..c7df434d 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -144,7 +144,8 @@ private:enum class Drag { None, Stroke, Orbit, Pan, Zoom, Roll, MaskCanvas, SubToolClick, Select, Gizmo, Guide, Sculptable, ZSphere, Insert, Capture,AirStroke, UvTool }; // AirStroke: a stroke begun off the model, waiting to reach it (#62)- void handleEvent(const SDL_Event& e);+ void handleEvent(const SDL_Event& e); // the pen's mouse events first (penMouseEvents), then dispatchEvent+ void dispatchEvent(const SDL_Event& e);void onMouseDown(float x, float y, Uint8 button);void onMouseMove(float x, float y, float dx, float dy, bool fromPen);void onMouseUp();@@ -1765,6 +1766,10 @@ private:// ---- end ZBrush feel ----// ---- Redraw on demand (App.cpp run()) ----bool wantsFrames(double now) const; // something animates or runs: keep drawing+ bool focusKeepsDrawing(double now) const; // the window has focus and was just used (not: the pointer is over it)+ static constexpr double kFocusedActiveSeconds = 2.0;+ bool windowFocused_ = false; // keyboard focus (SDL_EVENT_WINDOW_FOCUS_GAINED / _LOST)+ double lastInputAt_ = -1e9; // seconds (SDL clock) of the last key, mouse or pen eventuint64_t frameKey() const; // changes when a poller changed what the window showsdouble activeUntil_ = 0.0; // seconds: keep drawing until then (after input: hover, tooltips)double redrawAt_ = 0.0; // seconds (SDL clock): draw one frame then, asleep or not (0: none)@@ -1772,12 +1777,15 @@ public:void redrawIn(double seconds); // something on screen changes by itself later (an error colour fading)private:// ---- Tablet (Tablet.cpp, core/Tablet.h, docs/tablet.md) ----- // SDL turns pen tip contact into left mouse button events (SDL_PEN_MOUSEID); the pen's own events- // add pressure, tilt, the eraser end and the side buttons, which are mapped in Preferences > Tablet.- bool dropPenMouseEvent(const SDL_Event& e) const; // SDL's version-dependent clicks for pen buttons- bool strayMouseMotion(const SDL_Event& e) const; // a driver's mouse motion beside the pen's (Tablet.cpp)+ // The pen tip is a left mouse button (SDL_PEN_MOUSEID) for the canvas and the UI. The app makes those mouse+ // events from the pen's own (penMouseEvents), not SDL (#95); the pen's events add pressure, tilt, the eraser+ // end and the side buttons, which are mapped in Preferences > Tablet.+ bool dropPenMouseEvent(const SDL_Event& e) const; // SDL's own mouse events for the pen (Tablet.cpp)+ void penMouseEvents(const SDL_Event& pen, bool before); // the pen tip's mouse events, made from the pen's+ void sendPenMouse(SDL_EventType type, float x, float y, SDL_WindowID window);+ bool strayMouseMotion(const SDL_Event& e) const; // a driver's mouse events beside the pen's (Tablet.cpp)void keepImGuiOnPen(); // after ImGui_ImplSDL3_NewFrame (Tablet.cpp)- bool systemCursorInWindow(float& x, float& y) const; // mouse mode (#72): the system cursor, window pixels+ bool penSoftCursor() const; // mouse mode with the pen: the arrow is drawn at the brush (Tablet.cpp)void mapPenPosition(SDL_Event& e); // mouse mode: the pen moves the cursor relatively (Tablet.cpp)// The system cursor (#73): ImGui's (resize handles, text fields, links) over the panels, a crosshair over the// 3D view where a brush acts (Preferences > Interface > Viewport cursor). Called after each ImGui::Render.@@ -1811,8 +1819,10 @@ private:bool wintabTip_ = false;Uint64 wintabNs_ = 0; // the last Wintab packetbool 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;+ bool penMouseSending_ = false; // dispatching a mouse event made from the pen (not SDL's)+ bool penMouseDown_ = false; // the pen tip's left button is down (as made by penMouseEvents)+ SDL_PenID penMouseOwner_ = 0; // the pen holding it+ float penMouseX_ = 0.0f, penMouseY_ = 0.0f; // the pen's last position (window pixels, before mouse mode)PenCursor penCursor_; // where the pen puts the cursor (Preferences > Tablet > Mouse mode)bool penInProximity_ = false, penTouching_ = false, penEraser_ = false;Uint64 lastPenNs_ = 0; // when the last pen event arrived (see onMouseMove)diff --git a/src/app/Demo.h b/src/app/Demo.hindex 8d9839e9..3815a99f 100644--- a/src/app/Demo.h+++ b/src/app/Demo.h@@ -170,6 +170,11 @@ private: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;+ // The pen's mouse events as SDL 3.2 makes them (true), or none: SDL after it dropped a touching pen (#95,+ // sdl_pen_mouse / pen_lost). A Huion-style driver also sends mouse events of its own (pen_stream huion).+ bool sdlPenMouse_ = true;+ bool huionStream_ = false;+ static constexpr SDL_MouseID kDriverMouse = 7; // the driver's own mouse device// `snapshot` saves the active mesh; moved* values compare against it. `remember` keeps values for $NAME.std::vector<glm::vec3> basePositions_, baseNormals_;std::map<std::string, double> remembered_;diff --git a/src/app/Tablet.cpp b/src/app/Tablet.cppindex 673b0ecd..6b0a4f5a 100644--- a/src/app/Tablet.cpp+++ b/src/app/Tablet.cpp@@ -31,72 +31,132 @@ constexpr const char* kButtonNames[kPenButtons] = {"Lower button", "Upper button} // namespace-// SDL also turns pen side buttons into mouse clicks, but which button depends on the SDL version-// (3.2.10: middle / right and only while touching; 3.2.12 and later: right / middle, also while hovering).-// The mapping in Preferences > Tablet decides instead (see penButton).+// The pen tip works the panels and the canvas as a left mouse button (SDL_PEN_MOUSEID). SDL 3.2 makes those mouse+// events itself, but keeps one "a pen is touching" flag for all pens: on Windows, a pen SDL drops while it touches+// (WM_POINTERCAPTURECHANGED, e.g. when a drag captures the mouse) leaves the flag set, and from then on no pen gets+// a mouse event, while its own events still come. The pen sculpted once, then could neither pick a second brush nor+// sculpt again (#95, Huion Kamvas, display tablets, Wacom with Windows Ink on or off). So SDL's pen mouse events are+// off (SDL_HINT_PEN_MOUSE_EVENTS) and ignored should a system SDL send them anyway, its clicks for the side buttons+// included (those depend on the SDL version; Preferences > Tablet maps the buttons, see penButton). penMouseEvents+// makes the tip's events from the pen's own, in SDL's order, and lets go of the button whenever the pen goes.bool App::dropPenMouseEvent(const SDL_Event& e) const {- return (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN || e.type == SDL_EVENT_MOUSE_BUTTON_UP) &&- e.button.which == SDL_PEN_MOUSEID && e.button.button != SDL_BUTTON_LEFT;+ if (penMouseSending_) return false;+ switch (e.type) {+ case SDL_EVENT_MOUSE_MOTION: return e.motion.which == SDL_PEN_MOUSEID;+ case SDL_EVENT_MOUSE_BUTTON_DOWN:+ case SDL_EVENT_MOUSE_BUTTON_UP: return e.button.which == SDL_PEN_MOUSEID;+ case SDL_EVENT_MOUSE_WHEEL: return e.wheel.which == SDL_PEN_MOUSEID;+ default: return false;+ }+}++void App::sendPenMouse(SDL_EventType type, float x, float y, SDL_WindowID window) {+ SDL_Event m{};+ m.type = type;+ if (type == SDL_EVENT_MOUSE_MOTION) {+ m.motion.windowID = window;+ m.motion.which = SDL_PEN_MOUSEID;+ m.motion.state = penMouseDown_ ? SDL_BUTTON_LMASK : 0;+ m.motion.x = x;+ m.motion.y = y;+ m.motion.xrel = x - penMouseX_;+ m.motion.yrel = y - penMouseY_;+ } else {+ m.button.windowID = window;+ m.button.which = SDL_PEN_MOUSEID;+ m.button.button = SDL_BUTTON_LEFT;+ m.button.down = type == SDL_EVENT_MOUSE_BUTTON_DOWN;+ m.button.clicks = 1;+ m.button.x = x;+ m.button.y = y;+ }+ penMouseX_ = x;+ penMouseY_ = y;+ penMouseSending_ = true;+ dispatchEvent(m);+ penMouseSending_ = false;}-// Windows Ink and some tablet drivers (Ugee on X11, #7) send mouse events of their own beside the pen's, often at an-// old position (where the pen last touched). Following those made the brush ring flick between two places, the view-// shake, and the UI see the cursor jump: a click landed somewhere else, a drag on the Move button threw the model-// off the canvas (#13). So while the pen is in range (heard from in the last second) only the pen moves the cursor,-// for the canvas and the UI alike.+// before: called ahead of the pen event (a pen leaving, or coming back without having left: let go first);+// otherwise after it, as SDL orders them (the pen's touch, then the mouse's button).+void App::penMouseEvents(const SDL_Event& e, bool before) {+ if (before) {+ if ((e.type == SDL_EVENT_PEN_PROXIMITY_OUT || e.type == SDL_EVENT_PEN_PROXIMITY_IN) && penMouseDown_) {+ penMouseDown_ = false;+ sendPenMouse(SDL_EVENT_MOUSE_BUTTON_UP, penMouseX_, penMouseY_, e.pproximity.windowID);+ }+ return;+ }+ switch (e.type) {+ case SDL_EVENT_PEN_DOWN:+ if (penMouseDown_) break; // another pen already touches+ sendPenMouse(SDL_EVENT_MOUSE_MOTION, e.ptouch.x, e.ptouch.y, e.ptouch.windowID);+ penMouseDown_ = true;+ penMouseOwner_ = e.ptouch.which;+ sendPenMouse(SDL_EVENT_MOUSE_BUTTON_DOWN, e.ptouch.x, e.ptouch.y, e.ptouch.windowID);+ break;+ case SDL_EVENT_PEN_UP:+ if (!penMouseDown_ || e.ptouch.which != penMouseOwner_) break;+ penMouseDown_ = false;+ sendPenMouse(SDL_EVENT_MOUSE_BUTTON_UP, e.ptouch.x, e.ptouch.y, e.ptouch.windowID);+ break;+ case SDL_EVENT_PEN_MOTION:+ if (penMouseDown_ && e.pmotion.which != penMouseOwner_) break;+ sendPenMouse(SDL_EVENT_MOUSE_MOTION, e.pmotion.x, e.pmotion.y, e.pmotion.windowID);+ if (!penMouseDown_) {+ // A hovering pen presses nothing. Should the UI still hold a left button (a driver's press whose+ // release never came), let go of it: the pen's next tap is a click again.+ ImGuiIO& io = ImGui::GetIO();+ if (io.MouseDown[ImGuiMouseButton_Left] && !(SDL_GetGlobalMouseState(nullptr, nullptr) & SDL_BUTTON_LMASK))+ io.AddMouseButtonEvent(ImGuiMouseButton_Left, false);+ }+ break;+ default:+ break;+ }+}++// Windows Ink and some tablet drivers (Ugee on X11, #7; Huion) send mouse events of their own beside the pen's, often+// at an old position (where the pen last touched). Following those made the brush ring flick between two places,+// the view shake, and the UI see the cursor jump: a click landed somewhere else, a drag on the Move button threw the+// model off the canvas (#13). So while the pen is in range (heard from in the last second) only the pen moves the+// cursor, for the canvas and the UI alike. The left button too: a driver's own press beside the pen's, whose+// release may never come, left the panels holding a button, and the pen's next taps were not clicks (#95).bool App::strayMouseMotion(const SDL_Event& e) const {- if (e.type != SDL_EVENT_MOUSE_MOTION || e.motion.which == SDL_PEN_MOUSEID) return false;+ const bool motion = e.type == SDL_EVENT_MOUSE_MOTION && e.motion.which != SDL_PEN_MOUSEID;+ const bool left = (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN || e.type == SDL_EVENT_MOUSE_BUTTON_UP) &&+ e.button.which != SDL_PEN_MOUSEID && e.button.button == SDL_BUTTON_LEFT;+ if (!motion && !left) return false;if (dragFromPen_ && drag_ != Drag::None) return true;return penInProximity_ && SDL_GetTicksNS() - lastPenNs_ < 1000000000ull;}// ImGui's SDL backend also reads the system cursor every frame while no button is held (the cursor minus the window-// position, on Windows, macOS and X11). With a pen that cursor can be stale or elsewhere, so ImGui's mouse would-// jump between it and the pen each frame: put it back on the pen.+// position, on Windows, macOS and X11). With a pen that cursor can be stale or elsewhere (and in Mouse mode it is+// somewhere else by design), so ImGui's mouse would jump between it and the pen each frame: put it back on the pen.void App::keepImGuiOnPen() {- if (!penInProximity_ || SDL_GetTicksNS() - lastPenNs_ >= 1000000000ull) return;- ImGui::GetIO().AddMousePosEvent(mouseX_, mouseY_);-}--// A tablet in mouse mode (Preferences > Tablet): the pen reports where it is on the tablet, but the cursor moves by-// how far it moves, like a mouse. Every pen position, the pen's own events and SDL's mouse events for the pen, goes-// through penCursor_ before anything reads it (#54: lifting the pen and putting it down elsewhere made the cursor-// jump back). The mouse moving the cursor in between moves where the pen carries on from.-//-// #72: in mouse mode the driver moves the system cursor itself, so the system cursor is the one cursor, for the-// brush circle and the panels alike; each pen position is replaced by it (the pen adds pressure and tilt). A-// cursor of our own, moved by the pen's motion, drifted from the system's after the pen was lifted and put down-// elsewhere, and clicks landed at the brush instead of the arrow.-bool App::systemCursorInWindow(float& x, float& y) const {- if (demo_) { // UI tests: the system cursor a script set (os_cursor), or none- if (!testSystemCursor_) return false;- x = testSystemCursorX_;- y = testSystemCursorY_;- return true;- }- if (!window_) return false;- float gx = 0.0f, gy = 0.0f;- int wx = 0, wy = 0;- SDL_GetGlobalMouseState(&gx, &gy);- if (!SDL_GetWindowPosition(window_, &wx, &wy)) return false;- x = gx - float(wx);- y = gy - float(wy);- return std::isfinite(x) && std::isfinite(y);+ ImGuiIO& io = ImGui::GetIO();+ const bool soft = penSoftCursor();+ io.MouseDrawCursor = soft && !viewportCursorWanted(); // over the view the brush ring is the cursor+ if (!soft && (!penInProximity_ || SDL_GetTicksNS() - lastPenNs_ >= 1000000000ull)) return;+ io.AddMousePosEvent(mouseX_, mouseY_);}+// Mouse mode with the pen in range: the system arrow is wherever the driver puts it (where the pen is on the+// tablet, with the driver in pen mode), not where the pen's relative movement took the cursor. It is hidden and+// the panels draw their arrow at the cursor instead, so the arrow, the brush and the clicks agree (#72). (A pen+// resting still in range sends nothing, so this lasts until it leaves or the mouse moves, not a second.)+bool App::penSoftCursor() const { return uiPrefs_.tablet.mouseMode && penInProximity_; }++// Mouse mode (Preferences > Tablet): the pen reports where it is on the tablet, but the cursor moves by how far it+// moves, like a mouse: lifting it and putting it down elsewhere leaves the cursor where it was (#54). Every pen+// position, the pen's own events and its mouse events, goes through penCursor_ before anything reads it, and+// the mouse moving the cursor in between moves where the pen carries on from. Only the pen's movement counts, not+// the system cursor: with the tablet driver in pen mode that is where the pen is, which made Mouse mode the same as+// Pen mode (#72, 0.10.8); the arrow follows the cursor instead (penSoftCursor).void App::mapPenPosition(SDL_Event& e) {penCursor_.mouseMode = uiPrefs_.tablet.mouseMode;const Uint64 now = SDL_GetTicksNS();- if (penCursor_.mouseMode) {- float sx = 0.0f, sy = 0.0f;- const bool penPosition = e.type == SDL_EVENT_PEN_DOWN || e.type == SDL_EVENT_PEN_UP || e.type == SDL_EVENT_PEN_MOTION ||- e.type == SDL_EVENT_PEN_AXIS || e.type == SDL_EVENT_PEN_BUTTON_DOWN ||- e.type == SDL_EVENT_PEN_BUTTON_UP ||- ((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 (penPosition && systemCursorInWindow(sx, sy)) penCursor_.systemCursor(sx, sy);- }switch (e.type) {case SDL_EVENT_PEN_PROXIMITY_IN:case SDL_EVENT_PEN_PROXIMITY_OUT: penCursor_.proximity(); break;diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex 95179ad5..d189d24b 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -90,7 +90,7 @@// symmetry_pivot_x (local symmetry's centre), timeline_top (1: the strip at the top),// timeline_y / axis_gizmo_y (their tops / centre, fractions of the canvas from its top),// style_window_min (ImGui's minimum window height, unscaled px: 32 in every look),-// tablet_mouse_mode (Preferences > Tablet > Mouse mode), os_cursor_x / os_cursor_y,+// tablet_mouse_mode (Preferences > Tablet > Mouse mode),// ui_mouse_x / ui_mouse_y (where ImGui sees the pointer), backface_culling, drawn_triangles,// viewport_cursor (the system cursor: 0 arrow, 1 text, 7 hand, 100 crosshair; #73),// smooth_keep_volume (Brush > Smooth keeps volume), stroke_keep_volume (1 when Smooth@@ -169,13 +169,15 @@// STATUS (e.g. phone_request POST /api/ar 200 {"scope":"subtool","size":20})// (tools/guide/update_shortcuts.py puts it into docs/guide/shortcuts.md)//-// Simulated graphics tablet: real SDL3 pen events, each followed by the mouse event SDL synthesizes-// from it (SDL_PEN_MOUSEID), in SDL's order. The touch comes before its first pressure sample, as+// Simulated graphics tablet: real SDL3 pen events, each followed by the mouse event SDL 3.2 synthesizes+// from it (SDL_PEN_MOUSEID), in SDL's order (the app ignores those and makes its own, #95). The touch comes before its first pressure sample, as// with Windows Ink, and every frame carries 4 pen samples (a 240 Hz pen at 60 fps).// pen_stroke U0 V0 U1 V1 P0 P1 [eraser] [leave] [S] pen stroke across the model with pressure P0 -> P1;// eraser: the eraser end; leave: the pen leaves proximity before lifting-// pen_drag_item LABEL DX DY P0 P1 [stray X Y] [S] pen stroke through the item's centre, DX DY pixels long (a-// short one is a tap with a little jitter); stray: as for pen_drag+// pen_drag_item LABEL DX DY P0 P1 [stray X Y] [lost] [S] pen stroke through the item's centre, DX DY pixels long+// (a short one is a tap with a little jitter); stray: as for pen_drag; lost: SDL drops+// the pen while it touches (out of proximity without lifting, then sdl_pen_mouse stuck+// and a new pen id, as SDL 3.2 on Windows after WM_POINTERCAPTURECHANGED)// pen_button N down|up side button N (1 lower, 2 upper, 3 third) with SDL 3.4's synthesized click// pen_hover U V [S] move the hovering pen to a point on the model (drags the view while a// button mapped to rotate / pan / right-click is held)@@ -190,7 +192,15 @@// 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+// sdl_pen_mouse on|stuck SDL's mouse events for the pen: on (SDL 3.2's), stuck (none, as after SDL dropped a+// touching pen on Windows, #95; the app makes its own)+// pen_lost SDL drops the pen while it touches (WM_POINTERCAPTURECHANGED): it leaves proximity+// without lifting, and SDL's pen mouse events stop (sdl_pen_mouse stuck)+// pen_stream ink|huion ink: the pen's events (and SDL's); huion: the driver also sends mouse events of its own+// beside the pen's, a press at each touch whose release never comes+// window_focus 1|0 the window gains or loses keyboard focus; mouse_leave: the pointer leaves the window+// (another screen); values: focus_frames (1 while focus keeps it drawing),+// pen_mouse_down, ui_left_down (the panels hold the left button), soft_cursor// 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//@@ -742,8 +752,10 @@ double Demo::query(const std::string& name, bool& known) const {return double(app_.renderer_.lastDrawnTriangles());}if (name == "viewport_cursor") return app_.cursorKind_; // the system cursor: 0 arrow, 100 crosshair (#73)- if (name == "os_cursor_x") return app_.testSystemCursorX_;- if (name == "os_cursor_y") return app_.testSystemCursorY_;+ if (name == "focus_frames") return app_.focusKeepsDrawing(double(SDL_GetTicksNS()) * 1e-9) ? 1.0 : 0.0;+ if (name == "pen_mouse_down") return app_.penMouseDown_ ? 1.0 : 0.0;+ if (name == "ui_left_down") return ImGui::IsMouseDown(ImGuiMouseButton_Left) ? 1.0 : 0.0;+ if (name == "soft_cursor") return app_.penSoftCursor() ? 1.0 : 0.0; // Mouse mode: the app draws the arrowif (name == "ui_mouse_x") return ImGui::GetIO().MousePos.x; // where the panels see the pointerif (name == "ui_mouse_y") return ImGui::GetIO().MousePos.y;if (name == "cursor_x") return app_.mouseX_;@@ -881,6 +893,7 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& eauto strayOption = [&](size_t from, float& seconds) -> std::function<void()> {std::function<void()> stray;for (size_t i = from; i < w.size(); ++i) {+ if (w[i] == "lost") continue; // (pen_drag_item's)if (w[i] == "stray" && i + 2 < w.size()) {stray = [this, sx = num(i + 1, 0), sy = num(i + 2, 0)] {int ww = 0, wh = 0;@@ -1794,8 +1807,13 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& eif (t > 3.0f) fail(line, notFound(label));return t > 3.0f;});+ const bool lost = std::find(w.begin() + 6, w.end(), "lost") != w.end();penStroke([centre, dx, dy](float u) { return ImVec2(centre->x + dx * (u - 0.5f), centre->y + dy * (u - 0.5f)); }, p0, p1,- false, false, seconds, strayEvent);+ false, lost, seconds, strayEvent);+ if (lost) run([this] { // SDL dropped the touching pen (#95): its mouse events stop; it comes back as a new pen+ sdlPenMouse_ = false;+ ++testPen_;+ });wait(0.1f);} else if (verb == "pen_button") {if (!need(2)) return false;@@ -1856,19 +1874,35 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& eif (!penIn_) penProximity(true);penPressure(p);});- } else if (verb == "os_cursor") {- // Where the system cursor is, as a tablet driver in mouse mode moves it (#72): `os_cursor U V` on the model,- // `os_cursor off` (the default) for a system that can't say.+ } else if (verb == "sdl_pen_mouse") {if (!need(1)) return false;- const bool off = w[1] == "off";- if (!off && !need(2)) return false;- run([this, off, u = off ? 0.0f : num(1, 0.5f), v = off ? 0.0f : num(2, 0.5f)] {- app_.testSystemCursor_ = !off;- if (off) return;- const ImVec2 p = onModel(u, v);- app_.testSystemCursorX_ = p.x;- app_.testSystemCursorY_ = p.y;+ run([this, on = w[1] != "stuck"] { sdlPenMouse_ = on; });+ } else if (verb == "pen_lost") {+ run([this] {+ penProximity(false); // no pen up: SDL's "touching" flag stays set+ sdlPenMouse_ = false;+ ++testPen_; // SDL adds it again at its next proximity, with a new id+ });+ wait(0.05f);+ } else if (verb == "pen_stream") {+ if (!need(1)) return false;+ run([this, huion = w[1] == "huion"] { huionStream_ = huion; });+ } else if (verb == "window_focus") {+ if (!need(1)) return false;+ run([this, in = num(1, 1) != 0.0f] {+ SDL_Event e{};+ e.type = in ? SDL_EVENT_WINDOW_FOCUS_GAINED : SDL_EVENT_WINDOW_FOCUS_LOST;+ e.window.windowID = SDL_GetWindowID(app_.window_);+ app_.handleEvent(e);+ });+ } else if (verb == "mouse_leave") {+ run([this] {+ SDL_Event e{};+ e.type = SDL_EVENT_WINDOW_MOUSE_LEAVE;+ e.window.windowID = SDL_GetWindowID(app_.window_);+ app_.handleEvent(e);});+ wait(0.05f);} else if (verb == "mouse_move") {if (!need(2)) return false;run([this, u = num(1, 0.5f), v = num(2, 0.5f)] {@@ -2033,7 +2067,8 @@ void Demo::penMove(float x, float y) {e.pmotion.y = y;penAt_ = ImVec2(x, y);app_.handleEvent(e);- mouseMotion(x, y, SDL_PEN_MOUSEID); // SDL moves the mouse with a touching or hovering pen+ if (sdlPenMouse_) mouseMotion(x, y, SDL_PEN_MOUSEID); // SDL moves the mouse with a touching or hovering pen+ if (huionStream_) mouseMotion(x, y, kDriverMouse); // ... and the driver moves it too}void Demo::penPressure(float pressure) {@@ -2064,8 +2099,11 @@ void Demo::penTouch(bool down, bool eraser) {e.ptouch.eraser = eraser;e.ptouch.down = down;app_.handleEvent(e);- if (down) mouseMotion(penAt_.x, penAt_.y, SDL_PEN_MOUSEID); // SDL_SendPenTouch: motion, then the left button- mouseButton(down, SDL_BUTTON_LEFT, SDL_PEN_MOUSEID);+ if (sdlPenMouse_) {+ if (down) mouseMotion(penAt_.x, penAt_.y, SDL_PEN_MOUSEID); // SDL_SendPenTouch: motion, then the left button+ mouseButton(down, SDL_BUTTON_LEFT, SDL_PEN_MOUSEID);+ }+ if (huionStream_ && down) mouseButton(true, SDL_BUTTON_LEFT, kDriverMouse); // its release never comes}void Demo::penButtonEvent(int button, bool down) {@@ -2083,7 +2121,7 @@ void Demo::penButtonEvent(int button, bool down) {app_.handleEvent(e);// SDL 3.4 also clicks a mouse button for it (1: right, 2: middle ...); the app must ignore that.static const int kMouse[] = {SDL_BUTTON_LEFT, SDL_BUTTON_RIGHT, SDL_BUTTON_MIDDLE, SDL_BUTTON_X1, SDL_BUTTON_X2};- if (button >= 1 && button < 5) mouseButton(down, kMouse[button], SDL_PEN_MOUSEID);+ if (sdlPenMouse_ && button >= 1 && button < 5) mouseButton(down, kMouse[button], SDL_PEN_MOUSEID);}void Demo::penStroke(std::function<ImVec2(float u)> path, float p0, float p1, bool eraser, bool leave, float seconds,diff --git a/src/core/Tablet.cpp b/src/core/Tablet.cppindex e17e7bf3..56a8f51c 100644--- a/src/core/Tablet.cpp+++ b/src/core/Tablet.cpp@@ -164,41 +164,9 @@ void PenCursor::cursorAt(float x, float y) {anchored_ = false;}-void PenCursor::systemCursor(float x, float y) {- if (!std::isfinite(x) || !std::isfinite(y)) return;- systemX_ = x;- systemY_ = y;- haveSystem_ = true;-}-void PenCursor::map(float& x, float& y, uint64_t nowNs) {if (!std::isfinite(x) || !std::isfinite(y)) return;- bool system = haveSystem_;- haveSystem_ = false; // (one reading per position)- if (mouseMode && system && !systemStale_) {- if (haveLast_) {- const bool penMoved = std::fabs(x - lastPenX_) + std::fabs(y - lastPenY_) > 2.0f;- const bool systemStill = std::fabs(systemX_ - lastSysX_) + std::fabs(systemY_ - lastSysY_) < 0.5f;- staleCount_ = penMoved && systemStill ? staleCount_ + 1 : (penMoved ? 0 : staleCount_);- if (staleCount_ >= 3) systemStale_ = true;- }- lastPenX_ = x;- lastPenY_ = y;- lastSysX_ = systemX_;- lastSysY_ = systemY_;- haveLast_ = true;- }- if (systemStale_) system = false;- if (mouseMode && system) {- // The system cursor is the one cursor; the pen only adds pressure and tilt. Should the system stop- // saying where it is, the pen carries on from here.- offsetX_ = systemX_ - x;- offsetY_ = systemY_ - y;- anchored_ = true;- lastNs_ = nowNs;- x = systemX_;- y = systemY_;- } else if (mouseMode) {+ if (mouseMode) {// A pen that comes back (into range, or after a pause) moves the cursor on from where it is.if (!anchored_ || nowNs - lastNs_ > kLiftedNs) {offsetX_ = haveCursor_ ? cursorX_ - x : 0.0f;diff --git a/src/core/Tablet.h b/src/core/Tablet.hindex 0513daf0..230d7d84 100644--- a/src/core/Tablet.h+++ b/src/core/Tablet.h@@ -129,11 +129,13 @@ private:// 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-// pen's place on the tablet even then, so following it made the cursor jump back (#54). The app passes every pen-// position (the pen's own events and SDL's mouse events for the pen) through map().+// Where the pen puts the cursor. In pen mode that is where the pen reports it is. In mouse 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. Tablet drivers report the pen's place on+// the tablet (Windows Ink even with the driver in its own mouse mode), so following it made the cursor jump back+// (#54), and following the system cursor made Mouse mode the same as Pen mode with the driver in pen mode (#72).+// Only the pen's movement counts. The app passes every pen position (the pen's own events and its mouse events)+// through map().class PenCursor {public:// A pen silent this long was lifted away (the driver may not say it left): the next position starts afresh.@@ -142,32 +144,16 @@ public:bool mouseMode = false;// The pen came into range or left it: in mouse mode its next position moves the cursor from where it is.- void proximity() {- anchored_ = false;- systemStale_ = false;- staleCount_ = 0;- }+ void proximity() { anchored_ = false; }// Something else (the mouse) moved the cursor to x, y.void cursorAt(float x, float y);- // Where the system cursor is (window pixels), read just before the next map(). In mouse mode the driver moves- // the system cursor itself, so that is the cursor: map() returns it for the brush and the panels alike (#72:- // a cursor of our own, moved by the pen's motion, drifted away from the system's after the pen was lifted and- // put down elsewhere, and clicks landed at the brush, not the arrow). Without one (a system that can't say),- // the pen's motion moves the cursor on from where it was.- void systemCursor(float x, float y);// A pen position (window pixels) at time nowNs -> the cursor's position, in place.void map(float& x, float& y, uint64_t nowNs);private:float offsetX_ = 0.0f, offsetY_ = 0.0f; // mouse mode: cursor = pen + offsetfloat cursorX_ = 0.0f, cursorY_ = 0.0f;- float systemX_ = 0.0f, systemY_ = 0.0f;- bool haveCursor_ = false, anchored_ = false, haveSystem_ = false;- // A system cursor that stays put while the pen moves (seen on Windows tablets in 0.10.7: the brush froze) is- // not the cursor: the pen moves the cursor relatively until it comes back into range.- float lastPenX_ = 0.0f, lastPenY_ = 0.0f, lastSysX_ = 0.0f, lastSysY_ = 0.0f;- bool haveLast_ = false, systemStale_ = false;- int staleCount_ = 0;+ bool haveCursor_ = false, anchored_ = false;uint64_t lastNs_ = 0;};diff --git a/tests/test_tablet.cpp b/tests/test_tablet.cppindex 0ba6a21a..c1bee5e7 100644--- a/tests/test_tablet.cpp+++ b/tests/test_tablet.cpp@@ -168,71 +168,41 @@ TEST(pen_cursor_mouse_mode_moves_relatively) {CHECK(near(x, 25.0f) && near(y, 10.0f));}-TEST(pen_cursor_mouse_mode_follows_the_system_cursor) {- // #72: in mouse mode the driver moves the system cursor; when the system says where it is, that is the cursor- // for every pen position (no cursor of our own to drift from it).+TEST(pen_cursor_mouse_mode_is_relative_with_an_absolute_driver) {+ // #72, drag-d's sequence (0.10.8: "no difference between default and mouse mode"): the tablet driver in pen+ // (absolute) mode reports the pen's place on the tablet. Mouse mode must still move the cursor by the pen's+ // movement only: hover, lift, put down elsewhere, hover again.PenCursor c;c.mouseMode = true;- c.cursorAt(400.0f, 300.0f);+ c.cursorAt(400.0f, 300.0f); // where the mouse left the cursorc.proximity();- float x = 100.0f, y = 50.0f;- c.systemCursor(400.0f, 300.0f);+ float x = 100.0f, y = 100.0f; // the pen comes into range at the tablet's top leftc.map(x, y, 1000);- CHECK(near(x, 400.0f) && near(y, 300.0f));- // The pen lifted, put down at another point, the driver's cursor moved elsewhere meanwhile: the system's.+ CHECK(near(x, 400.0f) && near(y, 300.0f)); // no jump to the pen's place+ for (int i = 1; i <= 10; ++i) { // it moves 100 px right and 50 px down+ x = 100.0f + 10.0f * float(i), y = 100.0f + 5.0f * float(i);+ c.map(x, y, 1000 + uint64_t(i) * 8000000);+ }+ CHECK(near(x, 500.0f) && near(y, 350.0f));+ // Lifted away and put down at the tablet's bottom right: the cursor stays, then moves on by the pen's motion.c.proximity();- x = 900.0f, y = 700.0f;- c.systemCursor(250.0f, 120.0f);- c.map(x, y, 2000);- CHECK(near(x, 250.0f) && near(y, 120.0f));- // The driver moves its cursor less (or more) than the pen moved on the tablet: still the system's.- x = 950.0f, y = 700.0f;- c.systemCursor(260.0f, 121.0f);- c.map(x, y, 3000);- CHECK(near(x, 260.0f) && near(y, 121.0f));- // One reading per position: without a new one the pen carries on from the last (a system that can't say).- x = 960.0f, y = 705.0f;- c.map(x, y, 4000);- CHECK(near(x, 270.0f) && near(y, 126.0f));- // Pen mode ignores the system cursor (#54's case: the driver's mouse events land slightly off the pen).+ x = 1800.0f, y = 1000.0f;+ c.map(x, y, 200000000);+ CHECK(near(x, 500.0f) && near(y, 350.0f));+ x = 1780.0f, y = 990.0f;+ c.map(x, y, 208000000);+ CHECK(near(x, 480.0f) && near(y, 340.0f));+ // Lifted without the driver saying so (silent for longer than kLiftedNs), put down elsewhere: no jump either.+ x = 300.0f, y = 200.0f;+ c.map(x, y, 208000000 + PenCursor::kLiftedNs + 1);+ CHECK(near(x, 480.0f) && near(y, 340.0f));+ // Pen mode: the pen's own place.c.mouseMode = false;x = 50.0f, y = 60.0f;- c.systemCursor(300.0f, 300.0f);- c.map(x, y, 5000);+ c.map(x, y, 900000000);CHECK(near(x, 50.0f) && near(y, 60.0f));}-TEST(pen_cursor_mouse_mode_ignores_a_frozen_system_cursor) {- // 0.10.7 on Windows (#72 follow-up): the system cursor reading stayed put while the pen moved, so the brush- // froze. After a few such positions the pen moves the cursor relatively, on from where it was, with no jump.- sf::PenCursor c;- c.mouseMode = true;- float x = 100.0f, y = 100.0f;- c.systemCursor(400.0f, 300.0f);- c.map(x, y, 1000);- CHECK(near(x, 400.0f) && near(y, 300.0f));- float px = 100.0f;- for (int i = 1; i <= 6; ++i) {- px += 10.0f;- x = px;- y = 100.0f;- c.systemCursor(400.0f, 300.0f); // frozen- c.map(x, y, 1000 + uint64_t(i) * 1000000);- }- CHECK(x > 420.0f); // moving again, by the pen's motion- CHECK(near(y, 300.0f));- // A moving system cursor is still followed (the usual case).- sf::PenCursor d;- d.mouseMode = true;- for (int i = 0; i < 6; ++i) {- x = 100.0f + 10.0f * float(i);- y = 100.0f;- d.systemCursor(400.0f + 7.0f * float(i), 300.0f);- d.map(x, y, uint64_t(i + 1) * 1000000);- CHECK(near(x, 400.0f + 7.0f * float(i)));- }-}-TEST(tablet_settings_mouse_mode_round_trip) {TabletSettings t;CHECK(!t.mouseMode);diff --git a/tests/ui/pen_ui_windows.sfui b/tests/ui/pen_ui_windows.sfuinew file mode 100644index 00000000..1df781e5--- /dev/null+++ b/tests/ui/pen_ui_windows.sfui@@ -0,0 +1,101 @@+# The pen stops clicking the UI (#95, Huion Kamvas Pro 24; display tablets; Wacom with Windows Ink on or off): the pen+# sculpts, then can't pick a second brush or go back to the canvas. SDL 3.2 makes the pen's mouse events but keeps+# one "a pen is touching" flag for every pen; on Windows a pen it drops while touching (WM_POINTERCAPTURECHANGED)+# leaves the flag set, and no pen gets mouse events after that. The app now makes the pen's mouse events from the+# pen's own and ignores SDL's. Each stream below: pen tap on a brush, lift, out of range and back, tap a second+# brush, sculpt on the canvas; nothing is left holding the panels' left button.+run new_sphere subdivisions=4+key f++# Windows Ink, as SDL reports it (the pen's events and SDL's mouse events for it).+pen_stream ink+pen_drag_item "Clay##brush.clay" 2 1 0.5 0.5+expect_value brush_type == 1+pen_out+pen_enter 0.5 0.5+pen_drag_item "Inflate##brush.inflate" 2 1 0.5 0.5+expect_value brush_type == 4+snapshot+pen_stroke 0.3 0.45 0.7 0.55 0.4 0.8+expect_value moved > 0+expect_value pen_mouse_down == 0+expect_value ui_left_down == 0+key ctrl+z++# The #95 sequence: SDL drops the pen while it touches a brush button (it leaves without lifting), comes back as a+# new pen, and SDL sends no mouse events for any pen from then on. The pen still picks a second brush and sculpts.+pen_drag_item "Clay##brush.clay" 2 1 0.5 0.5 lost+expect_value brush_type == 1+expect_value pen_mouse_down == 0+pen_enter 0.5 0.5+pen_hover 0.5 0.4+expect_value ui_left_down == 0+pen_drag_item "Standard##brush.standard" 2 1 0.5 0.5+expect_value brush_type == 0+pen_drag_item "Inflate##brush.inflate" 1 2 0.5 0.5+expect_value brush_type == 4+snapshot+pen_stroke 0.3 0.45 0.7 0.55 0.4 0.8+expect_value moved > 0+expect_value stroking == 0+key ctrl+z+# The same drop in the middle of a stroke on the canvas: the stroke ends (one undo step), the pen carries on.+snapshot+pen_stroke 0.3 0.6 0.7 0.6 0.5 0.5 leave+expect_value stroking == 0+expect_value moved > 0+key ctrl+z+pen_drag_item "Clay##brush.clay" 2 1 0.5 0.5+expect_value brush_type == 1+pen_out++# A Huion-style driver: beside the pen's events it sends mouse events of its own, a press at each touch whose+# release never comes. The panels must not be left holding the button, so the next pen tap is a click again.+sdl_pen_mouse on+pen_stream huion+pen_drag_item "Standard##brush.standard" 2 1 0.5 0.5+expect_value brush_type == 0+pen_out+pen_enter 0.5 0.5+pen_hover 0.5 0.45+expect_value ui_left_down == 0+pen_drag_item "Inflate##brush.inflate" 2 1 0.5 0.5+expect_value brush_type == 4+snapshot+pen_stroke 0.3 0.45 0.7 0.55 0.4 0.8+expect_value moved > 0+expect_value ui_left_down == 0+key ctrl+z+# ... with SDL's pen mouse events stuck as well.+pen_drag_item "Clay##brush.clay" 2 1 0.5 0.5 lost+pen_enter 0.5 0.5+pen_drag_item "Standard##brush.standard" 2 1 0.5 0.5+expect_value brush_type == 0+pen_out+pen_stream ink++# Windows Ink off (Wintab drivers): no pen events, the driver moves the mouse. Switching between it and the pen+# works both ways.+click "Clay##brush.clay"+expect_value brush_type == 1+snapshot+stroke 0.3 0.45 0.7 0.55+expect_value moved > 0+key ctrl+z+pen_enter 0.5 0.5+pen_drag_item "Inflate##brush.inflate" 2 1 0.5 0.5+expect_value brush_type == 4+pen_out+click "Standard##brush.standard"+expect_value brush_type == 0++# Two screens (Discord, Windows 11): the pointer on the other screen doesn't put the app in the background. While+# the window has focus, using it keeps the frames coming, wherever the pointer is; losing focus lets it rest.+window_focus 1+mouse_leave+key escape+expect_value focus_frames == 1+mouse_leave+expect_value focus_frames == 1+window_focus 0+expect_value focus_frames == 0diff --git a/tests/ui/tablet.sfui b/tests/ui/tablet.sfuiindex 1706694d..616870b9 100644--- a/tests/ui/tablet.sfui+++ b/tests/ui/tablet.sfui@@ -172,38 +172,37 @@ remember cursor_x mouse_xpen_enter 0.9 0.1expect_value cursor_x >= $mouse_x-0.5expect_value cursor_x <= $mouse_x+0.5-# #72: the driver moves the system cursor itself, and that is the one cursor for the brush and the panels. The pen-# lifted, the cursor moved elsewhere, the pen put down at another point: brush, panels and system cursor agree.+# #72 (drag-d, driver in pen mode, 0.10.8: "no difference between default and mouse mode"): the pen reports its+# place on the tablet, yet the cursor moves only by the pen's movement. The pen lifted and put down elsewhere: no+# jump; it moves on from there, the panels see the pointer at the brush, and they draw the arrow there (the system+# arrow, at the pen's place on the tablet, is hidden).pen_out-os_cursor 0.3 0.6-remember os_cursor_x os_x-remember os_cursor_y os_y-pen_enter 0.85 0.2-expect_value cursor_x >= $os_x-1.5-expect_value cursor_x <= $os_x+1.5-expect_value cursor_y >= $os_y-1.5-expect_value cursor_y <= $os_y+1.5-expect_value ui_mouse_x >= $os_x-1.5-expect_value ui_mouse_x <= $os_x+1.5-os_cursor 0.4 0.55-remember os_cursor_x os_x-remember os_cursor_y os_y-pen_hover 0.9 0.15-expect_value cursor_x >= $os_x-1.5-expect_value cursor_x <= $os_x+1.5-expect_value ui_mouse_y >= $os_y-1.5-expect_value ui_mouse_y <= $os_y+1.5+pen_enter 0.3 0.6+remember cursor_x a_x+remember cursor_y a_y+pen_hover 0.4 0.6+expect_value cursor_x > $a_x+5+remember cursor_x b_x+remember cursor_y b_y+pen_out+pen_enter 0.9 0.15+expect_value cursor_x >= $b_x-0.5+expect_value cursor_x <= $b_x+0.5+expect_value cursor_y >= $b_y-0.5+expect_value cursor_y <= $b_y+0.5+expect_value ui_mouse_x >= $b_x-0.5+expect_value ui_mouse_x <= $b_x+0.5+expect_value ui_mouse_y >= $b_y-0.5+expect_value ui_mouse_y <= $b_y+0.5+pen_hover 0.8 0.15+expect_value cursor_x < $b_x-5+expect_value cursor_y >= $b_y-1.5+expect_value cursor_y <= $b_y+1.5+wait 0.1+expect_value soft_cursor == 1pen_out-mouse_move 0.65 0.35-os_cursor 0.65 0.35-remember os_cursor_x os_x-remember os_cursor_y os_y-pen_enter 0.1 0.9-expect_value cursor_x >= $os_x-1.5-expect_value cursor_x <= $os_x+1.5-expect_value ui_mouse_x >= $os_x-1.5-expect_value ui_mouse_x <= $os_x+1.5-os_cursor off+wait 0.1+expect_value soft_cursor == 0# Pen mode again: the cursor is where the pen is.pen_outclick "Preferences##palette.preferences"