summaryrefslogtreecommitdiffstats
path: root/src/lmsensors.cpp
blob: 73b8253924d76648a139653fcd238407c884151f (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***************************************************************************
                          LMSensors.cpp  -  description
                             -------------------
    begin                : Mon Aug 6 2001
    copyright            : (C) 2001 by
    email                :
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdio.h>

#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

#include <qstrlist.h>
#include <qobjectlist.h>

#include "lmsensors.h"

//****************************************************************************
// Public methods
//****************************************************************************

LMSensors::LMSensors(QObject *parent, const char * name)
                                  : QObject(parent,name)
{
 if(initSensors()) createLMSensors();
 createI8KSensors();
 createHDSensors();
}

LMSensors::~LMSensors()
{
 if(count()) sensors_cleanup();
}

bool LMSensors::initSensors()
{
 FILE *fp=fopen("/etc/sensors.conf","r");
 if(!fp) {
   qWarning("KSensors error: /etc/sensors.conf not found !");
   return false;
 }
 int err= sensors_init(fp);
 if(err) {
   qWarning("KSensors error: sensors_init fail, error code %d",err);
   return false;
 }
 fclose(fp);
 return true;
}

void LMSensors::createLMSensors()
{
 const sensors_chip_name *chip_name;
 int err= 0;
 while( (chip_name= sensors_get_detected_chips(&err)) )
   if( existSensor(chip_name,"temp")  ||
       existSensor(chip_name,"fan")   )
       {
          (void)new LMSensorsChip(chip_name,this);
       }
}

void LMSensors::createHDSensors()
{
 HDSensorsList *disks= new HDSensorsList(this,"Disks");
 if(disks->count()==0) delete disks;
}

void LMSensors::createI8KSensors()
{
 if(I8KSensorsList::I8KAvailable())
   (void) new I8KSensorsList(this,"I8KSensors");
}


int LMSensors::existSensor(const sensors_chip_name *chip_name,const char *sensor_name)
{
int nr1,nr2;
const sensors_feature_data *sensor_data;

 nr1=nr2= 0;
 while( (sensor_data= sensors_get_all_features(*chip_name, &nr1, &nr2)) )
   if( strstr(sensor_data->name,sensor_name) )
     return sensor_data->number;
 return 0;
}

void LMSensors::setMonitorized(bool enable)
{
 QObjectList *list= getSensorsChips();
 if(list)
   for(LMSensorsChip *sensor=(LMSensorsChip*)list->first(); sensor!=0; sensor= (LMSensorsChip *)list->next())
     sensor->setMonitorized(enable);
}


Sensor *LMSensors::getSensor(const char *name)
{
 int     index= count();
 Sensor *sensor= 0;
 while(--index>=0 && !sensor) {
    sensor= getSensorsChip(index)->getSensor(name);
 }
 return sensor;
}

void LMSensors::childEvent( QChildEvent *e )
{
 if(e->inserted()) {
    connect((SensorsList *)e->child(),SIGNAL(valueChanged(Sensor *)) ,this,SIGNAL(valueChanged(Sensor  *)));
    connect((SensorsList *)e->child(),SIGNAL(configChanged(const char *)),this,SIGNAL(configChanged(const char *)));
 }
}


/*********************************************************************************/