4. Initialization

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();

4.1. Initialization functions

4.2. raw() and cbreak()

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
cbreak() 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.

4.3. echo() and noecho()

These functions control the echoing of characters typed by the user to the
terminal. noecho() 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
noecho() 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.

4.4. keypad()

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
keypad(stdscr, TRUE) to enable this feature
for the regular screen (stdscr). You will learn more about key management in
later sections of this document.

4.5. halfdelay()

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.

4.6. Miscellaneous Initialization functions

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.

4.7. An Example

Let's write a program which will clarify the usage of these functions.

Example 2. Initialization Function Usage example

#include <ncurses.h>
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;
}

This program is self-explanatory. But I used functions which aren't explained
yet. The function getch() is used to get a
character from user. It is equivalent to normal
getchar() except that we can disable the line
buffering to avoid <enter> after input. Look for more about
getch()and reading keys in the key management section . 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.