Sunday, December 30, 2012

Getting started with ncurses programs

I will be explaining some interesting programs using ncurses library from this post onward. This will actually give you an interesting experience since ncurses programming is a little different from the traditional structured or object oriented programming.

ncurses is a programming library that provides an API which allows the programmer to write text-base user interfaces in a terminal-independent manner. It is a toolkit for developing GUI like applications that runs under a terminal emulator. This provides manipulation of the terminal accurately such as clearing the screen, getting a single character from the user, positioning text at a certain screen location, changing colors etc. Though ncurses is a UNIX library, it can also be ported to other platforms including DOS. 

I will explain you how to work with ncurses programs, compile and run them. Before starting just type the below code in a new text editor, compile as shown below. If you are having problems, then you have to make sure that you are having the ncurses library installed in your linux OS.

open a new turminal and type type vi ncurse1.cpp  to open a text editor.
then, type the below shown code

#include<ncurses.h>
int main()
{
initscr();
move(15,25);
printw("First Ncurses Program\n");
getch();
endwin();
return 0;
}

press Esc key and then type :wq to save the program and quit the text editor. then type the below code to compile the program

g++ -lncurses -oncurse1 ncurse1.cpp

If your program compiled correctly, you should not get any error messages. If the compiler complains about any syntax errors, then you have to fix them one by one. but of you get a message as shown in the below image, 









then, your OS may not have the ncurses library installed yet. Which means you cannot compile and run ncurses library in your existing system . But don't worry, as same as you install some software in windows platform, you can also download and install any missing software in linux via the terminal. 

How to install ncurses library in linux?

As same as you need administrative privileges in windows (or in most of the OSs) , you must be in the root directory to install or uninstall software in Linux. Note that being in root directory can be only done by a user with administrative privileges because to be in root, you must know the password that you provided during the installation of the OS. OK, let's start with it.

  1. open a new terminal ( if you don't have one opened yet)
  2. type su - and then press Enter key
  3. type your password (the one that you gave during the OS installation) and press Enter key
  4. type yum install ncurses-devel ncurses and press Enter key to install ncurses library .but make sure that you are connected to internet since the library will be automatically downloaded from the internet and install.
  5. your installation process will automatically start as shown in the below image
     6. once it asks for the conformation to download, enter y and then press Enter key as shown below


    7. once it asks for the conformation to install, enter and then press Enter key as shown below


8. If the installation is successful, then the terminal will display the completed message as shown below. then you are ready to work with ncurses programs.



to come out of the root directory, type exit and pres Enter. 
If you had problems in executing the ncurse1.cpp program earlier, I think  now you can run it smoothly. 

If you follow the earlier instructions to compile your program and run it, you will observe the below output.


to exit from the program and get back to the terminal, press any key from the keyboard.

If you are a beginner for ncurses, you may feel little odd with these codes. So, let me explain you why we used each code and how it works as a whole.
  • In order to use the libary, it is necessay to have certain types and variables defined. Therefore, the programmer must have a line in top of the program source as,
            #include<ncurses.h>
  • In linking with ncurses you need to have -lncurses in your LDFLAGS or on the terminal. This is what you mentioned -lncurses when you compile the program before mentioning the name of the program file and so.
  • inside the main method, you can see initscr()  method is called. This is done done initialize the curses library. In order to use the screen package, the routines must know about trminal characteristics and the space for curscr and stdscr must be allocated. These function initscr() does both these things 
  • Once the screen windows have been allocated, you can set them up for your program. here, what I need is to move the curser to a preferred location in the screen and print the message. move (x,y) method is called to do that task. two of those parameters say the x and y coordinates where we actually want the curser to move.
  • I think the task of printw()  method is similar to cout  that is to print something on the screen. As we have mentioned a simple message to print on screen, we have to wrap that within double quotation marks.
  • \n  at the end of the string message is similar to endl  which we need to go to a new line
  • Once you run an ncurses program, you will see the output on in the running terminal itself but in a special screen. Did you think how we are going to exit from that screen? (by pressing the close button in the right  side upper corner? ... No, since it will close the terminal also). getch() is the method that we are using for that. It says, if the user enters a key while the screen is running (get character) , then ready to exit.
  • In order to clean up after the ncurses routines, the routine endwin() is provided. It restores tty modes to what they were when initscr()  was first called and moves the cursor down to the lower-left corner. Thus, anytime after the call to initscr(), endwin() should be called before exiting. 
I'll discuss more interesting programs in ncurses in the future posts.



No comments:

Post a Comment