From 532cf2878280ecfc1d2d83f581eccfd5a8abc357 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 6 Feb 2018 20:40:46 -0800 Subject: [PATCH] add support for setting the primary selection --- src/xgfx.rs | 5 +++ src/xwin.rs | 112 +++++++++++++++++++++++++++++++++++++++++++++++- term/src/lib.rs | 3 ++ 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/src/xgfx.rs b/src/xgfx.rs index 8098a97d1..2897b24d4 100644 --- a/src/xgfx.rs +++ b/src/xgfx.rs @@ -23,6 +23,7 @@ pub struct Connection { pub atom_delete: xcb::Atom, pub atom_utf8_string: xcb::Atom, pub atom_xsel_data: xcb::Atom, + pub atom_targets: xcb::Atom, keysyms: *mut xcb_key_symbols_t, shm_available: bool, } @@ -50,6 +51,9 @@ impl Connection { let atom_xsel_data = xcb::intern_atom(&conn, false, "XSEL_DATA") .get_reply()? .atom(); + let atom_targets = xcb::intern_atom(&conn, false, "TARGETS") + .get_reply()? + .atom(); let keysyms = unsafe { xcb_key_symbols_alloc(conn.get_raw_conn()) }; @@ -65,6 +69,7 @@ impl Connection { shm_available, atom_utf8_string, atom_xsel_data, + atom_targets, }) } diff --git a/src/xwin.rs b/src/xwin.rs index c75ad19bc..2da9e0d09 100644 --- a/src/xwin.rs +++ b/src/xwin.rs @@ -65,6 +65,7 @@ struct Host<'a> { window: xgfx::Window<'a>, pty: MasterPty, timestamp: xcb::xproto::Timestamp, + clipboard: Option, } pub struct TerminalWindow<'a> { @@ -145,13 +146,39 @@ impl<'a> term::TerminalHost for Host<'a> { } _ => { eprintln!( - "got XCB event type {} while waiting for selection", + "whoops: got XCB event type {} while waiting for selection", event.response_type() & 0x7f ); + // Rather than block forever, give up and yield an empty string + // for pasting purposes. We lost an event. This sucks. + // Will likely need to rethink how we handle passing the clipboard + // data down to the terminal. + return Ok("".into()); } } } } + + fn set_clipboard(&mut self, clip: Option) -> Result<(), Error> { + self.clipboard = clip; + let conn = self.window.get_conn(); + + xcb::set_selection_owner( + conn.conn(), + if self.clipboard.is_some() { + self.window.as_drawable() + } else { + xcb::NONE + }, + xcb::ATOM_PRIMARY, + self.timestamp, + ); + + // TODO: icccm says that we should check that we got ownership and + // amend our UI accordingly + + Ok(()) + } } impl<'a> TerminalWindow<'a> { @@ -184,6 +211,7 @@ impl<'a> TerminalWindow<'a> { window, pty, timestamp: 0, + clipboard: None, }, window_context, buffer_image, @@ -769,6 +797,88 @@ impl<'a> TerminalWindow<'a> { bail!("window close requested!"); } } + xcb::SELECTION_REQUEST => { + // Someone is asking for our selected text + + let request: &xcb::SelectionRequestEvent = unsafe { xcb::cast_event(&event) }; + debug!( + "SEL: time={} owner={} requestor={} selection={} target={} property={}", + request.time(), + request.owner(), + request.requestor(), + request.selection(), + request.target(), + request.property() + ); + debug!( + "XSEL={}, UTF8={} PRIMARY={}", + self.conn.atom_xsel_data, + self.conn.atom_utf8_string, + xcb::ATOM_PRIMARY, + ); + + + // I'd like to use `match` here, but the atom values are not + // known at compile time so we have to `if` like a caveman :-p + let selprop = if request.target() == self.conn.atom_targets { + // They want to know which targets we support + let atoms: [u32; 1] = [self.conn.atom_utf8_string]; + xcb::xproto::change_property( + self.conn.conn(), + xcb::xproto::PROP_MODE_REPLACE as u8, + request.requestor(), + request.property(), + xcb::xproto::ATOM_ATOM, + 32, /* 32-bit atom value */ + &atoms, + ); + + // let the requestor know that we set their property + request.property() + + } else if request.target() == self.conn.atom_utf8_string || + request.target() == xcb::xproto::ATOM_STRING + { + // We'll accept requests for UTF-8 or STRING data. + // We don't and won't do any conversion from UTF-8 to + // whatever STRING represents; let's just assume that + // the other end is going to handle it correctly. + if let &Some(ref text) = &self.host.clipboard { + xcb::xproto::change_property( + self.conn.conn(), + xcb::xproto::PROP_MODE_REPLACE as u8, + request.requestor(), + request.property(), + request.target(), + 8, /* 8-bit string data */ + text.as_bytes(), + ); + // let the requestor know that we set their property + request.property() + } else { + // We have no clipboard so there is nothing to report + xcb::NONE + } + } else { + // We didn't support their request, so there is nothing + // we can report back to them. + xcb::NONE + }; + + xcb::xproto::send_event( + self.conn.conn(), + true, + request.requestor(), + 0, + &xcb::xproto::SelectionNotifyEvent::new( + request.time(), + request.requestor(), + request.selection(), + request.target(), + selprop, // the disposition from the operation above + ), + ); + } _ => {} } Ok(()) diff --git a/term/src/lib.rs b/term/src/lib.rs index f2c15ebb2..93441c0c2 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -1339,6 +1339,9 @@ pub trait TerminalHost { /// Returns the current clipboard contents fn get_clipboard(&mut self) -> Result; + + /// Adjust the contents of the clipboard + fn set_clipboard(&mut self, clip: Option) -> Result<(), Error>; } impl vte::Perform for TerminalState {