Skip to content

Commit 4b9444c

Browse files
committed
workaround a bug in cursor positioning
this testprogram is supposed to create a rotating wheel animation: #include <ncurses.h> #include <unistd.h> int main() { int i, x, y; initscr(); cbreak(); noecho(); printw("press a key to start - you should see a 'rotating wheel' animation"); getch(); getmaxyx(stdscr, y, x); for (i = 0; i < 16; i++) { move(0, x-1); addch("\\-/|"[i%4]); refresh(); sleep(1); } clear(); refresh(); endwin(); return 0; } however it actually produced output like "\-/|\-/|\-/|". this is due to an optimization that does not move the terminal cursor if the target position is the same as the supposedly current position. supposedly, because the code that writes a single character does not increment the internal cursor position, even though the terminal's cursor is advanced by one through the write. it is possible that the author of the code in question trusted in the domvcur() function to do the bookkeeping for him, and wasn't aware of the optimization, or vice versa the author of the optimization unaware of the usage of the function for syncing the cursor position.
1 parent bf1e504 commit 4b9444c

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

libcurses/refresh.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,9 @@ makech(int wy)
12381238
#endif
12391239
#else
12401240
if (WCOL(*nsp) > 0) {
1241+
/* FIXME: putting a character advances the cursor, but the internal
1242+
housekeeping is not updated, this prevents from using the now commented
1243+
out optimization in domvcur(). */
12411244
__cputwchar((int) nsp->ch);
12421245
#ifdef DEBUG
12431246
__CTRACE(__CTRACE_REFRESH,
@@ -1318,8 +1321,18 @@ domvcur(oy, ox, ny, nx)
13181321
oy, ox, ny, nx );
13191322
#endif /* DEBUG */
13201323
__unsetattr(1);
1324+
/* FIXME: we have to turn off this optimization, because the internal cursor
1325+
position is not updated in makech() when putting a character.
1326+
eventually this optimization was not here when the other code was written,
1327+
so the author assumed that calling domvcur() would take care of that.
1328+
this causes breakage in a loop like:
1329+
for(i...) { move(0, 0); addch("\\-/|"[i%4]); refresh(); sleep(1); }
1330+
which is supposed to create a rotating wheel animation, but actually wrote
1331+
"\-/|\-/|\-/|\-/|\-/|" etc to the screen since the move() call turned NOP. */
1332+
#if 0
13211333
if ( oy == ny && ox == nx )
13221334
return;
1335+
#endif
13231336
__mvcur(oy, ox, ny, nx, 1);
13241337
}
13251338

0 commit comments

Comments
 (0)