1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use core::mem;
use core::ptr;
use core::str;
use cstr_core::{CStr, CString};
use ndless::prelude::*;

pub mod ll {
	#![allow(non_camel_case_types)]

	use cty::{c_char as c_schar, c_int, uint8_t};

	use crate::video::ll::SDL_Surface;

	pub type SDL_GrabMode = c_int;

	pub const SDL_GRAB_QUERY: SDL_GrabMode = -1;
	pub const SDL_GRAB_OFF: SDL_GrabMode = 0;
	pub const SDL_GRAB_ON: SDL_GrabMode = 1;
	pub const SDL_GRAB_FULLSCREEN: SDL_GrabMode = 2;

	extern "C" {
		pub fn SDL_WM_SetCaption(title: *const c_schar, icon: *const c_schar);
		pub fn SDL_WM_GetCaption(title: *mut *mut c_schar, icon: *mut *mut c_schar);
		pub fn SDL_WM_SetIcon(icon: *mut SDL_Surface, mask: *mut uint8_t);
		pub fn SDL_WM_IconifyWindow() -> c_int;
		pub fn SDL_WM_ToggleFullScreen(surface: *mut SDL_Surface) -> c_int;
		pub fn SDL_WM_GrabInput(mode: SDL_GrabMode) -> SDL_GrabMode;
	}
}

#[derive(PartialEq, Eq, Copy, Clone)]
pub enum GrabMode {
	Query = ll::SDL_GRAB_QUERY as isize,
	Off = ll::SDL_GRAB_OFF as isize,
	On = ll::SDL_GRAB_ON as isize,
}

pub fn set_caption(title: &str, icon: &str) {
	unsafe {
		ll::SDL_WM_SetCaption(
			CString::new(title.as_bytes()).unwrap().as_ptr(),
			CString::new(icon.as_bytes()).unwrap().as_ptr(),
		);
	}
}

pub fn get_caption() -> (String, String) {
	let mut title_buf = ptr::null_mut();
	let mut icon_buf = ptr::null_mut();
	let mut title = String::new();
	let mut icon = String::new();

	unsafe {
		ll::SDL_WM_GetCaption(&mut title_buf, &mut icon_buf);

		if !title_buf.is_null() {
			let slice = CStr::from_ptr(mem::transmute_copy(&title_buf)).to_bytes();
			title = str::from_utf8(slice).unwrap().to_string();
		}

		if !icon_buf.is_null() {
			let slice = CStr::from_ptr(mem::transmute_copy(&icon_buf)).to_bytes();
			icon = str::from_utf8(slice).unwrap().to_string();
		}

		(title, icon)
	}
}

pub fn set_icon(surface: crate::video::Surface) {
	unsafe {
		ll::SDL_WM_SetIcon(surface.raw, ptr::null_mut());
	}
}

pub fn iconify_window() {
	unsafe {
		ll::SDL_WM_IconifyWindow();
	}
}

pub fn toggle_fullscreen(surface: crate::video::Surface) {
	unsafe {
		ll::SDL_WM_ToggleFullScreen(surface.raw);
	}
}

pub fn grab_input(mode: GrabMode) {
	unsafe {
		ll::SDL_WM_GrabInput(mode as i32);
	}
}

pub fn toggle_grab_input() {
	unsafe {
		if ll::SDL_WM_GrabInput(GrabMode::Query as i32) == GrabMode::On as i32 {
			ll::SDL_WM_GrabInput(GrabMode::Off as i32);
		} else {
			ll::SDL_WM_GrabInput(GrabMode::On as i32);
		}
	}
}

pub fn is_grabbing_input() -> bool {
	unsafe { ll::SDL_WM_GrabInput(GrabMode::Query as i32) == GrabMode::On as i32 }
}

// TODO: get_wm_info