style
[psensor.git] / src / lib / psensor.c
index a385dda..141779e 100644 (file)
@@ -1,32 +1,37 @@
 /*
-    Copyright (C) 2010-2011 jeanfi@gmail.com
-
-    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.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
-
+ * Copyright (C) 2010-2011 jeanfi@gmail.com
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 #include <stdlib.h>
 #include <string.h>
 
-#include <sensors/sensors.h>
-#include <sensors/error.h>
+#include <locale.h>
+#include <libintl.h>
+#define _(str) gettext(str)
 
 #include "hdd.h"
 #include "psensor.h"
 #include "lmsensor.h"
 
+#ifdef HAVE_GTOP
+#include "cpu.h"
+#endif
+
+
 struct psensor *psensor_create(char *id, char *name,
                               unsigned int type, int values_max_length)
 {
@@ -36,13 +41,13 @@ struct psensor *psensor_create(char *id, char *name,
        psensor->id = id;
        psensor->name = name;
        psensor->enabled = 1;
-       psensor->min = UNKNOWN_VALUE;
-       psensor->max = UNKNOWN_VALUE;
+       psensor->min = UNKNOWN_DBL_VALUE;
+       psensor->max = UNKNOWN_DBL_VALUE;
 
        psensor->type = type;
 
        psensor->values_max_length = values_max_length;
-       psensor->measures = measures_create(values_max_length);
+       psensor->measures = measures_dbl_create(values_max_length);
 
        psensor->alarm_limit = 0;
 
@@ -66,7 +71,7 @@ void psensor_values_resize(struct psensor *s, int new_size)
 
        cur_size = s->values_max_length;
        cur_ms = s->measures;
-       new_ms = measures_create(new_size);
+       new_ms = measures_dbl_create(new_size);
 
        if (cur_ms) {
                int i;
@@ -203,6 +208,8 @@ char *psensor_value_to_string(unsigned int type, double value)
 
        if (is_temp_type(type))
                unit = "C";
+       else if (type & SENSOR_TYPE_CPU_USAGE)
+               unit = "%";
        else
                unit = "";
 
@@ -229,13 +236,13 @@ psensor_set_current_measure(struct psensor *s,
                &s->measures[1],
                (s->values_max_length - 1) * sizeof(struct measure));
 
-       s->measures[s->values_max_length - 1].value = v;
+       s->measures[s->values_max_length - 1].value.d_num = v;
        s->measures[s->values_max_length - 1].time = tv;
 
-       if (s->min == UNKNOWN_VALUE || v < s->min)
+       if (s->min == UNKNOWN_DBL_VALUE || v < s->min)
                s->min = v;
 
-       if (s->max == UNKNOWN_VALUE || v > s->max)
+       if (s->max == UNKNOWN_DBL_VALUE || v > s->max)
                s->max = v;
 
        if (s->alarm_limit && s->alarm_enabled) {
@@ -253,7 +260,7 @@ psensor_set_current_measure(struct psensor *s,
 
 double psensor_get_current_value(struct psensor *sensor)
 {
-       return sensor->measures[sensor->values_max_length - 1].value;
+       return sensor->measures[sensor->values_max_length - 1].value.d_num;
 }
 
 struct measure *psensor_get_current_measure(struct psensor *sensor)
@@ -267,7 +274,7 @@ struct measure *psensor_get_current_measure(struct psensor *sensor)
  */
 double get_min_value(struct psensor **sensors, int type)
 {
-       double m = UNKNOWN_VALUE;
+       double m = UNKNOWN_DBL_VALUE;
        struct psensor **s = sensors;
 
        while (*s) {
@@ -278,12 +285,12 @@ double get_min_value(struct psensor **sensors, int type)
                        double t;
 
                        for (i = 0; i < sensor->values_max_length; i++) {
-                               t = sensor->measures[i].value;
+                               t = sensor->measures[i].value.d_num;
 
-                               if (t == UNKNOWN_VALUE)
+                               if (t == UNKNOWN_DBL_VALUE)
                                        continue;
 
-                               if (m == UNKNOWN_VALUE || t < m)
+                               if (m == UNKNOWN_DBL_VALUE || t < m)
                                        m = t;
                        }
                }
@@ -297,9 +304,9 @@ double get_min_value(struct psensor **sensors, int type)
   Returns the maximal value of a given 'type' (SENSOR_TYPE_TEMP or
   SENSOR_TYPE_FAN)
  */
-static double get_max_value(struct psensor **sensors, int type)
+double get_max_value(struct psensor **sensors, int type)
 {
-       double m = UNKNOWN_VALUE;
+       double m = UNKNOWN_DBL_VALUE;
        struct psensor **s = sensors;
 
        while (*s) {
@@ -309,12 +316,12 @@ static double get_max_value(struct psensor **sensors, int type)
                        int i;
                        double t;
                        for (i = 0; i < sensor->values_max_length; i++) {
-                               t = sensor->measures[i].value;
+                               t = sensor->measures[i].value.d_num;
 
-                               if (t == UNKNOWN_VALUE)
+                               if (t == UNKNOWN_DBL_VALUE)
                                        continue;
 
-                               if (m == UNKNOWN_VALUE || t > m)
+                               if (m == UNKNOWN_DBL_VALUE || t > m)
                                        m = t;
                        }
                }
@@ -327,7 +334,7 @@ static double get_max_value(struct psensor **sensors, int type)
 double
 psensor_get_max_current_value(struct psensor **sensors, unsigned int type)
 {
-       double m = UNKNOWN_VALUE;
+       double m = UNKNOWN_DBL_VALUE;
        struct psensor **s_cur = sensors;
 
        while (*s_cur) {
@@ -336,7 +343,7 @@ psensor_get_max_current_value(struct psensor **sensors, unsigned int type)
                if (s->enabled && (s->type & type)) {
                        double v = psensor_get_current_value(s);
 
-                       if (m == UNKNOWN_VALUE || v > m)
+                       if (m == UNKNOWN_DBL_VALUE || v > m)
                                m = v;
                }
 
@@ -369,41 +376,11 @@ double get_max_temp(struct psensor **sensors)
 struct psensor **get_all_sensors(int values_max_length)
 {
        struct psensor **psensors = NULL;
-       int count = 0;
-       const sensors_chip_name *chip;
-       int chip_nr = 0;
        struct psensor **tmp_psensors;
-       const sensors_feature *feature;
-       struct psensor *psensor;
-       int i;
-
-       while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
-               i = 0;
-               while ((feature = sensors_get_features(chip, &i))) {
-
-                       if (feature->type == SENSORS_FEATURE_TEMP
-                           || feature->type == SENSORS_FEATURE_FAN) {
 
-                               psensor = lmsensor_psensor_create
-                                       (chip, feature, values_max_length);
-
-                               if (psensor) {
-                                       tmp_psensors
-                                               = psensor_list_add(psensors,
-                                                                  psensor);
-
-                                       free(psensors);
-
-                                       psensors = tmp_psensors;
-
-                                       count++;
-                               }
-                       }
-               }
-       }
+       psensors = lmsensor_psensor_list_add(NULL, values_max_length);
 
        tmp_psensors = hdd_psensor_list_add(psensors, values_max_length);
-
        if (tmp_psensors != psensors) {
                free(psensors);
                psensors = tmp_psensors;
@@ -428,19 +405,57 @@ const char *psensor_type_to_str(unsigned int type)
        if (type & SENSOR_TYPE_LMSENSOR_FAN)
                return "Fan";
 
-       if (type & SENSOR_TYPE_NVIDIA)
+       if (type & SENSOR_TYPE_NVIDIA_TEMP)
                return "NVidia GPU Temperature";
 
+       if (type & SENSOR_TYPE_AMD_TEMP)
+               return "AMD GPU Temperature";
+
+       if (type & SENSOR_TYPE_AMD_FAN)
+               return "AMD GPU Fan Speed";
+
        if (type & SENSOR_TYPE_HDD_TEMP)
                return "HDD Temperature";
 
+       if (type & SENSOR_TYPE_CPU_USAGE)
+               return "CPU Usage";
+
        return "N/A";           /* should not be possible */
 }
 
+
+const char *psensor_type_to_unit_str(unsigned int type)
+{
+       if (type & SENSOR_TYPE_TEMP)
+               return _("C");
+
+       if (type & SENSOR_TYPE_FAN)
+               return _("RPM");
+
+       if (type & SENSOR_TYPE_CPU_USAGE)
+               return _("%");
+
+       return "N/A";
+}
+
 void psensor_list_update_measures(struct psensor **sensors)
 {
        lmsensor_psensor_list_update(sensors);
 
+#ifdef HAVE_GTOP
+       cpu_psensor_list_update(sensors);
+#endif
+
        if (psensor_list_contains_type(sensors, SENSOR_TYPE_HDD_TEMP))
                hdd_psensor_list_update(sensors);
 }
+
+void psensor_init()
+{
+       lmsensor_init();
+}
+
+void psensor_cleanup()
+{
+       lmsensor_cleanup();
+}