#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)touchwin.c 8.2 (Berkeley) 5/4/94";
#else
__RCSID("$NetBSD: touchwin.c,v 1.36 2026/04/01 20:32:56 hgutch Exp $");
#endif
#endif
#include "curses.h"
#include "curses_private.h"
static int _cursesi_touchline_force(WINDOW *, int, int, int, int);
void
__sync(WINDOW *win)
{
if (__predict_false(win == NULL))
return;
if (win->flags & __IMMEDOK)
wrefresh(win);
if (win->flags & __SYNCOK)
wsyncup(win);
}
bool
is_linetouched(WINDOW *win, int line)
{
if (__predict_false(win == NULL))
return ERR;
if (line > win->maxy)
return FALSE;
__CTRACE(__CTRACE_LINE, "is_linetouched: (%p, line %d, dirty %d)\n",
win, line, (win->alines[line]->flags & __ISDIRTY));
return (win->alines[line]->flags & __ISDIRTY) != 0;
}
int
touchline(WINDOW *win, int start, int count)
{
__CTRACE(__CTRACE_LINE, "touchline: (%p, %d, %d)\n", win, start, count);
return wtouchln(win, start, count, 1);
}
int wredrawln(WINDOW *win, int start, int count)
{
__CTRACE(__CTRACE_LINE, "wredrawln: (%p, %d, %d)\n", win, start, count);
return wtouchln(win, start, count, 1);
}
bool
is_wintouched(WINDOW *win)
{
int y, maxy;
__CTRACE(__CTRACE_LINE, "is_wintouched: (%p, maxy %d)\n", win,
(win != NULL) ? win->maxy : 0);
if (__predict_false(win == NULL))
return FALSE;
maxy = win->maxy;
for (y = 0; y < maxy; y++) {
if (is_linetouched(win, y) == TRUE)
return TRUE;
}
return FALSE;
}
int
touchwin(WINDOW *win)
{
__CTRACE(__CTRACE_LINE, "touchwin: (%p)\n", win);
return wtouchln(win, 0, win->maxy, 1);
}
int
redrawwin(WINDOW *win)
{
__CTRACE(__CTRACE_LINE, "redrawwin: (%p)\n", win);
return wtouchln(win, 0, win->maxy, 1);
}
int
untouchwin(WINDOW *win)
{
__CTRACE(__CTRACE_LINE, "untouchwin: (%p)\n", win);
return wtouchln(win, 0, win->maxy, 0);
}
int
wtouchln(WINDOW *win, int line, int n, int changed)
{
int y;
__LINE *wlp;
__CTRACE(__CTRACE_LINE, "wtouchln: (%p) %d, %d, %d\n",
win, line, n, changed);
if (__predict_false(win == NULL))
return FALSE;
if (line < 0 || win->maxy <= line)
return ERR;
if (n < 0)
return ERR;
if (n > win->maxy - line)
n = win->maxy - line;
for (y = line; y < line + n; y++) {
if (changed == 1)
_cursesi_touchline_force(win, y, 0,
(int) win->maxx - 1, 1);
else {
wlp = win->alines[y];
if (*wlp->firstchp >= win->ch_off &&
*wlp->firstchp < win->maxx + win->ch_off)
*wlp->firstchp = win->maxx + win->ch_off;
if (*wlp->lastchp >= win->ch_off &&
*wlp->lastchp < win->maxx + win->ch_off)
*wlp->lastchp = win->ch_off;
wlp->flags &= ~(__ISDIRTY | __ISFORCED);
}
}
return OK;
}
int
__touchwin(WINDOW *win, int force)
{
int y, maxy;
__CTRACE(__CTRACE_LINE, "__touchwin: (%p)\n", win);
maxy = win->maxy;
for (y = 0; y < maxy; y++)
_cursesi_touchline_force(win, y, 0, (int) win->maxx - 1,
force);
return OK;
}
int
__touchline(WINDOW *win, int y, int sx, int ex)
{
return _cursesi_touchline_force(win, y, sx, ex, 0);
}
static int
_cursesi_touchline_force(WINDOW *win, int y, int sx, int ex, int force)
{
__CTRACE(__CTRACE_LINE, "__touchline: (%p, %d, %d, %d, %d)\n",
win, y, sx, ex, force);
__CTRACE(__CTRACE_LINE, "__touchline: first = %d, last = %d\n",
*win->alines[y]->firstchp, *win->alines[y]->lastchp);
sx += win->ch_off;
ex += win->ch_off;
win->alines[y]->flags |= __ISDIRTY;
if (force == 1)
win->alines[y]->flags |= __ISFORCED;
if (*win->alines[y]->firstchp > sx)
*win->alines[y]->firstchp = sx;
if (*win->alines[y]->lastchp < ex)
*win->alines[y]->lastchp = ex;
__CTRACE(__CTRACE_LINE, "__touchline: first = %d, last = %d\n",
*win->alines[y]->firstchp, *win->alines[y]->lastchp);
return OK;
}
void
wsyncup(WINDOW *win)
{
if (__predict_false(win == NULL))
return;
do {
__touchwin(win, 0);
win = win->orig;
} while (win);
}
void
wsyncdown(WINDOW *win)
{
if (__predict_false(win == NULL))
return;
WINDOW *w = win->orig;
while (w) {
if (is_wintouched(w)) {
__touchwin(win, 0);
break;
}
w = w->orig;
}
}