added icons and xml to livewp directory in debian rules
[shermanaquarium] / sherman-aquarium / shermans / thermometer.c
1
2 #include "status.h"
3 #include "aquarium.h"
4 #include "draw.h"
5 #include "thermometer.h"
6
7
8
9 static Thermometer_settings thermometer_settings;
10 static SA_Image thermometer_1, thermometer_2;
11 static AquariumData *ad;
12 static int status_list[STATUSES];
13
14 Thermometer_settings *thermometer_get_settings_ptr(void)
15 {
16     return &thermometer_settings;
17 }
18
19 int thermometer_get_real_status(int status)
20 {
21     int i,j=0;
22     for(i=0;i<STATUSES;i++){
23         if(status_list[i]){ 
24             if(j==status)
25                 return i;
26             j++;
27         }
28     }
29     return 0;
30 }
31
32 void thermometer_gen_list(void)
33 {
34     status_list[STATUS_OFF] = TRUE;
35
36 #ifdef STATUS_HAVE_FAN1
37     status_list[STATUS_FAN1] = TRUE;
38 #else
39     status_list[STATUS_FAN1] = FALSE;
40 #endif 
41
42 #ifdef STATUS_HAVE_FAN2
43     status_list[STATUS_FAN2] = TRUE;
44 #else
45     status_list[STATUS_FAN2] = FALSE;
46 #endif 
47
48 #ifdef STATUS_HAVE_TEMP1
49     status_list[STATUS_TEMP1] = TRUE;
50 #else
51     status_list[STATUS_TEMP1] = FALSE;
52 #endif 
53 #ifdef STATUS_HAVE_TEMP2
54     status_list[STATUS_TEMP2] = TRUE;
55 #else
56     status_list[STATUS_TEMP2] = FALSE;
57 #endif 
58 #ifdef STATUS_HAVE_SWAP
59     status_list[STATUS_SWAP] = TRUE;
60 #else
61     status_list[STATUS_SWAP] = FALSE;
62 #endif 
63 #ifdef STATUS_HAVE_DISC
64     status_list[STATUS_DISC] = TRUE;
65 #else
66     status_list[STATUS_DISC] = FALSE;
67 #endif 
68
69 #ifdef STATUS_HAVE_CPU
70     status_list[STATUS_CPU] = TRUE;
71 #else
72     status_list[STATUS_CPU] = FALSE;
73 #endif 
74
75 #ifdef STATUS_HAVE_MEM
76     status_list[STATUS_MEM] = TRUE;
77 #else
78     status_list[STATUS_MEM] = FALSE;
79 #endif 
80
81
82 #ifdef STATUS_HAVE_NET_ETH0
83     status_list[STATUS_NET_ETH0_RECV] = TRUE;
84     status_list[STATUS_NET_ETH0_SEND] = TRUE;
85     status_list[STATUS_NET_ETH0_BOTH] = TRUE;
86 #else
87     status_list[STATUS_NET_ETH0_RECV] = FALSE;
88     status_list[STATUS_NET_ETH0_SEND] = FALSE;
89     status_list[STATUS_NET_ETH0_BOTH] = FALSE;
90 #endif 
91
92
93 #ifdef STATUS_HAVE_NET_ETH1
94     status_list[STATUS_NET_ETH1_RECV] = TRUE;
95     status_list[STATUS_NET_ETH1_SEND] = TRUE;
96     status_list[STATUS_NET_ETH1_BOTH] = TRUE;
97 #else
98     status_list[STATUS_NET_ETH1_RECV] = FALSE;
99     status_list[STATUS_NET_ETH1_SEND] = FALSE;
100     status_list[STATUS_NET_ETH1_BOTH] = FALSE;
101 #endif 
102
103
104 #ifdef STATUS_HAVE_NET_PPP0
105     status_list[STATUS_NET_PPP0_RECV] = TRUE;
106     status_list[STATUS_NET_PPP0_SEND] = TRUE;
107     status_list[STATUS_NET_PPP0_BOTH] = TRUE;
108 #else
109     status_list[STATUS_NET_PPP0_RECV] = FALSE;
110     status_list[STATUS_NET_PPP0_SEND] = FALSE;
111     status_list[STATUS_NET_PPP0_BOTH] = FALSE;
112 #endif 
113
114
115 #ifdef STATUS_HAVE_NET_LO
116     status_list[STATUS_NET_LO_RECV] = TRUE;
117     status_list[STATUS_NET_LO_SEND] = TRUE;
118     status_list[STATUS_NET_LO_BOTH] = TRUE;
119 #else
120     status_list[STATUS_NET_LO_RECV] = FALSE;
121     status_list[STATUS_NET_LO_SEND] = FALSE;
122     status_list[STATUS_NET_LO_BOTH] = FALSE;
123 #endif 
124
125
126 }
127
128 void thermometer_exit(void)
129 {
130     if(thermometer_1.pixbuf!=NULL){
131         g_object_unref(thermometer_1.pixbuf);
132         thermometer_1.pixbuf = NULL;
133     }
134
135     if(thermometer_2.pixbuf!=NULL){
136         g_object_unref(thermometer_2.pixbuf);
137         thermometer_2.pixbuf = NULL;
138     }
139 }
140
141 void thermometer_change_colour(SA_Image *image, unsigned char r, unsigned char g, unsigned char b)
142 {
143     int x,y,ypos, alpha;
144     
145     alpha = gdk_pixbuf_get_has_alpha(image->pixbuf);
146     for(y=0;y<image->height;y++){
147         ypos = y*image->rowstride;
148
149         for(x=0;x<image->width*(3+alpha);x+=(3+alpha)){
150                 if(image->image[ypos+x+0] == 0xff){
151                     image->image[ypos+x+0] = r;
152                     image->image[ypos+x+1] = g;
153                     image->image[ypos+x+2] = b;
154                 }
155
156                 if(image->image[ypos+x+0] == 0xbf){
157
158                     if(r>0x20)
159                         image->image[ypos+x+0] = r-0x20;
160                     else
161                         image->image[ypos+x+0] = 0x00;
162
163                     if(g>0x20)
164                         image->image[ypos+x+1] = g-0x20;
165                     else
166                         image->image[ypos+x+1] = 0x00;
167
168                     if(b>0x20)
169                         image->image[ypos+x+2] = b-0x20;
170                     else
171                         image->image[ypos+x+2] = 0x00;
172                 }
173
174         }
175     }
176        
177
178 }
179
180 void thermometer_init(void)
181 {
182     ad = aquarium_get_settings_ptr();
183
184     thermometer_gen_list();
185
186     if(thermometer_1.image!=NULL || thermometer_2.image!=NULL)
187         thermometer_exit();
188
189
190     if(thermometer_settings.messure1 != thermometer_get_real_status(STATUS_OFF)){
191         load_image("thermometer.png", &thermometer_1,1);
192         thermometer_change_colour(&thermometer_1,
193                                   thermometer_settings.c1.r,thermometer_settings.c1.g,thermometer_settings.c1.b);
194
195     }
196     if(thermometer_settings.messure2 != thermometer_get_real_status(STATUS_OFF)){
197         load_image("thermometer.png", &thermometer_2,1);
198         thermometer_change_colour(&thermometer_2,
199                                   thermometer_settings.c2.r,thermometer_settings.c2.g,thermometer_settings.c2.b);
200
201
202     }
203 }
204
205 int thermometer_status_level(int messure, int roof, char *mount_p)
206 {
207     //(30 * status_level / 100)
208     int status_type, level, real_roof;
209     status_type = thermometer_get_real_status(messure);
210
211     switch(status_type){
212
213     case STATUS_OFF:
214         level = 0;
215         real_roof = 100;
216         break;
217     case STATUS_FAN1:
218         level = status_sensors(SENSORS_FAN1);
219         real_roof = roof;
220         break;
221     case STATUS_FAN2:
222         level = status_sensors(SENSORS_FAN2);
223         real_roof = roof;
224         break;
225     case STATUS_TEMP1:
226         level = status_sensors(SENSORS_TEMP1);
227         real_roof = roof;
228         break;
229     case STATUS_TEMP2:
230         level = status_sensors(SENSORS_TEMP2);
231         real_roof = roof;
232         break;
233     case STATUS_SWAP:
234         level = status_swap();
235         real_roof = 100;
236         break;
237     case STATUS_DISC:
238         level = status_disc(mount_p);
239         real_roof = 100;
240         break;  
241     case STATUS_CPU:
242         level = status_cpu();
243         real_roof = 100;
244         break;
245     case STATUS_MEM:
246         level = status_mem();
247         real_roof = 100;
248         break;
249
250     case STATUS_NET_ETH0_RECV:
251         level = status_net(NET_ETH0, NET_RECV);
252         real_roof = roof;
253         break;
254     case STATUS_NET_ETH0_SEND:
255         level = status_net(NET_ETH0, NET_SENT);
256         real_roof = roof;
257         break;
258     case STATUS_NET_ETH0_BOTH:
259         level = (status_net(NET_ETH0, NET_RECV)  + status_net(NET_ETH0, NET_SENT))/2;
260         real_roof = roof;
261         break;
262     case STATUS_NET_ETH1_RECV:
263         level = status_net(NET_ETH1, NET_RECV);
264         real_roof = roof;
265         break;
266     case STATUS_NET_ETH1_SEND:
267         level = status_net(NET_ETH1, NET_SENT);
268         real_roof = roof;
269         break;
270     case STATUS_NET_ETH1_BOTH:
271         level = (status_net(NET_ETH1, NET_RECV)  + status_net(NET_ETH1, NET_SENT))/2;
272         real_roof = roof;
273         break;
274     case STATUS_NET_PPP0_RECV:
275         level = status_net(NET_PPP0, NET_RECV);
276         real_roof = roof;
277         break;
278     case STATUS_NET_PPP0_SEND:
279         level = status_net(NET_PPP0, NET_SENT);
280         real_roof = roof;
281         break;
282     case STATUS_NET_PPP0_BOTH:
283         level = (status_net(NET_PPP0, NET_RECV)  + status_net(NET_PPP0, NET_SENT))/2;
284         real_roof = roof;
285         break;
286     case STATUS_NET_LO_RECV:
287         level = status_net(NET_LO, NET_RECV);
288         real_roof = roof;
289         break;
290     case STATUS_NET_LO_SEND:
291         level = status_net(NET_LO, NET_SENT);
292         real_roof = roof;
293         break;
294     case STATUS_NET_LO_BOTH:
295         level = (status_net(NET_LO, NET_RECV)  + status_net(NET_LO, NET_SENT))/2;
296         real_roof = roof;
297         break;
298
299
300     default:
301         level = 30;
302         real_roof = 100;
303     }
304
305     if(level > real_roof)
306         return 0;
307
308     if(level <0 || real_roof <=0)
309         return 30;
310     else 
311         return (int)(30.0 - 30.0*(float)level / (float)real_roof);
312
313 }
314
315 void thermometer_core(SA_Image *thermometer, int mode,int draw, int vert, int horz, int split, 
316                       unsigned char r, unsigned char g, unsigned char b, unsigned char alpha,
317                       unsigned char r_s, unsigned char g_s, unsigned char b_s, unsigned char alpha_s,
318                       int messure, int messure_s, int roof, int roof_s, char *mount_point, char *mount_point_s)
319 {
320
321     int x=0, y=0, colour, status_type;
322     static int status_level[2] = {0, 0}, count = 0;
323
324
325     if(mode != draw)
326         return;
327
328     status_type = thermometer_get_real_status(messure);
329     if(status_type == STATUS_OFF)
330         return;
331
332     if(vert == TOP)
333         y = 0;
334     if(vert == CENTER)
335         y = ad->ymax/2 - thermometer->height/2;
336     if(vert == BOTTOM)
337         y = ad->ymax - thermometer->height;
338
339     if(horz == LEFT)
340         x = 0;
341     if(horz == CENTER)
342         x = ad->xmax/2 - thermometer->width/2;
343     if(horz == RIGHT)
344         x = ad->xmax - thermometer->width - 1;
345
346     draw_pic_alpha(thermometer->image, thermometer->width,
347                    thermometer->height, x, y, 0, 0x80);
348
349     colour = (((int)r) <<16) + (((int)g) <<8) + (int)b;
350
351
352     /* Status level is between 0 and 30, 0 is max */
353     if(count == 0)
354         status_level[0] = thermometer_status_level(messure, roof, mount_point);
355
356     anti_line(x+3, y + 2 + status_level[0], x+3,
357               y+thermometer->height-3, 
358               1, colour, 0);
359
360     anti_line(x+2, y+thermometer->height-4, x+2,
361               y + thermometer->height-5, 1, colour, 0);
362
363
364     if(split){
365         colour = (((int)r_s) <<16) + (((int)g_s) <<8) + (int)b_s;
366
367         if(count == 0)
368             status_level[1] = thermometer_status_level(messure_s, roof_s, mount_point_s);
369
370         anti_line(x+4, y+thermometer->height-3, x+4,
371                   y + 2 + status_level[1], 1, colour, 0);
372
373     } else {
374         anti_line(x+4, y+thermometer->height-3, x+4,
375                   y + 2 + status_level[0], 1, colour, 0);
376     }
377
378     anti_line(x+5, y+thermometer->height-4, x+5,
379               y + thermometer->height-5, 1, colour, 0);
380
381     /* Update about once a second */
382     if(count == 0)
383         count = 20;
384     else
385         count --;
386
387 }
388
389 void thermometer_update(int mode)
390 {
391
392     thermometer_core(&thermometer_1, mode, thermometer_settings.draw1,
393                      thermometer_settings.vert1,thermometer_settings.horz1,
394                      thermometer_settings.split1,
395                      thermometer_settings.c1.r, thermometer_settings.c1.g,thermometer_settings.c1.b,
396                      thermometer_settings.c1.alpha,
397                      thermometer_settings.c1_s.r,thermometer_settings.c1_s.g,thermometer_settings.c1_s.b,
398                      thermometer_settings.c1_s.alpha,
399                      thermometer_settings.messure1, thermometer_settings.messure1_s, 
400                      thermometer_settings.roof1, thermometer_settings.roof1_s,
401                      thermometer_settings.mount_point1,
402                      thermometer_settings.mount_point1_s);
403
404
405     thermometer_core(&thermometer_2, mode, thermometer_settings.draw2,
406                      thermometer_settings.vert2,thermometer_settings.horz2,
407                      thermometer_settings.split2,
408                      thermometer_settings.c2.r, thermometer_settings.c2.g,thermometer_settings.c2.b,
409                      thermometer_settings.c2.alpha,
410                      thermometer_settings.c2_s.r,thermometer_settings.c2_s.g,thermometer_settings.c2_s.b,
411                      thermometer_settings.c2_s.alpha,
412                      thermometer_settings.messure2, thermometer_settings.messure2_s, 
413                      thermometer_settings.roof2, thermometer_settings.roof2_s,
414                      thermometer_settings.mount_point2,
415                      thermometer_settings.mount_point2_s);
416
417 }
418
419
420
421