Skip to content

Commit

Permalink
Unify splash and entrypoint [CPP-769] (#584)
Browse files Browse the repository at this point in the history
Jira: https://swift-nav.atlassian.net/browse/CPP-769

Test release here: https://github.com/swift-nav/swift-toolbox/releases/tag/v4.0.7-unify-splash

The splash screen launching as a separate process on macOS causes some strange interactions with the launcher bar in that basically you can't pin the app (it looks like this is because it loses track of what the main window of the app is).

Instead of doing this outside of Qt, do it inside Qt... this also vastly simplifies the implementation of the splash screen (which is probably what we should have done from the start, but ~this was my fault for encouraging the over-engineering here~ looks like this doesn't really work on Linux... since we already have the code for the hacky splash screen, we'll just use it on Linux).
  • Loading branch information
Jason Mobarak authored Jun 4, 2022
1 parent fcf7a22 commit a74e792
Show file tree
Hide file tree
Showing 23 changed files with 398 additions and 294 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ jobs:
path: ${{ env.PIP_CACHE_DIR }}
key: ${{ runner.os }}-pyproject-toml-${{ secrets.CACHE_VERSION }}-${{ hashFiles('pyproject.toml') }}

- name: Install ImageMagick
shell: bash
run: |
cargo make install-imagemagick
- name: Set up python builder
shell: bash
run: |
Expand Down Expand Up @@ -160,6 +165,11 @@ jobs:
path: ${{ env.PIP_CACHE_DIR }}
key: ${{ runner.os }}-pyproject-toml-${{ secrets.CACHE_VERSION }}-${{ hashFiles('pyproject.toml') }}

- name: Install ImageMagick
shell: bash
run: |
cargo make install-imagemagick
- name: Set up python builder
run: |
cargo make setup-builder
Expand Down
20 changes: 9 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 40 additions & 20 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,11 @@ exec --fail-on-error ${python} -m py2many --rust=1 /tmp/piksi_tools_constants.py
'''

[tasks.generate-resources]
command = "${PYSIDE2_RCC}"
args = [
"resources/console_resources.qrc",
"-o",
"swiftnav_console/console_resources.py",
"-g",
"python",
]
dependencies = ["build-splash-version"]
script_runner = "@duckscript"
script = '''
exec --fail-on-error "${PYSIDE2_RCC}" "resources/console_resources.qrc" "-o" "swiftnav_console/console_resources.py" "-g" "python"
'''

[tasks.check-capnp]
script_runner = "@shell"
Expand Down Expand Up @@ -357,7 +354,6 @@ script = "sudo apt-get install gsfonts imagemagick"
script = "brew install imagemagick"

[tasks.build-splash-version]
private = true
script_runner = "@duckscript"
script = '''
output = exec --fail-on-error git describe --always --tags --dirty
Expand All @@ -370,30 +366,39 @@ else
end
'''

[tasks.build-splash-bin]
private = true
[tasks.build-windowpos-bin]
dependencies = ["build-splash-version"]
command = "cargo"
args = [
"build",
"--release",
"-vv",
"--features",
"splash",
"winit",
"--bin",
"swift-console-splash",
"windowpos",
]

[tasks.build-console-bin]
private = true
dependencies = ["build-splash-bin"]
command = "cargo"
args = [
"build",
"--release",
"-vv",
"--features=entrypoint",
"--bin",
"${APP_NAME}",
]

[tasks.build-console-bin.linux]
dependencies = ["build-windowpos-bin"]
command = "cargo"
args = [
"build",
"--release",
"-vv",
"--features",
"entrypoint",
"entrypoint,splash",
"--bin",
"${APP_NAME}",
]
Expand All @@ -412,19 +417,21 @@ script_runner = "@duckscript"
script = '''
app_name = get_env APP_NAME
cp target/release/${app_name} py39-dist/${app_name}
cp target/release/swift-console-splash py39-dist/swift-console-splash
os = os_family
if eq ${os} mac
exec --fail-on-error install_name_tool -change /install/lib/libpython3.9.dylib @executable_path/lib/libpython3.9.dylib py39-dist/${app_name}
elseif eq ${os} linux
cp target/release/windowpos py39-dist/windowpos
end
'''

[tasks.build-dist-install-console.windows]
script = '''
cp target/release/swift-console-splash.exe py39-dist/swift-console-splash.exe
cp target/release/${APP_NAME}.exe py39-dist/${APP_NAME}.exe
cp target/release/${APP_NAME}.d py39-dist/${APP_NAME}.d
cp target/release/swift_console.pdb py39-dist/swift_console.pdb
if is_path_exists target/release/swift_console.pdb
cp target/release/swift_console.pdb py39-dist/swift_console.pdb
end
cp target/x86_64-pc-windows-msvc/release/console_backend.pdb py39-dist/Lib/site-packages/console_backend/console_backend.pdb
'''

Expand Down Expand Up @@ -688,7 +695,6 @@ mkdir ${contents_dir}
mkdir ${contents_mac_os}
exec --fail-on-error cp -r py39-dist "${contents_resources_dir}"
exec --fail-on-error mv ./${contents_resources_dir}/${app_original_name} "./${contents_mac_os}/${app_file_prefix}"
exec --fail-on-error mv ./${contents_resources_dir}/swift-console-splash "./${contents_mac_os}/swift-console-splash"
exec --fail-on-error ln -s "../Resources/lib" ${contents_mac_os}/lib
exec --fail-on-error ln -s "../Resources/.frozen" ${contents_mac_os}/.frozen
exec --fail-on-error ln -s "../Resources/resources" ${contents_mac_os}/resources
Expand Down Expand Up @@ -885,6 +891,20 @@ dependencies = ["store-version", "copy-capnp"]
command = "cargo"
args = ["check"]

[tasks.watch-entrypoint]
dependencies = ["store-version", "copy-capnp"]
command = "cargo"
args = [
"watch",
"--exec",
"check --bin swift-console --features=entrypoint,splash",
]

[tasks.watch-windowpos]
dependencies = ["store-version", "copy-capnp"]
command = "cargo"
args = ["watch", "--exec", "check --bin windowpos --features=winit"]

[tasks.python-type-check]
dependencies = ["set-python-files"]
command = "${PYTHON}"
Expand Down
2 changes: 1 addition & 1 deletion console_backend/src/common_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,6 @@ pub enum ApplicationMetadata {
ORGANIZATION_NAME,
#[strum(serialize = "swiftnav.com")]
ORGANIZATION_DOMAIN,
#[strum(serialize = "swift-toolbox")]
#[strum(serialize = "Swift Console")]
APPLICATION_NAME,
}
29 changes: 14 additions & 15 deletions entrypoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,31 @@ required-features = ["entrypoint"]

[[bin]]
bench = false
name = "swift-console-splash"
path = "src/splash.rs"
required-features = ["splash"]
name = "windowpos"
path = "src/windowpos.rs"
required-features = ["winit"]

[dependencies.lazy_static]
optional = true
version = "1.4.0"

[dependencies.image]
version = "0.24.2"

[dependencies.winit]
optional = true
version = "0.26.1"

[dependencies.image]
optional = true
version = "0.24.2"

[dependencies.minifb]
optional = true
default-features = false
features = ["x11"]
version = "0.23"
optional = true

[dependencies.pyo3]
default-features = false
features = ["auto-initialize"]
optional = true
version = "0.16"

[features]
entrypoint = ["pyo3", "windows"]
splash = ["minifb", "image", "winit", "windows", "lazy_static"]
optional = true

[target]
[target."cfg(target_os = \"windows\")"]
Expand All @@ -58,5 +52,10 @@ version = "0.1"
[target."cfg(target_os = \"windows\")".dependencies]
[target."cfg(target_os = \"windows\")".dependencies.windows]
features = ["Win32_System_Console", "Win32_Foundation"]
optional = true
version = ">=0.24"
optional = true

[features]
default = []
entrypoint = ["pyo3", "windows", "minifb"]
splash = []
2 changes: 1 addition & 1 deletion entrypoint/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(target_os = "windows")]
#[cfg(target_os = "windows")]
use winres::WindowsResource;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

Expand Down
23 changes: 23 additions & 0 deletions entrypoint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
use std::io::Cursor;

use lazy_static::lazy_static;

#[cfg(feature = "splash")]
pub mod splash;

pub type Error = Box<dyn std::error::Error>;
pub type Result<T> = std::result::Result<T, Error>;

lazy_static! {
pub static ref SPLASH_IMAGE: image::DynamicImage = {
let splash_image_buf = include_bytes!(concat!("../../", env!("CONSOLE_SPLASH_IMAGE")));
image::io::Reader::with_format(
std::io::BufReader::new(Cursor::new(splash_image_buf)),
image::ImageFormat::Jpeg,
)
.decode()
.expect("could not decode splash image")
};
}

#[cfg(feature = "entrypoint")]
pub fn attach_console() {
#[cfg(target_os = "windows")]
{
Expand Down
36 changes: 14 additions & 22 deletions entrypoint/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#![cfg_attr(target_os = "windows", windows_subsystem = "windows")]

use pyo3::prelude::*;
use pyo3::types::PyTuple;

use entrypoint::attach_console;

const SWIFT_CONSOLE_PID: &str = "SWIFT_CONSOLE_PID";
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

fn handle_wayland() {
#[cfg(target_os = "linux")]
Expand All @@ -25,37 +23,31 @@ fn handle_wayland() {
}
}

fn handle_splash() {
#[cfg(feature = "splash")]
{
std::env::set_var(
"SWIFTNAV_CONSOLE_SPLASH",
entrypoint::splash::marker_filepath(),
);
entrypoint::splash::spawn();
}
}

fn main() -> Result<()> {
attach_console();
handle_wayland();
let current_exe = std::env::current_exe()?;
let parent = current_exe.parent().ok_or("no parent directory")?;
let args: Vec<_> = std::env::args().collect();
let helper_found = args
.iter()
.any(|arg| matches!(arg.as_ref(), "--help" | "-h" | "--version" | "-V"));
if !helper_found {
let mut command = std::process::Command::new(parent.join("swift-console-splash"));
match command.spawn() {
Ok(child) => {
let pid = child.id();
std::env::set_var(SWIFT_CONSOLE_PID, format!("{pid}"));
}
Err(e) => {
eprintln!("Starting splash screen failed: {e}");
}
};
}

std::env::set_var("SWIFTNAV_CONSOLE_FROZEN", parent);

std::env::set_var("PYTHONHOME", parent);
std::env::set_var("PYTHONDONTWRITEBYTECODE", "1");
handle_splash();
let exit_code = Python::with_gil(|py| {
let args = PyTuple::new(py, args);
let snav = py.import("swiftnav_console.main")?;
snav.call_method("main", (args,), None)?.extract::<i32>()
})?;

std::process::exit(exit_code);
}
Loading

0 comments on commit a74e792

Please sign in to comment.