summaryrefslogtreecommitdiff
path: root/sensors-applet/plugins/nvidia/nvidia-plugin.c
diff options
context:
space:
mode:
authormbkma <[email protected]>2026-01-31 12:11:25 +0100
committermbkma <[email protected]>2026-01-31 12:11:25 +0100
commitbb4fa0ea945f9dd4d0bff0182c381c2f14f78ae3 (patch)
tree523c91356c59557d0bcfa0a878f4e5cadd0c4ab2 /sensors-applet/plugins/nvidia/nvidia-plugin.c
parentd433bbd9aeb7ae3377e59c70f6608c3e29f9ebbc (diff)
downloadmate-applets-integrate-all-applets.tar.bz2
mate-applets-integrate-all-applets.tar.xz
merge mate-indicator-applet and sensors-applet into mate-appletsintegrate-all-applets
Diffstat (limited to 'sensors-applet/plugins/nvidia/nvidia-plugin.c')
-rw-r--r--sensors-applet/plugins/nvidia/nvidia-plugin.c222
1 files changed, 222 insertions, 0 deletions
diff --git a/sensors-applet/plugins/nvidia/nvidia-plugin.c b/sensors-applet/plugins/nvidia/nvidia-plugin.c
new file mode 100644
index 00000000..2643a2a6
--- /dev/null
+++ b/sensors-applet/plugins/nvidia/nvidia-plugin.c
@@ -0,0 +1,222 @@
+/*
+ * Copyright (C) 2006 Sven Peter <[email protected]>
+ * Copyright (C) 2006 Alex Murray <[email protected]>
+ * Copyright (C) 2012-2021 MATE Developers
+ *
+ * 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 THERMAL_SENSOR_TEMP "SensorTemp"
+#define THERMAL_COOLER_LEVEL "CoolerLevel"
+#define GPU_CORE_TEMP "CoreTemp"
+#define AMBIENT_TEMP "AmbientTemp"
+
+/* global variables */
+Display *nvidia_sensors_dpy; /* the connection to the X server */
+
+/* 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, THERMAL_SENSOR_TEMP) == 0) {
+ res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
+ NV_CTRL_TARGET_TYPE_THERMAL_SENSOR,
+ i,
+ 0,
+ NV_CTRL_THERMAL_SENSOR_READING,
+ &temp);
+
+ } else if (g_ascii_strcasecmp(path, THERMAL_COOLER_LEVEL) == 0) {
+ res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
+ NV_CTRL_TARGET_TYPE_COOLER,
+ i,
+ 0,
+ NV_CTRL_THERMAL_COOLER_LEVEL,
+ &temp);
+
+ } else 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)) {
+ int i, cnt;
+
+ if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
+ NV_CTRL_TARGET_TYPE_THERMAL_SENSOR,
+ &cnt)) {
+
+ for (i = 0; i < cnt; i++) {
+ gchar *id = g_strdup_printf("GPU%d%s", i, THERMAL_SENSOR_TEMP);
+
+ sensors_applet_plugin_add_sensor(&sensors,
+ THERMAL_SENSOR_TEMP,
+ id,
+ _("GPU"),
+ TEMP_SENSOR,
+ TRUE,
+ GPU_ICON,
+ DEFAULT_GRAPH_COLOR);
+
+ g_free(id);
+ }
+ }
+
+ if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
+ NV_CTRL_TARGET_TYPE_COOLER,
+ &cnt)) {
+
+ for (i = 0; i < cnt; i++) {
+ gchar *id = g_strdup_printf("GPU%d%s", i, THERMAL_COOLER_LEVEL);
+
+ sensors_applet_plugin_add_sensor(&sensors,
+ THERMAL_COOLER_LEVEL,
+ id,
+ _("GPU"),
+ FAN_SENSOR,
+ TRUE,
+ FAN_ICON,
+ DEFAULT_GRAPH_COLOR);
+
+ g_free(id);
+ }
+ }
+
+ if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
+ NV_CTRL_TARGET_TYPE_GPU,
+ &cnt)) {
+
+ for (i = 0; i < cnt; 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);
+}
+