[comp.windows.x] Reply to my message on XtAddInput

bjaspan@athena.mit.edu (Barr3y Jaspan) (11/22/89)

Just recently I received a question from adrian@UUNET.UU.NET about the
message I sent some time ago about XtAddInput.  For bizarre reasons
that I don't understand, my mailer won't let me reply directly to the
message.  Since the answer to the question may help others I am
posting my response here; I ask that whoever forwarded my message to
adrian@UUNET.UU.NET in the first place also forward this response to
his (her?) message.  Thanks.

Barr3y Jaspan, MIT-Project Athena
bjaspan@athena.mit.edu

------------------------ snip snip snip snip --------------------------

From: Barr3y Jaspan <bjaspan@ATHENA.MIT.EDU>
Sender: bjaspan@ATHENA.MIT.EDU
To: adrian@UUNET.UU.NET
In-Reply-To: Adrian Nye's message of Tue, 21 Nov 89 10:20:52 EST <8911211521.AA01108@spike>
Subject: XtAddInput

   Date: Tue, 21 Nov 89 10:20:52 EST
   From: adrian@UUNET.UU.NET (Adrian Nye)



   I was forwarded a message of yours regarding XtAddInput, explaining
   when it calls the function continuously and when it doesn't.

   <my message deleted>

   Being not a UNIX wizard, I'm not sure what fd 0 is.  stdin?

Well, stdin is of type FILE *.  It is a stream, which is an
abstraction built on top of file descriptors (at least, that's how
it's implemented in unix.)  However, each FILE structure contains the
file descriptor it is built on top of, and yes, stdin is on fd 0.

   But my real question is how to accomplish some common tasks with
   XtAddInput.  Namely, how do you read input from a file or pipe?
   Since the file descriptor for the file or pipe is always ready for
   reading, Xt seems to call the function even when there is no new
   input available.  This is unfortunate, because the calling of the
   function continuously loads down the system a lot.

You are a little confused (which is not surprising, as this topic is
confusing).  A fd is "ready for reading" when there is data waiting to
be read; a fd is "ready for writing" when it is in a state that in
which it can deal with being written to.  Thus, stdout is almost
always ready for writing, but stdin is only ready for reading when the
user types a character.

   What I really want is to be called only when new input is
   available.

The following program works with X11R3 on Project Athena, which is
running BSD4.3.  If it does not work on your system then probably some
low-level fd parameters have different default values (although I'm
not sure what they may be), and those can be changed.  Basically, the
program prints out each line of input as it receives it, and as you
can tell if you just run the program with no redirection, input() only
gets called when there is actually data to be read.

I hope this helps.  Let me know if you have more questions.

Barr3y Jaspan, MIT-Project Athena
bjaspan@athena.mit.edu

-------- snip snip -------

#include <stdio.h>

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Label.h>

void input();

main(argc, argv)
   int argc;
   char **argv;
{
     Widget	top;

     top = XtInitialize("Hello", "World", NULL, 0, &argc, argv);
     (void) XtCreateManagedWidget("label", labelWidgetClass, top,
				  NULL, 0);
     
     XtAddInput(0, XtInputReadMask, input, NULL);
     XtRealizeWidget(top);
     XtMainLoop();
}

void input(client_data, source, input_id)
   caddr_t	client_data;
   int		*source;
   XtInputId	*input_id;
{
     char	buf[BUFSIZ];
     
     printf("Input received on fd %d.\n", *source);
     scanf("%s", buf);
     printf("\"%s\"\n", buf);
}



-------------------------

Hmmm... I wonder if Pnews is going to automatically append my
.signature file without asking.

Barry Jaspan, MIT-Project Athena
bjaspan@athena.mit.edu