|
| 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 | +} |
0 commit comments