Skip to content

cvcGL: SDL3 owns the onscreen window, one input path on every platform - #829

Merged
transfix merged 3 commits into
masterfrom
feat/sdl3-window
Oct 29, 2026
Merged

transfix merged 3 commits into
masterfrom
feat/sdl3-window

Conversation

@transfix

@transfix transfix commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Why

Keyboard and mouse input should reach a document the same way on every platform. Until now SDL3 wrapped VTK's window on Windows, listened on the page in the browser, and on X11, Wayland and macOS arigl fell back to VTK's key events. SDL can't wrap a foreign window cleanly on those: on X11 its XInput2 selection takes VTK's clicks, and on Wayland it opens a separate toplevel.

So SDL3 now owns the onscreen window. SDL creates the window and its OpenGL context, VTK renders into that context, and SDL's events drive VTK's interactor. The camera, ImGuiOverlay and anything else observing the interactor still see ordinary VTK events, and SdlInput gets the same events in SDL's names.

What's here

  • src/cvcGL/SdlWindow.{h,cpp}, modelled on VTK's old SDL2 pair; VTK has no SDL3 classes.
    • SdlGLRenderWindow, a vtkOpenGLRenderWindow:
      • creates an SDL window (OpenGL, resizable, high pixel density) with a 3.2 core context, loads GL through SDL_GL_GetProcAddress and swaps with SDL_GL_SwapWindow;
      • keeps VTK's size in drawable pixels, as VTK's own windows do (on macOS and scaled Wayland outputs the window is that many times smaller in points); title, position, cursor show/hide/warp/shape, fullscreen and screen size go through SDL;
      • leaves the process as VTK's windows did unless the app set the hint: the screensaver, signal handlers (Ctrl+C), X11's compositor and the Windows DPI awareness stay as they were;
      • if SDL can open no window or GL context, stays empty and never touches GL.
    • SdlRenderWindowInteractor:
      • drains SDL's queue into VTK events: X11 keysyms (the unshifted key's, with the character it types as the key code), text input as Latin-1 CharEvents, clicks at their pixel with y up, fractional wheel steps, resize, expose, enter/leave and close;
      • runs VTK timers on SDL timers;
      • routes events by window: SDL has one queue per process, so whichever interactor drains it keeps another cvcGL window's events for that window's interactor;
      • passes every event of its window to registered sinks.
  • ViewportManager makes onscreen windows SDL's when cvcGL has SDL3. $CVCGL_SDL_WINDOW=0 keeps VTK's own window, and so does SDL failing to open one (with a message on stderr). Offscreen windows (snapshots, the terminal scene, the golden images) stay VTK's.
  • SdlInput::attach subscribes to the window's interactor and no longer drains SDL's queue itself. The Win32 HWND wrap is gone. Under VTK's window it uses VTK's key events. An SdlInput that is not attached leaves SDL's queue alone while a cvcGL SDL window is open. ariadne_hello now attaches its SdlInput to the window, as arigl does.
  • CameraController recenters the pointer through vtkRenderWindow::SetCursorPosition. VTK's own X11 window has no SetCursorPosition, so under it the warp still goes through Xlib.
  • The clipboard uses SDL's while SDL's video is up, which covers Wayland.
  • cvcgl_sdl_attach is rewritten for the new window. It checks keysyms and key codes, CharEvents, clicks, SdlInput's copy of the events, resize, a VTK timer, routing between two windows, an unattached SdlInput, and the $CVCGL_SDL_WINDOW=0 fallback. On Windows it also checks a key message through SDL's window procedure.
  • CI builds cvcGL with SDL3 on the Linux, macOS and Windows package lanes. Before this, every lane configured "SDL3 not found", so none of this was compiled in CI. A configure that does not find SDL3 now fails, as one without the Ariadne terminal backends does.

Tested

  • Windows (MSVC 19.44, Release, VTK 9.5.0, SDL 3.4.12, cvcGL with ImGui and SDL):
    • cvcgl_sdl_attach and cvcgl_sdl_input pass.
    • 81 of the 86 cvcgl_* / arigl_* ctest tests pass, including cvcgl_imgui_*, cvcgl_ari_golden and cvcgl_ari_switch. Of the other five:
      • four arigl terminal tests skip because this build has no FTXUI;
      • cvcgl_fe_interval crashes in LBIE's mesher. It never opens a window, and a master build on the same machine crashes at the same point.
    • ariadne_runtime_test: 181/181.
    • The golden images are unchanged. They render offscreen, in VTK's window as before.
    • Live: arigl run fem_gallery.ari and ariadne_hello hello.ari open SDL's window, run, and exit 0 when the window is closed.
  • Linux (X11 on Xvfb with Mesa llvmpipe) and macOS: CI only. Wayland is not covered by CI.

Not done yet (phase 2)

  • The browser: SDL owning the canvas and its WebGL2 context, with the hidden SDL canvas and the page key listener removed. Until then the browser keeps VTK's canvas window and SdlInput's page listener.

Keyboard and mouse input reached a document a different way on each
platform: SDL3 wrapped VTK's HWND on Windows, listened on the page in the
browser, and on X11, Wayland and macOS arigl fell back to VTK's key events,
because SDL cannot take a foreign window cleanly there (on X11 its XInput2
selection takes VTK's clicks; on Wayland it opens a separate toplevel).

Now SDL makes the onscreen window and its OpenGL context, VTK renders into
that context, and SDL's events drive VTK's interactor. The camera,
ImGuiOverlay and anything else observing the interactor still see ordinary
VTK events, and SdlInput gets the same events in SDL's names.

src/cvcGL/SdlWindow.{h,cpp} are modelled on VTK's SDL2 pair; VTK has no
SDL3 classes. SdlGLRenderWindow is a vtkOpenGLRenderWindow on an SDL window
(OpenGL, resizable, high pixel density) with a 3.2 core context. It loads
GL through SDL_GL_GetProcAddress and swaps with SDL_GL_SwapWindow. Its size
is the drawable's, in pixels, as in VTK's own windows. Title, position,
cursor show/hide/warp/shape, fullscreen and screen size go through SDL.
SDL's video leaves the process as VTK's windows did: the screensaver,
signal handlers, X11's compositor and the Windows DPI awareness stay as
they were. A window SDL cannot open stays empty and never touches GL.

SdlRenderWindowInteractor drains SDL's queue into VTK events. Keys get X11
keysyms (the unshifted key's) and the character they type as the key code.
Text input becomes Latin-1 CharEvents, clicks land at their pixel with y
up, and wheel steps can be fractional. Resize, expose, enter/leave and
close are handled too. VTK timers run on SDL timers. SDL has one queue per
process, so an event of another cvcGL window is kept for that window's
interactor. Registered sinks see each event of the window first.

ViewportManager makes onscreen windows SDL's when cvcGL has SDL3.
$CVCGL_SDL_WINDOW=0 keeps VTK's own window, and so does SDL failing to
open one. Offscreen windows (snapshots, the terminal scene, the golden
images) stay VTK's.

SdlInput::attach subscribes to the window's interactor instead of wrapping
the Win32 HWND. Under VTK's window it reads VTK's key events, and the
browser keeps its page listener. poll() leaves SDL's queue alone while a
cvcGL SDL window is open, and shutdown() drops pending events.
ariadne_hello attaches its SdlInput to the window, as arigl does.

CameraController recenters the pointer with SetCursorPosition. VTK's own
X11 window has none, so under it the warp still goes through Xlib. The
clipboard is SDL's while SDL's video is up, which covers Wayland.

cvcgl_sdl_attach covers the new window: keysyms and key codes, CharEvents,
clicks, SdlInput's copy of the events, resize, a VTK timer, routing
between two windows, an unattached SdlInput, and the $CVCGL_SDL_WINDOW=0
fallback. On Windows it also sends a key message through SDL's window
procedure.

The browser keeps VTK's canvas window for now. SDL owning the canvas and
its WebGL2 context is the next step.
@transfix
transfix changed the base branch from examples/live-gallery to master October 28, 2026 19:41
@transfix
transfix marked this pull request as ready for review October 28, 2026 19:41
The package lanes configured cvcGL without SDL3 ("SDL3 not found"), so its
SDL window and SdlInput's window path were compiled nowhere in CI, and
cvcgl_sdl_attach skipped. Install sdl3 next to VTK and imgui, as the
shipped cvcgl recipe has it. A configure that does not find it now fails,
as one without the Ariadne terminal backends does. On Linux the window
opens on Xvfb with Mesa's llvmpipe.
The test exited 0 when it skipped (no SDL, or SDL opened no window), so
ctest printed "Passed" either way, and a CI lane that should exercise
cvcGL's SDL window could not be told from one that never opened it. It
now exits 77 on a skip, registered as its SKIP_RETURN_CODE, as
physics_replay_test does.
@transfix
transfix merged commit 8380876 into master Oct 29, 2026
24 checks passed
@transfix
transfix deleted the feat/sdl3-window branch October 29, 2026 20:53
transfix added a commit that referenced this pull request Oct 29, 2026
The project version and the cvcpkg recipe family move to 3.7.4. Every recipe is at cvc_revision 1
already, so none restarts.

3.7.4 carries the whole plant ledger on every plant (#876): plant_sample's vehicle
contact and what each plant measures, the dynamic plants' building and vehicle contacts and
attitude, nav_stats' vehicle_hits, cvc/nav/plant_ledger.h, and publish_nav_stats writing every
item, n/a where a plant does not measure it. demo3's Ledger screen (CVC-DBG/cvcdbg#256) reads it.
It also carries SDL3 owning cvcGL's onscreen window (#829), state_exec's state-incr, state-cas and
state-swap (#860), and Ariadne's window fitting (#828) and room relay test counting (#867).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant