Skip to main content

virtual_console_lib/
args.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::colors::ColorScheme;
6use anyhow::Error;
7use carnelian::drawing::DisplayRotation;
8use std::convert::TryFrom;
9use std::str::FromStr;
10use virtcon_config::Config;
11
12pub const MIN_FONT_SIZE: f32 = 16.0;
13pub const MAX_FONT_SIZE: f32 = 160.0;
14
15#[derive(Debug, Default)]
16pub struct VirtualConsoleArgs {
17    pub disable: bool,
18    pub keep_log_visible: bool,
19    pub show_logo: bool,
20    pub keyrepeat: bool,
21    pub rounded_corners: bool,
22    pub color_scheme: ColorScheme,
23    pub keymap: String,
24    pub display_rotation: DisplayRotation,
25    pub font_size: f32,
26    pub dpi: Vec<u32>,
27    pub scrollback_rows: u32,
28    pub buffer_count: usize,
29}
30
31impl TryFrom<Config> for VirtualConsoleArgs {
32    type Error = Error;
33
34    fn try_from(config: Config) -> Result<Self, Self::Error> {
35        let keymap = match config.key_map.as_str() {
36            "qwerty" => "US_QWERTY",
37            "dvorak" => "US_DVORAK",
38            _ => &config.key_map,
39        }
40        .to_string();
41        let font_size = config.font_size.parse::<f32>()?.clamp(MIN_FONT_SIZE, MAX_FONT_SIZE);
42
43        Ok(VirtualConsoleArgs {
44            disable: config.disable,
45            keep_log_visible: config.keep_log_visible,
46            show_logo: config.show_logo,
47            keyrepeat: config.keyrepeat,
48            rounded_corners: config.rounded_corners,
49            color_scheme: ColorScheme::from_str(&config.color_scheme)?,
50            keymap,
51            display_rotation: DisplayRotation::try_from(config.display_rotation)?,
52            font_size,
53            dpi: config.dpi,
54            scrollback_rows: config.scrollback_rows,
55            buffer_count: config.buffer_count as usize,
56        })
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::colors::LIGHT_COLOR_SCHEME;
64
65    fn new_config() -> Config {
66        // Should match defaults set in component manifest.
67        Config {
68            buffer_count: 1,
69            color_scheme: "default".into(),
70            disable: false,
71            display_rotation: 0,
72            dpi: vec![],
73            font_size: "16.0".into(),
74            keep_log_visible: false,
75            show_logo: false,
76            keyrepeat: false,
77            key_map: "qwerty".into(),
78            rounded_corners: false,
79            scrollback_rows: 1024,
80        }
81    }
82
83    #[test]
84    fn check_disable() -> Result<(), Error> {
85        let config = Config { disable: true, ..new_config() };
86        let args = VirtualConsoleArgs::try_from(config)?;
87        assert_eq!(args.disable, true);
88
89        let config = Config { disable: false, ..new_config() };
90        let args = VirtualConsoleArgs::try_from(config)?;
91        assert_eq!(args.disable, false);
92
93        Ok(())
94    }
95
96    #[test]
97    fn check_keep_log_visible() -> Result<(), Error> {
98        let config = Config { keep_log_visible: true, ..new_config() };
99        let args = VirtualConsoleArgs::try_from(config)?;
100        assert_eq!(args.keep_log_visible, true);
101
102        let config = Config { keep_log_visible: false, ..new_config() };
103        let args = VirtualConsoleArgs::try_from(config)?;
104        assert_eq!(args.keep_log_visible, false);
105
106        Ok(())
107    }
108
109    #[test]
110    fn check_show_logo() -> Result<(), Error> {
111        let config = Config { show_logo: true, ..new_config() };
112        let args = VirtualConsoleArgs::try_from(config)?;
113        assert_eq!(args.show_logo, true);
114
115        let config = Config { show_logo: false, ..new_config() };
116        let args = VirtualConsoleArgs::try_from(config)?;
117        assert_eq!(args.show_logo, false);
118
119        Ok(())
120    }
121
122    #[test]
123    fn check_keyrepeat() -> Result<(), Error> {
124        let config = Config { keyrepeat: true, ..new_config() };
125        let args = VirtualConsoleArgs::try_from(config)?;
126        assert_eq!(args.keyrepeat, true);
127
128        let config = Config { keyrepeat: false, ..new_config() };
129        let args = VirtualConsoleArgs::try_from(config)?;
130        assert_eq!(args.keyrepeat, false);
131
132        Ok(())
133    }
134
135    #[test]
136    fn check_rounded_corners() -> Result<(), Error> {
137        let config = Config { rounded_corners: true, ..new_config() };
138        let args = VirtualConsoleArgs::try_from(config)?;
139        assert_eq!(args.rounded_corners, true);
140
141        let config = Config { rounded_corners: false, ..new_config() };
142        let args = VirtualConsoleArgs::try_from(config)?;
143        assert_eq!(args.rounded_corners, false);
144
145        Ok(())
146    }
147
148    #[test]
149    fn check_color_scheme() -> Result<(), Error> {
150        let config = Config { color_scheme: "light".into(), ..new_config() };
151        let args = VirtualConsoleArgs::try_from(config)?;
152        assert_eq!(args.color_scheme, LIGHT_COLOR_SCHEME);
153
154        let config = Config { ..new_config() };
155        let args = VirtualConsoleArgs::try_from(config)?;
156        assert_eq!(args.color_scheme, ColorScheme::default());
157
158        Ok(())
159    }
160
161    #[test]
162    fn check_keymap() -> Result<(), Error> {
163        let config = Config { key_map: "US_DVORAK".into(), ..new_config() };
164        let args = VirtualConsoleArgs::try_from(config)?;
165        assert_eq!(args.keymap, "US_DVORAK");
166
167        let config = Config { key_map: "dvorak".into(), ..new_config() };
168        let args = VirtualConsoleArgs::try_from(config)?;
169        assert_eq!(args.keymap, "US_DVORAK");
170
171        let config = Config { ..new_config() };
172        let args = VirtualConsoleArgs::try_from(config)?;
173        assert_eq!(args.keymap, "US_QWERTY");
174
175        Ok(())
176    }
177
178    #[test]
179    fn check_display_rotation() -> Result<(), Error> {
180        let config = Config { display_rotation: 90, ..new_config() };
181        let args = VirtualConsoleArgs::try_from(config)?;
182        assert_eq!(args.display_rotation, DisplayRotation::Deg90);
183
184        let config = Config { display_rotation: 0, ..new_config() };
185        let args = VirtualConsoleArgs::try_from(config)?;
186        assert_eq!(args.display_rotation, DisplayRotation::Deg0);
187
188        Ok(())
189    }
190
191    #[test]
192    fn check_font_size() -> Result<(), Error> {
193        let config = Config { font_size: "32.0".into(), ..new_config() };
194        let args = VirtualConsoleArgs::try_from(config)?;
195        assert_eq!(args.font_size, 32.0);
196
197        let config = Config { font_size: "1000000.0".into(), ..new_config() };
198        let args = VirtualConsoleArgs::try_from(config)?;
199        assert_eq!(args.font_size, MAX_FONT_SIZE);
200
201        Ok(())
202    }
203
204    #[test]
205    fn check_dpi() -> Result<(), Error> {
206        let config = Config { dpi: vec![160, 320, 480, 640], ..new_config() };
207        let args = VirtualConsoleArgs::try_from(config)?;
208        assert_eq!(args.dpi, vec![160, 320, 480, 640]);
209
210        Ok(())
211    }
212
213    #[test]
214    fn check_scrollback_rows() -> Result<(), Error> {
215        let config = Config { scrollback_rows: 10_000, ..new_config() };
216        let args = VirtualConsoleArgs::try_from(config)?;
217        assert_eq!(args.scrollback_rows, 10_000);
218
219        Ok(())
220    }
221
222    #[test]
223    fn check_buffer_count() -> Result<(), Error> {
224        let config = Config { buffer_count: 2, ..new_config() };
225        let args = VirtualConsoleArgs::try_from(config)?;
226        assert_eq!(args.buffer_count, 2);
227
228        Ok(())
229    }
230}