ironrdp_displaycontrol/pdu/mod.rs
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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
//! Display Update Virtual Channel Extension PDUs [MS-RDPEDISP][1] implementation.
//!
//! [1]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/d2954508-f487-48bc-8731-39743e0854a9
use ironrdp_core::{
ensure_fixed_part_size, invalid_field_err, Decode, DecodeResult, Encode, EncodeResult, ReadCursor, WriteCursor,
};
use ironrdp_dvc::DvcEncode;
use tracing::warn;
const DISPLAYCONTROL_PDU_TYPE_CAPS: u32 = 0x00000005;
const DISPLAYCONTROL_PDU_TYPE_MONITOR_LAYOUT: u32 = 0x00000002;
const DISPLAYCONTROL_MONITOR_PRIMARY: u32 = 0x00000001;
// Set out expectations about supported PDU values. 1024 monitors with 8k*8k pixel area is
// already excessive, (this extension only supports displays up to 8k*8k) therefore we could safely
// use those limits to detect ill-formed PDUs and set out invariants.
const MAX_SUPPORTED_MONITORS: u16 = 1024;
const MAX_MONITOR_AREA_FACTOR: u16 = 1024 * 16;
/// Display Update Virtual Channel message (PDU prefixed with `DISPLAYCONTROL_HEADER`)
///
/// INVARIANTS: size of encoded inner PDU is always less than `u32::MAX - Self::FIXED_PART_SIZE`
/// (See [`DisplayControlCapabilities`] & [`DisplayControlMonitorLayout`] invariants)
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DisplayControlPdu {
Caps(DisplayControlCapabilities),
MonitorLayout(DisplayControlMonitorLayout),
}
impl DisplayControlPdu {
const NAME: &'static str = "DISPLAYCONTROL_HEADER";
const FIXED_PART_SIZE: usize = 4 /* Type */ + 4 /* Length */;
}
impl Encode for DisplayControlPdu {
fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
ensure_fixed_part_size!(in: dst);
let (kind, payload_length) = match self {
DisplayControlPdu::Caps(caps) => (DISPLAYCONTROL_PDU_TYPE_CAPS, caps.size()),
DisplayControlPdu::MonitorLayout(layout) => (DISPLAYCONTROL_PDU_TYPE_MONITOR_LAYOUT, layout.size()),
};
// This will never overflow as per invariants.
#[allow(clippy::arithmetic_side_effects)]
let pdu_size = payload_length + Self::FIXED_PART_SIZE;
// Write `DISPLAYCONTROL_HEADER` fields.
dst.write_u32(kind);
dst.write_u32(pdu_size.try_into().unwrap());
match self {
DisplayControlPdu::Caps(caps) => caps.encode(dst),
DisplayControlPdu::MonitorLayout(layout) => layout.encode(dst),
}?;
Ok(())
}
fn name(&self) -> &'static str {
Self::NAME
}
fn size(&self) -> usize {
// As per invariants: This will never overflow.
#[allow(clippy::arithmetic_side_effects)]
let size = Self::FIXED_PART_SIZE
+ match self {
DisplayControlPdu::Caps(caps) => caps.size(),
DisplayControlPdu::MonitorLayout(layout) => layout.size(),
};
size
}
}
impl DvcEncode for DisplayControlPdu {}
impl<'de> Decode<'de> for DisplayControlPdu {
fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
ensure_fixed_part_size!(in: src);
// Read `DISPLAYCONTROL_HEADER` fields.
let kind = src.read_u32();
let pdu_length = src.read_u32();
let _payload_length = pdu_length
.checked_sub(Self::FIXED_PART_SIZE.try_into().unwrap())
.ok_or_else(|| invalid_field_err!("Length", "Display control PDU length is too small"))?;
match kind {
DISPLAYCONTROL_PDU_TYPE_CAPS => {
let caps = DisplayControlCapabilities::decode(src)?;
Ok(DisplayControlPdu::Caps(caps))
}
DISPLAYCONTROL_PDU_TYPE_MONITOR_LAYOUT => {
let layout = DisplayControlMonitorLayout::decode(src)?;
Ok(DisplayControlPdu::MonitorLayout(layout))
}
_ => Err(invalid_field_err!("Type", "Unknown display control PDU type")),
}
}
}
impl From<DisplayControlCapabilities> for DisplayControlPdu {
fn from(caps: DisplayControlCapabilities) -> Self {
Self::Caps(caps)
}
}
impl From<DisplayControlMonitorLayout> for DisplayControlPdu {
fn from(layout: DisplayControlMonitorLayout) -> Self {
Self::MonitorLayout(layout)
}
}
/// 2.2.2.1 DISPLAYCONTROL_CAPS_PDU
///
/// Display control channel capabilities PDU.
///
/// INVARIANTS:
/// 0 <= max_num_monitors <= MAX_SUPPORTED_MONITORS
/// 0 <= max_monitor_area_factor_a <= MAX_MONITOR_AREA_FACTOR
/// 0 <= max_monitor_area_factor_b <= MAX_MONITOR_AREA_FACTOR
///
/// [2.2.2.1]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/8989a211-984e-4ecc-80f3-60694fc4b476
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DisplayControlCapabilities {
max_num_monitors: u32,
max_monitor_area_factor_a: u32,
max_monitor_area_factor_b: u32,
max_monitor_area: u64,
}
impl DisplayControlCapabilities {
const NAME: &'static str = "DISPLAYCONTROL_CAPS_PDU";
const FIXED_PART_SIZE: usize = 4 /* MaxNumMonitors */
+ 4 /* MaxMonitorAreaFactorA */
+ 4 /* MaxMonitorAreaFactorB */;
pub fn new(
max_num_monitors: u32,
max_monitor_area_factor_a: u32,
max_monitor_area_factor_b: u32,
) -> DecodeResult<Self> {
let max_monitor_area =
calculate_monitor_area(max_num_monitors, max_monitor_area_factor_a, max_monitor_area_factor_b)?;
Ok(Self {
max_num_monitors,
max_monitor_area_factor_a,
max_monitor_area_factor_b,
max_monitor_area,
})
}
pub fn max_monitor_area(&self) -> u64 {
self.max_monitor_area
}
}
impl Encode for DisplayControlCapabilities {
fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
ensure_fixed_part_size!(in: dst);
dst.write_u32(self.max_num_monitors);
dst.write_u32(self.max_monitor_area_factor_a);
dst.write_u32(self.max_monitor_area_factor_b);
Ok(())
}
fn name(&self) -> &'static str {
Self::NAME
}
fn size(&self) -> usize {
Self::FIXED_PART_SIZE
}
}
impl<'de> Decode<'de> for DisplayControlCapabilities {
fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
ensure_fixed_part_size!(in: src);
let max_num_monitors = src.read_u32();
let max_monitor_area_factor_a = src.read_u32();
let max_monitor_area_factor_b = src.read_u32();
let max_monitor_area =
calculate_monitor_area(max_num_monitors, max_monitor_area_factor_a, max_monitor_area_factor_b)?;
Ok(Self {
max_num_monitors,
max_monitor_area_factor_a,
max_monitor_area_factor_b,
max_monitor_area,
})
}
}
/// [2.2.2.2] DISPLAYCONTROL_MONITOR_LAYOUT_PDU
///
/// Sent from client to server to notify about new monitor layout (e.g screen resize).
///
/// INVARIANTS:
/// 0 <= monitors.length() <= MAX_SUPPORTED_MONITORS
///
/// [2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/22741217-12a0-4fb8-b5a0-df43905aaf06
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DisplayControlMonitorLayout {
monitors: Vec<MonitorLayoutEntry>,
}
impl DisplayControlMonitorLayout {
const NAME: &'static str = "DISPLAYCONTROL_MONITOR_LAYOUT_PDU";
const FIXED_PART_SIZE: usize = 4 /* MonitorLayoutSize */ + 4 /* NumMonitors */;
pub fn new(monitors: &[MonitorLayoutEntry]) -> EncodeResult<Self> {
if monitors.len() > MAX_SUPPORTED_MONITORS.into() {
return Err(invalid_field_err!("NumMonitors", "Too many monitors",));
}
let primary_monitors_count = monitors.iter().filter(|monitor| monitor.is_primary()).count();
if primary_monitors_count != 1 {
return Err(invalid_field_err!(
"PrimaryMonitor",
"There must be exactly one primary monitor"
));
}
Ok(Self {
monitors: monitors.to_vec(),
})
}
/// Creates a new [`DisplayControlMonitorLayout`] with a single primary monitor
///
/// Per [2.2.2.2.1]:
/// - The `width` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels, and MUST NOT be an odd value.
/// - The `height` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels.
/// - The `scale_factor` MUST be ignored if it is less than 100 percent or greater than 500 percent.
/// - The `physical_dims` (width, height) MUST be ignored if either is less than 10 mm or greater than 10,000 mm.
///
/// Use [`MonitorLayoutEntry::adjust_display_size`] to adjust `width` and `height` before calling this function
/// to ensure the display size is within the valid range.
///
/// [2.2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/ea2de591-9203-42cd-9908-be7a55237d1c
pub fn new_single_primary_monitor(
width: u32,
height: u32,
scale_factor: Option<u32>,
physical_dims: Option<(u32, u32)>,
) -> EncodeResult<Self> {
let entry = MonitorLayoutEntry::new_primary(width, height)?.with_orientation(if width > height {
MonitorOrientation::Landscape
} else {
MonitorOrientation::Portrait
});
let entry = if let Some(scale_factor) = scale_factor {
entry
.with_desktop_scale_factor(scale_factor)?
.with_device_scale_factor(DeviceScaleFactor::Scale100Percent)
} else {
entry
};
let entry = if let Some((physical_width, physical_height)) = physical_dims {
entry.with_physical_dimensions(physical_width, physical_height)?
} else {
entry
};
Ok(DisplayControlMonitorLayout::new(&[entry]).unwrap())
}
pub fn monitors(&self) -> &[MonitorLayoutEntry] {
&self.monitors
}
}
impl Encode for DisplayControlMonitorLayout {
fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
ensure_fixed_part_size!(in: dst);
dst.write_u32(MonitorLayoutEntry::FIXED_PART_SIZE.try_into().unwrap());
let monitors_count: u32 = self
.monitors
.len()
.try_into()
.map_err(|_| invalid_field_err!("NumMonitors", "Number of monitors is too big"))?;
dst.write_u32(monitors_count);
for monitor in &self.monitors {
monitor.encode(dst)?;
}
Ok(())
}
fn name(&self) -> &'static str {
Self::NAME
}
fn size(&self) -> usize {
// As per invariants: This will never overflow:
// 0 <= Self::FIXED_PART_SIZE + MAX_SUPPORTED_MONITORS * MonitorLayoutEntry::FIXED_PART_SIZE < u16::MAX
#[allow(clippy::arithmetic_side_effects)]
let size = Self::FIXED_PART_SIZE + self.monitors.iter().map(|monitor| monitor.size()).sum::<usize>();
size
}
}
impl<'de> Decode<'de> for DisplayControlMonitorLayout {
fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
ensure_fixed_part_size!(in: src);
let monitor_layout_size = src.read_u32();
if monitor_layout_size != MonitorLayoutEntry::FIXED_PART_SIZE.try_into().unwrap() {
return Err(invalid_field_err!(
"MonitorLayoutSize",
"Monitor layout size is invalid"
));
}
let num_monitors = src.read_u32();
if num_monitors > MAX_SUPPORTED_MONITORS.into() {
return Err(invalid_field_err!("NumMonitors", "Too many monitors"));
}
let mut monitors = Vec::with_capacity(usize::try_from(num_monitors).unwrap());
for _ in 0..num_monitors {
let monitor = MonitorLayoutEntry::decode(src)?;
monitors.push(monitor);
}
Ok(Self { monitors })
}
}
/// [2.2.2.2.1] DISPLAYCONTROL_MONITOR_LAYOUT_PDU
///
/// [2.2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/ea2de591-9203-42cd-9908-be7a55237d1c
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MonitorLayoutEntry {
is_primary: bool,
left: i32,
top: i32,
width: u32,
height: u32,
physical_width: u32,
physical_height: u32,
orientation: u32,
desktop_scale_factor: u32,
device_scale_factor: u32,
}
macro_rules! validate_dimensions {
($width:expr, $height:expr) => {{
if !(200..=8192).contains(&$width) {
return Err(invalid_field_err!("Width", "Monitor width is out of range"));
}
if $width % 2 != 0 {
return Err(invalid_field_err!("Width", "Monitor width cannot be odd"));
}
if !(200..=8192).contains(&$height) {
return Err(invalid_field_err!("Height", "Monitor height is out of range"));
}
Ok(())
}};
}
impl MonitorLayoutEntry {
const FIXED_PART_SIZE: usize = 4 /* Flags */
+ 4 /* Left */
+ 4 /* Top */
+ 4 /* Width */
+ 4 /* Height */
+ 4 /* PhysicalWidth */
+ 4 /* PhysicalHeight */
+ 4 /* Orientation */
+ 4 /* DesktopScaleFactor */
+ 4 /* DeviceScaleFactor */;
const NAME: &'static str = "DISPLAYCONTROL_MONITOR_LAYOUT";
/// Creates a new [`MonitorLayoutEntry`].
///
/// Per [2.2.2.2.1]:
/// - The `width` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels, and MUST NOT be an odd value.
/// - The `height` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels.
///
/// [2.2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/ea2de591-9203-42cd-9908-be7a55237d1c
fn new_impl(mut width: u32, height: u32) -> EncodeResult<Self> {
if width % 2 != 0 {
let prev_width = width;
width = width.saturating_sub(1);
warn!(
"Monitor width cannot be odd, adjusting from [{}] to [{}]",
prev_width, width
)
}
validate_dimensions!(width, height)?;
Ok(Self {
is_primary: false,
left: 0,
top: 0,
width,
height,
physical_width: 0,
physical_height: 0,
orientation: 0,
desktop_scale_factor: 0,
device_scale_factor: 0,
})
}
/// Adjusts the display size to be within the valid range.
///
/// Per [2.2.2.2.1]:
/// - The `width` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels, and MUST NOT be an odd value.
/// - The `height` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels.
///
/// Functions that create [`MonitorLayoutEntry`] should typically use this function to adjust the display size first.
///
/// [2.2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/ea2de591-9203-42cd-9908-be7a55237d1c
pub fn adjust_display_size(width: u32, height: u32) -> (u32, u32) {
fn constrain(value: u32) -> u32 {
value.clamp(200, 8192)
}
let mut width = width;
if width % 2 != 0 {
width = width.saturating_sub(1);
}
(constrain(width), constrain(height))
}
/// Creates a new primary [`MonitorLayoutEntry`].
///
/// Per [2.2.2.2.1]:
/// - The `width` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels, and MUST NOT be an odd value.
/// - The `height` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels.
///
/// Use [`MonitorLayoutEntry::adjust_display_size`] before calling this function to ensure the display size is within the valid range.
///
/// [2.2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/ea2de591-9203-42cd-9908-be7a55237d1c
pub fn new_primary(width: u32, height: u32) -> EncodeResult<Self> {
let mut entry = Self::new_impl(width, height)?;
entry.is_primary = true;
Ok(entry)
}
/// Creates a new primary [`MonitorLayoutEntry`].
///
/// Per [2.2.2.2.1]:
/// - The `width` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels, and MUST NOT be an odd value.
/// - The `height` MUST be greater than or equal to 200 pixels and less than or equal to 8192 pixels.
///
/// Use [`MonitorLayoutEntry::adjust_display_size`] before calling this function to ensure the display size is within the valid range.
///
/// [2.2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpedisp/ea2de591-9203-42cd-9908-be7a55237d1c
pub fn new_secondary(width: u32, height: u32) -> EncodeResult<Self> {
Self::new_impl(width, height)
}
/// Sets the monitor's orientation. (Default is [`MonitorOrientation::Landscape`])
#[must_use]
pub fn with_orientation(mut self, orientation: MonitorOrientation) -> Self {
self.orientation = orientation.angle();
self
}
/// Sets the monitor's position (left, top) in pixels. (Default is (0, 0))
///
/// Note: The primary monitor position must be always (0, 0).
pub fn with_position(mut self, left: i32, top: i32) -> EncodeResult<Self> {
validate_position(left, top, self.is_primary)?;
self.left = left;
self.top = top;
Ok(self)
}
/// Sets the monitor's device scale factor in percent. (Default is [`DeviceScaleFactor::Scale100Percent`])
#[must_use]
pub fn with_device_scale_factor(mut self, device_scale_factor: DeviceScaleFactor) -> Self {
self.device_scale_factor = device_scale_factor.value();
self
}
/// Sets the monitor's desktop scale factor in percent.
///
/// NOTE: As specified in [MS-RDPEDISP], if the desktop scale factor is not in the valid range
/// (100..=500 percent), the monitor desktop scale factor is considered invalid and should be ignored.
pub fn with_desktop_scale_factor(mut self, desktop_scale_factor: u32) -> EncodeResult<Self> {
validate_desktop_scale_factor(desktop_scale_factor)?;
self.desktop_scale_factor = desktop_scale_factor;
Ok(self)
}
/// Sets the monitor's physical dimensions in millimeters.
///
/// NOTE: As specified in [MS-RDPEDISP], if the physical dimensions are not in the valid range
/// (10..=10000 millimeters), the monitor physical dimensions are considered invalid and
/// should be ignored.
pub fn with_physical_dimensions(mut self, physical_width: u32, physical_height: u32) -> EncodeResult<Self> {
validate_physical_dimensions(physical_width, physical_height)?;
self.physical_width = physical_width;
self.physical_height = physical_height;
Ok(self)
}
pub fn is_primary(&self) -> bool {
self.is_primary
}
/// Returns the monitor's position (left, top) in pixels.
pub fn position(&self) -> Option<(i32, i32)> {
validate_position(self.left, self.top, self.is_primary).ok()?;
Some((self.left, self.top))
}
/// Returns the monitor's dimensions (width, height) in pixels.
pub fn dimensions(&self) -> (u32, u32) {
(self.width, self.height)
}
/// Returns the monitor's orientation if it is valid.
///
/// NOTE: As specified in [MS-RDPEDISP], if the orientation is not one of the valid values
/// (0, 90, 180, 270), the monitor orientation is considered invalid and should be ignored.
pub fn orientation(&self) -> Option<MonitorOrientation> {
MonitorOrientation::from_angle(self.orientation)
}
/// Returns the monitor's physical dimensions (width, height) in millimeters.
///
/// NOTE: As specified in [MS-RDPEDISP], if the physical dimensions are not in the valid range
/// (10..=10000 millimeters), the monitor physical dimensions are considered invalid and
/// should be ignored.
pub fn physical_dimensions(&self) -> Option<(u32, u32)> {
validate_physical_dimensions(self.physical_width, self.physical_height).ok()?;
Some((self.physical_width, self.physical_height))
}
/// Returns the monitor's device scale factor in percent if it is valid.
///
/// NOTE: As specified in [MS-RDPEDISP], if the desktop scale factor is not in the valid range
/// (100..=500 percent), the monitor desktop scale factor is considered invalid and should be ignored.
///
/// IMPORTANT: When processing scale factors, make sure that both desktop and device scale factors
/// are valid, otherwise they both should be ignored.
pub fn desktop_scale_factor(&self) -> Option<u32> {
validate_desktop_scale_factor(self.desktop_scale_factor).ok()?;
Some(self.desktop_scale_factor)
}
/// Returns the monitor's device scale factor in percent if it is valid.
///
/// IMPORTANT: When processing scale factors, make sure that both desktop and device scale factors
/// are valid, otherwise they both should be ignored.
pub fn device_scale_factor(&self) -> Option<DeviceScaleFactor> {
match self.device_scale_factor {
100 => Some(DeviceScaleFactor::Scale100Percent),
140 => Some(DeviceScaleFactor::Scale140Percent),
180 => Some(DeviceScaleFactor::Scale180Percent),
_ => None,
}
}
}
impl Encode for MonitorLayoutEntry {
fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
ensure_fixed_part_size!(in: dst);
let flags = if self.is_primary {
DISPLAYCONTROL_MONITOR_PRIMARY
} else {
0
};
dst.write_u32(flags);
dst.write_i32(self.left);
dst.write_i32(self.top);
dst.write_u32(self.width);
dst.write_u32(self.height);
dst.write_u32(self.physical_width);
dst.write_u32(self.physical_height);
dst.write_u32(self.orientation);
dst.write_u32(self.desktop_scale_factor);
dst.write_u32(self.device_scale_factor);
Ok(())
}
fn name(&self) -> &'static str {
Self::NAME
}
fn size(&self) -> usize {
Self::FIXED_PART_SIZE
}
}
impl<'de> Decode<'de> for MonitorLayoutEntry {
fn decode(src: &mut ReadCursor<'de>) -> DecodeResult<Self> {
ensure_fixed_part_size!(in: src);
let flags = src.read_u32();
let left = src.read_i32();
let top = src.read_i32();
let width = src.read_u32();
let height = src.read_u32();
let physical_width = src.read_u32();
let physical_height = src.read_u32();
let orientation = src.read_u32();
let desktop_scale_factor = src.read_u32();
let device_scale_factor = src.read_u32();
validate_dimensions!(width, height)?;
Ok(Self {
is_primary: flags & DISPLAYCONTROL_MONITOR_PRIMARY != 0,
left,
top,
width,
height,
physical_width,
physical_height,
orientation,
desktop_scale_factor,
device_scale_factor,
})
}
}
/// Valid monitor orientations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MonitorOrientation {
Landscape,
Portrait,
LandscapeFlipped,
PortraitFlipped,
}
impl MonitorOrientation {
pub fn from_angle(angle: u32) -> Option<Self> {
match angle {
0 => Some(Self::Landscape),
90 => Some(Self::Portrait),
180 => Some(Self::LandscapeFlipped),
270 => Some(Self::PortraitFlipped),
_ => None,
}
}
pub fn angle(&self) -> u32 {
match self {
Self::Landscape => 0,
Self::Portrait => 90,
Self::LandscapeFlipped => 180,
Self::PortraitFlipped => 270,
}
}
}
/// Valid device scale factors for monitors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceScaleFactor {
Scale100Percent,
Scale140Percent,
Scale180Percent,
}
impl DeviceScaleFactor {
pub fn value(&self) -> u32 {
match self {
Self::Scale100Percent => 100,
Self::Scale140Percent => 140,
Self::Scale180Percent => 180,
}
}
}
fn validate_position(left: i32, top: i32, is_primary: bool) -> EncodeResult<()> {
if is_primary && (left != 0 || top != 0) {
return Err(invalid_field_err!(
"Position",
"Primary monitor position must be (0, 0)"
));
}
Ok(())
}
fn validate_desktop_scale_factor(desktop_scale_factor: u32) -> EncodeResult<()> {
if !(100..=500).contains(&desktop_scale_factor) {
return Err(invalid_field_err!(
"DesktopScaleFactor",
"Desktop scale factor is out of range"
));
}
Ok(())
}
fn validate_physical_dimensions(physical_width: u32, physical_height: u32) -> EncodeResult<()> {
if !(10..=10000).contains(&physical_width) {
return Err(invalid_field_err!("PhysicalWidth", "Physical width is out of range"));
}
if !(10..=10000).contains(&physical_height) {
return Err(invalid_field_err!("PhysicalHeight", "Physical height is out of range"));
}
Ok(())
}
fn calculate_monitor_area(
max_num_monitors: u32,
max_monitor_area_factor_a: u32,
max_monitor_area_factor_b: u32,
) -> DecodeResult<u64> {
if max_num_monitors > MAX_SUPPORTED_MONITORS.into() {
return Err(invalid_field_err!("NumMonitors", "Too many monitors"));
}
if max_monitor_area_factor_a > MAX_MONITOR_AREA_FACTOR.into()
|| max_monitor_area_factor_b > MAX_MONITOR_AREA_FACTOR.into()
{
return Err(invalid_field_err!(
"MaxMonitorAreaFactor",
"Invalid monitor area factor"
));
}
// As per invariants: This multiplication would never overflow.
// 0 <= MAX_MONITOR_AREA_FACTOR * MAX_MONITOR_AREA_FACTOR * MAX_SUPPORTED_MONITORS <= u64::MAX
#[allow(clippy::arithmetic_side_effects)]
Ok(u64::from(max_monitor_area_factor_a) * u64::from(max_monitor_area_factor_b) * u64::from(max_num_monitors))
}