Skip to content
Permalink
Browse files

Add NetBSD support.

  • Loading branch information...
jaredmcneill authored and raveit65 committed May 21, 2018
1 parent ff7769b commit 972866ede42e9e3c19241b88703cee6510bb2657
Showing with 255 additions and 0 deletions.
  1. +14 −0 configure.ac
  2. +5 −0 plugins/Makefile.am
  3. +23 −0 plugins/netbsd/Makefile.am
  4. +189 −0 plugins/netbsd/netbsd-plugin.c
  5. +24 −0 plugins/netbsd/netbsd-plugin.h
@@ -242,6 +242,19 @@ else
AC_DEFINE_UNQUOTED(ATICONFIG_EXE, ["$aticonfig_exe"], [aticonfig executable])
fi

# enable netbsd plugin
AC_ARG_ENABLE(netbsd,
AS_HELP_STRING([--enable-netbsd],
[Enable NetBSD plugin.]),
enable_netbsd=$enableval, enable_netbsd="no")

if test "x$enable_netbsd" = "xyes" ; then
echo "Enabling NetBSD support"
fi

# use same test as above, because AM_CONDITIONAL may not be in if/else
AM_CONDITIONAL(NETBSD, test "x$enable_netbsd" = "xyes")

AC_SUBST(CFLAGS)
# for help docs stuff
AC_PATH_PROG(XSLTPROC, xsltproc, no)
@@ -279,6 +292,7 @@ AC_CONFIG_FILES([
plugins/ibm-acpi/Makefile
plugins/libsensors/Makefile
plugins/mbmon/Makefile
plugins/netbsd/Makefile
plugins/nvidia/Makefile
plugins/aticonfig/Makefile
plugins/omnibook/Makefile
@@ -9,6 +9,7 @@ DIST_SUBDIRS = \
ibm-acpi \
libsensors \
mbmon \
netbsd \
nvidia \
omnibook \
pmu-sys \
@@ -41,6 +42,10 @@ else
SUBDIRS += i2c-proc i2c-sys
endif

if NETBSD
SUBDIRS += netbsd
endif

if NVIDIA
SUBDIRS += nvidia
endif
@@ -0,0 +1,23 @@
# NetBSD plugin
plugindir = $(libdir)/mate-sensors-applet/plugins

AM_CPPFLAGS = -DMATELOCALEDIR=\""$(datadir)/locale/"\" \
-DG_LOG_DOMAIN=\""Sensors Applet"\" \
-DPIXMAPS_DIR=\""$(datadir)/pixmaps/$(PACKAGE)/"\" \
-DDATADIR=\""$(datadir)"\" \
-DLIBDIR=\""$(libdir)"\" \
-DSYSCONFDIR=\""$(sysconfdir)"\" \
-DPREFIX=\""$(prefix)"\" \
-I$(top_srcdir) \
$(GLIB_CFLAGS)


plugin_LTLIBRARIES = libnetbsd.la

libnetbsd_la_SOURCES = \
netbsd-plugin.h \
netbsd-plugin.c

libnetbsd_la_LDFLAGS = $(PLUGIN_LIBTOOL_FLAGS) $(GLIB_LIBS) -lprop

libnetbsd_la_LIBADD = $(top_builddir)/lib/libmate-sensors-applet-plugin.la
@@ -0,0 +1,189 @@
/*
* Copyright (C) 2018 Jared McNeill <jmcneill@invisible.ca>
*
* 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 <fcntl.h>
#include <unistd.h>
#include <prop/proplib.h>
#include <sys/envsys.h>

#include "netbsd-plugin.h"

static int sysmon_fd = -1;

static gdouble netbsd_plugin_get_sensor_value(const gchar *path,
const gchar *id,
SensorType type,
GError **error) {

prop_dictionary_t dict;
prop_array_t array;
prop_object_iterator_t iter, siter;
prop_object_t obj, sobj;
const gchar *desc = NULL, *state = NULL;
gdouble sensor_val = -1.0f;
int64_t val;

if (prop_dictionary_recv_ioctl(sysmon_fd, ENVSYS_GETDICTIONARY, &dict) == -1)
return sensor_val;

iter = prop_dictionary_iterator(dict);
while ((obj = prop_object_iterator_next(iter)) != NULL) {
if (strcmp(path, prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
continue;

array = prop_dictionary_get_keysym(dict, obj);
siter = prop_array_iterator(array);
while ((sobj = prop_object_iterator_next(siter)) != NULL) {
if (prop_dictionary_get_cstring_nocopy(sobj, "description", &desc) == false)
continue;
if (strcmp(id, desc) != 0)
continue;

prop_dictionary_get_cstring_nocopy(sobj, "state", &state);
prop_dictionary_get_int64(sobj, "cur-value", &val);

if (strcmp(state, "valid") == 0) {
switch (type) {
case TEMP_SENSOR:
/* K to C */
sensor_val = ((val - 273150000) / 1000000.0);
break;
case FAN_SENSOR:
/* RPM */
sensor_val = (gdouble)val;
break;
case VOLTAGE_SENSOR:
/* uV to V */
sensor_val = val / 1000000.0;
break;
default:
break;
}
}
break;
}
prop_object_iterator_release(siter);
break;
}
prop_object_iterator_release(iter);

prop_object_release(dict);

return sensor_val;
}

static IconType netbsd_plugin_get_sensor_icon(const gchar *desc) {
IconType icontype = GENERIC_ICON;

if (strcasestr(desc, "cpu") != NULL)
icontype = CPU_ICON;
else if (strcasestr(desc, "gpu") != NULL)
icontype = GPU_ICON;
else if (strcasestr(desc, "batt") != NULL)
icontype = BATTERY_ICON;
else if (strcasestr(desc, "mem") != NULL)
icontype = MEMORY_ICON;
else if (strcasestr(desc, "disk") != NULL || strcasestr(desc, "hdd") != NULL)
icontype = HDD_ICON;

return icontype;
}

static void netbsd_plugin_get_sensors(GList **sensors) {
prop_dictionary_t dict;
prop_array_t array;
prop_object_iterator_t iter, siter;
prop_object_t obj, sobj;

sysmon_fd = open("/dev/sysmon", O_RDONLY);
if (sysmon_fd == -1)
return;

if (prop_dictionary_recv_ioctl(sysmon_fd, ENVSYS_GETDICTIONARY, &dict) == -1) {
close(sysmon_fd);
return;
}

iter = prop_dictionary_iterator(dict);
while ((obj = prop_object_iterator_next(iter)) != NULL) {
const gchar *path = prop_dictionary_keysym_cstring_nocopy(obj);
array = prop_dictionary_get_keysym(dict, obj);
siter = prop_array_iterator(array);
while ((sobj = prop_object_iterator_next(siter)) != NULL) {
const gchar *type = NULL, *desc = NULL;
if (prop_dictionary_get_cstring_nocopy(sobj, "type", &type) == false)
continue;
if (prop_dictionary_get_cstring_nocopy(sobj, "description", &desc) == false)
continue;

SensorType sensortype;
if (strcmp(type, "Fan") == 0)
sensortype = FAN_SENSOR;
else if (strcmp(type, "Temperature") == 0)
sensortype = TEMP_SENSOR;
else if (strcmp(type, "Voltage AC") == 0 || strcmp(type, "Voltage DC") == 0)
sensortype = VOLTAGE_SENSOR;
else
continue;

IconType icontype = netbsd_plugin_get_sensor_icon(desc);

sensors_applet_plugin_add_sensor(sensors,
g_strdup(path),
g_strdup(desc),
g_strdup(desc),
sensortype,
TRUE,
icontype,
DEFAULT_GRAPH_COLOR);
}
prop_object_iterator_release(siter);
}
prop_object_iterator_release(iter);

prop_object_release(dict);
}

static GList *netbsd_plugin_init(void) {
GList *sensors = NULL;

netbsd_plugin_get_sensors(&sensors);

return sensors;
}

/* API functions */
const gchar *sensors_applet_plugin_name(void) {
return "netbsd";
}

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

gdouble sensors_applet_plugin_get_sensor_value(const gchar *path,
const gchar *id,
SensorType type,
GError **error) {

return netbsd_plugin_get_sensor_value(path, id, type, error);
}
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2018 Jared McNeill <jmcneill@invisible.ca>
*
* 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
*/

#ifndef NETBSD_PLUGIN_H
#define NETBSD_PLUGIN_H

#include <sensors-applet/sensors-applet-plugin.h>

#endif /* NETBSD_PLUGIN_H */

0 comments on commit 972866e

Please sign in to comment.
You can’t perform that action at this time.