some fixes
This commit is contained in:
parent
fc1ca721fc
commit
376d0a17e8
33
src/main.cpp
33
src/main.cpp
@ -16,39 +16,6 @@ GMainLoop *gMainLoop = nullptr;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/*
|
||||
// Daemonize the process
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
return 1; // Fork failed
|
||||
}
|
||||
|
||||
if (pid > 0) {
|
||||
return 0; // Parent process exits
|
||||
}
|
||||
|
||||
// Child process continues as daemon
|
||||
umask(0);
|
||||
|
||||
// Create new session
|
||||
if (setsid() < 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Change directory to root
|
||||
chdir("/");
|
||||
|
||||
// Close standard file descriptors
|
||||
int fd = open("/dev/null", O_RDWR);
|
||||
if (fd != -1) {
|
||||
dup2(fd, STDIN_FILENO);
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
dup2(fd, STDERR_FILENO);
|
||||
close(fd);
|
||||
}
|
||||
*/
|
||||
|
||||
gtk_init();
|
||||
|
||||
// Register resources containing the application icon
|
||||
|
||||
@ -9,20 +9,28 @@
|
||||
|
||||
TempMonitor::TempMonitor()
|
||||
{
|
||||
std::cerr << "[DEBUG] TempMonitor constructor - starting sensor discovery" << std::endl;
|
||||
discoverSensors();
|
||||
std::cerr << "[DEBUG] Sensor discovery complete - found " << discoveredSensors.size() << " sensors" << std::endl;
|
||||
}
|
||||
|
||||
void TempMonitor::discoverSensors()
|
||||
{
|
||||
std::cerr << "[DEBUG] discoverSensors() started" << std::endl;
|
||||
discoveredSensors.clear();
|
||||
DIR* hwmonDir = opendir("/sys/class/hwmon");
|
||||
if (!hwmonDir) return;
|
||||
if (!hwmonDir) {
|
||||
std::cerr << "[ERROR] Cannot open /sys/class/hwmon" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cerr << "[DEBUG] Opened /sys/class/hwmon successfully" << std::endl;
|
||||
struct dirent* entry;
|
||||
while ((entry = readdir(hwmonDir)) != nullptr) {
|
||||
std::string hwmonName = entry->d_name;
|
||||
if (hwmonName.find("hwmon") != 0) continue;
|
||||
|
||||
std::cerr << "[DEBUG] Processing hwmon device: " << hwmonName << std::endl;
|
||||
std::string path = "/sys/class/hwmon/" + hwmonName;
|
||||
|
||||
// Read "name" from sysfs
|
||||
@ -34,17 +42,24 @@ void TempMonitor::discoverSensors()
|
||||
nameFile.close();
|
||||
}
|
||||
|
||||
std::cerr << "[DEBUG] Device name: " << nameFromSysfs << std::endl;
|
||||
|
||||
// Filter: Only interested in nvme and coretemp (CPU)
|
||||
if (nameFromSysfs != "nvme" && nameFromSysfs != "coretemp") continue;
|
||||
if (nameFromSysfs != "nvme" && nameFromSysfs != "coretemp") {
|
||||
std::cerr << "[DEBUG] Skipped - not nvme or coretemp" << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
DIR* dDir = opendir(path.c_str());
|
||||
if (dDir) {
|
||||
std::cerr << "[DEBUG] Opened directory: " << path << std::endl;
|
||||
struct dirent* sEntry;
|
||||
while ((sEntry = readdir(dDir)) != nullptr) {
|
||||
std::string fname = sEntry->d_name;
|
||||
// Only temperature sensors (temp*_input)
|
||||
if (fname.find("temp") == 0 && fname.find("_input") != std::string::npos) {
|
||||
std::string id = fname.substr(4, fname.find("_input") - 4);
|
||||
std::cerr << "[DEBUG] Found temperature file: " << fname << " (id: " << id << ")" << std::endl;
|
||||
|
||||
// Use "label" from sysfs for sensor name if available
|
||||
std::string labelFromSysfs = "";
|
||||
@ -55,34 +70,42 @@ void TempMonitor::discoverSensors()
|
||||
labelFile.close();
|
||||
}
|
||||
|
||||
// For CPU (coretemp), filter only Package id 0
|
||||
if (nameFromSysfs == "coretemp") {
|
||||
if (labelFromSysfs != "Package id 0") continue;
|
||||
}
|
||||
std::cerr << "[DEBUG] Label: '" << labelFromSysfs << "'" << std::endl;
|
||||
|
||||
// For NVMe, filter out Composite sensor
|
||||
if (nameFromSysfs == "nvme") {
|
||||
if (labelFromSysfs == "Composite") continue;
|
||||
// For CPU (coretemp), filter only Package id 0 (if label exists)
|
||||
if (nameFromSysfs == "coretemp") {
|
||||
if (!labelFromSysfs.empty() && labelFromSysfs != "Package id 0") {
|
||||
std::cerr << "[DEBUG] Filtered (coretemp, not Package id 0)" << std::endl;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
std::string finalSensorName = labelFromSysfs.empty() ? "temp" + id : labelFromSysfs;
|
||||
std::string deviceDisplayName = nameFromSysfs + " (" + hwmonName + ")";
|
||||
|
||||
std::cerr << "[DEBUG] Added sensor: " << finalSensorName << " from " << deviceDisplayName << std::endl;
|
||||
std::cerr << "[DEBUG] Path: " << path + "/" + fname << std::endl;
|
||||
|
||||
discoveredSensors.push_back({deviceDisplayName, finalSensorName, path + "/" + fname});
|
||||
}
|
||||
}
|
||||
closedir(dDir);
|
||||
} else {
|
||||
std::cerr << "[ERROR] Cannot open directory: " << path << std::endl;
|
||||
}
|
||||
}
|
||||
closedir(hwmonDir);
|
||||
std::cerr << "[DEBUG] discoverSensors() finished - total sensors: " << discoveredSensors.size() << std::endl;
|
||||
}
|
||||
|
||||
std::vector<std::string> TempMonitor::getAvailableDevices()
|
||||
{
|
||||
std::cerr << "[DEBUG] getAvailableDevices() called" << std::endl;
|
||||
std::vector<std::string> devices;
|
||||
auto all = getAllTemperatures();
|
||||
for (auto const& [name, sensors] : all) {
|
||||
devices.push_back(name);
|
||||
std::cerr << "[DEBUG] Device: " << name << " with " << sensors.size() << " sensors" << std::endl;
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
@ -91,6 +114,7 @@ double TempMonitor::readTemperatureFromFile(const std::string &filePath)
|
||||
{
|
||||
std::ifstream file(filePath);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "[ERROR] Cannot open temperature file: " << filePath << std::endl;
|
||||
return -1000.0;
|
||||
}
|
||||
|
||||
@ -98,8 +122,11 @@ double TempMonitor::readTemperatureFromFile(const std::string &filePath)
|
||||
if (std::getline(file, line)) {
|
||||
try {
|
||||
long tempMilliC = std::stol(line);
|
||||
return tempMilliC / 1000.0;
|
||||
double tempC = tempMilliC / 1000.0;
|
||||
std::cerr << "[DEBUG] Read temperature from " << filePath << ": " << tempC << "°C" << std::endl;
|
||||
return tempC;
|
||||
} catch (...) {
|
||||
std::cerr << "[ERROR] Failed to parse temperature value: " << line << std::endl;
|
||||
}
|
||||
}
|
||||
return -1000.0;
|
||||
@ -107,15 +134,20 @@ double TempMonitor::readTemperatureFromFile(const std::string &filePath)
|
||||
|
||||
std::map<std::string, double> TempMonitor::readTemperatures(const std::string &device)
|
||||
{
|
||||
std::cerr << "[DEBUG] readTemperatures() called for device: " << device << std::endl;
|
||||
auto all = getAllTemperatures();
|
||||
if (all.count(device)) {
|
||||
return all.at(device);
|
||||
auto sensors = all.at(device);
|
||||
std::cerr << "[DEBUG] Found " << sensors.size() << " sensors for device" << std::endl;
|
||||
return sensors;
|
||||
}
|
||||
std::cerr << "[ERROR] Device not found: " << device << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::map<std::string, std::map<std::string, double>> TempMonitor::getAllTemperatures()
|
||||
{
|
||||
std::cerr << "[DEBUG] getAllTemperatures() called" << std::endl;
|
||||
std::map<std::string, std::map<std::string, double>> allTemperatures;
|
||||
|
||||
for (const auto& sensor : discoveredSensors) {
|
||||
@ -125,5 +157,6 @@ std::map<std::string, std::map<std::string, double>> TempMonitor::getAllTemperat
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "[DEBUG] getAllTemperatures() finished - total devices: " << allTemperatures.size() << std::endl;
|
||||
return allTemperatures;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user