9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] Good sample GUI code (window creation, management,
@ 2012-12-19 11:14 Luke Evans
  2012-12-19 12:59 ` Jacob Todd
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Luke Evans @ 2012-12-19 11:14 UTC (permalink / raw)
  To: 9fans

I thought I had bumped into a short example on the web for creating
a window in C, but can't seem to find it again.

I'm sure I could search all the sources for various examples of such
things, but does anybody know of a good (preferably concise) sample
that demonstrates the correct way to write GUI apps in Plan 9?

Thanks!



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 11:14 [9fans] Good sample GUI code (window creation, management, Luke Evans
@ 2012-12-19 12:59 ` Jacob Todd
  2012-12-19 13:02 ` cinap_lenrek
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Jacob Todd @ 2012-12-19 12:59 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 506 bytes --]

9.intro.pdf has examples of creating windows iirc; also, check the rio
source.
On Dec 19, 2012 6:18 AM, "Luke Evans" <luke.evans@gmail.com> wrote:

> I thought I had bumped into a short example on the web for creating
> a window in C, but can't seem to find it again.
>
> I'm sure I could search all the sources for various examples of such
> things, but does anybody know of a good (preferably concise) sample
> that demonstrates the correct way to write GUI apps in Plan 9?
>
> Thanks!
>
>

[-- Attachment #2: Type: text/html, Size: 765 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 11:14 [9fans] Good sample GUI code (window creation, management, Luke Evans
  2012-12-19 12:59 ` Jacob Todd
@ 2012-12-19 13:02 ` cinap_lenrek
  2012-12-19 13:32   ` erik quanstrom
  2012-12-19 16:07 ` Stuart Morrow
  2012-12-20  9:45 ` dexen deVries
  3 siblings, 1 reply; 12+ messages in thread
From: cinap_lenrek @ 2012-12-19 13:02 UTC (permalink / raw)
  To: 9fans

the trick is just mounting $wsys and optionally supply some window
attributes in the mount spec. see rio(1) and /sys/src/libdraw/newwindow.c

after that, you have the new window mounted on /dev and a following
initdraw() will pick it up.

if you want to maintain multiple windows for your program, its better
to mount each window somewhere else and supply windir.

its unusual for plan9 programs to make ther own windows tho. instead,
its more common to create windows/layers inside your screen (like
sam or acme).

--
cinap



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 13:02 ` cinap_lenrek
@ 2012-12-19 13:32   ` erik quanstrom
  2012-12-19 14:35     ` Giovanni Casano
  0 siblings, 1 reply; 12+ messages in thread
From: erik quanstrom @ 2012-12-19 13:32 UTC (permalink / raw)
  To: 9fans

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);
}



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 13:32   ` erik quanstrom
@ 2012-12-19 14:35     ` Giovanni Casano
  2012-12-19 14:42       ` erik quanstrom
  0 siblings, 1 reply; 12+ messages in thread
From: Giovanni Casano @ 2012-12-19 14:35 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Does it work in Plan9Port?

2012/12/19 erik quanstrom <quanstro@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);
> }
>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 14:35     ` Giovanni Casano
@ 2012-12-19 14:42       ` erik quanstrom
  2012-12-19 14:54         ` Giovanni Casano
  0 siblings, 1 reply; 12+ messages in thread
From: erik quanstrom @ 2012-12-19 14:42 UTC (permalink / raw)
  To: 9fans

On Wed Dec 19 09:36:41 EST 2012, ucasano@gmail.com wrote:
> Does it work in Plan9Port?
>

it's left as an exercize to the reader to add the missing
function parameters and USED(unused).  but it does work.

- erik



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 14:42       ` erik quanstrom
@ 2012-12-19 14:54         ` Giovanni Casano
  0 siblings, 0 replies; 12+ messages in thread
From: Giovanni Casano @ 2012-12-19 14:54 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

:D

2012/12/19 erik quanstrom <quanstro@quanstro.net>:
> On Wed Dec 19 09:36:41 EST 2012, ucasano@gmail.com wrote:
>> Does it work in Plan9Port?
>>
>
> it's left as an exercize to the reader to add the missing
> function parameters and USED(unused).  but it does work.
>
> - erik
>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 11:14 [9fans] Good sample GUI code (window creation, management, Luke Evans
  2012-12-19 12:59 ` Jacob Todd
  2012-12-19 13:02 ` cinap_lenrek
@ 2012-12-19 16:07 ` Stuart Morrow
  2012-12-19 16:17   ` Dustin Fechner
  2012-12-19 16:29   ` erik quanstrom
  2012-12-20  9:45 ` dexen deVries
  3 siblings, 2 replies; 12+ messages in thread
From: Stuart Morrow @ 2012-12-19 16:07 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 12/19/12, Luke Evans <luke.evans@gmail.com> wrote:
> I'm sure I could search all the sources for various examples of such
> things, but does anybody know of a good (preferably concise) sample
> that demonstrates the correct way to write GUI apps in Plan 9?

Russ Cox had a page full of trivial graphics programs to learn from on
the web.  This isn't swtch.com though - I think it was some .edu
website.  This may be what you were thinking of.  I don't know if it
still exists, but the source is probably on my disembodied desktop HDD
from the first time I was interested in Plan 9 (searching 9fans for my
name says 2010 surprisingly; it feels longer than that).

> I thought I had bumped into a short example on the web for creating
> a window in C, but can't seem to find it again.

Plan 9 programs don't create windows, they use the window already
allocated in their namespace.  This is analogous to programs not
creating /fd/^(0 1 2) by themselves - they use the ones inherited
through fork.

cinap_lenrek wrote:
>its unusual for plan9 programs to make ther own windows tho. instead,
>its more common to create windows/layers inside your screen (like
>sam or acme).

This is someone I was just thinking about the other day (Plan 9 is on
my mind lately, I just resubscribed to 9fans and am planning on
getting properly into it after my January exams - creating a sources
account and everything) -- does sam really do its own window
management -- I mean, it's got overlapping windows and everything so
that's non-trivial - or does it ask devdraw?  I suppose an easy way to
find out would be to run Plan 9 with nothing but sam running, open
alot of sam's internal windows, and ls in /dev/draw to see how many
subdirectories there are.  But I'm not actually installing Plan 9
until I have the time for it.

Stuart - who just learned that the last IWP9 was a one-hour train ride from him.

P.S. you might be interested to know that the "theoretical model" of
how Plan 9's window management works -- that rio serves a fake mouse,
keyboard, and screen to each of its children -- is wrong.  rio serves
a fake mouse and cons, but program write directly to the "real"
/dev/draw.  rio gives them a file called /dev/winname which contains a
pointer to whereabouts in /dev/draw they are to write to (and they can
violate this convention if they so wish).  You could even say winname
is a type of symbol link (!)

You might think that this is primarily for speed instead of
virtualising a whole draw device,  This is partly the reason, but also
Rob Pike wanted to do some sort of graphical Acme (and never got
around to it), so he put the graphics somewhere where rio and acme
could both have access to them.

FGB slapped a sort of graphical Acme together in a few minutes one
time, but it's not the same sort of thing that Rob had in mind.

Stuart - surprised how much of this he remembers.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  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
  1 sibling, 1 reply; 12+ messages in thread
From: Dustin Fechner @ 2012-12-19 16:17 UTC (permalink / raw)
  To: 9fans

On 12/19/2012 05:07 PM, Stuart Morrow wrote:
> Russ Cox had a page full of trivial graphics programs to learn from on
> the web.  This isn't swtch.com though - I think it was some .edu
> website.

Do you mean this site?
http://pdos.csail.mit.edu/~rsc/plan9.html




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 16:17   ` Dustin Fechner
@ 2012-12-19 16:27     ` Stuart Morrow
  0 siblings, 0 replies; 12+ messages in thread
From: Stuart Morrow @ 2012-12-19 16:27 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On 12/19/12, Dustin Fechner <dfe@hush.com> wrote:
> Do you mean this site?
> http://pdos.csail.mit.edu/~rsc/plan9.html

Yep, click.c was the one I was thinking of specifically.  I remember
reading that - I was a C virgin at the time, I "knew" C but was never
really brave enough to try it before I got Plan 9.  I mean, I had the
K&R book, but that doesn't really prepare you for
Linux/Windows/glibc/gcc's weirdness.

Stuart



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 16:07 ` Stuart Morrow
  2012-12-19 16:17   ` Dustin Fechner
@ 2012-12-19 16:29   ` erik quanstrom
  1 sibling, 0 replies; 12+ messages in thread
From: erik quanstrom @ 2012-12-19 16:29 UTC (permalink / raw)
  To: 9fans

> > I thought I had bumped into a short example on the web for creating
> > a window in C, but can't seem to find it again.
> 
> Plan 9 programs don't create windows, they use the window already
> allocated in their namespace.  This is analogous to programs not
> creating /fd/^(0 1 2) by themselves - they use the ones inherited
> through fork.

that's not quite correct.  by default plan 9 programs use the window
they're in.  but they can create a new window (e.g. page -w).

> account and everything) -- does sam really do its own window
> management -- I mean, it's got overlapping windows and everything so
> that's non-trivial - or does it ask devdraw?  I suppose an easy way to

yes, samterm really does.

> P.S. you might be interested to know that the "theoretical model" of
> how Plan 9's window management works -- that rio serves a fake mouse,
> keyboard, and screen to each of its children -- is wrong.  rio serves

it was correct for 8½.

- erik



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] Good sample GUI code (window creation, management,
  2012-12-19 11:14 [9fans] Good sample GUI code (window creation, management, Luke Evans
                   ` (2 preceding siblings ...)
  2012-12-19 16:07 ` Stuart Morrow
@ 2012-12-20  9:45 ` dexen deVries
  3 siblings, 0 replies; 12+ messages in thread
From: dexen deVries @ 2012-12-20  9:45 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Wednesday 19 of December 2012 11:14:30 Luke Evans wrote:
> I thought I had bumped into a short example on the web for creating
> a window in C, but can't seem to find it again.
> 
> I'm sure I could search all the sources for various examples of such
> things, but does anybody know of a good (preferably concise) sample
> that demonstrates the correct way to write GUI apps in Plan 9?


$PLAN9/src/cmd/draw/*.c
in particular, stats.c -- comes with a simple pop-up menu, at under 1kLOC.

-- 
dexen deVries

[[[↓][→]]]


Reality is just a convenient measure of complexity.
                -- Alvy Ray Smith



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-12-20  9:45 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-19 11:14 [9fans] Good sample GUI code (window creation, management, Luke Evans
2012-12-19 12:59 ` Jacob Todd
2012-12-19 13:02 ` cinap_lenrek
2012-12-19 13:32   ` erik quanstrom
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

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).