Skip to content
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

Added SDL_GetWindowFromEvent #10459

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions VisualC-GDK/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@
<ClInclude Include="..\..\src\events\blank_cursor.h" />
<ClInclude Include="..\..\src\events\default_cursor.h" />
<ClInclude Include="..\..\src\events\scancodes_windows.h" />
<ClInclude Include="..\..\src\events\SDL_categories_c.h" />
<ClInclude Include="..\..\src\events\SDL_clipboardevents_c.h" />
<ClInclude Include="..\..\src\events\SDL_displayevents_c.h" />
<ClInclude Include="..\..\src\events\SDL_dropevents_c.h" />
Expand Down Expand Up @@ -631,6 +632,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Gaming.Desktop.x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Gaming.Desktop.x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\..\src\events\SDL_categories.c" />
<ClCompile Include="..\..\src\events\SDL_clipboardevents.c" />
<ClCompile Include="..\..\src\events\SDL_displayevents.c" />
<ClCompile Include="..\..\src\events\SDL_dropevents.c" />
Expand Down
2 changes: 2 additions & 0 deletions VisualC-GDK/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<ClCompile Include="..\..\src\core\windows\SDL_xinput.c" />
<ClCompile Include="..\..\src\cpuinfo\SDL_cpuinfo.c" />
<ClCompile Include="..\..\src\dynapi\SDL_dynapi.c" />
<ClCompile Include="..\..\src\events\SDL_categories.c" />
<ClCompile Include="..\..\src\events\SDL_clipboardevents.c" />
<ClCompile Include="..\..\src\events\SDL_displayevents.c" />
<ClCompile Include="..\..\src\events\SDL_dropevents.c" />
Expand Down Expand Up @@ -326,6 +327,7 @@
<ClInclude Include="..\..\src\events\blank_cursor.h" />
<ClInclude Include="..\..\src\events\default_cursor.h" />
<ClInclude Include="..\..\src\events\scancodes_windows.h" />
<ClInclude Include="..\..\src\events\SDL_categories_c.h" />
<ClInclude Include="..\..\src\events\SDL_clipboardevents_c.h" />
<ClInclude Include="..\..\src\events\SDL_displayevents_c.h" />
<ClInclude Include="..\..\src\events\SDL_dropevents_c.h" />
Expand Down
2 changes: 2 additions & 0 deletions VisualC-WinRT/SDL-UWP.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<ClInclude Include="..\src\dynapi\SDL_dynapi_procs.h" />
<ClInclude Include="..\src\events\blank_cursor.h" />
<ClInclude Include="..\src\events\default_cursor.h" />
<ClInclude Include="..\src\events\SDL_categories_c.h" />
<ClInclude Include="..\src\events\SDL_clipboardevents_c.h" />
<ClInclude Include="..\src\events\SDL_displayevents_c.h" />
<ClInclude Include="..\src\events\SDL_dropevents_c.h" />
Expand Down Expand Up @@ -316,6 +317,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\src\dialog\SDL_dialog_utils.c" />
<ClCompile Include="..\src\events\SDL_categories.c" />
<ClCompile Include="..\src\events\SDL_clipboardevents.c" />
<ClCompile Include="..\src\events\SDL_displayevents.c" />
<ClCompile Include="..\src\events\SDL_dropevents.c" />
Expand Down
2 changes: 2 additions & 0 deletions VisualC/SDL/SDL.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
<ClInclude Include="..\..\src\events\blank_cursor.h" />
<ClInclude Include="..\..\src\events\default_cursor.h" />
<ClInclude Include="..\..\src\events\scancodes_windows.h" />
<ClInclude Include="..\..\src\events\SDL_categories_c.h" />
<ClInclude Include="..\..\src\events\SDL_clipboardevents_c.h" />
<ClInclude Include="..\..\src\events\SDL_displayevents_c.h" />
<ClInclude Include="..\..\src\events\SDL_dropevents_c.h" />
Expand Down Expand Up @@ -521,6 +522,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\..\src\events\SDL_categories.c" />
<ClCompile Include="..\..\src\events\SDL_clipboardevents.c" />
<ClCompile Include="..\..\src\events\SDL_displayevents.c" />
<ClCompile Include="..\..\src\events\SDL_dropevents.c" />
Expand Down
6 changes: 6 additions & 0 deletions VisualC/SDL/SDL.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,9 @@
<ClInclude Include="..\..\src\dynapi\SDL_dynapi_unsupported.h">
<Filter>dynapi</Filter>
</ClInclude>
<ClInclude Include="..\..\src\events\SDL_categories.h">
<Filter>events</Filter>
</ClInclude>
<ClInclude Include="..\..\src\events\SDL_clipboardevents_c.h">
<Filter>events</Filter>
</ClInclude>
Expand Down Expand Up @@ -998,6 +1001,9 @@
<ClCompile Include="..\..\src\dynapi\SDL_dynapi.c">
<Filter>dynapi</Filter>
</ClCompile>
<ClCompile Include="..\..\src\events\SDL_categories.c">
<Filter>events</Filter>
</ClCompile>
<ClCompile Include="..\..\src\events\SDL_clipboardevents.c">
<Filter>events</Filter>
</ClCompile>
Expand Down
14 changes: 14 additions & 0 deletions include/SDL3/SDL_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,20 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_EventEnabled(Uint32 type);
*/
extern SDL_DECLSPEC Uint32 SDLCALL SDL_RegisterEvents(int numevents);

/**
* Get window associated with an event.
*
* \param event an event containing a `windowID`.
*
* \returns the associated window on success or NULL if there is none.
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_PollEvent
* \sa SDL_WaitEvent
* \sa SDL_WaitEventTimeout
*/
extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetWindowFromEvent(const SDL_Event *event);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi.sym
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ SDL3_0.0.0 {
SDL_GetWindowBordersSize;
SDL_GetWindowDisplayScale;
SDL_GetWindowFlags;
SDL_GetWindowFromEvent;
SDL_GetWindowFromID;
SDL_GetWindowFullscreenMode;
SDL_GetWindowICCProfile;
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@
#define SDL_GetWindowBordersSize SDL_GetWindowBordersSize_REAL
#define SDL_GetWindowDisplayScale SDL_GetWindowDisplayScale_REAL
#define SDL_GetWindowFlags SDL_GetWindowFlags_REAL
#define SDL_GetWindowFromEvent SDL_GetWindowFromEvent_REAL
#define SDL_GetWindowFromID SDL_GetWindowFromID_REAL
#define SDL_GetWindowFullscreenMode SDL_GetWindowFullscreenMode_REAL
#define SDL_GetWindowICCProfile SDL_GetWindowICCProfile_REAL
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ SDL_DYNAPI_PROC(int,SDL_GetWindowAspectRatio,(SDL_Window *a, float *b, float *c)
SDL_DYNAPI_PROC(int,SDL_GetWindowBordersSize,(SDL_Window *a, int *b, int *c, int *d, int *e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(float,SDL_GetWindowDisplayScale,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(SDL_WindowFlags,SDL_GetWindowFlags,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(SDL_Window*,SDL_GetWindowFromEvent,(const SDL_Event *a),(a),return)
SDL_DYNAPI_PROC(SDL_Window*,SDL_GetWindowFromID,(SDL_WindowID a),(a),return)
SDL_DYNAPI_PROC(const SDL_DisplayMode*,SDL_GetWindowFullscreenMode,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(void*,SDL_GetWindowICCProfile,(SDL_Window *a, size_t *b),(a,b),return)
Expand Down
228 changes: 228 additions & 0 deletions src/events/SDL_categories.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

/* SDL event categories */

#include "SDL_events_c.h"
#include "SDL_categories_c.h"

SDL_EventCategory SDL_GetEventCategory(Uint32 type)
{
if (type >= SDL_EVENT_USER && type <= SDL_EVENT_LAST) {
return SDL_EVENTCATEGORY_USER;
}
else if (type >= SDL_EVENT_DISPLAY_FIRST && type <= SDL_EVENT_DISPLAY_LAST) {
return SDL_EVENTCATEGORY_DISPLAY;
}
else if (type >= SDL_EVENT_WINDOW_FIRST && type <= SDL_EVENT_WINDOW_LAST) {
return SDL_EVENTCATEGORY_WINDOW;
}
switch (type) {
default:
return SDL_SetError("Unknown event type");

case SDL_EVENT_KEYMAP_CHANGED:
case SDL_EVENT_TERMINATING:
case SDL_EVENT_LOW_MEMORY:
case SDL_EVENT_WILL_ENTER_BACKGROUND:
case SDL_EVENT_DID_ENTER_BACKGROUND:
case SDL_EVENT_WILL_ENTER_FOREGROUND:
case SDL_EVENT_DID_ENTER_FOREGROUND:
case SDL_EVENT_LOCALE_CHANGED:
case SDL_EVENT_SYSTEM_THEME_CHANGED:
case SDL_EVENT_RENDER_TARGETS_RESET:
case SDL_EVENT_RENDER_DEVICE_RESET:
return SDL_EVENTCATEGORY_SYSTEM;

case SDL_EVENT_QUIT:
return SDL_EVENTCATEGORY_QUIT;

case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
return SDL_EVENTCATEGORY_KEY;

case SDL_EVENT_TEXT_EDITING:
return SDL_EVENTCATEGORY_EDIT;

case SDL_EVENT_TEXT_INPUT:
return SDL_EVENTCATEGORY_TEXT;

case SDL_EVENT_KEYBOARD_ADDED:
case SDL_EVENT_KEYBOARD_REMOVED:
return SDL_EVENTCATEGORY_KDEVICE;

case SDL_EVENT_TEXT_EDITING_CANDIDATES:
return SDL_EVENTCATEGORY_EDIT_CANDIDATES;

case SDL_EVENT_MOUSE_MOTION:
return SDL_EVENTCATEGORY_MOTION;

case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
return SDL_EVENTCATEGORY_BUTTON;

case SDL_EVENT_MOUSE_WHEEL:
return SDL_EVENTCATEGORY_WHEEL;

case SDL_EVENT_MOUSE_ADDED:
case SDL_EVENT_MOUSE_REMOVED:
return SDL_EVENTCATEGORY_MDEVICE;

case SDL_EVENT_JOYSTICK_AXIS_MOTION:
return SDL_EVENTCATEGORY_JAXIS;

case SDL_EVENT_JOYSTICK_BALL_MOTION:
return SDL_EVENTCATEGORY_JBALL;

case SDL_EVENT_JOYSTICK_HAT_MOTION:
return SDL_EVENTCATEGORY_JHAT;

case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
case SDL_EVENT_JOYSTICK_BUTTON_UP:
return SDL_EVENTCATEGORY_JBUTTON;

case SDL_EVENT_JOYSTICK_ADDED:
case SDL_EVENT_JOYSTICK_REMOVED:
case SDL_EVENT_JOYSTICK_UPDATE_COMPLETE:
return SDL_EVENTCATEGORY_JDEVICE;

case SDL_EVENT_JOYSTICK_BATTERY_UPDATED:
return SDL_EVENTCATEGORY_JBATTERY;

case SDL_EVENT_GAMEPAD_AXIS_MOTION:
return SDL_EVENTCATEGORY_GAXIS;

case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP:
return SDL_EVENTCATEGORY_GBUTTON;

case SDL_EVENT_GAMEPAD_ADDED:
case SDL_EVENT_GAMEPAD_REMOVED:
case SDL_EVENT_GAMEPAD_REMAPPED:
case SDL_EVENT_GAMEPAD_UPDATE_COMPLETE:
case SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED:
return SDL_EVENTCATEGORY_GDEVICE;

case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:
case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:
case SDL_EVENT_GAMEPAD_TOUCHPAD_UP:
return SDL_EVENTCATEGORY_GTOUCHPAD;

case SDL_EVENT_GAMEPAD_SENSOR_UPDATE:
return SDL_EVENTCATEGORY_GSENSOR;

case SDL_EVENT_FINGER_DOWN:
case SDL_EVENT_FINGER_UP:
case SDL_EVENT_FINGER_MOTION:
return SDL_EVENTCATEGORY_TFINGER;

case SDL_EVENT_CLIPBOARD_UPDATE:
return SDL_EVENTCATEGORY_CLIPBOARD;

case SDL_EVENT_DROP_FILE:
case SDL_EVENT_DROP_TEXT:
case SDL_EVENT_DROP_BEGIN:
case SDL_EVENT_DROP_COMPLETE:
case SDL_EVENT_DROP_POSITION:
return SDL_EVENTCATEGORY_DROP;

case SDL_EVENT_AUDIO_DEVICE_ADDED:
case SDL_EVENT_AUDIO_DEVICE_REMOVED:
case SDL_EVENT_AUDIO_DEVICE_FORMAT_CHANGED:
return SDL_EVENTCATEGORY_ADEVICE;

case SDL_EVENT_SENSOR_UPDATE:
return SDL_EVENTCATEGORY_SENSOR;

case SDL_EVENT_PEN_DOWN:
case SDL_EVENT_PEN_UP:
return SDL_EVENTCATEGORY_PTIP;

case SDL_EVENT_PEN_MOTION:
return SDL_EVENTCATEGORY_PMOTION;

case SDL_EVENT_PEN_BUTTON_DOWN:
case SDL_EVENT_PEN_BUTTON_UP:
return SDL_EVENTCATEGORY_PBUTTON;

case SDL_EVENT_CAMERA_DEVICE_ADDED:
case SDL_EVENT_CAMERA_DEVICE_REMOVED:
case SDL_EVENT_CAMERA_DEVICE_APPROVED:
case SDL_EVENT_CAMERA_DEVICE_DENIED:
return SDL_EVENTCATEGORY_CDEVICE;
}
}

SDL_Window *SDL_GetWindowFromEvent(const SDL_Event *event)
{
SDL_WindowID windowID;

switch (SDL_GetEventCategory(event->type)) {
case SDL_EVENTCATEGORY_USER:
windowID = event->user.windowID;
break;
case SDL_EVENTCATEGORY_WINDOW:
windowID = event->window.windowID;
break;
case SDL_EVENTCATEGORY_KEY:
windowID = event->key.windowID;
break;
case SDL_EVENTCATEGORY_EDIT:
windowID = event->edit.windowID;
break;
case SDL_EVENTCATEGORY_TEXT:
windowID = event->text.windowID;
break;
case SDL_EVENTCATEGORY_EDIT_CANDIDATES:
windowID = event->edit_candidates.windowID;
break;
case SDL_EVENTCATEGORY_MOTION:
windowID = event->motion.windowID;
break;
case SDL_EVENTCATEGORY_BUTTON:
windowID = event->button.windowID;
break;
case SDL_EVENTCATEGORY_WHEEL:
windowID = event->wheel.windowID;
break;
case SDL_EVENTCATEGORY_TFINGER:
windowID = event->tfinger.windowID;
break;
case SDL_EVENTCATEGORY_PTIP:
windowID = event->ptip.windowID;
break;
case SDL_EVENTCATEGORY_PMOTION:
windowID = event->pmotion.windowID;
break;
case SDL_EVENTCATEGORY_PBUTTON:
windowID = event->pbutton.windowID;
break;
case SDL_EVENTCATEGORY_DROP:
windowID = event->drop.windowID;
break;
default:
/* < 0 -> invalid event type (error is set by SDL_GetEventCategory) */
/* else -> event has no associated window (not an error) */
return NULL;
}
return SDL_GetWindowFromID(windowID);
}
Loading
Loading