main.rs 4.6 KB

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