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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2022 Th3-S1lenc3
use derive_setters::*;
// IDEA: Add documentation for this module
#[derive(Debug, Default, Setters, PartialEq, Clone)]
#[setters(borrow_self, prefix = "set_")]
pub struct Connector {
pub name: String, // HDMI 1
pub status: String, // (Dis)connected
pub primary: bool, // True if primary
pub current_resolution: Resolution, // 1920x1080,
pub current_refresh_rate: String, // 60
pub prefered_resolution: Resolution, // 2560×1440
pub prefered_refresh_rate: String, // 120
pub position: Position, // 0,0 or 1920,0 or ...
pub orientation: String, // normal, left, etc...
pub available_orientations: Vec<String>, // normal, left, etc...
pub physical_dimensions: Dimensions, // 1210mm x 680mm
pub output_info: Vec<Output>,
}
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Position {
pub x: String, // 0 or 1920 or ...
pub y: String, // 0 or 1080 or ...
}
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Dimensions {
pub x: String, // 1210 or ...
pub y: String, // 680 or ...
}
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Resolution {
pub horizontal: String, // 1920
pub vertical: String, // 1080
}
impl Resolution {
pub fn pretty(&self) -> String {
format!("{}x{}", self.horizontal, self.vertical)
}
}
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Output {
pub resolution: Resolution,
pub rates: Vec<String>,
}
impl Connector {
/// Create a new instance of Connector
pub fn new() -> Self {
Connector::default()
}
/// Getter function for `Connector.name`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let name = &connector.name();
///
/// println!("Connector name: {}", name);
///
/// # assert_eq!(name, &"HDMI-1".to_string());
/// Ok(())
/// }
/// ```
pub fn name(&self) -> String {
self.name.clone()
}
/// Getter function for `Connector.status`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let status = &connector.status();
///
/// println!("Connector status: {}", status);
///
/// # assert_eq!(status, &"connected".to_string());
/// Ok(())
/// }
/// ```
pub fn status(&self) -> String {
self.status.clone()
}
/// Getter function for `Connector.primary`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let primary = &connector.primary();
///
/// println!("Connector primary: {}", primary);
///
/// # assert_eq!(primary, &true);
/// Ok(())
/// }
/// ```
pub fn primary(&self) -> bool {
self.primary.clone()
}
/// Getter function for `Connector.current_resolution`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
/// # use xrandr_parser::connector::Resolution;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let current_resolution = &connector.current_resolution();
///
/// println!("Connector Current Resolution: {:#?}", current_resolution);
///
/// # assert_eq!(current_resolution, &Resolution {
/// # horizontal: "1920".to_string(),
/// # vertical: "1080".to_string(),
/// # });
/// Ok(())
/// }
/// ```
pub fn current_resolution(&self) -> Resolution {
self.current_resolution.clone()
}
/// Getter function for `Connector.current_refresh_rate`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let current_refresh_rate = &connector.current_refresh_rate();
///
/// println!("Connector Current Refresh Rate: {}", current_refresh_rate);
///
/// # assert_eq!(current_refresh_rate, &"60.00".to_string());
/// Ok(())
/// }
/// ```
pub fn current_refresh_rate(&self) -> String {
self.current_refresh_rate.clone()
}
/// Getter function for `Connector.prefered_resolution`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
/// # use xrandr_parser::connector::Resolution;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let prefered_resolution = &connector.prefered_resolution();
///
/// println!("Connector Prefered Resolution: {:#?}", prefered_resolution);
///
/// # assert_eq!(prefered_resolution, &Resolution {
/// # horizontal: "1920".to_string(),
/// # vertical: "1080".to_string(),
/// # });
/// Ok(())
/// }
/// ```
pub fn prefered_resolution(&self) -> Resolution {
self.prefered_resolution.clone()
}
/// Getter function for `Connector.prefered_refresh_rate`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let prefered_refresh_rate = &connector.prefered_refresh_rate();
///
/// println!("Connector Prefered Refresh Rate: {}", prefered_refresh_rate);
///
/// # assert_eq!(prefered_refresh_rate, &"60.00".to_string());
/// Ok(())
/// }
/// ```
pub fn prefered_refresh_rate(&self) -> String {
self.prefered_refresh_rate.clone()
}
/// Getter function for `Connector.primary`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
/// # use xrandr_parser::connector::Position;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let position = &connector.position();
///
/// println!("Connector position: {:#?}", position);
///
/// # assert_eq!(position, &Position {
/// # x: "0".to_string(),
/// # y: "0".to_string()
/// # });
/// Ok(())
/// }
/// ```
pub fn position(&self) -> Position {
self.position.clone()
}
/// Getter function for `Connector.orientation`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let orientation = &connector.orientation();
///
/// println!("Connector orientation: {}", orientation);
///
/// # assert_eq!(orientation, &"normal".to_string());
/// Ok(())
/// }
/// ```
pub fn orientation(&self) -> String {
self.orientation.clone()
}
/// Getter function for `Connector.available_orientations`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let available_orientations = &connector.available_orientations();
///
/// println!("Connector Available Orientations: {:#?}", available_orientations);
///
/// # assert_eq!(available_orientations, &vec![
/// # "normal".to_string(),
/// # "left".to_string(),
/// # "inverted".to_string(),
/// # "right".to_string(),
/// # "x".to_string(),
/// # "axis".to_string(),
/// # "y".to_string(),
/// # "axis".to_string(),
/// # ]);
/// Ok(())
/// }
/// ```
pub fn available_orientations(&self) -> Vec<String> {
self.available_orientations.clone()
}
/// Getter function for `Connector.physical_dimensions`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
/// # use xrandr_parser::connector::Dimensions;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let physical_dimensions = &connector.physical_dimensions();
///
/// println!("Connector Physical Dimensions: {:#?}", physical_dimensions);
///
/// # assert_eq!(physical_dimensions, &Dimensions {
/// # x: "1210".to_string(),
/// # y: "680".to_string(),
/// # });
/// Ok(())
/// }
/// ```
pub fn physical_dimensions(&self) -> Dimensions {
self.physical_dimensions.clone()
}
/// Getter function for `Connector.output_info`
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
/// # use xrandr_parser::connector::{Output, Resolution};
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let output_info = &connector.output_info();
///
/// println!("Connector Output Info: {:#?}", output_info);
///
/// # assert_eq!(output_info, &vec![
/// # Output {
/// # resolution: Resolution {
/// # horizontal: "1920".to_string(),
/// # vertical: "1080".to_string(),
/// # },
/// # rates: vec![
/// # "60.00".to_string(),
/// # ].to_vec(),
/// # }
/// # ]);
/// Ok(())
/// }
/// ```
pub fn output_info(&self) -> Vec<Output> {
self.output_info.clone()
}
/// Get available resolutions for a specific connector in `struct` form. If the resolution
/// is not found it returns an empty `Vec<Resolution>` not an error.
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
/// # use xrandr_parser::connector::*;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let available_resolutions = &connector.available_resolutions()?;
///
/// println!("Available Resolutions: {:#?}", available_resolutions);
///
/// # assert_eq!(available_resolutions, &[
/// # Resolution {
/// # horizontal: "1920".to_string(),
/// # vertical: "1080".to_string(),
/// # }
/// # ].to_vec());
/// Ok(())
/// }
/// ```
// TODO: Condense these two functions into a single generic function.
// IDEA: Add error if `Vec<Resolution>` is empty
pub fn available_resolutions(&self) -> Result<Vec<Resolution>, String> {
let outputs: Vec<Output> = self.output_info();
let resolutions: Vec<Resolution> = outputs.into_iter().map(|o| o.resolution).collect();
Ok(resolutions)
}
/// Get available resolutions for a specific connector in `String` form. If the resolution
/// is not found it returns an empty `Vec<String>` not an error.
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let available_resolutions = &connector.available_resolutions_pretty()?;
///
/// println!("Available Resolutions: {:#?}", available_resolutions);
///
/// # assert_eq!(available_resolutions, &[
/// # "1920x1080".to_string(),
/// # ].to_vec());
/// Ok(())
/// }
/// ```
// IDEA: Add error if `Vec<String>` is empty
pub fn available_resolutions_pretty(&self) -> Result<Vec<String>, String> {
let outputs: Vec<Output> = self.output_info();
let resolutions: Vec<String> = outputs.into_iter().map(|o| o.resolution.pretty()).collect();
Ok(resolutions)
}
/// Get available refresh rates for a specific connector at a given resolution. If the resolution
/// is not found it returns an empty `Vec<String>` not an error.
///
/// ## Example
///
/// ```edition2021
/// #[allow(non_snake_case)]
///
/// use xrandr_parser::Parser;
///
/// fn main() -> Result<(), String> {
/// let mut XrandrParser = Parser::new();
///
/// XrandrParser.parse()?;
///
/// let connector = &XrandrParser.get_connector("HDMI-1")?;
///
/// let available_refresh_rates = &connector.available_refresh_rates("1920x1080")?;
///
/// println!("Available Refresh Rates: {:#?}", available_refresh_rates);
///
/// # assert_eq!(available_refresh_rates, &[
/// # "60.00".to_string(),
/// # ].to_vec());
/// Ok(())
/// }
/// ```
// IDEA: Add error if `Vec<String>` is empty
// TODO: Rewrite so target_resolution in `Resolution` struct
pub fn available_refresh_rates(&self, target_resolution: &str) -> Result<Vec<String>, String> {
let mut outputs: Vec<Output> = self.output_info();
outputs.retain(|o| o.resolution.pretty() == target_resolution);
Ok(outputs.into_iter().map(|o| o.rates).flatten().collect())
}
}