summaryrefslogtreecommitdiff
path: root/plugins/nvidia/nvidia-plugin.c
blob: 8cf9237c80eeff6369652cdc75f17fa0743f49a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2006 Sven Peter <svpe@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <X11/Xlib.h>
#include <NVCtrl/NVCtrl.h>
#include <NVCtrl/NVCtrlLib.h>

#include <glib.h>
#include <glib/gi18n.h>
#include "nvidia-plugin.h"

const gchar *plugin_name = "nvidia";

#define GPU_CORE_TEMP "CoreTemp"
#define AMBIENT_TEMP "AmbientTemp"

/* global variables */
Display *nvidia_sensors_dpy; /* the connection to the X server */

int num_gpus;

/* returns the value of the sensor */
static gdouble nvidia_plugin_get_sensor_value(const gchar *path, 
                                              const gchar *id, 
                                              SensorType type,
                                              GError **error)
{
	Bool res;
	int temp;
	int i;

	i = g_ascii_strtoll(id + strlen("GPU"), NULL, 10);
        if (g_ascii_strcasecmp(path, GPU_CORE_TEMP) == 0) {
		res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
						  NV_CTRL_TARGET_TYPE_GPU,
						  i,
						  0,
						  NV_CTRL_GPU_CORE_TEMPERATURE,
						  &temp);
        } else if (g_ascii_strcasecmp(path, AMBIENT_TEMP) == 0) {
		res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
						  NV_CTRL_TARGET_TYPE_GPU,
						  i,
						  0,
						  NV_CTRL_AMBIENT_TEMPERATURE,
						  &temp);
        } else {
                g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, 0, "Invalid path string passed to nvidia_plugin_get_sensor_value");
                return 0;
        }
                

        if (res != True) {
                /* when res isn't true something went wrong */
                g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, 0, "XNVCTRLQueryAttribute returned false");
                return 0;
        }
        
        /* convert the int to gdouble and return it */
        return (gdouble)temp;
}

/* creates the connection to the X server and checks whether the
 * NV-CONTROL extension is loaded */
static GList *nvidia_plugin_init(void)
{
	int dummy;
	int event_base, error_base;
        GList *sensors = NULL;

	/* create the connection to the X server */
	if (!(nvidia_sensors_dpy = XOpenDisplay(NULL))) {
		/* no connection to the X server avaible */
		return sensors;
	}

	/* check if the NV-CONTROL extension is available on this X
         * server - if so add the two sensors if they exist */
	if (XNVCTRLQueryExtension(nvidia_sensors_dpy, &event_base, &error_base)) {
		if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
					    NV_CTRL_TARGET_TYPE_GPU,
					    &num_gpus))
		{
			int i;
			for (i = 0; i < num_gpus; i++)
			{
				if (XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
								NV_CTRL_TARGET_TYPE_GPU,
								i,
							       	0, NV_CTRL_GPU_CORE_TEMPERATURE, &dummy)) {
					gchar *id = g_strdup_printf("GPU%d%s", i, GPU_CORE_TEMP);

					sensors_applet_plugin_add_sensor(&sensors,
									 GPU_CORE_TEMP,
									 id,
									 _("GPU"),
									 TEMP_SENSOR,
									 TRUE,
									 GPU_ICON,
									 DEFAULT_GRAPH_COLOR);
					g_free(id);
				}
				if (XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
								NV_CTRL_TARGET_TYPE_GPU,
								i,
							       	0, NV_CTRL_AMBIENT_TEMPERATURE, &dummy)) {
					gchar *id = g_strdup_printf("GPU%d%s", i, AMBIENT_TEMP);

					sensors_applet_plugin_add_sensor(&sensors,
                                                         AMBIENT_TEMP,
							 id,
                                                         _("Ambient"),
                                                         TEMP_SENSOR,
                                                         FALSE,
                                                         GENERIC_ICON,
                                                         DEFAULT_GRAPH_COLOR);
					g_free(id);
				}
			}
                }

        }
        return sensors;
}

const gchar *sensors_applet_plugin_name(void) 
{
        return plugin_name;
}

GList *sensors_applet_plugin_init(void) 
{
        return nvidia_plugin_init();
}

gdouble sensors_applet_plugin_get_sensor_value(const gchar *path, 
                                                const gchar *id, 
                                                SensorType type,
                                                GError **error) {
        return nvidia_plugin_get_sensor_value(path, id, type, error);
}