Using ttysw_output() shows that the text is simply output to the dummy terminal emu-
lator described by ttysw. ttysw_input() is useful for sending data as input to a pro-
gram running in the TTY subwindow. For example, a window-based front end for a text edi-
tor could be written where all the common functions such as Save and Next Page can be pro-
grammed into panel buttons. Selecting one of those panel buttons would cause a constant
string to be sent to the application to be processed as input. The write filename function in vi
could have a button that uses ttysw_input() to send the string (w!\n) to the TTY
subwindow containing the program.
9.3 Monitoring the Program in the TTY Subwindow
When you use the TTY_ARGV attribute to pass the name of a program to run to the TTY
subwindow, the program runs as a forked child process. If the attribute:
TTY_QUIT_ON_CHILD_DEATH
is set to TRUE, then the application exits when the forked program exits. But, by default, this
attribute is set to FALSE. You can use TTY_PID to monitor the state of the child process run-
ning in the TTY window via the Notifier using notify_set_wait3_func(). The cli-
ent’s wait3() function gets called when the state of the process in the TTY subwindow
changes:*
#include <sys/wait.h>
static Notify_value my_wait3();
...
ttysw = xv_create(base_frame, TTY,
TTY_ARGV, my_argv,
NULL);
child_pid = (int)xv_get(ttysw, TTY_PID);
notify_set_wait3_func(ttysw, my_wait3, child_pid);
...
The wait3() function can then do something useful, such as destroying the TTY window or
starting up another process. The code fragment below detects when any of the TTY subwin-
dow’s child processes has died.
static Notify_value
my_wait3(ttysw, pid, status, rusage)
Tty ttysw;
int pid;
union wait *status;
struct rusage *rusage;
{
int child_pid;
notify_next_wait3_func(ttysw, pid, status, rusage);
if (!(WIFSTOPPED(*status))) {
/* rerun the program */
xv_set(ttysw, TTY_ARGV, my_argv, NULL);
*This includes when the program stops in addition to when it exits.
246 XView Programming Manual
child_pid = (int)xv_get(ttysw, TTY_PID);
notify_set_wait3_func(ttysw, my_wait3, child_pid);
}
return NOTIFY_DONE;
}
You can set TTY_PID as well as get it, but if you set it, you are responsible for setting:
notify_set_wait3_func()
to catch the child’s death. You are also responsible for directing the standard input and stan-
dard output of the child to the pseudo-TTY.
9.4 Talking Directly to the TTY Subwindow
Setting TTY_ARGV to TTY_ARGV_DO_NOT_FORK tells the system not to fork a child in the TTY
subwindow. In combination with TTY_TTY_FD, this allows the tool to use standard I/O rou-
tines to read and write to the TTY subwindow by getting the file descriptor of the pseudo-
TTY associated with the TTY subwindow. You can then use this to read and write to the
pseudo-TTY using standard UNIX I/O routines.
Example 9-3 uses a TTY subwindow to create a pseudo terminal in which curses routines can
be used. Five panel items are displayed. Along with the usual Quit button to exit the pro-
gram, a Print button displays the text in the text panel item at the coordinates input in the X
and Y numeric text items.
Example 9-3. The ttycurses.c program
/*
* ttycurses.c -- An application that uses a tty subwindow that
* emulates a tty so well, you can use curses(3x) routines in it.
* This program does not handle resizes -- resizing the base frame
* produces unpredictable results. To handle resizing properly,
* the application should install a resize event handler and
* call endwin() followed by initscr() to reinitialize curses
* to reflect the size of the window.
*
* cc ttycurses.c -lxview -lcurses -ltermlib
*/
#include <curses.h>
#undef WINDOW /* defined by curses.h -- needs to be undefined */
#include <xview/xview.h>
#include <xview/panel.h>
#include <xview/textsw.h>
#include <xview/tty.h>
/* panel items contain the x,y info for outputting text to the ttysw */
Panel_item x, y, text;
main(argc,argv)
int argc;
char *argv[ ];
{
Frame frame;
TTY Subwindows
TTY Subwindows 247
Example 9-3. The ttycurses.c program (continued)
Panel panel;
Tty ttysw;
char buf[16];
void output(), exit();
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, NULL);
frame = xv_create(XV_NULL, FRAME,
FRAME_LABEL, argv[0],
FRAME_SHOW_FOOTER, TRUE,
NULL);
panel = (Frame)xv_create(frame, PANEL, NULL);
(void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Quit",
PANEL_NOTIFY_PROC, exit,
NULL);
(void) xv_create(panel, PANEL_BUTTON,
PANEL_LABEL_STRING, "Print",
PANEL_NOTIFY_PROC, output,
NULL);
x = (Panel_item)xv_create(panel, PANEL_NUMERIC_TEXT,
PANEL_LABEL_STRING, "X:",
PANEL_VALUE_DISPLAY_LENGTH, 3,
NULL);
y = (Panel_item)xv_create(panel, PANEL_NUMERIC_TEXT,
PANEL_LABEL_STRING, "Y:",
PANEL_VALUE_DISPLAY_LENGTH, 3,
NULL);
text = (Panel_item)xv_create(panel, PANEL_TEXT,
PANEL_LABEL_STRING, "Text:",
PANEL_VALUE_DISPLAY_LENGTH, 10,
PANEL_VALUE, "X",
NULL);
window_fit(panel);
ttysw = (Tty)xv_create(frame, TTY,
WIN_BELOW, panel,
WIN_X, 0,
TTY_ARGV, TTY_ARGV_DO_NOT_FORK,
NULL);
window_fit(frame);
dup2((int)xv_get(ttysw, TTY_TTY_FD), 0); /* dup2 closes 0 first */
dup2((int)xv_get(ttysw, TTY_TTY_FD), 1); /* dup2 closes 1 first */
/* initscr() initializes the curses package and determines
* characteristics about the window as if it were a terminal.
* The curses specific variables, LINES and COLS are now set
* to the row and column sizes of the window.
*/
initscr();
xv_set(x, PANEL_MAX_VALUE, COLS-1, NULL);
xv_set(y, PANEL_MAX_VALUE, LINES-1, NULL);
sprintf(buf, "LINES: %d", LINES–1);
248 XView Programming Manual
Example 9-3. The ttycurses.c program (continued)
xv_set(frame, FRAME_LEFT_FOOTER, buf, NULL);
sprintf(buf, "COLS: %d", COLS–1);
xv_set(frame, FRAME_RIGHT_FOOTER, buf, NULL);
xv_main_loop(frame);
}
/*
* callback routine for the <print> panel button. Get the coordinates
* and the text to print on the tty subwindow and use curses library
* routines to render the text.
*/
void
output()
{
int X = (int)xv_get(x, PANEL_VALUE);
int Y = (int)xv_get(y, PANEL_VALUE);
char *Text = (char *)xv_get(text, PANEL_VALUE);
mvaddstr(Y, X, Text);
refresh();
}
9.5 TTY Subwindow Function Key Escape Sequences
XView provides a default .ttyswrc file for the shelltool which provides the SunView
escape key sequences for the following keys: L3, F1-F12, R1-R7, R9, R11, R13, and R15 (for
more details see $OPENWINHOME/lib/.ttyswrc). To override the default file, place any
.ttyswrc file in your $HOME directory. To avoid using any .ttyswrc file, place the follow-
ing line in your .Xdefaults file:
term.useAlternateTtyswrc: False
In addition, you will need to remove any .ttyswrc files from your $HOME directory. To spec-
ify a different alternate file than the one in $OPENWINHOME/lib/.ttyswrc, place the follow-
ing into your .Xdefaults file:
term.alternateTtyswrc:
filename
where filename is the path and name of the file to use. For example:
term.alternateTtyswrc: /usr/lib/ttyswrc
When using a Sun Type 4 keyboard with an X11 server other than OpenWindows 3.0, escape
key sequences cannot be produced for keys F11 and F12 due to a limitation in the standard
X11 keysym definition file. The base keysym file distributed by the X Consortium limits the
number of unique function keys to a maximum of 35 (for more details see
$OPENWINHOME/include/X11/keysym.h). The Sun Type 4 keyboard has 37 keysyms. For
OpenWindows, there are two extra keysyms Sun_F36 and Sun_F37 which allow XView and
other X-based programs to perform unique actions on the F11 and F12 keys (for more details
see $OPENWINHOME/include/X11/Sunkeysym.h).
TTY Subwindows
TTY Subwindows 249
9.6 TTY Package Summary
The TTYSW procedures are shown in Table 9-1. Table 9-2 lists the TTYSW attributes. This
information is described fully in the XView Reference Manual.
Table 9-1. TTY Subwindow Procedures
ttysw_input()
ttysw_output()
Table 9-2. TTY Subwindow Attributes
TTY_ARGV
TTY_ARGV_DO_NOT_FORK
TTY_CONSOLE
TTY_PAGE_MODE
TTY_PID
TTY_QUIT_ON_CHILD_DEATH
TTY_TTY_FD
WIN_FONT
WIN_SET_FOCUS
250 XView Programming Manual
Get Volume 7A: XView Programming Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.