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