[go: up one dir, main page]

sdl2/
clipboard.rs

1use crate::get_error;
2use libc::c_char;
3use libc::c_void;
4use std::ffi::{CStr, CString};
5
6use crate::sys;
7
8/// Clipboard utility functions. Access with `VideoSubsystem::clipboard()`.
9///
10/// These functions require the video subsystem to be initialized.
11///
12/// ```no_run
13/// let sdl_context = sdl2::init().unwrap();
14/// let video_subsystem = sdl_context.video().unwrap();
15///
16/// video_subsystem.clipboard().set_clipboard_text("Hello World!").unwrap();
17/// ```
18pub struct ClipboardUtil {
19    _subsystem: crate::VideoSubsystem,
20}
21
22impl crate::VideoSubsystem {
23    #[inline]
24    pub fn clipboard(&self) -> ClipboardUtil {
25        ClipboardUtil {
26            _subsystem: self.clone(),
27        }
28    }
29}
30
31impl ClipboardUtil {
32    #[doc(alias = "SDL_SetClipboardText")]
33    pub fn set_clipboard_text(&self, text: &str) -> Result<(), String> {
34        unsafe {
35            let text = CString::new(text).unwrap();
36            let result = sys::SDL_SetClipboardText(text.as_ptr() as *const c_char);
37
38            if result != 0 {
39                Err(get_error())
40            } else {
41                Ok(())
42            }
43        }
44    }
45
46    #[doc(alias = "SDL_GetClipboardText")]
47    pub fn clipboard_text(&self) -> Result<String, String> {
48        unsafe {
49            let buf = sys::SDL_GetClipboardText();
50
51            if buf.is_null() {
52                Err(get_error())
53            } else {
54                let s = CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned();
55                sys::SDL_free(buf as *mut c_void);
56                Ok(s)
57            }
58        }
59    }
60
61    #[doc(alias = "SDL_HasClipboardText")]
62    pub fn has_clipboard_text(&self) -> bool {
63        unsafe { sys::SDL_HasClipboardText() == sys::SDL_bool::SDL_TRUE }
64    }
65
66    #[doc(alias = "SDL_SetPrimarySelectionText")]
67    pub fn set_primary_selection_text(&self, text: &str) -> Result<(), String> {
68        unsafe {
69            let text = CString::new(text).unwrap();
70            let result = sys::SDL_SetPrimarySelectionText(text.as_ptr() as *const c_char);
71
72            if result != 0 {
73                Err(get_error())
74            } else {
75                Ok(())
76            }
77        }
78    }
79
80    #[doc(alias = "SDL_GetPrimarySelectionText")]
81    pub fn primary_selection_text(&self) -> Result<String, String> {
82        unsafe {
83            let buf = sys::SDL_GetPrimarySelectionText();
84
85            if buf.is_null() {
86                Err(get_error())
87            } else {
88                let s = CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned();
89                sys::SDL_free(buf as *mut c_void);
90                Ok(s)
91            }
92        }
93    }
94
95    #[doc(alias = "SDL_HasPrimarySelectionText")]
96    pub fn has_primary_selection_text(&self) -> bool {
97        unsafe { sys::SDL_HasPrimarySelectionText() == sys::SDL_bool::SDL_TRUE }
98    }
99}