Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / src / lib / nvidia.c
1 /*
2  * Copyright (C) 2010-2012 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         fprintf(stderr, _("ERROR: failed to retrieve nvidia temperature\n"));
55         return 0;
56 }
57
58 static struct psensor *create_sensor(int id, int values_len)
59 {
60         char name[200];
61         char *sid;
62         struct psensor *s;
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         s = psensor_create(sid, strdup(name),
70                            SENSOR_TYPE_NVIDIA_TEMP, values_len);
71
72         s->nvidia_id = id;
73
74         return s;
75 }
76
77 /*
78   Opens connection to X server and returns the number
79   of NVidia GPUs.
80
81   Return 0 if no NVidia gpus or cannot get information.
82 */
83 static int init()
84 {
85         int evt, err, n;
86
87         display = XOpenDisplay(NULL);
88
89         if (!display) {
90                 fprintf(stderr,
91                         _("ERROR: Cannot open connection to X Server\n"));
92                 return 0;
93         }
94
95         if (XNVCTRLQueryExtension(display, &evt, &err) &&
96             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
97                 return n;
98
99         fprintf(stderr, _("ERROR: Cannot retrieve NVidia information\n"));
100
101         return 0;
102 }
103
104 void nvidia_psensor_list_update(struct psensor **sensors)
105 {
106         struct psensor **ss, *s;
107
108         ss = sensors;
109         while (*ss) {
110                 s = *ss;
111
112                 if (s->type == SENSOR_TYPE_NVIDIA_TEMP)
113                         psensor_set_current_value(s, get_temp(s));
114
115                 ss++;
116         }
117 }
118
119 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
120                                          int values_len)
121 {
122         int i, n;
123         struct psensor **tmp, **ss, *s;
124
125         n = init();
126
127         ss = sensors;
128         for (i = 0; i < n; i++) {
129                 s = create_sensor(i, values_len);
130
131                 tmp = psensor_list_add(ss, s);
132
133                 if (ss != tmp)
134                         free(ss);
135
136                 ss = tmp;
137         }
138
139         return ss;
140 }
141
142 void nvidia_cleanup()
143 {
144         if (display) {
145                 XCloseDisplay(display);
146                 display = NULL;
147         }
148 }