コード例 #1
0
void sensors_applet_plugin_add_sensor(GList **sensors,
                                      const gchar *path,
                                      const gchar *id,
                                      const gchar *label,
                                      SensorType type,
                                      gboolean enable,
                                      IconType icon,
                                      const gchar *graph_color) 
{
        SensorsAppletSensorInfo *info;
        
        info = g_malloc0(sizeof(*info));

        info->path = g_strdup(path);
        info->id = g_strdup(id);
        info->label = g_strdup(label);
        info->type = type;
        info->enable = enable;
        sensors_applet_plugin_default_sensor_limits(type,
                                                    &(info->low_value),
                                                    &(info->high_value));
        info->multiplier = 1.0;
        info->offset = 0.0;
        info->icon = icon;
        info->graph_color = g_strdup(graph_color);

        *sensors = g_list_append(*sensors, info);
}
コード例 #2
0
static GList *libsensors_plugin_get_sensors(void) {
	const sensors_chip_name *chip_name;
	int i;
        GList *sensors = NULL;

#if SENSORS_API_VERSION < 0x400        
	FILE *file;
        g_debug("%s: using libsensors version < 4", __FUNCTION__);

	/* try to open config file, otherwise try alternate config
	 * file - if neither succeed, exit */
	if ((file = fopen (LIBSENSORS_CONFIG_FILE, "r")) == NULL) {
		if ((file = fopen (LIBSENSORS_ALTERNATIVE_CONFIG_FILE, "r")) == NULL) {
                        g_debug("%s: error opening libsensors config file... ", __FUNCTION__);
			return sensors;
		}
	}
	
	/* at this point should have an open config file, if is not
	 * valid, close file and return */
	if (sensors_init(file) != 0) {
		fclose(file);
                g_debug("%s: error initing libsensors from config file...", __FUNCTION__);
		return sensors;
	}
	fclose(file);

	/* libsensors exposes a number of chips -  ... */
	i = 0;
	while ((chip_name = sensors_get_detected_chips (&i)) != NULL) {
		char *chip_name_string;
		const sensors_feature_data *data;
		int n1 = 0, n2 = 0;
                
		chip_name_string = get_chip_name_string(chip_name);
		
		/* ... each of which has one or more 'features' ... */
		while ((data = sensors_get_all_features (*chip_name, &n1, &n2)) != NULL) { // error
                        // fill in list for us
                        check_sensor_with_data(&sensors, chip_name_string, 
                                               chip_name, &n1, &n2, data);
                }
		g_free (chip_name_string);
	}

#else
        g_debug("%s: using libsensors version >= 4", __FUNCTION__);
        
        int nr = 0;
        if (sensors_init(NULL) != 0) {
                g_debug("%s: error initing libsensors", __FUNCTION__);
                return sensors;
        }
	i = 0;
	while ((chip_name = sensors_get_detected_chips(NULL, &nr)))
        {
                char *chip_name_string, *label;                
                const sensors_subfeature *input_feature;
                const sensors_subfeature *low_feature;
                const sensors_subfeature *high_feature;
		const sensors_feature *main_feature;
                SensorType type;
		gint nr1 = 0;
                gdouble value, low, high;
                gchar *path;
                gboolean visible;
                IconType icon;
                
		chip_name_string = get_chip_name_string(chip_name);
                if (chip_name_string == NULL) {
                        g_debug("%s: %d: error getting name string for sensor: %s\n",
                                        __FILE__, __LINE__, chip_name->path);
                        continue;
                }
                
		while ((main_feature = sensors_get_features(chip_name, &nr1)))
                {
			switch (main_feature->type)
                        {
                        case SENSORS_FEATURE_IN:
				type = VOLTAGE_SENSOR;
				input_feature = sensors_get_subfeature(chip_name, 
                                                                    main_feature, 
                                                                    SENSORS_SUBFEATURE_IN_INPUT);
				low_feature = sensors_get_subfeature(chip_name, 
                                                                     main_feature, 
                                                                     SENSORS_SUBFEATURE_IN_MIN);
				high_feature = sensors_get_subfeature(chip_name, 
                                                                      main_feature, 
                                                                      SENSORS_SUBFEATURE_IN_MAX);
			  	break;
                        case SENSORS_FEATURE_FAN:
				type = FAN_SENSOR;
				input_feature = sensors_get_subfeature(chip_name, 
                                                                    main_feature, 
                                                                    SENSORS_SUBFEATURE_FAN_INPUT);
				low_feature = sensors_get_subfeature(chip_name, 
                                                                     main_feature, 
                                                                     SENSORS_SUBFEATURE_FAN_MIN);
                                // no fan max feature
				high_feature = NULL;
				break;
                        case SENSORS_FEATURE_TEMP:
				type = TEMP_SENSOR;
				input_feature = sensors_get_subfeature(chip_name, 
                                                                    main_feature, 
                                                                    SENSORS_SUBFEATURE_TEMP_INPUT);
				low_feature = sensors_get_subfeature(chip_name, 
                                                                     main_feature, 
                                                                     SENSORS_SUBFEATURE_TEMP_MIN);
				high_feature = sensors_get_subfeature(chip_name, 
                                                                      main_feature, 
                                                                      SENSORS_SUBFEATURE_TEMP_MAX);
				if (!high_feature)
					high_feature = sensors_get_subfeature(chip_name, 
									      main_feature, 
									      SENSORS_SUBFEATURE_TEMP_CRIT);
				break;
                        default:
                                g_debug("%s: %d: error determining type for: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
				continue;
                        }
                        
			if (!input_feature)
                        {
                                g_debug("%s: %d: could not get input subfeature for: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
				continue;
                        }
                        // if still here we got input feature so get label
                        label = sensors_get_label(chip_name, main_feature);
                        if (!label) 
                        {
                                g_debug("%s: %d: error: could not get label for: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
                                continue;
                        }

                        g_assert(chip_name_string && label);
                        
                        icon = get_sensor_icon(type);
                        visible = (type == TEMP_SENSOR ? TRUE : FALSE);
                        sensors_applet_plugin_default_sensor_limits(type, 
                                                                    &low, 
                                                                    &high);                        
                        if (low_feature) {
                                sensors_get_value(chip_name, low_feature->number, &low);
                        }

                        if (high_feature) {
                                sensors_get_value(chip_name, high_feature->number, &high);
                        }
                        
                                
                        if (sensors_get_value(chip_name, input_feature->number, &value) < 0) {
                                g_debug("%s: %d: error: could not get value for input feature of sensor: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
                                free(label);
                                continue;
                        }

                        g_debug("for chip %s (type %s) got label %s and value %f", chip_name_string, 
                                (type == TEMP_SENSOR ? "temp" :
                                 (type == FAN_SENSOR ? "fan" :
                                  (type == VOLTAGE_SENSOR ? "voltage" : "error"))), label, value);

                        path = g_strdup_printf ("sensor://%s/%d", chip_name_string, input_feature->number);
			g_hash_table_insert(hash_table, g_strdup(path), (void *)chip_name);
                        sensors_applet_plugin_add_sensor_with_limits(&sensors,
                                                                     path,
                                                                     label,
                                                                     label,
                                                                     type, 
                                                                     visible,
                                                                     low, 
                                                                     high,
                                                                     icon,
                                                                     DEFAULT_GRAPH_COLOR);
                }
                g_free(chip_name_string);
                
                
        }
#endif

        return sensors;
}
コード例 #3
0
static void check_sensor_with_data(GList **sensors,
                                   const char * const chip_name,
                                   const sensors_chip_name *chip,
                                   int *n1, int *n2,
                                   const sensors_feature_data *data) 
{
        char *label;
	double value;
        SensorsAppletSensorInfo *sensor_info = NULL;
        
        /* ... some of which are boring ... */
        if ((label = get_sensor_interesting_label (*chip, data->number))) {
                /* ... and some of which are actually sensors */
                if ((data->mode & SENSORS_MODE_R) && 
                    (data->mapping == SENSORS_NO_MAPPING) && 
                    (sensors_get_feature(*chip, data->number, &value) == 0) // make sure we can actually get a value for it
                        ) {
                        SensorType type;
                        gboolean visible;
                        IconType icon;
                        gdouble low_value, high_value;
			
                        gchar *url;
			
                        type = get_sensor_type (data->name);
                        visible = (type == TEMP_SENSOR ? TRUE : FALSE);
                        icon = get_sensor_icon(type);
                        
                        // the 'path' contains all the information we need to
                        // identify this sensor later
                        url = g_strdup_printf ("sensor://%s/%d", chip_name, data->number);
                        
                        // get low and high values
                        sensors_applet_plugin_default_sensor_limits(type, 
                                                                    &low_value,
                                                                    &high_value);
                        
                        data = get_sensor_min_max(chip, n1, n2, data->number,
                                                  &low_value, &high_value);                        
                        if (data != NULL) {
                                // try adding this one
                                                        // at this point we have called sensors_get_all_features() and stopped when we have a potential new sensor, so make sure we try this as well - do a recursive call
                                check_sensor_with_data(sensors, chip_name, chip, n1, n2, data);
                                        
                        }

                        g_hash_table_insert(hash_table, g_strdup(url), (void *)chip);
                        // the id identifies a particular sensor for the user;
                        // we default to the label returned by libsensors
                        sensors_applet_plugin_add_sensor_with_limits(sensors,
                                                                     url,
                                                                     label, 
                                                                     label,
                                                                     type, 
                                                                     visible,
                                                                     low_value, 
                                                                     high_value, 
                                                                     icon,
                                                                     DEFAULT_GRAPH_COLOR);                        
                        g_free(url);
                }
                free (label);
        }
        
}