main.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #![feature(deadline_api)]
  19. #![feature(str_split_whitespace_remainder)]
  20. #![feature(duration_millis_float)]
  21. #![feature(stmt_expr_attributes)]
  22. // Use these to incrementally fix warnings with cargo fix
  23. //#![allow(warnings, unused)]
  24. //#![deny(unused_imports)]
  25. use async_lock::Mutex as AsyncMutex;
  26. use std::sync::{mpsc, Arc};
  27. #[macro_use]
  28. extern crate log;
  29. #[allow(unused_imports)]
  30. use log::LevelFilter;
  31. mod app;
  32. mod darkirc;
  33. mod error;
  34. mod expr;
  35. mod gfx;
  36. mod mesh;
  37. mod net;
  38. //mod plugin;
  39. mod prop;
  40. mod pubsub;
  41. //mod py;
  42. mod ringbuf;
  43. mod scene;
  44. mod text;
  45. mod ui;
  46. mod util;
  47. use crate::{darkirc::DarkIrcBackend, net::ZeroMQAdapter, scene::SceneGraph, text::TextShaper};
  48. pub type ExecutorPtr = Arc<smol::Executor<'static>>;
  49. fn panic_hook(panic_info: &std::panic::PanicInfo) {
  50. error!("panic occurred: {panic_info}");
  51. //error!("panic: {}", std::backtrace::Backtrace::force_capture().to_string());
  52. std::process::exit(1);
  53. }
  54. fn main() {
  55. // Exit the application on panic right away
  56. std::panic::set_hook(Box::new(panic_hook));
  57. #[cfg(target_os = "android")]
  58. {
  59. android_logger::init_once(
  60. android_logger::Config::default().with_max_level(LevelFilter::Debug).with_tag("darkfi"),
  61. );
  62. let paths = std::fs::read_dir("/data/data/darkfi.darkwallet/").unwrap();
  63. for path in paths {
  64. debug!("{}", path.unwrap().path().display())
  65. }
  66. }
  67. #[cfg(target_os = "linux")]
  68. {
  69. // For ANSI colors in the terminal
  70. colored::control::set_override(true);
  71. let term_logger = simplelog::TermLogger::new(
  72. simplelog::LevelFilter::Debug,
  73. simplelog::Config::default(),
  74. simplelog::TerminalMode::Mixed,
  75. simplelog::ColorChoice::Auto,
  76. );
  77. simplelog::CombinedLogger::init(vec![term_logger]).expect("logger");
  78. }
  79. let ex = Arc::new(smol::Executor::new());
  80. let sg = Arc::new(AsyncMutex::new(SceneGraph::new()));
  81. let async_runtime = app::AsyncRuntime::new(ex.clone());
  82. async_runtime.start();
  83. let sg2 = sg.clone();
  84. let ex2 = ex.clone();
  85. let zmq_task = ex.spawn(async {
  86. let zmq_rpc = ZeroMQAdapter::new(sg2, ex2).await;
  87. zmq_rpc.run().await;
  88. });
  89. async_runtime.push_task(zmq_task);
  90. let (method_req, method_rep) = mpsc::channel();
  91. // The UI actually needs to be running for this to reply back.
  92. // Otherwise calls will just hang.
  93. let render_api = gfx::RenderApi::new(method_req);
  94. let event_pub = gfx::GraphicsEventPublisher::new();
  95. let text_shaper = TextShaper::new();
  96. let darkirc_backend = DarkIrcBackend::new();
  97. let app = app::App::new(
  98. sg.clone(),
  99. ex.clone(),
  100. render_api.clone(),
  101. event_pub.clone(),
  102. text_shaper,
  103. darkirc_backend,
  104. );
  105. let app_task = ex.spawn(app.clone().start());
  106. async_runtime.push_task(app_task);
  107. // Nice to see which events exist
  108. let ev_sub = event_pub.subscribe_key_down();
  109. let ev_relay_task = ex.spawn(async move {
  110. debug!(target: "main", "event relayer started");
  111. loop {
  112. let Ok((key, mods, repeat)) = ev_sub.receive().await else {
  113. debug!(target: "main", "Event relayer closed");
  114. break
  115. };
  116. // Ignore keys which get stuck repeating when switching windows
  117. match key {
  118. miniquad::KeyCode::LeftShift | miniquad::KeyCode::LeftSuper => continue,
  119. _ => {}
  120. }
  121. if !repeat {
  122. debug!(target: "main", "key_down event: {:?} {:?} {}", key, mods, repeat);
  123. }
  124. }
  125. });
  126. async_runtime.push_task(ev_relay_task);
  127. let ev_sub = event_pub.subscribe_key_up();
  128. let ev_relay_task = ex.spawn(async move {
  129. debug!(target: "main", "event relayer started");
  130. loop {
  131. let Ok((key, mods)) = ev_sub.receive().await else {
  132. debug!(target: "main", "Event relayer closed");
  133. break
  134. };
  135. // Ignore keys which get stuck repeating when switching windows
  136. match key {
  137. miniquad::KeyCode::LeftShift | miniquad::KeyCode::LeftSuper => continue,
  138. _ => {}
  139. }
  140. debug!(target: "main", "key_up event: {:?} {:?}", key, mods);
  141. }
  142. });
  143. async_runtime.push_task(ev_relay_task);
  144. let ev_sub = event_pub.subscribe_char();
  145. let ev_relay_task = ex.spawn(async move {
  146. debug!(target: "main", "event relayer started");
  147. loop {
  148. let Ok((key, mods, repeat)) = ev_sub.receive().await else {
  149. debug!(target: "main", "Event relayer closed");
  150. break
  151. };
  152. debug!(target: "main", "char event: {:?} {:?} {}", key, mods, repeat);
  153. }
  154. });
  155. async_runtime.push_task(ev_relay_task);
  156. //let stage = gfx::Stage::new(method_rep, event_pub);
  157. gfx::run_gui(app, async_runtime, method_rep, event_pub);
  158. debug!(target: "main", "Started GFX backend");
  159. }
  160. /*
  161. use rustpython_vm::{self as pyvm, convert::ToPyObject};
  162. fn main() {
  163. let module = pyvm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
  164. let source = r#"
  165. def foo():
  166. open("hihi", "w")
  167. return 110
  168. #max(1 + lw/3, 4*10) + foo(2, True)
  169. "#;
  170. //let code_obj = vm
  171. // .compile(source, pyvm::compiler::Mode::Exec, "<embedded>".to_owned())
  172. // .map_err(|err| vm.new_syntax_error(&err, Some(source))).unwrap();
  173. //code_obj
  174. pyvm::import::import_source(vm, "lain", source).unwrap()
  175. });
  176. fn foo(x: u32, y: bool) -> u32 {
  177. if y {
  178. 2 * x
  179. } else {
  180. x
  181. }
  182. }
  183. let res = pyvm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
  184. let globals = vm.ctx.new_dict();
  185. globals.set_item("lw", vm.ctx.new_int(110).to_pyobject(vm), vm).unwrap();
  186. globals.set_item("lh", vm.ctx.new_int(4).to_pyobject(vm), vm).unwrap();
  187. globals.set_item("foo", vm.new_function("foo", foo).into(), vm).unwrap();
  188. let scope = pyvm::scope::Scope::new(None, globals);
  189. let foo_fn = module.get_attr("foo", vm).unwrap();
  190. foo_fn.call((), vm).unwrap()
  191. //vm.run_code_obj(code_obj, scope).unwrap()
  192. });
  193. println!("{:?}", res);
  194. }
  195. */