From c1f089970fd3d4b9137d07647f8cd028b2b5b3a9 Mon Sep 17 00:00:00 2001 From: mkosem Date: Tue, 24 Sep 2019 13:43:54 -0400 Subject: [PATCH] Add Xembed support Fixes #631. --- alacritty/src/cli.rs | 196 ++++++++++++------------ alacritty_terminal/src/config/window.rs | 4 + alacritty_terminal/src/window.rs | 57 ++++++- extra/alacritty.man | 3 + extra/completions/_alacritty | 1 + extra/completions/alacritty.bash | 2 +- extra/completions/alacritty.fish | 3 + 7 files changed, 169 insertions(+), 97 deletions(-) diff --git a/alacritty/src/cli.rs b/alacritty/src/cli.rs index 7c1554aa..d48ab4d7 100644 --- a/alacritty/src/cli.rs +++ b/alacritty/src/cli.rs @@ -32,6 +32,7 @@ pub struct Options { pub position: Option>, pub title: Option, pub class: Option, + pub embed: Option, pub log_level: LevelFilter, pub command: Option>, pub working_dir: Option, @@ -49,6 +50,7 @@ impl Default for Options { position: None, title: None, class: None, + embed: None, log_level: LevelFilter::Warn, command: None, working_dir: None, @@ -69,100 +71,104 @@ impl Options { let mut options = Options::default(); - let matches = App::new(crate_name!()) - .version(version.as_str()) - .author(crate_authors!("\n")) - .about(crate_description!()) - .arg(Arg::with_name("ref-test").long("ref-test").help("Generates ref test")) - .arg( - Arg::with_name("live-config-reload") - .long("live-config-reload") - .help("Enable automatic config reloading"), - ) - .arg( - Arg::with_name("no-live-config-reload") - .long("no-live-config-reload") - .help("Disable automatic config reloading") - .conflicts_with("live-config-reload"), - ) - .arg( - Arg::with_name("print-events") - .long("print-events") - .help("Print all events to stdout"), - ) - .arg( - Arg::with_name("persistent-logging") - .long("persistent-logging") - .help("Keep the log file after quitting Alacritty"), - ) - .arg( - Arg::with_name("dimensions") - .long("dimensions") - .short("d") - .value_names(&["columns", "lines"]) - .help( - "Defines the window dimensions. Falls back to size specified by window \ - manager if set to 0x0 [default: 0x0]", - ), - ) - .arg( - Arg::with_name("position") - .long("position") - .allow_hyphen_values(true) - .value_names(&["x-pos", "y-pos"]) - .help( - "Defines the window position. Falls back to position specified by window \ - manager if unset [default: unset]", - ), - ) - .arg( - Arg::with_name("title") - .long("title") - .short("t") - .takes_value(true) - .help(&format!("Defines the window title [default: {}]", DEFAULT_NAME)), - ) - .arg( - Arg::with_name("class") - .long("class") - .takes_value(true) - .help(&format!("Defines window class on Linux [default: {}]", DEFAULT_NAME)), - ) - .arg( - Arg::with_name("q") - .short("q") - .multiple(true) - .conflicts_with("v") - .help("Reduces the level of verbosity (the min level is -qq)"), - ) - .arg( - Arg::with_name("v") - .short("v") - .multiple(true) - .conflicts_with("q") - .help("Increases the level of verbosity (the max level is -vvv)"), - ) - .arg( - Arg::with_name("working-directory") - .long("working-directory") - .takes_value(true) - .help("Start the shell in the specified working directory"), - ) - .arg(Arg::with_name("config-file").long("config-file").takes_value(true).help( - "Specify alternative configuration file [default: \ - $XDG_CONFIG_HOME/alacritty/alacritty.yml]", - )) - .arg( - Arg::with_name("command") - .long("command") - .short("e") - .multiple(true) - .takes_value(true) - .min_values(1) - .allow_hyphen_values(true) - .help("Command and args to execute (must be last argument)"), - ) - .get_matches(); + let matches = + App::new(crate_name!()) + .version(version.as_str()) + .author(crate_authors!("\n")) + .about(crate_description!()) + .arg(Arg::with_name("ref-test").long("ref-test").help("Generates ref test")) + .arg( + Arg::with_name("live-config-reload") + .long("live-config-reload") + .help("Enable automatic config reloading"), + ) + .arg( + Arg::with_name("no-live-config-reload") + .long("no-live-config-reload") + .help("Disable automatic config reloading") + .conflicts_with("live-config-reload"), + ) + .arg( + Arg::with_name("print-events") + .long("print-events") + .help("Print all events to stdout"), + ) + .arg( + Arg::with_name("persistent-logging") + .long("persistent-logging") + .help("Keep the log file after quitting Alacritty"), + ) + .arg( + Arg::with_name("dimensions") + .long("dimensions") + .short("d") + .value_names(&["columns", "lines"]) + .help( + "Defines the window dimensions. Falls back to size specified by \ + window manager if set to 0x0 [default: 0x0]", + ), + ) + .arg( + Arg::with_name("position") + .long("position") + .allow_hyphen_values(true) + .value_names(&["x-pos", "y-pos"]) + .help( + "Defines the window position. Falls back to position specified by \ + window manager if unset [default: unset]", + ), + ) + .arg( + Arg::with_name("title") + .long("title") + .short("t") + .takes_value(true) + .help(&format!("Defines the window title [default: {}]", DEFAULT_NAME)), + ) + .arg( + Arg::with_name("class").long("class").takes_value(true).help(&format!( + "Defines window class on Linux [default: {}]", + DEFAULT_NAME + )), + ) + .arg(Arg::with_name("embed").long("embed").takes_value(true).help( + "Defines the X11 window ID (as a decimal integer) to embed Alacritty within", + )) + .arg( + Arg::with_name("q") + .short("q") + .multiple(true) + .conflicts_with("v") + .help("Reduces the level of verbosity (the min level is -qq)"), + ) + .arg( + Arg::with_name("v") + .short("v") + .multiple(true) + .conflicts_with("q") + .help("Increases the level of verbosity (the max level is -vvv)"), + ) + .arg( + Arg::with_name("working-directory") + .long("working-directory") + .takes_value(true) + .help("Start the shell in the specified working directory"), + ) + .arg(Arg::with_name("config-file").long("config-file").takes_value(true).help( + "Specify alternative configuration file [default: \ + $XDG_CONFIG_HOME/alacritty/alacritty.yml]", + )) + .arg( + Arg::with_name("command") + .long("command") + .short("e") + .multiple(true) + .takes_value(true) + .min_values(1) + .allow_hyphen_values(true) + .help("Command and args to execute (must be last argument)"), + ) + .get_matches(); if matches.is_present("ref-test") { options.ref_test = true; @@ -200,6 +206,7 @@ impl Options { options.class = matches.value_of("class").map(ToOwned::to_owned); options.title = matches.value_of("title").map(ToOwned::to_owned); + options.embed = matches.value_of("embed").map(ToOwned::to_owned); match matches.occurrences_of("q") { 0 => {}, @@ -250,6 +257,7 @@ impl Options { config.window.dimensions = self.dimensions.unwrap_or(config.window.dimensions); config.window.position = self.position.or(config.window.position); config.window.title = self.title.or(config.window.title); + config.window.embed = self.embed.and_then(|embed| embed.parse().ok()); if let Some(class) = self.class { let parts: Vec<_> = class.split(',').collect(); diff --git a/alacritty_terminal/src/config/window.rs b/alacritty_terminal/src/config/window.rs index 60298383..7ca90a5b 100644 --- a/alacritty_terminal/src/config/window.rs +++ b/alacritty_terminal/src/config/window.rs @@ -39,6 +39,10 @@ pub struct WindowConfig { #[serde(deserialize_with = "from_string_or_deserialize")] pub class: Class, + /// XEmbed parent + #[serde(skip)] + pub embed: Option, + /// GTK theme variant #[serde(deserialize_with = "option_explicit_none")] pub gtk_theme_variant: Option, diff --git a/alacritty_terminal/src/window.rs b/alacritty_terminal/src/window.rs index b5c56d3c..3e3f47f7 100644 --- a/alacritty_terminal/src/window.rs +++ b/alacritty_terminal/src/window.rs @@ -12,11 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. use std::convert::From; -#[cfg(not(any(target_os = "macos", target_os = "windows")))] +#[cfg(not(any(target_os = "macos", windows)))] use std::ffi::c_void; use std::fmt::Display; -use crate::gl; use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalSize}; #[cfg(target_os = "macos")] use glutin::os::macos::WindowExt; @@ -24,14 +23,19 @@ use glutin::os::macos::WindowExt; use glutin::os::unix::{EventsLoopExt, WindowExt}; #[cfg(not(target_os = "macos"))] use glutin::Icon; +#[cfg(not(any(target_os = "macos", windows)))] +use glutin::Window as GlutinWindow; use glutin::{ self, ContextBuilder, ControlFlow, Event, EventsLoop, MouseCursor, PossiblyCurrent, WindowBuilder, }; #[cfg(not(target_os = "macos"))] use image::ImageFormat; +#[cfg(not(any(target_os = "macos", windows)))] +use x11_dl::xlib::{Xlib, Display as XDisplay, PropModeReplace, XErrorEvent}; use crate::config::{Config, Decorations, StartupMode, WindowConfig}; +use crate::gl; // It's required to be in this directory due to the `windows.rc` file #[cfg(not(target_os = "macos"))] @@ -167,6 +171,16 @@ impl Window { // Set OpenGL symbol loader. This call MUST be after window.make_current on windows. gl::load_with(|symbol| windowed_context.get_proc_address(symbol) as *const _); + // On X11, embed the window inside another if the parent ID has been set + #[cfg(not(any(target_os = "macos", windows)))] + { + if event_loop.is_x11() { + if let Some(parent_window_id) = config.window.embed { + x_embed_window(window, parent_window_id); + } + } + } + let window = Window { event_loop, current_mouse_cursor: MouseCursor::Default, @@ -409,6 +423,45 @@ impl Window { } } +#[cfg(not(any(target_os = "macos", windows)))] +fn x_embed_window(window: &GlutinWindow, parent_id: u64) { + let (xlib_display, xlib_window) = match (window.get_xlib_display(), window.get_xlib_window()) { + (Some(display), Some(window)) => (display, window), + _ => return, + }; + + let xlib = Xlib::open().expect("get xlib"); + + unsafe { + let atom = (xlib.XInternAtom)(xlib_display as *mut _, "_XEMBED".as_ptr() as *const _, 0); + (xlib.XChangeProperty)( + xlib_display as _, + xlib_window as _, + atom, + atom, + 32, + PropModeReplace, + [0, 1].as_ptr(), + 2, + ); + + // Register new error handler + let old_handler = (xlib.XSetErrorHandler)(Some(xembed_error_handler)); + + // Check for the existence of the target before attempting reparenting + (xlib.XReparentWindow)(xlib_display as _, xlib_window as _, parent_id, 0, 0); + + // Drain errors and restore original error handler + (xlib.XSync)(xlib_display as _, 0); + (xlib.XSetErrorHandler)(old_handler); + } +} + +#[cfg(not(any(target_os = "macos", windows)))] +unsafe extern "C" fn xembed_error_handler(_: *mut XDisplay, _: *mut XErrorEvent) -> i32 { + die!("Could not embed into specified window."); +} + impl Proxy { /// Wakes up the event loop of the window /// diff --git a/extra/alacritty.man b/extra/alacritty.man index dc0f9096..fafb10c3 100644 --- a/extra/alacritty.man +++ b/extra/alacritty.man @@ -57,6 +57,9 @@ Defines the window position. Falls back to position specified by window manager \fB\-t\fR, \fB\-\-title\fR Defines the window title [default: Alacritty] .TP +\fB\-\-embed\fR <parent> +Defines the X11 window ID (as a decimal integer) to embed Alacritty within +.TP \fB\-\-working\-directory\fR <working\-directory> Start the shell in the specified working directory .SH "SEE ALSO" diff --git a/extra/completions/_alacritty b/extra/completions/_alacritty index e9b0656f..c2fb29c2 100644 --- a/extra/completions/_alacritty +++ b/extra/completions/_alacritty @@ -14,6 +14,7 @@ _arguments \ '(-q)'{-v,-vv,-vvv}"[increase the level of verbosity (max is -vvv)]" \ "$ign(-)"{-V,--version}"[print version information]" \ "--class=[define the window class]:class" \ + "--embed=[define the X11 window ID (as a decimal integer) to embed Alacritty within]:windowId" \ "(-e --command)"{-e,--command}"[execute command (must be last arg)]:program: _command_names -e:*::program arguments: _normal" \ "--config-file=[specify an alternative config file]:file:_files" \ "(-d --dimensions)"{-d,--dimensions}"[specify window dimensions]:columns: :lines" \ diff --git a/extra/completions/alacritty.bash b/extra/completions/alacritty.bash index fc39eb17..d87dd215 100644 --- a/extra/completions/alacritty.bash +++ b/extra/completions/alacritty.bash @@ -11,7 +11,7 @@ _alacritty() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" prevprev="${COMP_WORDS[COMP_CWORD-2]}" - opts="-h --help -V --version --live-config-reload --no-live-config-reload --persistent-logging --print-events -q -qq -v -vv -vvv --ref-test -e --command --config-file -d --dimensions --position -t --title --class --working-directory" + opts="-h --help -V --version --live-config-reload --no-live-config-reload --persistent-logging --print-events -q -qq -v -vv -vvv --ref-test -e --command --config-file -d --dimensions --position -t --title --embed --class --working-directory" # If `--command` or `-e` is used, stop completing for i in "${!COMP_WORDS[@]}"; do diff --git a/extra/completions/alacritty.fish b/extra/completions/alacritty.fish index dedf6c47..3daa3447 100644 --- a/extra/completions/alacritty.fish +++ b/extra/completions/alacritty.fish @@ -29,6 +29,9 @@ complete -c alacritty \ complete -c alacritty \ -l "class" \ -d "Defines the window class" +complete -c alacritty \ + -l "embed" \ + -d "Defines the X11 window ID (as a decimal integer) to embed Alacritty within" complete -c alacritty \ -x \ -a '(__fish_complete_directories (commandline -ct))' \