File: nvidia.c

package info (click to toggle)
xfce4-sensors-plugin 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,652 kB
  • sloc: sh: 11,446; ansic: 5,108; makefile: 225
file content (182 lines) | stat: -rw-r--r-- 4,681 bytes parent folder | download | duplicates (3)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Copyright (c) 2011 Amir Aupov <fads93@gmail.com>
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* Package includes */
#include <X11/Xlib.h>
#include <NVCtrl/NVCtrl.h>
#include <NVCtrl/NVCtrlLib.h>
#include <nvidia.h>
#include <types.h>
#include <sensors-interface-common.h>
#include <middlelayer.h>

/* Gtk/Glib includes */
#include <glib.h>

/* Global includes */
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

/* Global variables */
Display *nvidia_sensors_display;

/* Local functions */
int read_gpus (t_chip *chip);

/* Defines */
#define ZERO_KELVIN -273

int initialize_nvidia (GPtrArray *chips) {
	int retval;
	int num_gpus;
	t_chip *chip;
	t_chipfeature *chipfeature;

	TRACE ("enters initialize_nvidia");
	
	chip = g_new0 (t_chip, 1);
	//chip -> chip_name = g_strdup(_("nvidia"));
	chip -> chip_features = g_ptr_array_new ();
	chip -> num_features = 0;
	chip -> description = g_strdup(_("NVidia GPU core temperature"));
	chip -> name = g_strdup(_("nvidia"));
	chip -> sensorId = g_strdup("nvidia");
	chip -> type = GPU;

	num_gpus = read_gpus (chip);
	if (chip -> num_features > 0) {
		int i;
		for (i = 0; i < num_gpus; i++) {
			chipfeature = g_ptr_array_index (chip -> chip_features, i);
			g_assert (chipfeature != NULL);
			chipfeature -> address = i;
			chipfeature -> name = g_strdup(chipfeature -> devicename);
			chipfeature -> color = g_strdup ("#000000");
			chipfeature -> valid = TRUE;
			chipfeature -> formatted_value = g_strdup ("0.0");
			chipfeature -> raw_value = 0.0;
			chipfeature -> class = TEMPERATURE;
			chipfeature -> min_value = 10.0;
			chipfeature -> max_value = 50.0;
			chipfeature -> show = FALSE;
		}
		g_ptr_array_add (chips, chip);
		retval = 2;
	} else retval = 0;

	TRACE ("leaves initialize_nvidia");

	return retval;
}

double get_nvidia_value (int gpu) {
	int temp;

	TRACE ("enters get_nvidia_value for %d gpu", gpu);

	if (!(XNVCTRLQueryTargetAttribute (nvidia_sensors_display, 
				NV_CTRL_TARGET_TYPE_GPU,
				gpu,
				0,
				NV_CTRL_GPU_CORE_TEMPERATURE,
				&temp))) {
		TRACE ("NVCtrl doesn't work properly");
		return ZERO_KELVIN;
	}

	TRACE ("leaves get_nvidia_value for %d gpu", gpu);

	return (double) (1.0 * temp);
}

void refresh_nvidia (gpointer chip_feature, gpointer data) {
	t_chipfeature *cf;
	double value;
	t_sensors *sensors;

	g_assert (chip_feature != NULL);

	TRACE ("enters refresh_nvidia");

	if (data) sensors = (t_sensors *) data;	
	cf = (t_chipfeature *) chip_feature;
	value = get_nvidia_value (cf -> address);
	if (value == ZERO_KELVIN) return; 

	g_free (cf -> formatted_value);
	cf -> formatted_value = g_strdup_printf(_("%.1f °C"), value);
	cf -> raw_value = value;

	TRACE ("leaves refresh_nvidia");

}

int read_gpus (t_chip *chip) {
	t_chipfeature *chipfeature;
	int num_gpus;
	int event, error;
	int i;

	TRACE ("enters read_gpus");

	/* create the connection to the X server */
	if (!(nvidia_sensors_display = XOpenDisplay (NULL))) {
		TRACE ("failed to connect to X server");
		return 0;
	}

	/* check if the NVCtrl is available on this X server
	 * if so - add sensors*/
	if (!(XNVCTRLQueryExtension (nvidia_sensors_display, 
			&event, &error))) {
		TRACE ("NVCtrl is not available");
		return 0;
	}
	
	if (!(XNVCTRLQueryTargetCount (nvidia_sensors_display,
			NV_CTRL_TARGET_TYPE_GPU,
			&num_gpus))) {
		TRACE ("No NVidia devices found");
		return 0;
	}
	
	for (i = 0; i < num_gpus; i++){ 
		gchar *device_name = (gchar*) malloc (100 * sizeof(gchar));
		if (XNVCTRLQueryTargetStringAttribute (
					nvidia_sensors_display,
					NV_CTRL_TARGET_TYPE_GPU, 
					i,
					0,
					NV_CTRL_STRING_PRODUCT_NAME,
					&device_name))
			TRACE ("GPU%d:%s", i, device_name);
		chipfeature = g_new0 (t_chipfeature, 1);
		chipfeature -> devicename = g_strdup (device_name);
		chipfeature -> name = g_strdup (device_name);
		g_ptr_array_add (chip -> chip_features, chipfeature);
		chip -> num_features++;
	}

	TRACE ("leaves read_gpus");
	return num_gpus;
}