[comp.windows.x] Help: need to handle input-from-stdin and events simultaneously

mike@aleytys.UU.NET (Michael Kent) (05/01/89)

Can someone give me some tips...

Environment:
	X11R2 toolkit and Athena widgets, UNIX/386 SYS5 rel. 3.2

I need to do character-by-character input from stdin while at the same time
handling events.  I see two ways of doing this:

1. Don't use XtMainLoop, use 'handle-a-char/XtNextEvent/XtDispatchEvent'.

2. Use XtAddInput.

Which has the better efficiency?

And regarding XtAddInput: the second parameter (condition) is documented
as 'specifies the mask that indicates either a read, write, or exception
condition or some operating system dependend condition.'

Can someone translate this for me and let me know what I'm supposed to
do for this parameter?
-- 
Michael Kent                      INTERNET: mike@aleytys.UU.NET
Center for M.C.E.                 UUCP:     ...!{uflorida, uunet}!aleytys!mike
2521 NW 57th Pl.                  VOICE:    +1-904-335-1573
Gainesville, Fl 32606             "These are MY opinions, get your own!"

bw@hpcvlx.HP.COM (Bill Wilhelmi) (05/03/89)

I am reposting an example Scott Bolte posted some time ago
on this subject.  It demonstrates both XtAddTimeOut and
XtAddInput.  Good luck!

Bill Wilhelmi
Hewlett-Packard Company



/*******************************************
 * XtAddInput() demo program.
 *
 * By Scott Bolte of Cray Research, Inc.
 *
 * Posted to comp.windows.x on Jan. 26, 1989
 *******************************************/



#include <stdio.h>
#include <sys/time.h>
#include <X11/StringDefs.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>

main(argc, argv)
	int             argc;
	char           *argv[];

{
	int             pipes[2],
	                read_fd();
	void            time_out();


	pipe(pipes);
	fprintf(stderr, "read pipe is %d and write pipe is %d\n",
		pipes[0], pipes[1]);

	(void) XtInitialize("master", "crayperf", NULL, 0, &argc, argv);

	(void) XtAddTimeOut(5000, time_out, pipes[1]);
	(void) XtAddInput(pipes[0], XtInputReadMask, read_fd, NULL);

	XtMainLoop();
}

void
time_out(fd, id)
	int             fd;
	XtIntervalId   *id;
{
	int             i;

	fprintf(stderr, "time_out() was called with fd %d.\n", fd);
	i = write(fd, "X", 1);
	fprintf(stderr, "time_out() wrote %d %s to fd %d\n", i
		,i > 1 ? "bytes" : "byte", fd);
	
	/*** The timeout is automatically removed, so let's add it again ***/
	XtAddTimeOut(5000, time_out, fd); 
}

read_fd(junk, socket, InId)
	caddr_t         junk;		/* Client data for X callbacks */
	int            *socket;
	caddr_t         InId;		/* XtInputId if we were to use it */
{
	long            t;
	char            c;
	int             i;

	t = time(0);
	fprintf(stderr, "read_fd() called at %ld.\n", t);
	if (read_check(*socket) <= 0) {
		fprintf(stderr, " ****** I do not agree that there is data.\n");
		return;
	}
	i = read(*socket, &c, 1);
	switch (i) {
	case -1:
		perror("read returned an error. ");
		break;

	case 0:
		fprintf(stderr, "read returned zero bytes.\n");
		break;

	case 1:
		fprintf(stderr, "read returned the '%c' character.\n", c);
		break;

	default:
		fprintf(stderr, "read returned more than a single character!\n");
		break;
	}
}

read_check(fd)
	int             fd;
{
	int             status;
	int             rmask;
	struct timeval  tv;

	tv.tv_sec = 0;
	tv.tv_usec = 0;
	rmask = (1 << fd);

	status = select(fd + 1, &rmask, 0, 0, &tv);
	return (status);
}