ndless_sdl/
wm.rs

1use core::mem;
2use core::ptr;
3use core::str;
4use cstr_core::{CStr, CString};
5use ndless::prelude::*;
6
7pub mod ll {
8	#![allow(non_camel_case_types)]
9
10	use cty::{c_char as c_schar, c_int, uint8_t};
11
12	use crate::video::ll::SDL_Surface;
13
14	pub type SDL_GrabMode = c_int;
15
16	pub const SDL_GRAB_QUERY: SDL_GrabMode = -1;
17	pub const SDL_GRAB_OFF: SDL_GrabMode = 0;
18	pub const SDL_GRAB_ON: SDL_GrabMode = 1;
19	pub const SDL_GRAB_FULLSCREEN: SDL_GrabMode = 2;
20
21	extern "C" {
22		pub fn SDL_WM_SetCaption(title: *const c_schar, icon: *const c_schar);
23		pub fn SDL_WM_GetCaption(title: *mut *mut c_schar, icon: *mut *mut c_schar);
24		pub fn SDL_WM_SetIcon(icon: *mut SDL_Surface, mask: *mut uint8_t);
25		pub fn SDL_WM_IconifyWindow() -> c_int;
26		pub fn SDL_WM_ToggleFullScreen(surface: *mut SDL_Surface) -> c_int;
27		pub fn SDL_WM_GrabInput(mode: SDL_GrabMode) -> SDL_GrabMode;
28	}
29}
30
31#[derive(PartialEq, Eq, Copy, Clone)]
32pub enum GrabMode {
33	Query = ll::SDL_GRAB_QUERY as isize,
34	Off = ll::SDL_GRAB_OFF as isize,
35	On = ll::SDL_GRAB_ON as isize,
36}
37
38pub fn set_caption(title: &str, icon: &str) {
39	unsafe {
40		ll::SDL_WM_SetCaption(
41			CString::new(title.as_bytes()).unwrap().as_ptr(),
42			CString::new(icon.as_bytes()).unwrap().as_ptr(),
43		);
44	}
45}
46
47pub fn get_caption() -> (String, String) {
48	let mut title_buf = ptr::null_mut();
49	let mut icon_buf = ptr::null_mut();
50	let mut title = String::new();
51	let mut icon = String::new();
52
53	unsafe {
54		ll::SDL_WM_GetCaption(&mut title_buf, &mut icon_buf);
55
56		if !title_buf.is_null() {
57			let slice = CStr::from_ptr(mem::transmute_copy(&title_buf)).to_bytes();
58			title = str::from_utf8(slice).unwrap().to_string();
59		}
60
61		if !icon_buf.is_null() {
62			let slice = CStr::from_ptr(mem::transmute_copy(&icon_buf)).to_bytes();
63			icon = str::from_utf8(slice).unwrap().to_string();
64		}
65
66		(title, icon)
67	}
68}
69
70pub fn set_icon(surface: crate::video::Surface) {
71	unsafe {
72		ll::SDL_WM_SetIcon(surface.raw, ptr::null_mut());
73	}
74}
75
76pub fn iconify_window() {
77	unsafe {
78		ll::SDL_WM_IconifyWindow();
79	}
80}
81
82pub fn toggle_fullscreen(surface: crate::video::Surface) {
83	unsafe {
84		ll::SDL_WM_ToggleFullScreen(surface.raw);
85	}
86}
87
88pub fn grab_input(mode: GrabMode) {
89	unsafe {
90		ll::SDL_WM_GrabInput(mode as i32);
91	}
92}
93
94pub fn toggle_grab_input() {
95	unsafe {
96		if ll::SDL_WM_GrabInput(GrabMode::Query as i32) == GrabMode::On as i32 {
97			ll::SDL_WM_GrabInput(GrabMode::Off as i32);
98		} else {
99			ll::SDL_WM_GrabInput(GrabMode::On as i32);
100		}
101	}
102}
103
104pub fn is_grabbing_input() -> bool {
105	unsafe { ll::SDL_WM_GrabInput(GrabMode::Query as i32) == GrabMode::On as i32 }
106}
107
108// TODO: get_wm_info