thermal: prepare for multiple sensors
authorHenry Gebhardt <hsggebhardt@googlemail.com>
Fri, 30 Dec 2011 21:47:00 +0000 (22:47 +0100)
committerMartin Bagge / brother <brother@bsnet.se>
Sun, 20 May 2012 13:57:26 +0000 (15:57 +0200)
src/plugins/thermal/thermal.c

index 1ac8093..95b4174 100644 (file)
@@ -224,39 +224,41 @@ update_display(thermal *th)
 }
 
 
-/* get_sensor():
+static void
+add_sensor(thermal* th, char const* sensor_path)
+{
+    if (th->sensor)
+        g_free(th->sensor);
+
+    th->sensor = g_strdup(sensor_path);
+}
+
+/* find_sensors():
  *      - Get the sensor directory, and store it in '*sensor'.
  *      - It is searched for in 'directory'.
  *      - Only the subdirectories starting with 'subdir_prefix' are accepted as sensors.
  *      - 'subdir_prefix' may be NULL, in which case any subdir is considered a sensor. */
 static void
-get_sensor(char** sensor, char const* directory, char const* subdir_prefix)
+find_sensors(thermal* th, char const* directory, char const* subdir_prefix)
 {
     GDir *sensorsDirectory;
     const char *sensor_name;
     char sensor_path[100];
 
     if (! (sensorsDirectory = g_dir_open(directory, 0, NULL)))
-    {
-        *sensor = NULL;
         return;
-    }
 
     /* Scan the thermal_zone directory for available sensors */
     while ((sensor_name = g_dir_read_name(sensorsDirectory))) {
-        if (sensor_name[0] != '.') {
-            if (subdir_prefix) {
-                if (strncmp(sensor_name, subdir_prefix, strlen(subdir_prefix)) != 0)
-                    continue;
-            }
-            sprintf(sensor_path,"%s%s/", directory, sensor_name);
-            if(*sensor) {
-                g_free(*sensor);
-                *sensor = NULL;
-            }
-            *sensor = strdup(sensor_path);
-            break;
+        if (sensor_name[0] == '.')
+            continue;
+        if (subdir_prefix) {
+            if (strncmp(sensor_name, subdir_prefix, strlen(subdir_prefix)) != 0)
+                continue;
         }
+        sprintf(sensor_path,"%s%s/", directory, sensor_name);
+        add_sensor(th, sensor_path);
+        break;
     }
     g_dir_close(sensorsDirectory);
 }
@@ -269,12 +271,10 @@ check_sensors( thermal *th )
         th->sensor = NULL;
     }
 
-    get_sensor(&th->sensor, PROC_THERMAL_DIRECTORY, NULL);
+    find_sensors(th, PROC_THERMAL_DIRECTORY, NULL);
 
     if (!th->sensor)
-        get_sensor(&th->sensor, SYSFS_THERMAL_DIRECTORY, SYSFS_THERMAL_SUBDIR_PREFIX);
-
-    //printf("thermal sensor: %s\n", th->sensor);
+        find_sensors(th, SYSFS_THERMAL_DIRECTORY, SYSFS_THERMAL_SUBDIR_PREFIX);
 }