Skip to content

Commit

Permalink
Low Latency 🔥
Browse files Browse the repository at this point in the history
	modified:   src/graphics_capture_api.rs
	modified:   src/monitor.rs
	modified:   src/window.rs
  • Loading branch information
NiiightmareXD committed Dec 14, 2023
1 parent 6eee616 commit 2b3eb08
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/graphics_capture_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl GraphicsCaptureApi {
// Create Frame Pool
trace!("Creating Frame Pool");
let frame_pool =
Direct3D11CaptureFramePool::Create(&direct3d_device, pixel_format, 2, item.Size()?)?;
Direct3D11CaptureFramePool::Create(&direct3d_device, pixel_format, 1, item.Size()?)?;
let frame_pool = Arc::new(frame_pool);

// Create Capture Session
Expand Down Expand Up @@ -211,7 +211,7 @@ impl GraphicsCaptureApi {
.Recreate(
&direct3d_device_recreate.0,
pixel_format,
2,
1,
frame_content_size,
)
.unwrap();
Expand Down
82 changes: 74 additions & 8 deletions src/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{mem, num::ParseIntError, ptr, string::FromUtf16Error};

use windows::{
core::PCWSTR,
core::{HSTRING, PCWSTR},
Graphics::Capture::GraphicsCaptureItem,
Win32::{
Devices::Display::{
Expand All @@ -14,8 +14,9 @@ use windows::{
},
Foundation::{BOOL, LPARAM, POINT, RECT, TRUE},
Graphics::Gdi::{
EnumDisplayDevicesW, EnumDisplayMonitors, GetMonitorInfoW, MonitorFromPoint,
DISPLAY_DEVICEW, HDC, HMONITOR, MONITORINFO, MONITORINFOEXW, MONITOR_DEFAULTTOPRIMARY,
EnumDisplayDevicesW, EnumDisplayMonitors, EnumDisplaySettingsW, GetMonitorInfoW,
MonitorFromPoint, DEVMODEW, DISPLAY_DEVICEW, ENUM_CURRENT_SETTINGS, HDC, HMONITOR,
MONITORINFO, MONITORINFOEXW, MONITOR_DEFAULTTONULL,
},
System::WinRT::Graphics::Capture::IGraphicsCaptureItemInterop,
},
Expand All @@ -32,6 +33,8 @@ pub enum Error {
IndexIsLowerThanOne,
#[error("Failed To Get Monitor Info")]
FailedToGetMonitorInfo,
#[error("Failed To Get Monitor ettings")]
FailedToGetMonitorSettings,
#[error("Failed To Get Monitor Name")]
FailedToGetMonitorName,
#[error(transparent)]
Expand All @@ -52,7 +55,7 @@ impl Monitor {
/// Get The Primary Monitor
pub fn primary() -> Result<Self, Error> {
let point = POINT { x: 0, y: 0 };
let monitor = unsafe { MonitorFromPoint(point, MONITOR_DEFAULTTOPRIMARY) };
let monitor = unsafe { MonitorFromPoint(point, MONITOR_DEFAULTTONULL) };

if monitor.is_invalid() {
return Err(Error::NotFound);
Expand Down Expand Up @@ -145,7 +148,7 @@ impl Monitor {
.iter()
.take_while(|ch| **ch != 0x0000)
.copied()
.collect::<Vec<_>>(),
.collect::<Vec<u16>>(),
)?;

if unsafe { DisplayConfigGetDeviceInfo(&mut source.header) } == 0
Expand Down Expand Up @@ -176,7 +179,7 @@ impl Monitor {
.iter()
.take_while(|ch| **ch != 0x0000)
.copied()
.collect::<Vec<_>>(),
.collect::<Vec<u16>>(),
)?;
return Ok(name);
}
Expand Down Expand Up @@ -216,7 +219,7 @@ impl Monitor {
.iter()
.take_while(|ch| **ch != 0x0000)
.copied()
.collect::<Vec<_>>(),
.collect::<Vec<u16>>(),
)?;

Ok(device_name)
Expand Down Expand Up @@ -271,12 +274,75 @@ impl Monitor {
.iter()
.take_while(|ch| **ch != 0x0000)
.copied()
.collect::<Vec<_>>(),
.collect::<Vec<u16>>(),
)?;

Ok(device_string)
}

/// Get Monitor Width
pub fn width(&self) -> Result<u32, Error> {
let mut device_mode = DEVMODEW {
dmSize: u16::try_from(mem::size_of::<DEVMODEW>()).unwrap(),
..DEVMODEW::default()
};
let name = HSTRING::from(self.device_name()?);
if unsafe {
!EnumDisplaySettingsW(
PCWSTR(name.as_ptr()),
ENUM_CURRENT_SETTINGS,
&mut device_mode,
)
.as_bool()
} {
return Err(Error::FailedToGetMonitorSettings);
}

Ok(device_mode.dmPelsWidth)
}

/// Get Monitor Height
pub fn height(&self) -> Result<u32, Error> {
let mut device_mode = DEVMODEW {
dmSize: u16::try_from(mem::size_of::<DEVMODEW>()).unwrap(),
..DEVMODEW::default()
};
let name = HSTRING::from(self.device_name()?);
if unsafe {
!EnumDisplaySettingsW(
PCWSTR(name.as_ptr()),
ENUM_CURRENT_SETTINGS,
&mut device_mode,
)
.as_bool()
} {
return Err(Error::FailedToGetMonitorSettings);
}

Ok(device_mode.dmPelsHeight)
}

/// Get Monitor Refresh Rate
pub fn refresh_rate(&self) -> Result<u32, Error> {
let mut device_mode = DEVMODEW {
dmSize: u16::try_from(mem::size_of::<DEVMODEW>()).unwrap(),
..DEVMODEW::default()
};
let name = HSTRING::from(self.device_name()?);
if unsafe {
!EnumDisplaySettingsW(
PCWSTR(name.as_ptr()),
ENUM_CURRENT_SETTINGS,
&mut device_mode,
)
.as_bool()
} {
return Err(Error::FailedToGetMonitorSettings);
}

Ok(device_mode.dmDisplayFrequency)
}

/// Get A List Of All Monitors
pub fn enumerate() -> Result<Vec<Self>, Error> {
let mut monitors: Vec<Self> = Vec::new();
Expand Down
17 changes: 17 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use windows::{
Graphics::Capture::GraphicsCaptureItem,
Win32::{
Foundation::{BOOL, HWND, LPARAM, RECT, TRUE},
Graphics::Gdi::{MonitorFromWindow, MONITOR_DEFAULTTONULL},
System::{
Threading::GetCurrentProcessId, WinRT::Graphics::Capture::IGraphicsCaptureItemInterop,
},
Expand All @@ -17,6 +18,8 @@ use windows::{
},
};

use crate::monitor::Monitor;

/// Used To Handle Window Errors
#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -99,6 +102,20 @@ impl Window {
Ok(name)
}

/// Get The Monitor That Has The Largest Area Of Intersection With The Window, None Means Windows Doesn't Intersect With Any Monitor
#[must_use]
pub fn monitor(&self) -> Option<Monitor> {
let window = self.window;

let monitor = unsafe { MonitorFromWindow(window, MONITOR_DEFAULTTONULL) };

if monitor.is_invalid() {
None
} else {
Some(Monitor::from_raw_hmonitor(monitor))
}
}

/// Check If The Window Is A Valid Window
#[must_use]
pub fn is_window_valid(window: HWND) -> bool {
Expand Down

0 comments on commit 2b3eb08

Please sign in to comment.