- Notifications
You must be signed in to change notification settings - Fork3.8k
Add "window" attribute to DROPFILE and DROPTEXT event#3568
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
src_c/event.c Outdated
| caseSDL_DROPFILE:{ | ||
| SDL_Window*window=SDL_GetWindowFromID(event->drop.windowID); | ||
| PyObject*pgWindow; | ||
| if (!window|| | ||
| !(pgWindow=SDL_GetWindowData(window,"pg_window"))) { | ||
| pgWindow=Py_None; | ||
| } | ||
| Py_INCREF(pgWindow); | ||
| _pg_insobj(dict,"window",pgWindow); | ||
| break; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This chunk of code (minus the newcase) is a duplicate of code like 2 lines above, you could just add those new cases above the existing piece instead of duplicating it.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks pretty good to me.
Relevant SDL event documentation page.@ankith26 should review though as they are theevent.c expert.
Looks like the linter is complaining: if you have from the pygame directory in the console. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Hi, thanks for the PR! 🎈
This PR looks almost good, but for the sake of better robustness I think more changes need doing around these bits of code. If you are interested, it'd be nice if you could do these changes in this PR. Otherwise we could also merge this PR first, and me or someone else could do a follow up PR later.
The issue here is thatevent->window.windowID is only guaranteed to be the correct way for accessingwindowID attributes ofWINDOW* events. It just so happens that this also works for some more events because of how the internal union withinSDL_Event is defined. And as you figured out, this way of accessing is wrong for drop events (and one must not attempt to do this either, because this is basically undefined behavior and you will get a garbage id because the wrong memory location is being accessed)
Initialise window to 0 then slap |
I mean the code should be like this: |
ankith26 commentedNov 20, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
This is not a bug, rather it's a side effect of how the underlying
Yeah, the code you suggested will do for now, but for robustness I'd like the splitting of this large switch statement into the correct sub-categories, something like (untested code). SDL_Window*window;switch (event->type) {casePGE_WINDOWSHOWN:casePGE_WINDOWHIDDEN:casePGE_WINDOWEXPOSED:casePGE_WINDOWMOVED:casePGE_WINDOWRESIZED:casePGE_WINDOWSIZECHANGED:casePGE_WINDOWMINIMIZED:casePGE_WINDOWMAXIMIZED:casePGE_WINDOWRESTORED:casePGE_WINDOWENTER:casePGE_WINDOWLEAVE:casePGE_WINDOWFOCUSGAINED:casePGE_WINDOWFOCUSLOST:casePGE_WINDOWCLOSE:casePGE_WINDOWTAKEFOCUS:casePGE_WINDOWHITTEST:casePGE_WINDOWICCPROFCHANGED:casePGE_WINDOWDISPLAYCHANGED:window=SDL_GetWindowFromID(event->window.windowID);break;caseSDL_TEXTEDITING:caseSDL_TEXTINPUT:window=SDL_GetWindowFromID(event->edit.windowID);break;caseSDL_DROPTEXT:caseSDL_DROPFILE:window=SDL_GetWindowFromID(event->drop.windowID);break;caseSDL_MOUSEWHEEL:window=SDL_GetWindowFromID(event->wheel.windowID);break;caseSDL_KEYDOWN:caseSDL_KEYUP:window=SDL_GetWindowFromID(event->key.windowID);break;caseSDL_MOUSEMOTION:window=SDL_GetWindowFromID(event->motion.windowID);break;caseSDL_MOUSEBUTTONDOWN:caseSDL_MOUSEBUTTONUP:window=SDL_GetWindowFromID(event->button.windowID);break;default:/* Event doesn't have a "window" attribute. Can exit the function early */returndict; }/* Handle "window" here */PyObject*pgWindow;if (!window|| !(pgWindow=SDL_GetWindowData(window,"pg_window"))) {pgWindow=Py_None; }Py_INCREF(pgWindow);_pg_insobj(dict,"window",pgWindow);returndict; |
Thank you. The code is modified now. caseSDL_FINGERMOTION:caseSDL_FINGERDOWN:caseSDL_FINGERUP: {window=SDL_GetWindowFromID(event->tfinger.windowID);break;} |
|
Looks like that build uses SDL 2.0.8 released in 2018, and see: https://discourse.libsdl.org/t/sdl-2-0-8-released/23957 and: libsdl-org/SDL@d5ec735 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks again for the PR! I have left a minor addition suggestion, apart from that this PR is good to go! 🎉 🍰
Uh oh!
There was an error while loading.Please reload this page.
Thanks for making this PR! I notice that the Edit here:https://github.com/pygame/pygame/blob/main/docs/reST/ref/event.rst |
Not sure about the latest commit that documents this should be a part of this PR. This is a feature that has been left undocumented so far because it has to do with |
Lets just go ahead and put it out. The underlying SDL event provides the window data, so when we're all caught up with SDL this will be like this anyways. You okay with that@ankith26 ? |
This reverts commit6db7e82.
Starbuck5 commentedDec 13, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Ok@yunline, I talked to Ankith about it and he was still against documenting this. Nice work! |



