Fixed restoration of the panel divider position.
[psensor.git] / src / lib / nvidia.c
index 4f2c67a..685f97d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2014 jeanfi@gmail.com
+ * Copyright (C) 2010-2016 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
 #include <NVCtrl/NVCtrl.h>
 #include <NVCtrl/NVCtrlLib.h>
 
+#include <nvidia.h>
 #include <psensor.h>
 
-Display *display;
+static Display *display;
+
+static const char *PROVIDER_NAME = "nvctrl";
+
+static void set_nvidia_id(struct psensor *s, int id)
+{
+       *(int *)s->provider_data = id;
+}
+
+static int get_nvidia_id(struct psensor *s)
+{
+       return *(int *)s->provider_data;
+}
 
 static char *get_product_name(int id, int type)
 {
@@ -53,10 +66,14 @@ static char *get_product_name(int id, int type)
                if (strcmp(name, "Unknown"))
                        return name;
 
-               log_err(_("Unknown NVIDIA product name for GPU %d"), id);
+               log_err(_("%s: Unknown NVIDIA product name for GPU %d"),
+                       PROVIDER_NAME,
+                       id);
                free(name);
        } else {
-               log_err(_("Failed to retrieve NVIDIA product name for GPU %d"),
+               log_err(_("%s: "
+                         "Failed to retrieve NVIDIA product name for GPU %d"),
+                       PROVIDER_NAME,
                        id);
        }
 
@@ -213,14 +230,18 @@ static double get_value(int id, int type)
 static void update(struct psensor *sensor)
 {
        double v;
+       int id;
+
+       id = get_nvidia_id(sensor);
 
-       v = get_value(sensor->nvidia_id, sensor->type);
+       v = get_value(id, sensor->type);
 
        if (v == UNKNOWN_DBL_VALUE)
-               log_err(_("Failed to retrieve measure of type %x "
+               log_err(_("%s: Failed to retrieve measure of type %x "
                          "for NVIDIA GPU %d"),
+                       PROVIDER_NAME,
                        sensor->type,
-                       sensor->nvidia_id);
+                       id);
        psensor_set_current_value(sensor, v);
 }
 
@@ -235,7 +256,8 @@ static char *i2str(int i)
        size_t n;
 
        /* second +1 to avoid issue about the conversion of a double
-        * to a lower int */
+        * to a lower int
+        */
        n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
 
        str = malloc(n);
@@ -251,6 +273,7 @@ static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
        int type;
        size_t n;
        struct psensor *s;
+       double v;
 
        type = SENSOR_TYPE_NVCTRL | subtype;
 
@@ -265,48 +288,58 @@ static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
        name = malloc(n);
        sprintf(name, "%s %s %s", pname, strnid, stype);
 
-       sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
-       sprintf(sid, "nvidia %s", name);
+       sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
+       sprintf(sid, "%s %s", PROVIDER_NAME, name);
 
        s = psensor_create(sid, name, pname, type, value_len);
-       s->nvidia_id = id;
+       s->provider_data = malloc(sizeof(int));
+       set_nvidia_id(s, id);
+
+       if ((type & SENSOR_TYPE_GPU) && (type & SENSOR_TYPE_TEMP)) {
+               v = get_att(NV_CTRL_TARGET_TYPE_GPU,
+                           id,
+                           NV_CTRL_GPU_CORE_THRESHOLD);
+               s->max = v;
+       }
 
        free(strnid);
 
        return s;
 }
 
-static int init()
+static int init(void)
 {
        int evt, err;
 
        display = XOpenDisplay(NULL);
 
        if (!display) {
-               log_err(_("Cannot open connection to X11 server."));
+               log_err(_("%s: Cannot open connection to X11 server."),
+                       PROVIDER_NAME);
                return 0;
        }
 
        if (XNVCTRLQueryExtension(display, &evt, &err))
                return 1;
 
-       log_err(_("Failed to retrieve NVIDIA information."));
+       log_err(_("%s: Failed to retrieve NVIDIA information."),
+               PROVIDER_NAME);
 
        return 0;
 }
 
 void nvidia_psensor_list_update(struct psensor **sensors)
 {
-       struct psensor **ss, *s;
+       struct psensor *s;
 
-       ss = sensors;
-       while (*ss) {
-               s = *ss;
+       while (*sensors) {
+               s = *sensors;
 
-               if (s->type & SENSOR_TYPE_NVCTRL)
+               if (!(s->type & SENSOR_TYPE_REMOTE)
+                   && s->type & SENSOR_TYPE_NVCTRL)
                        update(s);
 
-               ss++;
+               sensors++;
        }
 }
 
@@ -320,52 +353,50 @@ static void add(struct psensor ***sensors, int id, int type, int values_len)
                psensor_list_append(sensors, s);
 }
 
-struct psensor **
-nvidia_psensor_list_add(struct psensor **ss, int values_len)
+void nvidia_psensor_list_append(struct psensor ***ss, int values_len)
 {
        int i, n, utype;
        Bool ret;
 
        if (!init())
-               return ss;
+               return;
 
        ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
        if (ret == True) {
                for (i = 0; i < n; i++) {
-                       add(&ss,
+                       add(ss,
                            i,
                            SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
                            values_len);
 
                        utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
-                       add(&ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
-                       add(&ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
-                       add(&ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
-                       add(&ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
-                       add(&ss, i, utype | SENSOR_TYPE_PCIE, values_len);
+                       add(ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
+                       add(ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
+                       add(ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
+                       add(ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
+                       add(ss, i, utype | SENSOR_TYPE_PCIE, values_len);
                }
        }
 
        ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
        if (ret == True) {
-               log_debug("NVIDIA: number of fans: %d", n);
+               log_fct("%s: Number of fans: %d", PROVIDER_NAME, n);
                for (i = 0; i < n; i++) {
                        utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
                        if (check_sensor(i, utype))
-                               add(&ss, i, utype, values_len);
+                               add(ss, i, utype, values_len);
 
                        utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
                        if (check_sensor(i, utype))
-                               add(&ss, i, utype, values_len);
+                               add(ss, i, utype, values_len);
                }
        } else {
-               log_err(_("NVIDIA: failed to retrieve number of fans."));
+               log_err(_("%s: Failed to retrieve number of fans."),
+                       PROVIDER_NAME);
        }
-
-       return ss;
 }
 
-void nvidia_cleanup()
+void nvidia_cleanup(void)
 {
        if (display) {
                XCloseDisplay(display);