Merge pull request #11 from hecrj/replace-xcb-with-x11rb

Replace `xcb` with `x11rb` in `clipboard_x11`
This commit is contained in:
Héctor Ramón 2021-02-11 22:51:41 +01:00 committed by GitHub
commit 8ef9c2ae01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 145 deletions

View file

@ -10,4 +10,5 @@ documentation = "https://docs.rs/clipboard_x11"
keywords = ["clipboard", "x11"] keywords = ["clipboard", "x11"]
[dependencies] [dependencies]
xcb = { version = "0.9", features = ["thread"] } x11rb = "0.8"
thiserror = "1.0"

View file

@ -1,8 +1,12 @@
use crate::error::Error; use crate::error::Error;
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use xcb::base::ConnError; use x11rb::connection::Connection as _;
use xcb::{Atom, Connection, Window}; use x11rb::errors::ConnectError;
use x11rb::protocol::xproto::{self, Atom, AtomEnum, Window};
use x11rb::protocol::Event;
use x11rb::rust_connection::RustConnection as Connection;
const POLL_DURATION: std::time::Duration = Duration::from_micros(50); const POLL_DURATION: std::time::Duration = Duration::from_micros(50);
@ -24,65 +28,62 @@ pub struct Clipboard {
pub struct Context { pub struct Context {
pub connection: Connection, pub connection: Connection,
pub screen: i32, pub screen: usize,
pub window: Window, pub window: Window,
pub atoms: Atoms, pub atoms: Atoms,
} }
#[inline] #[inline]
fn get_atom(connection: &Connection, name: &str) -> Result<Atom, Error> { fn get_atom(connection: &Connection, name: &str) -> Result<Atom, Error> {
xcb::intern_atom(connection, false, name) x11rb::protocol::xproto::intern_atom(connection, false, name.as_bytes())
.get_reply() .map_err(Into::into)
.map(|reply| reply.atom()) .and_then(|cookie| cookie.reply())
.map(|reply| reply.atom)
.map_err(Into::into) .map_err(Into::into)
} }
impl Context { impl Context {
pub fn new(displayname: Option<&str>) -> Result<Self, Error> { pub fn new(displayname: Option<&str>) -> Result<Self, Error> {
let (connection, screen) = Connection::connect(displayname)?; let (connection, screen) = Connection::connect(displayname)?;
let window = connection.generate_id(); let window = connection.generate_id().map_err(|_| {
Error::ConnectionFailed(ConnectError::InvalidScreen)
})?;
{ {
let screen = connection let screen =
.get_setup() connection.setup().roots.get(screen as usize).ok_or(
.roots() Error::ConnectionFailed(ConnectError::InvalidScreen),
.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 { let _ = xproto::create_window(
( $name:expr ) => { &connection,
get_atom(&connection, $name)? x11rb::COPY_DEPTH_FROM_PARENT,
}; window,
screen.root,
0,
0,
1,
1,
0,
xproto::WindowClass::INPUT_OUTPUT,
screen.root_visual,
&xproto::CreateWindowAux::new().event_mask(
xproto::EventMask::STRUCTURE_NOTIFY
| xproto::EventMask::PROPERTY_CHANGE,
),
)?;
let _ = connection.flush()?;
} }
let atoms = Atoms { let atoms = Atoms {
primary: xcb::ATOM_PRIMARY, primary: AtomEnum::PRIMARY.into(),
clipboard: intern_atom!("CLIPBOARD"), clipboard: get_atom(&connection, "CLIPBOARD")?,
property: intern_atom!("THIS_CLIPBOARD_OUT"), property: get_atom(&connection, "THIS_CLIPBOARD_OUT")?,
targets: intern_atom!("TARGETS"), targets: get_atom(&connection, "TARGETS")?,
string: xcb::ATOM_STRING, string: AtomEnum::STRING.into(),
utf8_string: intern_atom!("UTF8_STRING"), utf8_string: get_atom(&connection, "UTF8_STRING")?,
incr: intern_atom!("INCR"), incr: get_atom(&connection, "INCR")?,
}; };
Ok(Context { Ok(Context {
@ -132,7 +133,7 @@ impl Clipboard {
return Err(Error::Timeout); return Err(Error::Timeout);
} }
let event = match self.getter.connection.poll_for_event() { let event = match self.getter.connection.poll_for_event()? {
Some(event) => event, Some(event) => event,
None => { None => {
thread::park_timeout(POLL_DURATION); thread::park_timeout(POLL_DURATION);
@ -140,97 +141,96 @@ impl Clipboard {
} }
}; };
let r = event.response_type(); match event {
Event::SelectionNotify(event) => {
match r & !0x80 { if event.selection != selection {
xcb::SELECTION_NOTIFY => {
let event = unsafe {
xcb::cast_event::<xcb::SelectionNotifyEvent>(&event)
};
if event.selection() != selection {
continue; continue;
}; };
// Note that setting the property argument to None indicates that the // Note that setting the property argument to None indicates that the
// conversion requested could not be made. // conversion requested could not be made.
if event.property() == xcb::ATOM_NONE { if event.property == AtomEnum::NONE.into() {
break; break;
} }
let reply = xcb::get_property( let reply = xproto::get_property(
&self.getter.connection, &self.getter.connection,
false, false,
self.getter.window, self.getter.window,
event.property(), event.property,
xcb::ATOM_ANY, Atom::from(AtomEnum::ANY),
buff.len() as u32, buff.len() as u32,
::std::u32::MAX, // FIXME reasonable buffer size ::std::u32::MAX, // FIXME reasonable buffer size
) )
.get_reply()?; .map_err(Into::into)
.and_then(|cookie| cookie.reply())?;
if reply.type_() == self.getter.atoms.incr { if reply.type_ == self.getter.atoms.incr {
if let Some(&size) = reply.value::<i32>().get(0) { if let Some(&size) = reply.value.get(0) {
buff.reserve(size as usize); buff.reserve(size as usize);
} }
xcb::delete_property(
let _ = xproto::delete_property(
&self.getter.connection, &self.getter.connection,
self.getter.window, self.getter.window,
property, property,
); );
self.getter.connection.flush();
let _ = self.getter.connection.flush();
is_incr = true; is_incr = true;
continue; continue;
} else if reply.type_() != target { } else if reply.type_ != target {
return Err(Error::UnexpectedType(reply.type_())); return Err(Error::UnexpectedType(reply.type_));
} }
buff.extend_from_slice(reply.value()); buff.extend_from_slice(&reply.value);
break; break;
} }
xcb::PROPERTY_NOTIFY if is_incr => { Event::PropertyNotify(event) if is_incr => {
let event = unsafe { if event.state != xproto::Property::NEW_VALUE {
xcb::cast_event::<xcb::PropertyNotifyEvent>(&event)
};
if event.state() != xcb::PROPERTY_NEW_VALUE as u8 {
continue; continue;
}; };
let length = xcb::get_property( let length = xproto::get_property(
&self.getter.connection, &self.getter.connection,
false, false,
self.getter.window, self.getter.window,
property, property,
xcb::ATOM_ANY, Atom::from(AtomEnum::ANY),
0, 0,
0, 0,
) )
.get_reply() .map_err(Into::into)
.map(|reply| reply.bytes_after())?; .and_then(|cookie| cookie.reply())?
.bytes_after;
let reply = xcb::get_property( let reply = xproto::get_property(
&self.getter.connection, &self.getter.connection,
true, true,
self.getter.window, self.getter.window,
property, property,
xcb::ATOM_ANY, Atom::from(AtomEnum::ANY),
0, 0,
length, length,
) )
.get_reply()?; .map_err(Into::into)
.and_then(|cookie| cookie.reply())?;
if reply.type_() != target { if reply.type_ != target {
continue; continue;
}; };
if reply.value_len() != 0 { if reply.value_len != 0 {
buff.extend_from_slice(reply.value()); buff.extend_from_slice(&reply.value);
} else { } else {
break; break;
} }
} }
_ => (), _ => {}
} }
} }
Ok(()) Ok(())
} }
@ -248,25 +248,27 @@ impl Clipboard {
let mut buff = Vec::new(); let mut buff = Vec::new();
let timeout = timeout.into(); let timeout = timeout.into();
xcb::convert_selection( let _ = xproto::convert_selection(
&self.getter.connection, &self.getter.connection,
self.getter.window, self.getter.window,
selection, selection,
target, target,
property, property,
xcb::CURRENT_TIME, // FIXME ^ x11rb::CURRENT_TIME, // FIXME ^
// Clients should not use CurrentTime for the time argument of a ConvertSelection request. // 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. // Instead, they should use the timestamp of the event that caused the request to be made.
); )?;
self.getter.connection.flush(); let _ = self.getter.connection.flush();
self.process_event(&mut buff, selection, target, property, timeout)?; self.process_event(&mut buff, selection, target, property, timeout)?;
xcb::delete_property(
let _ = xproto::delete_property(
&self.getter.connection, &self.getter.connection,
self.getter.window, self.getter.window,
property, property,
); )?;
self.getter.connection.flush(); let _ = self.getter.connection.flush()?;
Ok(buff) Ok(buff)
} }
} }

View file

@ -1,59 +1,17 @@
use std::error::Error as StdError; use x11rb::errors::{ConnectError, ConnectionError, ReplyError};
use std::fmt; use x11rb::protocol::xproto::Atom;
use std::sync::mpsc::SendError;
use xcb::base::{ConnError, GenericError};
use xcb::Atom;
#[must_use] #[must_use]
#[derive(Debug)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
Set(SendError<Atom>), #[error("connection failed: {0}")]
XcbConn(ConnError), ConnectionFailed(#[from] ConnectError),
XcbGeneric(GenericError), #[error("connection errored: {0}")]
ConnectionErrored(#[from] ConnectionError),
#[error("reply failed: {0}")]
ReplyError(#[from] ReplyError),
#[error("timeout")]
Timeout, Timeout,
Owner, #[error("unexpected type: {0}")]
UnexpectedType(Atom), UnexpectedType(Atom),
} }
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Set(e) => write!(f, "XCB - couldn't set atom: {:?}", e),
Error::XcbConn(e) => write!(f, "XCB connection error: {:?}", e),
Error::XcbGeneric(e) => write!(f, "XCB generic error: {:?}", e),
Error::Timeout => write!(f, "Selection timed out"),
Error::Owner => {
write!(f, "Failed to set new owner of XCB selection")
}
Error::UnexpectedType(target) => {
write!(f, "Unexpected Reply type: {}", target)
}
}
}
}
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),
Timeout | Owner | UnexpectedType(_) => None,
}
}
}
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);

View file

@ -1,8 +1,7 @@
#[forbid(unsafe_code)]
mod clipboard; mod clipboard;
mod error; mod error;
pub use xcb::*;
use std::error::Error; use std::error::Error;
pub struct Clipboard(clipboard::Clipboard); pub struct Clipboard(clipboard::Clipboard);