-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmisc.rs
More file actions
185 lines (165 loc) · 5.35 KB
/
misc.rs
File metadata and controls
185 lines (165 loc) · 5.35 KB
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
//! The CLI interface for displayz
//!
//! Use the `--help` flag to see the available options.
use std::cell::RefMut;
use color_eyre::eyre::{Result, eyre};
use displayz::{
DisplaySettings, FixedOutput, Frequency, Orientation, Position, Resolution, query_displays,
refresh,
};
use structopt::{StructOpt, clap::ArgGroup};
/// CLI arguments
#[derive(StructOpt, Debug)]
#[structopt(
name = "display-cli",
about = "Allows changing display settings on Windows using the CLI."
)]
struct Opts {
/// Subcommand to run
#[structopt(subcommand)]
cmd: SubCommands,
/// Output debug info
#[structopt(short, long, global = true)]
verbose: bool,
}
/// Subcommands to select the mode of operatiom
#[derive(StructOpt, Debug)]
enum SubCommands {
/// Sets the primary display
#[structopt(alias = "sp")]
SetPrimary {
#[structopt(short, long)]
id: usize,
},
/// Changes settings of the primary display
#[structopt(alias = "p")]
Primary {
/// The properties to change
#[structopt(flatten)]
properties: PropertiesOpt,
},
/// Changes settings of a display with a specified id
#[structopt(alias = "props")]
Properties {
/// THe id of the display
#[structopt(short, long)]
id: usize,
/// The properties to change
#[structopt(flatten)]
properties: PropertiesOpt,
},
}
/// Describes the properties that can be changed on a display
#[derive(StructOpt, Debug)]
#[structopt(group = ArgGroup::with_name("prop").required(true).multiple(true))]
struct PropertiesOpt {
/// Set the position of the display
#[structopt(
group = "prop",
short,
long,
long_help = "Set the position of the display. Expected format: `<x>,<y>`"
)]
position: Option<Position>,
/// Sets the resolution of the display
#[structopt(
group = "prop",
short,
long,
long_help = "Sets the resolution of the display. Expected format: `<width>x<height>`."
)]
resolution: Option<Resolution>,
/// Sets the orientation of the display
#[structopt(
group = "prop",
short,
long,
long_help = "Sets the orientation of the display. One of: `Default`, `UpsideDown`, `Right`, `Left`"
)]
orientation: Option<Orientation>,
/// Sets the fixed output of the display
#[structopt(
group = "prop",
short,
long,
long_help = "Sets the fixed output of the display. One of: `Default`, `Stretch`, `Center`."
)]
fixed_output: Option<FixedOutput>,
// Sets the refresh rate of the display
#[structopt(
group = "prop",
short("t"),
long,
long_help = "Sets the refresh rate of the display. Expected format: `<n>`."
)]
frequency: Option<Frequency>,
}
/// Entry point for `displayz`.
fn main() -> Result<()> {
color_eyre::install()?;
let opts = Opts::from_args();
let log_level = if opts.verbose {
log::LevelFilter::Trace
} else {
log::LevelFilter::Info
};
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_level.as_str()))
.init();
log::debug!("Parsed Opts:\n{:#?}", opts);
let display_set = query_displays()?;
log::debug!("Discovered displays:\n{}", display_set);
match opts.cmd {
SubCommands::SetPrimary { id } => {
let display = display_set
.get(id)
.ok_or_else(|| eyre!("Display with id {} not found", id))?;
display.set_primary()?;
display_set.apply()?;
refresh()?;
log::info!("Display settings changed");
}
SubCommands::Primary { properties } => {
let display = display_set.primary();
if let Some(settings) = display.settings() {
let mut settings = settings.borrow_mut();
set_properties(&properties, &mut settings);
} else {
Err(eyre!("Primary display has no settings"))?;
}
display.apply()?;
refresh()?;
log::info!("Display settings changed");
}
SubCommands::Properties { id, properties } => {
let display = display_set
.get(id)
.ok_or_else(|| eyre!("Display with id {} not found", id))?;
if let Some(settings) = display.settings() {
let mut settings = settings.borrow_mut();
set_properties(&properties, &mut settings)
} else {
Err(eyre!("Display has no settings"))?;
}
display.apply()?;
refresh()?;
log::info!("Display settings changed");
}
}
Ok(())
}
/// Sets a specific settings from the given properties
macro_rules! assign_if_ok {
($properties:expr_2021, $settings:expr_2021, $name:ident) => {
if let Some(value) = $properties.$name {
$settings.$name = value;
}
};
}
/// Sets all available properties
fn set_properties(properties: &PropertiesOpt, settings: &mut RefMut<DisplaySettings>) {
assign_if_ok!(properties, settings, position);
assign_if_ok!(properties, settings, resolution);
assign_if_ok!(properties, settings, orientation);
assign_if_ok!(properties, settings, fixed_output);
assign_if_ok!(properties, settings, frequency);
}