sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use crate::sys::macos::ffi;
use crate::{Component, Error};

use objc2_io_kit::io_connect_t;

mod io_service;
mod smc;

pub(crate) use self::io_service::IoService;
use self::smc::{get_key_size, get_temperature, get_temperature_inner};

const COMPONENTS_TEMPERATURE_IDS: &[(&str, &str, &[i8])] = &[
    (
        "PECI CPU",
        "TCXC",
        &['T' as i8, 'C' as i8, 'X' as i8, 'C' as i8],
    ), // PECI CPU "TCXC"
    (
        "PECI CPU",
        "TCXc",
        &['T' as i8, 'C' as i8, 'X' as i8, 'c' as i8],
    ), // PECI CPU "TCXc"
    (
        "CPU Proximity",
        "TC0P",
        &['T' as i8, 'C' as i8, '0' as i8, 'P' as i8],
    ), // CPU Proximity (heat spreader) "TC0P"
    ("GPU", "TG0P", &['T' as i8, 'G' as i8, '0' as i8, 'P' as i8]), // GPU "TG0P"
    (
        "Battery",
        "TB0T",
        &['T' as i8, 'B' as i8, '0' as i8, 'T' as i8],
    ), // Battery "TB0T"
];

pub(crate) struct ComponentFFI {
    input_structure: ffi::KeyData_t,
    val: ffi::Val_t,
    /// The `System::connection`.
    connection: io_connect_t,
}

impl ComponentFFI {
    fn new(key: &[i8], connection: io_connect_t) -> Option<ComponentFFI> {
        unsafe {
            get_key_size(connection, key)
                .ok()
                .map(|(input_structure, val)| ComponentFFI {
                    input_structure,
                    val,
                    connection,
                })
        }
    }

    fn temperature(&self) -> Option<f32> {
        get_temperature_inner(self.connection, &self.input_structure, &self.val)
    }
}

// Used to get CPU information; not supported on iOS or inside the default macOS sandbox.
pub(crate) struct ComponentsInner {
    pub(crate) components: Vec<Component>,
    connection: Option<IoService>,
}

impl ComponentsInner {
    pub(crate) fn new() -> Result<Self, Error> {
        Ok(Self {
            components: Vec::with_capacity(2),
            connection: IoService::new_connection(),
        })
    }

    pub(crate) fn list(&self) -> &[Component] {
        &self.components
    }

    pub(crate) fn list_mut(&mut self) -> &mut [Component] {
        &mut self.components
    }

    pub(crate) fn refresh(&mut self) {
        let Some(ref connection) = self.connection else {
            sinf_debug!("No connection to IoService, skipping components refresh");
            return;
        };
        let connection = connection.inner();
        // Retrieve the CPU critical temperature.
        let critical_temp =
            get_temperature(connection, &['T' as i8, 'C' as i8, '0' as i8, 'D' as i8, 0]);

        for (label, id, v) in COMPONENTS_TEMPERATURE_IDS.iter() {
            if let Some(c) = self.components.iter_mut().find(|c| c.inner.id == *id) {
                c.refresh();
                c.inner.updated = true;
            } else if let Some(c) = ComponentInner::new(
                (*id).to_owned(),
                (*label).to_owned(),
                None,
                critical_temp,
                v,
                connection,
            ) {
                self.components.push(Component { inner: c });
            }
        }
    }
}

pub(crate) struct ComponentInner {
    id: String,
    temperature: Option<f32>,
    max: f32,
    critical: Option<f32>,
    label: String,
    ffi_part: ComponentFFI,
    pub(crate) updated: bool,
}

impl ComponentInner {
    /// Creates a new `ComponentInner` with the given information.
    pub(crate) fn new(
        id: String,
        label: String,
        max: Option<f32>,
        critical: Option<f32>,
        key: &[i8],
        connection: io_connect_t,
    ) -> Option<Self> {
        let ffi_part = ComponentFFI::new(key, connection)?;
        ffi_part.temperature().map(|temperature| Self {
            id,
            temperature: Some(temperature),
            label,
            max: max.unwrap_or(temperature),
            critical,
            ffi_part,
            updated: true,
        })
    }

    pub(crate) fn temperature(&self) -> Option<f32> {
        self.temperature
    }

    pub(crate) fn max(&self) -> Option<f32> {
        Some(self.max)
    }

    pub(crate) fn critical(&self) -> Option<f32> {
        self.critical
    }

    pub(crate) fn label(&self) -> &str {
        &self.label
    }

    pub(crate) fn id(&self) -> Option<&str> {
        Some(&self.id)
    }

    pub(crate) fn refresh(&mut self) {
        self.temperature = self.ffi_part.temperature();
        if let Some(temperature) = self.temperature
            && temperature > self.max
        {
            self.max = temperature;
        }
    }
}