diff --git a/src/xgfx.rs b/src/xgfx.rs index 0918c67b2..8098a97d1 100644 --- a/src/xgfx.rs +++ b/src/xgfx.rs @@ -19,8 +19,10 @@ use super::term::color::RgbColor; pub struct Connection { conn: xcb::Connection, screen_num: i32, - atom_protocols: xcb::Atom, - atom_delete: xcb::Atom, + pub atom_protocols: xcb::Atom, + pub atom_delete: xcb::Atom, + pub atom_utf8_string: xcb::Atom, + pub atom_xsel_data: xcb::Atom, keysyms: *mut xcb_key_symbols_t, shm_available: bool, } @@ -42,6 +44,12 @@ impl Connection { let atom_delete = xcb::intern_atom(&conn, false, "WM_DELETE_WINDOW") .get_reply()? .atom(); + let atom_utf8_string = xcb::intern_atom(&conn, false, "UTF8_STRING") + .get_reply()? + .atom(); + let atom_xsel_data = xcb::intern_atom(&conn, false, "XSEL_DATA") + .get_reply()? + .atom(); let keysyms = unsafe { xcb_key_symbols_alloc(conn.get_raw_conn()) }; @@ -55,6 +63,8 @@ impl Connection { atom_delete, keysyms, shm_available, + atom_utf8_string, + atom_xsel_data, }) } diff --git a/src/xkeysyms.rs b/src/xkeysyms.rs index f0fb26faf..9226eb261 100644 --- a/src/xkeysyms.rs +++ b/src/xkeysyms.rs @@ -25,6 +25,7 @@ pub const XK_Next: xcb_keysym_t = 0xff56; pub const XK_Page_Down: xcb_keysym_t = 0xff56; pub const XK_End: xcb_keysym_t = 0xff57; pub const XK_Begin: xcb_keysym_t = 0xff58; +pub const XK_Insert: xcb_keysym_t = 0xff63; pub const XK_KP_Space: xcb_keysym_t = 0xff80; pub const XK_KP_Tab: xcb_keysym_t = 0xff89; @@ -218,6 +219,7 @@ pub fn xcb_keysym_to_keycode(k: xcb_keysym_t) -> KeyCode { XK_Page_Up => KeyCode::PageUp, XK_Page_Down => KeyCode::PageDown, XK_End => KeyCode::End, + XK_KP_Insert | XK_Insert => KeyCode::Insert, _ => KeyCode::Unknown, } } diff --git a/src/xwin.rs b/src/xwin.rs index 9fb8821d4..8bb7e5ff9 100644 --- a/src/xwin.rs +++ b/src/xwin.rs @@ -1,4 +1,4 @@ -use failure::Error; +use failure::{self, Error}; use font::{Font, GlyphInfo, ftwrap}; use pty::MasterPty; use std::cell::RefCell; @@ -10,6 +10,7 @@ use std::rc::Rc; use std::slice; use term::{self, KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; use xcb; +use xcb_util; use xgfx::{self, BitmapImage, Connection, Drawable}; use xkeysyms; @@ -75,6 +76,7 @@ pub struct TerminalWindow<'a> { process: Child, glyph_cache: RefCell>>, palette: term::color::ColorPalette, + timestamp: xcb::xproto::Timestamp, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -99,12 +101,58 @@ struct CachedGlyph { struct Host<'a> { pty: &'a mut Write, + window: &'a xgfx::Window<'a>, + timestamp: xcb::xproto::Timestamp, } impl<'a> term::TerminalHost for Host<'a> { fn writer(&mut self) -> &mut Write { self.pty } + + // Check out https://tronche.com/gui/x/icccm/sec-2.html for some deep and complex + // background on what's happening in here. + fn get_clipboard(&mut self) -> Result { + let conn = self.window.get_conn(); + + xcb::convert_selection( + conn.conn(), + self.window.as_drawable(), + xcb::ATOM_PRIMARY, + conn.atom_utf8_string, + conn.atom_xsel_data, + self.timestamp, + ); + conn.flush(); + + loop { + let event = conn.wait_for_event().ok_or_else( + || failure::err_msg("X connection EOF"), + )?; + match event.response_type() & 0x7f { + xcb::SELECTION_NOTIFY => { + let selection: &xcb::SelectionNotifyEvent = unsafe { xcb::cast_event(&event) }; + + if selection.selection() == xcb::ATOM_PRIMARY && + selection.property() != xcb::NONE + { + let prop = xcb_util::icccm::get_text_property( + conn, + selection.requestor(), + selection.property(), + ).get_reply()?; + return Ok(prop.name().into()); + } + } + _ => { + eprintln!( + "got XCB event type {} while waiting for selection", + event.response_type() & 0x7f + ); + } + } + } + } } impl<'a> TerminalWindow<'a> { @@ -148,6 +196,7 @@ impl<'a> TerminalWindow<'a> { process, glyph_cache: RefCell::new(HashMap::new()), palette: term::color::ColorPalette::default(), + timestamp: 0, }) } @@ -660,20 +709,30 @@ impl<'a> TerminalWindow<'a> { } xcb::KEY_PRESS => { let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) }; + self.timestamp = key_press.time(); let (code, mods) = self.decode_key(key_press); self.terminal.key_down( code, mods, - &mut Host { pty: &mut self.pty }, + &mut Host { + pty: &mut self.pty, + window: &self.window, + timestamp: self.timestamp, + }, )?; } xcb::KEY_RELEASE => { let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) }; + self.timestamp = key_press.time(); let (code, mods) = self.decode_key(key_press); self.terminal.key_up( code, mods, - &mut Host { pty: &mut self.pty }, + &mut Host { + pty: &mut self.pty, + window: &self.window, + timestamp: self.timestamp, + }, )?; } xcb::MOTION_NOTIFY => { @@ -688,12 +747,17 @@ impl<'a> TerminalWindow<'a> { }; self.terminal.mouse_event( event, - &mut Host { pty: &mut self.pty }, + &mut Host { + pty: &mut self.pty, + window: &self.window, + timestamp: self.timestamp, + }, )?; } xcb::BUTTON_PRESS | xcb::BUTTON_RELEASE => { let button_press: &xcb::ButtonPressEvent = unsafe { xcb::cast_event(&event) }; + self.timestamp = button_press.time(); let event = MouseEvent { kind: match r { @@ -719,7 +783,11 @@ impl<'a> TerminalWindow<'a> { self.terminal.mouse_event( event, - &mut Host { pty: &mut self.pty }, + &mut Host { + pty: &mut self.pty, + window: &self.window, + timestamp: self.timestamp, + }, )?; } xcb::CLIENT_MESSAGE => { diff --git a/term/src/lib.rs b/term/src/lib.rs index 44d9952bb..f2c15ebb2 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -109,6 +109,7 @@ pub enum KeyCode { PageDown, Home, End, + Insert, } #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -862,9 +863,7 @@ impl TerminalState { let shift = mods & SHIFT; let alt = mods & ALT; - // https://doc.rust-lang.org/std/primitive.char.html#method.encode_utf8 - // says "A buffer of length four is large enough to encode any char." - let mut buf = [0u8; 4]; + let mut buf = String::new(); // TODO: also respect self.application_keypad @@ -872,23 +871,41 @@ impl TerminalState { (Char(c), CTRL, _, SHIFT, _) if c <= 0xff as char => { // If shift is held we have C == 0x43 and want to translate // that into 0x03 - ((c as u8 - 0x40) as char).encode_utf8(&mut buf) as &str + buf.push((c as u8 - 0x40) as char); + buf.as_str() } (Char(c), CTRL, ..) if c <= 0xff as char => { // If shift is not held we have C == 0x63 and want to translate // that into 0x03 - ((c as u8 - 0x60) as char).encode_utf8(&mut buf) as &str + buf.push((c as u8 - 0x60) as char); + buf.as_str() } (Char(c), _, ALT, ..) if c <= 0xff as char => { - ((c as u8 | 0x80) as char).encode_utf8(&mut buf) as &str + buf.push((c as u8 | 0x80) as char); + buf.as_str() } - (Char(c), ..) => c.encode_utf8(&mut buf), + (Char(c), ..) => { + buf.push(c); + buf.as_str() + } + (Insert, _, _, SHIFT, _) => { + let clip = host.get_clipboard()?; + if self.bracketed_paste { + use std::fmt::Write; + write!(buf, "\x1b[200~{}\x1b[201~", clip)?; + } else { + buf = clip; + } + buf.as_str() + } + (Up, _, _, _, APPCURSOR) => "\x1bOA", (Down, _, _, _, APPCURSOR) => "\x1bOB", (Right, _, _, _, APPCURSOR) => "\x1bOC", (Left, _, _, _, APPCURSOR) => "\x1bOD", (Home, _, _, _, APPCURSOR) => "\x1bOH", (End, _, _, _, APPCURSOR) => "\x1bOF", + (Up, ..) => "\x1b[A", (Down, ..) => "\x1b[B", (Right, ..) => "\x1b[C", @@ -907,6 +924,7 @@ impl TerminalState { (PageDown, ..) => "\x1b[6~", (Home, ..) => "\x1b[H", (End, ..) => "\x1b[F", + (Insert, ..) => "\x1b[2~", // Modifier keys pressed on their own and unmappable keys don't expand to anything (Control, ..) | (Alt, ..) | (Meta, ..) | (Super, ..) | (Hyper, ..) | (Shift, ..) | @@ -1315,7 +1333,12 @@ impl Terminal { /// Provides a means for sending data to the connected pty, /// and for operating on the clipboard pub trait TerminalHost { + /// Returns an object that can be used to send data to the + /// slave end of the associated pty. fn writer(&mut self) -> &mut std::io::Write; + + /// Returns the current clipboard contents + fn get_clipboard(&mut self) -> Result; } impl vte::Perform for TerminalState {