#include #include #include #include #include #include #include Mousectl *mousectl; Keyboardctl *keyboardctl; Image *bg; Image *black; Point p1, p2; ulong colors[] = { 0x000000FF, 0xFFFFFFFF, 0xFF0000FF, 0x00FF00FF, 0x0000FFFF, 0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFAAFF, 0xEEEE9EFF, 0x448844FF, 0xAAFFAAFF, 0x88CC88FF, 0x000055FF, 0xAAFFFFFF, 0x0000BBFF, 0x008888FF, 0x55AAAAFF, 0x9EEEEEFF, 0x99994CFF, 0x000099FF, 0x005DBBFF, 0x4993DDFF, 0x8888CCFF, }; void redraw(void); ulong randomcol(void) { return colors[nrand(nelem(colors)-1)]; } void mouseproc(void *) { Mouse *m; Point p; enum {Amouse, Aresize, Aend}; Alt alts[] = { [Amouse]{mousectl->c, &mousectl->Mouse, CHANRCV}, [Aresize]{mousectl->resizec, nil, CHANRCV}, [Aend]{nil, nil, CHANEND}, }; m = &mousectl->Mouse; for(;;){ switch(alt(alts)){ case Aresize: if(getwindow(display, Refnone) < 0) sysfatal("getwindow: %r"); redraw(); break; case Amouse: switch(m->buttons){ case 1: p1 = m->xy; break; case 2: break; case 4: p2 = m->xy; break; case 0: break; } if(m->buttons) redraw(); break; } } } void keyboardproc(void *) { Rune r; int n; char buf[128] = {0}; for(;;){ recv(keyboardctl->c, &r); switch(r){ case 'q': threadexitsall(nil); break; case 'r': freeimage(bg); bg = allocimage(display, Rect(0,0,1,1), RGB24, 1, randomcol()); redraw(); break; case 'm': if((n = enter("prompt:", buf, sizeof(buf)-1, mousectl, keyboardctl, _screen)) < 0) sysfatal("enter: %r"); buf[n] = 0; print("%s\n", buf); redraw(); break; default: print("%C", r); break; } } } void redraw(void) { Point c, tl, tr, bl, br; Point t1, t2; c = addpt(screen->r.min, Pt(Dx(screen->r)/2, Dy(screen->r)/2)); tl = screen->r.min; br = screen->r.max; tr = Pt(br.x, tl.y); bl = Pt(tl.x, br.y); t1 = p1; t2 = p2; draw(screen, screen->r, bg, nil, ZP); line(screen, p1, p2, Endarrow, Endarrow, 0, black, ZP); for(int i = 3; i < 20; i++){ bezier(screen, tr, t1, t2, br, 0, 0, 0, black, ZP); bezier(screen, tl, t1, t2, bl, 0, 0, 0, black, ZP); t1 = subpt(t1, Pt(0, i*i)); } flushimage(display, 1); } void threadmain(int argc, char *argv[]) { ARGBEGIN{ }ARGEND; srand(time(0)); if(newwindow(nil) < 0) sysfatal("newwindow: %r"); if(initdraw(nil, nil, "t") < 0) sysfatal("initdraw: %r"); if((mousectl = initmouse(nil, screen)) == nil) sysfatal("initmouse: %r"); if((keyboardctl = initkeyboard(nil)) == nil) sysfatal("initkeyboard: %r"); black = allocimage(display, Rect(0,0,1,1), RGBA32, 1, 0x000000BF); bg = allocimage(display, Rect(0,0,1,1), RGB24, 1, randomcol()); p1 = Pt(screen->r.max.x - 50, screen->r.min.y + 50); p2 = Pt(screen->r.max.x - 10, screen->r.min.y + 10); redraw(); proccreate(mouseproc, nil, 8192); proccreate(keyboardproc, nil, 8192); threadexits(nil); }