9fans archive / 1997 / 11 / 16 / prev next
From: john@min... john@min...
Subject: [9fans] Novice use of libg
Date: Thu, 20 Nov 97 16:16:33
I'm an experienced UNIX programmer who should have made more time
over the last several years to get started writing programs for
use with Plan 9. I'm having a little trouble with writing a program
which I hope will sit at the r.h.s. of a two-process pipeline and draw
a graphical representation of some slowly incoming data, and also
allow the user to manipulate the representation with their mouse.
Perhaps some kind person will offer their advice.
I made a start with the tiny sample program below. I'm compiling
it on a 486 with `8c viewer.c' and loading it with `8l viewer.8',
then testing it with `echo hello | 8.out'. I thought I could use
estart() to get me an `Estdin' event key, but I actually see the
fatal error message `8.out events: bad slave assignment'.
I also have libXg from the UNIX sam distribution. What alterations
would I need to (the corrected version of) the program below in order
to run it under (a modern) UNIX? (My guess is that fcntl() with
O_NONBLOCK is part of the answer, but when I try this I see an
infinite number of copies of my message `debug: Estdin, e.n 0'.)
Any other comments, e.g. on style, would be thankfully received.
John A. Murdie
Department of Computer Science
University of York
England
------------------------------- cut here -----------------------
#include <u.h>
#include <libc.h>
#include <libg.h>
#include <stdio.h>
#define bool int
#define false 0
#define true 1
static char *progname;
void
panic(char *s) {
fprintf(stderr, "%s: %s\n", progname, s);
fflush(stderr);
exits("fatal");
}
void
ereshaped(Rectangle r) {
}
int
main(int argc, char **argv) {
Mouse mouse;
ulong Estdin;
bool done = false;
progname = argv[0];
binit(panic, 0, progname);
if ((Estdin = estart((ulong)0, 0, 8192)) == 0)
panic("Estdin = estart(0, 0, 8192) fails");
einit(Emouse | Estdin);
do {
struct Event e;
ulong key = eread(Emouse | Estdin, &e);
if (key == Estdin) {
fprintf(stderr, "debug: Estdin, e.n %d\n", e.n); fflush(stderr);
} else if (key == Emouse) {
fprintf(stderr, "debug: Emouse, x %d y %d\n", e.mouse.xy.x, e.mouse.xy.y);
fflush(stderr);
} else
panic("unexpected event");
} while (!done);
bflush();
exits(0);
}