|
|
1.1 root 1: .\" Copyright (c) 1980 The Regents of the University of California.
2: .\" All rights reserved.
3: .\"
4: .\" Redistribution and use in source and binary forms are permitted
5: .\" provided that the above copyright notice and this paragraph are
6: .\" duplicated in all such forms and that any documentation,
7: .\" advertising materials, and other materials related to such
8: .\" distribution and use acknowledge that the software was developed
9: .\" by the University of California, Berkeley. The name of the
10: .\" University may not be used to endorse or promote products derived
11: .\" from this software without specific prior written permission.
12: .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
13: .\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
14: .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15: .\"
16: .\" @(#)intro.4 6.2 (Berkeley) 3/17/89
17: .\"
18: .sh 1 "Cursor Motion Optimization: Standing Alone"
19: .pp
20: It is possible to use the cursor optimization functions of this screen package
21: without the overhead and additional size of the screen updating functions.
22: The screen updating functions are designed for uses
23: where parts of the screen are changed,
24: but the overall image remains the same.
25: This includes such programs as
26: .b rogue
27: and
28: .b vi \**.
29: .(f
30: \**
31: .b rogue
32: actually uses these functions,
33: .b vi
34: does not.
35: .)f
36: Certain other programs
37: will find it difficult to use these functions in this manner
38: without considerable unnecessary program overhead.
39: For such applications,
40: such as some
41: .q "\fIcrt hacks\fR\|" \**
42: .(f
43: \**
44: Graphics programs designed to run on character-oriented terminals.
45: I could name many,
46: but they come and go,
47: so the list would be quickly out of date.
48: Recently, there have been programs such as
49: .b rain ,
50: .b rocket ,
51: and
52: .b gun .
53: .)f
54: and optimizing
55: .b more (1)-type
56: programs,
57: all that is needed is the motion optimizations.
58: This, therefore, is a description
59: of what some of what goes on at the lower levels of this screen package.
60: The descriptions assume a certain amount of familiarity
61: with programming problems and some finer points of C.
62: None of it is terribly difficult,
63: but you should be forewarned.
64: .sh 2 "Terminal Information"
65: .pp
66: In order to use a terminal's
67: features to the best of a program's abilities,
68: it must first know what they are\**.
69: .(f
70: \**
71: If this comes as any surprise to you,
72: there's this tower in Paris they're thinking of junking
73: that I can let you have for a song.
74: .)f
75: The \*(tc \*(db describes these,
76: but a certain amount of decoding is necessary,
77: and there are, of course,
78: both efficient and inefficient ways of reading them in.
79: The algorithm that the uses is taken from
80: .b vi
81: and is hideously efficient.
82: It reads them
83: in a tight loop
84: into a set of variables
85: whose names are two uppercase letters with some mnemonic value.
86: For example,
87: .Vn HO
88: is a string which moves the cursor to the "home" position\**.
89: .(f
90: \**
91: These names are identical to those variables
92: used in the
93: .b termcap (5)
94: \*(db to describe each capability.
95: See Appendix A for a complete list of those read,
96: and the
97: .b termcap (5)
98: manual page
99: for a full description.
100: .)f
101: As there are two types of variables involving ttys,
102: there are two routines.
103: The first,
104: .Fn gettmode ,
105: sets some variables based upon the tty modes accessed by
106: .b gtty (2)
107: and
108: .b stty (2) .
109: The second,
110: .Fn setterm ,
111: a larger task by reading in the descriptions from the \*(tc \*(db.
112: This is the way these routines are used by
113: .Fn initscr :
114: .(b
115: .(l I
116: \*fif\fP (isatty(0)) {
117: gettmode();
118: \*fif\fP ((sp=getenv("TERM")) != NULL)
119: setterm(sp);
120: \*felse\fP
121: setterm(Def\*_term);
122: }
123: \*felse\fP
124: setterm(Def\*_term);
125: \*_puts(TI);
126: \*_puts(VS);
127: .)l
128: .)b
129: .pp
130: .Fn isatty
131: checks to see if file descriptor 0 is a terminal\**.
132: .(f
133: \**
134: .Fn isatty
135: is defined in the default C library function routines.
136: It does a
137: .b gtty (2)
138: on the descriptor and checks the return value.
139: .)f
140: If it is,
141: .Fn gettmode
142: sets the terminal description modes from a
143: .b gtty (2) .
144: .Fn getenv
145: is then called to get the name of the terminal,
146: and that value (if there is one) is passed to
147: .Fn setterm ,
148: which reads in the variables from \*(tc
149: associated with that terminal.
150: .Fn getenv "" (
151: returns a pointer to a string containing the name of the terminal,
152: which we save in the character pointer
153: .Vn sp .)
154: If
155: .Fn isatty
156: returns false,
157: the default terminal
158: .Vn Def\*_term
159: is used.
160: The
161: .Vn TI
162: and
163: .Vn VS
164: sequences initialize the terminal
165: .Fn \*_puts "" (
166: is a macro which uses
167: .Fn tputs
168: (see
169: .b termcap (3))
170: and
171: .Fn \*_putchar ""
172: to put out a string).
173: .Fn endwin
174: undoes these things.
175: .sh 2 "Movement Optimizations, or, Getting Over Yonder"
176: .pp
177: Now that we have all this useful information,
178: it would be nice to do something with it\**.
179: .(f
180: \**
181: Actually,
182: it
183: .i can
184: be emotionally fulfilling just to get the information.
185: This is usually only true, however,
186: if you have the social life of a kumquat.
187: .)f
188: The most difficult thing to do properly is motion optimization.
189: When you consider how many different features various terminals have
190: (tabs, backtabs, non-destructive space, home sequences, absolute tabs, .....)
191: you can see that deciding how to get from here to there
192: can be a decidedly non-trivial task.
193: The editor
194: .b vi
195: uses many of these features,
196: and the routines it uses to do this take up many pages of code.
197: Fortunately, I was able to liberate them with the author's permission,
198: and use them here.
199: .pp
200: After using
201: .Fn gettmode
202: and
203: .Fn setterm
204: to get the terminal descriptions,
205: the function
206: .Fn mvcur
207: deals with this task.
208: It usage is simple:
209: you simply tell it where you are now and where you want to go.
210: For example
211: .(l
212: mvcur(0\*,0\*,LINES/2\*,COLS/2)
213: .)l
214: .lp
215: would move the cursor from the home position (0\*,0)
216: to the middle of the screen.
217: If you wish to force absolute addressing,
218: you can use the function
219: .Fn tgoto
220: from the
221: .b termlib (7)
222: routines,
223: or you can tell
224: .Fn mvcur
225: that you are impossibly far away,
226: like Cleveland.
227: For example,
228: to absolutely address the lower left hand corner of the screen
229: from anywhere
230: just claim that you are in the upper right hand corner:
231: .(l
232: mvcur(0\*,COLS\-1\*,LINES\-1\*,0)
233: .)l
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.