diff --git a/espanso/src/cli/launcher/edition_check.rs b/espanso/src/cli/launcher/edition_check.rs new file mode 100644 index 0000000..1638c52 --- /dev/null +++ b/espanso/src/cli/launcher/edition_check.rs @@ -0,0 +1,59 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2021 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +use crate::warn_eprintln; +use espanso_modulo::wizard::DetectedOS; + +pub fn is_wrong_edition() -> (bool, DetectedOS) { + if !cfg!(target_os = "linux") { + return (false, DetectedOS::Unknown); + } + + match get_session_type().as_deref() { + Some("x11") if cfg!(feature = "wayland") => return (true, DetectedOS::X11), + Some("wayland") if !cfg!(feature = "wayland") => return (true, DetectedOS::Wayland), + None => { + warn_eprintln!("could not automatically determine the session type (X11/Wayland), so make sure you have the correct espanso version!"); + } + _ => {} + } + + (false, DetectedOS::Unknown) +} + +fn get_session_type() -> Option { + let output = std::process::Command::new("sh") + .arg("-c") + .arg("loginctl show-session $(loginctl | grep $(whoami) | awk '{print $1}') -p Type") + .output() + .ok()?; + + if !output.status.success() { + return None; + } + + let raw_session_type = String::from_utf8_lossy(&output.stdout); + let raw_session_type = raw_session_type.trim(); + if !raw_session_type.contains("Type=") { + return None; + } + + let session_type: Option<&str> = raw_session_type.split('=').into_iter().last(); + session_type.map(String::from) +} diff --git a/espanso/src/cli/launcher/mod.rs b/espanso/src/cli/launcher/mod.rs index 11720da..cedb366 100644 --- a/espanso/src/cli/launcher/mod.rs +++ b/espanso/src/cli/launcher/mod.rs @@ -26,6 +26,7 @@ use super::{CliModule, CliModuleArgs}; mod accessibility; mod daemon; +mod edition_check; mod util; // TODO: test also with modulo feature disabled @@ -44,7 +45,7 @@ pub fn new() -> CliModule { #[cfg(feature = "modulo")] fn launcher_main(args: CliModuleArgs) -> i32 { - use espanso_modulo::wizard::{MigrationResult, WizardHandlers, WizardOptions}; + use espanso_modulo::wizard::{DetectedOS, MigrationResult, WizardHandlers, WizardOptions}; let paths = args.paths.expect("missing paths in launcher main"); // TODO: should we create a non-gui wizard? We can also use it for the non-modulo versions of espanso @@ -73,6 +74,8 @@ fn launcher_main(args: CliModuleArgs) -> i32 { let runtime_dir_clone = paths.runtime.clone(); let is_legacy_version_running_handler = Box::new(move || util::is_legacy_version_running(&runtime_dir_clone)); + + let (is_wrong_edition_page_enabled, wrong_edition_detected_os) = edition_check::is_wrong_edition(); let is_migrate_page_enabled = espanso_config::is_legacy_config(&paths.config); let paths_clone = paths.clone(); @@ -132,11 +135,12 @@ fn launcher_main(args: CliModuleArgs) -> i32 { || is_add_path_page_enabled || is_accessibility_page_enabled { - let successful = espanso_modulo::wizard::show(WizardOptions { + espanso_modulo::wizard::show(WizardOptions { version: crate::VERSION.to_string(), is_welcome_page_enabled, is_move_bundle_page_enabled, is_legacy_version_page_enabled, + is_wrong_edition_page_enabled, is_migrate_page_enabled, is_add_path_page_enabled, is_accessibility_page_enabled, @@ -152,6 +156,7 @@ fn launcher_main(args: CliModuleArgs) -> i32 { accessibility_image_2_path: icon_paths .accessibility_image_2 .map(|path| path.to_string_lossy().to_string()), + detected_os: wrong_edition_detected_os, handlers: WizardHandlers { is_legacy_version_running: Some(is_legacy_version_running_handler), backup_and_migrate: Some(backup_and_migrate_handler), @@ -160,9 +165,7 @@ fn launcher_main(args: CliModuleArgs) -> i32 { is_accessibility_enabled: Some(is_accessibility_enabled_handler), on_completed: Some(on_completed_handler), }, - }); - - successful + }) } else { true };