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
#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: mkfile
Description: Binary data

Reply via email to