Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bionet!agate!ucbvax!hplabs!hp-pcd!hpcvlx!bw From: bw@hpcvlx.HP.COM (Bill Wilhelmi) Newsgroups: comp.windows.x Subject: Re: Help: need to handle input-from-stdin and events simultaneously Message-ID: <100920074@hpcvlx.HP.COM> Date: 2 May 89 21:09:55 GMT References: <311@aleytys.UU.NET> Organization: Elvis seen at Safeway Lines: 113 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 #include #include #include #include #include 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); }