use crate::arena::SmallString;
mod imp {
use std::string::String;
use std::sync::{Mutex, OnceLock};
static FALLBACK_CLIPBOARD: OnceLock<Mutex<String>> = OnceLock::new();
#[inline]
fn fallback() -> &'static Mutex<String> {
FALLBACK_CLIPBOARD.get_or_init(|| Mutex::new(String::new()))
}
#[cfg(target_os = "windows")]
mod win32 {
use core::ffi::c_void;
use std::string::String;
use std::vec::Vec;
const CF_UNICODETEXT: u32 = 13;
const GMEM_MOVEABLE: u32 = 0x0002;
#[link(name = "user32")]
unsafe extern "system" {
fn OpenClipboard(hwnd_new_owner: *mut c_void) -> i32;
fn CloseClipboard() -> i32;
fn EmptyClipboard() -> i32;
fn GetClipboardData(u_format: u32) -> *mut c_void;
fn SetClipboardData(u_format: u32, h_mem: *mut c_void) -> *mut c_void;
fn IsClipboardFormatAvailable(format: u32) -> i32;
}
#[link(name = "kernel32")]
unsafe extern "system" {
fn GlobalAlloc(u_flags: u32, dw_bytes: usize) -> *mut c_void;
fn GlobalLock(h_mem: *mut c_void) -> *mut c_void;
fn GlobalUnlock(h_mem: *mut c_void) -> i32;
fn GlobalFree(h_mem: *mut c_void) -> *mut c_void;
}
pub fn write_text(text: &str) -> bool {
let mut utf16: Vec<u16> = text.encode_utf16().collect();
utf16.push(0);
let byte_len = utf16.len() * core::mem::size_of::<u16>();
unsafe {
if OpenClipboard(core::ptr::null_mut()) == 0 {
return false;
}
if EmptyClipboard() == 0 {
let _ = CloseClipboard();
return false;
}
let hmem = GlobalAlloc(GMEM_MOVEABLE, byte_len);
if hmem.is_null() {
let _ = CloseClipboard();
return false;
}
let ptr = GlobalLock(hmem) as *mut u16;
if ptr.is_null() {
let _ = GlobalFree(hmem);
let _ = CloseClipboard();
return false;
}
core::ptr::copy_nonoverlapping(utf16.as_ptr(), ptr, utf16.len());
let _ = GlobalUnlock(hmem);
let set_res = SetClipboardData(CF_UNICODETEXT, hmem);
let _ = CloseClipboard();
if set_res.is_null() {
let _ = GlobalFree(hmem);
return false;
}
true
}
}
pub fn read_text() -> Option<String> {
unsafe {
if IsClipboardFormatAvailable(CF_UNICODETEXT) == 0 {
return None;
}
if OpenClipboard(core::ptr::null_mut()) == 0 {
return None;
}
let handle = GetClipboardData(CF_UNICODETEXT);
if handle.is_null() {
let _ = CloseClipboard();
return None;
}
let ptr = GlobalLock(handle) as *const u16;
if ptr.is_null() {
let _ = CloseClipboard();
return None;
}
let mut len = 0usize;
while len < 4 * 1024 * 1024 && *ptr.add(len) != 0 {
len += 1;
}
let slice = core::slice::from_raw_parts(ptr, len);
let text = String::from_utf16_lossy(slice);
let _ = GlobalUnlock(handle);
let _ = CloseClipboard();
Some(text)
}
}
pub fn has_text() -> bool {
unsafe { IsClipboardFormatAvailable(CF_UNICODETEXT) != 0 }
}
}
#[cfg(target_os = "windows")]
static WIN32_FALLBACK_AHEAD: core::sync::atomic::AtomicBool =
core::sync::atomic::AtomicBool::new(false);
pub fn write_text(text: &str) -> bool {
if let Ok(mut guard) = fallback().lock() {
guard.clear();
guard.push_str(text);
}
#[cfg(target_os = "windows")]
{
let mut synced = win32::write_text(text);
if !synced {
for _ in 0..3 {
std::thread::sleep(std::time::Duration::from_millis(2));
if win32::write_text(text) {
synced = true;
break;
}
}
}
WIN32_FALLBACK_AHEAD.store(!synced, core::sync::atomic::Ordering::Release);
}
true
}
pub fn read_text() -> Option<String> {
#[cfg(target_os = "windows")]
if !WIN32_FALLBACK_AHEAD.load(core::sync::atomic::Ordering::Acquire) {
if let Some(os_text) = win32::read_text() {
if !os_text.is_empty() {
return Some(os_text);
}
}
}
if let Ok(guard) = fallback().lock() {
if !guard.is_empty() {
return Some(guard.clone());
}
}
None
}
pub fn has_text() -> bool {
#[cfg(target_os = "windows")]
if win32::has_text() {
return true;
}
fallback().lock().map(|g| !g.is_empty()).unwrap_or(false)
}
}
#[inline]
pub fn clipboard_write_text(text: &str) -> bool {
imp::write_text(text)
}
#[inline]
pub fn clipboard_read_into<const N: usize>() -> SmallString<N> {
{
match imp::read_text() {
Some(s) => SmallString::from_str_trunc(&s),
None => SmallString::new(),
}
}
}
#[inline]
pub fn clipboard_read_text() -> Option<std::string::String> {
imp::read_text()
}
#[inline]
pub fn clipboard_has_text() -> bool {
imp::has_text()
}