In my revamp of cursor handling I had assumed that you were supposed
[u/mdw/putty] / terminal.c
index b167f3b..25fb81e 100644 (file)
@@ -271,7 +271,7 @@ static int termchars_equal_override(termchar *a, termchar *b,
     /* FULL-TERMCHAR */
     if (a->chr != bchr)
        return FALSE;
-    if (a->attr != battr)
+    if ((a->attr &~ DATTR_MASK) != (battr &~ DATTR_MASK))
        return FALSE;
     while (a->cc_next || b->cc_next) {
        if (!a->cc_next || !b->cc_next)
@@ -4539,16 +4539,16 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
     pos scrpos;
     wchar_t *ch;
     int chlen;
-    termchar cursor_background;
 #ifdef OPTIMISE_SCROLL
     struct scrollregion *sr;
 #endif /* OPTIMISE_SCROLL */
-
-    cursor_background = term->basic_erase_char;
+    termchar *newline;
 
     chlen = 1024;
     ch = snewn(chlen, wchar_t);
 
+    newline = snewn(term->cols, termchar);
+
     rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
 
     /* Depends on:
@@ -4643,15 +4643,12 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
        int start = 0;
        int ccount = 0;
        int last_run_dirty = 0;
+       int laststart, dirtyrect;
        int *backward;
 
        scrpos.y = i + term->disptop;
        ldata = lineptr(scrpos.y);
 
-       dirty_run = dirty_line = (ldata->lattr !=
-                                 term->disptext[i]->lattr);
-       term->disptext[i]->lattr = ldata->lattr;
-
        /* Do Arabic shaping and bidi. */
        lchars = term_bidi_line(term, ldata, i);
        if (lchars) {
@@ -4661,10 +4658,13 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
            backward = NULL;
        }
 
+       /*
+        * First loop: work along the line deciding what we want
+        * each character cell to look like.
+        */
        for (j = 0; j < term->cols; j++) {
            unsigned long tattr, tchar;
            termchar *d = lchars + j;
-           int break_run, do_copy;
            scrpos.x = backward ? backward[j] : j;
 
            tchar = d->chr;
@@ -4725,26 +4725,78 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
             */
            if (tchar != term->disptext[i]->chars[j].chr ||
                tattr != (term->disptext[i]->chars[j].attr &~
-                         ATTR_NARROW)) {
+                         (ATTR_NARROW | DATTR_MASK))) {
                if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
                    tattr |= ATTR_NARROW;
            } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
                tattr |= ATTR_NARROW;
 
-           /* Cursor here ? Save the 'background' */
            if (i == our_curs_y && j == our_curs_x) {
-               /* FULL-TERMCHAR */
-               cursor_background.chr = tchar;
-               cursor_background.attr = tattr;
-               /* For once, this cc_next field is an absolute index in lchars */
-               if (d->cc_next)
-                   cursor_background.cc_next = d->cc_next + j;
-               else
-                   cursor_background.cc_next = 0;
+               tattr |= cursor;
+               term->curstype = cursor;
                term->dispcursx = j;
                term->dispcursy = i;
            }
 
+           /* FULL-TERMCHAR */
+           newline[j].attr = tattr;
+           newline[j].chr = tchar;
+           /* Combining characters are still read from lchars */
+           newline[j].cc_next = 0;
+       }
+
+       /*
+        * Now loop over the line again, noting where things have
+        * changed.
+        * 
+        * During this loop, we keep track of where we last saw
+        * DATTR_STARTRUN. Any mismatch automatically invalidates
+        * _all_ of the containing run that was last printed: that
+        * is, any rectangle that was drawn in one go in the
+        * previous update should be either left completely alone
+        * or overwritten in its entirety. This, along with the
+        * expectation that front ends clip all text runs to their
+        * bounding rectangle, should solve any possible problems
+        * with fonts that overflow their character cells.
+        */
+       laststart = 0;
+       dirtyrect = FALSE;
+       for (j = 0; j < term->cols; j++) {
+           if (term->disptext[i]->chars[j].attr & DATTR_STARTRUN) {
+               laststart = j;
+               dirtyrect = FALSE;
+           }
+
+           if (term->disptext[i]->chars[j].chr != newline[j].chr ||
+               (term->disptext[i]->chars[j].attr &~ DATTR_MASK)
+               != newline[j].attr) {
+               int k;
+
+               for (k = laststart; k < j; k++)
+                   term->disptext[i]->chars[k].attr |= ATTR_INVALID;
+
+               dirtyrect = TRUE;
+           }
+
+           if (dirtyrect)
+               term->disptext[i]->chars[j].attr |= ATTR_INVALID;
+       }
+
+       /*
+        * Finally, loop once more and actually do the drawing.
+        */
+       dirty_run = dirty_line = (ldata->lattr !=
+                                 term->disptext[i]->lattr);
+       term->disptext[i]->lattr = ldata->lattr;
+
+       for (j = 0; j < term->cols; j++) {
+           unsigned long tattr, tchar;
+           int break_run, do_copy;
+           termchar *d = lchars + j;
+
+           tattr = newline[j].attr;
+           tchar = newline[j].chr;
+
            if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
                dirty_line = TRUE;
 
@@ -4770,7 +4822,7 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
 
            if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
                if (term->disptext[i]->chars[j].chr == tchar &&
-                   term->disptext[i]->chars[j].attr == tattr)
+                   (term->disptext[i]->chars[j].attr &~ DATTR_MASK) == tattr)
                    break_run = TRUE;
                else if (!dirty_run && ccount == 1)
                    break_run = TRUE;
@@ -4778,7 +4830,12 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
 
            if (break_run) {
                if ((dirty_run || last_run_dirty) && ccount > 0) {
-                   do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
+                   do_text(ctx, start, i, ch, ccount, attr,
+                           ldata->lattr);
+                   if (attr & (TATTR_ACTCURS | TATTR_PASCURS))
+                       do_cursor(ctx, start, i, ch, ccount, attr,
+                                 ldata->lattr);
+
                    updated_line = 1;
                }
                start = j;
@@ -4838,6 +4895,8 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
                copy_termchar(term->disptext[i], j, d);
                term->disptext[i]->chars[j].chr = tchar;
                term->disptext[i]->chars[j].attr = tattr;
+               if (start == j)
+                   term->disptext[i]->chars[j].attr |= DATTR_STARTRUN;
            }
 
            /* If it's a wide char step along to the next one. */
@@ -4857,57 +4916,19 @@ static void do_paint(Terminal *term, Context ctx, int may_optimise)
            }
        }
        if (dirty_run && ccount > 0) {
-           do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
-           updated_line = 1;
-       }
-
-       /* Cursor on this line ? (and changed) */
-       if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
-           ch[0] = (wchar_t) cursor_background.chr;
-           attr = cursor_background.attr | cursor;
-           ccount = 1;
+           do_text(ctx, start, i, ch, ccount, attr,
+                   ldata->lattr);
+           if (attr & (TATTR_ACTCURS | TATTR_PASCURS))
+               do_cursor(ctx, start, i, ch, ccount, attr,
+                         ldata->lattr);
 
-           if (cursor_background.cc_next) {
-               termchar *dd = ldata->chars + cursor_background.cc_next;
-
-               while (1) {
-                   unsigned long schar;
-
-                   schar = dd->chr;
-                   switch (schar & CSET_MASK) {
-                     case CSET_ASCII:
-                       schar = term->ucsdata->unitab_line[schar & 0xFF];
-                       break;
-                     case CSET_LINEDRW:
-                       schar = term->ucsdata->unitab_xterm[schar & 0xFF];
-                       break;
-                     case CSET_SCOACS:
-                       schar = term->ucsdata->unitab_scoacs[schar&0xFF];
-                       break;
-                   }
-
-                   if (ccount >= chlen) {
-                       chlen = ccount + 256;
-                       ch = sresize(ch, chlen, wchar_t);
-                   }
-                   ch[ccount++] = (wchar_t) schar;
-
-                   if (dd->cc_next)
-                       dd += dd->cc_next;
-                   else
-                       break;
-               }
-
-               attr |= TATTR_COMBINING;
-           }
-
-           do_cursor(ctx, our_curs_x, i, ch, ccount, attr, ldata->lattr);
-           term->curstype = cursor;
+           updated_line = 1;
        }
 
        unlineptr(ldata);
     }
 
+    sfree(newline);
     sfree(ch);
 }
 
@@ -4920,7 +4941,7 @@ void term_invalidate(Terminal *term)
 
     for (i = 0; i < term->rows; i++)
        for (j = 0; j < term->cols; j++)
-           term->disptext[i]->chars[j].attr = ATTR_INVALID;
+           term->disptext[i]->chars[j].attr |= ATTR_INVALID;
 
     term_schedule_update(term);
 }
@@ -4940,10 +4961,10 @@ void term_paint(Terminal *term, Context ctx,
     for (i = top; i <= bottom && i < term->rows; i++) {
        if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM)
            for (j = left; j <= right && j < term->cols; j++)
-               term->disptext[i]->chars[j].attr = ATTR_INVALID;
+               term->disptext[i]->chars[j].attr |= ATTR_INVALID;
        else
            for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
-               term->disptext[i]->chars[j].attr = ATTR_INVALID;
+               term->disptext[i]->chars[j].attr |= ATTR_INVALID;
     }
 
     if (immediately) {