ming_wm_lib/
ipc.rs

1use std::io::{ stdin, BufRead };
2use std::panic;
3
4use crate::window_manager_types::WindowLike;
5use crate::serialize::Serializable;
6use crate::themes::ThemeInfo;
7use crate::framebuffer_types::Dimensions;
8use crate::messages::WindowMessage;
9use crate::logging::log;
10
11/*
12pub trait WindowLike {
13  fn handle_message(&mut self, message: WindowMessage) -> WindowMessageResponse;
14
15  fn draw(&self, theme_info: &ThemeInfo) -> Vec<DrawInstructions>;
16
17  //properties
18  fn title(&self) -> &'static str {
19    ""
20  }
21
22  fn resizable(&self) -> bool {
23    false
24  }
25
26  fn subtype(&self) -> WindowLikeType;
27
28  fn ideal_dimensions(&self, dimensions: Dimensions) -> Dimensions; //needs &self or its not object safe or some bullcrap
29}
30*/
31
32const LOG: bool = false;
33
34pub fn listen(mut window_like: impl WindowLike) {
35  panic::set_hook(Box::new(|panic_info| {
36    let (filename, line) = panic_info.location().map(|l| (l.file(), l.line())).unwrap_or(("<unknown>", 0));
37
38    let cause = if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
39      format!("{:?}", s)
40    } else if let Some(s) = panic_info.payload().downcast_ref::<String>() {
41      format!("{:?}", s)
42    } else {
43      "panic occurred".to_string()
44    };
45
46    log(&format!("A panic occurred at {}:{}: {}", filename, line, cause));
47  }));
48
49  let stdin = stdin();
50  for line in stdin.lock().lines() {
51    let line = line.unwrap().clone();
52    if LOG {
53      log(&line);
54    }
55    let mut parts = line.split(" ");
56    let method = parts.next().unwrap();
57    let arg = &parts.collect::<Vec<&str>>().join(" ");
58    let output = match method {
59      "handle_message" => {
60        format!("{}", &window_like.handle_message(WindowMessage::deserialize(arg).unwrap()).serialize())
61      },
62      "draw" => {
63        format!("{}", &window_like.draw(&ThemeInfo::deserialize(arg).unwrap()).serialize())
64      },
65      "title" => {
66        format!("{}", window_like.title())
67      },
68      "resizable" => {
69        format!("{}", window_like.resizable())
70      },
71      "subtype" => {
72        format!("{}", &window_like.subtype().serialize())
73      },
74      "ideal_dimensions" => {
75        format!("{}", &window_like.ideal_dimensions(Dimensions::deserialize(arg).unwrap()).serialize())
76      },
77      _ => String::new(),
78    };
79    if output != String::new() {
80      if LOG {
81        log(&output);
82      }
83      println!("{}", output);
84    }
85  }
86}
87