Merge tag 'upstream/1.0.4'
[psensor-pkg-debian.git] / src / lib / nvidia.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <X11/Xlib.h>
28
29 #include <NVCtrl/NVCtrl.h>
30 #include <NVCtrl/NVCtrlLib.h>
31
32 #include "psensor.h"
33
34 Display *display;
35
36 /* Returns the temperature (Celsius) of a NVIDIA GPU. */
37 static int get_temp(struct psensor *sensor)
38 {
39         int temp;
40         Bool res;
41
42         res = XNVCTRLQueryTargetAttribute(display,
43                                           NV_CTRL_TARGET_TYPE_GPU,
44                                           sensor->nvidia_id,
45                                           0,
46                                           NV_CTRL_GPU_CORE_TEMPERATURE,
47                                           &temp);
48
49         if (res == True)
50                 return temp;
51
52         log_debug(_("NVIDIA proprietary driver not used or cannot "
53                     "retrieve NVIDIA GPU temperature."));
54         return 0;
55 }
56
57 static struct psensor *create_sensor(int id, int values_len)
58 {
59         char name[200];
60         char *sid;
61         struct psensor *s;
62         int t;
63
64         sprintf(name, "GPU%d", id);
65
66         sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
67         sprintf(sid, "NVIDIA %s", name);
68
69         t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP;
70
71         s = psensor_create(sid,
72                            strdup(name),
73                            strdup(_("NVIDIA GPU")),
74                            t,
75                            values_len);
76
77         s->nvidia_id = id;
78
79         return s;
80 }
81
82 /*
83   Opens connection to X server and returns the number
84   of NVIDIA GPUs.
85
86   Return 0 if no NVIDIA gpus or cannot get information.
87 */
88 static int init()
89 {
90         int evt, err, n;
91
92         display = XOpenDisplay(NULL);
93
94         if (!display) {
95                 log_err(_("Cannot open connection to X11 server."));
96                 return 0;
97         }
98
99         if (XNVCTRLQueryExtension(display, &evt, &err) &&
100             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
101                 return n;
102
103         log_err(_("Failed to retrieve NVIDIA information."));
104
105         return 0;
106 }
107
108 void nvidia_psensor_list_update(struct psensor **sensors)
109 {
110         struct psensor **ss, *s;
111
112         ss = sensors;
113         while (*ss) {
114                 s = *ss;
115
116                 if (s->type & SENSOR_TYPE_NVCTRL)
117                         psensor_set_current_value(s, get_temp(s));
118
119                 ss++;
120         }
121 }
122
123 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
124                                          int values_len)
125 {
126         int i, n;
127         struct psensor **tmp, **ss, *s;
128
129         n = init();
130
131         ss = sensors;
132         for (i = 0; i < n; i++) {
133                 s = create_sensor(i, values_len);
134
135                 tmp = psensor_list_add(ss, s);
136
137                 if (ss != tmp)
138                         free(ss);
139
140                 ss = tmp;
141         }
142
143         return ss;
144 }
145
146 void nvidia_cleanup()
147 {
148         if (display) {
149                 XCloseDisplay(display);
150                 display = NULL;
151         }
152 }