code styling
[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 /*
79   Opens connection to X server and returns the number
80   of NVidia GPUs.
81
82   Return 0 if no NVidia gpus or cannot get information.
83 */
84 static int init()
85 {
86         int evt, err, n;
87
88         display = XOpenDisplay(NULL);
89
90         if (!display) {
91                 fprintf(stderr,
92                         _("ERROR: Cannot open connection to X Server\n"));
93                 return 0;
94         }
95
96         if (XNVCTRLQueryExtension(display, &evt, &err) &&
97             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
98                 return n;
99
100         fprintf(stderr, _("ERROR: Cannot retrieve NVidia information\n"));
101
102         return 0;
103 }
104
105 void nvidia_psensor_list_update(struct psensor **sensors)
106 {
107         struct psensor **ss, *s;
108
109         ss = sensors;
110         while (*ss) {
111                 s = *ss;
112
113                 if (s->type == SENSOR_TYPE_NVIDIA)
114                         psensor_set_current_value(s, get_temp(s));
115
116                 ss++;
117         }
118 }
119
120 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
121                                          int values_len)
122 {
123         int i, n;
124         struct psensor **tmp, **ss, *s;
125
126         n = init();
127
128         ss = sensors;
129         for (i = 0; i < n; i++) {
130                 s = create_sensor(i, values_len);
131
132                 tmp = psensor_list_add(ss, s);
133
134                 if (ss != tmp)
135                         free(ss);
136
137                 ss = tmp;
138         }
139
140         return ss;
141 }
142
143 void nvidia_cleanup()
144 {
145         if (display) {
146                 XCloseDisplay(display);
147                 display = NULL;
148         }
149 }