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