wmhdplop: Add version 0.9.9 to repository.
[dockapps.git] / wmhdplop / wmhdplop.c
blobf55224fa778de28f3a260dfd70a2c3ce2d00a789
1 /*
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
15 USA.
18 #include "config.h"
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <dirent.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <unistd.h>
26 #include <time.h>
27 #include <math.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <getopt.h>
31 #ifdef ENABLE_HDDTEMP_QUERY
32 #include <sys/socket.h>
33 #include <sys/wait.h>
34 #include <netinet/in.h>
35 #include <netdb.h>
36 #endif
37 #define GLOBALS_HERE
38 #include "global.h"
39 #include "wmhdplop.h"
41 uid_t euid, uid;
45 void update_swap_matrix(App *app) {
46 SwapMatrix *sm = &app->sm;
47 unsigned i;
48 int col,row;
49 unsigned sw_in = ceil(get_swapin_throughput()*4), sw_out = ceil(get_swapout_throughput()*4);
50 for (i=0; i < sw_in+sw_out; i++) {
51 col = rand() % sm->ncol;
52 row = rand() % sm->nrow;
53 if (sm->intensity[row][col] == 0) {
54 sm->pre_cnt[row][col] = rand() % 10; //(app->update_stats_mult);
56 sm->intensity[row][col] = app->swap_matrix_lighting * ((i < sw_in) ? -1 : +1);
60 void update_io_matrix_rw(App *app, float mbs, int op) {
61 IOMatrix *io = &app->iom;
62 IO_op_lst *l;
63 if (mbs > 10000) mbs = 10000; /* very quick and ugly fix for the strange bug which only occurs in gkhdplop */
64 int bsz = MAX(SQR(16*64./(app->dock->w + app->dock->h)),2); /* 1024 for a 64x64 applet */
65 while (mbs>1e-5) {
66 float v = MIN(mbs, bsz); /* a throughput > 1MB/s will create more than one spot */
67 mbs -= v;
68 l = calloc(1,sizeof(*l)); assert(l);
69 l->next = io->ops;
70 l->n = (int)(log2f(1+v*1024)/10.);
71 l->op = op;
72 l->i = rand() % io->h;
73 l->j = rand() % io->w;
74 io->ops = l;
78 void update_io_matrix(App *app) {
79 update_io_matrix_rw(app, get_read_throughput(), OP_READ);
80 update_io_matrix_rw(app, get_write_throughput(), OP_WRITE);
83 #define IOSCAL 32768
85 void evolve_io_matrix(App *app, DATA32 * __restrict buff) {
86 IOMatrix *io = &app->iom;
87 int i,j;
88 int * __restrict pl, * __restrict tmp;
89 IO_op_lst *o = io->ops, *po = NULL, *no;
90 while (o) {
91 io->v[o->i+1][o->j+1] = ((o->op == OP_READ) ? +50000000 : -50000000);
92 no = o->next;
93 if (--o->n <= 0) {
94 if (po) { po->next = no; }
95 else { io->ops = no; }
96 free(o);
97 } else po = po->next;
98 o = no;
101 /* brain-dead diffusion */
102 pl = io->v[io->h+2];
103 int * __restrict l = io->v[io->h+3];
104 for (j=1; j < io->w+1; ++j) pl[j] = 0;
105 for (i=1; i < io->h+1; ++i) {
107 for (j=1; j < io->w+1; ++j) {
108 l[j] = io->v[i][j]*0.99 + (io->v[i][j-1] + io->v[i][j+1] + pl[j] + io->v[i+1][j] - 4*io->v[i][j])/5.;
112 float *__restrict nl = io->v[i+1]+1;
113 float *__restrict cl = io->v[i];
114 for (j=1; j < io->w+1; ++j, ++cl) {
115 l[j] = (cl[1])*0.99 + (cl[0] + cl[2] + pl[j] + *nl++ - 4*cl[1])/5.;
118 int *__restrict dest;
119 int *__restrict pn = io->v[i+1]+1;
120 int *__restrict pc = io->v[i]+1;
121 int *__restrict pp = pl+1;
122 int pj,cj=0,nj=*pc++;
123 for (j=0, dest=l+1, pj = 0.; j < io->w; ++j) {
124 pj = cj; cj = nj; nj = *pc++;
125 *dest = (cj * 37)/200 + (pj + nj + *pp++ + *pn++)/5;
126 /* *dest = 99*(cj + (pj + nj + *pp++ + *pn++)/(4))/200; */
127 //} for (j=0, dest=l+1; j < io->w; ++j) {
128 int v = (int)(*dest++ >> 10);
129 if (v == 0) { *buff++ = io->cm.p[CMAPSZ/2]; continue; }
130 if (v > CMAPSZ/4) { /* cheaper than a log(vv) ... */
131 v = MIN(CMAPSZ/4 + (v-CMAPSZ/4)/16,CMAPSZ/2-1);
132 } else if (v < -CMAPSZ/4) {
133 v = MAX(-CMAPSZ/4 + (v+CMAPSZ/4)/16,-CMAPSZ/2);
135 *buff++ = io->cm.p[v+CMAPSZ/2];
138 tmp = pl; pl = io->v[i]; io->v[i] = l; l = tmp;
139 io->v[io->h+2] = pl; io->v[io->h+3] = l;
144 #if 0
145 static void draw_io_matrix(App *app, DATA32 * __restrict buff) {
146 IOMatrix *io = &app->iom;
147 int i,j;
148 for (i=0; i < io->h; ++i) {
149 for (j=0; j < io->w; ++j) {
150 float vv = io->v[i+1][j+1];
151 int v = (int)(vv * 32768); //((int)ldexpf(vv,15)); /* (int)v*(2^15) */
152 //if (v == 0) continue;
153 //if (v < 0) v*=3; /* write op are rare, so they are brighter .. */
154 if (v > CMAPSZ/4) { /* cheaper than a log(vv) ... */
155 v = MIN(CMAPSZ/4 + (v-CMAPSZ/4)/16,CMAPSZ/2-1);
156 } else if (v < -CMAPSZ/4) {
157 v = MAX(-CMAPSZ/4 + (v+CMAPSZ/4)/16,-CMAPSZ/2);
159 //assert(v+CMAPSZ/2>=0);
160 //assert(v+CMAPSZ/2 < sizeof io->cm.p);
161 *buff++ /*[j+i*io->w]*/ = io->cm.p[v+CMAPSZ/2];
165 #endif
167 void draw_swap_matrix(App *app) {
168 SwapMatrix *sm = &app->sm;
169 int rcnt[sm->nrow+1], ccnt[sm->ncol+1];
170 int row, col;
171 int isswapping = 0;
172 static int darkcnt = 0;
173 memset(rcnt, 0, sizeof rcnt); memset(ccnt, 0, sizeof ccnt);
174 for (row = 0; row < sm->nrow; row++) {
175 for (col = 0; col < sm->ncol; col++) {
176 if (sm->pre_cnt[row][col]>0) { isswapping = 1; sm->pre_cnt[row][col]--; }
179 if (isswapping) darkcnt = MIN(darkcnt+1,7);
180 else darkcnt = MAX(darkcnt-1, 0);
182 /* darken everything */
183 if (darkcnt) {
184 imlib_context_set_color(0, 0, 0, (darkcnt+1)*16);
185 imlib_image_fill_rectangle(0, 0, app->dock->w, app->dock->h);
187 /* draw squares */
188 for (row = 0; row < sm->nrow; row++) {
189 for (col = 0; col < sm->ncol; col++) {
190 if (sm->intensity[row][col] && sm->pre_cnt[row][col]==0) {
191 int v = sm->intensity[row][col];
192 v = (v * app->swap_matrix_luminosity)/(int)app->swap_matrix_lighting;
193 rcnt[row ] = MAX(rcnt[row ],abs(v)); ccnt[col ] = MAX(ccnt[col ],abs(v));
194 rcnt[row+1] = MAX(rcnt[row+1],abs(v)); ccnt[col+1] = MAX(ccnt[col+1],abs(v));
195 //v = (v*155)/255;
196 /*if (v > 0) imlib_context_set_color(100+v, 155-v, 155-v, 250);
197 else imlib_context_set_color(0, 100-v, 0, 250);*/
198 if (v > 0) imlib_context_set_color(255, 50, 50, MIN(v+80,255));
199 else imlib_context_set_color(50, 255, 50, MIN(v+80,255));
200 imlib_image_fill_rectangle(row*sm->w+1,col*sm->w+1,sm->w-1,sm->w-1);
202 if (sm->intensity[row][col] > 0) {
203 sm->intensity[row][col]--;
204 } else if (sm->intensity[row][col] < 0) {
205 sm->intensity[row][col]++;
211 /* draw lines */
212 for (row = 0; row < sm->nrow+1; row++) {
213 if (rcnt[row]) {
214 imlib_context_set_color(255, 255, 255, MIN(rcnt[row]*2/3, 255));
215 imlib_image_draw_line(row*sm->w,0,row*sm->w,app->dock->w,0);
218 for (col = 0; col < sm->ncol+1; col++) {
219 if (ccnt[col]) {
220 imlib_context_set_color(255, 255, 255, MIN(ccnt[col]*2/3, 255));
221 imlib_image_draw_line(0,col*sm->w,app->dock->h,col*sm->w,0);
226 float f2celsius(float Tf) {
227 return (5./9.)*(Tf-32.);
230 float celsius2f(float Tc) {
231 return (9./5.)*Tc+32.;
234 static void query_hddtemp(App *app) {
235 int fd;
236 struct hostent *h;
237 struct sockaddr_in addr;
238 char buff[1024];
239 SET_VEC(app->disk_temperature, -1, 0, app->nb_hd);
240 if ((h = gethostbyname("127.0.0.1")) == NULL) {
241 fprintf(stderr, "gethostbyname(localhost) failed : %s\n", strerror(errno)); return;
243 if ((fd = socket(h->h_addrtype, SOCK_STREAM,0)) == -1) {
244 fprintf(stderr, "can't create socket : %s\n", strerror(errno)); return;
246 memset(&addr,0,sizeof(struct sockaddr_in));
247 addr.sin_family = AF_INET;
248 addr.sin_port = htons(Prefs.hddtemp_port);
249 addr.sin_addr.s_addr = ((struct in_addr*)(h->h_addr))->s_addr;
250 if (connect(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
251 close(fd);
252 fprintf(stderr,"can't connect to 127.0.0.1:%d : %s\n", Prefs.hddtemp_port, strerror(errno));
253 #ifdef GKRELLM
254 Prefs.enable_hddtemp = 0;
255 #endif
256 return;
258 int n = 0;
259 do {
260 int nn=read(fd,buff+n,MAX((int)(sizeof buff) - n,0));
261 if (nn <= 0) { if (nn < 0) n=nn; break; }
262 n+=nn;
263 } while (n != sizeof buff);
265 BLAHBLAH(2,printf("n=%d, err=%s\n",n, strerror(errno)));
266 if (n != -1) {
267 char *s;
268 DiskList *dl;
269 int cnt;
270 buff[MIN(n,(int)sizeof buff - 1)] = 0;
271 for (dl = first_hd_in_list(), cnt = 0; dl; dl = next_hd_in_list(dl), cnt++) {
272 if (dl->enable_hddtemp) {
273 int found = 0;
274 if ((s=strstr(buff, dl->dev_path))) { found = 1; s+= strlen(dl->name); }
275 if (found) {
276 s=strchr(s,'|');
277 if (s && ((s=strchr(s+1,'|')))) {
278 int unit = 'C';
279 char *p = strchr(++s,'|'), oldv=0;
280 if (p) {
281 oldv = *p;
282 if (*p && toupper(p[1]) == 'F') unit = 'F';
283 *p = 0;
285 BLAHBLAH(1,printf("temperature of '%s' : %s %c\n", dl->name, s, unit));
286 if (strcmp(s,"SLP") == 0) app->disk_temperature[cnt] = -2;
287 else {
288 int temp = atoi(s);
289 if (unit == 'C' && Prefs.temperatures_unit == 'F') {
290 temp = (int)(floor(celsius2f(temp)+.5));
291 } else if (unit == 'F' && Prefs.temperatures_unit == 'C') {
292 temp = (int)(floor(f2celsius(temp)+.5));
294 app->disk_temperature[cnt] = temp;
296 if (p) *p = oldv; /* or bug... */
298 } else {
299 ONLY_NTIMES(4,fprintf(stderr, "could not find device '%s' in the output of hddtemp: '%s'\n", dl->name, buff));
303 } else { fprintf(stderr, "error with hddtemp: %s\n", strerror(errno)); }
304 close(fd);
307 const char *power_mode_str(int m) {
308 switch (m) {
309 case HD_ACTIVE: return "active/idle";
310 case HD_STANDBY: return "standby";
311 case HD_SLEEP: return "sleep";
312 case HD_UNKNOWN:
313 default:
314 return "unknown/error";
318 void sethw(App *app, int lw, int lh, int pos, int *px, int *py, int *pw, int *ph) {
319 *px = *py = 0; *pw = lw; *ph = lh;
320 if (!(pos & (AL_RIGHT | AL_LEFT | AL_HCENTER))) *pw = app->dock->w;
321 if (pos & AL_RIGHT) *px = app->dock->w - lw;
322 else if (pos & AL_HCENTER) *px = (app->dock->w - lw)/2;
323 else if (pos & AL_LEFT) *px = 0;
324 else { *px = 0; *pw = app->dock->w; }
325 if (pos & AL_BOTTOM) *py = app->dock->h - lh;
326 else if (pos & AL_VCENTER) *py = (app->dock->h - lh)/2;
327 else if (pos & AL_TOP) *py = 0;
328 else { *py = 0; *ph = app->dock->h; }
331 static void my_imlib_text_draw(int x, int y, const char *s) {
332 char s2[100]; snprintf(s2,100,"%s ",s); /* my imlib2 often forgets half of the last character... */
333 imlib_text_draw(x,y,s2);
336 int is_displayed(int hd_id, int part_id) {
337 return (((hd_id == app->filter_hd) || (app->filter_hd == -1)) &&
338 ((part_id == app->filter_part) || (app->filter_part == -1)));
341 void change_displayed_hd(int dir) {
342 DiskList *dl = find_id(app->filter_hd, app->filter_part);
343 if (dl == NULL) {
344 app->filter_hd = -1; app->filter_part = -1;
345 dl = find_id(app->filter_hd, app->filter_part); assert(dl);
347 else if (dir > 0) {
348 if (app->filter_hd == -1 && app->filter_part == -1) {
349 app->filter_hd = -1; app->filter_part = 0;
350 } else if (app->filter_hd == -1 && app->filter_part == 0) {
351 app->filter_hd = first_dev_in_list()->hd_id;
352 app->filter_part = -1;
353 } else if (app->filter_hd != -1 && app->filter_part != -1) {
354 DiskList *dl2 = dl->next;
355 if (dl2) {
356 if (dl2->hd_id == app->filter_hd)
357 app->filter_part = dl2->part_id;
358 else {
359 app->filter_hd = dl2->hd_id;
360 app->filter_part = -1;
362 } else {
363 app->filter_hd = -1;
364 app->filter_part = -1;
366 } else if (app->filter_hd != -1) {
367 app->filter_part = dl->part_id;
368 } else {
369 app->filter_hd = dl->hd_id;
371 } else if (dir < 0) { /* very very bourin */
372 int ph, pph, pp, ppp;
373 ph = app->filter_hd; pp = app->filter_part;
374 do {
375 pph = app->filter_hd; ppp = app->filter_part;
376 change_displayed_hd(+1);
377 } while (!(app->filter_hd == ph && app->filter_part == pp));
378 app->filter_hd = pph; app->filter_part = ppp;
380 app->displayed_hd_changed = 1;
383 void next_displayed_hd() {
384 BLAHBLAH(1,printf("next_displayed_hd() : filter_hd=%d, filter_part=%d\n", app->filter_hd, app->filter_part));
385 change_displayed_hd(-1);
386 init_stats(app->update_display_delay_ms*1e-3*app->update_stats_mult);
389 void prev_displayed_hd() {
390 BLAHBLAH(1,printf("prev_displayed_hd() : filter_hd=%d, filter_part=%d\n", app->filter_hd, app->filter_part));
391 change_displayed_hd(+1);
392 init_stats(app->update_display_delay_ms*1e-3*app->update_stats_mult);
395 const char *shorten_name(DiskList *dl) {
396 static char s[8];
397 if (dl->name && strlen(dl->name)) {
398 const char *p = dl->name;
399 if (strchr(p,'/')) p = strrchr(p,'/')+1;
400 if (strlen(p) == 0) p = dl->name;
401 snprintf(s, sizeof s, "%s%s", dl->part_id ? " " : "", p);
402 } else strncpy(s, dl->name, sizeof s);
403 return s;
406 static void draw_hdlist(App *app) {
407 if (Prefs.hdlist_pos == AL_NONE) return;
408 DiskList *dl;
410 int dev_cnt, hd_cnt, w;
411 static int lw = -1, lh = -1, lx = -1, ly = -1, h = -1,
412 reshape_cnt = 0;
414 if (!app->smallfont) return;
415 if (app->displayed_hd_changed) { lx = -1; app->displayed_hd_changed = 0; }
416 imlib_context_set_font(app->smallfont);
417 /* get dimensions */
418 if (lx == -1 || reshape_cnt != app->reshape_cnt) {
419 int wtemp = 1;
420 lw = 0; lh = 0;
421 //printf("update : first displayed(%d) = %p\n", cnt, dl);
423 for (dl = first_dev_in_list(), dev_cnt=hd_cnt=-1; dl; dl = dl->next) {
424 if (dl->part_id == 0) ++hd_cnt; if (!is_displayed(dl->hd_id, dl->part_id)) continue; ++dev_cnt;
425 imlib_get_text_size(shorten_name(dl),&w,&h);
426 lw = MAX(w,lw);
427 lh += h;
429 if (!Prefs.disable_hd_leds) { lw += 5; }
430 if (Prefs.enable_hddtemp) imlib_get_text_size(" 000",&wtemp,&h);
431 if (lw + wtemp > (int)(app->dock->w*2/3)) { lw = app->dock->w; }
432 else lw += wtemp;
433 sethw(app,lw,lh,Prefs.hdlist_pos,&lx,&ly,&lw,&lh);
434 reshape_cnt = app->reshape_cnt;
437 imlib_context_set_color(100, 100, 100, 150);
438 imlib_image_fill_rectangle(lx,ly,lw,lh);
439 imlib_context_set_color(100, 100, 100, 200);
440 imlib_image_draw_rectangle(lx-1,ly-1,lw+2,lh+2);
442 for (dl = first_dev_in_list(), dev_cnt=hd_cnt=-1; dl; dl = dl->next) {
443 if (dl->part_id==0) ++hd_cnt; if (!is_displayed(dl->hd_id, dl->part_id)) continue; ++dev_cnt;
444 int x = lx, y = ly + lh - dev_cnt * h;
445 if (!Prefs.disable_hd_leds) {
446 if (dl->touched_r) {
447 imlib_context_set_color(50, 255, 50, 25*dl->touched_r--);
448 imlib_image_fill_rectangle(lx+1,y-5,3,3);
450 if (dl->touched_w) {
451 imlib_context_set_color(255,100-10*dl->touched_w,100-10*dl->touched_w, 25*dl->touched_w--);
452 imlib_image_fill_rectangle(lx+1,y-9,3,3);
454 x += 5;
456 imlib_context_set_color(200, 255, 255, 200);
457 my_imlib_text_draw(x,y-h,shorten_name(dl));
458 if (dl->part_id==0 && app->disk_temperature[hd_cnt] != -1) {
459 char s[200];
460 if (app->disk_temperature[hd_cnt] != -2) {
461 snprintf(s,200,"%d",app->disk_temperature[hd_cnt]);
462 } else {
463 strcpy(s,"SLP");
465 imlib_get_text_size(s,&w,&h);
466 x = lx + lw - w + ((app->disk_temperature[hd_cnt] != -2) ? -7 : -3);
467 imlib_context_set_color(255, 250, 100, 255);
468 my_imlib_text_draw(x, y-h, s);
469 imlib_context_set_color(255, 255, 255, 200);
471 /* below is a quick fix for the degree sign which is not drawn
472 properly by recent release of imlib -- many thanks to
473 Krzysztof Kotlenga for the patch */
475 //if (app->disk_temperature[hd_cnt] != -2) imlib_image_draw_ellipse(x+w+3, y - h + 2, 1, 1);
477 //if (app->disk_temperature[hd_cnt] != -2) imlib_image_draw_ellipse(x+w+3, y - h + 2, 1, 1);
478 /* imlib2 >= 1.1.1 workaround - imlib_image_draw_ellipse() can't properly draw circle with radius = 1 */
479 if (app->disk_temperature[hd_cnt] != -2) {
480 /*ImlibPolygon poly;
481 poly = imlib_polygon_new();
482 imlib_polygon_add_point(poly, x+w+3, y-h+3);
483 imlib_polygon_add_point(poly, x+w+2, y-h+2);
484 imlib_polygon_add_point(poly, x+w+3, y-h+1);
485 imlib_polygon_add_point(poly, x+w+4, y-h+2);
486 imlib_image_draw_polygon(poly, 1);*/
487 /*imlib_image_draw_pixel(x+w+3, y-h+3, 0);
488 imlib_image_draw_pixel(x+w+2, y-h+2, 0);
489 imlib_image_draw_pixel(x+w+3, y-h+1, 0);
490 imlib_image_draw_pixel(x+w+4, y-h+2, 0);*/
492 int i=x+w+2, j=y-h;
493 imlib_image_draw_pixel(i+1,j,0);
494 imlib_image_draw_pixel(i+2,j,0);
495 imlib_image_draw_pixel(i+3,j+1,0);
496 imlib_image_draw_pixel(i+3,j+2,0);
497 imlib_image_draw_pixel(i+1,j+3,0);
498 imlib_image_draw_pixel(i+2,j+3,0);
499 imlib_image_draw_pixel(i,j+1,0);
500 imlib_image_draw_pixel(i,j+2,0);
506 static void draw_throughput(App *app) {
507 static int tpstep = 0, tpw, tph;
508 static char tpmsg[20];
509 static int lw = -1, lh = -1, lx = -1, ly = -1;
510 static int reshape_cnt = 0;
511 if (Prefs.popup_throughput_pos == AL_NONE) return;
513 if (!app->bigfont) return;
514 imlib_context_set_font(app->bigfont);
515 /* get dimensions (only once) */
516 if (lx == -1 || app->reshape_cnt != reshape_cnt) {
517 imlib_get_text_size("00.0M/s",&lw,&lh);
518 if (lw > (int)(app->dock->w*3/4)) { lw = app->dock->w; }
519 sethw(app,lw,lh,Prefs.popup_throughput_pos,&lx,&ly,&lw,&lh);
520 reshape_cnt = app->reshape_cnt;
523 if (get_read_mean_throughput() + get_write_mean_throughput() > Prefs.popup_throughput_threshold) {
524 tpstep = MIN(tpstep+1,4);
525 snprintf(tpmsg,sizeof tpmsg, "%.1fM/s",get_read_mean_throughput() + get_write_mean_throughput());
526 imlib_get_text_size(tpmsg,&tpw,&tph);
527 if (tpw > lw) {
528 snprintf(tpmsg,sizeof tpmsg, "%.1fM",get_read_mean_throughput() + get_write_mean_throughput());
529 imlib_get_text_size(tpmsg,&tpw,&tph);
531 } else if (tpstep) tpstep--;
532 if (tpstep) {
533 imlib_context_set_color(128, 128, 128, tpstep*30);
534 imlib_image_draw_rectangle(lx-1,ly-1,lw+2,lh+2);
535 imlib_context_set_color(128, 128, 128, 10+tpstep*25);
536 imlib_image_fill_rectangle(lx,ly,lw,lh);
537 imlib_context_set_color(255, 255, 255, 50+tpstep*50);
538 my_imlib_text_draw(lx + (lw - tpw)/2, ly, tpmsg);
542 static void draw(App *app) {
543 DATA32 *buff = imlib_image_get_data();
545 if (!Prefs.disable_io_matrix) {
546 evolve_io_matrix(app,buff);
547 //draw_io_matrix(app,buff);
548 } else memset(buff, 0, sizeof(DATA32)*app->dock->w*app->dock->h);
549 imlib_image_put_back_data(buff);
550 draw_hdlist(app);
551 if (!Prefs.disable_swap_matrix) draw_swap_matrix(app);
552 draw_throughput(app);
555 void reshape(int w, int h) {
556 DockImlib2 *dock = app->dock;
557 static int isinit = 0;
558 dock->w = w; dock->h = h;
559 dock->win_width = dock->w + dock->x0;
560 dock->win_height = dock->h + dock->y0;
561 app->reshape_cnt++;
562 app->sm.w = 6;
563 app->sm.nrow = (dock->w-1) / app->sm.w;
564 app->sm.ncol = (dock->h-1) / app->sm.w;
565 if (isinit) FREE_ARR(app->sm.pre_cnt);
566 ALLOC_ARR(app->sm.pre_cnt, app->sm.nrow, app->sm.ncol);
567 if (isinit) FREE_ARR(app->sm.intensity)
568 ALLOC_ARR(app->sm.intensity, app->sm.nrow, app->sm.ncol);
569 app->iom.w = dock->w; app->iom.h = dock->h;
570 if (isinit) FREE_ARR((void*)app->iom.v);
571 ALLOC_ARR(app->iom.v,app->iom.h+4, app->iom.w+2);
572 if (isinit) { dockimlib2_reset_imlib(dock); }
574 isinit = 1;
577 #ifndef GKRELLM
578 static void event_loop(App *app) {
579 int tic_cnt = 0;
580 while (1) {
581 XEvent ev;
582 tic_cnt++;
583 if (tic_cnt % 5 == 0) {
584 XWindowAttributes attr;
585 if (app->dock->normalwin) {
586 XGetWindowAttributes(app->dock->display, app->dock->normalwin, &attr);
587 app->dock->normalwin_mapped = (attr.map_state == IsViewable);
589 if (app->dock->iconwin) {
590 XGetWindowAttributes(app->dock->display, app->dock->iconwin, &attr);
591 app->dock->iconwin_mapped = (attr.map_state == IsViewable);
595 while (XPending(app->dock->display)) {
596 XNextEvent(app->dock->display, &ev);
597 switch (ev.type) {
598 case ClientMessage:
599 if (ev.xclient.message_type == app->dock->atom_WM_PROTOCOLS
600 && ev.xclient.format == 32
601 && (Atom)ev.xclient.data.l[0] == app->dock->atom_WM_DELETE_WINDOW) {
602 exit(0);
604 break;
605 case ButtonRelease:
606 //exit(0);
607 if (ev.xbutton.button == Button4) prev_displayed_hd(-1);
608 else if (ev.xbutton.button == Button5 || ev.xbutton.button == Button1) next_displayed_hd(+1);
609 break;
610 case ConfigureNotify: {
611 if (app->dock->iconwin == None &&
612 (ev.xconfigure.width != (int)app->dock->win_width ||
613 ev.xconfigure.height != (int)app->dock->win_height)) {
614 app->dock->w = app->dock->win_width = ev.xconfigure.width;
615 app->dock->h = app->dock->win_height = ev.xconfigure.height;
616 reshape(ev.xconfigure.width, ev.xconfigure.height); //app->dock->w, app->dock->h, None);
617 /*printf("ConfigureNotify : %dx%d %dx%d\n",
618 ev.xconfigure.width, ev.xconfigure.height,
619 app->sm.nrow, app->sm.ncol);*/
621 } break;
624 if (tic_cnt % app->update_stats_mult == 0) {
625 update_stats();
626 if (!Prefs.disable_io_matrix) update_io_matrix(app);
627 if (!Prefs.disable_swap_matrix) update_swap_matrix(app);
629 if (tic_cnt % 100 == 5) {
630 # ifdef ENABLE_HDDTEMP_QUERY
631 if (Prefs.enable_hddtemp) {
632 query_hddtemp(app);
634 # endif
635 # ifdef ENABLE_POWER_QUERY
636 if (Prefs.enable_power_status) {
637 DiskList *dl; int cnt;
638 for (cnt = 0, dl = first_hd_in_list(); dl; dl = next_hd_in_list(dl), ++cnt) {
639 if (dl->enable_power_status && !dl->is_scsi) {
640 char devname[512]; snprintf(devname, 512, "/dev/%s", dl->name);
641 app->disk_power_mode[cnt] = query_power_mode(devname);
645 # endif
647 //if (tic_cnt > 500) exit(1);
648 usleep(app->update_display_delay_ms * 1000);
649 draw(app);
650 dockimlib2_render(app->dock);
653 #endif /* ndef GKRELLM */
655 void setup_cmap(cmap *m) {
656 struct {
657 float x0;
658 DATA32 c;
659 } colors0[] = {{-128, 0xff8080},
660 {- 70, 0xF00000},
661 { -60, 0xDf0080},
662 { -20, 0x800000},
663 { 0, 0x000000},
664 { 10, 0x008000},
665 { 60, 0xf09000},
666 { 90, 0xffa000},
667 { 116, 0xffd000},
668 { 127, 0xffff00}},
669 colors1[] = {{-128, 0xff0000},
670 {- 64, 0x808080},
671 { 0, 0x404040},
672 //{ , 0x000000},
673 //{ 0 , 0x000000},
674 { 1, 0x208020},
675 { 64, 0x509050},
676 {+ 90, 0x60C060},
677 {+127, 0x008000}},
678 colors2[] = {{-128, 0x400000},
679 { -60, 0xA00000},
680 { -34, 0xff0000},
681 { -16, 0x400000},
682 { 0, 0x000000},
683 { 16, 0x000040},
684 { 34, 0x0000ff},
685 { 60, 0x0000A0},
686 {+128, 0x000040}},
687 colors3[] = {{-128, 0x500060},
688 { -60, 0x500050},
689 { -34, 0x000000},
690 { 0, 0x000000},
691 { 34, 0x000000},
692 { 60, 0x206020},
693 {+128, 0x205020}},
694 colors4[] = {{-128, 0x5000F0},
695 { -70, 0x0000C0},
696 { -50, 0x0000A0},
697 { -40, 0x707090},
698 { -30, 0x000080},
699 { -20, 0x505070},
700 { -10, 0x000060},
701 { 0, 0x000000},
702 { 10, 0x006000},
703 { 20, 0x707070},
704 { 30, 0x008000},
705 { 40, 0x909090},
706 { 50, 0x00A000},
707 { 70, 0x00C000},
708 {+128, 0x20D020}},
710 *cdef = NULL;
712 #define SELMAP(n) \
713 if (Prefs.iomatrix_colormap == n) { sz = sizeof(colors##n)/sizeof(*colors0); cdef = colors##n; }
715 unsigned i, sz=0;
716 SELMAP(0); SELMAP(1); SELMAP(2); SELMAP(3); SELMAP(4);
717 float x0 = cdef[0].x0, x1 = cdef[sz-1].x0;
718 for (i = 0; i < sz - 1; ++i) {
719 int i0 = (int)((cdef[i].x0-x0)*CMAPSZ/(x1-x0));
720 int i1 = (int)((cdef[i+1].x0-x0)*CMAPSZ/(x1-x0));
721 int r1 = cdef[i].c>>16 & 0xff, r2 = cdef[i+1].c>>16 & 0xff;
722 int g1 = cdef[i].c>> 8 & 0xff, g2 = cdef[i+1].c>> 8 & 0xff;
723 int b1 = cdef[i].c & 0xff, b2 = cdef[i+1].c & 0xff;
724 int j;
725 for (j=i0; j <= MIN(i1,CMAPSZ-1); ++j) {
726 float alpha = (j-i0+.5)/(float)(i1-i0);
727 m->p[j] =
728 (MIN((int)(r1*(1-alpha) + alpha*r2),255)<<16) +
729 (MIN((int)(g1*(1-alpha) + alpha*g2),255)<<8) +
730 (MIN((int)(b1*(1-alpha) + alpha*b2),255));
731 //printf("cmap[%d] = 0x%06x\n", j, m->p[j]);
736 unsigned getpos(const char *pp) {
737 char p[2];
738 unsigned v = AL_NONE, i;
739 if (!pp || !pp[0]) return AL_NONE;
740 if (strlen(pp) > 2) { fprintf(stderr, "invalid position specification: '%s'\n", pp); exit(1); }
741 strncpy(p,pp,2);
742 if (p[0] == 'c') { char tmp = p[0]; p[0] = p[1]; p[1] = tmp; }
743 for (i=0; i < 2 && p[i]; ++i) {
744 if (p[i] == 'r') { v |= AL_RIGHT; }
745 else if (p[i] == 'l') { v |= AL_LEFT; }
746 else if (p[i] == 't') { v |= AL_TOP; }
747 else if (p[i] == 'b') { v |= AL_BOTTOM; }
748 else if (p[i] == 'c') {
749 if (v & (AL_LEFT | AL_RIGHT | AL_HCENTER)) v |= AL_VCENTER; else v |= AL_HCENTER;
750 } else {
751 fprintf(stderr, "unknown position specifier: '%c'\n", p[i]); exit(1);
754 return v;
757 void init_prefs(int argc, char **argv) {
758 #ifndef GKRELLM
759 /* Prefs already read from the gkrellm config file: */
760 Prefs.disable_swap_matrix = 0;
761 Prefs.disable_io_matrix = 0;
762 Prefs.disable_hd_leds = 0;
763 Prefs.iomatrix_colormap = 0;
764 Prefs.popup_throughput_threshold = 0.5; /* MB/s */
765 Prefs.hdlist_pos = AL_BOTTOM + AL_LEFT;
766 Prefs.enable_hddtemp = 0;
767 Prefs.bigfontname = Prefs.smallfontname = NULL;
768 #endif
769 Prefs.xprefs.dockapp_size = 64;
770 Prefs.verbosity = 0;
771 Prefs.hddtemp_port = 7634;
772 Prefs.enable_power_status = 0;
773 Prefs.debug_swapio = 0; Prefs.debug_disk_wr = Prefs.debug_disk_rd = 0;
774 Prefs.popup_throughput_pos = AL_TOP;
775 Prefs.xprefs.argc = argc; Prefs.xprefs.argv = argv;
776 Prefs.xprefs.flags = 0;
777 Prefs.temperatures_unit = 'C';
780 #ifndef GKRELLM
781 void parse_options(int argc, char **argv) {
782 int d_opt_used = 0;
783 enum { OPT_DISPLAY=1, OPT_SMALLFONT, OPT_BIGFONT, OPT_FONTPATH,
784 OPT_NOSWAP, OPT_NOIO, OPT_NOLEDS, OPT_HDLIST, OPT_THROUGHPUT, OPT_32, OPT_56, OPT_48 };
785 static struct option long_options[] = {
786 {"help",0,0,'h'},
787 {"verbose",0,0,'v'},
788 {"version",0,0,'V'},
789 {"hddtemp",0,0,'t'},
790 {"farenheit",0,0,'F'},
791 {"display",1,0,OPT_DISPLAY},
792 {"geometry",2,0,'g'},
793 {"smallfont",1,0,OPT_SMALLFONT},
794 {"bigfont",1,0,OPT_BIGFONT},
795 {"fontpath",1,0,OPT_FONTPATH},
796 {"colormap",1,0,'c'},
797 {"noswap",0,0,OPT_NOSWAP},
798 {"noio",0,0,OPT_NOIO},
799 {"noleds",0,0,OPT_NOLEDS},
800 {"hdlist",2,0,OPT_HDLIST},
801 {"throughput",2,0,OPT_THROUGHPUT},
802 {"32",0,0,OPT_32},
803 {"48",0,0,OPT_48},
804 {"56",0,0,OPT_56},
805 {NULL,0,0,0}
807 int long_options_index;
808 const char *help =
809 "wmhdplop " VERSION " - monitor hard-drive (or partition) activity\n"
810 "A recent kernel is required (>2.4.20)\n"
811 "Usage: wmhdplop [options]\n"
812 "Option list:\n"
813 " -h, --help print this.\n"
814 " -v, --verbose increase verbosity\n"
815 " -V, --version print version\n"
816 " -t[=port], --hddtemp[=port]\n"
817 " display hd temperatures, in Celsius degrees (requires hddtemp daemon running)\n"
818 " -F, --farenheit display hd temperatures in Farenheit degrees\n"
819 /* " -p : monitor harddrive power status (idle, standby or sleeping, as reported by hdparm -C)\n"
820 " note that this options requires that wmhd is suid..\n"*/ /* hddtemp already reports when a drive is asleep ... */
821 " --noswap disable the animation reflecting swap activity\n"
822 " --noio disable background animation reflecting disk activity\n"
823 " --noleds hide the small led indicating disk activity\n"
824 " --fontpath path add a new directory to the font search directory list\n"
825 " default: --fontpath=/usr/share/fonts/truetype (and subdirectories)\n"
826 " --fontpath=/usr/share/fonts/ttf (and subdirectories)\n"
827 " --fontpath=$HOME/.fonts (and subdirectories)\n"
828 " --smallfont fontname/size\n"
829 " --bigfont fontname/size\n"
830 " Set the 'small font' and the 'big font' name/size in pixel\n"
831 " (default: --smallfont=Vera/7 --bigfont=Arial_Black/10)\n"
832 " The font name are case-sensitive, and must correspound to the name\n"
833 " of a .ttf file which can be found in one of the fontpaths\n"
834 " By default, wmhdplop tries to load the following fonts:\n"
835 " * Arial_Black/10, FreeSansBold/11, VeraMoBd/9\n"
836 " * Vera/7, FreeSans/7, Trebuchet_MS/7\n"
837 " -c n, --colormap=n\n"
838 " select colormap number n\n"
839 " --hdlist[=pos] hide completely the list of drives with leds and temperatures (if no position given)\n"
840 " or change its location (default: 'B'). Possible positions are: 't','b','l','bl','bc','br',...\n"
841 " (t=top, b=bottom, l=left, r=right, c=centered, etc)\n"
842 " --throughput=mbs[,pos]\n"
843 " minimum io throughput (MB/s) that will be displayed in the popup display (default 0.5)\n"
844 " and position of the popup (same format than hdlist)\n"
845 " -g[=WxH+x+y], --geometry[=WxH+x+y]\n"
846 " start in window (i.e. undocked) mode with specified geometry (i.e -g 96x32 or -g 64x64+0+0)\n"
847 " --32, --48, --56\n"
848 " start in a reduced dockapp, for people whose dock is too small too contain 64x64 dockapps\n"
849 " -d device[:name] monitor activity on the specified hard-drive. This option can be\n"
850 " used many times. If you do not use it, all hard-drives will be monitored.\n"
851 " If an optional 'name' is given, it will be displayed instead of the /dev/hdx name\n"
852 " The device may also be a single partition.\n";
853 init_prefs(argc, argv);
854 while (1) {
855 int c;
856 c = getopt_long(argc, argv, "g::hvVt::Fc:LP:d:@:",long_options,&long_options_index); /* -g option is handled in dockapp_imlib2 */
857 if (c == -1)
858 break;
859 switch (c) {
860 case ':':
861 case '?':
862 exit(1);
863 case OPT_DISPLAY:
864 Prefs.xprefs.flags |= DOCKPREF_DISPLAY; Prefs.xprefs.display = strdup(optarg);
865 break;
866 case 'g':
867 Prefs.xprefs.flags |= DOCKPREF_GEOMETRY; if (optarg) Prefs.xprefs.geometry = strdup(optarg);
868 break;
869 case 'h':
870 puts(help); exit(0);
871 case 'v':
872 Prefs.verbosity++;
873 break;
874 case 'V':
875 printf("wmhdplop %s\n",VERSION); exit(0);
876 break;
877 case 't':
878 Prefs.enable_hddtemp = 1;
879 if (optarg) Prefs.hddtemp_port = atoi(optarg);
880 break;
881 /*case 'p':
882 Prefs.enable_power_status = 1;
883 break;*/
884 case 'F':
885 Prefs.enable_hddtemp = 1;
886 Prefs.temperatures_unit = 'F';
887 break;
888 case OPT_NOIO:
889 Prefs.disable_io_matrix = 1;
890 break;
891 case OPT_NOSWAP:
892 Prefs.disable_swap_matrix = 1;
893 break;
894 case OPT_NOLEDS:
895 Prefs.disable_hd_leds = 1;
896 break;
897 case OPT_FONTPATH:
898 printf("add font path: %s\n", optarg);
899 imlib_add_path_to_font_path(optarg);
900 break;
901 case OPT_SMALLFONT:
902 Prefs.smallfontname = strdup(optarg);
903 break;
904 case OPT_BIGFONT:
905 Prefs.bigfontname = strdup(optarg);
906 break;
907 case OPT_HDLIST:
908 Prefs.hdlist_pos = getpos(optarg);
909 break;
910 case 'c':
911 Prefs.iomatrix_colormap = atoi(optarg);
912 if (Prefs.iomatrix_colormap > 4) { fprintf(stderr,"invalid colormap number\n"); exit(1); }
913 break;
914 case OPT_THROUGHPUT:
915 if (optarg) {
916 Prefs.popup_throughput_threshold = atof(optarg);
917 if (strchr(optarg,',')) Prefs.popup_throughput_pos = getpos(strchr(optarg,',')+1);
918 } break;
919 case OPT_56:
920 Prefs.xprefs.dockapp_size = 56;
921 break;
922 case OPT_48:
923 Prefs.xprefs.dockapp_size = 48;
924 break;
925 case OPT_32:
926 Prefs.xprefs.dockapp_size = 32;
927 break;
928 case 'd':
929 if (optarg) {
930 char *p = strchr(optarg, ':');
931 assert(optarg);
932 if (p) *p++ = 0;
933 BLAHBLAH(1,printf("adding device %s to monitored disc list\n",optarg));
934 if (add_device_by_name(optarg, p) != 0)
935 fprintf(stderr, "Warning: device %s not found or not recognized -- try option -v to get additionnal information\n", optarg);
936 d_opt_used = 1;
937 } break;
938 case '@':
939 sscanf(optarg,"%d,%d,%d",&Prefs.debug_swapio,&Prefs.debug_disk_rd,&Prefs.debug_disk_wr);
940 break;
941 default:
942 assert(0);
945 if (optind != argc) {
946 fprintf(stderr, "unknown option: %s\n", argv[optind]); exit(1);
948 if (!d_opt_used) scan_all_hd(1);
950 #endif
952 char *xstrdup(const char *s) {
953 if (s) return strdup(s);
954 else return NULL;
957 void init_fonts(App *app) {
958 char *bigfontlist[] = {"Arial_Black/10", "luxisb/11", "VeraMoBd/9", "arialbd/12", "Vera/9", "Verdana_Bold/10", "VerdanaBd/10", "Verdana/10", "FreeSansBold/11", NULL};
959 char *smallfontlist[] = {"Vera/7","Trebuchet_MS/7", "luxisr/7", "Verdana/7","Arial/7","FreeSans/7", NULL};
960 if (app->bigfont) {
961 imlib_context_set_font(app->bigfont); imlib_free_font(); app->bigfont = NULL;
963 if (app->smallfont) {
964 imlib_context_set_font(app->smallfont); imlib_free_font(); app->smallfont = NULL;
966 app->bigfont = load_font(Prefs.bigfontname, bigfontlist);
967 if (app->bigfont)
968 app->current_bigfont_name = strdup(dockimlib2_last_loaded_font());
969 app->smallfont = load_font(Prefs.smallfontname, smallfontlist);
970 if (app->smallfont)
971 app->current_smallfont_name = strdup(dockimlib2_last_loaded_font());
974 #ifndef GKRELLM
975 int main(int argc, char**argv)
976 #else
977 int hdplop_main(int width, int height, GdkDrawable *gkdrawable)
978 #endif
980 euid = geteuid(); uid = getuid(); seteuid(uid);
981 ALLOC_OBJ(app);
982 srand(time(NULL));
983 /* Initialize options */
984 #ifndef GKRELLM
985 parse_options(argc,argv);
986 #else
987 init_prefs(0, NULL);
988 scan_all_hd(1);
989 #endif
990 /* Initialize imlib2 */
991 #ifndef GKRELLM
992 app->dock = dockimlib2_setup(2, 2, Prefs.xprefs.dockapp_size-5, Prefs.xprefs.dockapp_size-5, &Prefs.xprefs);
993 #else
994 app->dock = dockimlib2_gkrellm_setup(0, 0, width, height, &Prefs.xprefs, gkdrawable);
995 #endif
996 app->bigfont = app->smallfont = NULL;
997 app->current_bigfont_name = app->current_smallfont_name = NULL;
998 app->reshape_cnt = 0;
999 if (find_id(-1,0) == 0) {
1000 app->filter_hd = -1; app->filter_part = -1; /* only partitions */
1001 } else {
1002 app->filter_hd = -1; app->filter_part = 0;
1004 app->displayed_hd_changed = 1;
1005 if (nb_dev_in_list() == 0) {
1006 #ifndef GKRELLM
1007 fprintf(stderr, "No common hard-drives found in /etc/mtab and /proc/partitions..\n"
1008 "Please use option -d to add devices (i.e. %s -d /dev/hda -d /dev/hdb ...)\n", argv[0]);
1009 exit(1);
1010 #else
1011 fprintf(stderr, "No hard drive found...\n");
1012 /* Euh, on fait quoi maintenant ? exit ? */
1013 #endif
1015 init_fonts(app);
1016 app->update_display_delay_ms = 50;
1017 app->update_stats_mult = 2;
1019 app->swap_matrix_lighting = (int)(300/app->update_display_delay_ms);
1020 app->swap_matrix_luminosity = 255;
1022 app->nb_hd = nb_hd_in_list();
1023 app->nb_dev = nb_dev_in_list();
1024 ALLOC_VEC(app->disk_power_mode, app->nb_hd); SET_VEC(app->disk_power_mode, HD_ACTIVE,0,app->nb_hd);
1025 ALLOC_VEC(app->disk_temperature, app->nb_hd); SET_VEC(app->disk_temperature, -1, 0, app->nb_hd);
1027 init_stats(app->update_display_delay_ms*1e-3*app->update_stats_mult);
1028 BLAHBLAH(1, {
1029 DiskList *dl = first_dev_in_list();
1030 for ( ; dl; dl=dl->next) {
1031 printf("Monitored: %s (%s) major=%d, minor=%d is_partition=%d\n",
1032 dl->dev_path, dl->name, dl->major, dl->minor, is_partition(dl->major,dl->minor));
1036 reshape(app->dock->w,app->dock->h);
1037 app->iom.ops = NULL;
1038 setup_cmap(&app->iom.cm);
1039 #ifndef GKRELLM
1040 event_loop(app);
1041 #endif
1042 return 0;
1045 #ifdef GKRELLM
1046 void gkrellm_hdplop_update(int update_options) {
1047 static int tic_cnt = 0;
1048 if (update_options) {
1049 setup_cmap(&app->iom.cm);
1050 if (!Prefs.enable_hddtemp)
1051 SET_VEC(app->disk_temperature, -1, 0, app->nb_hd);
1053 // update the data:
1055 if (tic_cnt % app->update_stats_mult == 0) {
1056 update_stats();
1057 if (!Prefs.disable_io_matrix)
1058 update_io_matrix(app);
1059 if (!Prefs.disable_swap_matrix)
1060 update_swap_matrix(app);
1062 if (tic_cnt % 100 == 5 && Prefs.enable_hddtemp) {
1063 query_hddtemp(app);
1066 // draw the Imlib2 pixmap:
1067 draw(app);
1068 dockimlib2_render(app->dock);
1069 ++tic_cnt;
1071 #endif