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
33 * @(#)move.c 8.1 (Berkeley) 7/19/93
34 * $FreeBSD: src/games/snake/snake/move.c,v 1.5.2.1 2000/08/17 06:21:44 jhb Exp $
35 * $DragonFly: src/games/snake/snake/Attic/move.c,v 1.3 2004/07/27 07:37:39 asmodai Exp $
38 /*************************************************************************
42 * This set of subroutines moves a cursor to a predefined
43 * location, independent of the terminal type. If the
44 * terminal has an addressable cursor, it uses it. If
45 * not, it optimizes for tabs (currently) even if you don't
48 * At all times the current address of the cursor must be maintained,
49 * and that is available as structure cursor.
51 * The following calls are allowed:
52 * move(sp) move to point sp.
53 * up() move up one line.
54 * down() move down one line.
55 * bs() move left one space (except column 0).
56 * nd() move right one space(no write).
57 * clear() clear screen.
59 * ll() move to lower left corner of screen.
60 * cr() carriage return (no line feed).
61 * pr() just like standard printf, but keeps track
62 * of cursor position. (Uses pstring).
63 * apr() same as printf, but first argument is &point.
65 * pstring(s) output the string of printing characters.
66 * However, '\r' is interpreted to mean return
67 * to column of origination AND do linefeed.
68 * '\n' causes <cr><lf>.
69 * putpad(str) calls tputs to output character with proper
71 * outch() the output routine for a character used by
72 * tputs. It just calls putchar.
73 * pch(ch) output character to screen and update
74 * cursor address (must be a standard
75 * printing character). WILL SCROLL.
76 * pchar(ps,ch) prints one character if it is on the
77 * screen at the specified location;
78 * otherwise, dumps it.(no wrap-around).
80 * getcap() initializes strings for later calls.
81 * cap(string) outputs the string designated in the termcap
82 * data base. (Should not move the cursor.)
83 * done() returns the terminal to intial state and exits.
85 * point(&p,x,y) return point set to x,y.
87 * baudrate() returns the baudrate of the terminal.
88 * delay(t) causes an approximately constant delay
89 * independent of baudrate.
90 * Duration is ~ t/20 seconds.
92 ******************************************************************************/
116 if (sp->line <0 || sp->col <0 || sp->col > COLUMNS){
117 pr("move to [%d,%d]?",sp->line,sp->col);
120 if (sp->line >= LINES){
121 move(point(&z,sp->col,LINES-1));
122 while(sp->line-- >= LINES)putchar('\n');
127 char *cmstr = tgoto(CM, sp->col, sp->line);
129 CMlength = strlen(cmstr);
130 if(cursor.line == sp->line){
131 distance = sp->col - cursor.col;
132 if(distance == 0)return; /* Already there! */
133 if(distance > 0){ /* Moving to the right */
134 if(distance*NDlength < CMlength){
140 tabcol=(cursor.col|7)+1;
145 while(tabcol<sp->col);
151 } else { /* Moving to the left */
152 if (-distance*BSlength < CMlength){
157 if(sp->col < CMlength){
162 /* No more optimizations on same row. */
164 distance = sp->col - cursor.col;
165 distance = distance > 0 ?
166 distance*NDlength : -distance * BSlength;
168 pr("ERROR: distance is negative: %d",distance);
169 distance += abs(sp->line - cursor.line);
170 if(distance >= CMlength){
172 cursor.line = sp->line;
173 cursor.col = sp->col;
179 * If we get here we have a terminal that can't cursor
180 * address but has local motions or one which can cursor
181 * address but can get there quicker with local motions.
189 int distance,f,tfield;
191 if (cursor.line > LINES || cursor.line <0 ||
192 cursor.col <0 || cursor.col > COLUMNS)
193 pr("ERROR: cursor is at %d,%d\n",
194 cursor.line,cursor.col);
195 if (sp->line > LINES || sp->line <0 ||
196 sp->col <0 || sp->col > COLUMNS)
197 pr("ERROR: target is %d,%d\n",sp->line,sp->col);
198 tfield = (sp->col) >> 3;
199 if (sp->line == cursor.line){
200 if (sp->col > cursor.col)right(sp);
202 distance = (cursor.col -sp->col)*BSlength;
204 (distance > tfield+((sp->col)&7)*NDlength)
206 (((cursor.col)*NDlength) < distance)
212 while(cursor.col > sp->col) bs();
218 if (cursor.col - sp->col > (cursor.col >> 3)){
219 if (cursor.col == 0)f = 0;
222 else f = cursor.col >> 3;
223 if (((sp->line << 1) + 1 < cursor.line - f) && (HO != 0)){
225 * home quicker than rlf:
226 * (sp->line + f > cursor.line - sp->line)
229 cursor.col = cursor.line = 0;
233 if (((sp->line << 1) > cursor.line + LINES+1 + f) && (LL != 0)){
234 /* home,rlf quicker than lf
235 * (LINES+1 - sp->line + f < sp->line - cursor.line)
237 if (cursor.line > f + 1){
238 /* is home faster than wraparound lf?
239 * (cursor.line + 20 - sp->line > 21 - sp->line + f)
246 if ((LL != 0) && (sp->line > cursor.line + (LINES >> 1) - 1))
247 cursor.line += LINES;
248 while(sp->line > cursor.line)down();
249 while(sp->line < cursor.line)up();
250 gto(sp); /*can recurse since cursor.line = sp->line */
257 int tabcol,strlength;
259 if (sp->col < cursor.col)
260 pr("ERROR:right() can't move left\n");
261 if(TA){ /* If No Tabs: can't send tabs because ttydrive
262 * loses count with control characters.
264 field = cursor.col >> 3;
266 * This code is useful for a terminal which wraps around on backspaces.
267 * (Mine does.) Unfortunately, this is not specified in termcap, and
268 * most terminals don't work that way. (Of course, most terminals
269 * have addressible cursors, too).
271 if (BW && (CM == 0) &&
272 ((sp->col << 1) - field > (COLUMNS - 8) << 1 )
274 if (cursor.line == 0){
278 cursor.col = COLUMNS + 1;
279 while(cursor.col > sp->col)bs();
280 if (cursor.line != 0) outch('\n');
284 tfield = sp->col >> 3;
286 while (field < tfield){
288 cursor.col = ++field << 3;
290 tabcol = (cursor.col|7) + 1;
291 strlength = (tabcol - sp->col)*BSlength + 1;
292 /* length of sequence to overshoot */
293 if (((sp->col - cursor.col)*NDlength > strlength) &&
297 * Tab past and backup
300 cursor.col = (cursor.col | 7) + 1;
301 while(cursor.col > sp->col)bs();
304 while (sp->col > cursor.col){
319 cursor.col=cursor.line=0;
321 for(i=0; i<LINES; i++) {
324 cursor.line = LINES - 1;
334 cursor.col = cursor.line = 0;
346 if(LL != NULL && LINES==l){
348 cursor.line = LINES-1;
365 if (cursor.line >= LINES)cursor.line=LINES-1;
377 if (cursor.col == COLUMNS+1){
380 if (cursor.line >= LINES)cursor.line=LINES-1;
387 if(++cursor.col >= COLUMNS && AM) {
394 apr(struct point *ps, const char *fmt, ...)
399 p.line = ps->line+1; p.col = ps->col+1;
402 (void)vsprintf(str, fmt, ap);
408 pr(const char *fmt, ...)
413 (void)vsprintf(str, fmt, ap);
424 while (s[0] != '\0'){
427 move(point(&z,0,cursor.line+1));
430 move(point(&z,stcol,cursor.line+1));
433 z.col = (((cursor.col + 8) >> 3) << 3);
434 z.line = cursor.line;
444 if (s[0] < ' ')break;
455 p.col = ps->col + 1; p.line = ps->line + 1;
465 (p.col == COLUMNS) &&
485 tputs(str, 1, outch);
492 switch (orig.sg_ospeed){
510 while (usleep(t*50000U) == -1 && errno == EINTR) ;
528 ioctl(0, TIOCSLTC, &olttyc);
536 ioctl(0, TIOCSLTC, &nlttyc);
540 struct point *point(ps,x,y)
561 term = getenv("TERM");
563 fprintf(stderr, "No TERM in environment\n");
567 switch (tgetent(tbuf, term)) {
569 fprintf(stderr, "Cannot open termcap file\n");
572 fprintf(stderr, "%s: unknown terminal", term);
579 if (ioctl(0, TIOCGWINSZ, (char *) &win) < 0 ||
580 (LINES = win.ws_row) == 0 || (COLUMNS = win.ws_col) == 0) {
582 LINES = tgetnum("li");
583 COLUMNS = tgetnum("co");
595 ND = tgetstr("nd", &ap);
596 UP = tgetstr("up", &ap);
598 DO = tgetstr("do", &ap);
602 BS = tgetstr("bc", &ap);
603 if (BS == 0 && tgetflag("bs"))
608 TA = tgetstr("ta", &ap);
609 if (TA == 0 && tgetflag("pt"))
612 HO = tgetstr("ho", &ap);
613 CL = tgetstr("cl", &ap);
614 CM = tgetstr("cm", &ap);
615 LL = tgetstr("ll", &ap);
617 KL = tgetstr("kl", &ap);
618 KR = tgetstr("kr", &ap);
619 KU = tgetstr("ku", &ap);
620 KD = tgetstr("kd", &ap);
622 Klength = strlen(KL);
624 Klength = strlen(KL);
626 * NOTE: If KL, KR, KU, and KD are not
627 * all the same length, some problems
628 * may arise, since tests are made on
629 * all of them together.
632 TI = tgetstr("ti", &ap);
633 TE = tgetstr("te", &ap);
634 KS = tgetstr("ks", &ap);
635 KE = tgetstr("ke", &ap);
637 VI = tgetstr("vi", &ap);
638 VE = tgetstr("ve", &ap);
640 xPC = tgetstr("pc", &ap);
645 NDlength = strlen(ND);
650 BSlength = strlen(BS);
655 (HO == 0 || DO == 0 || UP==0 || BS==0 || ND==0))
657 /* XXX as written in rev.1.6, we can assert(DO) */
658 fprintf(stderr, "Terminal must have addressible ");
659 fprintf(stderr, "cursor or home + 4 local motions\n");
662 if (tgetflag("os")) {
663 fprintf(stderr, "Terminal must not overstrike\n");
666 if (LINES <= 0 || COLUMNS <= 0) {
667 fprintf(stderr, "Must know the screen size\n");
673 new.sg_flags &= ~(ECHO|CRMOD|ALLDELAY|XTABS);
674 new.sg_flags |= CBREAK;
676 ospeed = orig.sg_ospeed;
678 ioctl(0, TIOCGLTC, &olttyc);
680 nlttyc.t_suspc = '\377';
681 nlttyc.t_dsuspc = '\377';
685 if ((orig.sg_flags & XTABS) == XTABS) TA=0;
688 point(&cursor,0,LINES-1);