cursor.c revision d522f475
1d522f475Smrg/* $XTermId: cursor.c,v 1.45 2008/04/20 21:06:22 tom Exp $ */
2d522f475Smrg
3d522f475Smrg/* $XFree86: xc/programs/xterm/cursor.c,v 3.20 2006/02/13 01:14:58 dickey Exp $ */
4d522f475Smrg
5d522f475Smrg/*
6d522f475Smrg * Copyright 2002-2007,2008 by Thomas E. Dickey
7d522f475Smrg *
8d522f475Smrg *                         All Rights Reserved
9d522f475Smrg *
10d522f475Smrg * Permission is hereby granted, free of charge, to any person obtaining a
11d522f475Smrg * copy of this software and associated documentation files (the
12d522f475Smrg * "Software"), to deal in the Software without restriction, including
13d522f475Smrg * without limitation the rights to use, copy, modify, merge, publish,
14d522f475Smrg * distribute, sublicense, and/or sell copies of the Software, and to
15d522f475Smrg * permit persons to whom the Software is furnished to do so, subject to
16d522f475Smrg * the following conditions:
17d522f475Smrg *
18d522f475Smrg * The above copyright notice and this permission notice shall be included
19d522f475Smrg * in all copies or substantial portions of the Software.
20d522f475Smrg *
21d522f475Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22d522f475Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23d522f475Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24d522f475Smrg * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25d522f475Smrg * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26d522f475Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27d522f475Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d522f475Smrg *
29d522f475Smrg * Except as contained in this notice, the name(s) of the above copyright
30d522f475Smrg * holders shall not be used in advertising or otherwise to promote the
31d522f475Smrg * sale, use or other dealings in this Software without prior written
32d522f475Smrg * authorization.
33d522f475Smrg *
34d522f475Smrg * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
35d522f475Smrg *
36d522f475Smrg *                         All Rights Reserved
37d522f475Smrg *
38d522f475Smrg * Permission to use, copy, modify, and distribute this software and its
39d522f475Smrg * documentation for any purpose and without fee is hereby granted,
40d522f475Smrg * provided that the above copyright notice appear in all copies and that
41d522f475Smrg * both that copyright notice and this permission notice appear in
42d522f475Smrg * supporting documentation, and that the name of Digital Equipment
43d522f475Smrg * Corporation not be used in advertising or publicity pertaining to
44d522f475Smrg * distribution of the software without specific, written prior permission.
45d522f475Smrg *
46d522f475Smrg *
47d522f475Smrg * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
48d522f475Smrg * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
49d522f475Smrg * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
50d522f475Smrg * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
51d522f475Smrg * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
52d522f475Smrg * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
53d522f475Smrg * SOFTWARE.
54d522f475Smrg */
55d522f475Smrg
56d522f475Smrg/* cursor.c */
57d522f475Smrg
58d522f475Smrg#include <xterm.h>
59d522f475Smrg#include <data.h>
60d522f475Smrg#include <menu.h>
61d522f475Smrg
62d522f475Smrg#include <assert.h>
63d522f475Smrg
64d522f475Smrg/*
65d522f475Smrg * Moves the cursor to the specified position, checking for bounds.
66d522f475Smrg * (this includes scrolling regions)
67d522f475Smrg * The origin is considered to be 0, 0 for this procedure.
68d522f475Smrg */
69d522f475Smrgvoid
70d522f475SmrgCursorSet(TScreen * screen, int row, int col, unsigned flags)
71d522f475Smrg{
72d522f475Smrg    int use_row = row;
73d522f475Smrg    int max_row;
74d522f475Smrg
75d522f475Smrg    col = (col < 0 ? 0 : col);
76d522f475Smrg    set_cur_col(screen, (col <= screen->max_col ? col : screen->max_col));
77d522f475Smrg    max_row = screen->max_row;
78d522f475Smrg    if (flags & ORIGIN) {
79d522f475Smrg	use_row += screen->top_marg;
80d522f475Smrg	max_row = screen->bot_marg;
81d522f475Smrg    }
82d522f475Smrg    use_row = (use_row < 0 ? 0 : use_row);
83d522f475Smrg    set_cur_row(screen, (use_row <= max_row ? use_row : max_row));
84d522f475Smrg    screen->do_wrap = 0;
85d522f475Smrg
86d522f475Smrg    TRACE(("CursorSet(%d,%d) margins [%d..%d] -> %d,%d %s\n",
87d522f475Smrg	   row, col,
88d522f475Smrg	   screen->top_marg,
89d522f475Smrg	   screen->bot_marg,
90d522f475Smrg	   screen->cur_row,
91d522f475Smrg	   screen->cur_col,
92d522f475Smrg	   (flags & ORIGIN ? "origin" : "normal")));
93d522f475Smrg}
94d522f475Smrg
95d522f475Smrg/*
96d522f475Smrg * moves the cursor left n, no wrap around
97d522f475Smrg */
98d522f475Smrgvoid
99d522f475SmrgCursorBack(XtermWidget xw, int n)
100d522f475Smrg{
101d522f475Smrg    TScreen *screen = &xw->screen;
102d522f475Smrg    int i, j, k, rev;
103d522f475Smrg
104d522f475Smrg    if ((rev = (xw->flags & (REVERSEWRAP | WRAPAROUND)) ==
105d522f475Smrg	 (REVERSEWRAP | WRAPAROUND)) != 0
106d522f475Smrg	&& screen->do_wrap)
107d522f475Smrg	n--;
108d522f475Smrg    if ((screen->cur_col -= n) < 0) {
109d522f475Smrg	if (rev) {
110d522f475Smrg	    if ((i = ((j = MaxCols(screen))
111d522f475Smrg		      * screen->cur_row) + screen->cur_col) < 0) {
112d522f475Smrg		k = j * MaxRows(screen);
113d522f475Smrg		i += ((-i) / k + 1) * k;
114d522f475Smrg	    }
115d522f475Smrg	    set_cur_row(screen, i / j);
116d522f475Smrg	    set_cur_col(screen, i % j);
117d522f475Smrg	    do_xevents();
118d522f475Smrg	} else {
119d522f475Smrg	    set_cur_col(screen, 0);
120d522f475Smrg	}
121d522f475Smrg    }
122d522f475Smrg    screen->do_wrap = 0;
123d522f475Smrg}
124d522f475Smrg
125d522f475Smrg/*
126d522f475Smrg * moves the cursor forward n, no wraparound
127d522f475Smrg */
128d522f475Smrgvoid
129d522f475SmrgCursorForward(TScreen * screen, int n)
130d522f475Smrg{
131d522f475Smrg    int next = screen->cur_col + n;
132d522f475Smrg    int max = CurMaxCol(screen, screen->cur_row);
133d522f475Smrg
134d522f475Smrg    if (next > max)
135d522f475Smrg	next = max;
136d522f475Smrg
137d522f475Smrg    set_cur_col(screen, next);
138d522f475Smrg    screen->do_wrap = 0;
139d522f475Smrg}
140d522f475Smrg
141d522f475Smrg/*
142d522f475Smrg * moves the cursor down n, no scrolling.
143d522f475Smrg * Won't pass bottom margin or bottom of screen.
144d522f475Smrg */
145d522f475Smrgvoid
146d522f475SmrgCursorDown(TScreen * screen, int n)
147d522f475Smrg{
148d522f475Smrg    int max;
149d522f475Smrg    int next = screen->cur_row + n;
150d522f475Smrg
151d522f475Smrg    max = (screen->cur_row > screen->bot_marg ?
152d522f475Smrg	   screen->max_row : screen->bot_marg);
153d522f475Smrg    if (next > max)
154d522f475Smrg	next = max;
155d522f475Smrg    if (next > screen->max_row)
156d522f475Smrg	next = screen->max_row;
157d522f475Smrg
158d522f475Smrg    set_cur_row(screen, next);
159d522f475Smrg    screen->do_wrap = 0;
160d522f475Smrg}
161d522f475Smrg
162d522f475Smrg/*
163d522f475Smrg * moves the cursor up n, no linestarving.
164d522f475Smrg * Won't pass top margin or top of screen.
165d522f475Smrg */
166d522f475Smrgvoid
167d522f475SmrgCursorUp(TScreen * screen, int n)
168d522f475Smrg{
169d522f475Smrg    int min;
170d522f475Smrg    int next = screen->cur_row - n;
171d522f475Smrg
172d522f475Smrg    min = ((screen->cur_row < screen->top_marg)
173d522f475Smrg	   ? 0
174d522f475Smrg	   : screen->top_marg);
175d522f475Smrg    if (next < min)
176d522f475Smrg	next = min;
177d522f475Smrg    if (next < 0)
178d522f475Smrg	next = 0;
179d522f475Smrg
180d522f475Smrg    set_cur_row(screen, next);
181d522f475Smrg    screen->do_wrap = 0;
182d522f475Smrg}
183d522f475Smrg
184d522f475Smrg/*
185d522f475Smrg * Moves cursor down amount lines, scrolls if necessary.
186d522f475Smrg * Won't leave scrolling region. No carriage return.
187d522f475Smrg */
188d522f475Smrgvoid
189d522f475SmrgxtermIndex(XtermWidget xw, int amount)
190d522f475Smrg{
191d522f475Smrg    TScreen *screen = &xw->screen;
192d522f475Smrg    int j;
193d522f475Smrg
194d522f475Smrg    /*
195d522f475Smrg     * indexing when below scrolling region is cursor down.
196d522f475Smrg     * if cursor high enough, no scrolling necessary.
197d522f475Smrg     */
198d522f475Smrg    if (screen->cur_row > screen->bot_marg
199d522f475Smrg	|| screen->cur_row + amount <= screen->bot_marg) {
200d522f475Smrg	CursorDown(screen, amount);
201d522f475Smrg	return;
202d522f475Smrg    }
203d522f475Smrg
204d522f475Smrg    CursorDown(screen, j = screen->bot_marg - screen->cur_row);
205d522f475Smrg    xtermScroll(xw, amount - j);
206d522f475Smrg}
207d522f475Smrg
208d522f475Smrg/*
209d522f475Smrg * Moves cursor up amount lines, reverse scrolls if necessary.
210d522f475Smrg * Won't leave scrolling region. No carriage return.
211d522f475Smrg */
212d522f475Smrgvoid
213d522f475SmrgRevIndex(XtermWidget xw, int amount)
214d522f475Smrg{
215d522f475Smrg    TScreen *screen = &xw->screen;
216d522f475Smrg
217d522f475Smrg    /*
218d522f475Smrg     * reverse indexing when above scrolling region is cursor up.
219d522f475Smrg     * if cursor low enough, no reverse indexing needed
220d522f475Smrg     */
221d522f475Smrg    if (screen->cur_row < screen->top_marg
222d522f475Smrg	|| screen->cur_row - amount >= screen->top_marg) {
223d522f475Smrg	CursorUp(screen, amount);
224d522f475Smrg	return;
225d522f475Smrg    }
226d522f475Smrg
227d522f475Smrg    RevScroll(xw, amount - (screen->cur_row - screen->top_marg));
228d522f475Smrg    CursorUp(screen, screen->cur_row - screen->top_marg);
229d522f475Smrg}
230d522f475Smrg
231d522f475Smrg/*
232d522f475Smrg * Moves Cursor To First Column In Line
233d522f475Smrg * (Note: xterm doesn't implement SLH, SLL which would affect use of this)
234d522f475Smrg */
235d522f475Smrgvoid
236d522f475SmrgCarriageReturn(TScreen * screen)
237d522f475Smrg{
238d522f475Smrg    set_cur_col(screen, 0);
239d522f475Smrg    screen->do_wrap = 0;
240d522f475Smrg    do_xevents();
241d522f475Smrg}
242d522f475Smrg
243d522f475Smrg/*
244d522f475Smrg * When resizing the window, if we're showing the alternate screen, we still
245d522f475Smrg * have to adjust the saved cursor from the normal screen to account for
246d522f475Smrg * shifting of the saved-line region in/out of the viewable window.
247d522f475Smrg */
248d522f475Smrgvoid
249d522f475SmrgAdjustSavedCursor(XtermWidget xw, int adjust)
250d522f475Smrg{
251d522f475Smrg    TScreen *screen = &xw->screen;
252d522f475Smrg
253d522f475Smrg    if (screen->alternate) {
254d522f475Smrg	SavedCursor *sc = &screen->sc[screen->alternate == False];
255d522f475Smrg
256d522f475Smrg	if (adjust > 0) {
257d522f475Smrg	    TRACE(("AdjustSavedCursor %d -> %d\n", sc->row, sc->row - adjust));
258d522f475Smrg	    sc->row += adjust;
259d522f475Smrg	}
260d522f475Smrg    }
261d522f475Smrg}
262d522f475Smrg
263d522f475Smrg/*
264d522f475Smrg * Save Cursor and Attributes
265d522f475Smrg */
266d522f475Smrgvoid
267d522f475SmrgCursorSave(XtermWidget xw)
268d522f475Smrg{
269d522f475Smrg    TScreen *screen = &xw->screen;
270d522f475Smrg    SavedCursor *sc = &screen->sc[screen->alternate != False];
271d522f475Smrg
272d522f475Smrg    sc->saved = True;
273d522f475Smrg    sc->row = screen->cur_row;
274d522f475Smrg    sc->col = screen->cur_col;
275d522f475Smrg    sc->flags = xw->flags;
276d522f475Smrg    sc->curgl = screen->curgl;
277d522f475Smrg    sc->curgr = screen->curgr;
278d522f475Smrg#if OPT_ISO_COLORS
279d522f475Smrg    sc->cur_foreground = xw->cur_foreground;
280d522f475Smrg    sc->cur_background = xw->cur_background;
281d522f475Smrg    sc->sgr_foreground = xw->sgr_foreground;
282d522f475Smrg#endif
283d522f475Smrg    memmove(sc->gsets, screen->gsets, sizeof(screen->gsets));
284d522f475Smrg}
285d522f475Smrg
286d522f475Smrg/*
287d522f475Smrg * We save/restore all visible attributes, plus wrapping, origin mode, and the
288d522f475Smrg * selective erase attribute.
289d522f475Smrg */
290d522f475Smrg#define DECSC_FLAGS (ATTRIBUTES|ORIGIN|WRAPAROUND|PROTECTED)
291d522f475Smrg
292d522f475Smrg/*
293d522f475Smrg * Restore Cursor and Attributes
294d522f475Smrg */
295d522f475Smrgvoid
296d522f475SmrgCursorRestore(XtermWidget xw)
297d522f475Smrg{
298d522f475Smrg    TScreen *screen = &xw->screen;
299d522f475Smrg    SavedCursor *sc = &screen->sc[screen->alternate != False];
300d522f475Smrg
301d522f475Smrg    /* Restore the character sets, unless we never did a save-cursor op.
302d522f475Smrg     * In that case, we'll reset the character sets.
303d522f475Smrg     */
304d522f475Smrg    if (sc->saved) {
305d522f475Smrg	memmove(screen->gsets, sc->gsets, sizeof(screen->gsets));
306d522f475Smrg	screen->curgl = sc->curgl;
307d522f475Smrg	screen->curgr = sc->curgr;
308d522f475Smrg    } else {
309d522f475Smrg	resetCharsets(screen);
310d522f475Smrg    }
311d522f475Smrg
312d522f475Smrg    xw->flags &= ~DECSC_FLAGS;
313d522f475Smrg    xw->flags |= sc->flags & DECSC_FLAGS;
314d522f475Smrg    CursorSet(screen,
315d522f475Smrg	      ((xw->flags & ORIGIN)
316d522f475Smrg	       ? sc->row - screen->top_marg
317d522f475Smrg	       : sc->row),
318d522f475Smrg	      sc->col, xw->flags);
319d522f475Smrg
320d522f475Smrg#if OPT_ISO_COLORS
321d522f475Smrg    xw->sgr_foreground = sc->sgr_foreground;
322d522f475Smrg    SGR_Foreground(xw, xw->flags & FG_COLOR ? sc->cur_foreground : -1);
323d522f475Smrg    SGR_Background(xw, xw->flags & BG_COLOR ? sc->cur_background : -1);
324d522f475Smrg#endif
325d522f475Smrg    update_autowrap();
326d522f475Smrg}
327d522f475Smrg
328d522f475Smrg/*
329d522f475Smrg * Move the cursor to the first column of the n-th next line.
330d522f475Smrg */
331d522f475Smrgvoid
332d522f475SmrgCursorNextLine(TScreen * screen, int count)
333d522f475Smrg{
334d522f475Smrg    CursorDown(screen, count < 1 ? 1 : count);
335d522f475Smrg    CarriageReturn(screen);
336d522f475Smrg    do_xevents();
337d522f475Smrg}
338d522f475Smrg
339d522f475Smrg/*
340d522f475Smrg * Move the cursor to the first column of the n-th previous line.
341d522f475Smrg */
342d522f475Smrgvoid
343d522f475SmrgCursorPrevLine(TScreen * screen, int count)
344d522f475Smrg{
345d522f475Smrg    CursorUp(screen, count < 1 ? 1 : count);
346d522f475Smrg    CarriageReturn(screen);
347d522f475Smrg    do_xevents();
348d522f475Smrg}
349d522f475Smrg
350d522f475Smrg#if OPT_TRACE
351d522f475Smrgint
352d522f475Smrgset_cur_row(TScreen * screen, int value)
353d522f475Smrg{
354d522f475Smrg    assert(screen != 0);
355d522f475Smrg    assert(value >= 0);
356d522f475Smrg    assert(value <= screen->max_row);
357d522f475Smrg    screen->cur_row = value;
358d522f475Smrg    return value;
359d522f475Smrg}
360d522f475Smrg
361d522f475Smrgint
362d522f475Smrgset_cur_col(TScreen * screen, int value)
363d522f475Smrg{
364d522f475Smrg    assert(screen != 0);
365d522f475Smrg    assert(value >= 0);
366d522f475Smrg    assert(value <= screen->max_col);
367d522f475Smrg    screen->cur_col = value;
368d522f475Smrg    return value;
369d522f475Smrg}
370d522f475Smrg#endif /* OPT_TRACE */
371