fltk/app/
init.rs

1use fltk_sys::fl;
2use std::sync::{
3    Arc, LazyLock, Mutex,
4    atomic::{AtomicBool, Ordering},
5};
6
7/// Basically a check for global locking
8pub(crate) static IS_INIT: AtomicBool = AtomicBool::new(false);
9
10/// The fonts associated with the application
11pub(crate) static FONTS: LazyLock<Arc<Mutex<Vec<String>>>> = LazyLock::new(|| {
12    Arc::from(Mutex::from(vec![
13        "Helvetica".to_owned(),
14        "HelveticaBold".to_owned(),
15        "HelveticaItalic".to_owned(),
16        "HelveticaBoldItalic".to_owned(),
17        "Courier".to_owned(),
18        "CourierBold".to_owned(),
19        "CourierItalic".to_owned(),
20        "CourierBoldItalic".to_owned(),
21        "Times".to_owned(),
22        "TimesBold".to_owned(),
23        "TimesItalic".to_owned(),
24        "TimesBoldItalic".to_owned(),
25        "Symbol".to_owned(),
26        "Screen".to_owned(),
27        "ScreenBold".to_owned(),
28        "Zapfdingbats".to_owned(),
29    ]))
30});
31static UI_THREAD: LazyLock<std::thread::ThreadId> = LazyLock::new(|| std::thread::current().id());
32
33/// Inits all styles, fonts and images available to FLTK.
34/// Also initializes global locking
35/// # Panics
36/// If the current environment lacks threading support. Practically this should never happen!
37pub fn init_all() {
38    unsafe {
39        fl::Fl_init_all();
40        #[cfg(not(feature = "single-threaded"))]
41        assert!((fl::Fl_lock() == 0), "fltk-rs requires threading support!");
42        #[cfg(feature = "enable-glwindow")]
43        {
44            unsafe extern "C" {
45                pub fn open_gl() -> i32;
46            }
47            open_gl();
48        }
49        if !IS_INIT.load(Ordering::Relaxed) {
50            IS_INIT.store(true, Ordering::Relaxed);
51        }
52    }
53}
54
55#[doc(hidden)]
56/// Check whether we're in the ui thread
57pub fn is_ui_thread() -> bool {
58    *UI_THREAD == std::thread::current().id()
59}
60
61/// Check if fltk-rs was initialized
62pub fn is_initialized() -> bool {
63    IS_INIT.load(Ordering::Relaxed)
64}
65
66/// Disables wayland when building with use-wayland.
67/// Needs to be called before showing the first window
68pub fn disable_wayland() {
69    unsafe { fl::Fl_disable_wayland() }
70}