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

Initial refactoring of displayz #3

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4e46aea
Initial refactoring of `displayz`
shymega Jun 6, 2023
cc27b10
Improve documentation on `common` module
shymega Jun 6, 2023
484d20b
Fix #[cfg] attributes on Linux platform
shymega Jun 6, 2023
71530f5
Fix self-referencing import on Windows platform
shymega Jun 6, 2023
4d48493
Remove `deny` lint - as per review
shymega Jun 6, 2023
5fe124c
Rename `get_resolutions` to be more clear
shymega Jun 6, 2023
76b96d7
Change Resolution/Position types to struct
shymega Jun 6, 2023
2f7d397
Add Linux Wayland/X11 feature flags and optional deps
shymega Jun 6, 2023
73dc598
Clarify `get_resolution` method in `DisplayOutput` trait
shymega Jun 6, 2023
c6d2463
Bump Cargo.lock
shymega Jun 6, 2023
b5da993
Rename `linux` to `unix` to support BSDs/Linux
shymega Jun 6, 2023
d42f95f
Change Wayland dep target to `unix` target_family
shymega Jun 6, 2023
21aac60
Add initial struct for `WaylandBackend`
shymega Jun 6, 2023
364433a
Add note to `Resolutions` type about future use
shymega Jun 7, 2023
5129c31
Remove `is_focused` - move to platform-specific extensions
shymega Jun 7, 2023
f3a7e5a
Wrap `get_edid` in an `Option<T>`
shymega Jun 7, 2023
3d1318a
Run `cargo-fmt` on codebase
shymega Jun 7, 2023
657ce27
Revert "Add note to `Resolutions` type about future use"
shymega Jun 23, 2023
14871ba
Add Wayland protocols for Plasma/KDE
shymega Sep 24, 2023
6e37517
refactor: Refactor types into structs, impl Debug/Display
shymega Jun 5, 2024
7f1aa2f
refactor: Change `Display` type into `dyn` trait, for generics
shymega Jun 5, 2024
e62b7dc
chore: Remove Windows CLI
shymega Jun 5, 2024
c81f8dc
chore: bump Cargo.lock
shymega Jun 5, 2024
4d3a92f
fix: Fix Derives in `common/mod.rs`
shymega Jun 5, 2024
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
579 changes: 511 additions & 68 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ license = "MIT"
keywords = ["display", "settings", "cli"]
categories = ["command-line-utilities", "config"]

[features]
default = []
unix-wayland = [ "wayland-client", "wayland-protocols-wlr", "wayland-protocols-plasma" ]
unix-x11 = []

[dependencies]
color-eyre = "0.6.1"
env_logger = "0.9.0"
log = "0.4.17"
color-eyre = "0.6.2"
env_logger = "0.10.0"
log = "0.4.18"
structopt = "0.3.26"
thiserror = "1.0.31"
winsafe = { version = "0.0.10", features = ["user"] }
thiserror = "1.0.40"

[target.'cfg(target_os = "windows")'.dependencies]
winsafe = { version = "0.0.16", features = ["user"] }

[target.'cfg(target_family = "unix")'.dependencies]
wayland-client = { version = "=0.30.0", features = ["log", "calloop"], optional = true }
wayland-protocols-wlr = { version = "=0.1.0", features = ["client"], optional = true }
wayland-protocols-plasma = { version = "=0.2.0", features = ["client"], optional = true }
5 changes: 5 additions & 0 deletions examples/find.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(windows)]
use displayz::query_displays;

/// Finds displays by name and index
#[cfg(windows)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let display_set = query_displays()?;
println!("Discovered displays:\n{}", display_set);
Expand Down Expand Up @@ -32,3 +34,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[cfg(not(windows))]
fn main() {}
5 changes: 5 additions & 0 deletions examples/primary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(windows)]
use displayz::{query_displays, refresh};

/// Sets a display to be the new primary display
#[cfg(windows)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let display_set = query_displays()?;
println!("Discovered displays:\n{}", display_set);
Expand All @@ -22,3 +24,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[cfg(not(windows))]
fn main() {}
5 changes: 5 additions & 0 deletions examples/resolution.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(windows)]
use displayz::{query_displays, refresh, Resolution};

/// Prints and changes the current resolution of the primary display
#[cfg(windows)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let display_set = query_displays()?;
println!("Discovered displays:\n{}", display_set);
Expand All @@ -25,3 +27,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[cfg(not(windows))]
fn main() {}
5 changes: 5 additions & 0 deletions examples/upside-down.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(windows)]
use displayz::{query_displays, refresh, Orientation};

/// Turns the primary display upside-down
#[cfg(windows)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let display_set = query_displays()?;
println!("Discovered displays:\n{}", display_set);
Expand Down Expand Up @@ -28,3 +30,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

Ok(())
}

#[cfg(not(windows))]
fn main() {}
76 changes: 76 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Common helpers for `displayz`

use std::fmt::{self, Debug, Display, Formatter};

/// Height and Width of a Display (`i32`)
#[derive(Default, Clone)]
pub struct Resolution(i32, i32);

/// X/Y positions of a display.
#[derive(Default, Clone)]
pub struct Position(i32, i32);

/// `Vec` type of the `Display` type, exposed on a platform-dependent basis.
pub type Displays = Vec<crate::Display>;

/// `Vec` type of the `Resolution` type, generally exposing a collection of available resolutions.
#[derive(Default, Clone)]
pub struct Resolutions(Vec<Resolution>);

impl Display for Resolution {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}x{}", self.0, self.1)
}
}

impl Debug for Resolution {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Resolution of Display is: {}x{}", self.0, self.1)
}
}

impl Debug for Resolutions {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.0.iter())
.finish()
}
}

impl Display for Position {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{},{}", self.0, self.1)
}
}

impl Debug for Position {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Position of Display is: {},{}", self.0, self.1)
}
}

/// `DisplayOutput` defines the Trait specification of a platform's `Display`.
/// A `Display` may contain:
/// - An EDID.
/// - State, including if the `Display` is active, primary, or has a fault.
/// - Other helper methods, returning data stored in-memory state.
pub trait DisplayOutput {
/// Returns a boolean result, if the `Display` is the 'primary display' or not.
fn is_primary(&self) -> bool;
/// Returns a boolean result, if the `Display` is currently active or not.
fn is_active(&self) -> bool;
/// Returns the `Position` custom type of the `Display`.
fn get_position(&self) -> Position {
Position::default()
}
/// Returns the current `Resolution` of the `Display`.
fn get_resolution(&self) -> Resolution {
Resolution::default()
}
/// Returns the current supported `Resolutions` of the `Display`.
fn get_supported_resolutions(&self) -> Resolutions {
Resolutions::default()
}
/// Returns the EDID `&str` of the `Display.
fn get_edid(&self) -> Option<&str>;
}
21 changes: 17 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
//! A library to interact with the Windows API for display settings.
//!
//! This library provides an abstraction around some `winuser.h` calls relevant for modifying display settings.
#[cfg_attr(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
path = "platforms/unix/mod.rs"
)]
#[cfg_attr(target_os = "windows", path = "platforms/windows/mod.rs")]
#[cfg_attr(target_os = "macos", path = "platforms/macos/mod.rs")]
mod platform;
pub use crate::platform::*;

mod display;
mod properties;
mod common;
pub use crate::common::*;

pub use display::*;
pub use properties::*;
pub type Display = Box<dyn DisplayOutput>;
175 changes: 0 additions & 175 deletions src/main.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/platforms/macos/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

11 changes: 11 additions & 0 deletions src/platforms/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[cfg(feature = "unix-x11")]
mod x11;

#[cfg(feature = "unix-x11")]
pub use x11::*;

#[cfg(feature = "unix-wayland")]
mod wayland;

#[cfg(feature = "unix-wayland")]
pub use wayland::*;
2 changes: 2 additions & 0 deletions src/platforms/unix/wayland/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(Debug)]
struct WaylandBackend;
1 change: 1 addition & 0 deletions src/platforms/unix/x11/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
4 changes: 4 additions & 0 deletions src/platforms/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod display;
mod properties;

pub use crate::windows::*;
File renamed without changes.