sdl2/
hint.rs

1use crate::sys;
2use libc::c_char;
3use std::ffi::{CStr, CString};
4
5const VIDEO_MINIMIZE_ON_FOCUS_LOSS: &str = "SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS";
6
7pub enum Hint {
8    Default,
9    Normal,
10    Override,
11}
12
13/// A hint that specifies whether a fullscreen [Window](../video/Window.t.html) will be
14/// minimized if key focus is lost.
15///
16/// [Official SDL documentation](https://wiki.libsdl.org/SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS)
17///
18/// # Default
19/// This is enabled by default.
20///
21/// # Example
22/// ```rust,no_run
23/// sdl2::hint::set_video_minimize_on_focus_loss(false);
24/// ```
25///
26/// * `value`: `true` to enable minimizing of the Window if it loses key focus when in fullscreen mode,
27///            `false` to disable this feature.
28pub fn set_video_minimize_on_focus_loss(value: bool) -> bool {
29    set(VIDEO_MINIMIZE_ON_FOCUS_LOSS, if value { "1" } else { "0" })
30}
31
32/// A hint that specifies whether a fullscreen [Window](../video/Window.t.html) will be
33/// minimized if key focus is lost.
34///
35/// [Official SDL documentation](https://wiki.libsdl.org/SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS)
36///
37/// # Example
38/// ```rust,no_run
39/// sdl2::hint::set_video_minimize_on_focus_loss_with_priority(false, &sdl2::hint::Hint::Override);
40/// ```
41///
42/// * `value`: `true` to enable minimizing of the Window if it loses key focus when in fullscreen mode,
43///            `false` to disable this feature.
44/// * `priority`: The priority controls the behavior when setting a hint that already has a value.
45///               Hints will replace existing hints of their priority and lower.
46///               Environment variables are considered to have override priority.
47pub fn set_video_minimize_on_focus_loss_with_priority(value: bool, priority: &Hint) -> bool {
48    set_with_priority(
49        VIDEO_MINIMIZE_ON_FOCUS_LOSS,
50        if value { "1" } else { "0" },
51        priority,
52    )
53}
54
55/// A hint that specifies whether a fullscreen [Window](../video/Window.t.html) will be
56/// minimized if key focus is lost.
57///
58/// [Official SDL documentation](https://wiki.libsdl.org/SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS)
59///
60/// # Default
61/// By default this will return `true`.
62///
63/// # Example
64/// ```rust,no_run
65/// assert_eq!(sdl2::hint::get_video_minimize_on_focus_loss(), true);
66///
67/// sdl2::hint::set_video_minimize_on_focus_loss(false);
68/// assert_eq!(sdl2::hint::get_video_minimize_on_focus_loss(), false);
69/// ```
70pub fn get_video_minimize_on_focus_loss() -> bool {
71    matches!(
72        get(VIDEO_MINIMIZE_ON_FOCUS_LOSS).as_deref(),
73        Some("1") | None
74    )
75}
76
77#[doc(alias = "SDL_SetHint")]
78pub fn set(name: &str, value: &str) -> bool {
79    let name = CString::new(name).unwrap();
80    let value = CString::new(value).unwrap();
81    unsafe {
82        sys::SDL_SetHint(
83            name.as_ptr() as *const c_char,
84            value.as_ptr() as *const c_char,
85        ) == sys::SDL_bool::SDL_TRUE
86    }
87}
88
89#[doc(alias = "SDL_GetHint")]
90pub fn get(name: &str) -> Option<String> {
91    use std::str;
92
93    let name = CString::new(name).unwrap();
94
95    unsafe {
96        let res = sys::SDL_GetHint(name.as_ptr() as *const c_char);
97
98        if res.is_null() {
99            None
100        } else {
101            Some(
102                str::from_utf8(CStr::from_ptr(res as *const _).to_bytes())
103                    .unwrap()
104                    .to_owned(),
105            )
106        }
107    }
108}
109
110#[doc(alias = "SDL_SetHintWithPriority")]
111pub fn set_with_priority(name: &str, value: &str, priority: &Hint) -> bool {
112    let name = CString::new(name).unwrap();
113    let value = CString::new(value).unwrap();
114
115    let priority_val = match *priority {
116        Hint::Normal => sys::SDL_HintPriority::SDL_HINT_NORMAL,
117        Hint::Override => sys::SDL_HintPriority::SDL_HINT_OVERRIDE,
118        Hint::Default => sys::SDL_HintPriority::SDL_HINT_DEFAULT,
119    };
120
121    unsafe {
122        sys::SDL_SetHintWithPriority(
123            name.as_ptr() as *const c_char,
124            value.as_ptr() as *const c_char,
125            priority_val,
126        ) == sys::SDL_bool::SDL_TRUE
127    }
128}