#![allow(clippy::missing_safety_doc)]
pub mod event;
pub mod keysym;
mod keysym_table;
pub mod xkb;
pub mod icon;
pub mod install;
#[cfg(all(target_os = "linux", not(cosmo)))]
pub mod sys;
#[cfg(cosmo)]
#[path = "sys_cosmo.rs"]
pub mod sys;
#[cfg(target_os = "linux")]
mod shm;
#[cfg(target_os = "linux")]
pub mod x11_conn;
#[cfg(target_os = "linux")]
mod x11;
#[cfg(target_os = "linux")]
mod wayland;
#[cfg(any(target_os = "macos", cosmo))]
mod mac_sys;
#[cfg(any(target_os = "macos", cosmo))]
mod mac;
#[cfg(any(target_os = "windows", cosmo))]
pub mod sys_win;
#[cfg(any(target_os = "windows", cosmo))]
mod scancode_win;
#[cfg(any(target_os = "windows", cosmo))]
mod win_d3d;
#[cfg(any(target_os = "windows", cosmo))]
mod win;
pub use event::*;
use std::sync::Arc;
use std::sync::atomic::Ordering::*;
pub fn next_pow2(n: u32) -> u32 { let mut s = 256u32; while s < n { s <<= 1; } s }
enum Backend {
#[cfg(target_os = "linux")]
X11(x11::App),
#[cfg(target_os = "linux")]
Wayland(wayland::App),
#[cfg(any(target_os = "macos", cosmo))]
Mac(mac::App),
#[cfg(any(target_os = "windows", cosmo))]
Win(win::App),
}
pub struct Gui {
core: Arc<Core>,
back: Backend,
}
#[derive(Clone, Copy, Debug)]
pub struct Framebuffer {
pub pixels: *mut u32,
pub side: usize,
pub width: usize,
pub height: usize,
pub key: u64,
}
impl Framebuffer {
pub fn ok(&self) -> bool { !self.pixels.is_null() }
pub fn slice(&mut self) -> &mut [u32] { if self.pixels.is_null() { &mut [] } else { unsafe { core::slice::from_raw_parts_mut(self.pixels, self.side * self.side) } } }
}
pub fn open(title: &str, app_id: &str, width: u32, height: u32) -> Option<Gui> {
let core = Arc::new(Core::new());
#[cfg(cosmo)]
if mac_sys::on_macos() {
let _ = app_id;
return open_mac(core, title, width, height);
}
#[cfg(cosmo)]
if sys_win::cosmo::is_windows() {
let app = win::open(core.clone(), title, app_id, width, height)?;
return Some(Gui { core, back: Backend::Win(app) });
}
#[cfg(any(target_os = "linux", cosmo))]
{
let force_x11 = std::env::var("SOFTER_GUI_X11").map(|v| v == "1").unwrap_or(false);
if !force_x11 {
if let Some(app) = wayland::open(core.clone(), title, app_id, width, height) {
return Some(Gui { core, back: Backend::Wayland(app) });
}
}
let app = x11::open(core.clone(), title, app_id, width, height)?;
Some(Gui { core, back: Backend::X11(app) })
}
#[cfg(all(target_os = "macos", not(cosmo)))]
{
let _ = app_id;
open_mac(core, title, width, height)
}
#[cfg(all(target_os = "windows", not(cosmo)))]
{
let app = win::open(core.clone(), title, app_id, width, height)?;
Some(Gui { core, back: Backend::Win(app) })
}
}
#[cfg(any(target_os = "macos", cosmo))]
fn open_mac(core: Arc<Core>, title: &str, width: u32, height: u32) -> Option<Gui> {
let (app, pump) = mac::open(core.clone(), title, width, height)?;
if !mac::takeover_main_thread(pump) { return None; }
Some(Gui { core, back: Backend::Mac(app) })
}
impl Gui {
pub fn next_event(&mut self, ev: &mut Event) -> bool { self.core.next_event(ev) }
pub fn wait(&mut self) { self.wait_ms(0) }
pub fn wait_ms(&mut self, ms: u64) { self.core.wait(ms * 1_000_000) }
pub fn get_framebuffer(&mut self) -> Framebuffer {
let got = match &mut self.back {
#[cfg(target_os = "linux")]
Backend::X11(a) => a.get_framebuffer(),
#[cfg(target_os = "linux")]
Backend::Wayland(a) => a.get_framebuffer(),
#[cfg(any(target_os = "macos", cosmo))]
Backend::Mac(a) => a.get_framebuffer(),
#[cfg(any(target_os = "windows", cosmo))]
Backend::Win(a) => a.get_framebuffer(),
};
let Some((ptr, side, key)) = got else { return Framebuffer { pixels: core::ptr::null_mut(), side: 0, width: 0, height: 0, key: 0 } };
let side = side as usize;
let w = (self.core.win_w.load(Relaxed) as usize).min(side);
let h = (self.core.win_h.load(Relaxed) as usize).min(side);
Framebuffer { pixels: ptr, side, width: w, height: h, key }
}
pub fn submit(&mut self) {
match &mut self.back {
#[cfg(target_os = "linux")]
Backend::X11(a) => a.submit(),
#[cfg(target_os = "linux")]
Backend::Wayland(a) => a.submit(),
#[cfg(any(target_os = "macos", cosmo))]
Backend::Mac(a) => a.submit(),
#[cfg(any(target_os = "windows", cosmo))]
Backend::Win(a) => a.submit(),
}
}
pub fn request_frame(&mut self) { self.core.wants_frame.store(true, Relaxed); self.poke(); }
pub fn set_cursor_hidden(&mut self, hidden: bool) { self.core.cursor_hidden.store(hidden, Relaxed); self.poke(); }
pub fn set_fullscreen(&mut self, on: bool) { self.core.wants_fullscreen.store(on, Relaxed); self.poke(); }
pub fn is_fullscreen(&self) -> bool { self.core.wants_fullscreen.load(Relaxed) }
pub fn window_size(&self) -> (u32, u32) { (self.core.win_w.load(Relaxed), self.core.win_h.load(Relaxed)) }
pub fn take_full_redraw(&mut self) -> bool { self.core.full_redraw.swap(false, AcqRel) }
pub fn set_icon(&mut self, images: &[icon::IconImage]) { self.core.set_icon(icon::own_set(images)); self.poke(); }
pub fn period_fs(&self) -> u64 { self.core.period_fs() }
fn poke(&mut self) {
match &self.back {
#[cfg(target_os = "linux")]
Backend::Wayland(a) => a.poke(),
#[cfg(any(target_os = "windows", cosmo))]
Backend::Win(a) => a.poke(),
#[allow(unreachable_patterns)]
_ => {}
}
}
}
impl Drop for Gui {
fn drop(&mut self) { self.core.quit.store(true, Relaxed); self.poke(); }
}