old-www/HOWTO/NCURSES-Programming-HOWTO/init.html

344 lines
7.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Initialization </TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE=" NCURSES Programming HOWTO "
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="The Gory Details "
HREF="gory.html"><LINK
REL="NEXT"
TITLE="A Word about Windows"
HREF="awordwindows.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>NCURSES Programming HOWTO</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="gory.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="awordwindows.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="INIT"
></A
>4. Initialization</H1
><P
>We now know that to initialize curses system the function initscr() has to be
called. There are functions which can be called after this initialization to
customize our curses session. We may ask the curses system to set the terminal
in raw mode or initialize color or initialize the mouse etc.. Let's discuss some
of the functions that are normally called immediately after initscr();</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="ABOUTINIT"
></A
>4.1. Initialization functions</H2
><P
> </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="RAWCBREAK"
></A
>4.2. raw() and cbreak()</H2
><P
>Normally the terminal driver buffers the characters a user types until a new
line or carriage return is encountered. But most programs require that the
characters be available as soon as the user types them. The above two functions
are used to disable line buffering. The difference between these two functions
is in the way control characters like suspend (CTRL-Z), interrupt and quit
(CTRL-C) are passed to the program. In the raw() mode these characters are
directly passed to the program without generating a signal. In the
<TT
CLASS="LITERAL"
>cbreak()</TT
> mode these control characters are
interpreted as any other character by the terminal driver. I personally prefer
to use raw() as I can exercise greater control over what the user does.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="ECHONOECHO"
></A
>4.3. echo() and noecho()</H2
><P
>
These functions control the echoing of characters typed by the user to the
terminal. <TT
CLASS="LITERAL"
>noecho()</TT
> switches off echoing. The
reason you might want to do this is to gain more control over echoing or to
suppress unnecessary echoing while taking input from the user through the
getch() etc. functions. Most of the interactive programs call
<TT
CLASS="LITERAL"
>noecho()</TT
> at initialization and do the echoing
of characters in a controlled manner. It gives the programmer the flexibility
of echoing characters at any place in the window without updating current (y,x)
co-ordinates. </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="KEYPAD"
></A
>4.4. keypad()</H2
><P
>This is my favorite initialization function. It enables the reading of function
keys like F1, F2, arrow keys etc. Almost every interactive program enables this,
as arrow keys are a major part of any User Interface. Do
<TT
CLASS="LITERAL"
>keypad(stdscr, TRUE) </TT
> to enable this feature
for the regular screen (stdscr). You will learn more about key management in
later sections of this document.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="HALFDELAY"
></A
>4.5. halfdelay()</H2
><P
>This function, though not used very often, is a useful one at times.
halfdelay()is called to enable the half-delay mode, which is similar to the
cbreak() mode in that characters typed are immediately available to program.
However, it waits for 'X' tenths of a second for input and then returns ERR, if
no input is available. 'X' is the timeout value passed to the function
halfdelay(). This function is useful when you want to ask the user for input,
and if he doesn't respond with in certain time, we can do some thing else. One
possible example is a timeout at the password prompt. </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="MISCINIT"
></A
>4.6. Miscellaneous Initialization functions</H2
><P
>There are few more functions which are called at initialization to
customize curses behavior. They are not used as extensively as those mentioned
above. Some of them are explained where appropriate.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="INITEX"
></A
>4.7. An Example</H2
><P
>Let's write a program which will clarify the usage of these functions.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="BINFU"
></A
><P
><B
>Example 2. Initialization Function Usage example </B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
><SPAN
CLASS="INLINEMEDIAOBJECT"
>#include &#60;ncurses.h&#62;
int main()
{ int ch;
initscr(); /* Start curses mode */
raw(); /* Line buffering disabled */
keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
noecho(); /* Don't echo() while we do getch */
printw("Type any character to see it in bold\n");
ch = getch(); /* If raw() hadn't been called
* we have to press enter before it
* gets to the program */
if(ch == KEY_F(1)) /* Without keypad enabled this will */
printw("F1 Key pressed");/* not get to us either */
/* Without noecho() some ugly escape
* charachters might have been printed
* on screen */
else
{ printw("The pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
}
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}</SPAN
></PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
>This program is self-explanatory. But I used functions which aren't explained
yet. The function <TT
CLASS="LITERAL"
>getch()</TT
> is used to get a
character from user. It is equivalent to normal
<TT
CLASS="LITERAL"
>getchar()</TT
> except that we can disable the line
buffering to avoid &#60;enter&#62; after input. Look for more about
<TT
CLASS="LITERAL"
>getch()</TT
>and reading keys in the <A
HREF="keys.html"
> key management section </A
>. The functions attron and attroff
are used to switch some attributes on and off respectively. In the example I
used them to print the character in bold. These functions are explained in detail
later.</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="gory.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="awordwindows.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>The Gory Details</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>A Word about Windows</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>