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
11const 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