2 Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
22 #define _(str) gettext(str)
25 Following code is based on GNOME sensors applet code hddtemp-plugin.c
26 see http://sensors-applet.sourceforge.net/
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #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 fprintf(stderr, _("ERROR: hdd fetch, failed to open socket\n"));
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) {
73 _("ERROR: hdd fetch, failed to open connection\n"));
75 buffer = malloc(HDDTEMP_OUTPUT_BUFFER_LENGTH);
78 while ((n = read(sockfd,
80 HDDTEMP_OUTPUT_BUFFER_LENGTH -
81 output_length)) > 0) {
87 buffer[output_length] = '\0';
95 static int str_index(char *str, char d)
100 if (!str || *str == '\0')
116 static struct psensor *
117 create_sensor(char *id, char *name, int values_max_length)
119 return psensor_create(id, name, SENSOR_TYPE_HDD_TEMP,
123 static char *next_hdd_info(char *string, struct hdd_info *info)
126 int idx_name_n, i, temp;
128 if (!string || strlen(string) <= 5 /* at least 5 pipes */
132 /* skip first pipe */
136 idx_name_n = str_index(c, '|');
138 if (idx_name_n == -1)
140 c = c + idx_name_n + 1;
143 i = str_index(c, '|');
149 i = str_index(c, '|');
156 i = str_index(c, '|');
161 info->name = malloc(idx_name_n + 1);
162 strncpy(info->name, string + 1, idx_name_n);
163 info->name[idx_name_n] = '\0';
170 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
171 int values_max_length)
173 char *hddtemp_output = fetch();
175 struct hdd_info info;
176 struct psensor **result;
181 if (hddtemp_output[0] != '|') {
183 _("ERROR: wrong hdd string: %s"), hddtemp_output);
185 free(hddtemp_output);
194 while (c && (c = next_hdd_info(c, &info))) {
195 struct psensor *sensor;
196 struct psensor **tmp_sensors;
198 char *id = malloc(strlen("hdd ") + strlen(info.name) + 1);
200 strcat(id, info.name);
202 sensor = create_sensor(id, info.name, values_max_length);
204 tmp_sensors = psensor_list_add(result, sensor);
206 if (result != sensors)
209 result = tmp_sensors;
212 free(hddtemp_output);
217 static void update(struct psensor **sensors, struct hdd_info *info)
219 struct psensor **sensor_cur = sensors;
221 while (*sensor_cur) {
222 if ((*sensor_cur)->type == SENSOR_TYPE_HDD_TEMP
223 && !strcmp((*sensor_cur)->id + 4, info->name))
224 psensor_set_current_value(*sensor_cur,
231 void hdd_psensor_list_update(struct psensor **sensors)
233 char *hddtemp_output = fetch();
238 if (hddtemp_output[0] == '|') {
240 char *c = hddtemp_output;
241 struct hdd_info info;
245 while (c && (c = next_hdd_info(c, &info))) {
247 update(sensors, &info);
253 _("ERROR: wrong hdd string: %s\n"), hddtemp_output);
256 free(hddtemp_output);