1use file_system::{ControlCommand, define_command};
21
22use crate::kind;
23
24file_system::plain_data! {
25 #[repr(C)]
26 #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
28 pub struct Point {
29 pub x: i16,
30 pub y: i16,
31 }
32}
33
34impl Point {
35 pub const fn new(x: i16, y: i16) -> Self {
36 Self { x, y }
37 }
38
39 pub const fn get_x(&self) -> i16 {
40 self.x
41 }
42
43 pub const fn get_y(&self) -> i16 {
44 self.y
45 }
46
47 pub fn split(self) -> (i16, i16) {
48 (self.x, self.y)
49 }
50
51 pub fn set_x(mut self, value: i16) -> Self {
52 self.x = value;
53 self
54 }
55
56 pub fn set_y(mut self, value: i16) -> Self {
57 self.y = value;
58 self
59 }
60
61 pub fn set(mut self, x: i16, y: i16) -> Self {
62 self.x = x;
63 self.y = y;
64 self
65 }
66
67 pub const fn scale(mut self, factor: f64) -> Self {
68 let x = self.x as f64 * factor;
69 let y = self.y as f64 * factor;
70 self.x = (if x >= 0.0 { x + 0.5 } else { x - 0.5 }) as i16;
71 self.y = (if y >= 0.0 { y + 0.5 } else { y - 0.5 }) as i16;
72 self
73 }
74}
75
76impl From<(i16, i16)> for Point {
77 fn from((x, y): (i16, i16)) -> Self {
78 Self::new(x, y)
79 }
80}
81
82impl From<Point> for (i16, i16) {
83 fn from(point: Point) -> Self {
84 point.split()
85 }
86}
87
88file_system::plain_data! {
89 #[repr(C)]
90 #[derive(Clone, Copy, Default, Debug, PartialEq, Eq)]
92 pub struct Area(Point, Point);
93}
94
95impl Area {
96 pub fn new(point_1: Point, point_2: Point) -> Self {
97 Self(point_1, point_2)
98 }
99
100 pub fn get_point_1(&self) -> Point {
101 self.0
102 }
103
104 pub fn get_point_2(&self) -> Point {
105 self.1
106 }
107
108 pub fn get_width(&self) -> u16 {
109 (self.1.get_x() - self.0.get_x()).unsigned_abs() + 1
110 }
111
112 pub fn get_height(&self) -> u16 {
113 (self.1.get_y() - self.0.get_y()).unsigned_abs() + 1
114 }
115
116 pub fn set_point_1(mut self, value: Point) -> Self {
117 self.0 = value;
118 self
119 }
120
121 pub fn set_point_2(mut self, value: Point) -> Self {
122 self.1 = value;
123 self
124 }
125}
126
127impl AsRef<[u8]> for Area {
128 fn as_ref(&self) -> &[u8] {
129 unsafe {
130 core::slice::from_raw_parts(self as *const _ as *const u8, core::mem::size_of::<Self>())
131 }
132 }
133}
134
135impl AsRef<Area> for [u8; core::mem::size_of::<Area>()] {
136 fn as_ref(&self) -> &Area {
137 unsafe { &*(self as *const _ as *const Area) }
138 }
139}
140
141define_command!(SET_DRAWING_AREA, Write, kind::SCREEN, 0x01, Area, ());
142define_command!(GET_RESOLUTION, Read, kind::SCREEN, 0x02, (), Point);
143define_command!(WAS_RESIZED, Read, kind::SCREEN, 0x03, (), u8);
146
147pub const SCREENSHOT_PATH_MAX_BYTES: usize = 512;
148
149file_system::plain_data! {
150 #[repr(C)]
151 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
152 pub struct ScreenshotDimensions {
153 pub width: u32,
154 pub height: u32,
155 }
156}
157
158file_system::plain_data! {
159 #[repr(C)]
160 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
161 pub struct ScreenshotRequest {
162 path: [u8; SCREENSHOT_PATH_MAX_BYTES],
163 length: u16,
164 }
165}
166
167impl ScreenshotRequest {
168 pub fn new(path: &str) -> Option<Self> {
169 let path = path.as_bytes();
170 if path.is_empty() || path.len() > SCREENSHOT_PATH_MAX_BYTES {
171 return None;
172 }
173
174 let mut request = Self {
175 path: [0; SCREENSHOT_PATH_MAX_BYTES],
176 length: path.len() as u16,
177 };
178 request.path[..path.len()].copy_from_slice(path);
179 Some(request)
180 }
181
182 pub fn path(&self) -> Option<&str> {
183 core::str::from_utf8(&self.path[..self.length as usize]).ok()
184 }
185}
186
187define_command!(
188 WRITE_SCREENSHOT_TO_PATH,
189 Write,
190 kind::SCREEN,
191 0x04,
192 ScreenshotRequest,
193 ScreenshotDimensions
194);
195
196pub const BRIGHTNESS_OFF: u8 = 0;
198
199pub const BRIGHTNESS_FULL: u8 = u8::MAX;
201
202define_command!(GET_BRIGHTNESS, Read, kind::SCREEN, 0x05, (), u8);
204
205define_command!(SET_BRIGHTNESS, Write, kind::SCREEN, 0x06, u8, ());
207
208#[cfg(test)]
209mod tests {
210 use super::*;
211 use file_system::ControlCommand;
212
213 #[test]
214 fn was_resized_payload_is_a_flag_byte_not_a_bool() {
215 assert_eq!(WAS_RESIZED::IDENTIFIER.get_size(), 1);
218 assert!(bool::try_from(0u8).is_ok());
219 assert!(bool::try_from(1u8).is_ok());
220 assert!(bool::try_from(2u8).is_err());
221 }
222}