OSDN Git Service

Added fans function
authorwifiextender <router@archlinux.info>
Tue, 28 Jul 2015 02:22:13 +0000 (02:22 +0000)
committerwifiextender <router@archlinux.info>
Tue, 28 Jul 2015 02:22:13 +0000 (02:22 +0000)
img/pic.png
src/functions.c
src/functions.h
src/main.c

index a90676e..dd0a727 100644 (file)
Binary files a/img/pic.png and b/img/pic.png differ
index b4bd461..71cbe90 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <time.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <string.h>
 #include <stdlib.h>
 #include <inttypes.h>
@@ -33,6 +34,8 @@
 
 #include "functions.h"
 
+#define UFINT "%"PRIuFAST16
+
 void get_temp(char *, char *);
 void get_temp(char *str1, char *str2)
 {
@@ -50,58 +53,6 @@ void get_temp(char *str1, char *str2)
     snprintf(str2, VLA, FMT_UINT, temp);
 }
 
-void get_voltage(char *str1)
-{
-    float voltage[4];
-    FILE *fp;
-    uint_fast16_t x = 0;
-
-    char *voltage_files[] =
-    {
-        HWMON_DIR"in0_input",
-        HWMON_DIR"in1_input",
-        HWMON_DIR"in2_input",
-        HWMON_DIR"in3_input"
-    };
-
-    for (x = 0; x < 4; x++)
-    {
-        if(!(fp = fopen(voltage_files[x], "r")))
-            exit(EXIT_FAILURE);
-
-        fscanf(fp, "%f", &voltage[x]);
-        fclose(fp);
-
-        voltage[x] /= (float)1000.0;
-    }
-
-    snprintf(str1, VLA, "%.2f %.2f %.2f %.2f",
-            voltage[0], voltage[1], voltage[2], voltage[3]);
-}
-
-void get_mobo(char *str1, char *str2)
-{
-    char vendor[VLA], name[VLA];
-
-    FILE *fp = fopen(MOBO_VENDOR, "r");
-    if (NULL == fp)
-        exit(EXIT_FAILURE);
-
-    /* use %[^\n] to get the whole line */
-    fscanf(fp, "%s", vendor);
-    fclose(fp);
-
-    if (!(fp = fopen(MOBO_NAME, "r")))
-        exit(EXIT_FAILURE);
-
-    /* use %[^\n] to get the whole line */
-    fscanf(fp, "%s", name);
-    fclose(fp);
-
-    get_temp(MOBO_TEMP_FILE, str2);
-
-    snprintf(str1, VLA*2, "%s %s", vendor, name);
-}
 
 void get_cpu(char *str1, char *str2)
 {
@@ -144,6 +95,7 @@ void get_cpu(char *str1, char *str2)
     FILL_ARR(str1, percent);
 }
 
+
 void get_ram(char *str1)
 {
     uintmax_t used = 0, total = 0, percent = 0;
@@ -193,6 +145,7 @@ static uint_fast16_t glob_packages(char *str1)
     return packs_num;
 }
 
+
 void get_packs(char *str1)
 {
     FILE *pkgs_file;
@@ -263,6 +216,99 @@ void get_kernel(char *str1)
 }
 
 
+void get_voltage(char *str1)
+{
+    float voltage[4];
+    FILE *fp;
+    uint_fast16_t x = 0;
+
+    char *voltage_files[] =
+    {
+        HWMON_DIR"in0_input",
+        HWMON_DIR"in1_input",
+        HWMON_DIR"in2_input",
+        HWMON_DIR"in3_input"
+    };
+
+    for (x = 0; x < 4; x++)
+    {
+        if (!(fp = fopen(voltage_files[x], "r")))
+            exit(EXIT_FAILURE);
+
+        fscanf(fp, "%f", &voltage[x]);
+        fclose(fp);
+
+        voltage[x] /= (float)1000.0;
+    }
+
+    snprintf(str1, VLA, "%.2f %.2f %.2f %.2f",
+            voltage[0], voltage[1], voltage[2], voltage[3]);
+}
+
+
+void get_fans(char *str1)
+{
+    FILE *fp;
+    bool found_fans = true;
+    char tempstr[VLA], buffer[VLA*5];
+    char *all_fans = buffer;
+    uint_fast16_t x = 0, z = 0, rpm[11];
+
+    for (x = 1; x < 10; x++, z++)
+    {
+        snprintf(tempstr, VLA, HWMON_DIR"fan"UFINT"_input", x);
+
+        if (!(fp = fopen(tempstr, "r")) && x > 1)
+            break;
+        else
+            if (NULL == fp) /* no system fans */
+            {
+                snprintf(str1, VLA, "%s", "Not found");
+                found_fans = false;
+                break;
+            }
+
+        fscanf(fp, UFINT, &rpm[z]);
+        fclose(fp);
+    }
+
+    if (found_fans)
+    {
+        for (x = 0; x < z; ++x)
+            all_fans += snprintf(all_fans,
+                sizeof(buffer) - (all_fans - buffer),
+                    UFINT" ", rpm[x]);
+
+        snprintf(str1, VLA, "%s", buffer);
+    }
+}
+
+
+void get_mobo(char *str1, char *str2)
+{
+    char vendor[VLA], name[VLA];
+
+    FILE *fp = fopen(MOBO_VENDOR, "r");
+    if (NULL == fp)
+        exit(EXIT_FAILURE);
+
+    /* use %[^\n] to get the whole line */
+    fscanf(fp, "%s", vendor);
+    fclose(fp);
+
+    if (!(fp = fopen(MOBO_NAME, "r")))
+        exit(EXIT_FAILURE);
+
+    /* use %[^\n] to get the whole line */
+    fscanf(fp, "%s", name);
+    fclose(fp);
+
+    get_temp(MOBO_TEMP_FILE, str2);
+
+    snprintf(str1, VLA*2, "%s %s", vendor, name);
+}
+
+
 void get_time(char *str1)
 {
     char time_str[VLA];
index c792db6..8ad6d80 100644 (file)
 #define MOBO_NAME DMI_DIR"board_name"
 #define MOBO_VENDOR DMI_DIR"board_vendor"
 
-void get_voltage(char *);
-void get_mobo(char *, char *);
+
 void get_cpu(char *, char *);
 void get_ram(char *);
 void get_ssd(char *);
 void get_packs(char *);
 void get_kernel(char *);
+void get_voltage(char *);
+void get_fans(char *);
+void get_mobo(char *, char *);
 void get_time(char *);
 void get_volume(char *);
 void set_status(const char *);
index 068cb45..ee41bbf 100644 (file)
@@ -35,6 +35,7 @@
 #define FMT_SSD   FMT
 #define FMT_PKGS  FMT_TIME"%c "
 #define FMT_VOLT  FMT_PKGS
+#define FMT_FANS  FMT_TIME
 #define FMT_KERN  "\x09%s%c "
 #define FMT_VOL   FMT
 #define COMMA     ','
@@ -44,30 +45,33 @@ int main(void)
     char packs[VLA] = GIVEN_DISTRO;
     char mobo[VLA], cpu[VLA], ram[VLA], ssd[VLA];
     char kern[VLA], volume[VLA], Time[VLA], combine[VLA*9];
-    char voltage[VLA], cpu_temp[VLA], mobo_temp[VLA];
+    char voltage[VLA], cpu_temp[VLA], mobo_temp[VLA], fans[VLA];
 
     get_cpu(cpu, cpu_temp);
 
     sleep(1);
 
-    get_voltage(voltage);
-    get_mobo(mobo, mobo_temp);
     get_cpu(cpu, cpu_temp);
     get_ram(ram);
     get_ssd(ssd);
     get_packs(packs);
     get_kernel(kern);
+    get_voltage(voltage);
+    get_fans(fans);
+    get_mobo(mobo, mobo_temp);
     get_volume(volume);
     get_time(Time);
 
     snprintf(combine, VLA*9,
-        FMT_CPU FMT_RAM FMT_SSD FMT_PKGS FMT_KERN FMT_VOLT FMT_MOBO FMT_VOL FMT_TIME,
+        FMT_CPU FMT_RAM FMT_SSD FMT_PKGS FMT_KERN
+        FMT_VOLT FMT_FANS FMT_MOBO FMT_VOL FMT_TIME,
         "CPU", cpu, cpu_temp, COMMA,
         "RAM", ram, COMMA,
         "SSD", ssd, COMMA,
         "Pkgs", packs, COMMA,
         kern, COMMA,
         "Voltage", voltage, COMMA,
+        "Fans/RPM", fans,
         "Mobo", mobo, mobo_temp, COMMA,
         "Volume", volume, COMMA,
         "Time", Time);