main.rs 4.6 KB

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