9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: Re: [9fans] Good sample GUI code (window creation, management,
Date: Wed, 19 Dec 2012 08:32:26 -0500	[thread overview]
Message-ID: <e69108a5c8448b3df0b146db901a7106@brasstown.quanstro.net> (raw)
In-Reply-To: <62328632c05339f73df807068c511920@rei2.9hal>

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	other threads:[~2012-12-19 13:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-19 11:14 Luke Evans
2012-12-19 12:59 ` Jacob Todd
2012-12-19 13:02 ` cinap_lenrek
2012-12-19 13:32   ` erik quanstrom [this message]
2012-12-19 14:35     ` Giovanni Casano
2012-12-19 14:42       ` erik quanstrom
2012-12-19 14:54         ` Giovanni Casano
2012-12-19 16:07 ` Stuart Morrow
2012-12-19 16:17   ` Dustin Fechner
2012-12-19 16:27     ` Stuart Morrow
2012-12-19 16:29   ` erik quanstrom
2012-12-20  9:45 ` dexen deVries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e69108a5c8448b3df0b146db901a7106@brasstown.quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).