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*);
31 Image* drag(Window*, Rectangle*);
32 void refresh(Rectangle);
34 Channel *exitchan; /* chan(int) */
35 Channel *winclosechan; /* chan(Window*); */
36 Channel *kbdchan; /* chan(char*); */
38 int threadrforkflag = 0; /* should be RFENVG but that hides rio from plumber */
40 void mousethread(void*);
41 void keyboardthread(void*);
42 void winclosethread(void*);
43 void deletethread(void*);
45 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;
128 if(strstr(argv[0], ".out") == nil){
129 menu3str[Exit] = nil;
161 if(getwd(buf, sizeof buf) == nil)
162 startdir = estrdup(".");
164 startdir = estrdup(buf);
166 fontname = getenv("font");
167 s = getenv("tabstop");
169 maxtab = strtol(s, nil, 0);
175 /* check font before barging ahead */
176 if(access(fontname, 0) < 0){
177 fprint(2, "rio: can't access %s: %r\n", fontname);
180 putenv("font", fontname);
183 snarffd = open("/dev/snarf", OREAD|OCEXEC);
184 gotscreen = access("/dev/screen", AEXIST)==0;
186 if(geninitdraw(nil, derror, nil, "rio", nil, Refnone) < 0){
187 fprint(2, "rio: can't open display: %r\n");
188 exits("display open");
193 mousectl = initmouse(nil, screen);
195 error("can't find mouse");
199 error("can't find keyboard");
200 wscreen = allocscreen(screen, background, 0);
202 error("can't allocate screen");
203 draw(view, viewr, background, nil, ZP);
204 flushimage(display, 1);
206 exitchan = chancreate(sizeof(int), 0);
207 winclosechan = chancreate(sizeof(Window*), 0);
208 deletechan = chancreate(sizeof(char*), 0);
211 threadcreate(keyboardthread, nil, STACK);
212 threadcreate(mousethread, nil, STACK);
213 threadcreate(winclosethread, nil, STACK);
214 threadcreate(deletethread, nil, STACK);
215 filsys = filsysinit(xfidinit());
218 fprint(2, "rio: can't create file system server: %r\n");
220 errorshouldabort = 1; /* suicide if there's trouble after this */
222 proccreate(initcmd, initstr, STACK);
226 r.max.x = r.min.x+300;
227 r.max.y = r.min.y+80;
228 i = allocwindow(wscreen, r, Refbackup, DNofill);
229 wkeyboard = new(i, FALSE, scrolling, 0, nil, "/bin/rc", kbdargv);
231 error("can't create keyboard window");
233 threadnotify(shutdown, 1);
241 * /dev/snarf updates when the file is closed, so we must open our own
242 * fd here rather than use snarffd
249 if(snarffd<0 || nsnarf==0)
251 fd = open("/dev/snarf", OWRITE);
254 /* snarf buffer could be huge, so fprint will truncate; do it in blocks */
255 for(i=0; i<nsnarf; i+=n){
259 if(fprint(fd, "%.*S", n, snarf+i) < 0)
276 while((n = read(snarffd, buf, sizeof buf)) > 0){
277 sn = erealloc(sn, i+n+1);
278 memmove(sn+i, buf, n);
283 snarf = runerealloc(snarf, i+1);
284 cvttorunes(sn, i, snarf, &nb, &nsnarf, &nulls);
295 rfork(RFENVG|RFFDG|RFNOTEG|RFNAMEG);
296 procexecl(nil, "/bin/rc", "rc", "-c", cmd, nil);
297 fprint(2, "rio: exec failed: %r\n");
311 shutdown(void *, char *msg)
314 static Lock shutdownlk;
317 for(i=0; oknotes[i]; i++)
318 if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0){
319 lock(&shutdownlk); /* only one can threadexitsall */
322 fprint(2, "rio %d: abort: %s\n", getpid(), msg);
333 for(i=0; i<nwindow; i++)
334 if(window[i]->notefd >= 0)
335 write(window[i]->notefd, "hangup", 6);
339 keyboardthread(void*)
343 threadsetname("keyboardthread");
345 while(s = recvp(kbdchan)){
346 if(*s == 'k' || *s == 'K')
347 shiftdown = utfrune(s+1, Kshift) != nil;
348 if(input == nil || sendp(input->ck, s) <= 0)
357 keyboardsend(char *s, int cnt)
362 chanprint(kbdchan, "%s", s);
368 cvttorunes(s, cnt, r, &nb, &nr, nil);
371 chanprint(kbdchan, "c%C", r[i]);
378 portion(int x, int lo, int hi)
390 whichcorner(Window *w, Point p)
394 i = portion(p.x, w->screenr.min.x, w->screenr.max.x);
395 j = portion(p.y, w->screenr.min.y, w->screenr.max.y);
400 cornercursor(Window *w, Point p, int force)
402 if(w!=nil && winborder(w, p))
403 riosetcursor(corners[whichcorner(w, p)], force);
405 wsetcursor(w, force);
408 /* thread to allow fsysproc to synchronize window closing with main proc */
410 winclosethread(void*)
414 threadsetname("winclosethread");
416 w = recvp(winclosechan);
421 /* thread to make Deleted windows that the client still holds disappear offscreen after an interval */
428 threadsetname("deletethread");
430 s = recvp(deletechan);
431 i = namedimage(display, s);
433 /* move it off-screen to hide it, since client is slow in letting it go */
434 originwindow(i, i->r.min, view->r.max);
442 deletetimeoutproc(void *v)
447 sleep(750); /* remove window from screen after 3/4 of a second */
448 sendp(deletechan, s);
452 * Button 6 - keyboard toggle - has been pressed.
453 * Send event to keyboard, wait for button up, send that.
454 * Note: there is no coordinate translation done here; this
455 * is just about getting button 6 to the keyboard simulator.
460 send(wkeyboard->mc.c, mouse);
463 while(mouse->buttons & (1<<5));
464 send(wkeyboard->mc.c, mouse);
470 int sending, inside, scrolling, moving, band;
481 static Alt alts[NALT+1];
483 threadsetname("mousethread");
488 alts[MReshape].c = mousectl->resizec;
489 alts[MReshape].v = nil;
490 alts[MReshape].op = CHANRCV;
491 alts[MMouse].c = mousectl->c;
492 alts[MMouse].v = &mousectl->Mouse;
493 alts[MMouse].op = CHANRCV;
494 alts[NALT].op = CHANEND;
502 if(wkeyboard!=nil && (mouse->buttons & (1<<5))){
508 /* override everything for the keyboard window */
509 if(wkeyboard!=nil && ptinrect(mouse->xy, wkeyboard->screenr)){
510 /* make sure it's on top; this call is free if it is */
514 if(winput!=nil && !winput->deleted && winput->i!=nil){
515 /* convert to logical coordinates */
516 xy.x = mouse->xy.x + (winput->i->r.min.x-winput->screenr.min.x);
517 xy.y = mouse->xy.y + (winput->i->r.min.y-winput->screenr.min.y);
519 /* the up and down scroll buttons are not subject to the usual rules */
520 if((mouse->buttons&(8|16)) && !winput->mouseopen)
523 inside = ptinrect(mouse->xy, insetrect(winput->screenr, Selborder));
524 if(winput->mouseopen)
527 scrolling = mouse->buttons;
529 scrolling = mouse->buttons && ptinrect(xy, winput->scrollr);
530 /* topped will be zero or less if window has been bottomed */
531 if(sending == FALSE && !scrolling && winborder(winput, mouse->xy) && winput->topped>0){
533 }else if(inside && (scrolling || winput->mouseopen || (mouse->buttons&1)))
539 if(mouse->buttons == 0){
540 cornercursor(winput, mouse->xy, 0);
543 wsetcursor(winput, 0);
544 tmp = mousectl->Mouse;
546 send(winput->mc.c, &tmp);
549 w = wpointto(mouse->xy);
550 /* change cursor if over anyone's border */
552 cornercursor(w, mouse->xy, 0);
554 riosetcursor(nil, 0);
555 if(moving && (mouse->buttons&7)){
557 band = mouse->buttons & 3;
560 i = bandsize(winput);
562 i = drag(winput, &r);
566 wsendctlmesg(winput, Reshaped, i->r, i);
568 wsendctlmesg(winput, Moved, r, i);
569 cornercursor(winput, mouse->xy, 1);
571 if(wclose(winput) == 0)
574 riosetcursor(nil, 0);
578 /* we're not sending the event, but if button is down maybe we should */
580 /* w->topped will be zero or less if window has been bottomed */
581 if(w==nil || (w==winput && w->topped>0)){
582 if(mouse->buttons & 1){
584 }else if(mouse->buttons & 2){
585 if(winput && !winput->deleted && !winput->mouseopen){
590 }else if(mouse->buttons & 4)
593 /* if button 1 event in the window, top the window and wait for button up. */
594 /* otherwise, top the window and pass the event on */
595 if(wtop(mouse->xy) && (mouse->buttons!=1 || winborder(w, mouse->xy)))
606 while(mousectl->buttons);
608 goto Again; /* recalculate mouse position, cursor */
613 wtopcmp(void *a, void *b)
615 return (*(Window**)a)->topped - (*(Window**)b)->topped;
627 if(getwindow(display, Refnone) < 0)
628 error("failed to re-attach window");
632 wscreen = allocscreen(screen, background, 0);
634 error("can't re-allocate screen");
635 draw(view, view->r, background, nil, ZP);
636 o = subpt(viewr.max, viewr.min);
637 n = subpt(view->clipr.max, view->clipr.min);
638 qsort(window, nwindow, sizeof(window[0]), wtopcmp);
639 for(i=0; i<nwindow; i++){
641 r = rectsubpt(w->i->r, viewr.min);
642 r.min.x = (r.min.x*n.x)/o.x;
643 r.min.y = (r.min.y*n.y)/o.y;
644 r.max.x = (r.max.x*n.x)/o.x;
645 r.max.y = (r.max.y*n.y)/o.y;
647 r = rectsubpt(w->i->r, viewr.min);
648 r = rectaddpt(r, screen->clipr.min);
649 for(j=0; j<nhidden; j++)
654 im = allocimage(display, r, screen->chan, 0, DNofill);
657 im = allocwindow(wscreen, r, Refbackup, DNofill);
659 wsendctlmesg(w, Reshaped, r, im);
663 flushimage(display, 1);
667 obscured(Window *w, Rectangle r, int i)
671 if(Dx(r) < font->height || Dy(r) < font->height)
673 if(!rectclip(&r, screen->r))
675 for(; i<nwindow; i++){
677 if(t == w || t->topped <= w->topped)
679 if(Dx(t->screenr) == 0 || Dy(t->screenr) == 0 || rectXrect(r, t->screenr) == 0)
681 if(r.min.y < t->screenr.min.y)
682 if(!obscured(w, Rect(r.min.x, r.min.y, r.max.x, t->screenr.min.y), i))
684 if(r.min.x < t->screenr.min.x)
685 if(!obscured(w, Rect(r.min.x, r.min.y, t->screenr.min.x, r.max.y), i))
687 if(r.max.y > t->screenr.max.y)
688 if(!obscured(w, Rect(r.min.x, t->screenr.max.y, r.max.x, r.max.y), i))
690 if(r.max.x > t->screenr.max.x)
691 if(!obscured(w, Rect(t->screenr.max.x, r.min.y, r.max.x, r.max.y), i))
704 for(i=0; i<nwindow; i++){
706 if(window[i] == hidden[j])
709 if(obscured(window[i], window[i]->screenr, 0)){
710 hidden[n++] = window[i];
711 if(n >= nelem(hidden))
715 if(n >= nelem(menu3str)-Hidden)
716 n = nelem(menu3str)-Hidden-1;
718 free(menu3str[i+Hidden]);
719 menu3str[i+Hidden] = estrdup(hidden[i]->label);
721 for(i+=Hidden; menu3str[i]; i++){
726 switch(i = menuhit(3, mousectl, &menu3, wscreen)){
730 new(sweep(), FALSE, scrolling, 0, nil, "/bin/rc", nil);
749 /* else fall through */
758 button2menu(Window *w)
761 menu2str[Scroll] = "noscroll";
763 menu2str[Scroll] = "scroll";
764 switch(menuhit(2, mousectl, &menu2, wscreen)){
791 waddraw(w, snarf, nsnarf);
792 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
793 waddraw(w, L"\n", 1);
795 winsert(w, snarf, nsnarf, w->nr);
796 if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
797 winsert(w, L"\n", 1, w->nr);
799 wsetselect(w, w->nr, w->nr);
804 if(w->scrolling ^= 1)
808 wsendctlmesg(w, Wakeup, ZR, nil);
809 flushimage(display, 1);
815 p.x = max(screen->clipr.min.x, p.x);
816 p.x = min(screen->clipr.max.x, p.x);
817 p.y = max(screen->clipr.min.y, p.y);
818 p.y = min(screen->clipr.max.y, p.y);
831 riosetcursor(&crosscursor, 1);
832 while(mouse->buttons == 0)
834 p0 = onscreen(mouse->xy);
839 while(mouse->buttons == 4){
841 if(mouse->buttons != 4 && mouse->buttons != 0)
843 if(!eqpt(mouse->xy, p)){
844 p = onscreen(mouse->xy);
845 r = canonrect(Rpt(p0, p));
846 if(Dx(r)>5 && Dy(r)>5){
847 i = allocwindow(wscreen, r, Refnone, DNofill);
852 border(i, r, Selborder, sizecol, ZP);
853 draw(i, insetrect(r, Selborder), cols[BACK], nil, ZP);
854 flushimage(display, 1);
858 if(mouse->buttons != 0)
860 if(i==nil || Dx(i->r)<100 || Dy(i->r)<3*font->height)
863 i = allocwindow(wscreen, oi->r, Refbackup, DNofill);
867 cornercursor(input, mouse->xy, 1);
873 cornercursor(input, mouse->xy, 1);
874 while(mouse->buttons)
878 moveto(mousectl, mouse->xy); /* force cursor update; ugly */
884 drawedge(Image **bp, Rectangle r)
887 if(b != nil && Dx(b->r) == Dx(r) && Dy(b->r) == Dy(r))
888 originwindow(b, r.min, r.min);
891 b = allocwindow(wscreen, r, Refbackup, DNofill);
892 if(b != nil) draw(b, r, sizecol, nil, ZP);
898 drawborder(Rectangle r, int show)
903 for(i = 0; i < 4; i++){
909 drawedge(&b[0], Rect(r.min.x, r.min.y, r.min.x+Borderwidth, r.max.y));
910 drawedge(&b[1], Rect(r.min.x+Borderwidth, r.min.y, r.max.x-Borderwidth, r.min.y+Borderwidth));
911 drawedge(&b[2], Rect(r.max.x-Borderwidth, r.min.y, r.max.x, r.max.y));
912 drawedge(&b[3], Rect(r.min.x+Borderwidth, r.max.y-Borderwidth, r.max.x-Borderwidth, r.max.y));
917 drag(Window *w, Rectangle *rp)
920 Point p, op, d, dm, om;
926 riosetcursor(&boxcursor, 1);
927 dm = subpt(mouse->xy, w->screenr.min);
928 d = subpt(i->r.max, i->r.min);
929 op = subpt(mouse->xy, dm);
930 drawborder(Rect(op.x, op.y, op.x+d.x, op.y+d.y), 1);
931 flushimage(display, 1);
932 while(mouse->buttons == 4){
933 p = subpt(mouse->xy, dm);
935 drawborder(Rect(p.x, p.y, p.x+d.x, p.y+d.y), 1);
936 flushimage(display, 1);
941 r = Rect(op.x, op.y, op.x+d.x, op.y+d.y);
943 cornercursor(w, mouse->xy, 1);
944 moveto(mousectl, mouse->xy); /* force cursor update; ugly */
946 flushimage(display, 1);
947 if(mouse->buttons!=0 || (ni=allocwindow(wscreen, r, Refbackup, DNofill))==nil){
948 moveto(mousectl, om);
949 while(mouse->buttons)
951 *rp = Rect(0, 0, 0, 0);
954 draw(ni, ni->r, i, nil, i->r.min);
960 cornerpt(Rectangle r, Point p, int which)
963 case 0: /* top left */
964 p = Pt(r.min.x, r.min.y);
966 case 2: /* top right */
967 p = Pt(r.max.x,r.min.y);
969 case 6: /* bottom left */
970 p = Pt(r.min.x, r.max.y);
972 case 8: /* bottom right */
973 p = Pt(r.max.x, r.max.y);
975 case 1: /* top edge */
978 case 5: /* right edge */
979 p = Pt(r.max.x, p.y);
981 case 7: /* bottom edge */
982 p = Pt(p.x, r.max.y);
984 case 3: /* left edge */
985 p = Pt(r.min.x, p.y);
992 whichrect(Rectangle r, Point p, int which)
995 case 0: /* top left */
996 r = Rect(p.x, p.y, r.max.x, r.max.y);
998 case 2: /* top right */
999 r = Rect(r.min.x, p.y, p.x, r.max.y);
1001 case 6: /* bottom left */
1002 r = Rect(p.x, r.min.y, r.max.x, p.y);
1004 case 8: /* bottom right */
1005 r = Rect(r.min.x, r.min.y, p.x, p.y);
1007 case 1: /* top edge */
1008 r = Rect(r.min.x, p.y, r.max.x, r.max.y);
1010 case 5: /* right edge */
1011 r = Rect(r.min.x, r.min.y, p.x, r.max.y);
1013 case 7: /* bottom edge */
1014 r = Rect(r.min.x, r.min.y, r.max.x, p.y);
1016 case 3: /* left edge */
1017 r = Rect(p.x, r.min.y, r.max.x, r.max.y);
1020 return canonrect(r);
1031 but = mouse->buttons;
1032 which = whichcorner(w, p);
1033 p = cornerpt(w->screenr, p, which);
1035 readmouse(mousectl);
1036 r = whichrect(w->screenr, p, which);
1041 while(mouse->buttons == but){
1042 p = onscreen(mouse->xy);
1043 r = whichrect(w->screenr, p, which);
1044 if(!eqrect(r, or) && goodrect(r)){
1046 flushimage(display, 1);
1049 readmouse(mousectl);
1053 flushimage(display, 1);
1055 if(mouse->buttons!=0 || Dx(or)<100 || Dy(or)<3*font->height){
1056 while(mouse->buttons)
1057 readmouse(mousectl);
1060 if(abs(p.x-startp.x)+abs(p.y-startp.y) <= 1)
1062 return allocwindow(wscreen, or, Refbackup, DNofill);
1071 riosetcursor(&sightcursor, 1);
1072 while(mouse->buttons == 0)
1073 readmouse(mousectl);
1074 if(mouse->buttons == 4)
1075 w = wpointto(mouse->xy);
1079 while(mouse->buttons){
1080 if(mouse->buttons!=4 && w !=nil){ /* cancel */
1081 cornercursor(input, mouse->xy, 0);
1084 readmouse(mousectl);
1086 if(w != nil && wpointto(mouse->xy) != w)
1089 cornercursor(input, mouse->xy, 0);
1090 moveto(mousectl, mouse->xy); /* force cursor update; ugly */
1102 wsendctlmesg(w, Deleted, ZR, nil);
1117 wsendctlmesg(w, Reshaped, i->r, i);
1134 wsendctlmesg(w, Moved, r, i);
1135 cornercursor(w, mouse->xy, 1);
1145 for(j=0; j<nhidden; j++)
1146 if(hidden[j] == w) /* already hidden */
1148 if(nhidden >= nelem(hidden))
1151 i = allocimage(display, w->screenr, w->i->chan, 0, DNofill);
1153 hidden[nhidden++] = w;
1154 wsendctlmesg(w, Reshaped, ZR, i);
1166 for(j=0; j<nhidden; j++)
1170 return -1; /* not hidden */
1172 i = allocwindow(wscreen, w->i->r, Refbackup, DNofill);
1175 memmove(hidden+j, hidden+j+1, (nhidden-j)*sizeof(Window*));
1176 wsendctlmesg(w, Reshaped, w->i->r, i);
1207 /* uncover obscured window */
1208 for(j=0; j<nwindow; j++)
1219 new(Image *i, int hideit, int scrollit, int pid, char *dir, char *cmd, char **argv)
1223 Channel *cm, *ck, *cctl, *cpid;
1228 if(hideit && nhidden >= nelem(hidden)){
1232 cm = chancreate(sizeof(Mouse), 0);
1233 ck = chancreate(sizeof(char*), 0);
1234 cctl = chancreate(sizeof(Wctlmesg), 4);
1235 cpid = chancreate(sizeof(int), 0);
1236 if(cm==nil || ck==nil || cctl==nil)
1237 error("new: channel alloc failed");
1238 mc = emalloc(sizeof(Mousectl));
1242 w = wmk(i, mc, ck, cctl, scrollit);
1243 free(mc); /* wmk copies *mc */
1244 window = erealloc(window, ++nwindow*sizeof(Window*));
1245 window[nwindow-1] = w;
1247 hidden[nhidden++] = w;
1250 threadcreate(winctl, w, 8192);
1254 arg = emalloc(5*sizeof(void*));
1263 proccreate(winshell, arg, 8192);
1268 /* window creation failed */
1269 wsendctlmesg(w, Deleted, ZR, nil);
1277 w->dir = estrdup(dir);
1287 char buf[128], *p, *e;
1288 int fd, cfd, kfd, n;
1290 threadsetname("kbdproc");
1292 if((fd = open("/dev/cons", OREAD)) < 0){
1296 if((cfd = open("/dev/consctl", OWRITE)) < 0){
1300 fprint(cfd, "rawon");
1302 if(sendp(c, nil) <= 0)
1305 if((kfd = open("/dev/kbd", OREAD)) >= 0){
1308 /* read kbd state */
1309 while((n = read(kfd, buf, sizeof(buf))) > 0)
1310 chanprint(c, "%.*s", n, buf);
1313 /* read single characters */
1318 e = buf + sizeof(buf);
1319 if((n = read(fd, p, e-p)) <= 0)
1322 while(p < e && fullrune(p, e - p)){
1323 p += chartorune(&r, p);
1325 chanprint(c, "c%C", r);
1340 c = chancreate(sizeof(char*), 16);
1341 procrfork(kbdproc, c, STACK, RFCFDG);