summaryrefslogtreecommitdiff
path: root/plugins/netbsd/netbsd-plugin.c
blob: 50f5da3e86366d8dbda98cea5c29d2b03acc6fd8 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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);
}