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
34/// Listen and process what the window manager writes to our stdin
35pub fn listen(mut window_like: impl WindowLike) {
36  panic::set_hook(Box::new(|panic_info| {
37    let (filename, line) = panic_info.location().map(|l| (l.file(), l.line())).unwrap_or(("<unknown>", 0));
38
39    let cause = if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
40      format!("{:?}", s)
41    } else if let Some(s) = panic_info.payload().downcast_ref::<String>() {
42      format!("{:?}", s)
43    } else {
44      "panic occurred".to_string()
45    };
46
47    log(&format!("A panic occurred at {}:{}: {}", filename, line, cause));
48  }));
49
50  let stdin = stdin();
51  for line in stdin.lock().lines() {
52    let line = line.unwrap().clone();
53    if LOG {
54      log(&line);
55    }
56    let mut parts = line.split(" ");
57    let method = parts.next().unwrap();
58    let arg = &parts.collect::<Vec<&str>>().join(" ");
59    let output = match method {
60      "handle_message" => {
61        //newlines allowed for ClipboardCopy, but represented by the Linear A char
62        window_like.handle_message(WindowMessage::deserialize(arg).unwrap()).serialize().to_string()
63      },
64      "draw" => {
65        //newlines never allowed
66        window_like.draw(&ThemeInfo::deserialize(arg).unwrap()).serialize().replace("\n", "").to_string()
67      },
68      "title" => {
69        window_like.title().to_string()
70      },
71      "resizable" => {
72        window_like.resizable().to_string()
73      },
74      "subtype" => {
75        window_like.subtype().serialize().to_string()
76      },
77      "ideal_dimensions" => {
78        window_like.ideal_dimensions(Dimensions::deserialize(arg).unwrap()).serialize().to_string()
79      },
80      _ => String::new(),
81    };
82    if output != String::new() {
83      if LOG {
84        log(&output);
85      }
86      println!("{}", output);
87    }
88  }
89}
90