Understanding Unix Curses Programs

A primer on the theory behind curses programming

© Guy Lecky-Thompson

Nov 17, 2006
Learning curses programming need not be overly complex as long as the basics are understood. This article takes the reader through the theory behind curses programming.

The Unix curses library was designed to offer programmers the possibility to escape the usual line based programming that the shell offered. As such, many of the features associated with this level of control are present, including mouse and window handling.

Some programmers have great difficulty with curses programming, especially those who come from a pure DOS or Windows environment. The issues seem to center around how the curses library deals with color, windows, screen locations, standard output, and refreshing the display.

We shall deal with these in the following order:

  • Starting and stopping curses;
  • Directing output & refreshing the screen;
  • Screen co-ordinates;
  • Windows & Panels.

The aim is to provide detailed information about the concepts, before tackling actual code in future articles. This is, therefore, a theory primer, laying the groundwork for more detailed coded examples. If, as a programmer, the theory is not understood, it makes looking at code very difficult.

Starting & Stopping Curses

The curses library needs to be started and stopped - firstly to change the usual behavior associated with shell programs, and secondly to revert to the default behavior after the curses-enabled program has finished. It is also possible to move between the modes in the program itself, but this is not for novices.

The basic startup involves making sure that:

  • The screen is cleared
  • Line wrapping is turned off
  • Keyboard echo is disabled
  • Special keys are mapped to the input stream

This sequence prepares a blank screen, with all the usual handling associated with using stdio.h and other input libraries modified to make sure that they do not interfere with the input and output provided by curses.

Directing output & Refreshing

Generally speaking, output is directed to one of several streams which represent an area of screen. Should the programmer only need the one screen window, then a default stream is provided, and no further work is needed. Rather like using the standard input and output streams under regular C programming.

However, the screen is only updated explicitly at the command of the programmer. In addition, since echo is turned off, the programmer has to provide it. The advantage is simple : the curses library will only update 'dirty' areas of screen, i.e. ones that need updating, thus gaining a speed advantage.

Screen Co-Ordinates

Unlike some other co-ordinate systems, screen co-ordinates under curses are represented as [row,col] or [y,x]. The origin [0,0] is the top-left of the window. This window may or may not cover the whole screen.

Top tip : when programming with curses, always calculate available screen real estate from the sizes given by the system. This allows users of advanced windowing systems such as X Windows to resize their terminal windows, and for the programmer to take advantage of this.

Windows & Panels

Windows are areas of non-overlapping screen. Hence, if the programmer is to tile windows all over the display, then they can dispense with the standard windows created for them by curses. Some implementations offer the panels library to provide overlapping windows, to avoid the programmer having to create a system for doing so themselves.

However, it is far from perfect, and some programmers simply prefer to roll their own system, or build upon the panels library.

Summary

Curses programming is not terribly hard once one understands the premises mentioned here. The best way to learn, as always, is to read through other peoples code and try to understand it.

For example - this code shows how to draw a box on the screen in C; with both conio and curses.


The copyright of the article Understanding Unix Curses Programs in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Understanding Unix Curses Programs in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo