2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)move.c 8.1 (Berkeley) 7/19/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/games/snake/snake/move.c,v 1.5.2.1 2000/08/17 06:21:44 jhb Exp $";
42 /*************************************************************************
46 * This set of subroutines moves a cursor to a predefined
47 * location, independent of the terminal type. If the
48 * terminal has an addressable cursor, it uses it. If
49 * not, it optimizes for tabs (currently) even if you don't
52 * At all times the current address of the cursor must be maintained,
53 * and that is available as structure cursor.
55 * The following calls are allowed:
56 * move(sp) move to point sp.
57 * up() move up one line.
58 * down() move down one line.
59 * bs() move left one space (except column 0).
60 * nd() move right one space(no write).
61 * clear() clear screen.
63 * ll() move to lower left corner of screen.
64 * cr() carriage return (no line feed).
65 * pr() just like standard printf, but keeps track
66 * of cursor position. (Uses pstring).
67 * apr() same as printf, but first argument is &point.
69 * pstring(s) output the string of printing characters.
70 * However, '\r' is interpreted to mean return
71 * to column of origination AND do linefeed.
72 * '\n' causes <cr><lf>.
73 * putpad(str) calls tputs to output character with proper
75 * outch() the output routine for a character used by
76 * tputs. It just calls putchar.
77 * pch(ch) output character to screen and update
78 * cursor address (must be a standard
79 * printing character). WILL SCROLL.
80 * pchar(ps,ch) prints one character if it is on the
81 * screen at the specified location;
82 * otherwise, dumps it.(no wrap-around).
84 * getcap() initializes strings for later calls.
85 * cap(string) outputs the string designated in the termcap
86 * data base. (Should not move the cursor.)
87 * done() returns the terminal to intial state and exits.
89 * point(&p,x,y) return point set to x,y.
91 * baudrate() returns the baudrate of the terminal.
92 * delay(t) causes an approximately constant delay
93 * independent of baudrate.
94 * Duration is ~ t/20 seconds.
96 ******************************************************************************/
124 if (sp->line <0 || sp->col <0 || sp->col > COLUMNS){
125 pr("move to [%d,%d]?",sp->line,sp->col);
128 if (sp->line >= LINES){
129 move(point(&z,sp->col,LINES-1));
130 while(sp->line-- >= LINES)putchar('\n');
135 char *cmstr = tgoto(CM, sp->col, sp->line);
137 CMlength = strlen(cmstr);
138 if(cursor.line == sp->line){
139 distance = sp->col - cursor.col;
140 if(distance == 0)return; /* Already there! */
141 if(distance > 0){ /* Moving to the right */
142 if(distance*NDlength < CMlength){
148 tabcol=(cursor.col|7)+1;
153 while(tabcol<sp->col);
159 } else { /* Moving to the left */
160 if (-distance*BSlength < CMlength){
165 if(sp->col < CMlength){
170 /* No more optimizations on same row. */
172 distance = sp->col - cursor.col;
173 distance = distance > 0 ?
174 distance*NDlength : -distance * BSlength;
176 pr("ERROR: distance is negative: %d",distance);
177 distance += abs(sp->line - cursor.line);
178 if(distance >= CMlength){
180 cursor.line = sp->line;
181 cursor.col = sp->col;
187 * If we get here we have a terminal that can't cursor
188 * address but has local motions or one which can cursor
189 * address but can get there quicker with local motions.
197 int distance,f,tfield;
199 if (cursor.line > LINES || cursor.line <0 ||
200 cursor.col <0 || cursor.col > COLUMNS)
201 pr("ERROR: cursor is at %d,%d\n",
202 cursor.line,cursor.col);
203 if (sp->line > LINES || sp->line <0 ||
204 sp->col <0 || sp->col > COLUMNS)
205 pr("ERROR: target is %d,%d\n",sp->line,sp->col);
206 tfield = (sp->col) >> 3;
207 if (sp->line == cursor.line){
208 if (sp->col > cursor.col)right(sp);
210 distance = (cursor.col -sp->col)*BSlength;
212 (distance > tfield+((sp->col)&7)*NDlength)
214 (((cursor.col)*NDlength) < distance)
220 while(cursor.col > sp->col) bs();
226 if (cursor.col - sp->col > (cursor.col >> 3)){
227 if (cursor.col == 0)f = 0;
230 else f = cursor.col >> 3;
231 if (((sp->line << 1) + 1 < cursor.line - f) && (HO != 0)){
233 * home quicker than rlf:
234 * (sp->line + f > cursor.line - sp->line)
237 cursor.col = cursor.line = 0;
241 if (((sp->line << 1) > cursor.line + LINES+1 + f) && (LL != 0)){
242 /* home,rlf quicker than lf
243 * (LINES+1 - sp->line + f < sp->line - cursor.line)
245 if (cursor.line > f + 1){
246 /* is home faster than wraparound lf?
247 * (cursor.line + 20 - sp->line > 21 - sp->line + f)
254 if ((LL != 0) && (sp->line > cursor.line + (LINES >> 1) - 1))
255 cursor.line += LINES;
256 while(sp->line > cursor.line)down();
257 while(sp->line < cursor.line)up();
258 gto(sp); /*can recurse since cursor.line = sp->line */
265 int tabcol,strlength;
267 if (sp->col < cursor.col)
268 pr("ERROR:right() can't move left\n");
269 if(TA){ /* If No Tabs: can't send tabs because ttydrive
270 * loses count with control characters.
272 field = cursor.col >> 3;
274 * This code is useful for a terminal which wraps around on backspaces.
275 * (Mine does.) Unfortunately, this is not specified in termcap, and
276 * most terminals don't work that way. (Of course, most terminals
277 * have addressible cursors, too).
279 if (BW && (CM == 0) &&
280 ((sp->col << 1) - field > (COLUMNS - 8) << 1 )
282 if (cursor.line == 0){
286 cursor.col = COLUMNS + 1;
287 while(cursor.col > sp->col)bs();
288 if (cursor.line != 0) outch('\n');
292 tfield = sp->col >> 3;
294 while (field < tfield){
296 cursor.col = ++field << 3;
298 tabcol = (cursor.col|7) + 1;
299 strlength = (tabcol - sp->col)*BSlength + 1;
300 /* length of sequence to overshoot */
301 if (((sp->col - cursor.col)*NDlength > strlength) &&
305 * Tab past and backup
308 cursor.col = (cursor.col | 7) + 1;
309 while(cursor.col > sp->col)bs();
312 while (sp->col > cursor.col){
327 cursor.col=cursor.line=0;
329 for(i=0; i<LINES; i++) {
332 cursor.line = LINES - 1;
342 cursor.col = cursor.line = 0;
354 if(LL != NULL && LINES==l){
356 cursor.line = LINES-1;
373 if (cursor.line >= LINES)cursor.line=LINES-1;
385 if (cursor.col == COLUMNS+1){
388 if (cursor.line >= LINES)cursor.line=LINES-1;
395 if(++cursor.col >= COLUMNS && AM) {
403 apr(struct point *ps, const char *fmt, ...)
405 apr(ps, fmt, va_alist)
414 p.line = ps->line+1; p.col = ps->col+1;
421 (void)vsprintf(str, fmt, ap);
428 pr(const char *fmt, ...)
442 (void)vsprintf(str, fmt, ap);
453 while (s[0] != '\0'){
456 move(point(&z,0,cursor.line+1));
459 move(point(&z,stcol,cursor.line+1));
462 z.col = (((cursor.col + 8) >> 3) << 3);
463 z.line = cursor.line;
473 if (s[0] < ' ')break;
484 p.col = ps->col + 1; p.line = ps->line + 1;
494 (p.col == COLUMNS) &&
514 tputs(str, 1, outch);
521 switch (orig.sg_ospeed){
539 while (usleep(t*50000U) == -1 && errno == EINTR) ;
557 ioctl(0, TIOCSLTC, &olttyc);
565 ioctl(0, TIOCSLTC, &nlttyc);
569 struct point *point(ps,x,y)
590 term = getenv("TERM");
592 fprintf(stderr, "No TERM in environment\n");
596 switch (tgetent(tbuf, term)) {
598 fprintf(stderr, "Cannot open termcap file\n");
601 fprintf(stderr, "%s: unknown terminal", term);
608 if (ioctl(0, TIOCGWINSZ, (char *) &win) < 0 ||
609 (LINES = win.ws_row) == 0 || (COLUMNS = win.ws_col) == 0) {
611 LINES = tgetnum("li");
612 COLUMNS = tgetnum("co");
624 ND = tgetstr("nd", &ap);
625 UP = tgetstr("up", &ap);
627 DO = tgetstr("do", &ap);
631 BS = tgetstr("bc", &ap);
632 if (BS == 0 && tgetflag("bs"))
637 TA = tgetstr("ta", &ap);
638 if (TA == 0 && tgetflag("pt"))
641 HO = tgetstr("ho", &ap);
642 CL = tgetstr("cl", &ap);
643 CM = tgetstr("cm", &ap);
644 LL = tgetstr("ll", &ap);
646 KL = tgetstr("kl", &ap);
647 KR = tgetstr("kr", &ap);
648 KU = tgetstr("ku", &ap);
649 KD = tgetstr("kd", &ap);
651 Klength = strlen(KL);
653 Klength = strlen(KL);
655 * NOTE: If KL, KR, KU, and KD are not
656 * all the same length, some problems
657 * may arise, since tests are made on
658 * all of them together.
661 TI = tgetstr("ti", &ap);
662 TE = tgetstr("te", &ap);
663 KS = tgetstr("ks", &ap);
664 KE = tgetstr("ke", &ap);
666 VI = tgetstr("vi", &ap);
667 VE = tgetstr("ve", &ap);
669 xPC = tgetstr("pc", &ap);
674 NDlength = strlen(ND);
679 BSlength = strlen(BS);
684 (HO == 0 || DO == 0 || UP==0 || BS==0 || ND==0))
686 /* XXX as written in rev.1.6, we can assert(DO) */
687 fprintf(stderr, "Terminal must have addressible ");
688 fprintf(stderr, "cursor or home + 4 local motions\n");
691 if (tgetflag("os")) {
692 fprintf(stderr, "Terminal must not overstrike\n");
695 if (LINES <= 0 || COLUMNS <= 0) {
696 fprintf(stderr, "Must know the screen size\n");
702 new.sg_flags &= ~(ECHO|CRMOD|ALLDELAY|XTABS);
703 new.sg_flags |= CBREAK;
705 ospeed = orig.sg_ospeed;
707 ioctl(0, TIOCGLTC, &olttyc);
709 nlttyc.t_suspc = '\377';
710 nlttyc.t_dsuspc = '\377';
714 if ((orig.sg_flags & XTABS) == XTABS) TA=0;
717 point(&cursor,0,LINES-1);