refresh.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. refresh
  5. -------
  6. ### Synopsis
  7. int refresh(void);
  8. int wrefresh(WINDOW *win);
  9. int wnoutrefresh(WINDOW *win);
  10. int doupdate(void);
  11. int redrawwin(WINDOW *win);
  12. int wredrawln(WINDOW *win, int beg_line, int num_lines);
  13. ### Description
  14. wrefresh() copies the named window to the physical terminal screen,
  15. taking into account what is already there in order to optimize cursor
  16. movement. refresh() does the same, using stdscr. These routines must
  17. be called to get any output on the terminal, as other routines only
  18. manipulate data structures. Unless leaveok() has been enabled, the
  19. physical cursor of the terminal is left at the location of the
  20. window's cursor.
  21. wnoutrefresh() and doupdate() allow multiple updates with more
  22. efficiency than wrefresh() alone. wrefresh() works by first calling
  23. wnoutrefresh(), which copies the named window to the virtual screen.
  24. It then calls doupdate(), which compares the virtual screen to the
  25. physical screen and does the actual update. A series of calls to
  26. wrefresh() will result in alternating calls to wnoutrefresh() and
  27. doupdate(), causing several bursts of output to the screen. By first
  28. calling wnoutrefresh() for each window, it is then possible to call
  29. doupdate() only once.
  30. In PDCurses, redrawwin() is equivalent to touchwin(), and wredrawln()
  31. is the same as touchline(). In some other curses implementations,
  32. there's a subtle distinction, but it has no meaning in PDCurses.
  33. ### Return Value
  34. All functions return OK on success and ERR on error.
  35. ### Portability
  36. X/Open ncurses NetBSD
  37. refresh Y Y Y
  38. wrefresh Y Y Y
  39. wnoutrefresh Y Y Y
  40. doupdate Y Y Y
  41. redrawwin Y Y Y
  42. wredrawln Y Y Y
  43. **man-end****************************************************************/
  44. #include <string.h>
  45. int wnoutrefresh(WINDOW *win)
  46. {
  47. int begy, begx; /* window's place on screen */
  48. int i, j;
  49. PDC_LOG(("wnoutrefresh() - called: win=%p\n", win));
  50. if ( !win || (win->_flags & (_PAD|_SUBPAD)) )
  51. return ERR;
  52. begy = win->_begy;
  53. begx = win->_begx;
  54. for (i = 0, j = begy; i < win->_maxy; i++, j++)
  55. {
  56. if (win->_firstch[i] != _NO_CHANGE)
  57. {
  58. chtype *src = win->_y[i];
  59. chtype *dest = curscr->_y[j] + begx;
  60. int first = win->_firstch[i]; /* first changed */
  61. int last = win->_lastch[i]; /* last changed */
  62. /* ignore areas on the outside that are marked as changed,
  63. but really aren't */
  64. while (first <= last && src[first] == dest[first])
  65. first++;
  66. while (last >= first && src[last] == dest[last])
  67. last--;
  68. /* if any have really changed... */
  69. if (first <= last)
  70. {
  71. memcpy(dest + first, src + first,
  72. (last - first + 1) * sizeof(chtype));
  73. first += begx;
  74. last += begx;
  75. if (first < curscr->_firstch[j] ||
  76. curscr->_firstch[j] == _NO_CHANGE)
  77. curscr->_firstch[j] = first;
  78. if (last > curscr->_lastch[j])
  79. curscr->_lastch[j] = last;
  80. }
  81. win->_firstch[i] = _NO_CHANGE; /* updated now */
  82. }
  83. win->_lastch[i] = _NO_CHANGE; /* updated now */
  84. }
  85. if (win->_clear)
  86. win->_clear = FALSE;
  87. if (!win->_leaveit)
  88. {
  89. curscr->_cury = win->_cury + begy;
  90. curscr->_curx = win->_curx + begx;
  91. }
  92. return OK;
  93. }
  94. int doupdate(void)
  95. {
  96. int y;
  97. bool clearall;
  98. PDC_LOG(("doupdate() - called\n"));
  99. if (!SP || !curscr)
  100. return ERR;
  101. if (isendwin()) /* coming back after endwin() called */
  102. {
  103. reset_prog_mode();
  104. clearall = TRUE;
  105. SP->alive = TRUE; /* so isendwin() result is correct */
  106. }
  107. else
  108. clearall = curscr->_clear;
  109. for (y = 0; y < SP->lines; y++)
  110. {
  111. PDC_LOG(("doupdate() - Transforming line %d of %d: %s\n",
  112. y, SP->lines, (curscr->_firstch[y] != _NO_CHANGE) ?
  113. "Yes" : "No"));
  114. if (clearall || curscr->_firstch[y] != _NO_CHANGE)
  115. {
  116. int first, last;
  117. chtype *src = curscr->_y[y];
  118. chtype *dest = SP->lastscr->_y[y];
  119. if (clearall)
  120. {
  121. first = 0;
  122. last = COLS - 1;
  123. }
  124. else
  125. {
  126. first = curscr->_firstch[y];
  127. last = curscr->_lastch[y];
  128. }
  129. while (first <= last)
  130. {
  131. int len = 0;
  132. /* build up a run of changed cells; if two runs are
  133. separated by a single unchanged cell, ignore the
  134. break */
  135. if (clearall)
  136. len = last - first + 1;
  137. else
  138. while (first + len <= last &&
  139. (src[first + len] != dest[first + len] ||
  140. (len && first + len < last &&
  141. src[first + len + 1] != dest[first + len + 1])
  142. )
  143. )
  144. len++;
  145. /* update the screen, and SP->lastscr */
  146. if (len)
  147. {
  148. PDC_transform_line(y, first, len, src + first);
  149. memcpy(dest + first, src + first, len * sizeof(chtype));
  150. first += len;
  151. }
  152. /* skip over runs of unchanged cells */
  153. while (first <= last && src[first] == dest[first])
  154. first++;
  155. }
  156. curscr->_firstch[y] = _NO_CHANGE;
  157. curscr->_lastch[y] = _NO_CHANGE;
  158. }
  159. }
  160. curscr->_clear = FALSE;
  161. if (SP->visibility)
  162. PDC_gotoyx(curscr->_cury, curscr->_curx);
  163. SP->cursrow = curscr->_cury;
  164. SP->curscol = curscr->_curx;
  165. PDC_doupdate();
  166. return OK;
  167. }
  168. int wrefresh(WINDOW *win)
  169. {
  170. bool save_clear;
  171. PDC_LOG(("wrefresh() - called\n"));
  172. if ( !win || (win->_flags & (_PAD|_SUBPAD)) )
  173. return ERR;
  174. save_clear = win->_clear;
  175. if (win == curscr)
  176. curscr->_clear = TRUE;
  177. else
  178. wnoutrefresh(win);
  179. if (save_clear && win->_maxy == SP->lines && win->_maxx == SP->cols)
  180. curscr->_clear = TRUE;
  181. return doupdate();
  182. }
  183. int refresh(void)
  184. {
  185. PDC_LOG(("refresh() - called\n"));
  186. return wrefresh(stdscr);
  187. }
  188. int wredrawln(WINDOW *win, int start, int num)
  189. {
  190. int i;
  191. PDC_LOG(("wredrawln() - called: win=%p start=%d num=%d\n",
  192. win, start, num));
  193. if (!win || start > win->_maxy || start + num > win->_maxy)
  194. return ERR;
  195. for (i = start; i < start + num; i++)
  196. {
  197. win->_firstch[i] = 0;
  198. win->_lastch[i] = win->_maxx - 1;
  199. }
  200. return OK;
  201. }
  202. int redrawwin(WINDOW *win)
  203. {
  204. PDC_LOG(("redrawwin() - called: win=%p\n", win));
  205. if (!win)
  206. return ERR;
  207. return wredrawln(win, 0, win->_maxy);
  208. }