From 88f92d51b3863ed014b3443fac9b28300d232838 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Tue, 11 Jul 2023 16:23:10 +0300 Subject: [PATCH] build: fix build for desktop --- .cargo/config.toml | 2 - .gitignore | 2 +- Cargo.toml | 10 +-- src/grim.rs | 116 -------------------------------- src/gui/platform/desktop/mod.rs | 30 ++++++++- src/gui/platform/mod.rs | 3 +- src/lib.rs | 79 ++++++++++++++++++++-- src/main.rs | 36 ++++++++++ src/node/node.rs | 9 ++- 9 files changed, 149 insertions(+), 138 deletions(-) delete mode 100644 .cargo/config.toml delete mode 100644 src/grim.rs create mode 100644 src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 351acab..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -target = "aarch64-linux-android" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3f97f68..6f6d2a2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,5 @@ local.properties *.so # Added by cargo - /target +/.cargo/ diff --git a/Cargo.toml b/Cargo.toml index 69a219b..220f040 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,10 @@ version = "0.1.0" edition = "2021" build = "src/build/build.rs" +[lib] +name="grim" +crate_type=["cdylib", "rlib"] + [features] #static_ssl = ['openssl/vendored'] @@ -67,8 +71,4 @@ winit = { version = "0.28" } [target.'cfg(target_os = "android")'.dependencies] android_logger = "0.13.1" jni = "0.21.1" -winit = { version = "0.28", features = [ "android-game-activity" ] } - -[lib] -name="grim" -crate_type=["cdylib"] \ No newline at end of file +winit = { version = "0.28", features = [ "android-game-activity" ] } \ No newline at end of file diff --git a/src/grim.rs b/src/grim.rs deleted file mode 100644 index 6e73e1a..0000000 --- a/src/grim.rs +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright 2023 The Grim Developers -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use eframe::{AppCreator, NativeOptions, Renderer, Theme}; -#[cfg(target_os = "android")] -use winit::platform::android::activity::AndroidApp; - -use crate::gui::{App, PlatformApp}; -use crate::node::Node; -use crate::Settings; - -#[allow(dead_code)] -#[cfg(target_os = "android")] -#[no_mangle] -fn android_main(app: AndroidApp) { - #[cfg(debug_assertions)] - { - std::env::set_var("RUST_BACKTRACE", "full"); - let log_config = android_logger::Config::default() - .with_max_level(log::LevelFilter::Info) - .with_tag("grim"); - android_logger::init_once(log_config); - } - - use crate::gui::platform::Android; - let platform = Android::new(app.clone()); - - use winit::platform::android::EventLoopBuilderExtAndroid; - let mut options = NativeOptions::default(); - options.event_loop_builder = Some(Box::new(move |builder| { - builder.with_android_app(app); - })); - - start(options, app_creator(PlatformApp::new(platform))); -} - -#[allow(dead_code)] -#[cfg(not(target_os = "android"))] -fn main() { - #[cfg(debug_assertions)] - env_logger::builder() - .filter_level(Info) - .parse_default_env() - .init(); - - use crate::gui::platform::Desktop; - let options = NativeOptions::default(); - start(options, app_creator(Desktop::default())); -} - -fn app_creator(app: PlatformApp) -> AppCreator where PlatformApp: eframe::App { - Box::new(|cc| { - App::setup_visuals(&cc.egui_ctx); - App::setup_fonts(&cc.egui_ctx); - //TODO: Setup storage - Box::new(app) - }) -} - -fn start(mut options: NativeOptions, app_creator: AppCreator) { - options.default_theme = Theme::Light; - options.renderer = Renderer::Wgpu; - - setup_i18n(); - - if Settings::app_config_to_read().auto_start_node { - Node::start(); - } - - let _ = eframe::run_native("Grim", options, app_creator); -} - -fn setup_i18n() { - const DEFAULT_LOCALE: &str = "en"; - let locale = sys_locale::get_locale().unwrap_or(String::from(DEFAULT_LOCALE)); - let locale_str = if locale.contains("-") { - locale.split("-").next().unwrap_or(DEFAULT_LOCALE) - } else { - DEFAULT_LOCALE - }; - if crate::_rust_i18n_available_locales().contains(&locale_str) { - rust_i18n::set_locale(locale_str); - } -} - -mod built_info { - include!(concat!(env!("OUT_DIR"), "/built.rs")); -} - -pub fn info_strings() -> (String, String) { - ( - format!( - "This is Grim version {}{}, built for {} by {}.", - built_info::PKG_VERSION, - built_info::GIT_VERSION.map_or_else(|| "".to_owned(), |v| format!(" (git {})", v)), - built_info::TARGET, - built_info::RUSTC_VERSION, - ), - format!( - "Built with profile \"{}\", features \"{}\".", - built_info::PROFILE, - built_info::FEATURES_STR, - ), - ) -} \ No newline at end of file diff --git a/src/gui/platform/desktop/mod.rs b/src/gui/platform/desktop/mod.rs index f816e75..b3dc4e7 100644 --- a/src/gui/platform/desktop/mod.rs +++ b/src/gui/platform/desktop/mod.rs @@ -12,13 +12,37 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::gui::PlatformApp; +use crate::gui::{App, PlatformApp}; +use crate::gui::platform::PlatformCallbacks; #[derive(Default)] pub struct Desktop; +impl PlatformCallbacks for Desktop { + fn show_keyboard(&self) {} + + fn hide_keyboard(&self) {} + + fn copy_string_to_buffer(&self, data: String) {} + + fn get_string_from_buffer(&self) -> String { + "".to_string() + } + + fn exit(&self) {} +} + impl PlatformApp { - pub fn new(cc: &eframe::CreationContext<'_>) -> Self { - Self::default() + pub fn new(platform: Desktop) -> Self { + Self { + app: App::default(), + platform, + } + } +} + +impl eframe::App for PlatformApp { + fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { + self.app.ui(ctx, frame, &self.platform); } } diff --git a/src/gui/platform/mod.rs b/src/gui/platform/mod.rs index b76133f..f5d22df 100644 --- a/src/gui/platform/mod.rs +++ b/src/gui/platform/mod.rs @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + pub use self::platform::*; #[cfg(target_os = "android")] @@ -18,7 +19,7 @@ pub use self::platform::*; pub mod platform; #[cfg(not(target_os = "android"))] #[path = "desktop/mod.rs"] -pub mod app; +pub mod platform; pub trait PlatformCallbacks { fn show_keyboard(&self); diff --git a/src/lib.rs b/src/lib.rs index a46b71f..89a298c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,13 +14,82 @@ #[macro_use] extern crate rust_i18n; + +use eframe::{AppCreator, Renderer, Theme}; +#[cfg(target_os = "android")] +use winit::platform::android::activity::AndroidApp; + +pub use settings::{AppConfig, Settings}; + +use crate::gui::{App, PlatformApp}; +use crate::node::Node; + i18n!("locales"); -mod node; +pub mod node; mod wallet; -mod gui; - -pub mod grim; +pub mod gui; mod settings; -pub use settings::{Settings, AppConfig}; \ No newline at end of file + +#[allow(dead_code)] +#[cfg(target_os = "android")] +#[no_mangle] +fn android_main(app: AndroidApp) { + #[cfg(debug_assertions)] + { + std::env::set_var("RUST_BACKTRACE", "full"); + let log_config = android_logger::Config::default() + .with_max_level(log::LevelFilter::Info) + .with_tag("grim"); + android_logger::init_once(log_config); + } + + use gui::platform::Android; + use gui::PlatformApp; + + let platform = Android::new(app.clone()); + + use winit::platform::android::EventLoopBuilderExtAndroid; + let mut options = eframe::NativeOptions::default(); + options.event_loop_builder = Some(Box::new(move |builder| { + builder.with_android_app(app); + })); + + start(options, app_creator(PlatformApp::new(platform))); +} + +pub fn app_creator(app: PlatformApp) -> AppCreator where PlatformApp: eframe::App { + Box::new(|cc| { + App::setup_visuals(&cc.egui_ctx); + App::setup_fonts(&cc.egui_ctx); + //TODO: Setup storage + Box::new(app) + }) +} + +pub fn start(mut options: eframe::NativeOptions, app_creator: AppCreator) { + options.default_theme = Theme::Light; + options.renderer = Renderer::Wgpu; + + setup_i18n(); + + if Settings::app_config_to_read().auto_start_node { + Node::start(); + } + + let _ = eframe::run_native("Grim", options, app_creator); +} + +fn setup_i18n() { + const DEFAULT_LOCALE: &str = "en"; + let locale = sys_locale::get_locale().unwrap_or(String::from(DEFAULT_LOCALE)); + let locale_str = if locale.contains("-") { + locale.split("-").next().unwrap_or(DEFAULT_LOCALE) + } else { + DEFAULT_LOCALE + }; + if _rust_i18n_available_locales().contains(&locale_str) { + rust_i18n::set_locale(locale_str); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f8f996f --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +// Copyright 2023 The Grim Developers +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub fn main() { + #[allow(dead_code)] + #[cfg(not(target_os = "android"))] + real_main(); +} + +#[allow(dead_code)] +#[cfg(not(target_os = "android"))] +fn real_main() { + #[cfg(debug_assertions)] + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .parse_default_env() + .init(); + + use grim::gui::platform::Desktop; + use grim::gui::PlatformApp; + + let platform = Desktop::default(); + let options = eframe::NativeOptions::default(); + grim::start(options, grim::app_creator(PlatformApp::new(platform))); +} \ No newline at end of file diff --git a/src/node/node.rs b/src/node/node.rs index 47800d3..9387ec4 100644 --- a/src/node/node.rs +++ b/src/node/node.rs @@ -24,7 +24,6 @@ use grin_core::global; use grin_core::global::ChainTypes; use grin_servers::{Server, ServerStats, StratumServerConfig, StratumStats}; use grin_servers::common::types::Error; -use jni::sys::{jboolean, jstring}; use lazy_static::lazy_static; use crate::node::NodeConfig; use crate::node::stratum::{StratumStopState, StratumServer}; @@ -612,7 +611,7 @@ pub extern "C" fn Java_mw_gri_android_BackgroundService_getSyncStatusText( _env: jni::JNIEnv, _class: jni::objects::JObject, _activity: jni::objects::JObject, -) -> jstring { +) -> jni::sys::jstring { let status_text = Node::get_sync_status_text(); let j_text = _env.new_string(status_text); return j_text.unwrap().into_raw(); @@ -627,7 +626,7 @@ pub extern "C" fn Java_mw_gri_android_BackgroundService_getSyncTitle( _env: jni::JNIEnv, _class: jni::objects::JObject, _activity: jni::objects::JObject, -) -> jstring { +) -> jni::sys::jstring { let j_text = _env.new_string(t!("network.node")); return j_text.unwrap().into_raw(); } @@ -641,7 +640,7 @@ pub extern "C" fn Java_mw_gri_android_BackgroundService_exitAppAfterNodeStop( _env: jni::JNIEnv, _class: jni::objects::JObject, _activity: jni::objects::JObject, -) -> jboolean { +) -> jni::sys::jboolean { let exit_needed = !Node::is_running() && NODE_STATE.exit_after_stop.load(Ordering::Relaxed); - return exit_needed as jboolean; + return exit_needed as jni::sys::jboolean; } \ No newline at end of file