Skip to content

Commit 6a78af8

Browse files
authored
Merge pull request #1366 from serpilliere/primary_selection
Add primary selection API
2 parents b654517 + ffbdd54 commit 6a78af8

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

sdl2-sys/sdl_bindings.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5371,6 +5371,51 @@ extern "C" {
53715371
#[doc = " \\sa SDL_SetClipboardText"]
53725372
pub fn SDL_HasClipboardText() -> SDL_bool;
53735373
}
5374+
extern "C" {
5375+
#[doc = " Put UTF-8 text into the primary selection."]
5376+
#[doc = ""]
5377+
#[doc = "\\param text the text to store in the primary selection"]
5378+
#[doc = "\\returns 0 on success or a negative error code on failure; call"]
5379+
#[doc = " SDL_GetError() for more information."]
5380+
#[doc = ""]
5381+
#[doc = "\\since This function is available since SDL 2.26.0."]
5382+
#[doc = ""]
5383+
#[doc = "\\sa SDL_GetPrimarySelectionText"]
5384+
#[doc = "\\sa SDL_HasPrimarySelectionText"]
5385+
pub fn SDL_SetPrimarySelectionText(text: *const libc::c_char) -> libc::c_int;
5386+
}
5387+
extern "C" {
5388+
#[doc = " Get UTF-8 text from the primary selection, which must be freed with"]
5389+
#[doc = "SDL_free()."]
5390+
#[doc = ""]
5391+
#[doc = "This functions returns empty string if there was not enough memory left for"]
5392+
#[doc = "a copy of the primary selection's content."]
5393+
#[doc = ""]
5394+
#[doc = "\\returns the primary selection text on success or an empty string on"]
5395+
#[doc = " failure; call SDL_GetError() for more information. Caller must"]
5396+
#[doc = " call SDL_free() on the returned pointer when done with it (even if"]
5397+
#[doc = " there was an error)."]
5398+
#[doc = ""]
5399+
#[doc = "\\since This function is available since SDL 2.26.0."]
5400+
#[doc = ""]
5401+
#[doc = "\\sa SDL_HasPrimarySelectionText"]
5402+
#[doc = "\\sa SDL_SetPrimarySelectionText"]
5403+
pub fn SDL_GetPrimarySelectionText() -> *mut libc::c_char;
5404+
}
5405+
extern "C" {
5406+
5407+
#[doc = "Query whether the primary selection exists and contains a non-empty text"]
5408+
#[doc = "string."]
5409+
#[doc = ""]
5410+
#[doc = "\\returns SDL_TRUE if the primary selection has text, or SDL_FALSE if it"]
5411+
#[doc = " does not."]
5412+
#[doc = ""]
5413+
#[doc = "\\since This function is available since SDL 2.26.0."]
5414+
#[doc = ""]
5415+
#[doc = "\\sa SDL_GetPrimarySelectionText"]
5416+
#[doc = "\\sa SDL_SetPrimarySelectionText"]
5417+
pub fn SDL_HasPrimarySelectionText() -> SDL_bool;
5418+
}
53745419
pub type __m64 = [libc::c_longlong; 1usize];
53755420
pub type __v1di = [libc::c_longlong; 1usize];
53765421
pub type __v2si = [libc::c_int; 2usize];

src/sdl2/clipboard.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,38 @@ impl ClipboardUtil {
6262
pub fn has_clipboard_text(&self) -> bool {
6363
unsafe { sys::SDL_HasClipboardText() == sys::SDL_bool::SDL_TRUE }
6464
}
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+
}
6599
}

src/sdl2/video.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,9 @@ impl Window {
15641564
binarization_cutoff: u8,
15651565
) -> Result<(), i32> {
15661566
let mode = sys::WindowShapeMode::ShapeModeBinarizeAlpha;
1567-
let parameters = sys::SDL_WindowShapeParams { binarizationCutoff: binarization_cutoff };
1567+
let parameters = sys::SDL_WindowShapeParams {
1568+
binarizationCutoff: binarization_cutoff,
1569+
};
15681570
let mut shape_mode = sys::SDL_WindowShapeMode { mode, parameters };
15691571
let result = unsafe {
15701572
sys::SDL_SetWindowShape(self.context.raw, shape.as_ref().raw(), &mut shape_mode)

0 commit comments

Comments
 (0)