Remove copypasta and maintain our own fork
This commit is contained in:
parent
b55ba14585
commit
23004b960f
16 changed files with 685 additions and 40 deletions
272
x11/src/clipboard.rs
Normal file
272
x11/src/clipboard.rs
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
use crate::error::Error;
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use xcb::base::ConnError;
|
||||
use xcb::{Atom, Connection, Window};
|
||||
|
||||
const POLL_DURATION: std::time::Duration = Duration::from_micros(50);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Atoms {
|
||||
pub primary: Atom,
|
||||
pub clipboard: Atom,
|
||||
pub property: Atom,
|
||||
pub targets: Atom,
|
||||
pub string: Atom,
|
||||
pub utf8_string: Atom,
|
||||
pub incr: Atom,
|
||||
}
|
||||
|
||||
/// X11 Clipboard
|
||||
pub struct Clipboard {
|
||||
pub getter: Context,
|
||||
}
|
||||
|
||||
pub struct Context {
|
||||
pub connection: Connection,
|
||||
pub screen: i32,
|
||||
pub window: Window,
|
||||
pub atoms: Atoms,
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_atom(connection: &Connection, name: &str) -> Result<Atom, Error> {
|
||||
xcb::intern_atom(connection, false, name)
|
||||
.get_reply()
|
||||
.map(|reply| reply.atom())
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new(displayname: Option<&str>) -> Result<Self, Error> {
|
||||
let (connection, screen) = Connection::connect(displayname)?;
|
||||
let window = connection.generate_id();
|
||||
|
||||
{
|
||||
let screen = connection
|
||||
.get_setup()
|
||||
.roots()
|
||||
.nth(screen as usize)
|
||||
.ok_or(Error::XcbConn(ConnError::ClosedInvalidScreen))?;
|
||||
xcb::create_window(
|
||||
&connection,
|
||||
xcb::COPY_FROM_PARENT as u8,
|
||||
window,
|
||||
screen.root(),
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
|
||||
screen.root_visual(),
|
||||
&[(
|
||||
xcb::CW_EVENT_MASK,
|
||||
xcb::EVENT_MASK_STRUCTURE_NOTIFY
|
||||
| xcb::EVENT_MASK_PROPERTY_CHANGE,
|
||||
)],
|
||||
);
|
||||
connection.flush();
|
||||
}
|
||||
|
||||
macro_rules! intern_atom {
|
||||
( $name:expr ) => {
|
||||
get_atom(&connection, $name)?
|
||||
};
|
||||
}
|
||||
|
||||
let atoms = Atoms {
|
||||
primary: xcb::ATOM_PRIMARY,
|
||||
clipboard: intern_atom!("CLIPBOARD"),
|
||||
property: intern_atom!("THIS_CLIPBOARD_OUT"),
|
||||
targets: intern_atom!("TARGETS"),
|
||||
string: xcb::ATOM_STRING,
|
||||
utf8_string: intern_atom!("UTF8_STRING"),
|
||||
incr: intern_atom!("INCR"),
|
||||
};
|
||||
|
||||
Ok(Context {
|
||||
connection,
|
||||
screen,
|
||||
window,
|
||||
atoms,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Clipboard {
|
||||
/// Create Clipboard.
|
||||
pub fn new() -> Result<Self, Error> {
|
||||
let getter = Context::new(None)?;
|
||||
|
||||
Ok(Clipboard { getter })
|
||||
}
|
||||
|
||||
fn process_event<T>(
|
||||
&self,
|
||||
buff: &mut Vec<u8>,
|
||||
selection: Atom,
|
||||
target: Atom,
|
||||
property: Atom,
|
||||
timeout: T,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
T: Into<Option<Duration>>,
|
||||
{
|
||||
let mut is_incr = false;
|
||||
let timeout = timeout.into();
|
||||
let start_time = if timeout.is_some() {
|
||||
Some(Instant::now())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
loop {
|
||||
if timeout
|
||||
.into_iter()
|
||||
.zip(start_time)
|
||||
.next()
|
||||
.map(|(timeout, time)| (Instant::now() - time) >= timeout)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return Err(Error::Timeout);
|
||||
}
|
||||
|
||||
let event = match self.getter.connection.poll_for_event() {
|
||||
Some(event) => event,
|
||||
None => {
|
||||
thread::park_timeout(POLL_DURATION);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let r = event.response_type();
|
||||
|
||||
match r & !0x80 {
|
||||
xcb::SELECTION_NOTIFY => {
|
||||
let event = unsafe {
|
||||
xcb::cast_event::<xcb::SelectionNotifyEvent>(&event)
|
||||
};
|
||||
if event.selection() != selection {
|
||||
continue;
|
||||
};
|
||||
|
||||
// Note that setting the property argument to None indicates that the
|
||||
// conversion requested could not be made.
|
||||
if event.property() == xcb::ATOM_NONE {
|
||||
break;
|
||||
}
|
||||
|
||||
let reply = xcb::get_property(
|
||||
&self.getter.connection,
|
||||
false,
|
||||
self.getter.window,
|
||||
event.property(),
|
||||
xcb::ATOM_ANY,
|
||||
buff.len() as u32,
|
||||
::std::u32::MAX, // FIXME reasonable buffer size
|
||||
)
|
||||
.get_reply()?;
|
||||
|
||||
if reply.type_() == self.getter.atoms.incr {
|
||||
if let Some(&size) = reply.value::<i32>().get(0) {
|
||||
buff.reserve(size as usize);
|
||||
}
|
||||
xcb::delete_property(
|
||||
&self.getter.connection,
|
||||
self.getter.window,
|
||||
property,
|
||||
);
|
||||
self.getter.connection.flush();
|
||||
is_incr = true;
|
||||
continue;
|
||||
} else if reply.type_() != target {
|
||||
return Err(Error::UnexpectedType(reply.type_()));
|
||||
}
|
||||
|
||||
buff.extend_from_slice(reply.value());
|
||||
break;
|
||||
}
|
||||
xcb::PROPERTY_NOTIFY if is_incr => {
|
||||
let event = unsafe {
|
||||
xcb::cast_event::<xcb::PropertyNotifyEvent>(&event)
|
||||
};
|
||||
if event.state() != xcb::PROPERTY_NEW_VALUE as u8 {
|
||||
continue;
|
||||
};
|
||||
|
||||
let length = xcb::get_property(
|
||||
&self.getter.connection,
|
||||
false,
|
||||
self.getter.window,
|
||||
property,
|
||||
xcb::ATOM_ANY,
|
||||
0,
|
||||
0,
|
||||
)
|
||||
.get_reply()
|
||||
.map(|reply| reply.bytes_after())?;
|
||||
|
||||
let reply = xcb::get_property(
|
||||
&self.getter.connection,
|
||||
true,
|
||||
self.getter.window,
|
||||
property,
|
||||
xcb::ATOM_ANY,
|
||||
0,
|
||||
length,
|
||||
)
|
||||
.get_reply()?;
|
||||
|
||||
if reply.type_() != target {
|
||||
continue;
|
||||
};
|
||||
|
||||
if reply.value_len() != 0 {
|
||||
buff.extend_from_slice(reply.value());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// load value.
|
||||
pub fn load<T>(
|
||||
&self,
|
||||
selection: Atom,
|
||||
target: Atom,
|
||||
property: Atom,
|
||||
timeout: T,
|
||||
) -> Result<Vec<u8>, Error>
|
||||
where
|
||||
T: Into<Option<Duration>>,
|
||||
{
|
||||
let mut buff = Vec::new();
|
||||
let timeout = timeout.into();
|
||||
|
||||
xcb::convert_selection(
|
||||
&self.getter.connection,
|
||||
self.getter.window,
|
||||
selection,
|
||||
target,
|
||||
property,
|
||||
xcb::CURRENT_TIME, // FIXME ^
|
||||
// Clients should not use CurrentTime for the time argument of a ConvertSelection request.
|
||||
// Instead, they should use the timestamp of the event that caused the request to be made.
|
||||
);
|
||||
self.getter.connection.flush();
|
||||
|
||||
self.process_event(&mut buff, selection, target, property, timeout)?;
|
||||
xcb::delete_property(
|
||||
&self.getter.connection,
|
||||
self.getter.window,
|
||||
property,
|
||||
);
|
||||
self.getter.connection.flush();
|
||||
Ok(buff)
|
||||
}
|
||||
}
|
||||
63
x11/src/error.rs
Normal file
63
x11/src/error.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use xcb::Atom;
|
||||
use xcb::base::{ ConnError, GenericError };
|
||||
use std::fmt;
|
||||
use std::sync::mpsc::SendError;
|
||||
use std::error::Error as StdError;
|
||||
|
||||
#[must_use]
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Set(SendError<Atom>),
|
||||
XcbConn(ConnError),
|
||||
XcbGeneric(GenericError),
|
||||
Lock,
|
||||
Timeout,
|
||||
Owner,
|
||||
UnexpectedType(Atom),
|
||||
|
||||
#[doc(hidden)]
|
||||
__Unknown
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::Error::*;
|
||||
match self {
|
||||
Set(e) => write!(f, "XCB - couldn't set atom: {:?}", e),
|
||||
XcbConn(e) => write!(f, "XCB connection error: {:?}", e),
|
||||
XcbGeneric(e) => write!(f, "XCB generic error: {:?}", e),
|
||||
Lock => write!(f, "XCB: Lock is poisoned"),
|
||||
Timeout => write!(f, "Selection timed out"),
|
||||
Owner => write!(f, "Failed to set new owner of XCB selection"),
|
||||
UnexpectedType(target) => write!(f, "Unexpected Reply type: {}", target),
|
||||
__Unknown => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
||||
use self::Error::*;
|
||||
match self {
|
||||
Set(e) => Some(e),
|
||||
XcbConn(e) => Some(e),
|
||||
XcbGeneric(e) => Some(e),
|
||||
Lock | Timeout | Owner | UnexpectedType(_) => None,
|
||||
__Unknown => unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! define_from {
|
||||
( $item:ident from $err:ty ) => {
|
||||
impl From<$err> for Error {
|
||||
fn from(err: $err) -> Error {
|
||||
Error::$item(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
define_from!(Set from SendError<Atom>);
|
||||
define_from!(XcbConn from ConnError);
|
||||
define_from!(XcbGeneric from GenericError);
|
||||
23
x11/src/lib.rs
Normal file
23
x11/src/lib.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
mod clipboard;
|
||||
mod error;
|
||||
|
||||
pub use xcb::*;
|
||||
|
||||
use std::error::Error;
|
||||
|
||||
pub struct Clipboard(clipboard::Clipboard);
|
||||
|
||||
impl Clipboard {
|
||||
pub fn new() -> Result<Clipboard, Box<dyn Error>> {
|
||||
Ok(Clipboard(clipboard::Clipboard::new()?))
|
||||
}
|
||||
|
||||
pub fn read(&self) -> Result<String, Box<dyn Error>> {
|
||||
Ok(String::from_utf8(self.0.load(
|
||||
self.0.getter.atoms.clipboard,
|
||||
self.0.getter.atoms.utf8_string,
|
||||
self.0.getter.atoms.property,
|
||||
std::time::Duration::from_secs(3),
|
||||
)?)?)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue