|
|
An ETI program can produce output on more than one terminal at the same time. This is useful for single process programs that access a common database, such as multi-player games.
Writing programs that output to multiple terminals is a difficult business, and the ETI library does not solve all the problems you might encounter. For instance, the programs--not the library routines--must determine the file name of each terminal line, and what kind of terminal is on each of those lines. The standard method, checking $TERM in the environment, does not work, because each process can only examine its own environment.
Another problem you might face is that of multiple programs reading from one line. This situation produces a race condition and should be avoided. However, a program trying to take over another terminal cannot just shut off whatever program is currently running on that line. (Usually, security reasons would also make this inappropriate. But, for some applications, such as an inter-terminal communication program, or a program that takes over unused terminal lines, it would be appropriate.) A typical solution to this problem requires each user logged in on a line to run a program that notifies a master program that the user is interested in joining the master program and tells it the notification program's process ID, the name of the tty line, and the type of terminal being used. Then the program goes to sleep until the master program finishes. When done, the master program wakes up the notification program and all programs exit.
An ETI program handles multiple terminals by always having a current terminal. All function calls always affect the current terminal. The master program should set up each terminal, saving a reference to the terminals in its own variables. When it wishes to affect a terminal, it should set the current terminal as desired, and then call ordinary ETI routines.
References to terminals in an ETI program have the type SCREEN. A new terminal is initialized by calling newterm(type, outfd, infd). newterm returns a screen reference to the terminal being set up. type is a character string, naming the kind of terminal being used. outfd is a Intro(3S) file pointer (FILE) used for output to the terminal and infd a file pointer for input from the terminal. This call replaces the normal call to initscr, which calls newterm(getenv(''TERM''), stdout, stdin).
To change the current terminal, call set_term(sp) where sp is the screen reference to be made current. set_term returns a reference to the previous terminal.
It is important to realize that each terminal has its own set of windows and options. Each terminal must be initialized separately with newterm. Options such as cbreak and noecho must be set separately for each terminal. The functions endwin and refresh must be called separately for each terminal. ``Sending a message to several terminals'' shows a typical scenario to output a message to several terminals.
for (i=0; i<nterm; i++) { set_term(terms[i]); mvaddstr(0, 0, "Important message"); refresh(); }
Sending a message to several terminals
See ``The two program'' for a more complete example.