1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#[derive(Clone)]
pub struct ModelOptions {
pub(crate) color_order: ColorOrder,
pub(crate) orientation: Orientation,
pub(crate) invert_vertical_refresh: bool,
pub(crate) invert_horizontal_refresh: bool,
pub(crate) window_offset_handler: fn(&ModelOptions) -> (u16, u16),
pub(crate) display_size: (u16, u16),
pub(crate) framebuffer_size: (u16, u16),
}
impl ModelOptions {
pub fn with_sizes(display_size: (u16, u16), framebuffer_size: (u16, u16)) -> Self {
Self {
color_order: ColorOrder::default(),
orientation: Orientation::default(),
invert_horizontal_refresh: false,
invert_vertical_refresh: false,
window_offset_handler: no_offset,
display_size,
framebuffer_size,
}
}
pub fn with_all(
display_size: (u16, u16),
framebuffer_size: (u16, u16),
window_offset_handler: fn(&ModelOptions) -> (u16, u16),
) -> Self {
Self {
color_order: ColorOrder::default(),
orientation: Orientation::default(),
invert_horizontal_refresh: false,
invert_vertical_refresh: false,
window_offset_handler,
display_size,
framebuffer_size,
}
}
pub fn madctl(&self) -> u8 {
let mut value = self.orientation.value_u8();
if self.invert_vertical_refresh {
value |= 0b0001_0000;
}
match self.color_order {
ColorOrder::Rgb => {}
ColorOrder::Bgr => value |= 0b0000_1000,
}
if self.invert_horizontal_refresh {
value |= 0b0000_0100;
}
value
}
pub fn display_size(&self) -> (u16, u16) {
Self::orient_size(self.display_size, self.orientation())
}
pub fn framebuffer_size(&self) -> (u16, u16) {
let size = if self.framebuffer_size == (0, 0) {
self.display_size
} else {
self.framebuffer_size
};
Self::orient_size(size, self.orientation())
}
pub fn window_offset(&mut self) -> (u16, u16) {
(self.window_offset_handler)(self)
}
pub fn orientation(&self) -> Orientation {
self.orientation
}
pub fn set_orientation(&mut self, orientation: Orientation) {
self.orientation = orientation;
}
fn orient_size(size: (u16, u16), orientation: Orientation) -> (u16, u16) {
match orientation {
Orientation::Portrait(_) | Orientation::PortraitInverted(_) => size,
Orientation::Landscape(_) | Orientation::LandscapeInverted(_) => (size.1, size.0),
}
}
}
fn no_offset(options: &ModelOptions) -> (u16, u16) {
match options.orientation {
Orientation::PortraitInverted(_) | Orientation::LandscapeInverted(_) => {
let hdiff = options.framebuffer_size.1 - options.display_size.1;
let mut x = 0;
let mut y = 0;
match options.orientation {
Orientation::PortraitInverted(_) => y = hdiff,
Orientation::LandscapeInverted(_) => x = hdiff,
_ => {}
}
(x, y)
}
_ => (0, 0),
}
}
#[derive(Debug, Clone, Copy)]
pub enum Orientation {
Portrait(bool),
Landscape(bool),
PortraitInverted(bool),
LandscapeInverted(bool),
}
impl Default for Orientation {
fn default() -> Self {
Self::Portrait(false)
}
}
impl Orientation {
pub fn value_u8(&self) -> u8 {
match self {
Orientation::Portrait(false) => 0b0000_0000,
Orientation::Portrait(true) => 0b0100_0000,
Orientation::PortraitInverted(false) => 0b1100_0000,
Orientation::PortraitInverted(true) => 0b1000_0000,
Orientation::Landscape(false) => 0b0010_0000,
Orientation::Landscape(true) => 0b0110_0000,
Orientation::LandscapeInverted(false) => 0b1110_0000,
Orientation::LandscapeInverted(true) => 0b1010_0000,
}
}
}
#[derive(Copy, Clone)]
pub enum TearingEffect {
Off,
Vertical,
HorizontalAndVertical,
}
#[derive(Debug, Clone, Copy)]
pub enum ColorOrder {
Rgb,
Bgr,
}
impl Default for ColorOrder {
fn default() -> Self {
Self::Rgb
}
}