Skip to content

Conversation

ankith26
Copy link
Member

@ankith26 ankith26 commented Sep 1, 2025

  • Vendors in SDL_gesture.h
  • windowevent fixes

@ankith26 ankith26 requested a review from a team as a code owner September 1, 2025 14:13
Copy link
Contributor

coderabbitai bot commented Sep 1, 2025

📝 Walkthrough

Walkthrough

Introduces an SDL3-compatible gesture subsystem via a new header/implementation, integrates gesture lifecycle (init/quit) and event translation into the event module, updates event and window event enumerations with SDL3-aware guards, and adjusts constants mapping for MULTIGESTURE and JOYBALLMOTION to SDL values.

Changes

Cohort / File(s) Summary
SDL3 gesture subsystem (new API + impl)
src_c/include/SDL_gesture.h
Adds SDL gesture API with SDL3-compatible types, events, constants, and functions; provides SDL2 shims; includes full implementation when SDL_GESTURE_IMPLEMENTATION is defined (recording, recognition, template I/O, event emission).
Core header: SDL3 gating and enums
src_c/_pygame.h
Adds PygameWindowEventCode mapping to SDL3 window events; introduces SDL3 gesture macro aliases; removes SDL3-incompatible PG_MULTIGESTURE/PG_JOYBALLMOTION placeholders; gates PGE_WINDOW* in PygameEventCode to SDL2-only; conditions PGPOST_* gesture events on SDL version.
Event system integration
src_c/event.c
Wires Gesture_Init/Quit into auto init/quit for SDL3; enables proxying for DOLLARGESTURE/DOLLARRECORD/MULTIGESTURE; adds SDL3-specific dict mapping for gesture and touch events; introduces _pg_event_psuedo_block and related filtering updates.
Constants mapping update
src_c/constants.c
Switches JOYBALLMOTION and MULTIGESTURE constants to SDL_* values via DEC_CONST, replacing PG_* indirection.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App
  participant Pygame as Pygame Event Module
  participant SDL as SDL Core
  participant Gesture as Gesture Subsystem

  App->>Pygame: import/init
  Pygame->>Gesture: Gesture_Init() [SDL3+]
  Gesture-->>Pygame: init result
  note right of Pygame: Event filters and proxy mappings set

  App->>Pygame: quit
  Pygame->>Gesture: Gesture_Quit() [SDL3+]
  Gesture-->>Pygame: cleanup done
Loading
sequenceDiagram
  autonumber
  participant Touch as Touch Device
  participant SDL as SDL Event Loop
  participant Gesture as Gesture Subsystem
  participant Pygame as Pygame Event Module
  participant Py as Python

  Touch->>SDL: Finger events (down/move/up)
  SDL->>Gesture: Feed raw touch data [SDL3+ impl]
  Gesture-->>SDL: Emit GESTURE_MULTIGESTURE / GESTURE_DOLLARGESTURE / GESTURE_DOLLARRECORD
  SDL->>Pygame: SDL_* gesture/touch events
  Pygame->>Pygame: Translate to dict (SDL2/SDL3 paths)
  Pygame-->>Py: Post event objects
Loading

Estimated code review effort

🎯 5 (Critical) | ⏱️ ~120+ minutes

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ankith26-sdl3-1

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (3)
src_c/event.c (2)

493-506: Typo: _pg_event_psuedo_block → _pg_event_pseudo_block

Minor spelling fix improves searchability and consistency.

-static bool
-_pg_event_psuedo_block(SDL_Event *event)
+static bool
+_pg_event_pseudo_block(SDL_Event *event)
@@
-        return true;
+        return true;
@@
-    return false;
+    return false;

And update call sites:

-    if (_pg_event_psuedo_block(event)) {
+    if (_pg_event_pseudo_block(event)) {

(Apply in Line 514 and Line 748.)


818-822: Improve error reporting on Gesture_Init failure

If Gesture_Init doesn't set an SDL error, the raised message may be empty. Consider a fallback string.

-        if (Gesture_Init() != 0) {
-            return RAISE(pgExc_SDLError, SDL_GetError());
-        }
+        if (Gesture_Init() != 0) {
+            const char *err = SDL_GetError();
+            return RAISE(pgExc_SDLError, (err && *err) ? err : "Gesture_Init failed");
+        }
src_c/include/SDL_gesture.h (1)

878-900: Consider cleaning up commented debug code.

This section contains extensive commented-out debug code. Consider removing it entirely if it's no longer needed, or moving it to a debug-only block.

Either remove the commented code or wrap it in a debug macro:

-            /* inTouch->gestureLast[j].dDist = dDist;
-            inTouch->gestureLast[j].dtheta = dtheta;
-
-            printf("dDist = %f, dTheta = %f\n",dDist,dtheta);
-            gdtheta = gdtheta*.9 + dtheta*.1;
-            gdDist  =  gdDist*.9 +  dDist*.1
-            knob.r += dDist/numDownFingers;
-            knob.ang += dtheta;
-            printf("thetaSum = %f, distSum = %f\n",gdtheta,gdDist);
-            printf("id: %i dTheta = %f, dDist = %f\n",j,dtheta,dDist); */
+#ifdef DEBUG_GESTURES
+            /* Debug output for gesture processing */
+            printf("dDist = %f, dTheta = %f\n", dDist, dtheta);
+#endif
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c7e9c15 and 161264d.

📒 Files selected for processing (4)
  • src_c/_pygame.h (4 hunks)
  • src_c/constants.c (2 hunks)
  • src_c/event.c (7 hunks)
  • src_c/include/SDL_gesture.h (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-31T06:44:48.360Z
Learnt from: ankith26
PR: pygame-community/pygame-ce#3427
File: src_c/_sdl2/touch.c:99-103
Timestamp: 2025-08-31T06:44:48.360Z
Learning: SDL_GetNumTouchFingers() returns 0 on failure, not as a valid "no fingers" state. A return value of 0 should be treated as an error condition and SDL_GetError() should be called to get the error message.

Applied to files:

  • src_c/include/SDL_gesture.h
🧬 Code graph analysis (1)
src_c/event.c (1)
src_c/include/SDL_gesture.h (3)
  • SDLCALL (204-209)
  • Gesture_Quit (313-321)
  • Gesture_Init (211-217)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.9.23)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.14.0rc1)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.13.5)
  • GitHub Check: Debian (Bookworm - 12) [armv7]
  • GitHub Check: Debian (Bookworm - 12) [s390x]
  • GitHub Check: Debian (Bookworm - 12) [ppc64le]
  • GitHub Check: Debian (Bookworm - 12) [armv6]
  • GitHub Check: dev-check
  • GitHub Check: x86_64
  • GitHub Check: msys2 (ucrt64, ucrt-x86_64)
  • GitHub Check: x86
  • GitHub Check: AMD64
  • GitHub Check: msys2 (mingw64, x86_64)
  • GitHub Check: msys2 (clang64, clang-x86_64)
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (ubuntu-22.04)
  • GitHub Check: build (macos-14)
  • GitHub Check: aarch64
  • GitHub Check: build (ubuntu-24.04)
  • GitHub Check: i686
🔇 Additional comments (12)
src_c/constants.c (1)

263-263: Switch to SDL-native JOYBALLMOTION/MULTIGESTURE — verify SDL3 aliases exist

DEC_CONST maps to SDL_JOYBALLMOTION/SDL_MULTIGESTURE. Please confirm these are defined for SDL3 builds via pgcompat/pygame.h (or equivalent). Otherwise this will fail to compile on SDL3 and/or diverge from prior PG* placeholders. Also verify docs/tests no longer reference PG_JOYBALLMOTION/PG_MULTIGESTURE.

Also applies to: 305-305

src_c/event.c (5)

28-28: Single-TU gesture implementation — avoid duplicate definitions

Defining SDL_GESTURE_IMPLEMENTATION here compiles the implementation into this TU. Please verify no other TU defines it to prevent duplicate symbols at link time.


507-518: Event filter split looks good

Return type gating for SDL2/SDL3 and delegating pseudo-block decisions is appropriate.


741-749: Correct: do not hard-block window events here

Early return keeps window events visible to watchers; actual blocking is handled later.


783-786: Proper lifecycle: Gesture_Quit on AutoQuit

Good pairing with init; avoids leaking the event watch/touch state.


1310-1341: Casting SDL_Event to Gesture_MultiGestureEvent risks UB; prefer copy/accessor**

Strict-aliasing/ABI assumptions can break under optimization or platform ABIs. Please either memcpy into a local Gesture_MultiGestureEvent or add an accessor helper in SDL_gesture.h that fills an out struct. Add a size check, too.

Suggested patch for safer read (SDL3 branch):

-            _pg_insobj(dict, "touch_id",
-                       PyLong_FromLongLong(
-                           ((Gesture_MultiGestureEvent *)event)->touchID));
-            _pg_insobj(
-                dict, "x",
-                PyFloat_FromDouble(((Gesture_MultiGestureEvent *)event)->x));
-            _pg_insobj(
-                dict, "y",
-                PyFloat_FromDouble(((Gesture_MultiGestureEvent *)event)->y));
-            _pg_insobj(dict, "rotated",
-                       PyFloat_FromDouble(
-                           ((Gesture_MultiGestureEvent *)event)->dTheta));
-            _pg_insobj(dict, "pinched",
-                       PyFloat_FromDouble(
-                           ((Gesture_MultiGestureEvent *)event)->dDist));
-            _pg_insobj(dict, "num_fingers",
-                       PyLong_FromLong(
-                           ((Gesture_MultiGestureEvent *)event)->numFingers));
+            Gesture_MultiGestureEvent ge;
+            memcpy(&ge, event, sizeof(ge));
+            _pg_insobj(dict, "touch_id", PyLong_FromLongLong(ge.touchID));
+            _pg_insobj(dict, "x", PyFloat_FromDouble(ge.x));
+            _pg_insobj(dict, "y", PyFloat_FromDouble(ge.y));
+            _pg_insobj(dict, "rotated", PyFloat_FromDouble(ge.dTheta));
+            _pg_insobj(dict, "pinched", PyFloat_FromDouble(ge.dDist));
+            _pg_insobj(dict, "num_fingers", PyLong_FromLong(ge.numFingers));

Additionally, near the top of this file (or in SDL_gesture.h), add:

#if SDL_VERSION_ATLEAST(3, 0, 0)
_Static_assert(sizeof(Gesture_MultiGestureEvent) <= sizeof(SDL_Event),
               "Gesture_MultiGestureEvent must fit into SDL_Event");
#endif
src_c/_pygame.h (2)

53-59: SDL3 gesture aliases look good

Including SDL_gesture.h and aliasing SDL_* to GESTURE_* ensures the rest of the code can stay SDL2-style.


425-445: Verify WINDOWSIZECHANGED mapping semantics

PGE_WINDOWSIZECHANGED → SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: confirm this matches prior behavior (logical vs pixel size). If callers expect logical size changes, consider documenting or mapping accordingly.

src_c/include/SDL_gesture.h (4)

1-24: LGTM! Proper licensing and context documentation.

The file correctly includes the SDL license header and clearly documents that this is the SDL2 gesture implementation vendored for SDL3 compatibility.


872-876: Good defensive programming for division by zero.

The code properly handles the edge case where lDist == 0 to avoid division by zero and resulting NaN values.


454-454: Verify SDL_TouchID signedness and use of sentinel
SDL_TouchID is defined in the external SDL2 headers, so please confirm whether it’s a signed type (e.g. Sint64) and adjust the if (touchID >= 0) check to explicitly compare against the proper invalid sentinel (negative) or use an SDL-defined invalid touch-ID constant rather than relying on signedness.


283-293: Confirm SDL_GetTouchDevices(NULL) API usage: Per SDL3 docs, SDL_GetTouchDevices(NULL) returns a 0-terminated SDL_TouchID array (or NULL on failure), accepts a NULL count pointer, and must be freed with SDL_free(); the code correctly checks for NULL, iterates until the zero terminator, and frees the array (wiki.libsdl.org)

@Starbuck5 Starbuck5 added the sdl3 label Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants