9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: "ron minnich" <rminnich@gmail.com>
To: "Fans of the OS Plan 9 from Bell Labs" <9fans@cse.psu.edu>
Subject: [9fans] silliness in flight: build a desktop calculator with srv and rio
Date: Sun, 30 Mar 2008 22:08:01 -0700	[thread overview]
Message-ID: <13426df10803302208r5e21f6bi77fccf4bc998b221@mail.gmail.com> (raw)

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

I've been wanting a desktop calculator for some time. I'm sitting on
the plane, trying to avoid real work, and decided to see if I could do
this:
1. put a dc behind /srv/desk
2. start a rio in a window
3. window 1 becomes cat /srv/desk
4. other windows are stupid programs that, on mouse click, send text
to /srv/desk. What they send is easily changed.

voila. rpn calculator.

Anyway, it's a hack right now, but I thought it might be fun for
someone to take my hack and make it real or tell me that "it's already
written" (that happens) or "here is a better way". Part of the goal is
to show how to paste bits together with srv and such and make
something go.

So, step 1. put dc behind /srv/desk. What's a better way?
#include <u.h>
#include <libc.h>

void
main(int, char **)
{
	int fd, p[2];

	pipe(p);
	fd = create("/srv/desk", OWRITE, 0666);
	fprint(fd, "%d", p[0]);
	close(fd);
	close(p[0]);

	dup(p[1], 0);
	dup(p[1], 1);
	execl("/bin/dc", "dc", 0);
}

step 2 you better know, as well as step 3.

Step 4, attached, too much for inline.
What the program (but.c) really ought to do is read //dev/text, but I
don't know how to make that go.
It reads /dev/label instead.

So, to program a key, you can
echo 8+p>/dev/label
./8.but
And then you've got a "soft key" that can be anything you want, and
you can change it at any time. I re-read /dev/label fd in the loop.
You can obviously change the label external to the program. but.c
shows the label value in the window.

How to shrink the window down? How to get and LED font with demonic red color?

My crude libdraw skills are probably pretty obvious.

Anyway, I like the fact that I'm using a window manager and srv etc.
to build a graphical calculator with soft keys in 1/10 the space that
most X based calculators do it in. I would rather open a vein than try
to do this in X11 -- I even tried using Glade once and it's hateful.

thanks

ron

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: but.c --]
[-- Type: text/x-csrc; name=but.c, Size: 1732 bytes --]

#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>

Image *what, *back;
char *fontname = "/lib/font/bit/lucidasans/unicode.13.font";
Font *font;
char but[512];

void
redraw(Image *screen)
{
	draw(screen, screen->r, back, nil, ZP);
	string(screen, screen->r.min, what, ZP, font, but);

	flushimage(display, 1);
}

void
eresized(int new)
{
	if(new && getwindow(display, Refnone) < 0)
		fprint(2,"can't reattach to window");
	redraw(screen);
}

void
main(int, char**)
{
	Event e;
	Mouse m;
	int key, timer;
	int t;
	int text;
	int desk;
	char *deskname = "/srv/desk";

/* gosh I wish this worked -- it would be cool
	you would have soft keys. But there's all this junk in there. 
	text = open("/dev/text", OREAD);
 */
	text = open("/dev/label", OREAD);
	if (text < 0)
		exits(smprint("/dev/label: %r"));

	if (pread(text, but, sizeof(but), 0LL) < 0)
		exits("read label");

	desk = open(deskname, OWRITE);
	if (desk < 0)
		exits(smprint("%s: %r", deskname));
	initdraw(0,0,but);
	drawsetdebug(1);

	if((font = openfont(display, fontname)) == nil)
		sysfatal("font '%s' not found", fontname);

	back = allocimagemix(display, DPalebluegreen, DWhite);
	what = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);

	redraw(screen);

	einit(Emouse);
	t = (30*1000);
	timer = etimer(0, t);

	for(;;) {
		key = event(&e);
		if(key == Emouse) {
			m = e.mouse;
			if(m.buttons & 4) {
				int amt;
				/* why do we re read this? So you can have soft keys */
				memset(but, 0, sizeof(but));
				amt = pread(text, but, sizeof(but), 0LL);
				if (amt < 0)
					exits("read text: %r");
				amt = write(desk, but, amt);
				if (amt < 0)
					exits("write desk: %r");
			}
		} else if(key == timer) {
			redraw(screen);
		}
	}	
}

[-- Attachment #3: mkfile --]
[-- Type: application/octet-stream, Size: 394 bytes --]

</$objtype/mkfile
#
#		programs
#
TARG=\
	but\
	startdc

DIRS=

OTHEROFILES=

HFILES=

LIB=/$objtype/lib/libc.a
BIN=/$objtype/bin
CLIB=

UPDATE=\
	mkfile\
	$HFILES\
	${OTHEROFILES:%.$O=%.c}\
	${TARG:%=%.c}\

</sys/src/cmd/mkmany

all:V:	

clean:V:
	rm -f *.[$OS] *.[$OS].a [$OS].* y.tab.? y.debug y.output $TARG

nuke:V:
	rm -f *.[$OS] *.[$OS].a [$OS].* y.tab.? y.debug y.output $TARG *.acid



             reply	other threads:[~2008-03-31  5:08 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-31  5:08 ron minnich [this message]
2008-03-31 22:43 ` Federico G. Benavento
2008-03-31 22:51   ` Pietro Gagliardi
2008-03-31 22:57     ` andrey mirtchovski
2008-03-31 23:04     ` ron minnich
2008-03-31 23:02   ` ron minnich
2008-04-04 18:05     ` Paul Lalonde
2008-04-05 13:53       ` Caerwyn Jones
2008-03-31 23:04   ` ron minnich
2008-04-01 11:11   ` [9fans] silliness in flight: build a desktop calculator with kokamoto
2008-04-01 13:11     ` ron minnich
2008-04-01 13:29       ` Eric Van Hensbergen
2008-04-01 14:46         ` ron minnich
2008-04-01 16:10     ` Skip Tavakkolian
2008-04-02 11:19       ` kokamoto
2008-04-01 17:48     ` Federico G. Benavento
2008-04-01 22:08       ` hiro
2008-04-02 11:46       ` kokamoto
2008-04-02 13:20         ` hiro
2008-04-02 22:38         ` Federico G. Benavento

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=13426df10803302208r5e21f6bi77fccf4bc998b221@mail.gmail.com \
    --to=rminnich@gmail.com \
    --cc=9fans@cse.psu.edu \
    /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).