📄 ncurses-intro.html
字号:
<H3><A NAME="attributes">Character Attributes and Color</A></H3>The <CODE>ncurses</CODE> package supports screen highlights including standout,reverse-video, underline, and blink. It also supports color, which is treatedas another kind of highlight. <P>Highlights are encoded, internally, as high bits of the pseudo-character type(<CODE>chtype</CODE>) that <CODE>curses.h</CODE> uses to represent the contents of ascreen cell. See the <CODE>curses.h</CODE> header file for a complete list ofhighlight mask values (look for the prefix <CODE>A_</CODE>).<P>There are two ways to make highlights. One is to logical-or the value of thehighlights you want into the character argument of an <CODE>addch()</CODE> call,or any other output call that takes a <CODE>chtype</CODE> argument. <P>The other is to set the current-highlight value. This is logical-or'ed withany highlight you specify the first way. You do this with the functions<CODE>attron()</CODE>, <CODE>attroff()</CODE>, and <CODE>attrset()</CODE>; see the manualpages for details.Color is a special kind of highlight. The package actually thinks in termsof color pairs, combinations of foreground and background colors. The samplecode above sets up eight color pairs, all of the guaranteed-available colorson black. Note that each color pair is, in effect, given the name of itsforeground color. Any other range of eight non-conflicting values couldhave been used as the first arguments of the <CODE>init_pair()</CODE> values. <P>Once you've done an <CODE>init_pair()</CODE> that creates color-pair N, you canuse <CODE>COLOR_PAIR(N)</CODE> as a highlight that invokes that particularcolor combination. Note that <CODE>COLOR_PAIR(N)</CODE>, for constant N,is itself a compile-time constant and can be used in initializers.<H3><A NAME="mouse">Mouse Interfacing</A></H3>The <CODE>ncurses</CODE> library also provides a mouse interface.<!-- The 'note' tag is not portable enough --><blockquote><strong>NOTE:</strong> this facility is specific to <CODE>ncurses</CODE>, it is not part of eitherthe XSI Curses standard, nor of System V Release 4, nor BSD curses.System V Release 4 curses contains code with similar interface definitions,however it is not documented. Other than by disassembling the library, wehave no way to determine exactly how that mouse code works.Thus, we recommend that you wrap mouse-related code in an #ifdef using thefeature macro NCURSES_MOUSE_VERSION so it will not be compiled and linkedon non-ncurses systems.</blockquote>Presently, mouse event reporting works in the following environments:<ul><li>xterm and similar programs such as rxvt.<li>Linux console, when configured with <CODE>gpm</CODE>(1), AlessandroRubini's mouse server.<li>FreeBSD sysmouse (console)<li>OS/2 EMX</ul><P>The mouse interface is very simple. To activate it, you use the function<CODE>mousemask()</CODE>, passing it as first argument a bit-mask that specifieswhat kinds of events you want your program to be able to see. It willreturn the bit-mask of events that actually become visible, which may differfrom the argument if the mouse device is not capable of reporting some ofthe event types you specify. <P>Once the mouse is active, your application's command loop should watchfor a return value of <CODE>KEY_MOUSE</CODE> from <CODE>wgetch()</CODE>. Whenyou see this, a mouse event report has been queued. To pick it offthe queue, use the function <CODE>getmouse()</CODE> (you must do this beforethe next <CODE>wgetch()</CODE>, otherwise another mouse event might comein and make the first one inaccessible). <P>Each call to <CODE>getmouse()</CODE> fills a structure (the address of which you'llpass it) with mouse event data. The event data includes zero-origin,screen-relative character-cell coordinates of the mouse pointer. It alsoincludes an event mask. Bits in this mask will be set, correspondingto the event type being reported. <P>The mouse structure contains two additional fields which may besignificant in the future as ncurses interfaces to new kinds ofpointing device. In addition to x and y coordinates, there is a slotfor a z coordinate; this might be useful with touch-screens that canreturn a pressure or duration parameter. There is also a device IDfield, which could be used to distinguish between multiple pointingdevices. <P>The class of visible events may be changed at any time via <CODE>mousemask()</CODE>.Events that can be reported include presses, releases, single-, double- andtriple-clicks (you can set the maximum button-down time for clicks). Ifyou don't make clicks visible, they will be reported as press-releasepairs. In some environments, the event mask may include bits reportingthe state of shift, alt, and ctrl keys on the keyboard during the event. <P>A function to check whether a mouse event fell within a given window isalso supplied. You can use this to see whether a given window shouldconsider a mouse event relevant to it. <P>Because mouse event reporting will not be available in allenvironments, it would be unwise to build <CODE>ncurses</CODE>applications that <EM>require</EM> the use of a mouse. Rather, you shoulduse the mouse as a shortcut for point-and-shoot commands your applicationwould normally accept from the keyboard. Two of the test games in the<CODE>ncurses</CODE> distribution (<CODE>bs</CODE> and <CODE>knight</CODE>) containcode that illustrates how this can be done. <P>See the manual page <CODE>curs_mouse(3X)</CODE> for full details of themouse-interface functions.<H3><A NAME="finishing">Finishing Up</A></H3>In order to clean up after the <CODE>ncurses</CODE> routines, the routine<CODE>endwin()</CODE> is provided. It restores tty modes to what they were when<CODE>initscr()</CODE> was first called, and moves the cursor down to thelower-left corner. Thus, anytime after the call to initscr, <CODE>endwin()</CODE>should be called before exiting.<H2><A NAME="functions">Function Descriptions</A></H2>We describe the detailed behavior of some important curses functions here, as asupplement to the manual page descriptions.<H3><A NAME="init">Initialization and Wrapup</A></H3><DL><DT> <CODE>initscr()</CODE><DD> The first function called should almost always be <CODE>initscr()</CODE>.This will determine the terminal type andinitialize curses data structures. <CODE>initscr()</CODE> also arranges thatthe first call to <CODE>refresh()</CODE> will clear the screen. If an erroroccurs a message is written to standard error and the programexits. Otherwise it returns a pointer to stdscr. A few functions may becalled before initscr (<CODE>slk_init()</CODE>, <CODE>filter()</CODE>,<CODE>ripofflines()</CODE>, <CODE>use_env()</CODE>, and, if you are using multipleterminals, <CODE>newterm()</CODE>.)<DT> <CODE>endwin()</CODE><DD> Your program should always call <CODE>endwin()</CODE> before exiting orshelling out of the program. This function will restore tty modes,move the cursor to the lower left corner of the screen, reset theterminal into the proper non-visual mode. Calling <CODE>refresh()</CODE>or <CODE>doupdate()</CODE> after a temporary escape from the program willrestore the ncurses screen from before the escape.<DT> <CODE>newterm(type, ofp, ifp)</CODE><DD> A program which outputs to more than one terminal should use<CODE>newterm()</CODE> instead of <CODE>initscr()</CODE>. <CODE>newterm()</CODE> shouldbe called once for each terminal. It returns a variable of type<CODE>SCREEN *</CODE> which should be saved as a reference to thatterminal.(NOTE: a SCREEN variable is not a <em>screen</em> in the sense weare describing in this introduction, but a collection of parameters used to assist in optimizing the display.)The arguments are the type of the terminal (a string) and<CODE>FILE</CODE> pointers for the output and input of the terminal. Iftype is NULL then the environment variable <CODE>$TERM</CODE> is used.<CODE>endwin()</CODE> should called once at wrapup time for each terminalopened using this function.<DT> <CODE>set_term(new)</CODE><DD> This function is used to switch to a different terminal previouslyopened by <CODE>newterm()</CODE>. The screen reference for the new terminalis passed as the parameter. The previous terminal is returned by thefunction. All other calls affect only the current terminal.<DT> <CODE>delscreen(sp)</CODE><DD> The inverse of <CODE>newterm()</CODE>; deallocates the data structuresassociated with a given <CODE>SCREEN</CODE> reference.</DL><H3><A NAME="flush">Causing Output to the Terminal</A></H3><DL><DT> <CODE>refresh()</CODE> and <CODE>wrefresh(win)</CODE><DD> These functions must be called to actually get any output onthe terminal, as other routines merely manipulate datastructures. <CODE>wrefresh()</CODE> copies the named window to the physicalterminal screen, taking into account what is alreadythere in order to do optimizations. <CODE>refresh()</CODE> does arefresh of <CODE>stdscr()</CODE>. Unless <CODE>leaveok()</CODE> has beenenabled, the physical cursor of the terminal is left at thelocation of the window's cursor.<DT> <CODE>doupdate()</CODE> and <CODE>wnoutrefresh(win)</CODE><DD> These two functions allow multiple updates with more efficiencythan wrefresh. To use them, it is important to understand how cursesworks. In addition to all the window structures, curses keeps twodata structures representing the terminal screen: a physical screen,describing what is actually on the screen, and a virtual screen,describing what the programmer wants to have on the screen. wrefreshworks by first copying the named window to the virtual screen(<CODE>wnoutrefresh()</CODE>), and then calling the routine to update thescreen (<CODE>doupdate()</CODE>). If the programmer wishes to outputseveral windows at once, a series of calls to <CODE>wrefresh</CODE> will resultin alternating calls to <CODE>wnoutrefresh()</CODE> and <CODE>doupdate()</CODE>,causing several bursts of output to the screen. By calling<CODE>wnoutrefresh()</CODE> for each window, it is then possible to call<CODE>doupdate()</CODE> once, resulting in only one burst of output, withfewer total characters transmitted (this also avoids a visually annoyingflicker at each update).</DL><H3><A NAME="lowlevel">Low-Level Capability Access</A></H3><DL><DT> <CODE>setupterm(term, filenum, errret)</CODE><DD> This routine is called to initialize a terminal's description, without settingup the curses screen structures or changing the tty-driver mode bits.<CODE>term</CODE> is the character string representing the name of the terminalbeing used. <CODE>filenum</CODE> is the UNIX file descriptor of the terminal tobe used for output. <CODE>errret</CODE> is a pointer to an integer, in which asuccess or failure indication is returned. The values returned can be 1 (allis well), 0 (no such terminal), or -1 (some problem locating the terminfodatabase). <P>The value of <CODE>term</CODE> can be given as NULL, which will cause the value of<CODE>TERM</CODE> in the environment to be used. The <CODE>errret</CODE> pointer canalso be given as NULL, meaning no error code is wanted. If <CODE>errret</CODE> isdefaulted, and something goes wrong, <CODE>setupterm()</CODE> will print anappropriate error message and exit, rather than returning. Thus, a simpleprogram can call setupterm(0, 1, 0) and not worry about initializationerrors. <P>After the call to <CODE>setupterm()</CODE>, the global variable <CODE>cur_term</CODE> isset to point to the current structure of terminal capabilities. By calling<CODE>setupterm()</CODE> for each terminal, and saving and restoring<CODE>cur_term</CODE>, it is possible for a program to use two or more terminals atonce. <CODE>Setupterm()</CODE> also stores the names section of the terminaldescription in the global character array <CODE>ttytype[]</CODE>. Subsequent callsto <CODE>setupterm()</CODE> will overwrite this array, so you'll have to save ityourself if need be.</DL><H3><A NAME="debugging">Debugging</A></H3><!-- The 'note' tag is not portable enough --><blockquote><strong>NOTE:</strong> These functions are not part of the standard curses API!</blockquote><DL><DT> <CODE>trace()</CODE><DD>This function can be used to explicitly set a trace level. If thetrace level is nonzero, execution of your program will generate a filecalled `trace' in the current working directory containing a report onthe library's actions. Higher trace levels enable more detailed (andverbose) reporting -- see comments attached to <CODE>TRACE_</CODE> definesin the <CODE>curses.h</CODE> file for details. (It is also possible to seta trace level by assigning a trace level value to the environment variable<CODE>NCURSES_TRACE</CODE>).<DT> <CODE>_tracef()</CODE><DD>This function can be used to output your own debugging information. It is onlyavailable only if you link with -lncurses_g. It can be used the same way as<CODE>printf()</CODE>, only it outputs a newline after the end of arguments.The output goes to a file called <CODE>trace</CODE> in the current directory.</DL>Trace logs can be difficult to interpret due to the sheer volume ofdata dumped in them. There is a script called <STRONG>tracemunch</STRONG>included with the <CODE>ncurses</CODE> distribution that can alleviatethis problem somewhat; it compacts long sequences of similar operations intomore succinct single-line pseudo-operations. These pseudo-ops can bedistinguished by the fact that they are named in capital letters.<H2><A NAME="hints">Hints, Tips, and Tricks</A></H2>The <CODE>ncurses</CODE> manual pages are a complete reference for this library.In the remainder of this document, we discuss various useful methods thatmay not be obvious from the manual page descriptions.<H3><A NAME="caution">Some Notes of Caution</A></H3>If you find yourself thinking you need to use <CODE>noraw()</CODE> or<CODE>nocbreak()</CODE>, think again and move carefully. It's probablybetter design to use <CODE>getstr()</CODE> or one of its relatives tosimulate cooked mode. The <CODE>noraw()</CODE> and <CODE>nocbreak()</CODE>functions try to restore cooked mode, but they may end up clobberingsome control bits set before you started your application. Also, theyhave always been poorly documented, and are likely to hurt yourapplication's usability with other curses libraries. <P>Bear in mind that <CODE>refresh()</CODE> is a synonym for <CODE>wrefresh(stdscr)</CODE>.Don't try to mix use of <CODE>stdscr</CODE> with use of windows declaredby <CODE>newwin()</CODE>; a <CODE>refresh()</CODE> call will blow them off the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -