main.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // Use these to incrementally fix warnings with cargo fix
  22. //#![allow(warnings, unused)]
  23. //#![deny(unused_imports)]
  24. use async_lock::Mutex;
  25. use std::sync::{mpsc, Arc};
  26. #[macro_use]
  27. extern crate log;
  28. #[allow(unused_imports)]
  29. use log::LevelFilter;
  30. mod app;
  31. //mod chatapp;
  32. //mod chatview;
  33. //mod editbox;
  34. mod error;
  35. mod expr;
  36. //mod gfx;
  37. mod gfx2;
  38. mod keysym;
  39. mod mesh;
  40. mod net;
  41. //mod plugin;
  42. mod prop;
  43. mod pubsub;
  44. //mod py;
  45. //mod res;
  46. mod scene;
  47. mod shader;
  48. mod text2;
  49. mod ui;
  50. mod util;
  51. use crate::{net::ZeroMQAdapter, scene::SceneGraph, text2::TextShaper};
  52. fn panic_hook(panic_info: &std::panic::PanicInfo) {
  53. error!("panic occurred: {panic_info}");
  54. //error!("panic: {}", std::backtrace::Backtrace::force_capture().to_string());
  55. std::process::exit(1);
  56. }
  57. fn main() {
  58. // Exit the application on panic right away
  59. std::panic::set_hook(Box::new(panic_hook));
  60. #[cfg(target_os = "android")]
  61. {
  62. android_logger::init_once(
  63. android_logger::Config::default().with_max_level(LevelFilter::Debug).with_tag("darkfi"),
  64. );
  65. let paths = std::fs::read_dir("/data/data/darkfi.darkwallet/").unwrap();
  66. for path in paths {
  67. debug!("{}", path.unwrap().path().display())
  68. }
  69. }
  70. #[cfg(target_os = "linux")]
  71. {
  72. // For ANSI colors in the terminal
  73. colored::control::set_override(true);
  74. let term_logger = simplelog::TermLogger::new(
  75. simplelog::LevelFilter::Debug,
  76. simplelog::Config::default(),
  77. simplelog::TerminalMode::Mixed,
  78. simplelog::ColorChoice::Auto,
  79. );
  80. simplelog::CombinedLogger::init(vec![term_logger]).expect("logger");
  81. }
  82. let ex = Arc::new(smol::Executor::new());
  83. let sg = Arc::new(Mutex::new(SceneGraph::new()));
  84. let async_runtime = app::AsyncRuntime::new(ex.clone());
  85. async_runtime.start();
  86. let sg2 = sg.clone();
  87. let ex2 = ex.clone();
  88. let zmq_task = ex.spawn(async {
  89. let zmq_rpc = ZeroMQAdapter::new(sg2, ex2).await;
  90. zmq_rpc.run().await;
  91. });
  92. async_runtime.push_task(zmq_task);
  93. let (method_req, method_rep) = mpsc::channel();
  94. // The UI actually needs to be running for this to reply back.
  95. // Otherwise calls will just hang.
  96. let render_api = gfx2::RenderApi::new(method_req);
  97. let event_pub = gfx2::GraphicsEventPublisher::new();
  98. let text_shaper = TextShaper::new();
  99. let app =
  100. app::App::new(sg.clone(), ex.clone(), render_api.clone(), event_pub.clone(), text_shaper);
  101. let app_task = ex.spawn(app.start());
  102. async_runtime.push_task(app_task);
  103. //app.clone().start();
  104. // Nice to see which events exist
  105. let ev_sub = event_pub.subscribe_key_down();
  106. let ev_relay_task = ex.spawn(async move {
  107. debug!(target: "main", "event relayer started");
  108. loop {
  109. let Ok((key, mods, repeat)) = ev_sub.receive().await else {
  110. debug!(target: "main", "Event relayer closed");
  111. break
  112. };
  113. // Ignore keys which get stuck repeating when switching windows
  114. match key {
  115. miniquad::KeyCode::LeftShift | miniquad::KeyCode::LeftSuper => continue,
  116. _ => {}
  117. }
  118. if !repeat {
  119. debug!(target: "main", "key_down event: {:?} {:?} {}", key, mods, repeat);
  120. }
  121. }
  122. });
  123. async_runtime.push_task(ev_relay_task);
  124. let ev_sub = event_pub.subscribe_key_up();
  125. let ev_relay_task = ex.spawn(async move {
  126. debug!(target: "main", "event relayer started");
  127. loop {
  128. let Ok((key, mods)) = ev_sub.receive().await else {
  129. debug!(target: "main", "Event relayer closed");
  130. break
  131. };
  132. // Ignore keys which get stuck repeating when switching windows
  133. match key {
  134. miniquad::KeyCode::LeftShift | miniquad::KeyCode::LeftSuper => continue,
  135. _ => {}
  136. }
  137. debug!(target: "main", "key_up event: {:?} {:?}", key, mods);
  138. }
  139. });
  140. async_runtime.push_task(ev_relay_task);
  141. let ev_sub = event_pub.subscribe_char();
  142. let ev_relay_task = ex.spawn(async move {
  143. debug!(target: "main", "event relayer started");
  144. loop {
  145. let Ok((key, mods, repeat)) = ev_sub.receive().await else {
  146. debug!(target: "main", "Event relayer closed");
  147. break
  148. };
  149. debug!(target: "main", "char event: {:?} {:?} {}", key, mods, repeat);
  150. }
  151. });
  152. async_runtime.push_task(ev_relay_task);
  153. //let stage = gfx2::Stage::new(method_rep, event_pub);
  154. gfx2::run_gui(async_runtime, method_rep, event_pub);
  155. debug!(target: "main", "Started GFX backend");
  156. }
  157. /*
  158. use rustpython_vm::{self as pyvm, convert::ToPyObject};
  159. fn main() {
  160. let module = pyvm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
  161. let source = r#"
  162. def foo():
  163. open("hihi", "w")
  164. return 110
  165. #max(1 + lw/3, 4*10) + foo(2, True)
  166. "#;
  167. //let code_obj = vm
  168. // .compile(source, pyvm::compiler::Mode::Exec, "<embedded>".to_owned())
  169. // .map_err(|err| vm.new_syntax_error(&err, Some(source))).unwrap();
  170. //code_obj
  171. pyvm::import::import_source(vm, "lain", source).unwrap()
  172. });
  173. fn foo(x: u32, y: bool) -> u32 {
  174. if y {
  175. 2 * x
  176. } else {
  177. x
  178. }
  179. }
  180. let res = pyvm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
  181. let globals = vm.ctx.new_dict();
  182. globals.set_item("lw", vm.ctx.new_int(110).to_pyobject(vm), vm).unwrap();
  183. globals.set_item("lh", vm.ctx.new_int(4).to_pyobject(vm), vm).unwrap();
  184. globals.set_item("foo", vm.new_function("foo", foo).into(), vm).unwrap();
  185. let scope = pyvm::scope::Scope::new(None, globals);
  186. let foo_fn = module.get_attr("foo", vm).unwrap();
  187. foo_fn.call((), vm).unwrap()
  188. //vm.run_code_obj(code_obj, scope).unwrap()
  189. });
  190. println!("{:?}", res);
  191. }
  192. */