Skip to content

Support SDL_WINDOWID #1953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 29, 2023
Merged

Conversation

yunline
Copy link
Contributor

@yunline yunline commented Feb 28, 2023

Issue: #1943

Test code:

import tkinter
import threading
import os

root=tkinter.Tk()
root.geometry("640x480")
btn=tkinter.Button(root,text="HelloWorld")
btn.pack()
cv=tkinter.Canvas(root)
cv.pack()

off=0
def pg_loop(hwnd):
    import pygame
    from pygame._sdl2 import video

    os.environ['SDL_WINDOWID']=str(hwnd)

    pygame.display.init()

    sf=pygame.display.set_mode()
    #sf=pygame.display.set_mode(hwnd=hwnd) 

    pos=(20,20)
    while not off:
        sf.fill((0,0,0))
        pygame.draw.rect(sf,(255,0,0),(*pos,10,10))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type==1024:#MOUSEMOTION
                pos=event.pos


if __name__ == "__main__":
    hwnd=cv.winfo_id()
    threading.Thread(target=pg_loop,args=(hwnd,)).start()
    root.mainloop()
    off=1
    

@yunline yunline requested a review from a team as a code owner February 28, 2023 02:22
@Starbuck5
Copy link
Member

For the purposes of issue #1943 I was hoping for a more limited PR.

This adds another parameter to set_mode and has complex looking handling of cases where that intersects with OpenGL and different versions of SDL, which I don't understand without doing more research.

@yunline
Copy link
Contributor Author

yunline commented Feb 28, 2023

For the purposes of issue #1943 I was hoping for a more limited PR.

This adds another parameter to set_mode and has complex looking handling of cases where that intersects with OpenGL and different versions of SDL, which I don't understand without doing more research.

It's limited now. The rest of contents would be another PR

@ankith26 ankith26 added this to the 2.2 milestone Feb 28, 2023
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.

LGTM 👍

@MyreMylar MyreMylar modified the milestones: 2.2, 2.1.4 Feb 28, 2023
@ankith26
Copy link
Member

ankith26 commented Mar 4, 2023

Hmmm, this may actually be a more complex issue.

On my ubuntu machine, the pygame window does embed inside the tk one after this PR, but the application doesn't work as expected (pygame doesn't get any of the events apart from "keymapchanged")

@MyreMylar
Copy link
Member

MyreMylar commented Mar 4, 2023

Hmmm, this may actually be a more complex issue.

On my ubuntu machine, the pygame window does embed inside the tk one after this PR, but the application doesn't work as expected (pygame doesn't get any of the events apart from "keymapchanged")

Dang.

This prompted me to go check events on this PR versus pygame 1.9.6 (when this last worked).

On Windows

1.9.6

  • Mouse events - fine
  • keyboard events - fine
  • Seemingly no joystick events (were there any?).

1.9.6 just didn't have as many events as now.

This PR

  • Mouse events - fine
  • Window events -fine
  • No keyboard events.
  • Also no Joystick events except joy device added (I guess device removed as well).

@oddbookworm
Copy link
Member

I also just checked, and I could trigger every event I tried except keyboard inputs

@yunline
Copy link
Contributor Author

yunline commented Mar 5, 2023

  • No keyboard events.

I solved this by adding two lines to the test code (on Windows).
I'm trying to figer out why.

import tkinter
import threading
import os

root=tkinter.Tk()
root.geometry("640x480")
btn=tkinter.Button(root,text="HelloWorld")
btn.pack()
cv=tkinter.Canvas(root)
cv.pack()

off=0
def pg_loop(hwnd):
    import pygame
    from pygame._sdl2 import video

    os.environ['SDL_WINDOWID']=str(hwnd)

    pygame.display.init()

    sf=pygame.display.set_mode()
    
# ========== Added here ===========
    win=video.Window.from_display_module()
    win.focus()
# =================================
    pos=(20,20)
    while not off:
        sf.fill((0,0,0))
        pygame.draw.rect(sf,(255,0,0),(*pos,10,10))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type==1024:#MOUSEMOTION
                pos=event.pos
            else:
                print(event)

if __name__ == "__main__":
    hwnd=cv.winfo_id()
    threading.Thread(target=pg_loop,args=(hwnd,)).start()
    root.mainloop()
    off=1
<Event(768-KeyDown {'unicode': ' ', 'key': 32, 'mod': 4096, 'scancode': 44, 'window': <pygame._sdl2.video.Window object at 0x000001A1C7E73270>})>
<Event(769-KeyUp {'unicode': ' ', 'key': 32, 'mod': 4096, 'scancode': 44, 'window': <pygame._sdl2.video.Window object at 0x000001A1C7E73270>})>
<Event(768-KeyDown {'unicode': 'a', 'key': 97, 'mod': 4096, 'scancode': 4, 'window': <pygame._sdl2.video.Window object at 0x000001A1C7E73270>})>
<Event(769-KeyUp {'unicode': 'a', 'key': 97, 'mod': 4096, 'scancode': 4, 'window': <pygame._sdl2.video.Window object at 0x000001A1C7E73270>})>

@oddbookworm
Copy link
Member

It works, until you click out of the tkinter window and back in or press "Tab"

@Starbuck5 Starbuck5 modified the milestones: 2.1.4, 2.2 Mar 5, 2023
@oddbookworm oddbookworm linked an issue Mar 6, 2023 that may be closed by this pull request
@yunline yunline closed this Mar 19, 2023
@yunline
Copy link
Contributor Author

yunline commented Mar 19, 2023

Rerun CI

@yunline yunline reopened this Mar 19, 2023
@Starbuck5 Starbuck5 modified the milestones: 2.2, 2.3 Mar 26, 2023
@Starbuck5 Starbuck5 modified the milestones: 2.3, 2.3.1 May 31, 2023
Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

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

So it doesn't "work," and it's an overdue attempt to regain backwards compatibility with a questionable pygame 1 development strategy.

I'm not excited about it, but it looks like we should have this code attempt of compat in here for parity. I don't think there's anything else we can do with the code to make it better. Thank you yunline for working on this!

@Starbuck5 Starbuck5 merged commit 31f143c into pygame-community:main Jul 29, 2023
@yunline yunline deleted the support-sdlwindowid branch July 29, 2023 08:45
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.

Support SDL_WINDOWID
5 participants