avoid configuration data in psensor struct
authorJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 26 Sep 2014 10:42:17 +0000 (12:42 +0200)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Fri, 26 Sep 2014 10:42:17 +0000 (12:42 +0200)
src/graph.c
src/lib/psensor.c
src/lib/psensor.h
src/main.c
src/ui_sensorlist.c
src/ui_sensorpref.c
src/ui_unity.c

index 0348d1a..9f87eda 100644 (file)
@@ -60,6 +60,29 @@ struct graph_info {
        GdkRGBA theme_fg_color;
 };
 
+static struct psensor **list_filter_graph_enabled(struct psensor **sensors)
+{
+       int n, i;
+       struct psensor **result, **cur, *s;
+
+       if (!sensors)
+               return NULL;
+
+       n = psensor_list_size(sensors);
+       result = malloc((n+1) * sizeof(struct psensor *));
+
+       for (cur = sensors, i = 0; *cur; cur++) {
+               s = *cur;
+
+               if (config_is_sensor_graph_enabled(s->id))
+                       result[i++] = s;
+       }
+
+       result[i] = NULL;
+
+       return result;
+}
+
 /* Return the end time of the graph i.e. the more recent measure.  If
  * no measure are available, return 0.
  * If Bezier curves are used return the measure n-3 to avoid to
@@ -449,7 +472,7 @@ graph_update(struct psensor **sensors,
        if (!gtk_widget_is_drawable(w_graph))
                return;
 
-       enabled_sensors = psensor_list_filter_graph_enabled(sensors);
+       enabled_sensors = list_filter_graph_enabled(sensors);
 
        min_rpm = get_min_rpm(enabled_sensors);
        max_rpm = get_max_rpm(enabled_sensors);
index 58731d5..d151ac1 100644 (file)
@@ -61,8 +61,6 @@ struct psensor *psensor_create(char *id,
 
        psensor->color = NULL;
 
-       psensor->graph_enabled = 1;
-
        psensor->provider_data = NULL;
        psensor->provider_data_free_fct = &free;
 
@@ -348,28 +346,6 @@ double get_max_value(struct psensor **sensors, int type)
        return m;
 }
 
-double
-psensor_get_max_current_value(struct psensor **sensors, unsigned int type)
-{
-       double m = UNKNOWN_DBL_VALUE;
-       struct psensor **s_cur = sensors;
-
-       while (*s_cur) {
-               struct psensor *s = *s_cur;
-
-               if (s->graph_enabled && (s->type & type)) {
-                       double v = psensor_get_current_value(s);
-
-                       if (m == UNKNOWN_DBL_VALUE || v > m)
-                               m = v;
-               }
-
-               s_cur++;
-       }
-
-       return m;
-}
-
 double get_min_temp(struct psensor **sensors)
 {
        return get_min_value(sensors, SENSOR_TYPE_TEMP);
@@ -488,26 +464,3 @@ psensor_current_value_to_str(const struct psensor *s, unsigned int use_celsius)
                                    psensor_get_current_value(s),
                                    use_celsius);
 }
-
-struct psensor **psensor_list_filter_graph_enabled(struct psensor **sensors)
-{
-       int n, i;
-       struct psensor **result, **cur, *s;
-
-       if (!sensors)
-               return NULL;
-
-       n = psensor_list_size(sensors);
-       result = malloc((n+1) * sizeof(struct psensor *));
-
-       for (cur = sensors, i = 0; *cur; cur++) {
-               s = *cur;
-
-               if (s->graph_enabled)
-                       result[i++] = s;
-       }
-
-       result[i] = NULL;
-
-       return result;
-}
index 505bd6f..3a3ffa7 100644 (file)
@@ -81,9 +81,6 @@ struct psensor {
        /* Color of the sensor used for the graph */
        struct color *color;
 
-       /* Whether the graph sensor is displayed. */
-       bool graph_enabled;
-
        /* see psensor_type */
        unsigned int type;
 
@@ -97,7 +94,7 @@ struct psensor {
        int alarm_low_threshold;
 
        /* Whether an alarm is raised for this sensor */
-       unsigned int alarm_raised;
+       bool alarm_raised;
 
        void (*cb_alarm_raised)(struct psensor *, void *);
        void *cb_alarm_raised_data;
@@ -126,8 +123,6 @@ void psensor_free(struct psensor *sensor);
 void psensor_list_free(struct psensor **sensors);
 int psensor_list_size(struct psensor **sensors);
 
-struct psensor **psensor_list_filter_graph_enabled(struct psensor **);
-
 struct psensor *psensor_list_get_by_id(struct psensor **sensors,
                                       const char *id);
 
@@ -139,10 +134,6 @@ double get_max_temp(struct psensor **sensors);
 double get_min_rpm(struct psensor **sensors);
 double get_max_rpm(struct psensor **sensors);
 
-/* Get the maximal current value of all sensors of a given type. */
-double
-psensor_get_max_current_value(struct psensor **sensors, unsigned int type);
-
 /*
  * Converts the value of a sensor to a string.
  *
index 3a126c5..f3a0ae0 100644 (file)
@@ -332,8 +332,6 @@ static void associate_preferences(struct psensor **sensors)
                char *n;
                struct psensor *s = *sensor_cur;
 
-               s->graph_enabled = config_is_sensor_graph_enabled(s->id);
-
                n = config_get_sensor_name(s->id);
 
                if (n) {
index 5f1cb4f..53c7336 100644 (file)
@@ -85,7 +85,8 @@ static void populate(struct ui_psensor *ui)
                gtk_list_store_set(store, &iter,
                                   COL_NAME, s->name,
                                   COL_COLOR_STR, scolor,
-                                  COL_GRAPH_ENABLED, s->graph_enabled,
+                                  COL_GRAPH_ENABLED,
+                                  config_is_sensor_graph_enabled(s->id),
                                   COL_SENSOR, s,
                                   COL_DISPLAY_ENABLED, enabled,
                                   -1);
@@ -323,6 +324,7 @@ void ui_sensorlist_cb_graph_toggled(GtkCellRendererToggle *cell,
        GtkTreePath *path;
        struct psensor *s, *s2;
        gboolean valid;
+       bool b;
 
        ui = (struct ui_psensor *)data;
        fmodel = gtk_tree_view_get_model(ui->sensors_tree);
@@ -333,8 +335,9 @@ void ui_sensorlist_cb_graph_toggled(GtkCellRendererToggle *cell,
 
        gtk_tree_model_get(fmodel, &iter, COL_SENSOR, &s, -1);
 
-       s->graph_enabled ^= 1;
-       config_set_sensor_graph_enabled(s->id, s->graph_enabled);
+       b = config_is_sensor_graph_enabled(s->id) ^ 1;
+       config_set_sensor_graph_enabled(s->id, b);
+
        config_sync();
 
        gtk_tree_path_free(path);
@@ -348,7 +351,7 @@ void ui_sensorlist_cb_graph_toggled(GtkCellRendererToggle *cell,
                        gtk_list_store_set(ui->sensors_store,
                                           &iter,
                                           COL_GRAPH_ENABLED,
-                                          s->graph_enabled,
+                                          b,
                                           -1);
                valid = gtk_tree_model_iter_next(model, &iter);
        }
index 282eed4..78fb551 100644 (file)
@@ -61,7 +61,7 @@ sensor_pref_new(struct psensor *s, struct config *cfg)
 
        p->sensor = s;
        p->name = strdup(s->name);
-       p->graph_enabled = s->graph_enabled;
+       p->graph_enabled = config_is_sensor_graph_enabled(s->id);
        p->alarm_enabled = config_get_sensor_alarm_enabled(s->id);
        p->color = color_dup(s->color);
        p->display_enabled = config_is_sensor_enabled(s->id);
@@ -366,10 +366,7 @@ static void apply_pref(struct sensor_pref *p, int pos, struct config *cfg)
                config_set_sensor_name(s->id, s->name);
        }
 
-       if (s->graph_enabled != p->graph_enabled) {
-               s->graph_enabled = p->graph_enabled;
-               config_set_sensor_graph_enabled(s->id, s->graph_enabled);
-       }
+       config_set_sensor_graph_enabled(s->id, p->graph_enabled);
 
        if (is_temp_type(s->type) && cfg->temperature_unit == FAHRENHEIT) {
                s->alarm_high_threshold
index 2c00403..0ab2e12 100644 (file)
@@ -18,6 +18,7 @@
  */
 #include <unity.h>
 
+#include <cfg.h>
 #include <temperature.h>
 #include <ui_unity.h>
 
@@ -25,6 +26,28 @@ static int initialized;
 static UnityLauncherEntry *psensor_entry;
 static unsigned int last_visible = -1;
 
+static double get_max_current_value(struct psensor **sensors, unsigned int type)
+{
+       double m, v;
+       struct psensor *s;
+
+       m = UNKNOWN_DBL_VALUE;
+       while (*sensors) {
+               s = *sensors;
+
+               if ((s->type & type) && config_is_sensor_graph_enabled(s->id)) {
+                       v = psensor_get_current_value(s);
+
+                       if (m == UNKNOWN_DBL_VALUE || v > m)
+                               m = v;
+               }
+
+               sensors++;
+       }
+
+       return m;
+}
+
 void ui_unity_launcher_entry_update(struct psensor **sensors,
                                    unsigned int show,
                                    int use_celsius)
@@ -50,7 +73,7 @@ void ui_unity_launcher_entry_update(struct psensor **sensors,
        }
 
        if (sensors && *sensors) {
-               v = psensor_get_max_current_value(sensors, SENSOR_TYPE_TEMP);
+               v = get_max_current_value(sensors, SENSOR_TYPE_TEMP);
 
                if (!use_celsius)
                        v = celsius_to_fahrenheit(v);