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
pub mod mccs;

use std::io::{Error, ErrorKind};
use std::cell::RefCell;

#[cfg(feature = "node-bindings")]
mod neon_bindings;

#[cfg(feature = "node-bindings")]
use neon_bindings::{display_get_brightness, display_set_brightness, display_info};
#[cfg(feature = "node-bindings")]
use neon::prelude::*;

use ddc::{Ddc, VcpValue};
use ddc_hi::Display;

pub struct EnhancedDisplay {
    pub inner_display: Display,
}

#[cfg(feature = "node-bindings")]
impl Finalize for EnhancedDisplay {}

thread_local! {
    pub static ENHANCED_DISPLAYS: RefCell<Vec<EnhancedDisplay>> = RefCell::new(
        Display::enumerate().into_iter().map(|mut display|
            match display.update_capabilities() {
                Ok(()) => Ok(EnhancedDisplay { inner_display: display }),
                Err(err) => Err(Error::new(ErrorKind::TimedOut, err.to_string()))
            }
        ).filter_map(|display| display.ok()).collect());
}


pub fn get_brightness(id: String) -> Result<VcpValue, Error> {
    ENHANCED_DISPLAYS.with(|enhanced_displays| {
        enhanced_displays.take().iter_mut().find(|enhanced_display|
            enhanced_display.inner_display.info.id == id).map(|enhanced_display|
            match enhanced_display.inner_display.info.mccs_database.get(mccs::ImageAdjustment::Luminance.into()) {
                Some(feature) => {
                    enhanced_display.inner_display.handle.get_vcp_feature(feature.code)
                        .map_err(|error| Error::new(ErrorKind::TimedOut, error.to_string()))
                }
                None => Err(Error::new(ErrorKind::Unsupported, "This display doesn't support brightness operations"))
            }).unwrap_or(Err(Error::new(ErrorKind::Unsupported, format!("There is no display with id: {}", id))))
    })
}

pub fn set_brightness(id: String, value: u16) -> Result<(), Error> {
    ENHANCED_DISPLAYS.with(|enhanced_displays| {
        enhanced_displays.take().iter_mut().find(|enhanced_display|
            enhanced_display.inner_display.info.id == id).map(|enhanced_display|
            match enhanced_display.inner_display.info.mccs_database.get(mccs::ImageAdjustment::Luminance.into()) {
                Some(feature) => {
                    enhanced_display.inner_display.handle.set_vcp_feature(feature.code, value)
                        .map_err(|error| Error::new(ErrorKind::TimedOut, error.to_string()))
                }
                None => Err(Error::new(ErrorKind::Unsupported, "This display doesn't support brightness operations"))
            }).unwrap_or(Err(Error::new(ErrorKind::Unsupported, format!("There is no display with id: {}", id))))
    })
}

#[cfg(feature = "node-bindings")]
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
    cx.export_function("display_info", display_info)?;
    cx.export_function("display_get_brightness", display_get_brightness)?;
    cx.export_function("display_set_brightness", display_set_brightness)?;
    Ok(())
}