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) {
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 window_like.handle_message(WindowMessage::deserialize(arg).unwrap()).serialize().to_string()
63 },
64 "draw" => {
65 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