xref: /StarryEngine/starry_server/src/core/mod.rs (revision 48d97639c61db7d12182c250896dd116973d24c5)
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(CursorKind::Normal, Image::from_path(CURSOR_NORMAL).unwrap_or(Image::new(10, 10)));
56         // cursors.insert(CursorKind::BottomLeftCorner, Image::from_path_scale(&config.bottom_left_corner, scale).unwrap_or(Image::new(0, 0)));
57         // cursors.insert(CursorKind::BottomRightCorner, Image::from_path_scale(&config.bottom_right_corner, scale).unwrap_or(Image::new(0, 0)));
58         // cursors.insert(CursorKind::BottomSide, Image::from_path_scale(&config.bottom_side, scale).unwrap_or(Image::new(0, 0)));
59         // cursors.insert(CursorKind::LeftSide, Image::from_path_scale(&config.left_side, scale).unwrap_or(Image::new(0, 0)));
60         // cursors.insert(CursorKind::RightSide, Image::from_path_scale(&config.right_side, scale).unwrap_or(Image::new(0, 0)));
61 
62         let server = StarryServer {
63             data: RwLock::new(StarryServerData {
64                 displays: displays,
65                 config: Rc::clone(&config),
66                 cursors: cursors,
67             }),
68         };
69 
70         unsafe {
71             STARRY_SERVER = Some(Arc::new(server));
72         }
73 
74         // println!("[Init] Starry_Server created successfully!");
75     }
76 
77     /// 开启主循环
78     pub fn run(&self) {
79         WindowManager::new();
80         Compositor::new();
81         InputManager::new();
82 
83         // TODO 临时在此创建桌面窗口
84         window_manager().unwrap().window_new(
85             0,
86             0,
87             SCREEN_WIDTH as i32,
88             SCREEN_HEIGHT as i32,
89             "",
90             "".to_string(),
91             DESKTOP_BG,
92         );
93 
94         // println!("[Init] Starry_Server start main loop!");
95         loop {
96             input_manager().unwrap().polling_all();
97             window_manager().unwrap().handle_all_events();
98             compositor().unwrap().redraw_all();
99             // std::thread::sleep(std::time::Duration::from_millis(1));
100         }
101     }
102 }
103