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