|
|
1.1 root 1:
2:
3: curses Overview curses
4:
5:
6:
7:
8: Library of screen-handling functions
9:
10:
11: curses is a set of routines that allow you to manipulate the
12: screen in a sophisticated manner. These routines use the tteerrmmccaapp
13: functions to read information about the user's terminal. This
14: allows you to write programs that can perform rudimentary
15: graphics on a wide variety of terminals.
16:
17: curses contains routines that do the following:
18:
19: * Move the cursor about the screen.
20:
21: * Insert text onto the screen, either in normal or reverse video
22: (if supported by the display device).
23:
24: * Read what is typed by the user and display it properly.
25:
26: * Organize the screen into one or more rectangular regions, or
27: _w_i_n_d_o_w_s, optionally draw a border around each, and manage each
28: independently.
29:
30: curses organizes the screen into a two-dimensional array of
31: cells, one cell for every character that the device can display.
32: It maintains in memory an image of the screen, called the curscr.
33: A second image, called the stdcur, is manipulated by the user;
34: when the user has finished a given manipulation, curses copies
35: the changes from the stdcur to the curscr, which results in their
36: being displayed on the physical screen. This act of copying from
37: the stdscr to the curscr is called refreshing the screen. curses
38: keeps track of where all changes have begun and ended between one
39: refresh and the next; this lets it rewrite only the portions of
40: the curscr that the user has changed, and so speed up rewriting
41: of the screen.
42:
43: curses records the position of a ``logical cursor'', which points
44: to the position in the stdscr that is being manipulated by the
45: user, and also records the position of the physical cursor. Note
46: that the two are not necessarily identical: it is possible to
47: manipulate the logical cursor without repositioning the physical
48: cursor, and vice versa, depending on the task you wish to per-
49: form.
50:
51: Most curses routines work by manipulating WINDOW object. WINDOW
52: is defined in the header curses.h as follows:
53:
54:
55: #define WINDOW _win_st
56: struct _win_st {
57: short _cury, _curx;
58: short _maxy, _maxx;
59: short _begy, _begx;
60: short _flags;
61: short _ch_off;
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: curses Overview curses
70:
71:
72:
73: bool _clear;
74: bool _leave;
75: bool _scroll;
76: char **_y;
77: short *_firstch;
78: short *_lastch;
79: struct _win_st *_nextp, *_orig;
80: };
81:
82:
83: Type bool is defined in curses.h; an object of this type can hold
84: the value of true (nonzero) or false (zero).
85:
86: The following describes each WINDOW field in detail.
87:
88: _ccuurryy, _ccuurrxx
89: Give the Y and X positions of the logical cursor. The
90: upper left corner of the window is, by definition,
91: position 0,0. Note that curses by convention gives
92: positions as Y/X (column/row) rather than X/Y, as is
93: usual elsewhere.
94:
95: _mmaaxxyy, _mmaaxxxx
96: Width and height of the window.
97:
98: _bbeeggyy, _bbeeggxx
99: Position of the upper left corner of the window rela-
100: tive to the upper left corner of the physical screen.
101: For example, if the window's upper left corner is five
102: rows from the top of the screen and ten columns from
103: the left, then _begy and _begx will be set to ten and
104: five, respectively.
105:
106: _ffllaaggss One or more of the following flags, logically OR'd
107: together:
108:
109: _SSUUBBWWIINN -- Window is a sub-window
110: _EENNDDLLIINNEE -- Right edge of window touches edge of the screen
111: _FFUULLLLWWIINN -- Window fills the physical screen
112: _SSCCRROOLLLLWWIINN -- Window touches lower right corner of physical screen
113: _FFUULLLLIINNEE -- Window extends across entire physical screen
114: _SSTTAANNDDOOUUTT -- Write text in reverse video
115: _IINNSSLL -- Line has been inserted into window
116: _DDEELLLL -- Line has been deleted from window
117:
118:
119: _cchh_ooffff Character offset.
120:
121: _cclleeaarr Clear the physical screen before next refresh of the
122: screen.
123:
124: _lleeaavvee Do not move the physical cursor after refreshing the
125: screen.
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: curses Overview curses
136:
137:
138:
139: _ssccrroollll Enable scrolling for this window.
140:
141: _yy Pointer to an array of pointers to the character arrays
142: that hold the window's text.
143:
144: _ffiirrssttcchh Pointer to an array of integers, one for each line in
145: the window, whose value is the first character in the
146: line to have been altered by the user. If a line has
147: not been changed, then its corresponding entry in the
148: array is set to _NNOOCCHHAANNGGEE.
149:
150: _llaassttcchh Same as _ffiirrssttcchh, except that it indicates the last
151: character to have been changed on the line.
152:
153: _nneexxttpp Point to next window.
154:
155: _oorriigg Point to parent window.
156:
157: When curses is first invoked, it defines the entire screen as
158: being one large window. The programmer has the choice of sub-
159: dividing an existing window or creating new windows; when a win-
160: dow is subdivided, it shares the same curscr as its parent win-
161: dow, whereas a new window has its own stdscr.
162:
163: Mark Williams Company will document its curses library in full in
164: a later release of this manual. The following table, however,
165: summarizes the functions and macros that that compose the curses
166: library.
167:
168: aaddddcchh(_c_h) cchhaarr _c_h;
169: Insert a character into stdscr.
170:
171: aaddddssttrr(_s_t_r) cchhaarr *_s_t_r;
172: Insert a string into stdscr.
173:
174: bbooxx(_w_i_n, _v_e_r_t, _h_o_r) WWIINNDDOOWW *_w_i_n; cchhaarr _v_e_r_t, _h_o_r;
175: Draw a box. vert is the character used to draw the vertical
176: lines, and hor is used to draw the horizontal lines. For
177: example
178:
179: box(win, '|', '-');
180:
181: draws a box around window win, using `|' to draw the verti-
182: cal lines and `-' to draw the horizontal lines.
183:
184: cclleeaarr()
185: Clear the stdscr.
186:
187: cclleeaarrookk(_w_i_n,_b_f) WWIINNDDOOWW *_w_i_n; bbooooll _b_f;
188: Set the clear flag for window win. This will clear the
189: screen at the next refresh, but not reset the window.
190:
191: ccllrrttoobboott()
192: Clear from the position of the logical cursor to the bottom
193: of the window.
194:
195:
196: COHERENT Lexicon Page 3
197:
198:
199:
200:
201: curses Overview curses
202:
203:
204:
205:
206: ccllrrttooeeooll()
207: Clear from the logical cursor to the end of the line.
208:
209: ccrrmmooddee()
210: Turn on control-character mode; i.e., force terminal to
211: receive cooked input.
212:
213: ddeellcchh()
214: Delete a character from stdscr; shift the rest of the
215: characters on the line one position to the left.
216:
217: ddeelleetteellnn()
218: Delete all of the current line; shift up the rest of the
219: lines in the window.
220:
221: ddeellwwiinn(_w_i_n) WWIINNDDOOWW *_w_i_n;
222: Delete window win.
223:
224: eecchhoo()
225: Turn on both physical and logical echoing; i.e., character
226: are automatically inserted into the current window and onto
227: the physical screen.
228:
229: eennddwwiinn()
230: Terminate text processing with curses.
231:
232: eerraassee()
233: Erase a window; do not clear the screen.
234:
235: ggeettcchh()
236: Read a character from the terminal.
237:
238: ggeettssttrr(_s_t_r) cchhaarr *_s_t_r;
239: Read a string from the terminal.
240:
241: ggeettyyxx(_w_i_n,_y,_x) WWIINNDDOOWW *_w_i_n; sshhoorrtt _y,_x;
242: Read the position of the logical cursor in win and store it
243: in y,x. Note that this is a macro, and due to its construc-
244: tion the variables y and x must be integers, not pointers to
245: integers.
246:
247: iinncchh()
248: Read the character pointed to by the stdscr's logical cur-
249: sor.
250:
251: WWIINNDDOOWW *iinniittssccrr()
252: Initialize curses.
253:
254: iinnsscchh(_c_h) cchhaarr _c_h;
255: Insert character ch into the stdscr.
256:
257: iinnsseerrttllnn()
258: Insert a blank line into stdscr, above the current line.
259:
260:
261:
262: COHERENT Lexicon Page 4
263:
264:
265:
266:
267: curses Overview curses
268:
269:
270:
271: lleeaavveeookk(_w_i_n,_b_f) WWIINNDDOOWW *_w_i_n; bbooooll _b_f;
272: Set _leave in win to bf.
273:
274: cchhaarr *lloonnggnnaammee(_t_e_r_m_b_u_f, _n_a_m_e) cchhaarr *_t_e_r_m_b_u_f, *_n_a_m_e;
275: Copy the long name for the terminal from termbuf into name.
276:
277: mmoovvee(_y,_x) sshhoorrtt _y,_x;
278: Move logical cursor to position y,x in stdscr.
279:
280: mmvvaaddddbbyytteess(_y,_x,_d_a,_c_o_u_n_t) iinntt _y,_x; cchhaarr *_d_a; iinntt _c_o_u_n_t;
281: Move to position y,x and print count bytes from the string
282: pointed to by da.
283:
284: mmvvaaddddcchh(_y,_x,_c_h) sshhoorrtt _y,_x; cchhaarr _c_h;
285: Move the logical cursor to position y,x and insert character
286: ch.
287:
288: mmvvaaddddssttrr(_y,_x,_s_t_r) sshhoorrtt _y,_x; cchhaarr *_s_t_r;
289: Move the logical cursor to position y,x and insert string
290: str.
291:
292: mmvvccuurr(_y__c_u_r,_x__c_u_r,_y__n_e_w,_x__n_e_w) iinntt _y__c_u_r, _x__c_u_r, _y__n_e_w, _x__n_e_w;
293: Move cursor from position y_cur,x_cur to position
294: y_new,x_new.
295:
296: mmvvddeellcchh(_y,_x) sshhoorrtt _y,_x;
297: Move to position y,x and delete the character found there.
298:
299: mmvvggeettcchh(_y,_x) sshhoorrtt _y,_x;
300: Move to position y,x and get a character through stdscr.
301:
302: mmvvggeettssttrr(_y,_x,_s_t_r) sshhoorrtt _y,_x; cchhaarr *_s_t_r;
303: Move to position y,x, get a string through stdscr, and copy
304: it into string.
305:
306: mmvviinncchh(_y,_x) sshhoorrtt _y,_x;
307: Move to position y,x and get the character found there.
308:
309: mmvviinnsscchh(_y,_x,_c_h) sshhoorrtt _y,_x; cchhaarr _c_h;
310: Move to position y,x and insert a character into stdscr.
311:
312: mmvvwwaaddddbbyytteess(_w_i_n,_y,_x,_d_a,_c_o_u_n_t) WWIINNDDOOWW *_w_i_n; iinntt _y,_x; cchhaarr *_d_a; iinntt
313: _c_o_u_n_t;
314: Move to position y,x and print count bytes from the string
315: pointed to by da into window win.
316:
317: mmvvwwaaddddcchh(_w_i_n,_y,_x,_c_h) WWIINNDDOOWW *_w_i_n; iinntt _y,_x; cchhaarr _c_h;
318: Move to position y,x and insert character ch into window
319: win.
320:
321: mmvvwwaaddddssttrr(_w_i_n,_y,_x,_s_t_r) WWIINNDDOOWW *_w_i_n; sshhoorrtt _y,_x; cchhaarr *_s_t_r;
322: Move to position y,x and insert character ch.
323:
324: mmvvwwddeellcchh(_w_i_n,_y,_x) WWIINNDDOOWW *_w_i_n; iinntt _y,_x;
325: Move to position y,x and delete character ch from window
326:
327:
328: COHERENT Lexicon Page 5
329:
330:
331:
332:
333: curses Overview curses
334:
335:
336:
337: win.
338:
339: mmvvwwggeettcchh(_w_i_n,_y,_x) WWIINNDDOOWW *_w_i_n; sshhoorrtt _y,_x;
340: Move to position y,x and get a character.
341:
342: mmvvwwggeettssttrr(_w_i_n,_y,_x,_s_t_r) WWIINNDDOOWW *_w_i_n; sshhoorrtt _y,_x; cchhaarr *_s_t_r;
343: Move to position y,x, get a string, and write it into str.
344:
345: mmvvwwiinn(_w_i_n,_y,_x) WWIINNDDOOWW *_w_i_n; iinntt _y,_x;
346: Move window win to position y,x.
347:
348: mmvvwwiinncchh(_w_i_n,_y,_x) WWIINNDDOOWW *_w_i_n; sshhoorrtt _y,_x;
349: Move to position y,x and get character found there.
350:
351: mmvvwwiinnsscchh(_w_i_n,_y,_x,_c_h) WWIINNDDOOWW *_w_i_n; sshhoorrtt _y,_x; cchhaarr _c_h;
352: Move to position y,x and insert character ch there.
353:
354: WWIINNDDOOWW *nneewwwwiinn(lliinneess, ccoollss, yy11, xx11) iinntt _l_i_n_e_s, _c_o_l_s, _y_1, _x_1;
355: Create a new window. The new window is lines lines high,
356: cols columns wide, with the upper-left corner at position
357: y1,x1.
358:
359: nnll() Turn on newline mode; i.e., force terminal to output
360: <newline> after <linefeed>.
361:
362: nnooccrrmmooddee()
363: Turn off control-character mode; i.e., force terminal to ac-
364: cept raw input.
365:
366: nnooeecchhoo()
367: Turn off echo mode.
368:
369: nnoonnll()
370: Turn off newline mode.
371:
372: nnoorraaww()
373: Turn off raw mode.
374:
375: oovveerrllaayy(_w_i_n_1,_w_i_n_2) WWIINNDDOOWW *_w_i_n_1, _w_i_n_2;
376: Copy all characters, except spaces, from their current
377: positions in win1 to identical positions in win2.
378:
379: oovveerrwwrriittee(_w_i_n_1,_w_i_n_2) WWIINNDDOOWW *_w_i_n_1, _w_i_n_2;
380: Copy all characters, including spaces, from win1 to their
381: identical positions in win2.
382:
383: pprriinnttww(_f_o_r_m_a_t[,_a_r_g_1,..._a_r_g_N]) cchhaarr *_f_o_r_m_a_t; [_d_a_t_a _t_y_p_e]
384: _a_r_g_1,.._a_r_g_N;
385: Print formatted text on the standard screen.
386:
387: rraaww()
388: Turn on raw mode; i.e., kernel does not process what is
389: typed at the keyboard, but passes it directly to curses. In
390: normal (or _c_o_o_k_e_d) mode, the kernel intercepts and processes
391: the control characters <ctrl-C>, <ctrl-S>, <ctrl-Q>, and
392:
393:
394: COHERENT Lexicon Page 6
395:
396:
397:
398:
399: curses Overview curses
400:
401:
402:
403: <ctrl-Y>. See the entry for stty for more information.
404:
405: rreeffrreesshh()
406: Copy the contents of stdscr to the physical screen.
407:
408: rreesseettttyy()
409: Reset the terminal flags to values stored by earlier call to
410: savetty.
411:
412: ssaavveettttyy()
413: Save the current terminal settings.
414:
415: ssccaannww(_f_o_r_m_a_t[,_a_r_g_1,..._a_r_g_N]) cchhaarr *_f_o_r_m_a_t; [_d_a_t_a _t_y_p_e]
416: _a_r_g_1,.._a_r_g_N;
417: Read the standard input; translate what is read into the ap-
418: propriate data type.
419:
420: ssccrroollll(_w_i_n) WWIINNDDOOWW *_w_i_n;
421: Scroll win up by one line.
422:
423: ssccrroollllookk(_w_i_n,_b_f) WWIINNDDOOWW *_w_i_n; bbooooll _b_f;
424: Permit or forbid scrolling of window win, depending upon
425: whether bf is set to true or false.
426:
427: ssttaannddeenndd()
428: Turn off standout mode.
429:
430: ssttaannddoouutt()
431: Turn on standout mode for text. Usually, this means that
432: text will be displayed in reverse video.
433:
434: WWIINNDDOOWW *ssuubbwwiinn(_w_i_n,_l_i_n_e_s,_c_o_l_s,_y_1,_x_1) iinntt _w_i_n,_l_i_n_e_s,_c_o_l_s,_y_1,_x_1;
435: Create a sub-window in window win. New sub-window is lines
436: lines high, cols columns wide, and is fixed at position
437: y1,x1. Note that the position is relative to the upper-left
438: corner of the physical screen.
439:
440: ttoouucchhwwiinn(_w_i_n) WWIINNDDOOWW *_w_i_n;
441: Copy all characters in window win to the screen.
442:
443: wwaaddddcchh(_w_i_n,_c_h) WWIINNDDOOWW *_w_i_n; cchhaarr _c_h;
444: Add character ch win.
445:
446: wwaaddddssttrr(_w_i_n,_s_t_r) WWIINNDDOOWW *_w_i_n; cchhaarr *_s_t_r;
447: Add the string pointed to by str to window win.
448:
449: wwcclleeaarr(_w_i_n) WWIINNDDOOWW *_w_i_n;
450: Clear window win. Move cursor to position 0,0 and set the
451: screen's clear flag.
452:
453: wwccllrrttoobboott(_w_i_n) WWIINNDDOOWW *_w_i_n;
454: Clear window win from current position to the bottom.
455:
456: wwccllrrttooeeooll(_w_i_n) WWIINNDDOOWW *_w_i_n;
457: Clear window win from the current position to the end of the
458:
459:
460: COHERENT Lexicon Page 7
461:
462:
463:
464:
465: curses Overview curses
466:
467:
468:
469: line.
470:
471: wwddeellcchh(_w_i_n) WWIINNDDOOWW *_w_i_n;
472: Delete the character at the current position in window win;
473: shift all remaining characters to the right of the current
474: position one position left.
475:
476: wwddeelleetteellnn(_w_i_n) WWIINNDDOOWW *_w_i_n;
477: Delete the current line and shift all lines below it one
478: line up.
479:
480: wweerraassee(_w_i_n) WWIINNDDOOWW *_w_i_n;
481: Clear window win. Move the cursor to position 0,0 but do
482: not set the screen's clear flag.
483:
484: wwggeettcchh(_w_i_n) WWIINNDDOOWW *_w_i_n;
485: Read one character from the standard input.
486:
487: wwggeettssttrr(_w_i_n,_s_t_r) WWIINNDDOOWW *_w_i_n; cchhaarr *_s_t_r;
488: Read a string from the standard input; write it in the area
489: pointed to by str.
490:
491: wwiinncchh(_w_i_n) WWIINNDDOOWW *_w_i_n;
492: Force the next call to refresh() to rewrite the entire
493: screen.
494:
495: wwiinnsscchh(_w_i_n,_c_h) WWIINNDDOOWW *_w_i_n; cchhaarr _c_h;
496: Insert character ch into window win at the current position.
497: Shift all existing characters one position to the right.
498:
499: wwiinnsseerrttllnn(_w_i_n) WWIINNDDOOWW *_w_i_n;
500: Insert a blank line into window win at the current position.
501: Move all lines down by one position.
502:
503: wwmmoovvee(_w_i_n,_y,_x) WWIINNDDOOWW *_w_i_n; iinntt _y, _x;
504: Move current position in the window win to position y,x.
505:
506: wwpprriinnttww(_w_i_n,_f_o_r_m_a_t[,_a_r_g_1,..._a_r_g_N]) WWIINNDDOOWW *_w_i_n; cchhaarr *_f_o_r_m_a_t;
507: [_d_a_t_a _t_y_p_e] aarrgg11,..aarrggNN;
508: Format text and print it to the current position in window
509: win.
510:
511: wwrreeffrreesshh(_w_i_n) WWIINNDDOOWW *_w_i_n;
512: Refresh a window.
513:
514: wwssccaannww(_w_i_n,_f_o_r_m_a_t[,_a_r_g_1,..._a_r_g_N]) WWIINNDDOOWW *_w_i_n; cchhaarr *_f_o_r_m_a_t;
515: [_d_a_t_a _t_y_p_e] _a_r_g_1,.._a_r_g_N;
516: Read standard input from the current position in window win,
517: format it, and store it in the indicated places.
518:
519: wwssttaannddeenndd(_w_i_n) WWIINNDDOOWW *_w_i_n;
520: Turn off standout (reverse video) mode for window win.
521:
522: wwssttaannddoouutt(_w_i_n) WWIINNDDOOWW *_w_i_n;
523: Turn on standout (reverse video) mode for window win.
524:
525:
526: COHERENT Lexicon Page 8
527:
528:
529:
530:
531: curses Overview curses
532:
533:
534:
535:
536: These routines are declared and defined in the header file cur-
537: ses.h.
538:
539: ***** Structure of a curses Program *****
540:
541: To use the curses routines, a program must included the header
542: file curses.h, which declares and defines the functions and mac-
543: ros that comprise the curses library.
544:
545: Before a program can perform any graphics operations, it must
546: call the function initscr() to initialize the curses environment.
547: Then, the program must call cmdwind() to open the curscr.
548:
549: As noted above, curses manipulates text in a copy of the screen
550: that it maintains in memory. After a program has manipulated
551: text, it must call refresh() to copy these alterations from
552: memory to the physical screen. (This is done because writing to
553: the screen is slow; this scheme permits mass alterations to be
554: made to copy in memory, then written to the screen in a batch.)
555:
556: Finally, when the program has finished working with curses, it
557: must call the function endwin(). This frees memory allocated by
558: curses, and generally closes down the curses environment
559: gracefully.
560:
561: ***** Example *****
562:
563: The following program, called curexample.c, gives a simple ex-
564: ample of programming with curses. To compile this program, use
565: the command line:
566:
567:
568: cc curexample.c -lcurses -lterm
569:
570:
571: Note that order in which the libraries are called is significant.
572:
573: When this program is run, it clears the screen, then waits for
574: you to type a Y coordinate, a space, and then an X coordinate.
575: Note that these do not echo on the screen. It moves the cursor
576: to the requested coordinates, and there display any non-numeric
577: string that you type. If you type numerals, curexample will as-
578: sume that you wish to move the cursor to a new location. To
579: exit, type <ctrl-C>.
580:
581:
582: #include <ascii.h>
583: #include <ctype.h>
584: #include <curses.h>
585:
586:
587:
588:
589:
590:
591:
592: COHERENT Lexicon Page 9
593:
594:
595:
596:
597: curses Overview curses
598:
599:
600:
601: #define NORMAL 0
602: #define INY 1
603: #define INX 2
604:
605:
606:
607: main()
608: {
609: int c, y, x, state;
610:
611:
612:
613: initscr(); /* initialize curses */
614: noecho();
615: raw();
616:
617:
618:
619: clear();
620: move(0, 0);
621:
622:
623:
624: for(state = NORMAL;;) {
625: refresh();
626: c = getch();
627: if(isdigit(c)) {
628:
629:
630:
631: switch (state) {
632: case NORMAL:
633: y = x = 0;
634: state = INY;
635: case INY:
636: y *= 10;
637: y += c - '0';
638: break;
639: case INX:
640: x *= 10;
641: x += c - '0';
642: }
643: } else {
644:
645:
646:
647: if (c == A_ETX) { /* ctl-c */
648: noraw();
649: echo();
650: endwin();
651: exit(0);
652: }
653:
654:
655:
656:
657:
658: COHERENT Lexicon Page 10
659:
660:
661:
662:
663: curses Overview curses
664:
665:
666:
667:
668: switch (state) {
669: case INX:
670: state = NORMAL;
671: move(y, x);
672: case NORMAL:
673: addch(c);
674: break;
675: case INY:
676: state = INX;
677: }
678: }
679: }
680: }
681:
682:
683: ***** See Also *****
684:
685: curses.h, libraries, termcap
686: Strang J: _P_r_o_g_r_a_m_m_i_n_g _w_i_t_h _c_u_r_s_e_s. Sebastopol, Calif, O'Reilly &
687: Associates Inc., 1986.
688:
689: ***** Notes *****
690:
691: curses is copyrighted by the Regents of the University of
692: California.
693:
694:
695:
696:
697:
698:
699:
700:
701:
702:
703:
704:
705:
706:
707:
708:
709:
710:
711:
712:
713:
714:
715:
716:
717:
718:
719:
720:
721:
722:
723:
724: COHERENT Lexicon Page 11
725:
726:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.