Skip to content

Commit 2ef4089

Browse files
author
Semphris
committed
System preferences
1 parent 69d2802 commit 2ef4089

19 files changed

+1308
-7
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,7 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
17321732
sdl_sources(
17331733
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_dbus.c"
17341734
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_system_theme.c"
1735+
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_system_preferences.c"
17351736
)
17361737
endif()
17371738

include/SDL3/SDL_events.h

+23
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ typedef enum SDL_EventType
118118

119119
SDL_EVENT_SYSTEM_THEME_CHANGED, /**< The system theme changed */
120120

121+
SDL_EVENT_SYSTEM_PREFERENCE_CHANGED, /**< A system preference setting changed */
122+
SDL_EVENT_SYSTEM_TEXT_SCALE_CHANGED, /**< The text scale changed */
123+
SDL_EVENT_SYSTEM_CURSOR_SCALE_CHANGED, /**< The cursor scale changed */
124+
SDL_EVENT_SYSTEM_ACCENT_COLOR_CHANGED, /**< The accent color changed */
125+
121126
/* Display events */
122127
/* 0x150 was SDL_DISPLAYEVENT, reserve the number for sdl2-compat */
123128
SDL_EVENT_DISPLAY_ORIENTATION = 0x151, /**< Display orientation has changed to data1 */
@@ -925,6 +930,23 @@ typedef struct SDL_ClipboardEvent
925930
const char **mime_types; /**< current mime types */
926931
} SDL_ClipboardEvent;
927932

933+
/**
934+
* An event triggered when a system preference has changed (event.pref.*)
935+
*
936+
* Note that some platforms may provide certain settings, but not allow
937+
* listening to changes; as such, there may be certain preferences that can be
938+
* fetched but that won't produce events when they are changed.
939+
*
940+
* \since This struct is available since SDL 3.4.0.
941+
*/
942+
typedef struct SDL_PreferenceEvent
943+
{
944+
SDL_EventType type; /**< SDL_EVENT_SYSTEM_PREFERENCE_CHANGED */
945+
Uint32 reserved;
946+
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
947+
SDL_SystemPreference pref; /**< The preference setting that changed */
948+
} SDL_PreferenceEvent;
949+
928950
/**
929951
* Sensor event structure (event.sensor.*)
930952
*
@@ -1023,6 +1045,7 @@ typedef union SDL_Event
10231045
SDL_RenderEvent render; /**< Render event data */
10241046
SDL_DropEvent drop; /**< Drag and drop event data */
10251047
SDL_ClipboardEvent clipboard; /**< Clipboard event data */
1048+
SDL_PreferenceEvent pref; /**< Preference event data */
10261049

10271050
/* This is necessary for ABI compatibility between Visual C++ and GCC.
10281051
Visual C++ will respect the push pack pragma and use 52 bytes (size of

include/SDL3/SDL_video.h

+125
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,131 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentVideoDriver(void);
570570
*/
571571
extern SDL_DECLSPEC SDL_SystemTheme SDLCALL SDL_GetSystemTheme(void);
572572

573+
/**
574+
* An enumeration of various boolean system preferences.
575+
*
576+
* Some systems provide a variety of accessibility options that allow users to
577+
* adapt their environment to various conditions.
578+
*
579+
* The preference names have been chosen so that `true` indicate a positive
580+
* preference, whereas `false` indicate no preference. Some systems describe
581+
* their preferences negatively, like "Enable animations" on Windows, which is
582+
* `true` by default (no preference) and `false` when the user wants to disable
583+
* animations. In these situations, SDL inverts the preferences so that `false`
584+
* correspond to the situation where no particular change is needed.
585+
*
586+
* \since This enum is available since SDL 3.4.0.
587+
*
588+
* \sa SDL_GetSystemPreference
589+
*/
590+
typedef enum SDL_SystemPreference
591+
{
592+
SDL_SYSTEM_PREFERENCE_REDUCED_MOTION, /**< Disable smooth graphical transitions */
593+
SDL_SYSTEM_PREFERENCE_REDUCED_TRANSPARENCY, /**< Reduce usage of semi-transparent objects */
594+
SDL_SYSTEM_PREFERENCE_HIGH_CONTRAST, /**< Use extreme color differences between different elements of the interface */
595+
SDL_SYSTEM_PREFERENCE_COLORBLIND, /**< Add shape-based distinction between color-coded elements, for example "0" and "1" on switches */
596+
SDL_SYSTEM_PREFERENCE_PERSIST_SCROLLBARS, /**< Always show scrollbars, don't hide them after a few seconds of inactivity */
597+
SDL_SYSTEM_PREFERENCE_SCREEN_READER, /**< A screen reader (text-to-speech OR braille) is currently active */
598+
} SDL_SystemPreference;
599+
600+
/**
601+
* Get whether or not a certain system preference was enabled by the user.
602+
*
603+
* Some preferences may not be supported on some platforms; this function will
604+
* return false by default in this case. The preference names have been chosen
605+
* so that `true` indicate a positive preference, whereas `false` indicate no
606+
* preference.
607+
*
608+
* This setting will emit a `SDL_EVENT_SYSTEM_PREFERENCE_CHANGED` when the user
609+
* updates the corresponding preference in their settings or accessibility app.
610+
* Note that some platforms may provide certain settings, but not allow
611+
* listening to changes; as such, there may be certain preferences that can be
612+
* fetched but that won't produce events when they are changed.
613+
*
614+
* \param preference the preference to be fetched.
615+
* \returns true if the user enabled the system preference; false if the user
616+
* did not activate the setting, or the setting doesn't exist on the
617+
* current platform, or an error occured.
618+
*
619+
* \threadsafety This function should only be called on the main thread.
620+
*
621+
* \since This function is available since SDL 3.4.0.
622+
*
623+
* \sa SDL_SystemPreference
624+
* \sa SDL_PreferenceEvent
625+
*/
626+
extern SDL_DECLSPEC bool SDLCALL SDL_GetSystemPreference(SDL_SystemPreference preference);
627+
628+
/**
629+
* Get the system's accent color, as chosen by the user.
630+
*
631+
* If the current system does not have an accent color, false is returned and
632+
* the struct is unaffected.
633+
*
634+
* This setting will emit a `SDL_EVENT_SYSTEM_ACCENT_COLOR_CHANGED` when the
635+
* user updates the corresponding preference in their settings or accessibility
636+
* app. Note that some platforms may provide certain settings, but not allow
637+
* listening to changes; as such, there may be certain preferences that can be
638+
* fetched but that won't produce events when they are changed.
639+
*
640+
* \param color a pointer to a struct to be filled with the color info. The
641+
* alpha channel corresponds to the value returned by operating
642+
* system, which is not necessarily equivalent to SDL_ALPHA_OPAQUE.
643+
* \returns true on success or false on failure; call SDL_GetError() for more
644+
* information.
645+
*
646+
* \threadsafety This function should only be called on the main thread.
647+
*
648+
* \since This function is available since SDL 3.4.0.
649+
*/
650+
extern SDL_DECLSPEC bool SDLCALL SDL_GetSystemAccentColor(SDL_Color *color);
651+
652+
/**
653+
* Get the scale factor for text, as set by the user for their system.
654+
*
655+
* Some systems do not have a notion of text scale factor, but instead provide
656+
* a base font size. In this case, SDL calculates a scaling factor by dividing
657+
* the given font size by 16 pixels.
658+
*
659+
* If the system does not have a setting to scale the font, 1 is returned.
660+
*
661+
* This setting will emit a `SDL_EVENT_SYSTEM_TEXT_SCALE_CHANGED` when the user
662+
* updates the corresponding preference in their settings or accessibility app.
663+
* Note that some platforms may provide certain settings, but not allow
664+
* listening to changes; as such, there may be certain preferences that can be
665+
* fetched but that won't produce events when they are changed.
666+
*
667+
* \returns the preferred scale for text; a scale of 1 means no scaling.
668+
*
669+
* \threadsafety This function should only be called on the main thread.
670+
*
671+
* \since This function is available since SDL 3.4.0.
672+
*/
673+
extern SDL_DECLSPEC float SDLCALL SDL_GetSystemTextScale(void);
674+
675+
/**
676+
* Get the scale factor for the cursor, as set by the user for their system.
677+
*
678+
* Some systems do not have a notion of cursor scale factor, but instead provide
679+
* a base cursor size. In this case, SDL calculates a scaling factor by dividing
680+
* the given cursor lateral size by 32 pixels.
681+
*
682+
* If the system does not have a setting to scale the cursor, 1 is returned.
683+
*
684+
* This setting will emit a `SDL_EVENT_SYSTEM_CURSOR_SCALE_CHANGED` when the
685+
* user updates the corresponding preference in their settings or accessibility
686+
* app. Note that some platforms may provide certain settings, but not allow
687+
* listening to changes; as such, there may be certain preferences that can be
688+
* fetched but that won't produce events when they are changed.
689+
*
690+
* \returns the preferred scale for the cursor; a scale of 1 means no scaling.
691+
*
692+
* \threadsafety This function should only be called on the main thread.
693+
*
694+
* \since This function is available since SDL 3.4.0.
695+
*/
696+
extern SDL_DECLSPEC float SDLCALL SDL_GetSystemCursorScale(void);
697+
573698
/**
574699
* Get a list of currently connected displays.
575700
*

0 commit comments

Comments
 (0)