Imported Upstream version 0.8.0.4
[psensor-pkg-debian.git] / src / lib / nvidia.c
1 /*
2  * Copyright (C) 2010-2013 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 /*
37   Returns the temperature (Celcius) of a NVidia GPU.
38 */
39 static int get_temp(struct psensor *sensor)
40 {
41         int temp;
42         Bool res;
43
44         res = XNVCTRLQueryTargetAttribute(display,
45                                           NV_CTRL_TARGET_TYPE_GPU,
46                                           sensor->nvidia_id,
47                                           0,
48                                           NV_CTRL_GPU_CORE_TEMPERATURE,
49                                           &temp);
50
51         if (res == True)
52                 return temp;
53
54         log_debug(_("NVIDIA proprietary driver not used or cannot "
55                     "retrieve NVIDIA GPU temperature."));
56         return 0;
57 }
58
59 static struct psensor *create_sensor(int id, int values_len)
60 {
61         char name[200];
62         char *sid;
63         struct psensor *s;
64         int t;
65
66         sprintf(name, "GPU%d", id);
67
68         sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
69         sprintf(sid, "nvidia %s", name);
70
71         t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP;
72
73         s = psensor_create(sid,
74                            strdup(name),
75                            strdup("Nvidia GPU"),
76                            t,
77                            values_len);
78
79         s->nvidia_id = id;
80
81         return s;
82 }
83
84 /*
85   Opens connection to X server and returns the number
86   of NVidia GPUs.
87
88   Return 0 if no NVidia gpus or cannot get information.
89 */
90 static int init()
91 {
92         int evt, err, n;
93
94         display = XOpenDisplay(NULL);
95
96         if (!display) {
97                 log_err(_("Cannot open connection to X11 server."));
98                 return 0;
99         }
100
101         if (XNVCTRLQueryExtension(display, &evt, &err) &&
102             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
103                 return n;
104
105         log_err(_("Failed to retrieve NVIDIA information."));
106
107         return 0;
108 }
109
110 void nvidia_psensor_list_update(struct psensor **sensors)
111 {
112         struct psensor **ss, *s;
113
114         ss = sensors;
115         while (*ss) {
116                 s = *ss;
117
118                 if (s->type & SENSOR_TYPE_NVCTRL)
119                         psensor_set_current_value(s, get_temp(s));
120
121                 ss++;
122         }
123 }
124
125 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
126                                          int values_len)
127 {
128         int i, n;
129         struct psensor **tmp, **ss, *s;
130
131         n = init();
132
133         ss = sensors;
134         for (i = 0; i < n; i++) {
135                 s = create_sensor(i, values_len);
136
137                 tmp = psensor_list_add(ss, s);
138
139                 if (ss != tmp)
140                         free(ss);
141
142                 ss = tmp;
143         }
144
145         return ss;
146 }
147
148 void nvidia_cleanup()
149 {
150         if (display) {
151                 XCloseDisplay(display);
152                 display = NULL;
153         }
154 }