Skip to content

Commit

Permalink
Added SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED and SDL_EVENT_DISPLAY_CU…
Browse files Browse the repository at this point in the history
…RRENT_MODE_CHANGED
  • Loading branch information
slouken committed Jul 12, 2024
1 parent 5bf6bc4 commit 676c50a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 15 deletions.
3 changes: 3 additions & 0 deletions include/SDL3/SDL_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ typedef enum SDL_EventType
SDL_EVENT_DISPLAY_ADDED, /**< Display has been added to the system */
SDL_EVENT_DISPLAY_REMOVED, /**< Display has been removed from the system */
SDL_EVENT_DISPLAY_MOVED, /**< Display has changed position */
SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED, /**< Display has changed desktop mode */
SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED, /**< Display has changed current mode */
SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED, /**< Display has changed content scale */
SDL_EVENT_DISPLAY_FIRST = SDL_EVENT_DISPLAY_ORIENTATION,
SDL_EVENT_DISPLAY_LAST = SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED,
Expand Down Expand Up @@ -281,6 +283,7 @@ typedef struct SDL_DisplayEvent
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
SDL_DisplayID displayID;/**< The associated display */
Sint32 data1; /**< event dependent data */
Sint32 data2; /**< event dependent data */
} SDL_DisplayEvent;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/android/SDL_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(onNativeRotationChanged)(

if (Android_Window) {
SDL_VideoDisplay *display = SDL_GetVideoDisplay(SDL_GetPrimaryDisplay());
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, displayCurrentOrientation);
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, displayCurrentOrientation, 0);
}

SDL_UnlockMutex(Android_ActivityMutex);
Expand Down
3 changes: 2 additions & 1 deletion src/events/SDL_displayevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include "SDL_events_c.h"

int SDL_SendDisplayEvent(SDL_VideoDisplay *display, SDL_EventType displayevent, int data1)
int SDL_SendDisplayEvent(SDL_VideoDisplay *display, SDL_EventType displayevent, int data1, int data2)
{
int posted;

Expand All @@ -50,6 +50,7 @@ int SDL_SendDisplayEvent(SDL_VideoDisplay *display, SDL_EventType displayevent,
event.common.timestamp = 0;
event.display.displayID = display->id;
event.display.data1 = data1;
event.display.data2 = data2;
posted = (SDL_PushEvent(&event) > 0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/events/SDL_displayevents_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
#ifndef SDL_displayevents_c_h_
#define SDL_displayevents_c_h_

extern int SDL_SendDisplayEvent(SDL_VideoDisplay *display, SDL_EventType displayevent, int data1);
extern int SDL_SendDisplayEvent(SDL_VideoDisplay *display, SDL_EventType displayevent, int data1, int data2);

#endif /* SDL_displayevents_c_h_ */
6 changes: 4 additions & 2 deletions src/events/SDL_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ static void SDL_LogEvent(const SDL_Event *event)
#define SDL_DISPLAYEVENT_CASE(x) \
case x: \
SDL_strlcpy(name, #x, sizeof(name)); \
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u display=%u event=%s data1=%d)", \
(uint)event->display.timestamp, (uint)event->display.displayID, name, (int)event->display.data1); \
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u display=%u event=%s data1=%d, data2=%d)", \
(uint)event->display.timestamp, (uint)event->display.displayID, name, (int)event->display.data1, (int)event->display.data2); \
break
SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_ORIENTATION);
SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_ADDED);
SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_REMOVED);
SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_MOVED);
SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED);
SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED);
SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED);
#undef SDL_DISPLAYEVENT_CASE

Expand Down
10 changes: 9 additions & 1 deletion src/test/SDL_test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ static void SDLTest_PrintEvent(const SDL_Event *event)
{
switch (event->type) {
case SDL_EVENT_SYSTEM_THEME_CHANGED:
SDL_Log("SDL EVENT: System theme changed to %s\n", SystemThemeName());
SDL_Log("SDL EVENT: System theme changed to %s", SystemThemeName());
break;
case SDL_EVENT_DISPLAY_ADDED:
SDL_Log("SDL EVENT: Display %" SDL_PRIu32 " attached",
Expand All @@ -1576,6 +1576,14 @@ static void SDLTest_PrintEvent(const SDL_Event *event)
event->display.displayID, (int)(scale * 100.0f));
}
break;
case SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED:
SDL_Log("SDL EVENT: Display %" SDL_PRIu32 " desktop mode changed to %" SDL_PRIs32 "x%" SDL_PRIs32,
event->display.displayID, (int)event->display.data1, event->display.data2);
break;
case SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED:
SDL_Log("SDL EVENT: Display %" SDL_PRIu32 " current mode changed to %" SDL_PRIs32 "x%" SDL_PRIs32,
event->display.displayID, event->display.data1, event->display.data2);
break;
case SDL_EVENT_DISPLAY_MOVED:
SDL_Log("SDL EVENT: Display %" SDL_PRIu32 " changed position",
event->display.displayID);
Expand Down
40 changes: 36 additions & 4 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ SDL_DisplayID SDL_AddVideoDisplay(const SDL_VideoDisplay *display, SDL_bool send
SDL_UpdateDesktopBounds();

if (send_event) {
SDL_SendDisplayEvent(new_display, SDL_EVENT_DISPLAY_ADDED, 0);
SDL_SendDisplayEvent(new_display, SDL_EVENT_DISPLAY_ADDED, 0, 0);
}

return id;
Expand Down Expand Up @@ -823,7 +823,7 @@ void SDL_DelVideoDisplay(SDL_DisplayID displayID, SDL_bool send_event)
display = _this->displays[display_index];

if (send_event) {
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_REMOVED, 0);
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_REMOVED, 0, 0);
}

SDL_DestroyProperties(display->props);
Expand Down Expand Up @@ -1044,7 +1044,7 @@ void SDL_SetDisplayContentScale(SDL_VideoDisplay *display, float scale)
SDL_Window *window;

display->content_scale = scale;
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED, 0);
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED, 0, 0);

/* Check the windows on this display */
for (window = _this->windows; window; window = window->next) {
Expand Down Expand Up @@ -1310,14 +1310,34 @@ const SDL_DisplayMode *SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID display
return closest;
}

static SDL_bool DisplayModeChanged(const SDL_DisplayMode *old, const SDL_DisplayMode *new)
{
return ((old->displayID && old->displayID != new->displayID) ||
(old->format && old->format != new->format) ||
(old->w && old->h && (old->w != new->w ||old->h != new->h)) ||
(old->pixel_density && old->pixel_density != new->pixel_density) ||
(old->refresh_rate && old->refresh_rate != new->refresh_rate));
}

void SDL_SetDesktopDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
{
SDL_DisplayMode last_mode;

SDL_copyp(&last_mode, &display->desktop_mode);

if (display->desktop_mode.driverdata) {
SDL_free(display->desktop_mode.driverdata);
}
SDL_memcpy(&display->desktop_mode, mode, sizeof(*mode));
SDL_copyp(&display->desktop_mode, mode);
display->desktop_mode.displayID = display->id;
SDL_FinalizeDisplayMode(&display->desktop_mode);

if (DisplayModeChanged(&last_mode, &display->desktop_mode)) {
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED, mode->w, mode->h);
if (display->current_mode == &display->desktop_mode) {
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED, mode->w, mode->h);
}
}
}

const SDL_DisplayMode *SDL_GetDesktopDisplayMode(SDL_DisplayID displayID)
Expand All @@ -1331,7 +1351,19 @@ const SDL_DisplayMode *SDL_GetDesktopDisplayMode(SDL_DisplayID displayID)

void SDL_SetCurrentDisplayMode(SDL_VideoDisplay *display, const SDL_DisplayMode *mode)
{
SDL_DisplayMode last_mode;

if (display->current_mode) {
SDL_copyp(&last_mode, display->current_mode);
} else {
SDL_zero(last_mode);
}

display->current_mode = mode;

if (DisplayModeChanged(&last_mode, mode)) {
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED, mode->w, mode->h);
}
}

const SDL_DisplayMode *SDL_GetCurrentDisplayMode(SDL_DisplayID displayID)
Expand Down
2 changes: 1 addition & 1 deletion src/video/uikit/SDL_uikitmodes.m
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ void SDL_OnApplicationDidChangeStatusBarOrientation(void)
default:
break;
}
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, orientation);
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ORIENTATION, orientation, 0);
}
}
#endif /* !SDL_PLATFORM_TVOS */
Expand Down
2 changes: 1 addition & 1 deletion src/video/wayland/SDL_waylandvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ static void display_handle_done(void *data,
SDL_zero(driverdata->placeholder);
}
} else {
SDL_SendDisplayEvent(dpy, SDL_EVENT_DISPLAY_ORIENTATION, driverdata->orientation);
SDL_SendDisplayEvent(dpy, SDL_EVENT_DISPLAY_ORIENTATION, driverdata->orientation, 0);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/video/windows/SDL_windowsmodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,9 @@ static void WIN_AddDisplay(SDL_VideoDevice *_this, HMONITOR hMonitor, const MONI
SDL_copyp(&driverdata->bounds, &bounds);
}
if (moved || changed_bounds) {
SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_MOVED, 0);
SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_MOVED, 0, 0);
}
SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_ORIENTATION, current_orientation);
SDL_SendDisplayEvent(existing_display, SDL_EVENT_DISPLAY_ORIENTATION, current_orientation, 0);
SDL_SetDisplayContentScale(existing_display, content_scale);
#ifdef HAVE_DXGI1_6_H
SDL_HDROutputProperties HDR;
Expand Down Expand Up @@ -832,7 +832,7 @@ void WIN_RefreshDisplays(SDL_VideoDevice *_this)
SDL_VideoDisplay *display = _this->displays[i];
SDL_DisplayData *driverdata = display->driverdata;
if (driverdata->state == DisplayAdded) {
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ADDED, 0);
SDL_SendDisplayEvent(display, SDL_EVENT_DISPLAY_ADDED, 0, 0);
}
}
}
Expand Down

0 comments on commit 676c50a

Please sign in to comment.