From bd4e745380427ba7868ce468abb85a0e620eb7bc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 22 Feb 2018 00:14:14 -0800 Subject: [PATCH] go all-in on glutin and remove my own xcb based code --- Cargo.toml | 1 - src/clipboard/mod.rs | 2 + .../clipboard.rs => clipboard/x11.rs} | 0 src/gliumwindows.rs | 6 +- src/main.rs | 117 +----- src/xwindows/mod.rs | 347 ------------------ src/xwindows/xkeysyms.rs | 251 ------------- src/xwindows/xwin.rs | 267 -------------- 8 files changed, 8 insertions(+), 983 deletions(-) create mode 100644 src/clipboard/mod.rs rename src/{xwindows/clipboard.rs => clipboard/x11.rs} (100%) delete mode 100644 src/xwindows/mod.rs delete mode 100644 src/xwindows/xkeysyms.rs delete mode 100644 src/xwindows/xwin.rs diff --git a/Cargo.toml b/Cargo.toml index 5746e2703..58b81a8ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,6 @@ version = "0.1.0" [dependencies] bitflags = "1.0.1" -egli = "0.4.0" euclid = "0.17.0" failure = "0.1.1" gl = "0.10.0" diff --git a/src/clipboard/mod.rs b/src/clipboard/mod.rs new file mode 100644 index 000000000..ea0534168 --- /dev/null +++ b/src/clipboard/mod.rs @@ -0,0 +1,2 @@ +#[cfg(all(unix, not(target_os = "macos")))] +pub mod x11; diff --git a/src/xwindows/clipboard.rs b/src/clipboard/x11.rs similarity index 100% rename from src/xwindows/clipboard.rs rename to src/clipboard/x11.rs diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index 636823789..392d06d14 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -1,6 +1,7 @@ //! Generic system dependent windows via glium+glutin #![allow(dead_code)] +use clipboard::x11::Clipboard; use failure::Error; use font::FontConfiguration; use glium::{self, glutin}; @@ -17,7 +18,6 @@ use term::{MouseButton, MouseEventKind}; use term::KeyCode; use term::KeyModifiers; use term::hyperlink::Hyperlink; -use xwindows::clipboard::Clipboard; struct Host { display: glium::Display, @@ -418,12 +418,10 @@ impl TerminalWindow { event: WindowEvent::Refresh, .. } => { - println!("Refresh"); self.paint()?; } Event::Awakened => { - // TODO: plumb signals from clipboard and sigchld waiter - use xwindows::clipboard::Paste; + use clipboard::x11::Paste; match self.host.clipboard.receiver().try_recv() { Ok(Paste::Cleared) => { self.terminal.clear_selection(); diff --git a/src/main.rs b/src/main.rs index 08ade719a..c0e1f217f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ #[cfg(target_os = "macos")] extern crate core_text; -extern crate egli; extern crate euclid; #[macro_use] extern crate failure; @@ -34,10 +33,8 @@ extern crate xcb; extern crate xcb_util; use mio::{Events, Poll, PollOpt, Ready, Token}; -use mio::unix::EventedFd; use std::env; use std::ffi::CStr; -use std::os::unix::io::AsRawFd; use std::process::Command; use std::str; use std::time::Duration; @@ -46,10 +43,7 @@ mod config; mod opengl; -#[cfg(all(unix, not(target_os = "macos")))] -mod xwindows; -#[cfg(all(unix, not(target_os = "macos")))] -use xwindows::xwin::TerminalWindow; +mod clipboard; mod gliumwindows; mod font; @@ -77,9 +71,6 @@ fn get_shell() -> Result { }) } -/// Drives the gui on macos. It is included in the linux build to -/// make it less likely to get broken by accident. -#[allow(dead_code)] fn run_glium( master: pty::MasterPty, child: std::process::Child, @@ -89,7 +80,6 @@ fn run_glium( initial_pixel_width: u16, initial_pixel_height: u16, ) -> Result<(), Error> { - use glium::glutin; let poll = Poll::new()?; poll.register(&master, Token(0), Ready::readable(), PollOpt::edge())?; @@ -146,108 +136,9 @@ fn run_glium( Ok(()) } -#[cfg(all(unix, not(target_os = "macos")))] -fn run_xwindows( - master: pty::MasterPty, - child: std::process::Child, - config: config::Config, - fontconfig: FontConfiguration, - terminal: term::Terminal, - initial_pixel_width: u16, - initial_pixel_height: u16, -) -> Result<(), Error> { - let conn = xwindows::Connection::new()?; - let poll = Poll::new()?; - let waiter = sigchld::ChildWaiter::new()?; - - // Ask mio to watch the pty for input from the child process - poll.register(&master, Token(0), Ready::readable(), PollOpt::edge())?; - // Ask mio to monitor the X connection fd - poll.register( - &EventedFd(&conn.as_raw_fd()), - Token(1), - Ready::readable(), - PollOpt::edge(), - )?; - - poll.register(&waiter, Token(2), Ready::readable(), PollOpt::edge())?; - - // let message = "; ❤ 😍🤢\n\x1b[91;mw00t\n\x1b[37;104;m bleet\x1b[0;m."; - // terminal.advance_bytes(message); - // != - - let mut window = TerminalWindow::new( - &conn, - initial_pixel_width, - initial_pixel_height, - terminal, - master, - child, - fontconfig, - config - .colors - .map(|p| p.into()) - .unwrap_or_else(term::color::ColorPalette::default), - )?; - - window.show(); - - let mut events = Events::with_capacity(8); - conn.flush(); - - loop { - if poll.poll(&mut events, Some(Duration::new(0, 0)))? == 0 { - // No immediately ready events. Before we go to sleep, - // make sure we've flushed out any pending X work. - if window.need_paint() { - window.paint()?; - } - conn.flush(); - - poll.poll(&mut events, None)?; - } - - for event in &events { - if event.token() == Token(0) && event.readiness().is_readable() { - window.handle_pty_readable_event(); - } - if event.token() == Token(1) && event.readiness().is_readable() { - // Each time the XCB Connection FD shows as readable, we perform - // a single poll against the connection and then eagerly consume - // all of the queued events that came along as part of that batch. - // This is important because we can't assume that one readiness - // event from the kerenl maps to a single XCB event. We need to be - // sure that all buffered/queued events are consumed before we - // allow the mio poll() routine to put us to sleep, otherwise we - // will effectively hang without updating all the state. - match conn.poll_for_event() { - Some(event) => { - window.dispatch_event(event)?; - // Since we read one event from the connection, we must - // now eagerly consume the rest of the queued events. - loop { - match conn.poll_for_queued_event() { - Some(event) => window.dispatch_event(event)?, - None => break, - } - } - } - None => {} - } - - // If we got disconnected from the display server, we cannot continue - conn.has_error()?; - } - - if event.token() == Token(2) { - println!("sigchld ready"); - let pid = waiter.read_one()?; - println!("got sigchld from pid {}", pid); - window.test_for_child_exit()?; - } - } - } -} +// let message = "; ❤ 😍🤢\n\x1b[91;mw00t\n\x1b[37;104;m bleet\x1b[0;m."; +// terminal.advance_bytes(message); +// != fn run() -> Result<(), Error> { let config = config::Config::load()?; diff --git a/src/xwindows/mod.rs b/src/xwindows/mod.rs deleted file mode 100644 index 6324ec1cc..000000000 --- a/src/xwindows/mod.rs +++ /dev/null @@ -1,347 +0,0 @@ -use egli; -use gl; -use glium; -use glium::backend::Backend; -use std::mem; -use std::ops::Deref; -use std::os; -use std::ptr; -use std::rc::Rc; -use std::result; -use x11; -use xcb; -use xcb_util; -use xcb_util::ffi::keysyms::{xcb_key_press_lookup_keysym, xcb_key_symbols_alloc, - xcb_key_symbols_free, xcb_key_symbols_t}; - -use failure::{self, Error}; -pub type Result = result::Result; - -mod xkeysyms; -pub use self::xkeysyms::*; -pub mod xwin; -pub mod clipboard; - -pub struct Connection { - pub display: *mut x11::xlib::Display, - conn: xcb::Connection, - screen_num: i32, - pub atom_protocols: xcb::Atom, - pub atom_delete: xcb::Atom, - pub atom_utf8_string: xcb::Atom, - pub atom_xsel_data: xcb::Atom, - pub atom_targets: xcb::Atom, - pub atom_clipboard: xcb::Atom, - keysyms: *mut xcb_key_symbols_t, - egl_display: Rc, - egl_config: egli::FrameBufferConfigRef, -} - -impl Deref for Connection { - type Target = xcb::Connection; - - fn deref(&self) -> &xcb::Connection { - &self.conn - } -} - -#[link(name = "X11-xcb")] -extern "C" { - fn XGetXCBConnection(display: *mut x11::xlib::Display) -> *mut xcb::ffi::xcb_connection_t; - fn XSetEventQueueOwner(display: *mut x11::xlib::Display, owner: i32); -} - -fn egli_err(err: egli::error::Error) -> Error { - format_err!("egli error: {:?}", err) -} - -impl Connection { - pub fn new() -> Result { - let display = unsafe { x11::xlib::XOpenDisplay(ptr::null()) }; - if display.is_null() { - bail!("failed to open display"); - } - let screen_num = unsafe { x11::xlib::XDefaultScreen(display) }; - let conn = unsafe { xcb::Connection::from_raw_conn(XGetXCBConnection(display)) }; - unsafe { XSetEventQueueOwner(display, 1) }; - - let atom_protocols = xcb::intern_atom(&conn, false, "WM_PROTOCOLS") - .get_reply()? - .atom(); - 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 atom_targets = xcb::intern_atom(&conn, false, "TARGETS") - .get_reply()? - .atom(); - let atom_clipboard = xcb::intern_atom(&conn, false, "CLIPBOARD") - .get_reply()? - .atom(); - - let keysyms = unsafe { xcb_key_symbols_alloc(conn.get_raw_conn()) }; - - let egl_display = egli::Display::from_display_id(display as *mut _).map_err(egli_err)?; - - let egl_version = egl_display.initialize_and_get_version().map_err(egli_err)?; - println!("Using EGL {}", egl_version); - - let configs = egl_display - .config_filter() - .with_red_size(8) - .with_green_size(8) - .with_blue_size(8) - .with_depth_size(24) - .with_surface_type( - egli::SurfaceType::WINDOW | egli::SurfaceType::PBUFFER | egli::SurfaceType::PIXMAP, - ) - .with_renderable_type(egli::RenderableType::OPENGL_ES2) - .with_conformant(egli::RenderableType::OPENGL_ES2) - .choose_configs() - .map_err(|e| format_err!("failed to get EGL config: {:?}", e))?; - - let first_config = *configs.first().ok_or(failure::err_msg( - "no compatible EGL configuration was found", - ))?; - - Ok(Connection { - display, - conn, - screen_num, - atom_protocols, - atom_clipboard, - atom_delete, - keysyms, - atom_utf8_string, - atom_xsel_data, - atom_targets, - egl_display: Rc::new(egl_display), - egl_config: first_config, - }) - } - - pub fn conn(&self) -> &xcb::Connection { - &self.conn - } - - pub fn screen_num(&self) -> i32 { - self.screen_num - } - - pub fn atom_delete(&self) -> xcb::Atom { - self.atom_delete - } - - pub fn lookup_keysym(&self, event: &xcb::KeyPressEvent, shifted: bool) -> xcb::Keysym { - unsafe { - let sym = - xcb_key_press_lookup_keysym(self.keysyms, event.ptr, if shifted { 1 } else { 0 }); - if sym == 0 && shifted { - xcb_key_press_lookup_keysym(self.keysyms, event.ptr, 0) - } else { - sym - } - } - } -} - -impl Drop for Connection { - fn drop(&mut self) { - unsafe { - xcb_key_symbols_free(self.keysyms); - } - } -} - -/// The X protocol allows referencing a number of drawable -/// objects. This trait marks those objects here in code. -pub trait Drawable { - fn as_drawable(&self) -> xcb::xproto::Drawable; - fn get_conn(&self) -> &Connection; -} - -struct GlState { - display: Rc, - surface: egli::Surface, - egl_context: egli::Context, -} - -/// A Window! -pub struct Window<'a> { - window_id: xcb::xproto::Window, - conn: &'a Connection, - gl: Rc, - glium_context: Rc, -} - -impl<'a> Drawable for Window<'a> { - fn as_drawable(&self) -> xcb::xproto::Drawable { - self.window_id - } - - fn get_conn(&self) -> &Connection { - self.conn - } -} - -impl<'a> Window<'a> { - /// Create a new window on the specified screen with the specified - /// dimensions - pub fn new(conn: &Connection, width: u16, height: u16) -> Result { - let setup = conn.conn().get_setup(); - let screen = setup - .roots() - .nth(conn.screen_num() as usize) - .ok_or(failure::err_msg("no screen?"))?; - let window_id = conn.conn().generate_id(); - - xcb::create_window_checked( - conn.conn(), - xcb::COPY_FROM_PARENT as u8, - window_id, - screen.root(), - // x, y - 0, - 0, - // width, height - width, - height, - // border width - 0, - xcb::WINDOW_CLASS_INPUT_OUTPUT as u16, - screen.root_visual(), - &[ - ( - xcb::CW_EVENT_MASK, - xcb::EVENT_MASK_EXPOSURE | xcb::EVENT_MASK_KEY_PRESS - | xcb::EVENT_MASK_BUTTON_PRESS - | xcb::EVENT_MASK_BUTTON_RELEASE - | xcb::EVENT_MASK_POINTER_MOTION - | xcb::EVENT_MASK_BUTTON_MOTION - | xcb::EVENT_MASK_KEY_RELEASE - | xcb::EVENT_MASK_STRUCTURE_NOTIFY, - ), - ], - ).request_check()?; - - xcb::change_property( - conn, - xcb::PROP_MODE_REPLACE as u8, - window_id, - conn.atom_protocols, - 4, - 32, - &[conn.atom_delete], - ); - - let surface = conn.egl_display - .create_window_surface(conn.egl_config, window_id as *mut _) - .map_err(egli_err)?; - - let egl_context = conn.egl_display - .create_context_with_client_version( - conn.egl_config, - egli::ContextClientVersion::OpenGlEs2, - ) - .map_err(egli_err)?; - - conn.egl_display - .make_current(&surface, &surface, &egl_context) - .map_err(egli_err)?; - - gl::load_with(|s| unsafe { mem::transmute(egli::egl::get_proc_address(s)) }); - - let gl_state = Rc::new(GlState { - display: Rc::clone(&conn.egl_display), - egl_context, - surface, - }); - - let glium_context = unsafe { - glium::backend::Context::new( - Rc::clone(&gl_state), - // we're single threaded, so no need to check contexts - false, - if cfg!(debug_assertions) { - //glium::debug::DebugCallbackBehavior::PrintAll - glium::debug::DebugCallbackBehavior::DebugMessageOnError - } else { - glium::debug::DebugCallbackBehavior::Ignore - }, - )? - }; - - Ok(Window { - conn, - window_id, - gl: gl_state, - glium_context, - }) - } - - /// Change the title for the window manager - pub fn set_title(&self, title: &str) { - xcb_util::icccm::set_wm_name(self.conn.conn(), self.window_id, title); - } - - /// Display the window - pub fn show(&self) { - xcb::map_window(self.conn.conn(), self.window_id); - } - - pub fn draw(&self) -> glium::Frame { - glium::Frame::new( - self.glium_context.clone(), - self.gl.get_framebuffer_dimensions(), - ) - } -} - -impl<'a> Drop for Window<'a> { - fn drop(&mut self) { - xcb::destroy_window(self.conn.conn(), self.window_id); - } -} - -impl<'a> glium::backend::Facade for Window<'a> { - fn get_context(&self) -> &Rc { - &self.glium_context - } -} - -unsafe impl glium::backend::Backend for GlState { - fn swap_buffers(&self) -> result::Result<(), glium::SwapBuffersError> { - self.display.swap_buffers(&self.surface).map_err(|_| { - // We're guessing that this is the case as the other option - // that glium recognizes is threading related and we're - // single threaded. - glium::SwapBuffersError::AlreadySwapped - }) - } - - unsafe fn get_proc_address(&self, symbol: &str) -> *const os::raw::c_void { - mem::transmute(egli::egl::get_proc_address(symbol)) - } - - fn get_framebuffer_dimensions(&self) -> (u32, u32) { - ( - self.surface.query_width().unwrap() as u32, - self.surface.query_height().unwrap() as u32, - ) - } - - fn is_current(&self) -> bool { - true - } - - unsafe fn make_current(&self) { - self.display - .make_current(&self.surface, &self.surface, &self.egl_context) - .expect("make_current failed"); - } -} diff --git a/src/xwindows/xkeysyms.rs b/src/xwindows/xkeysyms.rs deleted file mode 100644 index 9226eb261..000000000 --- a/src/xwindows/xkeysyms.rs +++ /dev/null @@ -1,251 +0,0 @@ -#![allow(non_upper_case_globals, dead_code)] -use xcb::KeyPressEvent; -use xcb::ffi::xproto::xcb_keysym_t; - -pub const XK_VoidSymbol: xcb_keysym_t = 0xffffff; -pub const XK_BackSpace: xcb_keysym_t = 0xff08; -pub const XK_Tab: xcb_keysym_t = 0xff09; -pub const XK_Linefeed: xcb_keysym_t = 0xff0a; -pub const XK_Clear: xcb_keysym_t = 0xff0b; -pub const XK_Return: xcb_keysym_t = 0xff0d; -pub const XK_Pause: xcb_keysym_t = 0xff13; -pub const XK_Scroll_Lock: xcb_keysym_t = 0xff14; -pub const XK_Sys_Req: xcb_keysym_t = 0xff15; -pub const XK_Escape: xcb_keysym_t = 0xff1b; -pub const XK_Delete: xcb_keysym_t = 0xffff; - -pub const XK_Home: xcb_keysym_t = 0xff50; -pub const XK_Left: xcb_keysym_t = 0xff51; -pub const XK_Up: xcb_keysym_t = 0xff52; -pub const XK_Right: xcb_keysym_t = 0xff53; -pub const XK_Down: xcb_keysym_t = 0xff54; -pub const XK_Prior: xcb_keysym_t = 0xff55; -pub const XK_Page_Up: xcb_keysym_t = 0xff55; -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; -pub const XK_KP_Enter: xcb_keysym_t = 0xff8d; -pub const XK_KP_F1: xcb_keysym_t = 0xff91; -pub const XK_KP_F2: xcb_keysym_t = 0xff92; -pub const XK_KP_F3: xcb_keysym_t = 0xff93; -pub const XK_KP_F4: xcb_keysym_t = 0xff94; -pub const XK_KP_Home: xcb_keysym_t = 0xff95; -pub const XK_KP_Left: xcb_keysym_t = 0xff96; -pub const XK_KP_Up: xcb_keysym_t = 0xff97; -pub const XK_KP_Right: xcb_keysym_t = 0xff98; -pub const XK_KP_Down: xcb_keysym_t = 0xff99; -pub const XK_KP_Prior: xcb_keysym_t = 0xff9a; -pub const XK_KP_Page_Up: xcb_keysym_t = 0xff9a; -pub const XK_KP_Next: xcb_keysym_t = 0xff9b; -pub const XK_KP_Page_Down: xcb_keysym_t = 0xff9b; -pub const XK_KP_End: xcb_keysym_t = 0xff9c; -pub const XK_KP_Begin: xcb_keysym_t = 0xff9d; -pub const XK_KP_Insert: xcb_keysym_t = 0xff9e; -pub const XK_KP_Delete: xcb_keysym_t = 0xff9f; -pub const XK_KP_Equal: xcb_keysym_t = 0xffbd; -pub const XK_KP_Multiply: xcb_keysym_t = 0xffaa; -pub const XK_KP_Add: xcb_keysym_t = 0xffab; -pub const XK_KP_Separator: xcb_keysym_t = 0xffac; -pub const XK_KP_Subtract: xcb_keysym_t = 0xffad; -pub const XK_KP_Decimal: xcb_keysym_t = 0xffae; -pub const XK_KP_Divide: xcb_keysym_t = 0xffaf; - -pub const XK_KP_0: xcb_keysym_t = 0xffb0; -pub const XK_KP_1: xcb_keysym_t = 0xffb1; -pub const XK_KP_2: xcb_keysym_t = 0xffb2; -pub const XK_KP_3: xcb_keysym_t = 0xffb3; -pub const XK_KP_4: xcb_keysym_t = 0xffb4; -pub const XK_KP_5: xcb_keysym_t = 0xffb5; -pub const XK_KP_6: xcb_keysym_t = 0xffb6; -pub const XK_KP_7: xcb_keysym_t = 0xffb7; -pub const XK_KP_8: xcb_keysym_t = 0xffb8; -pub const XK_KP_9: xcb_keysym_t = 0xffb9; - -pub const XK_F1: xcb_keysym_t = 0xffbe; -pub const XK_F2: xcb_keysym_t = 0xffbf; -pub const XK_F3: xcb_keysym_t = 0xffc0; -pub const XK_F4: xcb_keysym_t = 0xffc1; -pub const XK_F5: xcb_keysym_t = 0xffc2; -pub const XK_F6: xcb_keysym_t = 0xffc3; -pub const XK_F7: xcb_keysym_t = 0xffc4; -pub const XK_F8: xcb_keysym_t = 0xffc5; -pub const XK_F9: xcb_keysym_t = 0xffc6; -pub const XK_F10: xcb_keysym_t = 0xffc7; -pub const XK_F11: xcb_keysym_t = 0xffc8; -pub const XK_F12: xcb_keysym_t = 0xffc9; - -pub const XK_Shift_L: xcb_keysym_t = 0xffe1; -pub const XK_Shift_R: xcb_keysym_t = 0xffe2; -pub const XK_Control_L: xcb_keysym_t = 0xffe3; -pub const XK_Control_R: xcb_keysym_t = 0xffe4; -pub const XK_Caps_Lock: xcb_keysym_t = 0xffe5; -pub const XK_Shift_Lock: xcb_keysym_t = 0xffe6; - -pub const XK_Meta_L: xcb_keysym_t = 0xffe7; -pub const XK_Meta_R: xcb_keysym_t = 0xffe8; -pub const XK_Alt_L: xcb_keysym_t = 0xffe9; -pub const XK_Alt_R: xcb_keysym_t = 0xffea; -pub const XK_Super_L: xcb_keysym_t = 0xffeb; -pub const XK_Super_R: xcb_keysym_t = 0xffec; -pub const XK_Hyper_L: xcb_keysym_t = 0xffed; -pub const XK_Hyper_R: xcb_keysym_t = 0xffee; - -pub const XK_space: xcb_keysym_t = 0x0020; -pub const XK_exclam: xcb_keysym_t = 0x0021; -pub const XK_quotedbl: xcb_keysym_t = 0x0022; -pub const XK_numbersign: xcb_keysym_t = 0x0023; -pub const XK_dollar: xcb_keysym_t = 0x0024; -pub const XK_percent: xcb_keysym_t = 0x0025; -pub const XK_ampersand: xcb_keysym_t = 0x0026; -pub const XK_apostrophe: xcb_keysym_t = 0x0027; -pub const XK_quoteright: xcb_keysym_t = 0x0027; -pub const XK_parenleft: xcb_keysym_t = 0x0028; -pub const XK_parenright: xcb_keysym_t = 0x0029; -pub const XK_asterisk: xcb_keysym_t = 0x002a; -pub const XK_plus: xcb_keysym_t = 0x002b; -pub const XK_comma: xcb_keysym_t = 0x002c; -pub const XK_minus: xcb_keysym_t = 0x002d; -pub const XK_period: xcb_keysym_t = 0x002e; -pub const XK_slash: xcb_keysym_t = 0x002f; -pub const XK_0: xcb_keysym_t = 0x0030; -pub const XK_1: xcb_keysym_t = 0x0031; -pub const XK_2: xcb_keysym_t = 0x0032; -pub const XK_3: xcb_keysym_t = 0x0033; -pub const XK_4: xcb_keysym_t = 0x0034; -pub const XK_5: xcb_keysym_t = 0x0035; -pub const XK_6: xcb_keysym_t = 0x0036; -pub const XK_7: xcb_keysym_t = 0x0037; -pub const XK_8: xcb_keysym_t = 0x0038; -pub const XK_9: xcb_keysym_t = 0x0039; -pub const XK_colon: xcb_keysym_t = 0x003a; -pub const XK_semicolon: xcb_keysym_t = 0x003b; -pub const XK_less: xcb_keysym_t = 0x003c; -pub const XK_equal: xcb_keysym_t = 0x003d; -pub const XK_greater: xcb_keysym_t = 0x003e; -pub const XK_question: xcb_keysym_t = 0x003f; -pub const XK_at: xcb_keysym_t = 0x0040; -pub const XK_A: xcb_keysym_t = 0x0041; -pub const XK_B: xcb_keysym_t = 0x0042; -pub const XK_C: xcb_keysym_t = 0x0043; -pub const XK_D: xcb_keysym_t = 0x0044; -pub const XK_E: xcb_keysym_t = 0x0045; -pub const XK_F: xcb_keysym_t = 0x0046; -pub const XK_G: xcb_keysym_t = 0x0047; -pub const XK_H: xcb_keysym_t = 0x0048; -pub const XK_I: xcb_keysym_t = 0x0049; -pub const XK_J: xcb_keysym_t = 0x004a; -pub const XK_K: xcb_keysym_t = 0x004b; -pub const XK_L: xcb_keysym_t = 0x004c; -pub const XK_M: xcb_keysym_t = 0x004d; -pub const XK_N: xcb_keysym_t = 0x004e; -pub const XK_O: xcb_keysym_t = 0x004f; -pub const XK_P: xcb_keysym_t = 0x0050; -pub const XK_Q: xcb_keysym_t = 0x0051; -pub const XK_R: xcb_keysym_t = 0x0052; -pub const XK_S: xcb_keysym_t = 0x0053; -pub const XK_T: xcb_keysym_t = 0x0054; -pub const XK_U: xcb_keysym_t = 0x0055; -pub const XK_V: xcb_keysym_t = 0x0056; -pub const XK_W: xcb_keysym_t = 0x0057; -pub const XK_X: xcb_keysym_t = 0x0058; -pub const XK_Y: xcb_keysym_t = 0x0059; -pub const XK_Z: xcb_keysym_t = 0x005a; -pub const XK_bracketleft: xcb_keysym_t = 0x005b; -pub const XK_backslash: xcb_keysym_t = 0x005c; -pub const XK_bracketright: xcb_keysym_t = 0x005d; -pub const XK_asciicircum: xcb_keysym_t = 0x005e; -pub const XK_underscore: xcb_keysym_t = 0x005f; -pub const XK_grave: xcb_keysym_t = 0x0060; -pub const XK_quoteleft: xcb_keysym_t = 0x0060; -pub const XK_a: xcb_keysym_t = 0x0061; -pub const XK_b: xcb_keysym_t = 0x0062; -pub const XK_c: xcb_keysym_t = 0x0063; -pub const XK_d: xcb_keysym_t = 0x0064; -pub const XK_e: xcb_keysym_t = 0x0065; -pub const XK_f: xcb_keysym_t = 0x0066; -pub const XK_g: xcb_keysym_t = 0x0067; -pub const XK_h: xcb_keysym_t = 0x0068; -pub const XK_i: xcb_keysym_t = 0x0069; -pub const XK_j: xcb_keysym_t = 0x006a; -pub const XK_k: xcb_keysym_t = 0x006b; -pub const XK_l: xcb_keysym_t = 0x006c; -pub const XK_m: xcb_keysym_t = 0x006d; -pub const XK_n: xcb_keysym_t = 0x006e; -pub const XK_o: xcb_keysym_t = 0x006f; -pub const XK_p: xcb_keysym_t = 0x0070; -pub const XK_q: xcb_keysym_t = 0x0071; -pub const XK_r: xcb_keysym_t = 0x0072; -pub const XK_s: xcb_keysym_t = 0x0073; -pub const XK_t: xcb_keysym_t = 0x0074; -pub const XK_u: xcb_keysym_t = 0x0075; -pub const XK_v: xcb_keysym_t = 0x0076; -pub const XK_w: xcb_keysym_t = 0x0077; -pub const XK_x: xcb_keysym_t = 0x0078; -pub const XK_y: xcb_keysym_t = 0x0079; -pub const XK_z: xcb_keysym_t = 0x007a; -pub const XK_braceleft: xcb_keysym_t = 0x007b; -pub const XK_bar: xcb_keysym_t = 0x007c; -pub const XK_braceright: xcb_keysym_t = 0x007d; -pub const XK_asciitilde: xcb_keysym_t = 0x007e; - -use term::KeyCode; -use term::KeyModifiers; - -pub fn xcb_keysym_to_keycode(k: xcb_keysym_t) -> KeyCode { - match k { - XK_space...XK_asciitilde => { - // This range overlaps with ascii - KeyCode::Char(k as u8 as char) - } - XK_BackSpace | XK_Tab | XK_Linefeed | XK_Return | XK_Escape => { - KeyCode::Char((k & 0xff) as u8 as char) - } - XK_Control_L | XK_Control_R => KeyCode::Control, - XK_Alt_L | XK_Alt_R => KeyCode::Alt, - XK_Meta_L | XK_Meta_R => KeyCode::Meta, - XK_Super_L | XK_Super_R => KeyCode::Super, - XK_Hyper_L | XK_Hyper_R => KeyCode::Hyper, - XK_Shift_L | XK_Shift_R => KeyCode::Shift, - XK_Home => KeyCode::Home, - XK_Left => KeyCode::Left, - XK_Up => KeyCode::Up, - XK_Right => KeyCode::Right, - XK_Down => KeyCode::Down, - XK_Page_Up => KeyCode::PageUp, - XK_Page_Down => KeyCode::PageDown, - XK_End => KeyCode::End, - XK_KP_Insert | XK_Insert => KeyCode::Insert, - _ => KeyCode::Unknown, - } -} - -pub fn modifiers_from_state(state: u16) -> KeyModifiers { - use xcb::xproto::*; - - let mut mods = KeyModifiers::default(); - let state = state as u32; - - if state & MOD_MASK_SHIFT != 0 { - mods |= KeyModifiers::SHIFT; - } - if state & MOD_MASK_CONTROL != 0 { - mods |= KeyModifiers::CTRL; - } - if state & MOD_MASK_1 != 0 { - mods |= KeyModifiers::ALT; - } - if state & MOD_MASK_4 != 0 { - mods |= KeyModifiers::SUPER; - } - - mods -} - -pub fn modifiers(event: &KeyPressEvent) -> KeyModifiers { - modifiers_from_state(event.state()) -} diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs deleted file mode 100644 index 7250c20e0..000000000 --- a/src/xwindows/xwin.rs +++ /dev/null @@ -1,267 +0,0 @@ -use super::{Connection, Window}; -use super::clipboard::Clipboard; -use super::super::opengl::render::Renderer; -use super::xkeysyms; -use failure::Error; -use font::FontConfiguration; -use pty::MasterPty; -use std::io::{Read, Write}; -use std::process::Child; -use std::process::Command; -use std::rc::Rc; -use term::{self, KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; -use term::hyperlink::Hyperlink; -use xcb; - -/// Holds the information we need to implement TerminalHost -struct Host<'a> { - window: Window<'a>, - pty: MasterPty, - timestamp: xcb::xproto::Timestamp, - clipboard: Clipboard, -} - -pub struct TerminalWindow<'a> { - host: Host<'a>, - conn: &'a Connection, - renderer: Renderer, - width: u16, - height: u16, - cell_height: usize, - cell_width: usize, - terminal: term::Terminal, - process: Child, -} - -impl<'a> term::TerminalHost for Host<'a> { - fn writer(&mut self) -> &mut Write { - &mut self.pty - } - - fn click_link(&mut self, link: &Rc) { - // TODO: make this configurable - let mut cmd = Command::new("xdg-open"); - cmd.arg(&link.url); - match cmd.spawn() { - Ok(_) => {} - Err(err) => eprintln!("failed to spawn xdg-open {}: {:?}", link.url, err), - } - } - - fn get_clipboard(&mut self) -> Result { - self.clipboard.get_clipboard() - } - - fn set_clipboard(&mut self, clip: Option) -> Result<(), Error> { - self.clipboard.set_clipboard(clip) - } - - fn set_title(&mut self, title: &str) { - self.window.set_title(title); - } -} - -impl<'a> TerminalWindow<'a> { - pub fn new( - conn: &Connection, - width: u16, - height: u16, - terminal: term::Terminal, - pty: MasterPty, - process: Child, - fonts: FontConfiguration, - palette: term::color::ColorPalette, - ) -> Result { - let (cell_height, cell_width) = { - // Urgh, this is a bit repeaty, but we need to satisfy the borrow checker - let font = fonts.default_font()?; - let metrics = font.borrow_mut().get_fallback(0)?.metrics(); - (metrics.cell_height, metrics.cell_width) - }; - - let window = Window::new(&conn, width, height)?; - window.set_title("wezterm"); - - let host = Host { - window, - pty, - timestamp: 0, - clipboard: Clipboard::new(|| {})?, - }; - - let renderer = Renderer::new(&host.window, width, height, fonts, palette)?; - let cell_height = cell_height.ceil() as usize; - let cell_width = cell_width.ceil() as usize; - - Ok(TerminalWindow { - host, - renderer, - conn, - width, - height, - cell_height, - cell_width, - terminal, - process, - }) - } - - pub fn show(&self) { - self.host.window.show(); - } - - pub fn resize_surfaces(&mut self, width: u16, height: u16) -> Result { - if width != self.width || height != self.height { - debug!("resize {},{}", width, height); - - self.width = width; - self.height = height; - self.renderer.resize(&self.host.window, width, height)?; - - // The +1 in here is to handle an irritating case. - // When we get N rows with a gap of cell_height - 1 left at - // the bottom, we can usually squeeze that extra row in there, - // so optimistically pretend that we have that extra pixel! - let rows = ((height as usize + 1) / self.cell_height) as u16; - let cols = ((width as usize + 1) / self.cell_width) as u16; - self.host.pty.resize(rows, cols, width, height)?; - self.terminal.resize(rows as usize, cols as usize); - - Ok(true) - } else { - debug!("ignoring extra resize"); - Ok(false) - } - } - - pub fn expose(&mut self, _x: u16, _y: u16, _width: u16, _height: u16) -> Result<(), Error> { - self.paint() - } - - pub fn paint(&mut self) -> Result<(), Error> { - let mut target = self.host.window.draw(); - let res = self.renderer.paint(&mut target, &mut self.terminal); - // Ensure that we finish() the target before we let the - // error bubble up, otherwise we lose the context. - target.finish().unwrap(); - res?; - Ok(()) - } - - pub fn test_for_child_exit(&mut self) -> Result<(), Error> { - match self.process.try_wait() { - Ok(Some(status)) => { - bail!("child exited: {}", status); - } - Ok(None) => { - println!("child still running"); - Ok(()) - } - Err(e) => { - bail!("failed to wait for child: {}", e); - } - } - } - - pub fn handle_pty_readable_event(&mut self) { - const BUFSIZE: usize = 8192; - let mut buf = [0; BUFSIZE]; - - match self.host.pty.read(&mut buf) { - Ok(size) => self.terminal.advance_bytes(&buf[0..size], &mut self.host), - Err(err) => eprintln!("error reading from pty: {:?}", err), - } - } - - pub fn need_paint(&self) -> bool { - self.terminal.has_dirty_lines() - } - - fn decode_key(&self, event: &xcb::KeyPressEvent) -> (KeyCode, KeyModifiers) { - let mods = xkeysyms::modifiers(event); - let sym = self.conn - .lookup_keysym(event, mods.contains(KeyModifiers::SHIFT)); - (xkeysyms::xcb_keysym_to_keycode(sym), mods) - } - - fn mouse_event(&mut self, event: MouseEvent) -> Result<(), Error> { - self.terminal.mouse_event(event, &mut self.host)?; - Ok(()) - } - - pub fn dispatch_event(&mut self, event: xcb::GenericEvent) -> Result<(), Error> { - let r = event.response_type() & 0x7f; - match r { - xcb::EXPOSE => { - let expose: &xcb::ExposeEvent = unsafe { xcb::cast_event(&event) }; - self.expose(expose.x(), expose.y(), expose.width(), expose.height())?; - } - xcb::CONFIGURE_NOTIFY => { - let cfg: &xcb::ConfigureNotifyEvent = unsafe { xcb::cast_event(&event) }; - self.resize_surfaces(cfg.width(), cfg.height())?; - } - xcb::KEY_PRESS => { - let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) }; - self.host.timestamp = key_press.time(); - let (code, mods) = self.decode_key(key_press); - self.terminal.key_down(code, mods, &mut self.host)?; - } - xcb::KEY_RELEASE => { - let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) }; - self.host.timestamp = key_press.time(); - let (code, mods) = self.decode_key(key_press); - self.terminal.key_up(code, mods, &mut self.host)?; - } - xcb::MOTION_NOTIFY => { - let motion: &xcb::MotionNotifyEvent = unsafe { xcb::cast_event(&event) }; - - let event = MouseEvent { - kind: MouseEventKind::Move, - button: MouseButton::None, - x: (motion.event_x() as usize / self.cell_width) as usize, - y: (motion.event_y() as usize / self.cell_height) as i64, - modifiers: xkeysyms::modifiers_from_state(motion.state()), - }; - self.mouse_event(event)?; - } - xcb::BUTTON_PRESS | xcb::BUTTON_RELEASE => { - let button_press: &xcb::ButtonPressEvent = unsafe { xcb::cast_event(&event) }; - self.host.timestamp = button_press.time(); - - let event = MouseEvent { - kind: match r { - xcb::BUTTON_PRESS => MouseEventKind::Press, - xcb::BUTTON_RELEASE => MouseEventKind::Release, - _ => unreachable!("button event mismatch"), - }, - x: (button_press.event_x() as usize / self.cell_width) as usize, - y: (button_press.event_y() as usize / self.cell_height) as i64, - button: match button_press.detail() { - 1 => MouseButton::Left, - 2 => MouseButton::Middle, - 3 => MouseButton::Right, - 4 => MouseButton::WheelUp, - 5 => MouseButton::WheelDown, - _ => { - eprintln!("button {} is not implemented", button_press.detail()); - return Ok(()); - } - }, - modifiers: xkeysyms::modifiers_from_state(button_press.state()), - }; - - self.mouse_event(event)?; - } - xcb::CLIENT_MESSAGE => { - let msg: &xcb::ClientMessageEvent = unsafe { xcb::cast_event(&event) }; - println!("CLIENT_MESSAGE {:?}", msg.data().data32()); - if msg.data().data32()[0] == self.conn.atom_delete() { - // TODO: cleaner exit handling - bail!("window close requested!"); - } - } - _ => {} - } - Ok(()) - } -}