sdl/
wm.rs

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