Skip to content

Commit 972866e

Browse files
jaredmcneillraveit65
authored andcommitted
Add NetBSD support.
1 parent ff7769b commit 972866e

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed

‎configure.ac‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,19 @@ else
242242
AC_DEFINE_UNQUOTED(ATICONFIG_EXE, ["$aticonfig_exe"], [aticonfig executable])
243243
fi
244244

245+
# enable netbsd plugin
246+
AC_ARG_ENABLE(netbsd,
247+
AS_HELP_STRING([--enable-netbsd],
248+
[Enable NetBSD plugin.]),
249+
enable_netbsd=$enableval, enable_netbsd="no")
250+
251+
if test "x$enable_netbsd" = "xyes" ; then
252+
echo "Enabling NetBSD support"
253+
fi
254+
255+
# use same test as above, because AM_CONDITIONAL may not be in if/else
256+
AM_CONDITIONAL(NETBSD, test "x$enable_netbsd" = "xyes")
257+
245258
AC_SUBST(CFLAGS)
246259
# for help docs stuff
247260
AC_PATH_PROG(XSLTPROC, xsltproc, no)
@@ -279,6 +292,7 @@ AC_CONFIG_FILES([
279292
plugins/ibm-acpi/Makefile
280293
plugins/libsensors/Makefile
281294
plugins/mbmon/Makefile
295+
plugins/netbsd/Makefile
282296
plugins/nvidia/Makefile
283297
plugins/aticonfig/Makefile
284298
plugins/omnibook/Makefile

‎plugins/Makefile.am‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ DIST_SUBDIRS = \
99
ibm-acpi \
1010
libsensors \
1111
mbmon \
12+
netbsd \
1213
nvidia \
1314
omnibook \
1415
pmu-sys \
@@ -41,6 +42,10 @@ else
4142
SUBDIRS += i2c-proc i2c-sys
4243
endif
4344

45+
if NETBSD
46+
SUBDIRS += netbsd
47+
endif
48+
4449
if NVIDIA
4550
SUBDIRS += nvidia
4651
endif

‎plugins/netbsd/Makefile.am‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# NetBSD plugin
2+
plugindir = $(libdir)/mate-sensors-applet/plugins
3+
4+
AM_CPPFLAGS = -DMATELOCALEDIR=\""$(datadir)/locale/"\" \
5+
-DG_LOG_DOMAIN=\""Sensors Applet"\" \
6+
-DPIXMAPS_DIR=\""$(datadir)/pixmaps/$(PACKAGE)/"\" \
7+
-DDATADIR=\""$(datadir)"\" \
8+
-DLIBDIR=\""$(libdir)"\" \
9+
-DSYSCONFDIR=\""$(sysconfdir)"\" \
10+
-DPREFIX=\""$(prefix)"\" \
11+
-I$(top_srcdir) \
12+
$(GLIB_CFLAGS)
13+
14+
15+
plugin_LTLIBRARIES = libnetbsd.la
16+
17+
libnetbsd_la_SOURCES = \
18+
netbsd-plugin.h \
19+
netbsd-plugin.c
20+
21+
libnetbsd_la_LDFLAGS = $(PLUGIN_LIBTOOL_FLAGS) $(GLIB_LIBS) -lprop
22+
23+
libnetbsd_la_LIBADD = $(top_builddir)/lib/libmate-sensors-applet-plugin.la

‎plugins/netbsd/netbsd-plugin.c‎

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (C) 2018 Jared McNeill <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU 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 St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifdef HAVE_CONFIG_H
20+
#include "config.h"
21+
#endif /* HAVE_CONFIG_H */
22+
23+
#include <fcntl.h>
24+
#include <unistd.h>
25+
#include <prop/proplib.h>
26+
#include <sys/envsys.h>
27+
28+
#include "netbsd-plugin.h"
29+
30+
static int sysmon_fd = -1;
31+
32+
static gdouble netbsd_plugin_get_sensor_value(const gchar *path,
33+
const gchar *id,
34+
SensorType type,
35+
GError **error) {
36+
37+
prop_dictionary_t dict;
38+
prop_array_t array;
39+
prop_object_iterator_t iter, siter;
40+
prop_object_t obj, sobj;
41+
const gchar *desc = NULL, *state = NULL;
42+
gdouble sensor_val = -1.0f;
43+
int64_t val;
44+
45+
if (prop_dictionary_recv_ioctl(sysmon_fd, ENVSYS_GETDICTIONARY, &dict) == -1)
46+
return sensor_val;
47+
48+
iter = prop_dictionary_iterator(dict);
49+
while ((obj = prop_object_iterator_next(iter)) != NULL) {
50+
if (strcmp(path, prop_dictionary_keysym_cstring_nocopy(obj)) != 0)
51+
continue;
52+
53+
array = prop_dictionary_get_keysym(dict, obj);
54+
siter = prop_array_iterator(array);
55+
while ((sobj = prop_object_iterator_next(siter)) != NULL) {
56+
if (prop_dictionary_get_cstring_nocopy(sobj, "description", &desc) == false)
57+
continue;
58+
if (strcmp(id, desc) != 0)
59+
continue;
60+
61+
prop_dictionary_get_cstring_nocopy(sobj, "state", &state);
62+
prop_dictionary_get_int64(sobj, "cur-value", &val);
63+
64+
if (strcmp(state, "valid") == 0) {
65+
switch (type) {
66+
case TEMP_SENSOR:
67+
/* K to C */
68+
sensor_val = ((val - 273150000) / 1000000.0);
69+
break;
70+
case FAN_SENSOR:
71+
/* RPM */
72+
sensor_val = (gdouble)val;
73+
break;
74+
case VOLTAGE_SENSOR:
75+
/* uV to V */
76+
sensor_val = val / 1000000.0;
77+
break;
78+
default:
79+
break;
80+
}
81+
}
82+
break;
83+
}
84+
prop_object_iterator_release(siter);
85+
break;
86+
}
87+
prop_object_iterator_release(iter);
88+
89+
prop_object_release(dict);
90+
91+
return sensor_val;
92+
}
93+
94+
static IconType netbsd_plugin_get_sensor_icon(const gchar *desc) {
95+
IconType icontype = GENERIC_ICON;
96+
97+
if (strcasestr(desc, "cpu") != NULL)
98+
icontype = CPU_ICON;
99+
else if (strcasestr(desc, "gpu") != NULL)
100+
icontype = GPU_ICON;
101+
else if (strcasestr(desc, "batt") != NULL)
102+
icontype = BATTERY_ICON;
103+
else if (strcasestr(desc, "mem") != NULL)
104+
icontype = MEMORY_ICON;
105+
else if (strcasestr(desc, "disk") != NULL || strcasestr(desc, "hdd") != NULL)
106+
icontype = HDD_ICON;
107+
108+
return icontype;
109+
}
110+
111+
static void netbsd_plugin_get_sensors(GList **sensors) {
112+
prop_dictionary_t dict;
113+
prop_array_t array;
114+
prop_object_iterator_t iter, siter;
115+
prop_object_t obj, sobj;
116+
117+
sysmon_fd = open("/dev/sysmon", O_RDONLY);
118+
if (sysmon_fd == -1)
119+
return;
120+
121+
if (prop_dictionary_recv_ioctl(sysmon_fd, ENVSYS_GETDICTIONARY, &dict) == -1) {
122+
close(sysmon_fd);
123+
return;
124+
}
125+
126+
iter = prop_dictionary_iterator(dict);
127+
while ((obj = prop_object_iterator_next(iter)) != NULL) {
128+
const gchar *path = prop_dictionary_keysym_cstring_nocopy(obj);
129+
array = prop_dictionary_get_keysym(dict, obj);
130+
siter = prop_array_iterator(array);
131+
while ((sobj = prop_object_iterator_next(siter)) != NULL) {
132+
const gchar *type = NULL, *desc = NULL;
133+
if (prop_dictionary_get_cstring_nocopy(sobj, "type", &type) == false)
134+
continue;
135+
if (prop_dictionary_get_cstring_nocopy(sobj, "description", &desc) == false)
136+
continue;
137+
138+
SensorType sensortype;
139+
if (strcmp(type, "Fan") == 0)
140+
sensortype = FAN_SENSOR;
141+
else if (strcmp(type, "Temperature") == 0)
142+
sensortype = TEMP_SENSOR;
143+
else if (strcmp(type, "Voltage AC") == 0 || strcmp(type, "Voltage DC") == 0)
144+
sensortype = VOLTAGE_SENSOR;
145+
else
146+
continue;
147+
148+
IconType icontype = netbsd_plugin_get_sensor_icon(desc);
149+
150+
sensors_applet_plugin_add_sensor(sensors,
151+
g_strdup(path),
152+
g_strdup(desc),
153+
g_strdup(desc),
154+
sensortype,
155+
TRUE,
156+
icontype,
157+
DEFAULT_GRAPH_COLOR);
158+
}
159+
prop_object_iterator_release(siter);
160+
}
161+
prop_object_iterator_release(iter);
162+
163+
prop_object_release(dict);
164+
}
165+
166+
static GList *netbsd_plugin_init(void) {
167+
GList *sensors = NULL;
168+
169+
netbsd_plugin_get_sensors(&sensors);
170+
171+
return sensors;
172+
}
173+
174+
/* API functions */
175+
const gchar *sensors_applet_plugin_name(void) {
176+
return "netbsd";
177+
}
178+
179+
GList *sensors_applet_plugin_init(void) {
180+
return netbsd_plugin_init();
181+
}
182+
183+
gdouble sensors_applet_plugin_get_sensor_value(const gchar *path,
184+
const gchar *id,
185+
SensorType type,
186+
GError **error) {
187+
188+
return netbsd_plugin_get_sensor_value(path, id, type, error);
189+
}

‎plugins/netbsd/netbsd-plugin.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2018 Jared McNeill <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU 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 St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef NETBSD_PLUGIN_H
20+
#define NETBSD_PLUGIN_H
21+
22+
#include <sensors-applet/sensors-applet-plugin.h>
23+
24+
#endif /* NETBSD_PLUGIN_H */

0 commit comments

Comments
 (0)