xref: /StarryEngine/starry_server/src/core/input/inputs.rs (revision 731cae0674923fcc85c6e683a2eee596eb642796) !
1 use super::InputHandler;
2 use bitflags::bitflags;
3 use starry_client::base::event::{ButtonEvent, Event, MouseRelativeEvent};
4 use std::{fs::File, io::Read};
5 
6 const MOUSE_DEVICE_PATH: &str = "/dev/char/psmouse";
7 const KBD_DEVICE_PATH: &str = "/dev/char/tty0";
8 
9 bitflags! {
10     /// 鼠标状态掩码
11     #[derive(Default)]
12     pub struct MouseFlags: u8 {
13         /// 左键是否按下
14         const LEFT_BUTTON = 0b0000_0001;
15 
16         /// 右键是否按下
17         const RIGHT_BUTTON = 0b0000_0010;
18 
19         /// 滚轮是否按下
20         const MIDDLE_BUTTON = 0b0000_0100;
21 
22         /// 鼠标是否启用
23         const ALWAYS_ONE = 0b0000_1000;
24 
25         /// x轴移动方向是否为负
26         const X_SIGN = 0b0001_0000;
27 
28         /// y轴移动方向是否为负
29         const Y_SIGN = 0b0010_0000;
30 
31         /// x方向移动超出范围
32         const X_OVERFLOW = 0b0100_0000;
33 
34         /// y方向移动超出范围
35         const Y_OVERFLOW = 0b1000_0000;
36     }
37 }
38 
39 #[allow(dead_code)]
40 pub struct MouseInputHandler {
41     /// 读取的文件
42     file: File,
43     /// 当前数据包序号
44     packet_index: u8,
45     /// 鼠标状态
46     flags: MouseFlags,
47     /// x轴相对移动
48     dx: i16,
49     /// y轴相对移动
50     dy: i16,
51     /// 移动系数/灵敏度
52     scale: u8,
53 }
54 
55 impl MouseInputHandler {
new() -> Box<MouseInputHandler>56     pub fn new() -> Box<MouseInputHandler> {
57         let file = File::open(MOUSE_DEVICE_PATH).expect("Fail to open mouse device");
58         // println!("[Init] Mouse_Input_Handler created successfully!");
59         Box::new(MouseInputHandler {
60             flags: MouseFlags::empty(),
61             packet_index: 0,
62             file: file,
63             dx: 0,
64             dy: 0,
65             scale: 1,
66         })
67     }
68 }
69 
70 impl InputHandler for MouseInputHandler {
get_listening_file(&mut self) -> &File71     fn get_listening_file(&mut self) -> &File {
72         self.file.by_ref()
73     }
74 
set_listening_file(&mut self, file: File)75     fn set_listening_file(&mut self, file: File) {
76         self.file = file;
77     }
78 
handle(&mut self, packet: u8) -> Vec<Event>79     fn handle(&mut self, packet: u8) -> Vec<Event> {
80         // println!("[Info] Mouse_Input_Handler handle packet {:?}", packet);
81         /// 求补码
82         fn sign_extend(value: u8) -> i16 {
83             ((value as u16) | 0xFF00) as i16
84         }
85 
86         let mut events: Vec<Event> = Vec::new();
87         match self.packet_index {
88             0 => {
89                 let flags = MouseFlags::from_bits_truncate(packet);
90                 if flags.contains(MouseFlags::ALWAYS_ONE) {
91                     self.flags = flags;
92                     events.push(ButtonEvent::new(packet).to_event());
93                 }
94             }
95             1 => {
96                 // 计算dx
97                 if self.flags.contains(MouseFlags::X_OVERFLOW) {
98                     self.dx = 0;
99                 } else if self.flags.contains(MouseFlags::X_SIGN) {
100                     self.dx = sign_extend(packet);
101                 } else {
102                     self.dx = packet as i16;
103                 }
104             }
105             2 => {
106                 // 计算dy
107                 if self.flags.contains(MouseFlags::Y_OVERFLOW) {
108                     self.dy = 0;
109                 } else if self.flags.contains(MouseFlags::Y_SIGN) {
110                     self.dy = sign_extend(packet);
111                 } else {
112                     self.dy = packet as i16;
113                 }
114 
115                 // 传入移动事件
116                 events.push(
117                     MouseRelativeEvent {
118                         dx: self.dx as i32 * self.scale as i32,
119                         dy: self.dy as i32 * self.scale as i32,
120                     }
121                     .to_event(),
122                 );
123             }
124             _ => unreachable!(),
125         }
126         self.packet_index = (self.packet_index + 1) % 3;
127         return events;
128     }
129 }
130 
131 // TODO
132 pub struct KeyboardInputHandler {
133     /// 读取的文件
134     file: File,
135 }
136 
137 impl KeyboardInputHandler {
new() -> Box<KeyboardInputHandler>138     pub fn new() -> Box<KeyboardInputHandler> {
139         let file = File::open(KBD_DEVICE_PATH).expect("Fail to open mouse device");
140         // println!("[Init] Keyboard_Input_Handler created successfully!");
141         Box::new(KeyboardInputHandler { file: file })
142     }
143 }
144 
145 impl InputHandler for KeyboardInputHandler {
get_listening_file(&mut self) -> &File146     fn get_listening_file(&mut self) -> &File {
147         &self.file
148     }
149 
set_listening_file(&mut self, file: File)150     fn set_listening_file(&mut self, file: File) {
151         self.file = file;
152     }
153 
handle(&mut self, _packet: u8) -> Vec<Event>154     fn handle(&mut self, _packet: u8) -> Vec<Event> {
155         Vec::new()
156     }
157 }
158