1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-25 14:05:41 -05:00

Add Xembed support

Fixes #631.
This commit is contained in:
mkosem 2019-09-24 13:43:54 -04:00 committed by Christian Duerr
parent 856cddc873
commit c1f089970f
7 changed files with 169 additions and 97 deletions

View file

@ -32,6 +32,7 @@ pub struct Options {
pub position: Option<Delta<i32>>, pub position: Option<Delta<i32>>,
pub title: Option<String>, pub title: Option<String>,
pub class: Option<String>, pub class: Option<String>,
pub embed: Option<String>,
pub log_level: LevelFilter, pub log_level: LevelFilter,
pub command: Option<Shell<'static>>, pub command: Option<Shell<'static>>,
pub working_dir: Option<PathBuf>, pub working_dir: Option<PathBuf>,
@ -49,6 +50,7 @@ impl Default for Options {
position: None, position: None,
title: None, title: None,
class: None, class: None,
embed: None,
log_level: LevelFilter::Warn, log_level: LevelFilter::Warn,
command: None, command: None,
working_dir: None, working_dir: None,
@ -69,100 +71,104 @@ impl Options {
let mut options = Options::default(); let mut options = Options::default();
let matches = App::new(crate_name!()) let matches =
.version(version.as_str()) App::new(crate_name!())
.author(crate_authors!("\n")) .version(version.as_str())
.about(crate_description!()) .author(crate_authors!("\n"))
.arg(Arg::with_name("ref-test").long("ref-test").help("Generates ref test")) .about(crate_description!())
.arg( .arg(Arg::with_name("ref-test").long("ref-test").help("Generates ref test"))
Arg::with_name("live-config-reload") .arg(
.long("live-config-reload") Arg::with_name("live-config-reload")
.help("Enable automatic config reloading"), .long("live-config-reload")
) .help("Enable automatic config reloading"),
.arg( )
Arg::with_name("no-live-config-reload") .arg(
.long("no-live-config-reload") Arg::with_name("no-live-config-reload")
.help("Disable automatic config reloading") .long("no-live-config-reload")
.conflicts_with("live-config-reload"), .help("Disable automatic config reloading")
) .conflicts_with("live-config-reload"),
.arg( )
Arg::with_name("print-events") .arg(
.long("print-events") Arg::with_name("print-events")
.help("Print all events to stdout"), .long("print-events")
) .help("Print all events to stdout"),
.arg( )
Arg::with_name("persistent-logging") .arg(
.long("persistent-logging") Arg::with_name("persistent-logging")
.help("Keep the log file after quitting Alacritty"), .long("persistent-logging")
) .help("Keep the log file after quitting Alacritty"),
.arg( )
Arg::with_name("dimensions") .arg(
.long("dimensions") Arg::with_name("dimensions")
.short("d") .long("dimensions")
.value_names(&["columns", "lines"]) .short("d")
.help( .value_names(&["columns", "lines"])
"Defines the window dimensions. Falls back to size specified by window \ .help(
manager if set to 0x0 [default: 0x0]", "Defines the window dimensions. Falls back to size specified by \
), window manager if set to 0x0 [default: 0x0]",
) ),
.arg( )
Arg::with_name("position") .arg(
.long("position") Arg::with_name("position")
.allow_hyphen_values(true) .long("position")
.value_names(&["x-pos", "y-pos"]) .allow_hyphen_values(true)
.help( .value_names(&["x-pos", "y-pos"])
"Defines the window position. Falls back to position specified by window \ .help(
manager if unset [default: unset]", "Defines the window position. Falls back to position specified by \
), window manager if unset [default: unset]",
) ),
.arg( )
Arg::with_name("title") .arg(
.long("title") Arg::with_name("title")
.short("t") .long("title")
.takes_value(true) .short("t")
.help(&format!("Defines the window title [default: {}]", DEFAULT_NAME)), .takes_value(true)
) .help(&format!("Defines the window title [default: {}]", DEFAULT_NAME)),
.arg( )
Arg::with_name("class") .arg(
.long("class") Arg::with_name("class").long("class").takes_value(true).help(&format!(
.takes_value(true) "Defines window class on Linux [default: {}]",
.help(&format!("Defines window class on Linux [default: {}]", DEFAULT_NAME)), DEFAULT_NAME
) )),
.arg( )
Arg::with_name("q") .arg(Arg::with_name("embed").long("embed").takes_value(true).help(
.short("q") "Defines the X11 window ID (as a decimal integer) to embed Alacritty within",
.multiple(true) ))
.conflicts_with("v") .arg(
.help("Reduces the level of verbosity (the min level is -qq)"), Arg::with_name("q")
) .short("q")
.arg( .multiple(true)
Arg::with_name("v") .conflicts_with("v")
.short("v") .help("Reduces the level of verbosity (the min level is -qq)"),
.multiple(true) )
.conflicts_with("q") .arg(
.help("Increases the level of verbosity (the max level is -vvv)"), Arg::with_name("v")
) .short("v")
.arg( .multiple(true)
Arg::with_name("working-directory") .conflicts_with("q")
.long("working-directory") .help("Increases the level of verbosity (the max level is -vvv)"),
.takes_value(true) )
.help("Start the shell in the specified working directory"), .arg(
) Arg::with_name("working-directory")
.arg(Arg::with_name("config-file").long("config-file").takes_value(true).help( .long("working-directory")
"Specify alternative configuration file [default: \ .takes_value(true)
$XDG_CONFIG_HOME/alacritty/alacritty.yml]", .help("Start the shell in the specified working directory"),
)) )
.arg( .arg(Arg::with_name("config-file").long("config-file").takes_value(true).help(
Arg::with_name("command") "Specify alternative configuration file [default: \
.long("command") $XDG_CONFIG_HOME/alacritty/alacritty.yml]",
.short("e") ))
.multiple(true) .arg(
.takes_value(true) Arg::with_name("command")
.min_values(1) .long("command")
.allow_hyphen_values(true) .short("e")
.help("Command and args to execute (must be last argument)"), .multiple(true)
) .takes_value(true)
.get_matches(); .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") { if matches.is_present("ref-test") {
options.ref_test = true; options.ref_test = true;
@ -200,6 +206,7 @@ impl Options {
options.class = matches.value_of("class").map(ToOwned::to_owned); options.class = matches.value_of("class").map(ToOwned::to_owned);
options.title = matches.value_of("title").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") { match matches.occurrences_of("q") {
0 => {}, 0 => {},
@ -250,6 +257,7 @@ impl Options {
config.window.dimensions = self.dimensions.unwrap_or(config.window.dimensions); config.window.dimensions = self.dimensions.unwrap_or(config.window.dimensions);
config.window.position = self.position.or(config.window.position); config.window.position = self.position.or(config.window.position);
config.window.title = self.title.or(config.window.title); 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 { if let Some(class) = self.class {
let parts: Vec<_> = class.split(',').collect(); let parts: Vec<_> = class.split(',').collect();

View file

@ -39,6 +39,10 @@ pub struct WindowConfig {
#[serde(deserialize_with = "from_string_or_deserialize")] #[serde(deserialize_with = "from_string_or_deserialize")]
pub class: Class, pub class: Class,
/// XEmbed parent
#[serde(skip)]
pub embed: Option<u64>,
/// GTK theme variant /// GTK theme variant
#[serde(deserialize_with = "option_explicit_none")] #[serde(deserialize_with = "option_explicit_none")]
pub gtk_theme_variant: Option<String>, pub gtk_theme_variant: Option<String>,

View file

@ -12,11 +12,10 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::convert::From; 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::ffi::c_void;
use std::fmt::Display; use std::fmt::Display;
use crate::gl;
use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalSize}; use glutin::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use glutin::os::macos::WindowExt; use glutin::os::macos::WindowExt;
@ -24,14 +23,19 @@ use glutin::os::macos::WindowExt;
use glutin::os::unix::{EventsLoopExt, WindowExt}; use glutin::os::unix::{EventsLoopExt, WindowExt};
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
use glutin::Icon; use glutin::Icon;
#[cfg(not(any(target_os = "macos", windows)))]
use glutin::Window as GlutinWindow;
use glutin::{ use glutin::{
self, ContextBuilder, ControlFlow, Event, EventsLoop, MouseCursor, PossiblyCurrent, self, ContextBuilder, ControlFlow, Event, EventsLoop, MouseCursor, PossiblyCurrent,
WindowBuilder, WindowBuilder,
}; };
#[cfg(not(target_os = "macos"))] #[cfg(not(target_os = "macos"))]
use image::ImageFormat; 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::config::{Config, Decorations, StartupMode, WindowConfig};
use crate::gl;
// It's required to be in this directory due to the `windows.rc` file // It's required to be in this directory due to the `windows.rc` file
#[cfg(not(target_os = "macos"))] #[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. // 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 _); 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 { let window = Window {
event_loop, event_loop,
current_mouse_cursor: MouseCursor::Default, 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 { impl Proxy {
/// Wakes up the event loop of the window /// Wakes up the event loop of the window
/// ///

View file

@ -57,6 +57,9 @@ Defines the window position. Falls back to position specified by window manager
\fB\-t\fR, \fB\-\-title\fR <title> \fB\-t\fR, \fB\-\-title\fR <title>
Defines the window title [default: Alacritty] Defines the window title [default: Alacritty]
.TP .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> \fB\-\-working\-directory\fR <working\-directory>
Start the shell in the specified working directory Start the shell in the specified working directory
.SH "SEE ALSO" .SH "SEE ALSO"

View file

@ -14,6 +14,7 @@ _arguments \
'(-q)'{-v,-vv,-vvv}"[increase the level of verbosity (max is -vvv)]" \ '(-q)'{-v,-vv,-vvv}"[increase the level of verbosity (max is -vvv)]" \
"$ign(-)"{-V,--version}"[print version information]" \ "$ign(-)"{-V,--version}"[print version information]" \
"--class=[define the window class]:class" \ "--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" \ "(-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" \ "--config-file=[specify an alternative config file]:file:_files" \
"(-d --dimensions)"{-d,--dimensions}"[specify window dimensions]:columns: :lines" \ "(-d --dimensions)"{-d,--dimensions}"[specify window dimensions]:columns: :lines" \

View file

@ -11,7 +11,7 @@ _alacritty()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
prevprev="${COMP_WORDS[COMP_CWORD-2]}" 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 # If `--command` or `-e` is used, stop completing
for i in "${!COMP_WORDS[@]}"; do for i in "${!COMP_WORDS[@]}"; do

View file

@ -29,6 +29,9 @@ complete -c alacritty \
complete -c alacritty \ complete -c alacritty \
-l "class" \ -l "class" \
-d "Defines the window 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 \ complete -c alacritty \
-x \ -x \
-a '(__fish_complete_directories (commandline -ct))' \ -a '(__fish_complete_directories (commandline -ct))' \