Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
Starbuck5 merged 9 commits intopygame:mainfromyunline:main
Dec 13, 2022
Merged

Add "window" attribute to DROPFILE and DROPTEXT event#3568

Starbuck5 merged 9 commits intopygame:mainfromyunline:main
Dec 13, 2022

Conversation

@yunline
Copy link
Contributor

image

Zireael07 reacted with thumbs up emoji
src_c/event.c Outdated
Comment on lines 1273 to 1283
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;
}
Copy link
Contributor

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.

Copy link
Contributor

@MyreMylarMyreMylar left a 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.

@MyreMylar
Copy link
Contributor

Looks like the linter is complaining:

'clang-format' failed. Please run: python setup.py format Do you have the latest version of clang-format?

if you haveclang-format installed (Install guide) (python package:https://pypi.org/project/clang-format/) it should just be a quick:

clang-format src_c/event.c -i

from the pygame directory in the console.

yunline reacted with thumbs up emoji

Copy link
Contributor

@ankith26ankith26 left a 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)

@MyreMylar
Copy link
Contributor

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 slapSDL_GetWindowFromID(event->window.windowID); into an else block?

@yunline
Copy link
ContributorAuthor

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)

I add 2 lines here
image
And I get this
image
Maybe there is a bug in SDL library.

@MyreMylar
Copy link
Contributor

I mean the code should be like this:

SDL_Window *window = 0;if (event->type == SDL_DROPTEXT || event->type == SDL_DROPFILE) {    window = SDL_GetWindowFromID(event->drop.windowID);}else{    window = SDL_GetWindowFromID(event->window.windowID);}PyObject *pgWindow;if (!window || !(pgWindow = SDL_GetWindowData(window, "pg_window"))) {    pgWindow = Py_None;}Py_INCREF(pgWindow);_pg_insobj(dict, "window", pgWindow);break;

@ankith26
Copy link
Contributor

ankith26 commentedNov 20, 2022
edited
Loading

Maybe there is a bug in SDL library.

This is not a bug, rather it's a side effect of how the underlyingunion is implemented in the event struct. With unions, you can have multiple names to access the same memory block.

I mean the code should be like this

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;
yunline and MyreMylar reacted with thumbs up emoji

@yunline
Copy link
ContributorAuthor

I mean the code should be like this

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.
I also addedSDL_FINGERMOTION,SDL_FINGERDOWN andSDL_FINGERUP into thecase.

caseSDL_FINGERMOTION:caseSDL_FINGERDOWN:caseSDL_FINGERUP: {window=SDL_GetWindowFromID(event->tfinger.windowID);break;}

@yunline
Copy link
ContributorAuthor

IMG_20221122_004435.jpg
Why? I don't understand. Is it a problem about wrong SDL version? But thesdl wiki indicates that'This structure is available since SDL 2.0.0.'

@MyreMylar
Copy link
Contributor

Looks like that build uses SDL 2.0.8 released in 2018, andwindowID was added in 2019.

see:

https://discourse.libsdl.org/t/sdl-2-0-8-released/23957

and:

libsdl-org/SDL@d5ec735
libsdl-org/SDL@2fb71ac
libsdl-org/SDL@109cbd6

yunline reacted with thumbs up emoji

Copy link
Contributor

@ankith26ankith26 left a 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! 🎉 🍰

@Starbuck5
Copy link
Contributor

Thanks for making this PR! I notice that theevents documentation describes what attributes each type of event should have, so that should be updated for this.

Edit here:https://github.com/pygame/pygame/blob/main/docs/reST/ref/event.rst
It would also be good to put in a "versionchanged" tag for this in the documentation there.

yunline reacted with thumbs up emoji

@ankith26
Copy link
Contributor

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_sdl2 which isn't public and stable.

@Starbuck5
Copy link
Contributor

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 _sdl2 which isn't public and stable.

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 ?

@Starbuck5
Copy link
Contributor

Starbuck5 commentedDec 13, 2022
edited
Loading

Ok@yunline, I talked to Ankith about it and he was still against documenting this.
So I went ahead and reverted the commit on your PR branch, we'll merge it when the tests pass.

Nice work!

yunline reacted with hooray emoji

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

Reviewers

3 more reviewers

@MatiissMatiissMatiiss requested changes

@MyreMylarMyreMylarMyreMylar approved these changes

@ankith26ankith26ankith26 approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

C codeInvolves changing C codeeventpygame.event

Projects

None yet

Milestone

2.2.0

Development

Successfully merging this pull request may close these issues.

Can't identify the window of DROPFILE event

5 participants

@yunline@MyreMylar@ankith26@Starbuck5@Matiiss

[8]ページ先頭

©2009-2025 Movatter.jp