wmclockmon: update change-log
[dockapps.git] / wmjiface / src / wmjiface.c
blob97e4fae4d5b7f8ea7eaecb0e5df69b7f262ce594
1 #include "jDockApp/jDockApp.h"
2 #include "jDockApp/colors.h"
3 #include <getopt.h>
5 int delay;
7 int skip = 0;
8 int out = 0;
9 int hw1 = 2000;
10 int hw2 = 4000;
11 int showboth = 0;
12 int use_expavg;
14 double alpha = 0.4;
15 double beta = 1.0;
16 double exp_avg[2][5];
18 void show_help() {
19 printf("wmjiface version %s was built on %s.\n\n", VERSION, BDATE);
20 printf("-s <sec>: Seconds between executions of ifacechk\n");
21 printf("-o : Show the traffic headed out instead.\n");
22 printf("-D : Doommaker's Show-Both (in&out traf simultaneously).\n");
23 printf("-A : Use approximate exponential averaging.\n");
24 printf("-a <num>: Alpha for the exp. avg. {default %f}.\n", alpha);
25 printf("-b <num>: Beta: subst for alpha if bps==0 {default %f}.\n", beta);
26 printf("-l <num>: Skip <num> lines before showing the first device.\n");
27 printf("-1 <num>: High-water-mark 1 (red color) {default %i B/s}.\n",hw1);
28 printf("-2 <num>: High-water-mark 2 (red color) {default %i B/s}.\n",hw2);
30 exit(0);
33 void setup(int argc, char** argv) {
34 //char c;
35 int c; // thanx to a PowerPC user named Carine Bournez
36 int i = 0;
38 while( -1 != (c = getopt(argc, argv, "hoADua:b:s:l:1:2:"))) {
39 switch(c) {
40 case '?': exit(1);
41 case 'h': show_help();
43 case 'o': out = 1; break;
44 case 'A': use_expavg = 1; break;
45 case 'D': showboth = 1; break;
47 case 'a': alpha=atof(optarg); break;
48 case 'b': beta=atof(optarg); break;
50 case 's': i=atoi(optarg); break;
51 case 'l': skip=atoi(optarg); break;
53 case '1': hw1=atoi(optarg); break;
54 case '2': hw2=atoi(optarg); break;
57 delay = (i) ? i : 1;
58 set_update_delay(delay); /* seconds */
59 set_loop_delay(1000); /* mu seconds */
62 void clear_avg() {
63 int i;
64 for(i=0; i<5; i++) {
65 exp_avg[0][i] = 0;
66 exp_avg[1][i] = 0;
70 void do_print_one_dev(char iface[5], char io, int bps_, int row) {
71 int color;
72 char letter;
73 float display;
75 jpprintf(0, row, GREEN, "%c", io);
76 if(iface[0] >= '0' && iface[0] <= '9') {
77 jprintf(RED, "%s", "unkn");
78 } else {
79 jprintf(BLUE, "%s", iface);
81 jprintf(CYAN, ":", io);
83 if(bps_ >= 0) {
84 if(bps_ > 1000) { display = (bps_)/1024.0; letter = 'k'; }
85 else { display = bps_; letter = ' '; }
87 display += 0.5; /* Round Up */
89 if(!bps_) { color = BLUE; }
90 else if(bps_ < hw1) { /* 2000 default */ color = GREEN; }
91 else if(bps_ < hw2) { /* 4000 default */ color = YELLOW; }
92 else { color = RED; }
94 if(letter=='k' && display<100) {
95 jpprintf(6, row, color, "%.1f", display);
96 } else {
97 jpprintf(6, row, color, "%.0f", display);
100 jprintf(ORANGE, "%c", letter);
101 } else {
102 jprintf(RED, "err");
103 clear_avg();
107 #define IN 0
108 #define OUT 1
110 #define Fal0d(X) ((X) ? alpha : beta)
112 void do_update() {
113 FILE *f = popen("ifacechk", "r");
114 char iface[5];
115 int bps_in, bps_out;
116 int row = 2 + skip;
117 int c = 0;
118 int toshow_in, toshow_out;
120 clear_window();
121 jpprintf(0, 0, YELLOW, " J-Iface"); /* title */
122 jpprintf(0, 1, PINK, "Dev:"); /* desc line */
123 jpprintf(5, 1, PINK, "bits/s");
125 while(1 + fscanf(f, "%s%i%i", iface, &bps_in, &bps_out)) {
126 if(use_expavg) {
127 exp_avg[IN][c] =
128 (Fal0d(bps_in)*bps_in) + (1-Fal0d(bps_in))*exp_avg[IN][c];
129 exp_avg[OUT][c] =
130 (Fal0d(bps_out)*bps_out) + (1-Fal0d(bps_out))*exp_avg[OUT][c];
131 toshow_in = exp_avg[IN ][c];
132 toshow_out = exp_avg[OUT][c];
133 } else {
134 toshow_in = bps_in;
135 toshow_out = bps_out;
138 if(showboth) {
139 if(row > 4) break;
140 do_print_one_dev(iface, 'i', toshow_in, row++);
141 do_print_one_dev(iface, 'o', toshow_out, row++);
142 } else {
143 if(row > 5) break;
144 do_print_one_dev(
145 iface,
146 (out) ? 'o':'i',
147 (out) ? toshow_out : toshow_in,
148 row++
151 c++;
154 pclose(f);
157 void do_expose() {
158 do_update();
161 void do_button_release() {
162 do_update();