From a995dfe6a8a2a8e4f643d5d44ba5cb5ae4d2c78b Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Sat, 20 Jan 2024 20:59:00 +0100 Subject: [PATCH 1/2] Add primary selection API --- sdl2-sys/sdl_bindings.rs | 45 ++++++++++++++++++++++++++++++++++++++++ src/sdl2/clipboard.rs | 35 +++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/sdl2-sys/sdl_bindings.rs b/sdl2-sys/sdl_bindings.rs index bc75efca531..b2bc6ca8ea8 100644 --- a/sdl2-sys/sdl_bindings.rs +++ b/sdl2-sys/sdl_bindings.rs @@ -5371,6 +5371,51 @@ extern "C" { #[doc = " \\sa SDL_SetClipboardText"] pub fn SDL_HasClipboardText() -> SDL_bool; } +extern "C" { + #[doc = " Put UTF-8 text into the primary selection."] + #[doc = ""] + #[doc = "\\param text the text to store in the primary selection"] + #[doc = "\\returns 0 on success or a negative error code on failure; call"] + #[doc = " SDL_GetError() for more information."] + #[doc = ""] + #[doc = "\\since This function is available since SDL 2.26.0."] + #[doc = ""] + #[doc = "\\sa SDL_GetPrimarySelectionText"] + #[doc = "\\sa SDL_HasPrimarySelectionText"] + pub fn SDL_SetPrimarySelectionText(text: *const libc::c_char) -> libc::c_int; +} +extern "C" { + #[doc = " Get UTF-8 text from the primary selection, which must be freed with"] + #[doc = "SDL_free()."] + #[doc = ""] + #[doc = "This functions returns empty string if there was not enough memory left for"] + #[doc = "a copy of the primary selection's content."] + #[doc = ""] + #[doc = "\\returns the primary selection text on success or an empty string on"] + #[doc = " failure; call SDL_GetError() for more information. Caller must"] + #[doc = " call SDL_free() on the returned pointer when done with it (even if"] + #[doc = " there was an error)."] + #[doc = ""] + #[doc = "\\since This function is available since SDL 2.26.0."] + #[doc = ""] + #[doc = "\\sa SDL_HasPrimarySelectionText"] + #[doc = "\\sa SDL_SetPrimarySelectionText"] + pub fn SDL_GetPrimarySelectionText() -> *mut libc::c_char; +} +extern "C" { + + #[doc = "Query whether the primary selection exists and contains a non-empty text"] + #[doc = "string."] + #[doc = ""] + #[doc = "\\returns SDL_TRUE if the primary selection has text, or SDL_FALSE if it"] + #[doc = " does not."] + #[doc = ""] + #[doc = "\\since This function is available since SDL 2.26.0."] + #[doc = ""] + #[doc = "\\sa SDL_GetPrimarySelectionText"] + #[doc = "\\sa SDL_SetPrimarySelectionText"] + pub fn SDL_HasPrimarySelectionText() -> SDL_bool; +} pub type __m64 = [libc::c_longlong; 1usize]; pub type __v1di = [libc::c_longlong; 1usize]; pub type __v2si = [libc::c_int; 2usize]; diff --git a/src/sdl2/clipboard.rs b/src/sdl2/clipboard.rs index 4eb14edde8f..52af61d964d 100644 --- a/src/sdl2/clipboard.rs +++ b/src/sdl2/clipboard.rs @@ -62,4 +62,39 @@ impl ClipboardUtil { pub fn has_clipboard_text(&self) -> bool { unsafe { sys::SDL_HasClipboardText() == sys::SDL_bool::SDL_TRUE } } + + #[doc(alias = "SDL_SetPrimarySelectionText")] + pub fn set_primary_selection_text(&self, text: &str) -> Result<(), String> { + unsafe { + let text = CString::new(text).unwrap(); + let result = sys::SDL_SetPrimarySelectionText(text.as_ptr() as *const c_char); + + if result != 0 { + Err(get_error()) + } else { + Ok(()) + } + } + } + + #[doc(alias = "SDL_GetPrimarySelectionText")] + pub fn primary_selection_text(&self) -> Result { + unsafe { + let buf = sys::SDL_GetPrimarySelectionText(); + + if buf.is_null() { + Err(get_error()) + } else { + let s = CStr::from_ptr(buf as *const _).to_str().unwrap().to_owned(); + sys::SDL_free(buf as *mut c_void); + Ok(s) + } + } + } + + #[doc(alias = "SDL_HasPrimarySelectionText")] + pub fn has_primary_selection_text(&self) -> bool { + unsafe { sys::SDL_HasPrimarySelectionText() == sys::SDL_bool::SDL_TRUE } + } + } From ffbdd5452f099b4afb8e3842efef5bcd82e34c9a Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Sat, 20 Jan 2024 21:07:40 +0100 Subject: [PATCH 2/2] Fix fmt --- src/sdl2/clipboard.rs | 1 - src/sdl2/video.rs | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sdl2/clipboard.rs b/src/sdl2/clipboard.rs index 52af61d964d..3a7c1e9ffa6 100644 --- a/src/sdl2/clipboard.rs +++ b/src/sdl2/clipboard.rs @@ -96,5 +96,4 @@ impl ClipboardUtil { pub fn has_primary_selection_text(&self) -> bool { unsafe { sys::SDL_HasPrimarySelectionText() == sys::SDL_bool::SDL_TRUE } } - } diff --git a/src/sdl2/video.rs b/src/sdl2/video.rs index 329d5122aea..d4f879ffc4f 100644 --- a/src/sdl2/video.rs +++ b/src/sdl2/video.rs @@ -1564,7 +1564,9 @@ impl Window { binarization_cutoff: u8, ) -> Result<(), i32> { let mode = sys::WindowShapeMode::ShapeModeBinarizeAlpha; - let parameters = sys::SDL_WindowShapeParams { binarizationCutoff: binarization_cutoff }; + let parameters = sys::SDL_WindowShapeParams { + binarizationCutoff: binarization_cutoff, + }; let mut shape_mode = sys::SDL_WindowShapeMode { mode, parameters }; let result = unsafe { sys::SDL_SetWindowShape(self.context.raw, shape.as_ref().raw(), &mut shape_mode)