Does it work in Plan9Port?

2012/12/19 erik quanstrom <quans...@quanstro.net>:
> i don't think there are any tricks.  here's a pretty minimal program
> that displays colors.  usage might be:
>
>         color 0x448844FF 0x000055FF
>
> - erik
>
> ----
> #include <u.h>
> #include <libc.h>
> #include <thread.h>
> #include <draw.h>
> #include <mouse.h>
> #include <keyboard.h>
>
> static  Mousectl        *m;
> static  long    colors[1024];
> static  Image   *c[1024];
> static  int     ncolor;
>
> int     rflag;
>
> void
> redraw(int)
> {
>         int i, x, y;
>         ulong chan;
>
>         chan = rflag ? CHAN3(CGreen, 8, CBlue, 8, CRed, 8) : RGB24;
>
>         if(c[0] == 0)
>                 for(i = 0; i < ncolor; i++){
>                         c[i] = allocimage(display, Rect(0, 0, 1, 1), chan, 1, 
> colors[i]);
>                         if(c[i] == 0)
>                                 sysfatal("allocimage: %r");
>                 }
>
>         x = screen->r.min.x;
>         y = screen->r.min.y;
>
>         for(i = 0; i < ncolor; i++){
>                 draw(screen, Rect(x, y, x+48, y+48), c[i], nil, ZP);
>                 x += 50;
>                 if(x > screen->r.max.x-60){
>                         x = screen->r.min.x;
>                         y += 50;
>                 }
>         }
> }
>
> void
> resizeproc(void *)
> {
>         for(; recv(m->resizec, 0);){
>                 if(getwindow(display, Refnone) < 0){
>                         fprint(2, "test: can't reattach to window\n");
>                         threadexitsall("resize");
>                 }
>                 redraw(1);
>                 flushimage(display, 1);
>         }
> }
>
> static void
> drawe(Display*, char *e)
> {
>         fprint(2, "%s\n", e);
>         threadexitsall("libdraw");
> }
>
> int
> init(void)
> {
> //      newwindow("-dx 300 -dy 300");
>         if(initdraw(drawe, 0, "test") < 0){
>                 fprint(2, "test: initdraw failed: %r");
>                 return -1;
>         }
>         m = initmouse(0, screen);
>         return 0;
> }
>
> void
> kbdproc(void*)
> {
>         Keyboardctl *k;
>
>         k = initkeyboard(0);
>         recv(k->c, 0);
>         threadexitsall("");
> }
>
> void
> mouseproc(void*)
> {
>         while(readmouse(m) >= 0)
>                 ;
>         threadexitsall("readmouse");
> }
>
> void
> usage(void)
> {
>         fprint(2, "usage: color [-r] color1 ...\n");
>         threadexitsall("usage");
> }
>
> void
> threadmain(int argc, char *argv[])
> {
>         ARGBEGIN{
>         case 'r':
>                 rflag ^= rflag;
>                 break;
>         default:
>                 usage();
>         }ARGEND;
>
>         if(argc == 0)
>                 usage();
>
>         if(argc > nelem(colors))
>                 argc = nelem(colors);
>
>         for(ncolor = 0; ncolor < argc; ncolor++)
>                 colors[ncolor] = strtoul(argv[ncolor], 0, 16);
>
>         if(init() < 0)
>                 threadexitsall("initdraw");
>
>         redraw(1);
>         proccreate(kbdproc, 0, 1024);
>         proccreate(mouseproc, 0, 8192);
>         proccreate(resizeproc, 0, 8*8192);
>         for(;;)
>                 sleep(10000);
> }
>

Reply via email to