@@ -23,7 +23,7 @@ pub struct Hueshift {
23
23
current_temp : u16 ,
24
24
max_temp : u16 ,
25
25
min_temp : u16 ,
26
- hue_shifter : Option < HueShifter > ,
26
+ hue_shift_driver : Box < dyn HueShiftDriver > ,
27
27
click_temp : u16 ,
28
28
29
29
//useful, but optional
@@ -33,11 +33,86 @@ pub struct Hueshift {
33
33
tx_update_request : Sender < Task > ,
34
34
}
35
35
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
+
36
110
#[ derive( Deserialize , Debug , Clone ) ]
37
111
#[ serde( rename_all = "lowercase" ) ]
38
112
pub enum HueShifter {
39
113
Redshift ,
40
114
Sct ,
115
+ Gammastep ,
41
116
}
42
117
43
118
#[ derive( Deserialize , Debug , Clone ) ]
@@ -103,6 +178,8 @@ impl HueshiftConfig {
103
178
Some ( HueShifter :: Redshift )
104
179
} else if has_command ( "hueshift" , "sct" ) . unwrap_or ( false ) {
105
180
Some ( HueShifter :: Sct )
181
+ } else if has_command ( "hueshift" , "gammastep" ) . unwrap_or ( false ) {
182
+ Some ( HueShifter :: Gammastep )
106
183
} else {
107
184
None
108
185
}
@@ -140,6 +217,16 @@ impl ConfigBlock for Hueshift {
140
217
if block_config. min_temp < 1000 || block_config. min_temp > block_config. max_temp {
141
218
min_temp = 1000 ;
142
219
}
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
+
143
230
Ok ( Hueshift {
144
231
id : id. clone ( ) ,
145
232
update_interval : block_config. interval ,
@@ -149,7 +236,7 @@ impl ConfigBlock for Hueshift {
149
236
max_temp,
150
237
min_temp,
151
238
current_temp,
152
- hue_shifter : block_config . hue_shifter ,
239
+ hue_shift_driver ,
153
240
click_temp : block_config. click_temp ,
154
241
config,
155
242
} )
@@ -172,15 +259,15 @@ impl Block for Hueshift {
172
259
match event. button {
173
260
MouseButton :: Left => {
174
261
self . current_temp = self . click_temp ;
175
- update_hue ( & self . hue_shifter , self . current_temp ) ;
262
+ self . hue_shift_driver . update ( self . current_temp ) ? ;
176
263
}
177
264
MouseButton :: Right => {
178
265
if self . max_temp > 6500 {
179
266
self . current_temp = 6500 ;
180
- reset_hue ( & self . hue_shifter ) ;
267
+ self . hue_shift_driver . reset ( ) ? ;
181
268
} else {
182
269
self . current_temp = self . max_temp ;
183
- update_hue ( & self . hue_shifter , self . current_temp ) ;
270
+ self . hue_shift_driver . update ( self . current_temp ) ? ;
184
271
}
185
272
}
186
273
mb => {
@@ -190,14 +277,14 @@ impl Block for Hueshift {
190
277
Some ( Up ) => {
191
278
new_temp = self . current_temp + self . step ;
192
279
if new_temp <= self . max_temp {
193
- update_hue ( & self . hue_shifter , new_temp) ;
280
+ self . hue_shift_driver . update ( new_temp) ? ;
194
281
self . current_temp = new_temp;
195
282
}
196
283
}
197
284
Some ( Down ) => {
198
285
new_temp = self . current_temp - self . step ;
199
286
if new_temp >= self . min_temp {
200
- update_hue ( & self . hue_shifter , new_temp) ;
287
+ self . hue_shift_driver . update ( new_temp) ? ;
201
288
self . current_temp = new_temp;
202
289
}
203
290
}
@@ -214,44 +301,3 @@ impl Block for Hueshift {
214
301
& self . id
215
302
}
216
303
}
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