bargraph.c 6.2 KB

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