Skip to content

Conversation

@damusss
Copy link
Member

@damusss damusss commented Nov 10, 2024

Window manager information and especially the window handle were only accessible to the display module. I feel like you should know the window handle of any window, so I added the property. This PR changed so now only the window handle is returned. Check the comments ( especially this comment ) for more.

Summary by CodeRabbit

  • New Features

    • Added Window.handle read-only property returning the platform window handle as an integer when available, otherwise 0. Available on Windows, X11, Cocoa, UIKit, Android and Vivante; not supported on Wayland and some others. Marked versionadded 2.5.7.
  • Documentation

    • Added docs describing the handle property's behavior and platform availability.
  • Tests

    • Added a test asserting Window.handle exists and is an int.

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame window pygame.Window labels Nov 10, 2024
@damusss damusss requested a review from a team as a code owner November 10, 2024 15:17
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

SDL_syswm.h header is removed in SDL3. The way to access the wm info has also changed, and I believe that also changes what values are exported by the SDL side.

I think that going forward any new API added should be SDL3 style, and any thing in the grey area should not be exported at all.

@damusss
Copy link
Member Author

damusss commented Nov 10, 2024

SDL_syswm.h header is removed in SDL3. The way to access the wm info has also changed, and I believe that also changes what values are exported by the SDL side.

I think that going forward any new API added should be SDL3 style, and any thing in the grey area should not be exported at all.

Alright, good to know. I'll study the new system that SDL3 uses and adjust the Window code for it. I believe I should keep the SDL2 version (unless it has more dictionary keys than sdl3 will support) so it can be added before pygame 3.

@damusss
Copy link
Member Author

damusss commented Nov 10, 2024

@ankith26 @Starbuck5
the most recent commit adds a codepath for SDL3. I removed the dictionary keys from the SDL2 codepath that weren't accessible in SDL3. My only limitations:

  • I have no idea on how to test the SDL3 codepath
  • I got confused sometimes. because I tried to be close to the sdl2 codepath, but sometimes PyLong_FromLong was used when the returned value was a pointer, instead of using PyCapsule_New. So I'd like to know if what I did is correct, as I assume the checks won't tell me anything

@damusss damusss added the sdl3 label Nov 18, 2024
@ankith26
Copy link
Member

Perhaps this PR should wait for the "port window to SDL3" PR to be made and merged first

@damusss
Copy link
Member Author

damusss commented Nov 18, 2024

Perhaps this PR should wait for the "port window to SDL3" PR to be made and merged first

I suppose that makes sense. Do you need special competence to pull off one of those PRs, or is it just about following the migration guide (and making different codepaths/creating compat macros)? (if so, I could even try to make it, otherwise I'll wait for you or starbuck). :)

Almost forgot, do you guys test your PR to actually compile with SDL3 locally, or do you just trust yourselves? About the first option (more likely), how do you do it? My WSL technically counts as linux if that helps

@damusss damusss requested a review from ankith26 November 21, 2024 09:38
@ankith26
Copy link
Member

We do test that the patch compiles on SDL3. It can be done locally, but you have to compile SDL3 from source if you are gonna go the WSL way (don't worry, it's not that hard). BTW, we also have SDL3 CI now that can do the testing on windows+mac+linux.

Basically the thing is we are yet to figure how to properly deal with the pixelformat changes uniformly, once that is done we can apply the same strategy everywhere and port the next wave of modules

@ankith26
Copy link
Member

Well actually, Window is not blocked by pixelformat changes, I am working on porting it to SDL3 rn

@damusss
Copy link
Member Author

damusss commented Nov 22, 2024

Alright then, nice, thanks

@Starbuck5
Copy link
Member

So even if this code works great on SDL3, why should we expose new API in the SDL2 style?

@damusss
Copy link
Member Author

damusss commented Dec 20, 2024

So even if this code works great on SDL3, why should we expose new API in the SDL2 style?

We don't have to expose it in a SDL2 style, but the window manager information is very dependent on the os so how would you provide the info without a dictionary?

@Starbuck5
Copy link
Member

Having something called "wm info" is clearly an SDL2 construction. Well, looking at the SDL code you have it looks like they expose it now as properties on the window object itself.

@Starbuck5 Starbuck5 marked this pull request as draft December 20, 2024 08:31
@Starbuck5
Copy link
Member

I have marked as a draft, because with uncertainty about API as well as merge conflicts this is not ready to merge.

@damusss
Copy link
Member Author

damusss commented Dec 20, 2024

Having something called "wm info" is clearly an SDL2 construction. Well, looking at the SDL code you have it looks like they expose it now as properties on the window object itself.

That's true, but each property is going to exist or not based on the OS and window manager. They are not generic properties that belong to almost every window like a position and a size. The dictionary reflects how uncertain the attributes are to exist. plus they are all very low level, so putting them besides common attributes would look "ugly" imo.

@damusss damusss changed the title Add Window.wm_info Add Window.handle Jan 5, 2025
@damusss
Copy link
Member Author

damusss commented Jan 5, 2025

Ok, this API was kinda ugly so I replaced it with Window.handle. it's always an integer, casting pointers to size_t. 0 on unsupported OS. This brings more freedom of Window from display. The rest of the display info can still use the old display function, the handle is the only real thing that needs to be added to Window. New MRE:

import pygame

DISPLAY = True
if DISPLAY:
    screen = pygame.display.set_mode((500, 500))
else:
    win = pygame.Window(size=(500, 500))
    screen = win.get_surface()
clock = pygame.Clock()
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT or e.type == pygame.WINDOWCLOSE:
            pygame.quit()
            raise SystemExit
        if e.type == pygame.KEYDOWN:
            if DISPLAY:
                print(pygame.display.get_wm_info()["window"])
            else:
                print(win.handle)
    clock.tick(60)
    if DISPLAY:
        pygame.display.flip()
    else:
        win.flip()

@damusss damusss requested a review from Starbuck5 January 5, 2025 13:46
@damusss damusss marked this pull request as ready for review January 5, 2025 13:55
@damusss
Copy link
Member Author

damusss commented Mar 15, 2025

I fixed the merge conflict and I also made sure that Window.handle compiles with SDL3 and returns the proper window handle (I modified window.c to be able to create a window with SDL3 so I could call the property). This is now ready to review.

@ankith26
Copy link
Member

ankith26 commented Jun 7, 2025

Is there any other usecase for this apart from diagnostics? We probably discussed this already but just so that it's on github - these handle numbers could potentially be useful for embedding foreign windows in pygame-ce or vice-versa. It is probably going to be very platform/configuration specific though.
pygame.display windows currently support SDL_WINDOWID based embedding already but there is no equivalent in the Window constructor.

@damusss
Copy link
Member Author

damusss commented Jun 7, 2025

@ankith26 I would say the usecase is using low level functionality of the OS that can do something if they know the window pointer. If you want, I can rename this to _handle, similar to Surface._pixels_address. How does that sound?

@damusss
Copy link
Member Author

damusss commented Jul 6, 2025

Looks like there are people that desire the handle:
https://discord.com/channels/772505616680878080/822265298085347368/1391179936311803965
and from my questions it looks like you can't achieve the same result by not using the handle.

Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

This seems to make sense to me based on long ago fiddling with window embedding. I'm happy to allow it and then see if there is demand for any other platform specific window properties with defined use cases as we go along.

The way windows are handled is very platform specific in the details and each platform has it's own selection of identifiers that we are unlikely to be able to abstract perfectly to interface with other library code. You can see SDLs SDL_GetWindowProperties() just returns a group of identifiers that differ per platform.

Anyway, approved. 👍

Copy link
Member

@zoldalma999 zoldalma999 left a comment

Choose a reason for hiding this comment

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

Added a small comment, but apart from that both API-wise and implementation wise it looks good to me. However I would maybe wait for one or two more approvals, because it is new API.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 17, 2025

Walkthrough

Adds a read-only Window.handle: int property that returns the native window manager handle when available, otherwise 0. Changes include a type stub, a documentation macro, a C getter with SDL3/SDL2 code paths, and a unit test.

Changes

Cohort / File(s) Summary
Type stub
buildconfig/stubs/pygame/window.pyi
Added public read-only handle: int property to Window stub with platform availability notes.
Documentation macro
src_c/doc/window_doc.h
Added DOC_WINDOW_HANDLE macro documenting handle -> int and its behavior when unsupported.
C implementation
src_c/window.c
Added window_get_handle(pgWindowObject *self, void *v) implementing SDL3 (SDL_GetWindowProperties/driver-specific props) and SDL2 (SDL_GetWindowWMInfo/platform structs) paths; exposed "handle" in Window getset table; conditionally included SDL_syswm.h.
Tests
test/window_test.py
Added test_handle asserting pygame.Window has a handle attribute of type int.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Py as Python
    participant WinObj as Window object
    participant C as C getter (window_get_handle)
    participant SDL as SDL
    rect rgb(0.95,0.98,1)
    Note over Py,WinObj: Access Window.handle (public read-only)
    end
    Py->>WinObj: read .handle
    WinObj->>C: call window_get_handle(self)
    C->>SDL: SDL_GetCurrentVideoDriver()
    alt SDL >= 3.0
        C->>SDL: SDL_GetWindowProperties(window, props)
        SDL-->>C: driver-specific pointer/number
        C-->>WinObj: integer handle or 0
    else SDL < 3.0
        C->>SDL: SDL_GetWindowWMInfo(window, &wminfo)
        SDL-->>C: platform-specific struct
        C-->>WinObj: extracted handle or 0
    end
    WinObj-->>Py: return int handle
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

Poem

🐇 I hopped through SDL and found a key,
a tiny handle safe with me.
On Windows, Cocoa, X11's green,
I clutch that number, neat and clean.
If Wayland hides it, I sigh — I keep it zero, spry.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Add Window.handle" directly and accurately reflects the main change in this pull request, which introduces a new public read-only property handle: int to the Window class. The title is concise, specific, and clearly communicates what is being added without using vague terminology or unnecessary noise. A reviewer scanning the commit history would immediately understand that this changeset adds a window handle property to the Window class, which aligns with the actual implementation across all modified files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dfbce58 and 875d57d.

📒 Files selected for processing (1)
  • src_c/window.c (3 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-17T08:53:32.421Z
Learnt from: damusss
PR: pygame-community/pygame-ce#3219
File: src_c/window.c:11-13
Timestamp: 2025-10-17T08:53:32.421Z
Learning: SDL3's first stable release is version 3.2.0 (released January 21, 2025). SDL 3.0.x and 3.1.x were preview/pre-release versions not intended for production use, so version checks for SDL3 features in pygame-ce should use SDL_VERSION_ATLEAST(3, 2, 0) to target the first stable release.

Applied to files:

  • src_c/window.c
🧬 Code graph analysis (1)
src_c/window.c (1)
buildconfig/stubs/pygame/window.pyi (1)
  • handle (316-326)
⏰ 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: AMD64
  • 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) [s390x]
  • GitHub Check: Debian (Bookworm - 12) [armv6]
  • GitHub Check: Debian (Bookworm - 12) [ppc64le]
  • GitHub Check: Debian (Bookworm - 12) [armv7]
  • GitHub Check: msys2 (clang64, clang-x86_64)
  • GitHub Check: msys2 (mingw64, x86_64)
  • GitHub Check: msys2 (ucrt64, ucrt-x86_64)
  • GitHub Check: dev-check
  • GitHub Check: x86_64
  • GitHub Check: i686
  • GitHub Check: aarch64
  • GitHub Check: build (macos-14)
  • GitHub Check: Pyodide build
  • GitHub Check: build (ubuntu-22.04)
  • GitHub Check: build (ubuntu-24.04)
  • GitHub Check: build (windows-latest)
🔇 Additional comments (3)
src_c/window.c (3)

11-13: Include guard correctly excludes SDL_syswm.h from all SDL3 versions.

The guard properly excludes the SDL2-only header from all SDL3 builds, aligning with the function implementation at line 929.


923-1005: Implementation looks solid; all previous critical issues have been resolved.

The function correctly implements SDL3/SDL2 branching:

  • SDL3 path uses the properties API with appropriate driver-specific constants
  • SDL2 path uses SDL_SysWMinfo with runtime subsystem checks
  • Android property name is now correct (line 956)
  • Semicolon is present after SDL_VERSION macro (line 966)
  • Consistent use of PyLong_FromSize_t for return values
  • Returns 0 sentinel value for unsupported platforms as documented

1524-1524: Property registration is correct.

The handle property is properly registered as read-only with the appropriate getter and documentation reference.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

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: 1

🧹 Nitpick comments (1)
src_c/doc/window_doc.h (1)

20-20: Minor: Add punctuation for consistency.

The documentation string should end with a period for consistency with other property documentation in this file.

Apply this diff:

-#define DOC_WINDOW_HANDLE "handle -> int\nGet the window handle provided by the window manager if supported otherwise 0"
+#define DOC_WINDOW_HANDLE "handle -> int\nGet the window handle provided by the window manager if supported otherwise 0."
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d5e66d8 and 74138c0.

📒 Files selected for processing (4)
  • buildconfig/stubs/pygame/window.pyi (1 hunks)
  • src_c/doc/window_doc.h (1 hunks)
  • src_c/window.c (3 hunks)
  • test/window_test.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
test/window_test.py (2)
buildconfig/stubs/pygame/window.pyi (2)
  • Window (11-503)
  • handle (316-326)
src_py/__init__.py (1)
  • Window (332-333)
src_c/window.c (1)
buildconfig/stubs/pygame/window.pyi (1)
  • handle (316-326)
⏰ 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: build (windows-latest)
  • GitHub Check: build (ubuntu-24.04)
  • GitHub Check: x86
  • GitHub Check: AMD64
  • GitHub Check: Debian (Bookworm - 12) [armv6]
  • GitHub Check: msys2 (clang64, clang-x86_64)
  • GitHub Check: msys2 (ucrt64, ucrt-x86_64)
  • GitHub Check: msys2 (mingw64, x86_64)
  • GitHub Check: Debian (Bookworm - 12) [ppc64le]
  • GitHub Check: Debian (Bookworm - 12) [s390x]
  • GitHub Check: Debian (Bookworm - 12) [armv7]
  • GitHub Check: Pyodide build
  • GitHub Check: build (ubuntu-22.04)
  • GitHub Check: dev-check
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.9.23)
  • GitHub Check: i686
  • GitHub Check: aarch64
  • GitHub Check: x86_64
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.14.0rc1)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.13.5)
🔇 Additional comments (6)
test/window_test.py (1)

461-463: LGTM!

The test correctly verifies that the handle attribute exists and returns an integer type.

src_c/window.c (4)

11-13: LGTM!

Correct conditional include for SDL2 compatibility.


963-990: Note the inconsistency between SDL2 and SDL3 platform handling.

The SDL2 code path uses compile-time checks (#if defined(SDL_VIDEO_DRIVER_WINDOWS)), meaning the handle is only available for drivers that were compiled into SDL. The SDL3 code path uses runtime checks (!strcmp(driver, "windows")), which is more flexible.

This means behavior differs between SDL versions: on SDL2, an unsupported platform returns 0 even if the driver name matches, while on SDL3, the driver name determines the property to fetch.

Consider documenting this nuance, or verify that the SDL2 compile-time approach is intentional and aligns with how other properties are handled in the codebase.


1512-1512: LGTM!

Correctly exposes the handle as a read-only property.


929-929: Based on verification against official SDL3 documentation, the version requirement in the code is correct.

SDL_GetCurrentVideoDriver() is available since SDL 3.2.0, as confirmed by both the official SDL Wiki and the SDL3 source code. The version check at line 929 is therefore accurate and necessary.

The review comment's concern about the function causing incorrect behavior in SDL 3.0.0-3.1.x builds is valid from a defensive coding perspective—attempting to call SDL_GetCurrentVideoDriver() on those versions would indeed fail. However, the #if SDL_VERSION_ATLEAST(3, 2, 0) guard correctly prevents this.

buildconfig/stubs/pygame/window.pyi (1)

315-326: LGTM!

The stub is well-documented with clear platform availability notes and the correct version annotation (2.5.7).

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: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 74138c0 and 19cd900.

📒 Files selected for processing (1)
  • src_c/window.c (3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src_c/window.c (1)
buildconfig/stubs/pygame/window.pyi (1)
  • handle (316-326)
⏰ 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: Debian (Bookworm - 12) [armv7]
  • GitHub Check: Debian (Bookworm - 12) [s390x]
  • GitHub Check: Debian (Bookworm - 12) [armv6]
  • GitHub Check: Pyodide build
  • GitHub Check: build (macos-14)
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (ubuntu-24.04)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.14.0rc1)
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.13.5)
  • GitHub Check: aarch64
  • GitHub Check: debug_coverage (ubuntu-24.04, 3.9.23)
  • GitHub Check: msys2 (clang64, clang-x86_64)
  • GitHub Check: msys2 (ucrt64, ucrt-x86_64)
  • GitHub Check: msys2 (mingw64, x86_64)
  • GitHub Check: AMD64
  • GitHub Check: x86_64
  • GitHub Check: i686
  • GitHub Check: x86
  • GitHub Check: build (ubuntu-22.04)
  • GitHub Check: dev-check
🔇 Additional comments (1)
src_c/window.c (1)

1512-1512: Stub correctly exposes handle as a read‑only property.

The verification confirms that buildconfig/stubs/pygame/window.pyi correctly declares handle with the @property decorator at line 315, followed by the method signature def handle(self) -> int: at line 316. The property is properly typed and matches the getter implementation in the C code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

New API This pull request may need extra debate as it adds a new class or function to pygame sdl3 window pygame.Window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants