15 * WASHINGTON (AP) - The Food and Drug Administration warned
16 * consumers Wednesday not to use ``Rio'' hair relaxer products
17 * because they may cause severe hair loss or turn hair green....
18 * The FDA urged consumers who have experienced problems with Rio
19 * to notify their local FDA office, local health department or the
20 * company at 1‑800‑543‑3002.
30 Image* bandsize(Window*);
33 Channel *exitchan; /* chan(int) */
34 Channel *winclosechan; /* chan(Window*); */
35 Channel *kbdchan; /* chan(char*); */
37 int threadrforkflag = 0; /* should be RFENVG but that hides rio from plumber */
39 void mousethread(void*);
40 void keyboardthread(void*);
41 void winclosethread(void*);
43 Channel* initkbd(void);
86 char *menu3str[100] = {
101 char *rcargv[] = { "rc", "-i", nil };
102 char *kbdargv[] = { "rc", "-c", nil, nil };
104 int errorshouldabort = 0;
107 derror(Display*, char *errorstr)
115 fprint(2, "usage: rio [-b] [-f font] [-i initcmd] [-k kbdcmd] [-s]\n");
120 threadmain(int argc, char *argv[])
122 char *initstr, *kbdin, *s;
127 if(strstr(argv[0], ".out") == nil){
128 menu3str[Exit] = nil;
139 fontname = EARGF(usage());
142 initstr = EARGF(usage());
147 kbdin = EARGF(usage());
159 if(getwd(buf, sizeof buf) == nil)
160 startdir = estrdup(".");
162 startdir = estrdup(buf);
164 fontname = getenv("font");
165 s = getenv("tabstop");
167 maxtab = strtol(s, nil, 0);
173 /* check font before barging ahead */
174 if(access(fontname, 0) < 0){
175 fprint(2, "rio: can't access %s: %r\n", fontname);
178 putenv("font", fontname);
181 snarffd = open("/dev/snarf", OREAD|OCEXEC);
182 gotscreen = access("/dev/screen", AEXIST)==0;
184 if(geninitdraw(nil, derror, nil, "rio", nil, Refnone) < 0){
185 fprint(2, "rio: can't open display: %r\n");
186 exits("display open");
190 exitchan = chancreate(sizeof(int), 0);
191 winclosechan = chancreate(sizeof(Window*), 0);
195 mousectl = initmouse(nil, screen);
197 error("can't find mouse");
201 error("can't find keyboard");
202 wscreen = allocscreen(screen, background, 0);
204 error("can't allocate screen");
205 draw(view, viewr, background, nil, ZP);
206 flushimage(display, 1);
209 threadcreate(keyboardthread, nil, STACK);
210 threadcreate(mousethread, nil, STACK);
211 threadcreate(winclosethread, nil, STACK);
212 filsys = filsysinit(xfidinit());
215 fprint(2, "rio: can't create file system server: %r\n");
217 errorshouldabort = 1; /* suicide if there's trouble after this */
219 proccreate(initcmd, initstr, STACK);
223 r.min.y = r.max.y-Dy(r)/3;
224 i = allocwindow(wscreen, r, Refbackup, DNofill);
225 wkeyboard = new(i, FALSE, scrolling, 0, nil, "/bin/rc", kbdargv);
227 error("can't create keyboard window");
229 threadnotify(shutdown, 1);
237 * /dev/snarf updates when the file is closed, so we must open our own
238 * fd here rather than use snarffd
245 if(snarffd<0 || nsnarf==0)
247 fd = open("/dev/snarf", OWRITE|OCEXEC);
250 /* snarf buffer could be huge, so fprint will truncate; do it in blocks */
251 for(i=0; i<nsnarf; i+=n){
255 if(fprint(fd, "%.*S", n, snarf+i) < 0)
275 if((s = realloc(sn, i+1024+1)) == nil)
278 if((n = read(snarffd, sn+i, 1024)) <= 0)
285 if((snarf = runerealloc(snarf, i+1)) != nil)
286 cvttorunes(sn, i, snarf, &nb, &nsnarf, &nulls);
296 rfork(RFENVG|RFFDG|RFNOTEG|RFNAMEG);
297 procexecl(nil, "/bin/rc", "rc", "-c", cmd, nil);
298 fprint(2, "rio: exec failed: %r\n");
312 shutdown(void *, char *msg)
315 static Lock shutdownlk;
318 for(i=0; oknotes[i]; i++)
319 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0){
320 lock(&shutdownlk); /* only one can threadexitsall */
323 fprint(2, "rio %d: abort: %s\n", getpid(), msg);
334 for(i=0; i<nwindow; i++)
335 if(window[i]->notefd >= 0)
336 write(window[i]->notefd, "hangup", 6);
340 keyboardthread(void*)
344 threadsetname("keyboardthread");
346 while(s = recvp(kbdchan)){
347 if(*s == 'k' || *s == 'K')
348 shiftdown = utfrune(s+1, Kshift) != nil;
349 if(input == nil || sendp(input->ck, s) <= 0)
355 inborder(Rectangle r, Point xy)
357 return ptinrect(xy, r) && !ptinrect(xy, insetrect(r, Selborder));
361 whichrect(Rectangle r, Point p, int which)
364 case 0: /* top left */
365 r = Rect(p.x, p.y, r.max.x, r.max.y);
367 case 2: /* top right */
368 r = Rect(r.min.x, p.y, p.x+1, r.max.y);
370 case 6: /* bottom left */
371 r = Rect(p.x, r.min.y, r.max.x, p.y+1);
373 case 8: /* bottom right */
374 r = Rect(r.min.x, r.min.y, p.x+1, p.y+1);
376 case 1: /* top edge */
377 r = Rect(r.min.x, p.y, r.max.x, r.max.y);
379 case 5: /* right edge */
380 r = Rect(r.min.x, r.min.y, p.x+1, r.max.y);
382 case 7: /* bottom edge */
383 r = Rect(r.min.x, r.min.y, r.max.x, p.y+1);
385 case 3: /* left edge */
386 r = Rect(p.x, r.min.y, r.max.x, r.max.y);
393 portion(int x, int lo, int hi)
408 whichcorner(Rectangle r, Point p)
412 i = portion(p.x, r.min.x, r.max.x);
413 j = portion(p.y, r.min.y, r.max.y);
417 /* thread to allow fsysproc to synchronize window closing with main proc */
419 winclosethread(void*)
423 threadsetname("winclosethread");
425 w = recvp(winclosechan);
431 * Button 6 - keyboard toggle - has been pressed.
432 * Send event to keyboard, wait for button up, send that.
433 * Note: there is no coordinate translation done here; this
434 * is just about getting button 6 to the keyboard simulator.
439 send(wkeyboard->mc.c, mouse);
442 while(mouse->buttons & (1<<5));
443 send(wkeyboard->mc.c, mouse);
449 int sending, inside, scrolling, moving;
459 static Alt alts[NALT+1];
461 threadsetname("mousethread");
465 alts[MReshape].c = mousectl->resizec;
466 alts[MReshape].v = nil;
467 alts[MReshape].op = CHANRCV;
468 alts[MMouse].c = mousectl->c;
469 alts[MMouse].v = &mousectl->Mouse;
470 alts[MMouse].op = CHANRCV;
471 alts[NALT].op = CHANEND;
479 if(wkeyboard!=nil && (mouse->buttons & (1<<5))){
486 /* override everything for the keyboard window */
487 if(wkeyboard!=nil && ptinrect(mouse->xy, wkeyboard->screenr)){
488 /* make sure it's on top; this call is free if it is */
492 if(winput!=nil && !winput->deleted && winput->i!=nil){
493 /* convert to logical coordinates */
494 xy.x = mouse->xy.x + (winput->i->r.min.x-winput->screenr.min.x);
495 xy.y = mouse->xy.y + (winput->i->r.min.y-winput->screenr.min.y);
497 /* the up and down scroll buttons are not subject to the usual rules */
498 if((mouse->buttons&(8|16)) && !winput->mouseopen)
501 inside = ptinrect(mouse->xy, insetrect(winput->screenr, Selborder));
502 if(winput->mouseopen)
505 scrolling = mouse->buttons;
507 scrolling = mouse->buttons && ptinrect(xy, winput->scrollr);
508 /* topped will be zero or less if window has been bottomed */
509 if(sending == FALSE && !scrolling && inborder(winput->screenr, mouse->xy) && winput->topped>0)
511 else if(inside && (scrolling || winput->mouseopen || (mouse->buttons&1)))
517 wsetcursor(winput, FALSE);
518 if(mouse->buttons == 0)
520 tmp = mousectl->Mouse;
522 send(winput->mc.c, &tmp);
525 if(moving && (mouse->buttons&7)){
528 if(mouse->buttons & 3)
529 i = bandsize(winput);
535 wsendctlmesg(winput, Reshaped, i->r, i);
540 w = wpointto(mouse->xy);
541 if(w!=nil && inborder(w->screenr, mouse->xy))
542 riosetcursor(corners[whichcorner(w->screenr, mouse->xy)]);
544 wsetcursor(w, FALSE);
545 /* we're not sending the event, but if button is down maybe we should */
547 /* w->topped will be zero or less if window has been bottomed */
548 if(w==nil || (w==winput && w->topped>0)){
549 if(mouse->buttons & 1){
551 }else if(mouse->buttons & 2){
552 if(winput && !winput->deleted && !winput->mouseopen){
557 }else if(mouse->buttons & 4)
560 /* if button 1 event in the window, top the window and wait for button up. */
561 /* otherwise, top the window and pass the event on */
562 if(wtop(mouse->xy) && (mouse->buttons!=1 || inborder(w->screenr, mouse->xy)))
572 while(mousectl->buttons);
573 goto Again; /* recalculate mouse position, cursor */
578 wtopcmp(void *a, void *b)
580 return (*(Window**)a)->topped - (*(Window**)b)->topped;
592 if(getwindow(display, Refnone) < 0)
593 error("failed to re-attach window");
597 wscreen = allocscreen(screen, background, 0);
599 error("can't re-allocate screen");
600 draw(view, view->r, background, nil, ZP);
601 o = subpt(viewr.max, viewr.min);
602 n = subpt(view->clipr.max, view->clipr.min);
603 qsort(window, nwindow, sizeof(window[0]), wtopcmp);
604 for(i=0; i<nwindow; i++){
606 r = rectsubpt(w->i->r, viewr.min);
607 r.min.x = (r.min.x*n.x)/o.x;
608 r.min.y = (r.min.y*n.y)/o.y;
609 r.max.x = (r.max.x*n.x)/o.x;
610 r.max.y = (r.max.y*n.y)/o.y;
611 r = rectaddpt(r, view->clipr.min);
613 r = rectsubpt(w->i->r, subpt(w->i->r.min, r.min));
614 for(j=0; j<nhidden; j++)
619 im = allocimage(display, r, screen->chan, 0, DNofill);
622 im = allocwindow(wscreen, r, Refbackup, DNofill);
625 wsendctlmesg(w, Reshaped, r, im);
629 flushimage(display, 1);
633 obscured(Window *w, Rectangle r, int i)
637 if(Dx(r) < font->height || Dy(r) < font->height)
639 if(!rectclip(&r, screen->r))
641 for(; i<nwindow; i++){
643 if(t == w || t->topped <= w->topped)
645 if(Dx(t->screenr) == 0 || Dy(t->screenr) == 0 || rectXrect(r, t->screenr) == 0)
647 if(r.min.y < t->screenr.min.y)
648 if(!obscured(w, Rect(r.min.x, r.min.y, r.max.x, t->screenr.min.y), i))
650 if(r.min.x < t->screenr.min.x)
651 if(!obscured(w, Rect(r.min.x, r.min.y, t->screenr.min.x, r.max.y), i))
653 if(r.max.y > t->screenr.max.y)
654 if(!obscured(w, Rect(r.min.x, t->screenr.max.y, r.max.x, r.max.y), i))
656 if(r.max.x > t->screenr.max.x)
657 if(!obscured(w, Rect(t->screenr.max.x, r.min.y, r.max.x, r.max.y), i))
668 static char buf[NBUF*UTFmax];
677 k += chartorune(&r, s+k);
681 strcpy(buf+k, "...");
682 while((l-i) >= NBUF/2-4){
683 k += chartorune(&r, s+k);
696 for(i=0; i<nwindow; i++){
698 if(window[i] == hidden[j])
701 if(obscured(window[i], window[i]->screenr, 0)){
702 hidden[n++] = window[i];
703 if(n >= nelem(hidden))
707 if(n >= nelem(menu3str)-Hidden)
708 n = nelem(menu3str)-Hidden-1;
710 free(menu3str[i+Hidden]);
711 menu3str[i+Hidden] = shortlabel(hidden[i]->label);
713 for(i+=Hidden; menu3str[i]; i++){
718 switch(i = menuhit(3, mousectl, &menu3, wscreen)){
722 new(sweep(), FALSE, scrolling, 0, nil, "/bin/rc", nil);
741 /* else fall through */
750 button2menu(Window *w)
753 menu2str[Scroll] = "noscroll";
755 menu2str[Scroll] = "scroll";
756 switch(menuhit(2, mousectl, &menu2, wscreen)){
786 if(w->scrolling ^= 1)
790 flushimage(display, 1);
791 wsendctlmesg(w, Wakeup, ZR, nil);
797 p.x = max(screen->clipr.min.x, p.x);
798 p.x = min(screen->clipr.max.x-1, p.x);
799 p.y = max(screen->clipr.min.y, p.y);
800 p.y = min(screen->clipr.max.y-1, p.y);
813 riosetcursor(&crosscursor);
814 while(mouse->buttons == 0)
816 p0 = onscreen(mouse->xy);
821 while(mouse->buttons == 4){
822 if(!eqpt(mouse->xy, p)){
823 p = onscreen(mouse->xy);
824 r = canonrect(Rpt(p0, p));
825 r = whichrect(r, p, whichcorner(r, p));
826 if(Dx(r)>5 && Dy(r)>5){
827 i = allocwindow(wscreen, r, Refnone, DNofill);
832 border(i, r, Selborder, sizecol, ZP);
833 draw(i, insetrect(r, Selborder), cols[BACK], nil, ZP);
838 if(mouse->buttons != 0)
840 if(i==nil || !goodrect(r))
843 i = allocwindow(wscreen, oi->r, Refbackup, DNofill);
847 riosetcursor(corners[whichcorner(i->r, mouse->xy)]);
854 flushimage(display, 1);
855 while(mouse->buttons)
864 drawedge(Image **bp, Image *col, Rectangle r)
867 if(b != nil && Dx(b->r) == Dx(r) && Dy(b->r) == Dy(r))
868 originwindow(b, r.min, r.min);
871 b = allocwindow(wscreen, r, Refbackup, DNofill);
872 if(b != nil) draw(b, r, col, nil, ZP);
878 drawborder(Rectangle r, Image *col)
880 static Image *b[4], *lastcol;
883 freeimage(b[0]), b[0] = nil;
884 freeimage(b[1]), b[1] = nil;
885 freeimage(b[2]), b[2] = nil;
886 freeimage(b[3]), b[3] = nil;
890 drawedge(&b[0], col, Rect(r.min.x, r.min.y, r.min.x+Borderwidth, r.max.y));
891 drawedge(&b[1], col, Rect(r.min.x+Borderwidth, r.min.y, r.max.x-Borderwidth, r.min.y+Borderwidth));
892 drawedge(&b[2], col, Rect(r.max.x-Borderwidth, r.min.y, r.max.x, r.max.y));
893 drawedge(&b[3], col, Rect(r.min.x+Borderwidth, r.max.y-Borderwidth, r.max.x-Borderwidth, r.max.y));
901 Point p, op, d, dm, om;
905 riosetcursor(&boxcursor);
907 dm = subpt(om, w->screenr.min);
908 d = subpt(w->screenr.max, w->screenr.min);
910 drawborder(Rect(op.x, op.y, op.x+d.x, op.y+d.y), sizecol);
911 while(mouse->buttons==4){
912 p = subpt(mouse->xy, dm);
914 drawborder(Rect(p.x, p.y, p.x+d.x, p.y+d.y), sizecol);
919 r = Rect(op.x, op.y, op.x+d.x, op.y+d.y);
922 riosetcursor(inborder(r, p) ? corners[whichcorner(r, p)] : nil);
924 if(mouse->buttons!=0 || !goodrect(r) || eqrect(r, w->screenr)){
925 flushimage(display, 1);
926 while(mouse->buttons)
930 return allocwindow(wscreen, r, Refbackup, DNofill);
938 int which, owhich, but;
942 but = mouse->buttons;
943 startp = onscreen(mouse->xy);
944 drawborder(or, sizecol);
945 while(mouse->buttons == but) {
946 p = onscreen(mouse->xy);
947 which = whichcorner(or, p);
948 if(which != owhich && which != 4 && (owhich|~which) & 1){
950 riosetcursor(corners[which]);
952 r = whichrect(or, p, owhich);
953 if(!eqrect(r, or) && goodrect(r)){
954 drawborder(r, sizecol);
962 if(mouse->buttons!=0 || !goodrect(or) || eqrect(or, w->screenr)
963 || abs(p.x-startp.x)+abs(p.y-startp.y) <= 1){
964 flushimage(display, 1);
965 while(mouse->buttons)
969 return allocwindow(wscreen, or, Refbackup, DNofill);
978 riosetcursor(&sightcursor);
979 while(mouse->buttons == 0)
981 if(mouse->buttons == 4)
982 w = wpointto(mouse->xy);
986 while(mouse->buttons){
987 if(mouse->buttons!=4 && w !=nil){ /* cancel */
993 if(w != nil && wpointto(mouse->xy) != w)
1008 wsendctlmesg(w, Deleted, ZR, nil);
1024 wsendctlmesg(w, Reshaped, i->r, i);
1042 wsendctlmesg(w, Reshaped, i->r, i);
1053 for(j=0; j<nhidden; j++)
1054 if(hidden[j] == w) /* already hidden */
1056 if(nhidden >= nelem(hidden))
1060 i = allocimage(display, w->screenr, w->i->chan, 0, DNofill);
1062 hidden[nhidden++] = w;
1063 wsendctlmesg(w, Reshaped, ZR, i);
1075 for(j=0; j<nhidden; j++)
1079 return -1; /* not hidden */
1082 i = allocwindow(wscreen, w->i->r, Refbackup, DNofill);
1085 memmove(hidden+j, hidden+j+1, (nhidden-j)*sizeof(Window*));
1086 wsendctlmesg(w, Reshaped, w->i->r, i);
1117 /* uncover obscured window */
1118 for(j=0; j<nwindow; j++)
1123 wsendctlmesg(w, Topped, ZR, nil);
1130 new(Image *i, int hideit, int scrollit, int pid, char *dir, char *cmd, char **argv)
1134 Channel *cm, *ck, *cctl, *cpid;
1139 if(hideit && nhidden >= nelem(hidden)){
1143 cm = chancreate(sizeof(Mouse), 0);
1144 ck = chancreate(sizeof(char*), 0);
1145 cctl = chancreate(sizeof(Wctlmesg), 4);
1146 cpid = chancreate(sizeof(int), 0);
1147 if(cm==nil || ck==nil || cctl==nil)
1148 error("new: channel alloc failed");
1149 mc = emalloc(sizeof(Mousectl));
1153 w = wmk(i, mc, ck, cctl, scrollit);
1154 free(mc); /* wmk copies *mc */
1155 window = erealloc(window, ++nwindow*sizeof(Window*));
1156 window[nwindow-1] = w;
1158 hidden[nhidden++] = w;
1161 threadcreate(winctl, w, STACK);
1165 arg = emalloc(5*sizeof(void*));
1174 proccreate(winshell, arg, STACK);
1179 /* window creation failed */
1180 wsendctlmesg(w, Deleted, ZR, nil);
1188 w->dir = estrdup(dir);
1198 char buf[1024], *p, *e;
1199 int fd, cfd, kfd, n;
1201 threadsetname("kbdproc");
1203 if((fd = open("/dev/cons", OREAD)) < 0){
1207 if((cfd = open("/dev/consctl", OWRITE)) < 0){
1211 fprint(cfd, "rawon");
1213 if(sendp(c, nil) <= 0)
1216 if((kfd = open("/dev/kbd", OREAD)) >= 0){
1219 /* only serve a kbd file per window when we got one */
1222 /* read kbd state */
1223 while((n = read(kfd, buf, sizeof(buf)-1)) > 0){
1227 for(p = buf; p < e; p += strlen(p)+1)
1228 chanprint(c, "%s", p);
1231 /* read single characters */
1236 e = buf + sizeof(buf);
1237 if((n = read(fd, p, e-p)) <= 0)
1240 while(p < e && fullrune(p, e - p)){
1241 p += chartorune(&r, p);
1243 chanprint(c, "c%C", r);
1250 send(exitchan, nil);
1259 c = chancreate(sizeof(char*), 16);
1260 procrfork(kbdproc, c, STACK, RFCFDG);