add support for setting the primary selection

This commit is contained in:
Wez Furlong committed 2018-02-06 20:40:46 -08:00
1 parent 7657a91188
commit 532cf28782
3 files changed
+119 -1

No files matched your search

+5
View File
@@ -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,
})
}
+111 -1
View File
@@ -65,6 +65,7 @@ struct Host<'a> {
window: xgfx::Window<'a>,
pty: MasterPty,
timestamp: xcb::xproto::Timestamp,
clipboard: Option<String>,
}
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<String>) -> 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(())
+3
View File
@@ -1339,6 +1339,9 @@ pub trait TerminalHost {
/// Returns the current clipboard contents
fn get_clipboard(&mut self) -> Result<String, Error>;
/// Adjust the contents of the clipboard
fn set_clipboard(&mut self, clip: Option<String>) -> Result<(), Error>;
}
impl vte::Perform for TerminalState {