Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add display frequency #26

Merged
merged 7 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "display-info"
version = "0.4.3"
version = "0.4.4"
edition = "2021"
description = "Cross-platform get display info"
license-file = "LICENSE"
Expand All @@ -19,10 +19,11 @@ core-graphics = "0.23"
[target.'cfg(target_os = "windows")'.dependencies]
fxhash = "0.2"
widestring = "1.0"
windows = { version = "0.48", features = [
windows = { version = "0.52", features = [
"Win32_Foundation",
"Win32_Graphics_Gdi",
"Win32_UI_HiDpi",
] }

[target.'cfg(target_os = "linux")'.dependencies]
xcb = { version = "1.2", features = ["randr"] }
xcb = { version = "1.3", features = ["randr"] }
94 changes: 56 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
# display-info

Cross-platform get display info for MacOS、Windows、Linux. Like [electron Display Object](https://www.electronjs.org/docs/latest/api/structures/display)

## example

```rust
use display_info::DisplayInfo;
use std::time::Instant;

fn main() {
let start = Instant::now();

let display_infos = DisplayInfo::all().unwrap();
for display_info in display_infos {
println!("display_info {display_info:?}");
}
let display_info = DisplayInfo::from_point(100, 100).unwrap();
println!("display_info {display_info:?}");
println!("运行耗时: {:?}", start.elapsed());
}
```

## Linux requirements

On Linux, you need to install `libxcb`、`libxrandr`

Debian/Ubuntu:

```sh
apt-get install libxcb1 libxrandr2
```

Alpine:

```sh
apk add libxcb libxrandr
```
# display-info

Cross-platform get display info for MacOS、Windows、Linux. Like [electron Display Object](https://www.electronjs.org/docs/latest/api/structures/display)

## Example

```rust
use display_info::DisplayInfo;
use std::time::Instant;

fn main() {
let start = Instant::now();

let display_infos = DisplayInfo::all().unwrap();
for display_info in display_infos {
println!("display_info {display_info:?}");
}
let display_info = DisplayInfo::from_point(100, 100).unwrap();
println!("display_info {display_info:?}");
println!("运行耗时: {:?}", start.elapsed());
}
```

## DisplayInfo struct

- `id` u32 - Unique identifier associated with the display.
- `x` i32 - The display x coordinate.
- `y` i32 - The display y coordinate.
- `width` u32 - The display pixel width.
- `height` u32 - The display pixel height.
- `rotation` f32 - Can be 0, 90, 180, 270, represents screen rotation in clock-wise degrees.
- `scale_factor` f32 - Output device's pixel scale factor.
- `frequency` f32 - The display refresh rate.
- `is_primary` bool - Whether the screen is the main screen

## Linux requirements

On Linux, you need to install `libxcb`、`libxrandr`

Debian/Ubuntu:

```sh
apt-get install libxcb1 libxrandr2
```

Alpine:

```sh
apk add libxcb libxrandr
```

ArchLinux:

```sh
pacman -S libxcb libxrandr
```
9 changes: 6 additions & 3 deletions src/darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ impl DisplayInfo {
let CGRect { origin, size } = cg_display.bounds();

let rotation = cg_display.rotation() as f32;
let scale_factor = cg_display
let (scale_factor, frequency) = cg_display
.display_mode()
.map(|display_mode| {
let pixel_width = display_mode.pixel_width();
let scale_factor = pixel_width as f32 / size.width as f32;
let refresh_rate = display_mode.refresh_rate() as f32;

(pixel_width as f32) / size.width as f32
(scale_factor, refresh_rate)
})
.unwrap_or(1.0);
.unwrap_or((1.0, 0.0));

DisplayInfo {
id,
Expand All @@ -24,6 +26,7 @@ impl DisplayInfo {
width: size.width as u32,
height: size.height as u32,
rotation,
frequency,
scale_factor,
is_primary: cg_display.is_main(),
}
Expand Down
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
//! # example
//! Get all display info
//! ```
//! use display_info::DisplayInfo;
//! use std::time::Instant;
//!
//! fn main() {
//! let start = Instant::now();
//!
//! let display_infos = DisplayInfo::all().unwrap();
//! for display_info in display_infos {
//! println!("display_info {display_info:?}");
//! }
//! let display_info = DisplayInfo::from_point(100, 100).unwrap();
//! println!("display_info {display_info:?}");
//! println!("运行耗时: {:?}", start.elapsed());
//! }
//! ```

use anyhow::Result;

#[cfg(target_os = "macos")]
Expand All @@ -17,13 +36,23 @@ use linux::*;

#[derive(Debug, Clone, Copy)]
pub struct DisplayInfo {
/// Unique identifier associated with the display.
pub id: u32,
/// The display x coordinate.
pub x: i32,
/// The display x coordinate.
pub y: i32,
/// The display pixel width.
pub width: u32,
/// The display pixel height.
pub height: u32,
/// Can be 0, 90, 180, 270, represents screen rotation in clock-wise degrees.
pub rotation: f32,
/// Output device's pixel scale factor.
pub scale_factor: f32,
/// The display refresh rate.
pub frequency: f32,
/// Whether the screen is the main screen
pub is_primary: bool,
}

Expand Down
63 changes: 58 additions & 5 deletions src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ use crate::DisplayInfo;
use anyhow::{anyhow, Result};
use std::str;
use xcb::{
randr::{GetCrtcInfo, GetMonitors, GetOutputInfo, MonitorInfo, Output, Rotation},
randr::{
GetCrtcInfo, GetMonitors, GetOutputInfo, GetScreenResources, Mode, ModeFlag, ModeInfo,
MonitorInfo, Output, Rotation,
},
x::{GetProperty, Screen, ATOM_RESOURCE_MANAGER, ATOM_STRING},
Connection, Xid,
};

impl DisplayInfo {
fn new(monitor_info: &MonitorInfo, output: &Output, rotation: f32, scale_factor: f32) -> Self {
fn new(
monitor_info: &MonitorInfo,
output: &Output,
rotation: f32,
scale_factor: f32,
frequency: f32,
) -> Self {
DisplayInfo {
id: output.resource_id(),
x: ((monitor_info.x() as f32) / scale_factor) as i32,
Expand All @@ -17,11 +26,37 @@ impl DisplayInfo {
height: ((monitor_info.height() as f32) / scale_factor) as u32,
rotation,
scale_factor,
frequency,
is_primary: monitor_info.primary(),
}
}
}

// per https://gitlab.freedesktop.org/xorg/app/xrandr/-/blob/master/xrandr.c#L576
fn get_current_frequency(mode_infos: &[ModeInfo], mode: Mode) -> f32 {
let mode_info = match mode_infos.iter().find(|m| m.id == mode.resource_id()) {
Some(mode_info) => mode_info,
None => return 0.0,
};

let vtotal = {
let mut val = mode_info.vtotal;
if mode_info.mode_flags.contains(ModeFlag::DOUBLE_SCAN) {
val *= 2;
}
if mode_info.mode_flags.contains(ModeFlag::INTERLACE) {
val /= 2;
}
val
};

if vtotal != 0 && mode_info.htotal != 0 {
(mode_info.dot_clock as f32) / (vtotal as f32 * mode_info.htotal as f32)
} else {
0.0
}
}

fn get_scale_factor(conn: &Connection, screen: &Screen) -> Result<f32> {
let xft_dpi_prefix = "Xft.dpi:\t";

Expand Down Expand Up @@ -50,7 +85,11 @@ fn get_scale_factor(conn: &Connection, screen: &Screen) -> Result<f32> {
Ok(dpi / 96.0)
}

fn get_rotation(conn: &Connection, output: &Output) -> Result<f32> {
fn get_rotation_frequency(
conn: &Connection,
mode_infos: &[ModeInfo],
output: &Output,
) -> Result<(f32, f32)> {
let get_output_info_cookie = conn.send_request(&GetOutputInfo {
output: *output,
config_timestamp: 0,
Expand All @@ -65,6 +104,8 @@ fn get_rotation(conn: &Connection, output: &Output) -> Result<f32> {

let get_crtc_info_reply = conn.wait_for_reply(get_crtc_info_cookie)?;

let mode = get_crtc_info_reply.mode();

let rotation = match get_crtc_info_reply.rotation() {
Rotation::ROTATE_0 => 0.0,
Rotation::ROTATE_90 => 90.0,
Expand All @@ -73,7 +114,9 @@ fn get_rotation(conn: &Connection, output: &Output) -> Result<f32> {
_ => 0.0,
};

Ok(rotation)
let frequency = get_current_frequency(mode_infos, mode);

Ok((rotation, frequency))
}

pub fn get_all() -> Result<Vec<DisplayInfo>> {
Expand All @@ -97,6 +140,14 @@ pub fn get_all() -> Result<Vec<DisplayInfo>> {

let monitor_info_iterator = get_monitors_reply.monitors();

let get_screen_resources_cookie = conn.send_request(&GetScreenResources {
window: screen.root(),
});

let get_screen_resources_reply = conn.wait_for_reply(get_screen_resources_cookie)?;

let mode_infos = get_screen_resources_reply.modes();

let mut display_infos = Vec::new();

for monitor_info in monitor_info_iterator {
Expand All @@ -105,13 +156,15 @@ pub fn get_all() -> Result<Vec<DisplayInfo>> {
.get(0)
.ok_or_else(|| anyhow!("Not found output"))?;

let rotation = get_rotation(&conn, output).unwrap_or(0.0);
let (rotation, frequency) =
get_rotation_frequency(&conn, mode_infos, output).unwrap_or((0.0, 0.0));

display_infos.push(DisplayInfo::new(
monitor_info,
output,
rotation,
scale_factor,
frequency,
));
}

Expand Down
Loading
Loading