Skip to content

Commit

Permalink
Merge pull request #1366 from serpilliere/primary_selection
Browse files Browse the repository at this point in the history
Add primary selection API
  • Loading branch information
Cobrand authored Jan 30, 2024
2 parents b654517 + ffbdd54 commit 6a78af8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
45 changes: 45 additions & 0 deletions sdl2-sys/sdl_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
34 changes: 34 additions & 0 deletions src/sdl2/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,38 @@ 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<String, String> {
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 }
}
}
4 changes: 3 additions & 1 deletion src/sdl2/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6a78af8

Please sign in to comment.