Skip to content

Commit b8e726d

Browse files
author
Semphris
committed
System preferences
1 parent f3d4e6f commit b8e726d

19 files changed

+1308
-7
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,7 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
17351735
sdl_sources(
17361736
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_dbus.c"
17371737
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_system_theme.c"
1738+
"${SDL3_SOURCE_DIR}/src/core/linux/SDL_system_preferences.c"
17381739
)
17391740
endif()
17401741

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
@@ -585,6 +585,131 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentVideoDriver(void);
585585
*/
586586
extern SDL_DECLSPEC SDL_SystemTheme SDLCALL SDL_GetSystemTheme(void);
587587

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

0 commit comments

Comments
 (0)