NCURSES Programming HOWTO - Linux Documentation Project

Transcription

NCURSES Programming HOWTOPradeep Padala ppadala@gmail.com v1.9, 2005 06 20Revision HistoryRevision 1.92005 06 20Revised by: ppadalaThe license has been changed to the MIT style license used by NCURSES. Note that the programs are alsore licensed under this.Revision 1.82005 06 17Revised by: ppadalaLots of updates. Added references and perl examples. Changes to examples. Many grammatical and stylisticchanges to the content. Changes to NCURSES history.Revision 1.7.12002 06 25Revised by: ppadalaAdded a README file for building and instructions for building from source.Revision 1.72002 06 25Revised by: ppadalaAdded "Other formats" section and made a lot of fancy changes to the programs. Inlining of programs isgone.Revision 1.6.12002 02 24Revised by: ppadalaRemoved the old Changelog section, cleaned the makefilesRevision 1.62002 02 16Revised by: ppadalaCorrected a lot of spelling mistakes, added ACS variables sectionRevision 1.52002 01 05Revised by: ppadalaChanged structure to present proper TOCRevision 1.3.12001 07 26Revised by: ppadalaCorrected maintainers paragraph, Corrected stable release numberRevision 1.32001 07 24Revised by: ppadalaAdded copyright notices to main document (LDP license) and programs (GPL), Corrected printw example.Revision 1.22001 06 05Revised by: ppadalaIncorporated ravi's changes. Mainly to introduction, menu, form, justforfun sectionsRevision 1.12001 05 22Revised by: ppadalaAdded "a word about window" section, Added scanw example.This document is intended to be an "All in One" guide for programming with ncurses and its sister libraries.We graduate from a simple "Hello World" program to more complex form manipulation. No prior experiencein ncurses is assumed. Send comments to this address

NCURSES Programming HOWTOTable of Contents1. Introduction.11.1. What is NCURSES?.11.2. What we can do with NCURSES.11.3. Where to get it.21.4. Purpose/Scope of the document.21.5. About the Programs.21.6. Other Formats of the document.41.6.1. Readily available formats from tldp.org.41.6.2. Building from source.41.7. Credits.51.8. Wish List.51.9. Copyright.52. Hello World !!!.72.1. Compiling With the NCURSES Library.72.2. Dissection.72.2.1. About initscr().72.2.2. The mysterious refresh().82.2.3. About endwin().83. The Gory Details.94. Initialization.104.1. Initialization functions.104.2. raw() and cbreak().104.3. echo() and noecho().104.4. keypad().104.5. halfdelay().104.6. Miscellaneous Initialization functions.114.7. An Example.115. A Word about Windows.126. Output functions.136.1. addch() class of functions.136.2. mvaddch(), waddch() and mvwaddch().136.3. printw() class of functions.146.3.1. printw() and mvprintw.146.3.2. wprintw() and mvwprintw.146.3.3. vwprintw().146.3.4. A Simple printw example.146.4. addstr() class of functions.156.5. A word of caution.157. Input functions.167.1. getch() class of functions.167.2. scanw() class of functions.167.2.1. scanw() and mvscanw.16i

NCURSES Programming HOWTOTable of Contents7. Input functions7.2.2. wscanw() and mvwscanw().167.2.3. vwscanw().167.3. getstr() class of functions.167.4. Some examples.178. Attributes.188.1. The details.198.2. attron() vs attrset().198.3. attr get().208.4. attr functions.208.5. wattr functions.208.6. chgat() functions.209. Windows.229.1. The basics.229.2. Let there be a Window !!!.229.3. Explanation.249.4. The other stuff in the example.249.5. Other Border functions.2410. Colors.2710.1. The basics.2710.2. Changing Color Definitions.2810.3. Color Content.2811. Interfacing with the key board.2911.1. The Basics.2911.2. A Simple Key Usage example.2912. Interfacing with the mouse.3212.1. The Basics.3212.2. Getting the events.3212.3. Putting it all Together.3312.4. Miscellaneous Functions.3513. Screen Manipulation.3613.1. getyx() functions.3613.2. Screen Dumping.3613.3. Window Dumping.3614. Miscellaneous features.3714.1. curs set().3714.2. Temporarily Leaving Curses mode.3714.3. ACS variables.37ii

NCURSES Programming HOWTOTable of Contents15. Other libraries.3916. Panel Library.4016.1. The Basics.4016.2. Compiling With the Panels Library.4016.3. Panel Window Browsing.4116.4. Using User Pointers.4316.5. Moving and Resizing Panels.4416.6. Hiding and Showing Panels.4816.7. panel above() and panel below() Functions.5117. Menus Library.5217.1. The Basics.5217.2. Compiling With the Menu Library.5217.3. Menu Driver: The work horse of the menu system.5417.4. Menu Windows.5517.5. Scrolling Menus.5717.6. Multi Columnar Menus.6017.7. Multi Valued Menus.6217.8. Menu Options.6417.9. The useful User Pointer.6518. Forms Library.6818.1. The Basics.6818.2. Compiling With the Forms Library.6818.3. Playing with Fields.7018.3.1. Fetching Size and Location of Field.7018.3.2. Moving the field.7018.3.3. Field Justification.7018.3.4. Field Display Attributes.7118.3.5. Field Option Bits.7318.3.6. Field Status.7518.3.7. Field User Pointer.7618.3.8. Variable Sized Fields.7618.4. Form Windows.7718.5. Field Validation.79TYPE ALPHA.81TYPE ALNUM.82TYPE ENUM.83TYPE INTEGER.84TYPE NUMERIC.85iii

NCURSES Programming HOWTOTable of ContentsTYPE REGEXP.8618.6. Form Driver: The work horse of the forms system.8618.6.1. Page Navigation Requests.8618.6.2. Inter Field Navigation Requests.8618.6.3. Intra Field Navigation Requests.8718.6.4. Scrolling Requests.8818.6.5. Editing Requests.8818.6.6. Order Requests.8918.6.7. Application Commands.8919. Tools and Widget Libraries.9019.1. CDK (Curses Development Kit).9019.1.1. Widget List.9019.1.2. Some Attractive Features.9119.1.3. Conclusion.9119.2. The dialog.9119.3. Perl Curses Modules CURSES::FORM and CURSES::WIDGETS.9220. Just For Fun !!!.9320.1. The Game of Life.9320.2. Magic Square.9320.3. Towers of Hanoi.9320.4. Queens Puzzle.9320.5. Shuffle.9420.6. Typing Tutor.9421. References.95iv

1. IntroductionIn the olden days of teletype terminals, terminals were away from computers and were connected to themthrough serial cables. The terminals could be configured by sending a series of bytes. All the capabilities(such as moving the cursor to a new location, erasing part of the screen, scrolling the screen, changing modesetc.) of terminals could be accessed through these series of bytes. These control seeuqnces are usually calledescape sequences, because they start with an escape(0x1B) character. Even today, with proper emulation, wecan send escape sequences to the emulator and achieve the same effect on a terminal window.Suppose you wanted to print a line in color. Try typing this on your console.echo " [[0;31;40mIn Color"The first character is an escape character, which looks like two characters and [. To be able to print it, youhave to press CTRL V and then the ESC key. All the others are normal printable characters. You should beable to see the string "In Color" in red. It stays that way and to revert back to the original mode type this.echo " [[0;37;40m"Now, what do these magic characters mean? Difficult to comprehend? They might even be different fordifferent terminals. So the designers of UNIX invented a mechanism named termcap. It is a file that lists allthe capabilities of a particular terminal, along with the escape sequences needed to achieve a particular effect.In the later years, this was replaced by terminfo. Without delving too much into details, this mechanismallows application programs to query the terminfo database and obtain the control characters to be sent to aterminal or terminal emulator.1.1. What is NCURSES?You might be wondering, what the import of all this technical gibberish is. In the above scenario, everyapplication program is supposed to query the terminfo and perform the necessary stuff (sending controlcharacters etc.). It soon became difficult to manage this complexity and this gave birth to 'CURSES'. Curses isa pun on the name "cursor optimization". The Curses library forms a wrapper over working with raw terminalcodes, and provides highly flexible and efficient API (Application Programming Interface). It providesfunctions to move the cursor, create windows, produce colors, play with mouse etc. The application programsneed not worry about the underlying terminal capabilities.So what is NCURSES? NCURSES is a clone of the original System V Release 4.0 (SVr4) curses. It is a freelydistributable library, fully compatible with older version of curses. In short, it is a library of functions thatmanages an application's display on character cell terminals. In the remainder of the document, the termscurses and ncurses are used interchangeably.A detailed history of NCURSES can be found in the NEWS file from the source distribution. The currentpackage is maintained by Thomas Dickey. You can contact the maintainers at bug ncurses@gnu.org.1.2. What we can do with NCURSESNCURSES not only creates a wrapper over terminal capabilities, but also gives a robust framework to createnice looking UI (User Interface)s in text mode. It provides functions to create windows etc. Its sister librariespanel, menu and form provide an extension to the basic curses library. These libraries usually come alongwith curses. One can create applications that contain multiple windows, menus, panels and forms. Windows1. Introduction1

NCURSES Programming HOWTOcan be managed independently, can provide 'scrollability' and even can be hidden.Menus provide the user with an easy command selection option. Forms allow the creation of easy to use dataentry and display windows. Panels extend the capabilities of ncurses to deal with overlapping and stackedwindows.These are just some of the basic things we can do with ncurses. As we move along, We will see all thecapabilities of these libraries.1.3. Where to get itAll right, now that you know what you can do with ncurses, you must be rearing to get started. NCURSES isusually shipped with your installation. In case you don't have the library or want to compile it on your own,read on.Compiling the packageNCURSES can be obtained from ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz or any of the ftp sitesmentioned in http://www.gnu.org/order/ftp.html.Read the README and INSTALL files for details on to how to install it. It usually involves the followingoperations.tar zxvf ncurses version .tar.gzcd ncurses version ./configuremakesu rootmake install# unzip and untar the archive# cd to the directory# configure the build according to your# environment# make it# become root# install itUsing the RPMNCURSES RPM can be found and downloaded from http://rpmfind.net . The RPM can be installed with thefollowing command after becoming root.rpm i downloaded rpm 1.4. Purpose/Scope of the documentThis document is intended to be a "All in One" guide for programming with ncurses and its sister libraries.We graduate from a simple "Hello World" program to more complex form manipulation. No prior experiencein ncurses is assumed. The writing is informal, but a lot of detail is provided for each of the examples.1.5. About the ProgramsAll the programs in the document are available in zipped form here. Unzip and untar it. The directorystructure looks like this.ncurses 1. Introduction2

NCURSES Programming HOWTO JustForFun just for fun programsbasics basic programsdemo output files go into this directory after make exe exe files of all example programsforms programs related to form librarymenus programs related to menus librarypanels programs related to panels libraryperl perl equivalents of the examples (contributedby Anuradha Ratnaweera)Makefile the top level MakefileREADME the top level README file. contains instructionsCOPYING copyright noticeThe individual directories contain the following files.Description of files in each directory JustForFun hanoi.c The Towers of Hanoi Solver life.c The Game of Life demo magic.c An Odd Order Magic Square builder queens.c The famous N Queens Solver shuffle.c A fun game, if you have time to kill tt.c A very trivial typing tutorbasics acs vars.chello world.cinit func example.ckey code.cmouse menu.cother border.c printw example.cscanw example.csimple attr.c simple color.csimple key.c temp leave.cwin border.cwith chgat.c forms form attrib.cform options.cform simple.cform win.cmenus menu attrib.c menu item data.c menu multi column.cmenu scroll.c menu simple.c menu toggle.c 1. Introduction ACS variables exampleSimple "Hello World" ProgramInitialization functions exampleShows the scan code of the key pressedA menu accessible by mouseShows usage of other border functions apa rt from box()A very simple printw() exampleA very simple getstr() exampleA program that can print a c file with comments in attributeA simple example demonstrating colorsA menu accessible with keyboard UP, DOWN arrowsDemonstrates temporarily leaving curses modeShows Creation of windows and borderschgat() usage exampleUsage of field attributesUsage of field optionsA simple form exampleDemo of windows associated with formsUsage of menu attributesUsage o

NCURSES Programming HOWTO Pradeep Padala ppadala@gmail.com v1.9, 2005 06 20 Revision History Revision 1.9 2005 06 20 Revised by: ppadala The license has been changed to the MIT style license used by NCURSES. Note that the programs are also re licensed under this. Revision 1.8 2005 06 17 Revised by: ppadala Lots of updates.