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 #define HDDTEMP_SERVER_IP_ADDRESS "127.0.0.1"
40 #define HDDTEMP_PORT_NUMBER 7634
41 #define HDDTEMP_OUTPUT_BUFFER_LENGTH 4048
52 int output_length = 0;
55 struct sockaddr_in address;
57 sockfd = socket(AF_INET, SOCK_STREAM, 0);
59 log_err(_("hddtemp: failed to open socket."));
63 address.sin_family = AF_INET;
64 address.sin_addr.s_addr = inet_addr(HDDTEMP_SERVER_IP_ADDRESS);
65 address.sin_port = htons(HDDTEMP_PORT_NUMBER);
70 (struct sockaddr *)&address,
71 (socklen_t) sizeof(address)) == -1) {
72 log_err(_("hddtemp: failed to open connection."));
74 buffer = malloc(HDDTEMP_OUTPUT_BUFFER_LENGTH);
77 while ((n = read(sockfd,
79 HDDTEMP_OUTPUT_BUFFER_LENGTH -
80 output_length)) > 0) {
86 buffer[output_length] = '\0';
94 static int str_index(char *str, char d)
99 if (!str || *str == '\0')
115 static struct psensor *
116 create_sensor(char *id, char *name, int values_max_length)
120 t = SENSOR_TYPE_HDD | SENSOR_TYPE_HDDTEMP | SENSOR_TYPE_TEMP;
122 return psensor_create(id, name, strdup(_("Disk")),
127 static char *next_hdd_info(char *string, struct hdd_info *info)
130 int idx_name_n, i, temp;
132 if (!string || strlen(string) <= 5 /* at least 5 pipes */
136 /* skip first pipe */
140 idx_name_n = str_index(c, '|');
142 if (idx_name_n == -1)
144 c = c + idx_name_n + 1;
147 i = str_index(c, '|');
153 i = str_index(c, '|');
160 i = str_index(c, '|');
165 info->name = malloc(idx_name_n + 1);
166 strncpy(info->name, string + 1, idx_name_n);
167 info->name[idx_name_n] = '\0';
174 struct psensor **hddtemp_psensor_list_add(struct psensor **sensors,
175 int values_max_length)
177 char *hddtemp_output = fetch();
179 struct hdd_info info;
180 struct psensor **result;
185 if (hddtemp_output[0] != '|') {
186 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
188 free(hddtemp_output);
197 while (c && (c = next_hdd_info(c, &info))) {
198 struct psensor *sensor;
199 struct psensor **tmp_sensors;
201 char *id = malloc(strlen("hddtemp ") + strlen(info.name) + 1);
202 strcpy(id, "hddtemp ");
203 strcat(id, info.name);
205 sensor = create_sensor(id, info.name, values_max_length);
207 tmp_sensors = psensor_list_add(result, sensor);
209 if (result != sensors)
212 result = tmp_sensors;
215 free(hddtemp_output);
220 static void update(struct psensor **sensors, struct hdd_info *info)
222 struct psensor **sensor_cur = sensors;
224 while (*sensor_cur) {
225 if (!((*sensor_cur)->type & SENSOR_TYPE_REMOTE)
226 && (*sensor_cur)->type & SENSOR_TYPE_HDDTEMP
227 && !strcmp((*sensor_cur)->id + 8, info->name))
228 psensor_set_current_value(*sensor_cur,
235 void hddtemp_psensor_list_update(struct psensor **sensors)
237 char *hddtemp_output = fetch();
242 if (hddtemp_output[0] == '|') {
244 char *c = hddtemp_output;
245 struct hdd_info info;
249 while (c && (c = next_hdd_info(c, &info))) {
251 update(sensors, &info);
256 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
259 free(hddtemp_output);