Zdenek Dohnal 978bc6
diff -up vim81/src/libvterm/src/termscreen.c.missing-file vim81/src/libvterm/src/termscreen.c
Zdenek Dohnal 978bc6
--- vim81/src/libvterm/src/termscreen.c.missing-file	2018-09-19 09:26:00.601523830 +0200
Zdenek Dohnal 978bc6
+++ vim81/src/libvterm/src/termscreen.c	2018-09-19 09:26:56.448031158 +0200
Zdenek Dohnal 978bc6
@@ -0,0 +1,935 @@
Zdenek Dohnal 978bc6
+#include "vterm_internal.h"
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+#include <stdio.h>
Zdenek Dohnal 978bc6
+#include <string.h>
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+#include "rect.h"
Zdenek Dohnal 978bc6
+#include "utf8.h"
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+#define UNICODE_SPACE 0x20
Zdenek Dohnal 978bc6
+#define UNICODE_LINEFEED 0x0a
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+/* State of the pen at some moment in time, also used in a cell */
Zdenek Dohnal 978bc6
+typedef struct
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  /* After the bitfield */
Zdenek Dohnal 978bc6
+  VTermColor   fg, bg;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  unsigned int bold      : 1;
Zdenek Dohnal 978bc6
+  unsigned int underline : 2;
Zdenek Dohnal 978bc6
+  unsigned int italic    : 1;
Zdenek Dohnal 978bc6
+  unsigned int blink     : 1;
Zdenek Dohnal 978bc6
+  unsigned int reverse   : 1;
Zdenek Dohnal 978bc6
+  unsigned int strike    : 1;
Zdenek Dohnal 978bc6
+  unsigned int font      : 4; /* 0 to 9 */
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  /* Extra state storage that isn't strictly pen-related */
Zdenek Dohnal 978bc6
+  unsigned int protected_cell : 1;
Zdenek Dohnal 978bc6
+  unsigned int dwl            : 1; /* on a DECDWL or DECDHL line */
Zdenek Dohnal 978bc6
+  unsigned int dhl            : 2; /* on a DECDHL line (1=top 2=bottom) */
Zdenek Dohnal 978bc6
+} ScreenPen;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+/* Internal representation of a screen cell */
Zdenek Dohnal 978bc6
+typedef struct
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  uint32_t chars[VTERM_MAX_CHARS_PER_CELL];
Zdenek Dohnal 978bc6
+  ScreenPen pen;
Zdenek Dohnal 978bc6
+} ScreenCell;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int vterm_screen_set_cell(VTermScreen *screen, VTermPos pos, const VTermScreenCell *cell);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+struct VTermScreen
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTerm *vt;
Zdenek Dohnal 978bc6
+  VTermState *state;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  const VTermScreenCallbacks *callbacks;
Zdenek Dohnal 978bc6
+  void *cbdata;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  VTermDamageSize damage_merge;
Zdenek Dohnal 978bc6
+  /* start_row == -1 => no damage */
Zdenek Dohnal 978bc6
+  VTermRect damaged;
Zdenek Dohnal 978bc6
+  VTermRect pending_scrollrect;
Zdenek Dohnal 978bc6
+  int pending_scroll_downward, pending_scroll_rightward;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  int rows;
Zdenek Dohnal 978bc6
+  int cols;
Zdenek Dohnal 978bc6
+  int global_reverse;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  /* Primary and Altscreen. buffers[1] is lazily allocated as needed */
Zdenek Dohnal 978bc6
+  ScreenCell *buffers[2];
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  /* buffer will == buffers[0] or buffers[1], depending on altscreen */
Zdenek Dohnal 978bc6
+  ScreenCell *buffer;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  /* buffer for a single screen row used in scrollback storage callbacks */
Zdenek Dohnal 978bc6
+  VTermScreenCell *sb_buffer;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  ScreenPen pen;
Zdenek Dohnal 978bc6
+};
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static ScreenCell *getcell(const VTermScreen *screen, int row, int col)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  if(row < 0 || row >= screen->rows)
Zdenek Dohnal 978bc6
+    return NULL;
Zdenek Dohnal 978bc6
+  if(col < 0 || col >= screen->cols)
Zdenek Dohnal 978bc6
+    return NULL;
Zdenek Dohnal 978bc6
+  return screen->buffer + (screen->cols * row) + col;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static ScreenCell *realloc_buffer(VTermScreen *screen, ScreenCell *buffer, int new_rows, int new_cols)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  ScreenCell *new_buffer = vterm_allocator_malloc(screen->vt, sizeof(ScreenCell) * new_rows * new_cols);
Zdenek Dohnal 978bc6
+  int row, col;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(row = 0; row < new_rows; row++) {
Zdenek Dohnal 978bc6
+    for(col = 0; col < new_cols; col++) {
Zdenek Dohnal 978bc6
+      ScreenCell *new_cell = new_buffer + row*new_cols + col;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+      if(buffer && row < screen->rows && col < screen->cols)
Zdenek Dohnal 978bc6
+        *new_cell = buffer[row * screen->cols + col];
Zdenek Dohnal 978bc6
+      else {
Zdenek Dohnal 978bc6
+        new_cell->chars[0] = 0;
Zdenek Dohnal 978bc6
+        new_cell->pen = screen->pen;
Zdenek Dohnal 978bc6
+      }
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(buffer)
Zdenek Dohnal 978bc6
+    vterm_allocator_free(screen->vt, buffer);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return new_buffer;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static void damagerect(VTermScreen *screen, VTermRect rect)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermRect emit;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  switch(screen->damage_merge) {
Zdenek Dohnal 978bc6
+  case VTERM_DAMAGE_CELL:
Zdenek Dohnal 978bc6
+    /* Always emit damage event */
Zdenek Dohnal 978bc6
+    emit = rect;
Zdenek Dohnal 978bc6
+    break;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  case VTERM_DAMAGE_ROW:
Zdenek Dohnal 978bc6
+    /* Emit damage longer than one row. Try to merge with existing damage in
Zdenek Dohnal 978bc6
+     * the same row */
Zdenek Dohnal 978bc6
+    if(rect.end_row > rect.start_row + 1) {
Zdenek Dohnal 978bc6
+      // Bigger than 1 line - flush existing, emit this
Zdenek Dohnal 978bc6
+      vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+      emit = rect;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+    else if(screen->damaged.start_row == -1) {
Zdenek Dohnal 978bc6
+      // None stored yet
Zdenek Dohnal 978bc6
+      screen->damaged = rect;
Zdenek Dohnal 978bc6
+      return;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+    else if(rect.start_row == screen->damaged.start_row) {
Zdenek Dohnal 978bc6
+      // Merge with the stored line
Zdenek Dohnal 978bc6
+      if(screen->damaged.start_col > rect.start_col)
Zdenek Dohnal 978bc6
+        screen->damaged.start_col = rect.start_col;
Zdenek Dohnal 978bc6
+      if(screen->damaged.end_col < rect.end_col)
Zdenek Dohnal 978bc6
+        screen->damaged.end_col = rect.end_col;
Zdenek Dohnal 978bc6
+      return;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+    else {
Zdenek Dohnal 978bc6
+      // Emit the currently stored line, store a new one
Zdenek Dohnal 978bc6
+      emit = screen->damaged;
Zdenek Dohnal 978bc6
+      screen->damaged = rect;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+    break;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  case VTERM_DAMAGE_SCREEN:
Zdenek Dohnal 978bc6
+  case VTERM_DAMAGE_SCROLL:
Zdenek Dohnal 978bc6
+    /* Never emit damage event */
Zdenek Dohnal 978bc6
+    if(screen->damaged.start_row == -1)
Zdenek Dohnal 978bc6
+      screen->damaged = rect;
Zdenek Dohnal 978bc6
+    else {
Zdenek Dohnal 978bc6
+      rect_expand(&screen->damaged, &rect);
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+    return;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  default:
Zdenek Dohnal 978bc6
+    DEBUG_LOG1("TODO: Maybe merge damage for level %d\n", screen->damage_merge);
Zdenek Dohnal 978bc6
+    return;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->callbacks && screen->callbacks->damage)
Zdenek Dohnal 978bc6
+    (*screen->callbacks->damage)(emit, screen->cbdata);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static void damagescreen(VTermScreen *screen)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermRect rect = {0,0,0,0};
Zdenek Dohnal 978bc6
+  rect.end_row = screen->rows;
Zdenek Dohnal 978bc6
+  rect.end_col = screen->cols;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  damagerect(screen, rect);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int putglyph(VTermGlyphInfo *info, VTermPos pos, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  int i;
Zdenek Dohnal 978bc6
+  int col;
Zdenek Dohnal 978bc6
+  VTermRect rect;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+  ScreenCell *cell = getcell(screen, pos.row, pos.col);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(!cell)
Zdenek Dohnal 978bc6
+    return 0;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(i = 0; i < VTERM_MAX_CHARS_PER_CELL && info->chars[i]; i++) {
Zdenek Dohnal 978bc6
+    cell->chars[i] = info->chars[i];
Zdenek Dohnal 978bc6
+    cell->pen = screen->pen;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+  if(i < VTERM_MAX_CHARS_PER_CELL)
Zdenek Dohnal 978bc6
+    cell->chars[i] = 0;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(col = 1; col < info->width; col++)
Zdenek Dohnal 978bc6
+    getcell(screen, pos.row, pos.col + col)->chars[0] = (uint32_t)-1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  rect.start_row = pos.row;
Zdenek Dohnal 978bc6
+  rect.end_row   = pos.row+1;
Zdenek Dohnal 978bc6
+  rect.start_col = pos.col;
Zdenek Dohnal 978bc6
+  rect.end_col   = pos.col+info->width;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  cell->pen.protected_cell = info->protected_cell;
Zdenek Dohnal 978bc6
+  cell->pen.dwl            = info->dwl;
Zdenek Dohnal 978bc6
+  cell->pen.dhl            = info->dhl;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  damagerect(screen, rect);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int moverect_internal(VTermRect dest, VTermRect src, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->callbacks && screen->callbacks->sb_pushline &&
Zdenek Dohnal 978bc6
+     dest.start_row == 0 && dest.start_col == 0 &&  // starts top-left corner
Zdenek Dohnal 978bc6
+     dest.end_col == screen->cols &&                // full width
Zdenek Dohnal 978bc6
+     screen->buffer == screen->buffers[0]) {        // not altscreen
Zdenek Dohnal 978bc6
+    VTermPos pos;
Zdenek Dohnal 978bc6
+    for(pos.row = 0; pos.row < src.start_row; pos.row++) {
Zdenek Dohnal 978bc6
+      for(pos.col = 0; pos.col < screen->cols; pos.col++)
Zdenek Dohnal 978bc6
+        (void)vterm_screen_get_cell(screen, pos, screen->sb_buffer + pos.col);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+      (screen->callbacks->sb_pushline)(screen->cols, screen->sb_buffer, screen->cbdata);
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  {
Zdenek Dohnal 978bc6
+    int cols = src.end_col - src.start_col;
Zdenek Dohnal 978bc6
+    int downward = src.start_row - dest.start_row;
Zdenek Dohnal 978bc6
+    int init_row, test_row, inc_row;
Zdenek Dohnal 978bc6
+    int row;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    if(downward < 0) {
Zdenek Dohnal 978bc6
+      init_row = dest.end_row - 1;
Zdenek Dohnal 978bc6
+      test_row = dest.start_row - 1;
Zdenek Dohnal 978bc6
+      inc_row  = -1;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+    else {
Zdenek Dohnal 978bc6
+      init_row = dest.start_row;
Zdenek Dohnal 978bc6
+      test_row = dest.end_row;
Zdenek Dohnal 978bc6
+      inc_row  = +1;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    for(row = init_row; row != test_row; row += inc_row)
Zdenek Dohnal 978bc6
+      memmove(getcell(screen, row, dest.start_col),
Zdenek Dohnal 978bc6
+	      getcell(screen, row + downward, src.start_col),
Zdenek Dohnal 978bc6
+	      cols * sizeof(ScreenCell));
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int moverect_user(VTermRect dest, VTermRect src, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->callbacks && screen->callbacks->moverect) {
Zdenek Dohnal 978bc6
+    if(screen->damage_merge != VTERM_DAMAGE_SCROLL)
Zdenek Dohnal 978bc6
+      // Avoid an infinite loop
Zdenek Dohnal 978bc6
+      vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    if((*screen->callbacks->moverect)(dest, src, screen->cbdata))
Zdenek Dohnal 978bc6
+      return 1;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  damagerect(screen, dest);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int erase_internal(VTermRect rect, int selective, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+  int row, col;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(row = rect.start_row; row < screen->state->rows && row < rect.end_row; row++) {
Zdenek Dohnal 978bc6
+    const VTermLineInfo *info = vterm_state_get_lineinfo(screen->state, row);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    for(col = rect.start_col; col < rect.end_col; col++) {
Zdenek Dohnal 978bc6
+      ScreenCell *cell = getcell(screen, row, col);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+      if(selective && cell->pen.protected_cell)
Zdenek Dohnal 978bc6
+        continue;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+      cell->chars[0] = 0;
Zdenek Dohnal 978bc6
+      cell->pen = screen->pen;
Zdenek Dohnal 978bc6
+      cell->pen.dwl = info->doublewidth;
Zdenek Dohnal 978bc6
+      cell->pen.dhl = info->doubleheight;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int erase_user(VTermRect rect, int selective UNUSED, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  damagerect(screen, rect);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int erase(VTermRect rect, int selective, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  erase_internal(rect, selective, user);
Zdenek Dohnal 978bc6
+  return erase_user(rect, 0, user);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int scrollrect(VTermRect rect, int downward, int rightward, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->damage_merge != VTERM_DAMAGE_SCROLL) {
Zdenek Dohnal 978bc6
+    vterm_scroll_rect(rect, downward, rightward,
Zdenek Dohnal 978bc6
+        moverect_internal, erase_internal, screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    vterm_scroll_rect(rect, downward, rightward,
Zdenek Dohnal 978bc6
+        moverect_user, erase_user, screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->damaged.start_row != -1 &&
Zdenek Dohnal 978bc6
+     !rect_intersects(&rect, &screen->damaged)) {
Zdenek Dohnal 978bc6
+    vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->pending_scrollrect.start_row == -1) {
Zdenek Dohnal 978bc6
+    screen->pending_scrollrect = rect;
Zdenek Dohnal 978bc6
+    screen->pending_scroll_downward  = downward;
Zdenek Dohnal 978bc6
+    screen->pending_scroll_rightward = rightward;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+  else if(rect_equal(&screen->pending_scrollrect, &rect) &&
Zdenek Dohnal 978bc6
+     ((screen->pending_scroll_downward  == 0 && downward  == 0) ||
Zdenek Dohnal 978bc6
+      (screen->pending_scroll_rightward == 0 && rightward == 0))) {
Zdenek Dohnal 978bc6
+    screen->pending_scroll_downward  += downward;
Zdenek Dohnal 978bc6
+    screen->pending_scroll_rightward += rightward;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+  else {
Zdenek Dohnal 978bc6
+    vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    screen->pending_scrollrect = rect;
Zdenek Dohnal 978bc6
+    screen->pending_scroll_downward  = downward;
Zdenek Dohnal 978bc6
+    screen->pending_scroll_rightward = rightward;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  vterm_scroll_rect(rect, downward, rightward,
Zdenek Dohnal 978bc6
+      moverect_internal, erase_internal, screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->damaged.start_row == -1)
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(rect_contains(&rect, &screen->damaged)) {
Zdenek Dohnal 978bc6
+    /* Scroll region entirely contains the damage; just move it */
Zdenek Dohnal 978bc6
+    vterm_rect_move(&screen->damaged, -downward, -rightward);
Zdenek Dohnal 978bc6
+    rect_clip(&screen->damaged, &rect);
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+  /* There are a number of possible cases here, but lets restrict this to only
Zdenek Dohnal 978bc6
+   * the common case where we might actually gain some performance by
Zdenek Dohnal 978bc6
+   * optimising it. Namely, a vertical scroll that neatly cuts the damage
Zdenek Dohnal 978bc6
+   * region in half.
Zdenek Dohnal 978bc6
+   */
Zdenek Dohnal 978bc6
+  else if(rect.start_col <= screen->damaged.start_col &&
Zdenek Dohnal 978bc6
+          rect.end_col   >= screen->damaged.end_col &&
Zdenek Dohnal 978bc6
+          rightward == 0) {
Zdenek Dohnal 978bc6
+    if(screen->damaged.start_row >= rect.start_row &&
Zdenek Dohnal 978bc6
+       screen->damaged.start_row  < rect.end_row) {
Zdenek Dohnal 978bc6
+      screen->damaged.start_row -= downward;
Zdenek Dohnal 978bc6
+      if(screen->damaged.start_row < rect.start_row)
Zdenek Dohnal 978bc6
+        screen->damaged.start_row = rect.start_row;
Zdenek Dohnal 978bc6
+      if(screen->damaged.start_row > rect.end_row)
Zdenek Dohnal 978bc6
+        screen->damaged.start_row = rect.end_row;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+    if(screen->damaged.end_row >= rect.start_row &&
Zdenek Dohnal 978bc6
+       screen->damaged.end_row  < rect.end_row) {
Zdenek Dohnal 978bc6
+      screen->damaged.end_row -= downward;
Zdenek Dohnal 978bc6
+      if(screen->damaged.end_row < rect.start_row)
Zdenek Dohnal 978bc6
+        screen->damaged.end_row = rect.start_row;
Zdenek Dohnal 978bc6
+      if(screen->damaged.end_row > rect.end_row)
Zdenek Dohnal 978bc6
+        screen->damaged.end_row = rect.end_row;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+  else {
Zdenek Dohnal 978bc6
+    DEBUG_LOG2("TODO: Just flush and redo damaged=" STRFrect " rect=" STRFrect "\n",
Zdenek Dohnal 978bc6
+        ARGSrect(screen->damaged), ARGSrect(rect));
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->callbacks && screen->callbacks->movecursor)
Zdenek Dohnal 978bc6
+    return (*screen->callbacks->movecursor)(pos, oldpos, visible, screen->cbdata);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 0;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int setpenattr(VTermAttr attr, VTermValue *val, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  switch(attr) {
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_BOLD:
Zdenek Dohnal 978bc6
+    screen->pen.bold = val->boolean;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_UNDERLINE:
Zdenek Dohnal 978bc6
+    screen->pen.underline = val->number;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_ITALIC:
Zdenek Dohnal 978bc6
+    screen->pen.italic = val->boolean;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_BLINK:
Zdenek Dohnal 978bc6
+    screen->pen.blink = val->boolean;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_REVERSE:
Zdenek Dohnal 978bc6
+    screen->pen.reverse = val->boolean;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_STRIKE:
Zdenek Dohnal 978bc6
+    screen->pen.strike = val->boolean;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_FONT:
Zdenek Dohnal 978bc6
+    screen->pen.font = val->number;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_FOREGROUND:
Zdenek Dohnal 978bc6
+    screen->pen.fg = val->color;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  case VTERM_ATTR_BACKGROUND:
Zdenek Dohnal 978bc6
+    screen->pen.bg = val->color;
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  case VTERM_N_ATTRS:
Zdenek Dohnal 978bc6
+    return 0;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 0;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int settermprop(VTermProp prop, VTermValue *val, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  switch(prop) {
Zdenek Dohnal 978bc6
+  case VTERM_PROP_ALTSCREEN:
Zdenek Dohnal 978bc6
+    if(val->boolean && !screen->buffers[1])
Zdenek Dohnal 978bc6
+      return 0;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    screen->buffer = val->boolean ? screen->buffers[1] : screen->buffers[0];
Zdenek Dohnal 978bc6
+    /* only send a damage event on disable; because during enable there's an
Zdenek Dohnal 978bc6
+     * erase that sends a damage anyway
Zdenek Dohnal 978bc6
+     */
Zdenek Dohnal 978bc6
+    if(!val->boolean)
Zdenek Dohnal 978bc6
+      damagescreen(screen);
Zdenek Dohnal 978bc6
+    break;
Zdenek Dohnal 978bc6
+  case VTERM_PROP_REVERSE:
Zdenek Dohnal 978bc6
+    screen->global_reverse = val->boolean;
Zdenek Dohnal 978bc6
+    damagescreen(screen);
Zdenek Dohnal 978bc6
+    break;
Zdenek Dohnal 978bc6
+  default:
Zdenek Dohnal 978bc6
+    ; /* ignore */
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->callbacks && screen->callbacks->settermprop)
Zdenek Dohnal 978bc6
+    return (*screen->callbacks->settermprop)(prop, val, screen->cbdata);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int bell(void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->callbacks && screen->callbacks->bell)
Zdenek Dohnal 978bc6
+    return (*screen->callbacks->bell)(screen->cbdata);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 0;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int resize(int new_rows, int new_cols, VTermPos *delta, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  int is_altscreen = (screen->buffers[1] && screen->buffer == screen->buffers[1]);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  int old_rows = screen->rows;
Zdenek Dohnal 978bc6
+  int old_cols = screen->cols;
Zdenek Dohnal 978bc6
+  int first_blank_row;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(!is_altscreen && new_rows < old_rows) {
Zdenek Dohnal 978bc6
+    // Fewer rows - determine if we're going to scroll at all, and if so, push
Zdenek Dohnal 978bc6
+    // those lines to scrollback
Zdenek Dohnal 978bc6
+    VTermPos pos = { 0, 0 };
Zdenek Dohnal 978bc6
+    VTermPos cursor = screen->state->pos;
Zdenek Dohnal 978bc6
+    // Find the first blank row after the cursor.
Zdenek Dohnal 978bc6
+    for(pos.row = old_rows - 1; pos.row >= new_rows; pos.row--)
Zdenek Dohnal 978bc6
+      if(!vterm_screen_is_eol(screen, pos) || cursor.row == pos.row)
Zdenek Dohnal 978bc6
+        break;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    first_blank_row = pos.row + 1;
Zdenek Dohnal 978bc6
+    if(first_blank_row > new_rows) {
Zdenek Dohnal 978bc6
+      VTermRect rect = {0,0,0,0};
Zdenek Dohnal 978bc6
+      rect.end_row   = old_rows;
Zdenek Dohnal 978bc6
+      rect.end_col   = old_cols;
Zdenek Dohnal 978bc6
+      scrollrect(rect, first_blank_row - new_rows, 0, user);
Zdenek Dohnal 978bc6
+      vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+      delta->row -= first_blank_row - new_rows;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->buffers[0] = realloc_buffer(screen, screen->buffers[0], new_rows, new_cols);
Zdenek Dohnal 978bc6
+  if(screen->buffers[1])
Zdenek Dohnal 978bc6
+    screen->buffers[1] = realloc_buffer(screen, screen->buffers[1], new_rows, new_cols);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->buffer = is_altscreen ? screen->buffers[1] : screen->buffers[0];
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->rows = new_rows;
Zdenek Dohnal 978bc6
+  screen->cols = new_cols;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->sb_buffer)
Zdenek Dohnal 978bc6
+    vterm_allocator_free(screen->vt, screen->sb_buffer);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->sb_buffer = vterm_allocator_malloc(screen->vt, sizeof(VTermScreenCell) * new_cols);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(new_cols > old_cols) {
Zdenek Dohnal 978bc6
+    VTermRect rect;
Zdenek Dohnal 978bc6
+    rect.start_row = 0;
Zdenek Dohnal 978bc6
+    rect.end_row   = old_rows;
Zdenek Dohnal 978bc6
+    rect.start_col = old_cols;
Zdenek Dohnal 978bc6
+    rect.end_col   = new_cols;
Zdenek Dohnal 978bc6
+    damagerect(screen, rect);
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(new_rows > old_rows) {
Zdenek Dohnal 978bc6
+    if(!is_altscreen && screen->callbacks && screen->callbacks->sb_popline) {
Zdenek Dohnal 978bc6
+      int rows = new_rows - old_rows;
Zdenek Dohnal 978bc6
+      while(rows) {
Zdenek Dohnal 978bc6
+        VTermRect rect = {0,0,0,0};
Zdenek Dohnal 978bc6
+        VTermPos pos = { 0, 0 };
Zdenek Dohnal 978bc6
+        if(!(screen->callbacks->sb_popline(screen->cols, screen->sb_buffer, screen->cbdata)))
Zdenek Dohnal 978bc6
+          break;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+	rect.end_row   = screen->rows;
Zdenek Dohnal 978bc6
+	rect.end_col   = screen->cols;
Zdenek Dohnal 978bc6
+        scrollrect(rect, -1, 0, user);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+        for(pos.col = 0; pos.col < screen->cols; pos.col += screen->sb_buffer[pos.col].width)
Zdenek Dohnal 978bc6
+          vterm_screen_set_cell(screen, pos, screen->sb_buffer + pos.col);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+        rect.end_row = 1;
Zdenek Dohnal 978bc6
+        damagerect(screen, rect);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+        vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+        rows--;
Zdenek Dohnal 978bc6
+        delta->row++;
Zdenek Dohnal 978bc6
+      }
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    {
Zdenek Dohnal 978bc6
+      VTermRect rect;
Zdenek Dohnal 978bc6
+      rect.start_row = old_rows;
Zdenek Dohnal 978bc6
+      rect.end_row   = new_rows;
Zdenek Dohnal 978bc6
+      rect.start_col = 0;
Zdenek Dohnal 978bc6
+      rect.end_col   = new_cols;
Zdenek Dohnal 978bc6
+      damagerect(screen, rect);
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->callbacks && screen->callbacks->resize)
Zdenek Dohnal 978bc6
+    return (*screen->callbacks->resize)(new_rows, new_cols, screen->cbdata);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int setlineinfo(int row, const VTermLineInfo *newinfo, const VTermLineInfo *oldinfo, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermScreen *screen = user;
Zdenek Dohnal 978bc6
+  int col;
Zdenek Dohnal 978bc6
+  VTermRect rect;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(newinfo->doublewidth != oldinfo->doublewidth ||
Zdenek Dohnal 978bc6
+     newinfo->doubleheight != oldinfo->doubleheight) {
Zdenek Dohnal 978bc6
+    for(col = 0; col < screen->cols; col++) {
Zdenek Dohnal 978bc6
+      ScreenCell *cell = getcell(screen, row, col);
Zdenek Dohnal 978bc6
+      cell->pen.dwl = newinfo->doublewidth;
Zdenek Dohnal 978bc6
+      cell->pen.dhl = newinfo->doubleheight;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    rect.start_row = row;
Zdenek Dohnal 978bc6
+    rect.end_row   = row + 1;
Zdenek Dohnal 978bc6
+    rect.start_col = 0;
Zdenek Dohnal 978bc6
+    rect.end_col   = newinfo->doublewidth ? screen->cols / 2 : screen->cols;
Zdenek Dohnal 978bc6
+    damagerect(screen, rect);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    if(newinfo->doublewidth) {
Zdenek Dohnal 978bc6
+      rect.start_col = screen->cols / 2;
Zdenek Dohnal 978bc6
+      rect.end_col   = screen->cols;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+      erase_internal(rect, 0, user);
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static VTermStateCallbacks state_cbs = {
Zdenek Dohnal 978bc6
+  &putglyph, /* putglyph */
Zdenek Dohnal 978bc6
+  &movecursor, /* movecursor */
Zdenek Dohnal 978bc6
+  &scrollrect, /* scrollrect */
Zdenek Dohnal 978bc6
+  NULL, /* moverect */
Zdenek Dohnal 978bc6
+  &erase, /* erase */
Zdenek Dohnal 978bc6
+  NULL, /* initpen */
Zdenek Dohnal 978bc6
+  &setpenattr, /* setpenattr */
Zdenek Dohnal 978bc6
+  &settermprop, /* settermprop */
Zdenek Dohnal 978bc6
+  &bell, /* bell */
Zdenek Dohnal 978bc6
+  &resize, /* resize */
Zdenek Dohnal 978bc6
+  &setlineinfo /* setlineinfo */
Zdenek Dohnal 978bc6
+};
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static VTermScreen *screen_new(VTerm *vt)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  VTermState *state = vterm_obtain_state(vt);
Zdenek Dohnal 978bc6
+  VTermScreen *screen;
Zdenek Dohnal 978bc6
+  int rows, cols;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(!state)
Zdenek Dohnal 978bc6
+    return NULL;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen = vterm_allocator_malloc(vt, sizeof(VTermScreen));
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  vterm_get_size(vt, &rows, &cols;;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->vt = vt;
Zdenek Dohnal 978bc6
+  screen->state = state;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->damage_merge = VTERM_DAMAGE_CELL;
Zdenek Dohnal 978bc6
+  screen->damaged.start_row = -1;
Zdenek Dohnal 978bc6
+  screen->pending_scrollrect.start_row = -1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->rows = rows;
Zdenek Dohnal 978bc6
+  screen->cols = cols;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->callbacks = NULL;
Zdenek Dohnal 978bc6
+  screen->cbdata    = NULL;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->buffers[0] = realloc_buffer(screen, NULL, rows, cols);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->buffer = screen->buffers[0];
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  screen->sb_buffer = vterm_allocator_malloc(screen->vt, sizeof(VTermScreenCell) * cols);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  vterm_state_set_callbacks(screen->state, &state_cbs, screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return screen;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+INTERNAL void vterm_screen_free(VTermScreen *screen)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  vterm_allocator_free(screen->vt, screen->buffers[0]);
Zdenek Dohnal 978bc6
+  if(screen->buffers[1])
Zdenek Dohnal 978bc6
+    vterm_allocator_free(screen->vt, screen->buffers[1]);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  vterm_allocator_free(screen->vt, screen->sb_buffer);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  vterm_allocator_free(screen->vt, screen);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void vterm_screen_reset(VTermScreen *screen, int hard)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  screen->damaged.start_row = -1;
Zdenek Dohnal 978bc6
+  screen->pending_scrollrect.start_row = -1;
Zdenek Dohnal 978bc6
+  vterm_state_reset(screen->state, hard);
Zdenek Dohnal 978bc6
+  vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static size_t _get_chars(const VTermScreen *screen, const int utf8, void *buffer, size_t len, const VTermRect rect)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  size_t outpos = 0;
Zdenek Dohnal 978bc6
+  int padding = 0;
Zdenek Dohnal 978bc6
+  int row, col;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+#define PUT(c)                                             \
Zdenek Dohnal 978bc6
+  if(utf8) {                                               \
Zdenek Dohnal 978bc6
+    size_t thislen = utf8_seqlen(c);                       \
Zdenek Dohnal 978bc6
+    if(buffer && outpos + thislen <= len)                  \
Zdenek Dohnal 978bc6
+      outpos += fill_utf8((c), (char *)buffer + outpos);   \
Zdenek Dohnal 978bc6
+    else                                                   \
Zdenek Dohnal 978bc6
+      outpos += thislen;                                   \
Zdenek Dohnal 978bc6
+  }                                                        \
Zdenek Dohnal 978bc6
+  else {                                                   \
Zdenek Dohnal 978bc6
+    if(buffer && outpos + 1 <= len)                        \
Zdenek Dohnal 978bc6
+      ((uint32_t*)buffer)[outpos++] = (c);                 \
Zdenek Dohnal 978bc6
+    else                                                   \
Zdenek Dohnal 978bc6
+      outpos++;                                            \
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(row = rect.start_row; row < rect.end_row; row++) {
Zdenek Dohnal 978bc6
+    for(col = rect.start_col; col < rect.end_col; col++) {
Zdenek Dohnal 978bc6
+      ScreenCell *cell = getcell(screen, row, col);
Zdenek Dohnal 978bc6
+      int i;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+      if(cell->chars[0] == 0)
Zdenek Dohnal 978bc6
+        // Erased cell, might need a space
Zdenek Dohnal 978bc6
+        padding++;
Zdenek Dohnal 978bc6
+      else if(cell->chars[0] == (uint32_t)-1)
Zdenek Dohnal 978bc6
+        // Gap behind a double-width char, do nothing
Zdenek Dohnal 978bc6
+        ;
Zdenek Dohnal 978bc6
+      else {
Zdenek Dohnal 978bc6
+        while(padding) {
Zdenek Dohnal 978bc6
+          PUT(UNICODE_SPACE);
Zdenek Dohnal 978bc6
+          padding--;
Zdenek Dohnal 978bc6
+        }
Zdenek Dohnal 978bc6
+        for(i = 0; i < VTERM_MAX_CHARS_PER_CELL && cell->chars[i]; i++) {
Zdenek Dohnal 978bc6
+          PUT(cell->chars[i]);
Zdenek Dohnal 978bc6
+        }
Zdenek Dohnal 978bc6
+      }
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    if(row < rect.end_row - 1) {
Zdenek Dohnal 978bc6
+      PUT(UNICODE_LINEFEED);
Zdenek Dohnal 978bc6
+      padding = 0;
Zdenek Dohnal 978bc6
+    }
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return outpos;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+size_t vterm_screen_get_chars(const VTermScreen *screen, uint32_t *chars, size_t len, const VTermRect rect)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  return _get_chars(screen, 0, chars, len, rect);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+size_t vterm_screen_get_text(const VTermScreen *screen, char *str, size_t len, const VTermRect rect)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  return _get_chars(screen, 1, str, len, rect);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+/* Copy internal to external representation of a screen cell */
Zdenek Dohnal 978bc6
+int vterm_screen_get_cell(const VTermScreen *screen, VTermPos pos, VTermScreenCell *cell)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  ScreenCell *intcell = getcell(screen, pos.row, pos.col);
Zdenek Dohnal 978bc6
+  int i;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(!intcell)
Zdenek Dohnal 978bc6
+    return 0;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(i = 0; ; i++) {
Zdenek Dohnal 978bc6
+    cell->chars[i] = intcell->chars[i];
Zdenek Dohnal 978bc6
+    if(!intcell->chars[i])
Zdenek Dohnal 978bc6
+      break;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  cell->attrs.bold      = intcell->pen.bold;
Zdenek Dohnal 978bc6
+  cell->attrs.underline = intcell->pen.underline;
Zdenek Dohnal 978bc6
+  cell->attrs.italic    = intcell->pen.italic;
Zdenek Dohnal 978bc6
+  cell->attrs.blink     = intcell->pen.blink;
Zdenek Dohnal 978bc6
+  cell->attrs.reverse   = intcell->pen.reverse ^ screen->global_reverse;
Zdenek Dohnal 978bc6
+  cell->attrs.strike    = intcell->pen.strike;
Zdenek Dohnal 978bc6
+  cell->attrs.font      = intcell->pen.font;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  cell->attrs.dwl = intcell->pen.dwl;
Zdenek Dohnal 978bc6
+  cell->attrs.dhl = intcell->pen.dhl;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  cell->fg = intcell->pen.fg;
Zdenek Dohnal 978bc6
+  cell->bg = intcell->pen.bg;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(pos.col < (screen->cols - 1) &&
Zdenek Dohnal 978bc6
+     getcell(screen, pos.row, pos.col + 1)->chars[0] == (uint32_t)-1)
Zdenek Dohnal 978bc6
+    cell->width = 2;
Zdenek Dohnal 978bc6
+  else
Zdenek Dohnal 978bc6
+    cell->width = 1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+/* Copy external to internal representation of a screen cell */
Zdenek Dohnal 978bc6
+/* static because it's only used internally for sb_popline during resize */
Zdenek Dohnal 978bc6
+static int vterm_screen_set_cell(VTermScreen *screen, VTermPos pos, const VTermScreenCell *cell)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  ScreenCell *intcell = getcell(screen, pos.row, pos.col);
Zdenek Dohnal 978bc6
+  int i;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(!intcell)
Zdenek Dohnal 978bc6
+    return 0;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(i = 0; ; i++) {
Zdenek Dohnal 978bc6
+    intcell->chars[i] = cell->chars[i];
Zdenek Dohnal 978bc6
+    if(!cell->chars[i])
Zdenek Dohnal 978bc6
+      break;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  intcell->pen.bold      = cell->attrs.bold;
Zdenek Dohnal 978bc6
+  intcell->pen.underline = cell->attrs.underline;
Zdenek Dohnal 978bc6
+  intcell->pen.italic    = cell->attrs.italic;
Zdenek Dohnal 978bc6
+  intcell->pen.blink     = cell->attrs.blink;
Zdenek Dohnal 978bc6
+  intcell->pen.reverse   = cell->attrs.reverse ^ screen->global_reverse;
Zdenek Dohnal 978bc6
+  intcell->pen.strike    = cell->attrs.strike;
Zdenek Dohnal 978bc6
+  intcell->pen.font      = cell->attrs.font;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  intcell->pen.fg = cell->fg;
Zdenek Dohnal 978bc6
+  intcell->pen.bg = cell->bg;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(cell->width == 2)
Zdenek Dohnal 978bc6
+    getcell(screen, pos.row, pos.col + 1)->chars[0] = (uint32_t)-1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+int vterm_screen_is_eol(const VTermScreen *screen, VTermPos pos)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  /* This cell is EOL if this and every cell to the right is black */
Zdenek Dohnal 978bc6
+  for(; pos.col < screen->cols; pos.col++) {
Zdenek Dohnal 978bc6
+    ScreenCell *cell = getcell(screen, pos.row, pos.col);
Zdenek Dohnal 978bc6
+    if(cell->chars[0] != 0)
Zdenek Dohnal 978bc6
+      return 0;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+VTermScreen *vterm_obtain_screen(VTerm *vt)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  if(!vt->screen)
Zdenek Dohnal 978bc6
+    vt->screen = screen_new(vt);
Zdenek Dohnal 978bc6
+  return vt->screen;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void vterm_screen_enable_altscreen(VTermScreen *screen, int altscreen)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(!screen->buffers[1] && altscreen) {
Zdenek Dohnal 978bc6
+    int rows, cols;
Zdenek Dohnal 978bc6
+    vterm_get_size(screen->vt, &rows, &cols;;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    screen->buffers[1] = realloc_buffer(screen, NULL, rows, cols);
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void vterm_screen_set_callbacks(VTermScreen *screen, const VTermScreenCallbacks *callbacks, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  screen->callbacks = callbacks;
Zdenek Dohnal 978bc6
+  screen->cbdata = user;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void *vterm_screen_get_cbdata(VTermScreen *screen)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  return screen->cbdata;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void vterm_screen_set_unrecognised_fallbacks(VTermScreen *screen, const VTermParserCallbacks *fallbacks, void *user)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  vterm_state_set_unrecognised_fallbacks(screen->state, fallbacks, user);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void *vterm_screen_get_unrecognised_fbdata(VTermScreen *screen)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  return vterm_state_get_unrecognised_fbdata(screen->state);
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void vterm_screen_flush_damage(VTermScreen *screen)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  if(screen->pending_scrollrect.start_row != -1) {
Zdenek Dohnal 978bc6
+    vterm_scroll_rect(screen->pending_scrollrect, screen->pending_scroll_downward, screen->pending_scroll_rightward,
Zdenek Dohnal 978bc6
+        moverect_user, erase_user, screen);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    screen->pending_scrollrect.start_row = -1;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(screen->damaged.start_row != -1) {
Zdenek Dohnal 978bc6
+    if(screen->callbacks && screen->callbacks->damage)
Zdenek Dohnal 978bc6
+      (*screen->callbacks->damage)(screen->damaged, screen->cbdata);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+    screen->damaged.start_row = -1;
Zdenek Dohnal 978bc6
+  }
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+void vterm_screen_set_damage_merge(VTermScreen *screen, VTermDamageSize size)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  vterm_screen_flush_damage(screen);
Zdenek Dohnal 978bc6
+  screen->damage_merge = size;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+static int attrs_differ(VTermAttrMask attrs, ScreenCell *a, ScreenCell *b)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_BOLD_MASK)       && (a->pen.bold != b->pen.bold))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_UNDERLINE_MASK)  && (a->pen.underline != b->pen.underline))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_ITALIC_MASK)     && (a->pen.italic != b->pen.italic))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_BLINK_MASK)      && (a->pen.blink != b->pen.blink))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_REVERSE_MASK)    && (a->pen.reverse != b->pen.reverse))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_STRIKE_MASK)     && (a->pen.strike != b->pen.strike))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_FONT_MASK)       && (a->pen.font != b->pen.font))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_FOREGROUND_MASK) && !vterm_color_equal(a->pen.fg, b->pen.fg))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+  if((attrs & VTERM_ATTR_BACKGROUND_MASK) && !vterm_color_equal(a->pen.bg, b->pen.bg))
Zdenek Dohnal 978bc6
+    return 1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 0;
Zdenek Dohnal 978bc6
+}
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+int vterm_screen_get_attrs_extent(const VTermScreen *screen, VTermRect *extent, VTermPos pos, VTermAttrMask attrs)
Zdenek Dohnal 978bc6
+{
Zdenek Dohnal 978bc6
+  int col;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  ScreenCell *target = getcell(screen, pos.row, pos.col);
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  // TODO: bounds check
Zdenek Dohnal 978bc6
+  extent->start_row = pos.row;
Zdenek Dohnal 978bc6
+  extent->end_row   = pos.row + 1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  if(extent->start_col < 0)
Zdenek Dohnal 978bc6
+    extent->start_col = 0;
Zdenek Dohnal 978bc6
+  if(extent->end_col < 0)
Zdenek Dohnal 978bc6
+    extent->end_col = screen->cols;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(col = pos.col - 1; col >= extent->start_col; col--)
Zdenek Dohnal 978bc6
+    if(attrs_differ(attrs, target, getcell(screen, pos.row, col)))
Zdenek Dohnal 978bc6
+      break;
Zdenek Dohnal 978bc6
+  extent->start_col = col + 1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  for(col = pos.col + 1; col < extent->end_col; col++)
Zdenek Dohnal 978bc6
+    if(attrs_differ(attrs, target, getcell(screen, pos.row, col)))
Zdenek Dohnal 978bc6
+      break;
Zdenek Dohnal 978bc6
+  extent->end_col = col - 1;
Zdenek Dohnal 978bc6
+
Zdenek Dohnal 978bc6
+  return 1;
Zdenek Dohnal 978bc6
+}