From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 In-Reply-To: References: <62328632c05339f73df807068c511920@rei2.9hal> Date: Wed, 19 Dec 2012 15:35:20 +0100 Message-ID: From: Giovanni Casano To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [9fans] Good sample GUI code (window creation, management, Topicbox-Message-UUID: f800a41e-ead7-11e9-9d60-3106f5b1d025 Does it work in Plan9Port? 2012/12/19 erik quanstrom : > 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 > #include > #include > #include > #include > #include > > 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); > } >