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