1
1
use crate :: calculator:: calculate_brightness;
2
- use crate :: config:: { BrightnessValues , Location , MonitorOverride } ;
2
+ use crate :: config:: { BrightnessValues , Location , MonitorOverride , MonitorProperty } ;
3
3
use brightness:: blocking:: { Brightness , BrightnessDevice } ;
4
- use maplit :: btreemap ;
4
+ use itertools :: Itertools ;
5
5
use serde:: Serialize ;
6
- use std:: collections:: BTreeMap ;
6
+ use std:: collections:: HashMap ;
7
7
use std:: time:: { SystemTime , UNIX_EPOCH } ;
8
8
use sunrise_sunset_calculator:: SunriseSunsetParameters ;
9
9
use wildmatch:: WildMatch ;
@@ -34,12 +34,22 @@ impl From<sunrise_sunset_calculator::SunriseSunsetResult> for SunriseSunsetResul
34
34
35
35
#[ derive( Debug , Serialize ) ]
36
36
pub struct MonitorResult {
37
- pub device_name : String ,
38
- pub properties : BTreeMap < & ' static str , String > ,
37
+ pub properties : MonitorProperties ,
39
38
pub brightness : Option < BrightnessDetails > ,
40
39
pub error : Option < String > ,
41
40
}
42
41
42
+ #[ derive( Debug , Serialize ) ]
43
+ pub struct MonitorProperties {
44
+ pub device_name : String ,
45
+ #[ cfg( windows) ]
46
+ pub device_description : String ,
47
+ #[ cfg( windows) ]
48
+ pub device_key : String ,
49
+ #[ cfg( windows) ]
50
+ pub device_path : String ,
51
+ }
52
+
43
53
#[ derive( Debug , Serialize ) ]
44
54
pub struct BrightnessDetails {
45
55
pub expiry_time : Option < i64 > ,
@@ -50,15 +60,15 @@ pub struct BrightnessDetails {
50
60
51
61
pub struct MonitorOverrideCompiled {
52
62
pub pattern : WildMatch ,
53
- pub key : String ,
63
+ pub key : MonitorProperty ,
54
64
pub brightness : Option < BrightnessValues > ,
55
65
}
56
66
57
67
impl From < & MonitorOverride > for MonitorOverrideCompiled {
58
68
fn from ( value : & MonitorOverride ) -> Self {
59
69
Self {
60
70
pattern : WildMatch :: new ( & value. pattern ) ,
61
- key : value. key . clone ( ) ,
71
+ key : value. key ,
62
72
brightness : value. brightness . clone ( ) ,
63
73
}
64
74
}
@@ -67,10 +77,11 @@ impl From<&MonitorOverride> for MonitorOverrideCompiled {
67
77
/// Find the first override that matches this monitor's properties
68
78
fn match_monitor < ' o > (
69
79
overrides : & ' o [ MonitorOverrideCompiled ] ,
70
- monitor : & BTreeMap < & ' static str , String > ,
80
+ monitor : & MonitorProperties ,
71
81
) -> Option < & ' o MonitorOverrideCompiled > {
82
+ let map = monitor. to_map ( ) ;
72
83
for o in overrides {
73
- if let Some ( value) = monitor . get ( o. key . as_str ( ) ) {
84
+ if let Some ( value) = map . get ( & o. key ) {
74
85
if o. pattern . matches ( value) {
75
86
return Some ( o) ;
76
87
}
@@ -108,8 +119,7 @@ pub fn apply_brightness(
108
119
let monitor_results = monitors
109
120
. into_iter ( )
110
121
. map ( |m| {
111
- let device_name = m. device_name ( ) . unwrap_or_default ( ) ;
112
- let properties = get_properties ( & m) . unwrap_or_default ( ) ;
122
+ let properties = MonitorProperties :: from_device ( & m) ;
113
123
let monitor_values = match match_monitor ( & overrides, & properties) {
114
124
None => Some ( BrightnessValues {
115
125
brightness_day,
@@ -132,25 +142,28 @@ pub fn apply_brightness(
132
142
) ;
133
143
log:: debug!(
134
144
"Computed brightness for '{}' = {:?} (day={}) (night={})" ,
135
- device_name,
145
+ properties . device_name,
136
146
brightness,
137
147
brightness_day,
138
148
brightness_night
139
149
) ;
140
150
141
151
let error = m. set ( brightness. brightness ) . err ( ) ;
142
152
if let Some ( err) = error. as_ref ( ) {
143
- log:: error!( "Failed to set brightness for '{}': {:?}" , device_name, err) ;
153
+ log:: error!(
154
+ "Failed to set brightness for '{}': {:?}" ,
155
+ properties. device_name,
156
+ err
157
+ ) ;
144
158
} else {
145
159
log:: info!(
146
160
"Successfully set brightness for '{}' to {}%" ,
147
- device_name,
161
+ properties . device_name,
148
162
brightness. brightness
149
163
) ;
150
164
}
151
165
152
166
MonitorResult {
153
- device_name,
154
167
properties,
155
168
brightness : Some ( BrightnessDetails {
156
169
expiry_time : brightness. expiry_time ,
@@ -161,15 +174,18 @@ pub fn apply_brightness(
161
174
error : error. map ( |e| e. to_string ( ) ) ,
162
175
}
163
176
} else {
164
- log:: info!( "Skipping '{}' due to monitor override" , device_name, ) ;
177
+ log:: info!(
178
+ "Skipping '{}' due to monitor override" ,
179
+ properties. device_name
180
+ ) ;
165
181
MonitorResult {
166
- device_name,
167
182
properties,
168
183
brightness : None ,
169
184
error : None ,
170
185
}
171
186
}
172
187
} )
188
+ . sorted_by_key ( |m| m. properties . device_name . clone ( ) )
173
189
. collect :: < Vec < _ > > ( ) ;
174
190
175
191
ApplyResults {
@@ -179,24 +195,30 @@ pub fn apply_brightness(
179
195
}
180
196
}
181
197
182
- #[ cfg( windows) ]
183
- pub fn get_properties (
184
- device : & BrightnessDevice ,
185
- ) -> Result < BTreeMap < & ' static str , String > , brightness:: Error > {
186
- use brightness:: blocking:: windows:: BrightnessExt ;
187
- Ok ( btreemap ! {
188
- "device_name" => device. device_name( ) ?,
189
- "device_description" => device. device_description( ) ?,
190
- "device_key" => device. device_registry_key( ) ?,
191
- "device_path" => device. device_path( ) ?,
192
- } )
193
- }
198
+ impl MonitorProperties {
199
+ fn from_device ( device : & BrightnessDevice ) -> Self {
200
+ #[ cfg( windows) ]
201
+ use brightness:: blocking:: windows:: BrightnessExt ;
202
+ Self {
203
+ device_name : device. device_name ( ) . unwrap ( ) ,
204
+ #[ cfg( windows) ]
205
+ device_description : device. device_description ( ) . unwrap ( ) ,
206
+ #[ cfg( windows) ]
207
+ device_key : device. device_registry_key ( ) . unwrap ( ) ,
208
+ #[ cfg( windows) ]
209
+ device_path : device. device_path ( ) . unwrap ( ) ,
210
+ }
211
+ }
194
212
195
- #[ cfg( target_os = "linux" ) ]
196
- pub fn get_properties (
197
- device : & BrightnessDevice ,
198
- ) -> Result < BTreeMap < & ' static str , String > , brightness:: Error > {
199
- Ok ( btreemap ! {
200
- "device_name" => device. device_name( ) ?,
201
- } )
213
+ pub fn to_map ( & self ) -> HashMap < MonitorProperty , & str > {
214
+ let mut map = HashMap :: < _ , & str > :: new ( ) ;
215
+ map. insert ( MonitorProperty :: DeviceName , & self . device_name ) ;
216
+ #[ cfg( windows) ]
217
+ {
218
+ map. insert ( MonitorProperty :: DeviceDescription , & self . device_description ) ;
219
+ map. insert ( MonitorProperty :: DeviceKey , & self . device_key ) ;
220
+ map. insert ( MonitorProperty :: DevicePath , & self . device_path ) ;
221
+ }
222
+ map
223
+ }
202
224
}
0 commit comments