Skip to content

Commit 99ff12a

Browse files
authored
Merge pull request #1027 from MaxVerevkin/gammastep
Hueshift: add support for gammastep
2 parents 6205f7e + 5210ffd commit 99ff12a

File tree

2 files changed

+104
-49
lines changed

2 files changed

+104
-49
lines changed

blocks.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,20 @@ click_temp = 3500
530530
Key | Values | Required | Default
531531
----|--------|----------|--------
532532
`step` | The step color temperature is in/decreased in Kelvin. | No | `100`
533-
`hue_shifter` | Program used to control screen color, either `"redshift"` or `"sct"`. | No | `"redshift"`
533+
`hue_shifter` | Program used to control screen color. | No | Detect automatically. |
534534
`max_temp` | Max color temperature in Kelvin. | No | `10000`
535535
`min_temp` | Min color temperature in Kelvin. | No | `1000`
536536
`click_temp` | Left click color temperature in Kelvin. | No | `6500`
537537

538+
#### Available Hue Shifters
539+
540+
Name | Supports
541+
-----|---------
542+
`"redshift"` | X11
543+
`"sct"` | X11
544+
`"gammastep"` | X11 and Wayland
545+
546+
538547
A hard limit is set for the `max_temp` to `10000K` and the same for the `min_temp` which is `1000K`.
539548
The `step` has a hard limit as well, defined to `500K` to avoid too brutal changes.
540549

src/blocks/hueshift.rs

+94-48
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct Hueshift {
2323
current_temp: u16,
2424
max_temp: u16,
2525
min_temp: u16,
26-
hue_shifter: Option<HueShifter>,
26+
hue_shift_driver: Box<dyn HueShiftDriver>,
2727
click_temp: u16,
2828

2929
//useful, but optional
@@ -33,11 +33,86 @@ pub struct Hueshift {
3333
tx_update_request: Sender<Task>,
3434
}
3535

36+
trait HueShiftDriver {
37+
fn update(&self, temp: u16) -> Result<()>;
38+
fn reset(&self) -> Result<()>;
39+
}
40+
struct Redshift();
41+
impl HueShiftDriver for Redshift {
42+
fn update(&self, temp: u16) -> Result<()> {
43+
Command::new("sh")
44+
.args(&[
45+
"-c",
46+
format!("redshift -O {} -P >/dev/null 2>&1", temp).as_str(),
47+
])
48+
.spawn()
49+
.block_error(
50+
"hueshift",
51+
"Failed to set new color temperature using redshift.",
52+
)?;
53+
Ok(())
54+
}
55+
fn reset(&self) -> Result<()> {
56+
Command::new("sh")
57+
.args(&["-c", "redshift -x >/dev/null 2>&1"])
58+
.spawn()
59+
.block_error(
60+
"redshift",
61+
"Failed to set new color temperature using redshift.",
62+
)?;
63+
Ok(())
64+
}
65+
}
66+
struct Sct();
67+
impl HueShiftDriver for Sct {
68+
fn update(&self, temp: u16) -> Result<()> {
69+
Command::new("sh")
70+
.args(&["-c", format!("sct {} >/dev/null 2>&1", temp).as_str()])
71+
.spawn()
72+
.block_error("hueshift", "Failed to set new color temperature using sct.")?;
73+
Ok(())
74+
}
75+
fn reset(&self) -> Result<()> {
76+
Command::new("sh")
77+
.args(&["-c", "sct >/dev/null 2>&1"])
78+
.spawn()
79+
.block_error("hueshift", "Failed to set new color temperature using sct.")?;
80+
Ok(())
81+
}
82+
}
83+
struct Gammastep();
84+
impl HueShiftDriver for Gammastep {
85+
fn update(&self, temp: u16) -> Result<()> {
86+
Command::new("sh")
87+
.args(&[
88+
"-c",
89+
&format!("killall gammastep; gammastep -O {} -P &", temp),
90+
])
91+
.spawn()
92+
.block_error(
93+
"hueshift",
94+
"Failed to set new color temperature using gammastep.",
95+
)?;
96+
Ok(())
97+
}
98+
fn reset(&self) -> Result<()> {
99+
Command::new("sh")
100+
.args(&["-c", "gammastep -x >/dev/null 2>&1"])
101+
.spawn()
102+
.block_error(
103+
"hueshift",
104+
"Failed to set new color temperature using gammastep.",
105+
)?;
106+
Ok(())
107+
}
108+
}
109+
36110
#[derive(Deserialize, Debug, Clone)]
37111
#[serde(rename_all = "lowercase")]
38112
pub enum HueShifter {
39113
Redshift,
40114
Sct,
115+
Gammastep,
41116
}
42117

43118
#[derive(Deserialize, Debug, Clone)]
@@ -103,6 +178,8 @@ impl HueshiftConfig {
103178
Some(HueShifter::Redshift)
104179
} else if has_command("hueshift", "sct").unwrap_or(false) {
105180
Some(HueShifter::Sct)
181+
} else if has_command("hueshift", "gammastep").unwrap_or(false) {
182+
Some(HueShifter::Gammastep)
106183
} else {
107184
None
108185
}
@@ -140,6 +217,16 @@ impl ConfigBlock for Hueshift {
140217
if block_config.min_temp < 1000 || block_config.min_temp > block_config.max_temp {
141218
min_temp = 1000;
142219
}
220+
221+
let hue_shift_driver: Box<dyn HueShiftDriver> = match block_config
222+
.hue_shifter
223+
.block_error("hueshift", "Cound not detect driver program")?
224+
{
225+
HueShifter::Redshift => Box::new(Redshift {}),
226+
HueShifter::Sct => Box::new(Sct {}),
227+
HueShifter::Gammastep => Box::new(Gammastep {}),
228+
};
229+
143230
Ok(Hueshift {
144231
id: id.clone(),
145232
update_interval: block_config.interval,
@@ -149,7 +236,7 @@ impl ConfigBlock for Hueshift {
149236
max_temp,
150237
min_temp,
151238
current_temp,
152-
hue_shifter: block_config.hue_shifter,
239+
hue_shift_driver,
153240
click_temp: block_config.click_temp,
154241
config,
155242
})
@@ -172,15 +259,15 @@ impl Block for Hueshift {
172259
match event.button {
173260
MouseButton::Left => {
174261
self.current_temp = self.click_temp;
175-
update_hue(&self.hue_shifter, self.current_temp);
262+
self.hue_shift_driver.update(self.current_temp)?;
176263
}
177264
MouseButton::Right => {
178265
if self.max_temp > 6500 {
179266
self.current_temp = 6500;
180-
reset_hue(&self.hue_shifter);
267+
self.hue_shift_driver.reset()?;
181268
} else {
182269
self.current_temp = self.max_temp;
183-
update_hue(&self.hue_shifter, self.current_temp);
270+
self.hue_shift_driver.update(self.current_temp)?;
184271
}
185272
}
186273
mb => {
@@ -190,14 +277,14 @@ impl Block for Hueshift {
190277
Some(Up) => {
191278
new_temp = self.current_temp + self.step;
192279
if new_temp <= self.max_temp {
193-
update_hue(&self.hue_shifter, new_temp);
280+
self.hue_shift_driver.update(new_temp)?;
194281
self.current_temp = new_temp;
195282
}
196283
}
197284
Some(Down) => {
198285
new_temp = self.current_temp - self.step;
199286
if new_temp >= self.min_temp {
200-
update_hue(&self.hue_shifter, new_temp);
287+
self.hue_shift_driver.update(new_temp)?;
201288
self.current_temp = new_temp;
202289
}
203290
}
@@ -214,44 +301,3 @@ impl Block for Hueshift {
214301
&self.id
215302
}
216303
}
217-
218-
#[inline]
219-
fn update_hue(hue_shifter: &Option<HueShifter>, new_temp: u16) {
220-
match hue_shifter {
221-
Some(HueShifter::Redshift) => {
222-
Command::new("sh")
223-
.args(&[
224-
"-c",
225-
format!("redshift -O {} -P >/dev/null 2>&1", new_temp).as_str(),
226-
])
227-
.spawn()
228-
.expect("Failed to set new color temperature using redshift.");
229-
}
230-
Some(HueShifter::Sct) => {
231-
Command::new("sh")
232-
.args(&["-c", format!("sct {} >/dev/null 2>&1", new_temp).as_str()])
233-
.spawn()
234-
.expect("Failed to set new color temperature using sct.");
235-
}
236-
None => {}
237-
}
238-
}
239-
240-
#[inline]
241-
fn reset_hue(hue_shifter: &Option<HueShifter>) {
242-
match hue_shifter {
243-
Some(HueShifter::Redshift) => {
244-
Command::new("sh")
245-
.args(&["-c", "redshift -x >/dev/null 2>&1"])
246-
.spawn()
247-
.expect("Failed to set new color temperature using redshift.");
248-
}
249-
Some(HueShifter::Sct) => {
250-
Command::new("sh")
251-
.args(&["-c", "sct >/dev/null 2>&1"])
252-
.spawn()
253-
.expect("Failed to set new color temperature using sct.");
254-
}
255-
None => {}
256-
}
257-
}

0 commit comments

Comments
 (0)