2 * Copyright (C) 2010-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
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 Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
24 * Following code is based on GNOME sensors applet code
25 * hddtemp-plugin.c see http://sensors-applet.sourceforge.net/
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
39 static const char *PROVIDER_NAME = "hddtemp";
41 #define HDDTEMP_SERVER_IP_ADDRESS "127.0.0.1"
42 #define HDDTEMP_PORT_NUMBER 7634
43 #define HDDTEMP_OUTPUT_BUFFER_LENGTH 4048
54 int output_length = 0;
57 struct sockaddr_in address;
59 sockfd = socket(AF_INET, SOCK_STREAM, 0);
61 log_err(_("hddtemp: failed to open socket."));
65 address.sin_family = AF_INET;
66 address.sin_addr.s_addr = inet_addr(HDDTEMP_SERVER_IP_ADDRESS);
67 address.sin_port = htons(HDDTEMP_PORT_NUMBER);
72 (struct sockaddr *)&address,
73 (socklen_t) sizeof(address)) == -1) {
74 log_err(_("hddtemp: failed to open connection."));
76 buffer = malloc(HDDTEMP_OUTPUT_BUFFER_LENGTH);
79 while ((n = read(sockfd,
81 HDDTEMP_OUTPUT_BUFFER_LENGTH -
82 output_length)) > 0) {
88 buffer[output_length] = '\0';
96 static int str_index(char *str, char d)
101 if (!str || *str == '\0')
117 static struct psensor *
118 create_sensor(char *id, char *name, int values_max_length)
122 t = SENSOR_TYPE_HDD | SENSOR_TYPE_HDDTEMP | SENSOR_TYPE_TEMP;
124 return psensor_create(id, name, strdup(_("Disk")),
129 static char *next_hdd_info(char *string, struct hdd_info *info)
132 int idx_name_n, i, temp;
134 if (!string || strlen(string) <= 5 /* at least 5 pipes */
138 /* skip first pipe */
142 idx_name_n = str_index(c, '|');
144 if (idx_name_n == -1)
146 c = c + idx_name_n + 1;
149 i = str_index(c, '|');
155 i = str_index(c, '|');
162 i = str_index(c, '|');
167 info->name = malloc(idx_name_n + 1);
168 strncpy(info->name, string + 1, idx_name_n);
169 info->name[idx_name_n] = '\0';
176 struct psensor **hddtemp_psensor_list_add(struct psensor **sensors,
177 int values_max_length)
179 char *hddtemp_output, *c;
180 struct hdd_info info;
181 struct psensor **result;
183 hddtemp_output = fetch();
188 if (hddtemp_output[0] != '|') {
189 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
191 free(hddtemp_output);
200 while (c && (c = next_hdd_info(c, &info))) {
201 struct psensor *sensor;
202 struct psensor **tmp_sensors;
205 id = malloc(strlen(PROVIDER_NAME) + 1 + strlen(info.name) + 1);
206 sprintf(id, "%s %s", PROVIDER_NAME, info.name);
208 sensor = create_sensor(id, info.name, values_max_length);
210 tmp_sensors = psensor_list_add(result, sensor);
212 if (result != sensors)
215 result = tmp_sensors;
218 free(hddtemp_output);
223 static void update(struct psensor **sensors, struct hdd_info *info)
225 struct psensor **sensor_cur = sensors;
227 while (*sensor_cur) {
228 if (!((*sensor_cur)->type & SENSOR_TYPE_REMOTE)
229 && (*sensor_cur)->type & SENSOR_TYPE_HDDTEMP
230 && !strcmp((*sensor_cur)->id + 8, info->name))
231 psensor_set_current_value(*sensor_cur,
238 void hddtemp_psensor_list_update(struct psensor **sensors)
240 char *hddtemp_output;
242 hddtemp_output = fetch();
247 if (hddtemp_output[0] == '|') {
248 char *c = hddtemp_output;
249 struct hdd_info info;
254 while (c && (c = next_hdd_info(c, &info))) {
256 update(sensors, &info);
261 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
264 free(hddtemp_output);