removed useless whitespace
[psensor.git] / src / lib / nvidia.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
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.
8
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.
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
20 #include <locale.h>
21 #include <libintl.h>
22 #define _(str) gettext(str)
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <X11/Xlib.h>
29
30 #include <NVCtrl/NVCtrl.h>
31 #include <NVCtrl/NVCtrlLib.h>
32
33 #include "psensor.h"
34
35 Display *display;
36
37 /*
38   Returns the temperature (Celcius) of a NVidia GPU.
39 */
40 static int get_temp(struct psensor *sensor)
41 {
42         int temp;
43         Bool res;
44
45         res = XNVCTRLQueryTargetAttribute(display,
46                                           NV_CTRL_TARGET_TYPE_GPU,
47                                           sensor->nvidia_id,
48                                           0,
49                                           NV_CTRL_GPU_CORE_TEMPERATURE,
50                                           &temp);
51
52         if (res == True)
53                 return temp;
54
55         fprintf(stderr, _("ERROR: failed to retrieve nvidia temperature\n"));
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
65         sprintf(name, "GPU%d", id);
66
67         sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
68         sprintf(sid, "nvidia %s", name);
69
70         s = psensor_create(sid, strdup(name),
71                            SENSOR_TYPE_NVIDIA, values_len);
72
73         s->nvidia_id = id;
74
75         return s;
76 }
77
78 static void close_display()
79 {
80         XCloseDisplay(display);
81         display = NULL;
82
83 }
84
85 /*
86   Opens connection to X server and returns the number
87   of NVidia GPUs.
88
89   Return 0 if no NVidia gpus or cannot get information.
90 */
91 static int init()
92 {
93         int evt, err, n;
94
95         display = XOpenDisplay(NULL);
96
97         if (!display) {
98                 fprintf(stderr,
99                         _("ERROR: Cannot open connection to X Server\n"));
100                 return 0;
101         }
102
103         if (XNVCTRLQueryExtension(display, &evt, &err) &&
104             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
105                 return n;
106
107         fprintf(stderr, _("ERROR: Cannot retrieve NVidia information\n"));
108
109         return 0;
110 }
111
112 void nvidia_psensor_list_update(struct psensor **sensors)
113 {
114         struct psensor **ss, *s;
115
116         ss = sensors;
117         while (*ss) {
118                 s = *ss;
119
120                 if (s->type == SENSOR_TYPE_NVIDIA)
121                         psensor_set_current_value(s, get_temp(s));
122
123                 ss++;
124         }
125 }
126
127 struct psensor * *
128 nvidia_psensor_list_add(struct psensor **sensors, int values_len)
129 {
130         int i, n;
131         struct psensor **tmp, **ss, *s;
132
133         n = init();
134
135         ss = sensors;
136         for (i = 0; i < n; i++) {
137                 s = create_sensor(i, values_len);
138
139                 tmp = psensor_list_add(ss, s);
140
141                 if (ss != tmp)
142                         free(ss);
143
144                 ss = tmp;
145         }
146
147         return ss;
148 }
149
150 void nvidia_cleanup()
151 {
152         if (display)
153                 close_display();
154 }