Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!bloom-beacon!athena.mit.edu!bjaspan From: bjaspan@athena.mit.edu (Barr3y Jaspan) Newsgroups: comp.windows.x Subject: Re: XtAddInput problem Keywords: XtAddInput XtAppAddInput select Message-ID: <15783@bloom-beacon.MIT.EDU> Date: 10 Nov 89 00:40:57 GMT References: <8911081827.AA12003@turnpike.sun.com> <294@nap1.cds.wpafb.af.mil> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: bjaspan@athena.mit.edu (Barr3y Jaspan) Organization: Massachusetts Institute of Technology Lines: 79 I've seen random discussion about the use of XtAddInput() and about how it supposedly does not work. It *does* work, and I will attempt to clear up the confusion. Anyone who is familiar with UNIX realizes that the intrinsics use select() to detect X and other events, and that XtAddInput() merely adds filedescriptors to the parameters passed to select. The man page for select() says that it returns when there a file descriptor is "ready for reading, ready for writing, ..." The confusion, I think, is in "ready for writing." That means that the fd is in a state such that data CAN be written to it, not that data actually has been written to it. Specifically, stdout is ALWAYS "ready for writing" (unless it has been messed with) and so *IF YOU DO AN XtAddInput ON FD 1, IT WILL TO CALL THE FUNCTION CONSTANTLY* and that is the correct behavior. For input events, XtAddInput also does the right thing: it calls the function whenever there is data waiting to be read. For example, if you do XtAddInput() on fd 0 with XtInputReadMask, the callback will be called when the user has typed a line (or a character in cbreak mode). The function IS NOT called constantly. As proof of my argument, I present the following source code and execution output. --- snip snip --- #include #include #include #include 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); } --- snip snip --- ~% cc -I/mit/x11/include -L/mit/x11/vaxlib input-example.c -lXaw -lXmu -lXt -lX11 ~% a.out testing Input received on fd 0. "testing" Hi there!! Input received on fd 0. "Hi" ^C~% Barr3y Jaspan, MIT-Project Athena bjaspan@athena.mit.edu Barry Jaspan, MIT-Project Athena bjaspan@athena.mit.edu