updated copyright to 2013
[psensor.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_err(_("Failed to retrieve NVIDIA temperature."));
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         int t;
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         t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP;
71
72         s = psensor_create(sid,
73                            strdup(name),
74                            strdup("Nvidia GPU"),
75                            t,
76                            values_len);
77
78         s->nvidia_id = id;
79
80         return s;
81 }
82
83 /*
84   Opens connection to X server and returns the number
85   of NVidia GPUs.
86
87   Return 0 if no NVidia gpus or cannot get information.
88 */
89 static int init()
90 {
91         int evt, err, n;
92
93         display = XOpenDisplay(NULL);
94
95         if (!display) {
96                 log_err(_("Cannot open connection to X11 server."));
97                 return 0;
98         }
99
100         if (XNVCTRLQueryExtension(display, &evt, &err) &&
101             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
102                 return n;
103
104         log_err(_("Failed to retrieve NVIDIA information."));
105
106         return 0;
107 }
108
109 void nvidia_psensor_list_update(struct psensor **sensors)
110 {
111         struct psensor **ss, *s;
112
113         ss = sensors;
114         while (*ss) {
115                 s = *ss;
116
117                 if (s->type & SENSOR_TYPE_NVCTRL)
118                         psensor_set_current_value(s, get_temp(s));
119
120                 ss++;
121         }
122 }
123
124 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
125                                          int values_len)
126 {
127         int i, n;
128         struct psensor **tmp, **ss, *s;
129
130         n = init();
131
132         ss = sensors;
133         for (i = 0; i < n; i++) {
134                 s = create_sensor(i, values_len);
135
136                 tmp = psensor_list_add(ss, s);
137
138                 if (ss != tmp)
139                         free(ss);
140
141                 ss = tmp;
142         }
143
144         return ss;
145 }
146
147 void nvidia_cleanup()
148 {
149         if (display) {
150                 XCloseDisplay(display);
151                 display = NULL;
152         }
153 }