bargraph.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <bio.h>
  5. #include <event.h>
  6. enum {PNCTL=3};
  7. static char* rdenv(char*);
  8. int newwin(char*);
  9. Rectangle screenrect(void);
  10. int nokill;
  11. int textmode;
  12. char *title;
  13. Image *light;
  14. Image *dark;
  15. Image *text;
  16. void
  17. initcolor(void)
  18. {
  19. text = display->black;
  20. light = allocimagemix(display, DPalegreen, DWhite);
  21. dark = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DDarkgreen);
  22. }
  23. Rectangle rbar;
  24. Point ptext;
  25. vlong n, d;
  26. int last;
  27. int lastp = -1;
  28. int first = 1;
  29. char backup[80];
  30. void
  31. drawbar(void)
  32. {
  33. int i, j;
  34. int p;
  35. char buf[200], bar[100];
  36. static char lastbar[100];
  37. if(n > d || n < 0 || d <= 0)
  38. return;
  39. i = (Dx(rbar)*n)/d;
  40. p = (n*100LL)/d;
  41. if(textmode){
  42. bar[0] = '|';
  43. for(j=0; j<i; j++)
  44. bar[j+1] = '#';
  45. for(; j<60; j++)
  46. bar[j+1] = '-';
  47. bar[61] = '|';
  48. bar[62] = ' ';
  49. sprint(bar+63, "%3d%% ", p);
  50. for(i=0; bar[i]==lastbar[i] && bar[i]; i++)
  51. ;
  52. memset(buf, '\b', strlen(lastbar)-i);
  53. strcpy(buf+strlen(lastbar)-i, bar+i);
  54. if(buf[0])
  55. write(1, buf, strlen(buf));
  56. strcpy(lastbar, bar);
  57. return;
  58. }
  59. if(lastp == p && last == i)
  60. return;
  61. if(lastp != p){
  62. sprint(buf, "%d%%", p);
  63. stringbg(screen, addpt(screen->r.min, Pt(Dx(rbar)-30, 4)), text, ZP, display->defaultfont, buf, light, ZP);
  64. lastp = p;
  65. }
  66. if(last != i){
  67. draw(screen, Rect(rbar.min.x+last, rbar.min.y, rbar.min.x+i, rbar.max.y),
  68. dark, nil, ZP);
  69. last = i;
  70. }
  71. flushimage(display, 1);
  72. }
  73. void
  74. eresized(int new)
  75. {
  76. Point p, q;
  77. Rectangle r;
  78. if(new && getwindow(display, Refnone) < 0)
  79. fprint(2,"can't reattach to window");
  80. r = screen->r;
  81. draw(screen, r, light, nil, ZP);
  82. p = string(screen, addpt(r.min, Pt(4,4)), text, ZP,
  83. display->defaultfont, title);
  84. p.x = r.min.x+4;
  85. p.y += display->defaultfont->height+4;
  86. q = subpt(r.max, Pt(4,4));
  87. rbar = Rpt(p, q);
  88. ptext = Pt(r.max.x-4-stringwidth(display->defaultfont, "100%"), r.min.x+4);
  89. border(screen, rbar, -2, dark, ZP);
  90. last = 0;
  91. lastp = -1;
  92. drawbar();
  93. }
  94. void
  95. bar(Biobuf *b)
  96. {
  97. char *p, *f[2];
  98. Event e;
  99. int k, die, parent, child;
  100. parent = getpid();
  101. die = 0;
  102. if(textmode)
  103. child = -1;
  104. else
  105. switch(child = rfork(RFMEM|RFPROC)) {
  106. case 0:
  107. sleep(1000);
  108. while(!die && (k = eread(Ekeyboard|Emouse, &e))) {
  109. if(nokill==0 && k == Ekeyboard && (e.kbdc == 0x7F || e.kbdc == 0x03)) { /* del, ctl-c */
  110. die = 1;
  111. postnote(PNPROC, parent, "interrupt");
  112. _exits("interrupt");
  113. }
  114. }
  115. _exits(0);
  116. }
  117. while(!die && (p = Brdline(b, '\n'))) {
  118. p[Blinelen(b)-1] = '\0';
  119. if(tokenize(p, f, 2) != 2)
  120. continue;
  121. n = strtoll(f[0], 0, 0);
  122. d = strtoll(f[1], 0, 0);
  123. drawbar();
  124. }
  125. postnote(PNCTL, child, "kill");
  126. }
  127. void
  128. usage(void)
  129. {
  130. fprint(2, "usage: bargraph [-kt] [-w minx,miny,maxx,maxy] 'title'\n");
  131. exits("usage");
  132. }
  133. void
  134. main(int argc, char **argv)
  135. {
  136. Biobuf b;
  137. char *p, *q;
  138. int lfd;
  139. p = "0,0,200,60";
  140. ARGBEGIN{
  141. case 'w':
  142. p = ARGF();
  143. break;
  144. case 't':
  145. textmode = 1;
  146. break;
  147. case 'k':
  148. nokill = 1;
  149. break;
  150. default:
  151. usage();
  152. }ARGEND;
  153. if(argc != 1)
  154. usage();
  155. title = argv[0];
  156. lfd = dup(0, -1);
  157. while(q = strchr(p, ','))
  158. *q = ' ';
  159. Binit(&b, lfd, OREAD);
  160. if(textmode || newwin(p) < 0){
  161. textmode = 1;
  162. rbar = Rect(0, 0, 60, 1);
  163. }else{
  164. initdraw(0, 0, "bar");
  165. initcolor();
  166. einit(Emouse|Ekeyboard);
  167. eresized(0);
  168. }
  169. bar(&b);
  170. }
  171. /* all code below this line should be in the library, but is stolen from colors instead */
  172. static char*
  173. rdenv(char *name)
  174. {
  175. char *v;
  176. int fd, size;
  177. fd = open(name, OREAD);
  178. if(fd < 0)
  179. return 0;
  180. size = seek(fd, 0, 2);
  181. v = malloc(size+1);
  182. if(v == 0){
  183. fprint(2, "%s: can't malloc: %r\n", argv0);
  184. exits("no mem");
  185. }
  186. seek(fd, 0, 0);
  187. read(fd, v, size);
  188. v[size] = 0;
  189. close(fd);
  190. return v;
  191. }
  192. int
  193. newwin(char *win)
  194. {
  195. char *srv, *mntsrv;
  196. char spec[100];
  197. int srvfd, cons, pid;
  198. switch(rfork(RFFDG|RFPROC|RFNAMEG|RFENVG|RFNOTEG|RFNOWAIT)){
  199. case -1:
  200. fprint(2, "bargraph: can't fork: %r\n");
  201. return -1;
  202. case 0:
  203. break;
  204. default:
  205. exits(0);
  206. }
  207. srv = rdenv("/env/wsys");
  208. if(srv == 0){
  209. mntsrv = rdenv("/mnt/term/env/wsys");
  210. if(mntsrv == 0){
  211. fprint(2, "bargraph: can't find $wsys\n");
  212. return -1;
  213. }
  214. srv = malloc(strlen(mntsrv)+10);
  215. sprint(srv, "/mnt/term%s", mntsrv);
  216. free(mntsrv);
  217. pid = 0; /* can't send notes to remote processes! */
  218. }else
  219. pid = getpid();
  220. USED(pid);
  221. srvfd = open(srv, ORDWR);
  222. free(srv);
  223. if(srvfd == -1){
  224. fprint(2, "bargraph: can't open %s: %r\n", srv);
  225. return -1;
  226. }
  227. sprint(spec, "new -r %s", win);
  228. if(mount(srvfd, -1, "/mnt/wsys", 0, spec) == -1){
  229. fprint(2, "bargraph: can't mount /mnt/wsys: %r (spec=%s)\n", spec);
  230. return -1;
  231. }
  232. close(srvfd);
  233. unmount("/mnt/acme", "/dev");
  234. bind("/mnt/wsys", "/dev", MBEFORE);
  235. cons = open("/dev/cons", OREAD);
  236. if(cons==-1){
  237. NoCons:
  238. fprint(2, "bargraph: can't open /dev/cons: %r");
  239. return -1;
  240. }
  241. dup(cons, 0);
  242. close(cons);
  243. cons = open("/dev/cons", OWRITE);
  244. if(cons==-1)
  245. goto NoCons;
  246. dup(cons, 1);
  247. dup(cons, 2);
  248. close(cons);
  249. // wctlfd = open("/dev/wctl", OWRITE);
  250. return 0;
  251. }
  252. Rectangle
  253. screenrect(void)
  254. {
  255. int fd;
  256. char buf[12*5];
  257. fd = open("/dev/screen", OREAD);
  258. if(fd == -1)
  259. fd=open("/mnt/term/dev/screen", OREAD);
  260. if(fd == -1){
  261. fprint(2, "%s: can't open /dev/screen: %r\n", argv0);
  262. exits("window read");
  263. }
  264. if(read(fd, buf, sizeof buf) != sizeof buf){
  265. fprint(2, "%s: can't read /dev/screen: %r\n", argv0);
  266. exits("screen read");
  267. }
  268. close(fd);
  269. return Rect(atoi(buf+12), atoi(buf+24), atoi(buf+36), atoi(buf+48));
  270. }
  271. int
  272. postnote(int group, int pid, char *note)
  273. {
  274. char file[128];
  275. int f, r;
  276. switch(group) {
  277. case PNPROC:
  278. sprint(file, "/proc/%d/note", pid);
  279. break;
  280. case PNGROUP:
  281. sprint(file, "/proc/%d/notepg", pid);
  282. break;
  283. case PNCTL:
  284. sprint(file, "/proc/%d/ctl", pid);
  285. break;
  286. default:
  287. return -1;
  288. }
  289. f = open(file, OWRITE);
  290. if(f < 0)
  291. return -1;
  292. r = strlen(note);
  293. if(write(f, note, r) != r) {
  294. close(f);
  295. return -1;
  296. }
  297. close(f);
  298. return 0;
  299. }