xref: /StarryEngine/starry_server/src/core/mod.rs (revision 282ef85ca13266353a8fb594bdc60918a2715518)
1 use std::{cell::RefCell, collections::BTreeMap, sync::Arc};
2 
3 use crate::{
4     base::{display::Display, image::Image},
5     config::Config,
6 };
7 
8 use self::{
9     compositor::{compositor, Compositor},
10     input::{input_manager, InputManager},
11     window_manager::{window_manager, CursorKind, WindowManager},
12 };
13 
14 pub mod compositor;
15 pub mod input;
16 pub mod window_manager;
17 
18 // TODO: 读帧缓冲设备属性
19 /// 屏幕宽度
20 pub const SCREEN_WIDTH: usize = 1440;
21 /// 屏幕高度
22 #[allow(dead_code)]
23 pub const SCREEN_HEIGHT: usize = 900;
24 
25 static DESKTOP_BG: &[u8] = include_bytes!("../resource/desktop_bg.png");
26 static CURSOR_NORMAL: &[u8] = include_bytes!("../resource/cursor_normal.png");
27 
28 static mut STARRY_SERVER: Option<Arc<StarryServer>> = None;
29 
30 pub fn starry_server() -> Option<Arc<StarryServer>> {
31     unsafe { STARRY_SERVER.clone() }
32 }
33 
34 /// 图形系统服务器
35 pub struct StarryServer {
36     pub displays: RefCell<Vec<Display>>,
37     pub config: RefCell<Arc<Config>>,
38     pub cursors: RefCell<BTreeMap<CursorKind, Image>>,
39 }
40 
41 impl StarryServer {
42     /// 创建图形服务器
43     pub fn new(config: Arc<Config>, displays: Vec<Display>) {
44         let mut cursors = BTreeMap::new();
45         cursors.insert(CursorKind::None, Image::new(0, 0));
46         cursors.insert(
47             CursorKind::Normal,
48             Image::from_path(CURSOR_NORMAL).unwrap_or(Image::new(10, 10)),
49         );
50         // cursors.insert(CursorKind::BottomLeftCorner, Image::from_path_scale(&config.bottom_left_corner, scale).unwrap_or(Image::new(0, 0)));
51         // cursors.insert(CursorKind::BottomRightCorner, Image::from_path_scale(&config.bottom_right_corner, scale).unwrap_or(Image::new(0, 0)));
52         // cursors.insert(CursorKind::BottomSide, Image::from_path_scale(&config.bottom_side, scale).unwrap_or(Image::new(0, 0)));
53         // cursors.insert(CursorKind::LeftSide, Image::from_path_scale(&config.left_side, scale).unwrap_or(Image::new(0, 0)));
54         // cursors.insert(CursorKind::RightSide, Image::from_path_scale(&config.right_side, scale).unwrap_or(Image::new(0, 0)));
55 
56         let server = StarryServer {
57             displays: RefCell::new(displays),
58             config: RefCell::new(config),
59             cursors: RefCell::new(cursors),
60         };
61 
62         unsafe {
63             STARRY_SERVER = Some(Arc::new(server));
64         }
65 
66         // println!("[Init] Starry_Server created successfully!");
67     }
68 
69     /// 开启主循环
70     pub fn run(&self) {
71         WindowManager::new();
72         Compositor::new();
73         InputManager::new();
74 
75         // TODO 临时在此创建桌面窗口
76         window_manager().unwrap().window_new(
77             0,
78             0,
79             SCREEN_WIDTH as i32,
80             SCREEN_HEIGHT as i32,
81             "",
82             "".to_string(),
83             DESKTOP_BG,
84         );
85 
86         // println!("[Init] Starry_Server start main loop!");
87         loop {
88             input_manager().unwrap().polling_all();
89             window_manager().unwrap().handle_all_events();
90             compositor().unwrap().redraw_all();
91             // std::thread::sleep(std::time::Duration::from_millis(1));
92         }
93     }
94 }
95