113 Commits
Author SHA1 Message Date
Greg ShuflinandClaude Fable 5.1 8104955c71 Make the webcam demo run on macOS
Lean runs main on a helper thread, which Cocoa rejects for video init;
export LEAN_MAIN_USE_THREAD=0 from the justfile on macOS. Wait for the
camera permission event before reading the camera format, since SDL
grants access asynchronously.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-08 15:26:20 -07:00
Greg ShuflinandClaude Fable 5.1 2de47f0db9 Fix macOS link: use ld64 flags instead of GNU ld ones
ld64.lld rejects --allow-shlib-undefined and -rpath=$ORIGIN. Add a
macOS branch with -undefined dynamic_lookup and an @executable_path
rpath, and skip the Linux-only patchelf step in the justfile.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-08 15:26:20 -07:00
Greg Shuflin a54101b48e Gamepad code in SDL3 C code 2026-07-13 20:41:26 -07:00
Greg Shuflin ab65bd39e1 Don't need separate run/main 2026-07-08 23:00:18 -07:00
Greg Shuflin 06293f9fb8 Don't actually need partial here 2026-07-08 22:59:40 -07:00
Greg Shuflin 63b1fbec67 Add C SDL3 examples 2026-07-08 22:28:38 -07:00
Greg Shuflin 23bead30bb Remove unused code from webcam demo 2026-07-08 22:21:03 -07:00
Greg Shuflin ce97ad440e Update toolchain to lastest version of Lean 2026-07-08 22:07:04 -07:00
Greg Shuflin 21c9cad34b C refactor: separate out SDL_Rect integer extraction code 2026-07-01 16:58:02 -07:00
Greg ShuflinandClaude Opus 4.6 1830995cc5 Remove opaque Pixels type; add safe updateTexture and zero-copy updateTextureFromSurface
The opaque Pixels type wrapped a void* pointing into an SDL_Surface's
pixel buffer, but nothing tied the Pixels lifetime to the surface. If
the GC collected the surface first, the Pixels pointer would dangle,
causing use-after-free.

Replace with two safe alternatives:

- updateTexture now takes a ByteArray (Lean-owned, GC-managed buffer)
  plus pitch, matching how Haskell's sdl2 uses ByteString and Rust's
  rust-sdl2 uses &[u8]. This is faithful to SDL_UpdateTexture's general
  void* interface but requires copying pixels into a ByteArray first.

- updateTextureFromSurface is a new zero-copy convenience that takes an
  SDLSurface directly. The C side extracts both pixels and pitch from
  the surface internally. Because the surface is borrowed (@&) for the
  call's duration, the GC cannot collect it mid-use. This is not
  strictly faithful to SDL_UpdateTexture's C API, but is the common
  case and avoids an unnecessary memcpy.

Also removes: Pixels type, Pixels.nonemptyType, sdl_pixels_external_class,
sdl_Surface_get_pixels, and the pixels_external_class registration in
sdl_init.

WebcamApp.lean is updated to use updateTextureFromSurface.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:41:24 -07:00
Greg ShuflinandClaude Opus 4.6 3ec33e9a27 Fix mouse state packing bit-field overlap
The old packing was:
  X << 32 | Y << 16 | buttons
where X and Y were full 32-bit values. Y's upper 16 bits (bits 32-47)
overlapped with X (bits 32-63), and buttons (bits 0-31) overlapped with
Y (bits 16-47). This corrupted X whenever Y >= 65536, and corrupted Y
whenever buttons had any bits set above bit 15.

The Lean unpacking side already assumed a non-overlapping layout:
  X = bits 63:32, Y = bits 31:16 (masked to 16), buttons = bits 15:0

Now the C side explicitly masks Y and buttons to 16 bits before packing,
matching the Lean expectations. This means Y is limited to [-32768,
32767] which is adequate for screen coordinates and typical relative
mode deltas.

Also fix the SDLIO error type in sdl_set_relative_mouse_mode (same
lean_mk_io_user_error issue fixed in the earlier commit).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 e3dcc8da14 Fix camera and camera spec finalizers to free resources
sdl_camera_finalizer was a no-op, so SDL cameras opened with
SDL_OpenCamera were never closed. Now calls SDL_CloseCamera.

sdl_camera_spec_finalizer was a no-op, so the SDL_CameraSpec structs
allocated with malloc in sdl_get_camera_format were never freed. Now
calls free().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 95fa063c2b Fix @& ownership mismatches causing refcount leaks
When a Lean extern parameter lacks @&, Lean increments the refcount
before the call and expects the C callee to lean_dec it. If the C side
declares b_lean_obj_arg (borrowed) and never decrements, the refcount
stays elevated and the object is never freed.

Add @& (borrow annotation) to parameters where C only reads the value:

- createWindow/createWindowAndRenderer: title String was owned but C
  only calls lean_string_cstr (borrowed read). Leaked the string.

- renderTextureRect: sourceRect and destRect SDLFRect were owned but C
  only reads scalar fields via lean_ctor_get_float. These are typically
  constructed fresh each frame, so this leaked two rect objects per
  frame (~48 bytes each at 60fps = ~5.6 KB/s).

- setRelativeMouseMode: window SDLWindow was owned but C only reads the
  external data pointer. Leaked the window's refcount.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 9c1c9e84db Fix error type mismatch in SDLIO-returning functions
SDLIO is defined as EIO SDLError, where SDLError is a single-field
structure wrapping String. At runtime, SDLError is represented as just
a String (newtype erasure). Therefore, error returns from C functions
with SDLIO return types should use lean_mk_string(...) directly.

Several functions incorrectly wrapped the error string in
lean_mk_io_user_error(), which creates an IO.Error constructor. When
Lean tried to pattern-match this as SDLError (a String), it would read
the wrong memory layout, causing potential crashes on any error path.

Affected functions: sdl_create_window, sdl_create_window_and_renderer,
sdl_set_render_draw_color, sdl_set_render_draw_color_float,
sdl_render_clear, sdl_render_fill_rect, sdl_create_texture_from_surface
(one of two error paths), sdl_set_track_audio, sdl_play_track.

sdl_render_present is left unchanged because it returns IO Unit (not
SDLIO), so lean_mk_io_user_error is correct there.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 35fde25748 Fix SDLFRect field offsets and float/double ABI mismatch
Two related issues with Lean's Float type being 8 bytes (double), not 4:

1. sdl_render_texture_rect read SDLFRect scalar fields at byte offsets
   0,4,8,12 but lean_ctor_get_float reads 8-byte doubles, so the correct
   offsets are 0,8,16,24. The old offsets caused each read to straddle
   two adjacent fields, producing garbage values for y/w/h.

2. sdl_set_render_draw_color_float declared its parameters as float (4
   bytes) but Lean passes Float as double (8 bytes) at the ABI level.
   On x86-64, Lean puts doubles in XMM registers and the C function
   interpreted the wrong bits—e.g. double 1.0 (0x3FF0000000000000) has
   lower 32 bits 0x00000000, so all colors read as 0.

Both functions now use double to match Lean's Float ABI, with explicit
(float) casts where SDL3 APIs expect float.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 0855aa6f5c Merge sdlObj target into libleansdl
sdlObj was only used by libleansdl, so inline the C compilation
directly into that target to reduce indirection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 20:25:50 -07:00
Greg ShuflinandClaude Opus 4.6 8072a56f7c Inline input_file declaration into sdlObj target
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 20:01:24 -07:00
Greg ShuflinandClaude Opus 4.6 66b2262676 Condense dynlib target declarations to single-expression form
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 19:53:33 -07:00
Greg ShuflinandClaude Opus 4.6 06e751af81 Extract shared sdlLinkObjs/sdlLinkLibs/sdlLinkArgs to reduce repetition across lean targets
Also fixes inconsistent indentation on webcam-app and renames libList/moreLinkArgs
to avoid shadowing Lake field names.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 19:50:17 -07:00
Greg ShuflinandClaude Opus 4.6 7968741b79 Extract shared sdlDeps list to remove duplicated dependency enumerations
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 19:42:12 -07:00
Greg Shuflin 9b6798063b More refactors 2026-01-23 21:07:49 -08:00
Greg Shuflin 304189f6f3 More consolidation work 2026-01-23 20:59:23 -08:00
Greg Shuflin e56901e3c4 Still more consolidation in lakefile.lean 2026-01-23 04:34:32 -08:00
Greg Shuflin c9a817c4be Still more consolidation
get rid of some top level targets that weren't doing anything
2026-01-22 02:38:05 -08:00
Greg Shuflin ec3563a867 Further consolidate lakefile.lean 2026-01-22 02:22:43 -08:00
Greg Shuflin 3e2adcec27 More cleanup of buildfile 2026-01-21 12:15:43 -08:00
Greg Shuflin 2fe1c36263 Cleanup lakefile.build 2026-01-21 02:10:47 -08:00
Greg Shuflin 0d49a9abb6 Consolidation in lakefile.lean 2026-01-20 00:41:49 -08:00
Greg Shuflin 0cff0486be Refactor lakefile 2026-01-19 20:40:24 -08:00
Greg Shuflin 25736f8a97 Add verbose flag in justfile 2026-01-19 19:50:45 -08:00
Greg Shuflin 2425bee7ab Improve justfile 2026-01-19 19:50:10 -08:00
Greg Shuflin 27e5e253ae Documentation update 2026-01-06 13:05:54 -08:00
Greg Shuflin cb9e7e8839 Fix signatures in C 2025-12-26 23:30:25 -08:00
Greg Shuflin 10fe99aa80 Remove printing 2025-12-26 23:17:16 -08:00
Greg Shuflin 62c72f978a Threw claude at the problem - webcam works now 2025-12-26 23:15:55 -08:00
Greg Shuflin 7c94e99f58 Float rect 2025-12-26 22:56:19 -08:00
Greg Shuflin 52b882daaf render texture rect partially done 2025-12-25 19:44:20 -08:00
Greg Shuflin 71ddbdeb5a texture updating + pixels 2025-12-25 18:44:18 -08:00
Greg Shuflin 93262301b5 lean webcamLoop modifications 2025-12-25 18:01:01 -08:00
Greg Shuflin 37fbf85118 SDLRect as lean structure 2025-12-25 04:23:29 -08:00
Greg Shuflin 38aa7e316b more webcam stuff 2025-12-24 02:53:47 -08:00
Greg Shuflin 5b96cc352f createTexture pixelformat 2025-12-24 02:42:04 -08:00
Greg Shuflin e21b84039b get format from sdl_surface 2025-12-24 02:40:26 -08:00
Greg Shuflin 15bde2fad8 move pixelformat 2025-12-24 02:36:38 -08:00
Greg Shuflin 23295a116f fix webcam frame leaking issue 2025-12-24 02:35:27 -08:00
Greg Shuflin f7b5532068 shouldn't be reference 2025-12-24 02:28:49 -08:00
Greg Shuflin a06b234ac0 more sdl texture 2025-12-24 02:25:54 -08:00
Greg Shuflin d1479f9db4 SDL_CreateTexture 2025-12-24 01:13:19 -08:00
Greg Shuflin e33e3ec144 use b_lean_obj_arg 2025-12-24 00:57:19 -08:00
Greg Shuflin 3880a650f4 sdl release camera frame 2025-12-24 00:53:17 -08:00