main.rs 6.6 KB

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