2 Copyright (C) 2010-2011 jeanfi@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #define _(str) gettext(str)
30 #include <NVCtrl/NVCtrl.h>
31 #include <NVCtrl/NVCtrlLib.h>
36 Display *nvidia_sensors_dpy;
38 int nvidia_get_sensor_temp(struct psensor *sensor)
43 res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
44 NV_CTRL_TARGET_TYPE_GPU,
47 NV_CTRL_GPU_CORE_TEMPERATURE, &temp);
53 _("ERROR: failed to retrieve nvidia temperature\n"));
58 struct psensor *nvidia_create_sensor(int id, int values_max_length)
64 sprintf(name, "GPU%d", id);
66 sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
67 sprintf(sid, "nvidia %s", name);
69 s = psensor_create(sid, strdup(name),
70 SENSOR_TYPE_NVIDIA, values_max_length);
79 int event_base, error_base;
82 nvidia_sensors_dpy = XOpenDisplay(NULL);
84 if (!nvidia_sensors_dpy) {
85 fprintf(stderr, _("ERROR: nvidia initialization failure\n"));
89 if (XNVCTRLQueryExtension(nvidia_sensors_dpy, &event_base,
91 if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
92 NV_CTRL_TARGET_TYPE_GPU,
99 fprintf(stderr, _("ERROR: nvidia initialization failure: %d\n"),
105 void nvidia_psensor_list_update(struct psensor **sensors)
107 struct psensor **s_ptr = sensors;
110 struct psensor *sensor = *s_ptr;
112 if (sensor->type == SENSOR_TYPE_NVIDIA) {
113 int val = nvidia_get_sensor_temp(sensor);
115 psensor_set_current_value(sensor, (double)val);
122 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
123 int values_max_length)
126 int nvidia_gpus_count = nvidia_init();
127 struct psensor **res = sensors;
130 if (!nvidia_gpus_count) {
133 "no nvidia chips or initialization failure\n"));
136 for (i = 0; i < nvidia_gpus_count; i++) {
137 struct psensor *sensor
138 = nvidia_create_sensor(i, values_max_length);
140 struct psensor **tmp_psensors = psensor_list_add(res,